Advertisement
incinirate

StarField Build

Jun 1st, 2016
488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 20.27 KB | None | 0 0
  1. --  Hideously Smashed Together by Compilr, a Hideous Smash-Stuff-Togetherer, (c) 2014 oeed  --
  2.  
  3. --  This file REALLLLLLLY isn't suitable to be used for anything other than being executed --
  4.  
  5. --  To extract all the files, run: "<filename> --extract" in the Shell --
  6.  
  7.  
  8.  
  9.  
  10.  
  11. --DONT WORRY ABOUT
  12. --ANYTHING BELOW HERE
  13. --JUST RUN THIS FILE AS
  14. --> build --extract
  15. --THEN RUN STARTUP
  16. --(or main.lua if
  17. --your a fancy)
  18.  
  19.  
  20.  
  21. local files = {
  22.   [ "rgb.lua" ] = "-- RGB API version 1.0 by CrazedProgrammer\
  23. -- You can find info and documentation on these pages:\
  24. -- http://cp.msdev.nl/computercraft/rgb-api/\
  25. -- You may use this in your ComputerCraft programs and modify it without asking.\
  26. -- However, you may not publish this API under your name without asking me.\
  27. -- If you have any suggestions, bug reports or questions then please send an email to:\
  28. -- crazedprogrammer@gmail.com\
  29. local hex = {\"F0F0F0\", \"F2B233\", \"E57FD8\", \"99B2F2\", \"DEDE6C\", \"7FCC19\", \"F2B2CC\", \"4C4C4C\", \"999999\", \"4C99B2\", \"B266E5\", \"3366CC\", \"7F664C\", \"57A64E\", \"CC4C4C\", \"191919\"}\
  30. local rgb = {}\
  31. \
  32. local frgb_memory = {}\
  33. \
  34. for i=1,16,1 do\
  35.  rgb[i] = {tonumber(hex[i]:sub(1, 2), 16), tonumber(hex[i]:sub(3, 4), 16), tonumber(hex[i]:sub(5, 6), 16)}\
  36. end\
  37. local rgb2 = {}\
  38. for i=1,16,1 do\
  39.  rgb2[i] = {}\
  40.  for j=1,16,1 do\
  41.    rgb2[i][j] = {(rgb[i][1] * 34 + rgb[j][1] * 20) / 54, (rgb[i][2] * 34 + rgb[j][2] * 20) / 54, (rgb[i][3] * 34 + rgb[j][3] * 20) / 54}\
  42.  end\
  43. end\
  44. \
  45. colors.fromRGB = function (r, g, b)\
  46.  local sav = bit.blshift( ( bit.blshift( r, 8) + g ), 8) + b\
  47.  if frgb_memory[sav] then return frgb_memory[sav] end\
  48.  local dist = 1e100\
  49.  local d = 1e100\
  50.  local color = -1\
  51.  for i=1,16,1 do\
  52.    d = math.sqrt((math.max(rgb[i][1], r) - math.min(rgb[i][1], r)) ^ 2 + (math.max(rgb[i][2], g) - math.min(rgb[i][2], g)) ^ 2 + (math.max(rgb[i][3], b) - math.min(rgb[i][3], b)) ^ 2)\
  53.    if d < dist then\
  54.      dist = d\
  55.      color = i - 1\
  56.    end\
  57.  end\
  58.  frgb_memory[sav] = 2 ^ color\
  59.  return 2 ^ color\
  60. end\
  61. \
  62. colors.toRGB = function(color)\
  63.  return unpack(rgb[math.floor(math.log(color) / math.log(2) + 1)])\
  64. end\
  65. \
  66. colors.fromRGB2 = function (r, g, b)\
  67.  local dist = 1e100\
  68.  local d = 1e100\
  69.  local color1 = -1\
  70.  local color2 = -1\
  71.  for i=1,16,1 do\
  72.    for j=1,16,1 do\
  73.      d = math.sqrt((math.max(rgb2[i][j][1], r) - math.min(rgb2[i][j][1], r)) ^ 2 + (math.max(rgb2[i][j][2], g) - math.min(rgb2[i][j][2], g)) ^ 2 + (math.max(rgb2[i][j][3], b) - math.min(rgb2[i][j][3], b)) ^ 2)\
  74.      if d < dist then\
  75.        dist = d\
  76.        color1 = i - 1\
  77.        color2 = j - 1\
  78.      end\
  79.    end\
  80.  end\
  81.  return 2 ^ color1, 2 ^ color2\
  82. end\
  83. \
  84. colors.toRGB2 = function(color1, color2, str)\
  85.  local c1 = math.floor(math.log(color1) / math.log(2) + 1)\
  86.  local c2 = math.floor(math.log(color2) / math.log(2) + 1)\
  87.  return math.floor(rgb2[c1][c2][1]), math.floor(rgb2[c1][c2][2]), math.floor(rgb2[c1][c2][3])\
  88. end\
  89. \
  90. colours.fromRGB = colors.fromRGB\
  91. colours.toRGB = colors.toRGB\
  92. colours.fromRGB2 = colors.fromRGB2\
  93. colours.toRGB2 = colors.toRGB2",
  94.   [ "main.lua" ] = "dofile(\"display.lua\")\
  95. dofile(\"canvas.lua\")\
  96. dofile(\"rgb.lua\")\
  97. dofile(\"bitmap.lua\")\
  98. --^^ MAIN FILES ^^\
  99. \
  100. dofile(\"stars3D.lua\")\
  101. --^^ PROGRAMS ^^\
  102. \
  103. \
  104. local display = Display()\
  105. \
  106. --[[display:drawLine(5,4,11,11)\
  107. display:drawLine(5,11,11,4)\
  108. \
  109. display:setSize(102,57)\
  110. \
  111. local bitmapTest = Bitmap(102,57)\
  112. \
  113. bitmapTest:setBG_RGB(127,127,127)\
  114. \
  115. bitmapTest:clear(127)\
  116. bitmapTest:drawPixel(4,4,255,0,0)\
  117. bitmapTest:drawPixel(5,4,255,0,0)\
  118. bitmapTest:drawPixel(5,5,255,0,0)\
  119. bitmapTest:drawPixel(4,5,255,0,0)\
  120. bitmapTest:drawPixel(4,6,255,0,0)\
  121. bitmapTest:drawPixel(5,6,255,0,0)\
  122. bitmapTest:drawPixel(6,5,255,0,0)\
  123. \
  124. display:drawImage(bitmapTest,0,0)\
  125. \
  126. \
  127. local happy = Bitmap.createBitmapFromFile(\"happy.bmp\")\
  128. happy:setBG_C(colors.black)\
  129. display:drawImage(happy,6,12)\
  130. \
  131. local happy2 = Bitmap.createBitmapFromFile(\"happy2.bmp\")\
  132. happy2:setBG_C(colors.black)\
  133. display:drawImage(happy2,6,30)\
  134. \
  135. local testBmp = Bitmap.createBitmapFromFile(\"test.bmp\")\
  136. testBmp:setBG_C(colors.black)\
  137. display:drawImage(testBmp,20,6)\
  138. \
  139. os.pullEvent(\"char\") --255 242 82 <<DELETE THIS COMMENT>> 255 240 43 ]]\
  140. \
  141. \
  142. \
  143. \
  144. local stars = Stars3D(200, 1, 0.5)\
  145. local stars_target = Bitmap(102,57)\
  146. stars_target:setBG_C(colors.black)\
  147. \
  148. local previousTime = os.clock()\
  149. local yieldCatch = 0\
  150. while true do\
  151.  local currentTime = os.clock()\
  152.  local delta = currentTime - previousTime\
  153.  yieldCatch = yieldCatch + 1\
  154.  \
  155.  stars:updateAndRender(stars_target, delta)\
  156.  \
  157.  display:drawImage(stars_target,0,0)\
  158.  display:flush()\
  159.  previousTime = currentTime\
  160.  if yieldCatch > 6 then\
  161.    sleep(0)\
  162.  end\
  163. end\
  164. \
  165. os.pullEvent(\"char\")",
  166.   startup = "shell.run(\"main.lua\")",
  167.   [ "canvas.lua" ] = "Character = {}\
  168. Character.__index = Character\
  169. \
  170. function Character.create()\
  171. \009local self = {}\
  172. \009setmetatable(self, Character)\
  173. \009\
  174. \009self.textColor = colors.white\
  175. \009self.backgroundColor = colors.black\
  176. \009self.pixelMode = false\
  177. \009self.character = \" \"\
  178. \009self.pixel = {{false, false, false},{false, false, false}}\
  179. \009self.invert = false\
  180. \009return self\
  181. end\
  182. \
  183. function Character:update()\
  184. \009if self.pixelMode then\
  185. \009\009local char = 128\
  186. \009\009if not self.pixel[2][3] then\
  187. \009\009\009char = char + (self.pixel[1][1] and 1 or 0)\
  188. \009\009\009char = char + (self.pixel[2][1] and 2 or 0)\
  189. \009\009\009char = char + (self.pixel[1][2] and 4 or 0)\
  190. \009\009\009char = char + (self.pixel[2][2] and 8 or 0)\
  191. \009\009\009char = char + (self.pixel[1][3] and 16 or 0)\
  192. \009\009\009self.invert = false\
  193. \009\009else\
  194. \009\009\009char = char + (self.pixel[1][1] and 0 or 1)\
  195. \009\009\009char = char + (self.pixel[2][1] and 0 or 2)\
  196. \009\009\009char = char + (self.pixel[1][2] and 0 or 4)\
  197. \009\009\009char = char + (self.pixel[2][2] and 0 or 8)\
  198. \009\009\009char = char + (self.pixel[1][3] and 0 or 16)\
  199. \009\009\009self.invert = true\
  200. \009\009end\
  201. \009\009self.character = string.char(char)\
  202. \009end\
  203. end\
  204. \
  205. function Character:draw()\
  206. \009if not self.invert then\
  207. \009\009term.setBackgroundColor(self.backgroundColor)\
  208. \009\009term.setTextColor(self.textColor)\
  209. \009else\
  210. \009\009term.setBackgroundColor(self.textColor)\
  211. \009\009term.setTextColor(self.backgroundColor)\
  212. \009end\
  213. \009term.write(self.character)\
  214. end\
  215. \
  216. Canvas = {}\
  217. Canvas.__index = Canvas\
  218. \
  219. function Canvas.create()\
  220. \009local self = {}\
  221. \009setmetatable (self, {__index=Canvas})\
  222. \009\
  223. \009self.x = 1\
  224. \009self.y = 1\
  225. \009self.character = {}\
  226. \009local w, h = term.getSize()\
  227. \009self:setSize(w, h)\
  228. \009\
  229. \009return self\
  230. end\
  231. \
  232. function Canvas:setSize(width, height)\
  233. \009self.__width = width\
  234. \009self.__height = height\
  235. \009self.character = {}\
  236. \009for i=1, self.__width do\
  237. \009\009self.character[i] = {}\
  238. \009\009for j=1, self.__height do\
  239. \009\009\009self.character[i][j] = Character.create()\
  240. \009\009end\
  241. \009end\
  242. end\
  243. \
  244. function Canvas:draw()\
  245. \009for j=self.y,(self.__height + self.y - 1) do\
  246. \009\009term.setCursorPos(self.x, j)\
  247. \009\009for i=self.x,(self.__width + self.x - 1) do\
  248. \009\009\009self.character[i][j]:draw()\
  249. \009\009end\
  250. \009end\
  251. end\
  252. \
  253. function Canvas:setPixel(x, y, value, bg, fg)\
  254. \009x = x - 1\
  255. \009y = y - 1\
  256. \009local charX = math.floor(x / 2)\
  257. \009local charY = math.floor(y / 3)\
  258. \009pixelX = x - charX * 2\
  259. \009pixelY = y - charY * 3\
  260. \009charX = charX + 1\
  261. \009charY = charY + 1\
  262.  if bg then self.character[charX][charY].backgroundColor = bg end\
  263.  if fg then self.character[charX][charY].textColor = fg end\
  264. \009self.character[charX][charY].pixelMode = true\
  265. \009self.character[charX][charY].pixel[pixelX + 1][pixelY + 1] = value;\
  266. \009self.character[charX][charY]:update()\
  267. end\
  268. \
  269. function Canvas:setCharacter(x, y, char)\
  270. \009self.character[charX][charY].invert = false\
  271. \009self.character[charX][charY].pixelMode = false\
  272. \009self.character[charX][charY].character = char;\
  273. end",
  274.   [ "display.lua" ] = "Display = {}\
  275. do\
  276.  local Display = _G.Display\
  277.  local math = _G.math\
  278.  \
  279.  function Display.__init__()\
  280.    local self={canvas=0}\
  281.    \
  282.    self.canvas = Canvas.create()\
  283.    \
  284.    setmetatable(self, {__index=Display})\
  285.    return self\
  286.  end\
  287.  \
  288.  setmetatable(Display, {__call=Display.__init__})\
  289.  \
  290.  function Display:drawLine(x1,y1,x2,y2,bg,fg) --Basically copied from paintutils\
  291.    bg = bg or colors.black\
  292.    fg = fg or colors.white\
  293.    \
  294.    x1,y1 = math.floor(x1),math.floor(y1)\
  295.    x2,y2 = math.floor(x2),math.floor(y2)\
  296.    \
  297.    if x1==x2 and y1==y2 then\
  298.      self.canvas:setPixel(x1,y1,true,bg,fg)\
  299.    end\
  300.    \
  301.    local minX = x1 > x2 and x2 or x1\
  302.    local minY,maxX,maxY\
  303.    if minX == x1 then\
  304.      minY = y1\
  305.      maxX = x2\
  306.      maxY = y2\
  307.    else\
  308.      minY = y2\
  309.      maxX = x1\
  310.      maxY = y1\
  311.    end\
  312.    \
  313.    local xDiff = x2 - x1\
  314.    local yDiff = y2 - y1\
  315.    \
  316.    if xDiff > ( yDiff < 0 and -yDiff or yDiff ) then --optimization bruh\
  317.      local y = minY\
  318.      local dy = yDiff / xDiff\
  319.      for x=minX,maxX do\
  320.        self.canvas:setPixel(x, math.floor( y + 0.5), true,bg,fg )\
  321.        y = y + dy\
  322.      end\
  323.    else\
  324.      local x = minX\
  325.      local dx = xDiff / yDiff\
  326.      if maxY >= minY then\
  327.        for y=minY,maxY do\
  328.          self.canvas:setPixel( math.floor( x + 0.5 ), y, true,bg,fg )\
  329.          x = x + dx\
  330.        end\
  331.      else\
  332.        for y=minY,maxY,-1 do\
  333.          self.canvas:setPixel( math.floor( x + 0.5 ), y, true,bg,fg )\
  334.          x = x - dx\
  335.        end\
  336.      end\
  337.    end\
  338.  end\
  339.  \
  340.  function Display:setSize(w,h)\
  341.    self.canvas:setSize(w,h)\
  342.  end\
  343.  \
  344.  --TODO: Add scaling for drawImage?\
  345.  function Display:drawImage(bitmap, x, y)\
  346.    for i=1,bitmap.width do\
  347.      local off = (i-1) * 3\
  348.      for j=1,bitmap.height do\
  349.        local offy = bitmap.width*(j-1)*3 + off\
  350.        local r = bitmap.components[offy + 1]\
  351.        local g = bitmap.components[offy + 2]\
  352.        local b = bitmap.components[offy + 3]\
  353.        --print(i..\";\"..j..\";\"..tostring(r)..g..b)\
  354.        local color = colors.fromRGB(r,g,b)\
  355.        if color == bitmap.bg then\
  356.          self.canvas:setPixel( i+x, j+y, false, bitmap.bg)\
  357.        else\
  358.          self.canvas:setPixel( i+x, j+y, true, bitmap.bg, color)\
  359.        end\
  360.      end\
  361.    end\
  362.  end\
  363.  \
  364.  function Display:flush()\
  365.    self.canvas:draw()\
  366.  end\
  367. end\
  368. \
  369. --local canvas = Canvas.create()\
  370. --canvas:setPixel(1,1,true)\
  371. --canvas:setPixel(3,3,true)\
  372. --canvas:setPixel(3,2,true, colors.red, colors.green)\
  373. --canvas:draw()\
  374. --os.pullEvent(\"char\")",
  375.   [ "stars3D.lua" ] = "Stars3D = {}\
  376. do\
  377.  local Stars3D = _G.Stars3D\
  378.  function Stars3D.__init__(_, numStars, spread, speed)\
  379.    local self = {}\
  380.    \
  381.    self.m_spread = spread\
  382.    self.m_speed  = speed\
  383.    \
  384.    self.m_starX  = {}\
  385.    self.m_starY  = {}\
  386.    self.m_starZ  = {}\
  387.    \
  388.    setmetatable(self, {__index=Stars3D})\
  389.    \
  390.    for i=1,numStars do\
  391.      self:InitStar(i)\
  392.    end\
  393.    \
  394.    return self\
  395.  end\
  396. \
  397.  setmetatable(Stars3D, {__call=Stars3D.__init__})\
  398.  \
  399.  --target is a bitmap object\
  400.  function Stars3D:updateAndRender(target, delta)\
  401.    target:clear(0)\
  402.    \
  403.    local halfWidth = target.width/2\
  404.    local halfHeight = target.height/2\
  405.    for i=1,#self.m_starX do\
  406.      self.m_starZ[i] = self.m_starZ[i] - (delta * self.m_speed)\
  407.      \
  408.      if self.m_starZ[i] <= 0 then\
  409.        self:InitStar(i)\
  410.      end\
  411.      \
  412.      local x = (self.m_starX[i]/self.m_starZ[i]) * halfWidth + halfWidth\
  413.      local y = (self.m_starY[i]/self.m_starZ[i]) * halfHeight + halfHeight\
  414.      \
  415.      if (x < 0 or x >= target.width) or (y < 0 or y >= target.height) then\
  416.        self:InitStar(i)\
  417.      else\
  418.        if self.m_starZ[i] > 0.60*self.m_spread then\
  419.          target:drawPixel(x, y, 76, 76, 76)\
  420.        elseif self.m_starZ[i] > 0.33*self.m_spread then\
  421.          target:drawPixel(x, y, 153, 153, 153)\
  422.        else\
  423.          target:drawPixel(x, y, 255, 255, 255)\
  424.        end\
  425.        --term.setCursorPos(1,19)\
  426.        --term.write(i..\": (\"..self.m_starX[i]..\",\"..y..\")\")\
  427.        --os.pullEvent(\"char\")\
  428.      end\
  429.    end\
  430.  end\
  431.  \
  432.  function Stars3D:InitStar(i)\
  433.    self.m_starX[i] = (math.random(-100,100)/100)  * self.m_spread\
  434.    self.m_starY[i] = (math.random(-100,100)/100)  * self.m_spread\
  435.    self.m_starZ[i] = (math.random(0.001,100)/100) * self.m_spread\
  436.  end\
  437. end",
  438.   [ "bitmap.lua" ] = "Bitmap = {}\
  439. do\
  440.  local Bitmap = _G.Bitmap\
  441. \
  442.  function Bitmap.createBitmapFromFile(filename)\
  443.    local handle = fs.open(filename, \"rb\")\
  444.    local header = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}\
  445.    for i=1,54 do\
  446.      local byteVal = handle.read()\
  447.      header[i] = byteVal\
  448.      write(\"[\"..i..\"]\"..byteVal)\
  449.    end\
  450.    print(\"\\nFile size = \"..header[5]*65536 + header[4]*256 + header[3]..\"\\n\")\
  451.    local offset = header[12]*256+header[11]\
  452.    print(\"Offset to image = \"..offset)\
  453.    local width = header[19]\
  454.    local height = header[23]\
  455.    \
  456.    local size = 3 * width * height\
  457.    \
  458.    local outdata = {}\
  459.    local padding = (4-((width*3)%4))%4\
  460.    for i=1,size do\
  461.      outdata[i] = handle.read()\
  462.      if i%(width*3)==0 then\
  463.        for i=1,padding do\
  464.          handle.read() --padding data we dont need or want\
  465.        end\
  466.      end\
  467.    end\
  468.    \
  469.    handle.close()\
  470.    \
  471.    for i=1, size, 3 do\
  472.      local tmp = outdata[i]\
  473.      outdata[i] = outdata[i+2]\
  474.      outdata[i+2] = tmp\
  475.    end\
  476.    \
  477.    local outmap = Bitmap(width, height)\
  478.    \
  479.    for i=1,width do\
  480.      for j=1,height do\
  481.      --write(\"{\"..happy[i]..\",\"..happy[i+1]..\",\"..happy[i+2]..\"},\")\
  482.        local poff = (j-1)*width*3 + (i-1)*3\
  483.        outmap:drawPixel(i,height-j+1,outdata[poff + 1],outdata[poff + 2],outdata[poff + 3]) --for some reason the bmps were inverted vertically?\
  484.      end\
  485.    end\
  486.    \
  487.    return outmap\
  488.  end\
  489. \
  490.  function Bitmap.__init__(_, nWidth, nHeight, bg)\
  491.    local self = {width = nWidth, height = nHeight,\
  492.      components = {}, bg = bg or colors.black}\
  493.    for i=1,(nWidth*nHeight*3) do\
  494.      self.components[i]=1\
  495.    end\
  496.    \
  497.    setmetatable(self, {__index=Bitmap})\
  498.    return self\
  499.  end\
  500. \
  501.  setmetatable(Bitmap, {__call=Bitmap.__init__})\
  502. \
  503.  --takes in a byte\
  504.  function Bitmap:clear(shade)\
  505.    for i=1,#self.components do\
  506.      self.components[i] = shade\
  507.    end\
  508.  end\
  509.  \
  510.  function Bitmap:setBG_C(bg)\
  511.    self.bg = bg\
  512.  end\
  513.  \
  514.  function Bitmap:setBG_RGB(r,g,b)\
  515.    self.bg = colors.fromRGB(r,g,b)\
  516.  end\
  517. \
  518.  function Bitmap:drawPixel(x,y,r,g,b)\
  519.    x,y = math.floor(x),math.floor(y)\
  520.    local index = ((x-1) + (y-1) * self.width) * 3 + 1\
  521.    self.components[index    ] = r\
  522.    self.components[index + 1] = g\
  523.    self.components[index + 2] = b\
  524.  end\
  525. end",
  526. }
  527.  
  528. local function run(tArgs)
  529.  
  530.   local fnFile, err = loadstring(files['startup'], 'startup')
  531.   if err then
  532.     error(err)
  533.   end
  534.  
  535.   local function split(str, pat)
  536.      local t = {}
  537.      local fpat = "(.-)" .. pat
  538.      local last_end = 1
  539.      local s, e, cap = str:find(fpat, 1)
  540.      while s do
  541.         if s ~= 1 or cap ~= "" then
  542.      table.insert(t,cap)
  543.         end
  544.         last_end = e+1
  545.         s, e, cap = str:find(fpat, last_end)
  546.      end
  547.      if last_end <= #str then
  548.         cap = str:sub(last_end)
  549.         table.insert(t, cap)
  550.      end
  551.      return t
  552.   end
  553.  
  554.   local function resolveTreeForPath(path, single)
  555.     local _files = files
  556.     local parts = split(path, '/')
  557.     if parts then
  558.       for i, v in ipairs(parts) do
  559.         if #v > 0 then
  560.           if _files[v] then
  561.             _files = _files[v]
  562.           else
  563.             _files = nil
  564.             break
  565.           end
  566.         end
  567.       end
  568.     elseif #path > 0 and path ~= '/' then
  569.       _files = _files[path]
  570.     end
  571.     if not single or type(_files) == 'string' then
  572.       return _files
  573.     end
  574.   end
  575.  
  576.   local oldFs = fs
  577.   local env
  578.   env = {
  579.     fs = {
  580.       list = function(path)
  581.               local list = {}
  582.               if fs.exists(path) then
  583.             list = fs.list(path)
  584.               end
  585.         for k, v in pairs(resolveTreeForPath(path)) do
  586.           if not fs.exists(path .. '/' ..k) then
  587.             table.insert(list, k)
  588.           end
  589.         end
  590.         return list
  591.       end,
  592.  
  593.       exists = function(path)
  594.         if fs.exists(path) then
  595.           return true
  596.         elseif resolveTreeForPath(path) then
  597.           return true
  598.         else
  599.           return false
  600.         end
  601.       end,
  602.  
  603.       isDir = function(path)
  604.         if fs.isDir(path) then
  605.           return true
  606.         else
  607.           local tree = resolveTreeForPath(path)
  608.           if tree and type(tree) == 'table' then
  609.             return true
  610.           else
  611.             return false
  612.           end
  613.         end
  614.       end,
  615.  
  616.       isReadOnly = function(path)
  617.         if not fs.isReadOnly(path) then
  618.           return false
  619.         else
  620.           return true
  621.         end
  622.       end,
  623.  
  624.       getName = fs.getName,
  625.  
  626.       getSize = fs.getSize,
  627.  
  628.       getFreespace = fs.getFreespace,
  629.  
  630.       makeDir = fs.makeDir,
  631.  
  632.       move = fs.move,
  633.  
  634.       copy = fs.copy,
  635.  
  636.       delete = fs.delete,
  637.  
  638.       combine = fs.combine,
  639.  
  640.       open = function(path, mode)
  641.         if fs.exists(path) then
  642.           return fs.open(path, mode)
  643.         elseif type(resolveTreeForPath(path)) == 'string' then
  644.           local handle = {close = function()end}
  645.           if mode == 'r' then
  646.             local content = resolveTreeForPath(path)
  647.             handle.readAll = function()
  648.               return content
  649.             end
  650.  
  651.             local line = 1
  652.             local lines = split(content, '\n')
  653.             handle.readLine = function()
  654.               if line > #lines then
  655.                 return nil
  656.               else
  657.                 return lines[line]
  658.               end
  659.               line = line + 1
  660.             end
  661.                       return handle
  662.           else
  663.             error('Cannot write to read-only file (compilr archived).')
  664.           end
  665.         else
  666.           return fs.open(path, mode)
  667.         end
  668.       end
  669.     },
  670.  
  671.     loadfile = function( _sFile )
  672.         local file = env.fs.open( _sFile, "r" )
  673.         if file then
  674.             local func, err = loadstring( file.readAll(), fs.getName( _sFile ) )
  675.             file.close()
  676.             return func, err
  677.         end
  678.         return nil, "File not found: ".._sFile
  679.     end,
  680.  
  681.     dofile = function( _sFile )
  682.         local fnFile, e = env.loadfile( _sFile )
  683.         if fnFile then
  684.             setfenv( fnFile, getfenv(2) )
  685.             return fnFile()
  686.         else
  687.             error( e, 2 )
  688.         end
  689.     end
  690.   }
  691.  
  692.   setmetatable( env, { __index = _G } )
  693.  
  694.   local tAPIsLoading = {}
  695.   env.os.loadAPI = function( _sPath )
  696.       local sName = fs.getName( _sPath )
  697.       if tAPIsLoading[sName] == true then
  698.           printError( "API "..sName.." is already being loaded" )
  699.           return false
  700.       end
  701.       tAPIsLoading[sName] = true
  702.          
  703.       local tEnv = {}
  704.       setmetatable( tEnv, { __index = env } )
  705.       local fnAPI, err = env.loadfile( _sPath )
  706.       if fnAPI then
  707.           setfenv( fnAPI, tEnv )
  708.           fnAPI()
  709.       else
  710.           printError( err )
  711.           tAPIsLoading[sName] = nil
  712.           return false
  713.       end
  714.      
  715.       local tAPI = {}
  716.       for k,v in pairs( tEnv ) do
  717.           tAPI[k] =  v
  718.       end
  719.      
  720.       env[sName] = tAPI    
  721.       tAPIsLoading[sName] = nil
  722.       return true
  723.   end
  724.  
  725.   env.shell = shell
  726.  
  727.   setfenv( fnFile, env )
  728.   fnFile(unpack(tArgs))
  729. end
  730.  
  731. local function extract()
  732.     local function node(path, tree)
  733.         if type(tree) == 'table' then
  734.             fs.makeDir(path)
  735.             for k, v in pairs(tree) do
  736.                 node(path .. '/' .. k, v)
  737.             end
  738.         else
  739.             local f = fs.open(path, 'w')
  740.             if f then
  741.                 f.write(tree)
  742.                 f.close()
  743.             end
  744.         end
  745.     end
  746.     node('', files)
  747. end
  748.  
  749. local tArgs = {...}
  750. if #tArgs == 1 and tArgs[1] == '--extract' then
  751.   extract()
  752. else
  753.   run(tArgs)
  754. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement