incinirate

StarField Build

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