Advertisement
Freack100

Program Builder

Oct 1st, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.22 KB | None | 0 0
  1. local c="{\
  2.  uuid = \"---------------------------------------------------------------------------------------\\\
  3. -- Copyright 2012 Rackspace (original), 2013 Thijs Schreijer (modifications)\\\
  4. -- \\\
  5. -- Licensed under the Apache License, Version 2.0 (the \\\"License\\\");\\\
  6. -- you may not use this file except in compliance with the License.\\\
  7. -- You may obtain a copy of the License at\\\
  8. -- \\\
  9. --     http://www.apache.org/licenses/LICENSE-2.0\\\
  10. -- \\\
  11. -- Unless required by applicable law or agreed to in writing, software\\\
  12. -- distributed under the License is distributed on an \\\"AS-IS\\\" BASIS,\\\
  13. -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\\\
  14. -- See the License for the specific language governing permissions and\\\
  15. -- limitations under the License.\\\
  16. -- \\\
  17. -- see http://www.ietf.org/rfc/rfc4122.txt\\\
  18. --\\\
  19. -- Note that this is not a true version 4 (random) UUID.  Since `os.time()` precision is only 1 second, it would be hard\\\
  20. -- to guarantee spacial uniqueness when two hosts generate a uuid after being seeded during the same second.  This\\\
  21. -- is solved by using the node field from a version 1 UUID.  It represents the mac address.\\\
  22. -- \\\
  23. -- 28-apr-2013 modified by Thijs Schreijer from the original [Rackspace code](https://github.com/kans/zirgo/blob/807250b1af6725bad4776c931c89a784c1e34db2/util/uuid.lua) as a generic Lua module.\\\
  24. -- Regarding the above mention on `os.time()`; the modifications use the `socket.gettime()` function from LuaSocket\\\
  25. -- if available and hence reduce that problem (provided LuaSocket has been loaded before uuid).\\\
  26. \\\
  27. local M = {}\\\
  28. \\\
  29. local bitsize = 32  -- bitsize assumed for Lua VM. See randomseed function below.\\\
  30. local lua_version = tonumber(_VERSION:match(\\\"%d%.*%d*\\\"))  -- grab Lua version used\\\
  31. \\\
  32. local MATRIX_AND = {{0,0},{0,1} }\\\
  33. local MATRIX_OR = {{0,1},{1,1}}\\\
  34. local HEXES = '0123456789abcdef'\\\
  35. \\\
  36. -- performs the bitwise operation specified by truth matrix on two numbers.\\\
  37. local function BITWISE(x, y, matrix)\\\
  38.  local z = 0\\\
  39.  local pow = 1\\\
  40.  while x > 0 or y > 0 do\\\
  41.    z = z + (matrix[x%2+1][y%2+1] * pow)\\\
  42.    pow = pow * 2\\\
  43.    x = math.floor(x/2)\\\
  44.    y = math.floor(y/2)\\\
  45.  end\\\
  46.  return z\\\
  47. end\\\
  48. \\\
  49. local function INT2HEX(x)\\\
  50.  local s,base = '',16\\\
  51.  local d\\\
  52.  while x > 0 do\\\
  53.    d = x % base + 1\\\
  54.    x = math.floor(x/base)\\\
  55.    s = string.sub(HEXES, d, d)..s\\\
  56.  end\\\
  57.  while #s < 2 do s = \\\"0\\\" .. s end\\\
  58.  return s\\\
  59. end\\\
  60. \\\
  61. ----------------------------------------------------------------------------\\\
  62. -- Creates a new uuid. Either provide a unique hex string, or make sure the\\\
  63. -- random seed is properly set. The module table itself is a shortcut to this\\\
  64. -- function, so `my_uuid = uuid.new()` equals `my_uuid = uuid()`.\\\
  65. --\\\
  66. -- For proper use there are 3 options;\\\
  67. --\\\
  68. -- 1. first require `luasocket`, then call `uuid.seed()`, and request a uuid using no \\\
  69. -- parameter, eg. `my_uuid = uuid()`\\\
  70. -- 2. use `uuid` without `luasocket`, set a random seed using `uuid.randomseed(some_good_seed)`, \\\
  71. -- and request a uuid using no parameter, eg. `my_uuid = uuid()`\\\
  72. -- 3. use `uuid` without `luasocket`, and request a uuid using an unique hex string, \\\
  73. -- eg. `my_uuid = uuid(my_networkcard_macaddress)`\\\
  74. --\\\
  75. -- @return a properly formatted uuid string\\\
  76. -- @param hwaddr (optional) string containing a unique hex value (e.g.: `00:0c:29:69:41:c6`), to be used to compensate for the lesser `math.random()` function. Use a mac address for solid results. If omitted, a fully randomized uuid will be generated, but then you must ensure that the random seed is set properly!\\\
  77. -- @usage\\\
  78. -- local uuid = require(\\\"uuid\\\")\\\
  79. -- print(\\\"here's a new uuid: \\\",uuid())\\\
  80. function M.new(hwaddr)\\\
  81.  -- bytes are treated as 8bit unsigned bytes.\\\
  82.  local bytes = {\\\
  83.      math.random(0, 255),\\\
  84.      math.random(0, 255),\\\
  85.      math.random(0, 255),\\\
  86.      math.random(0, 255),\\\
  87.      math.random(0, 255),\\\
  88.      math.random(0, 255),\\\
  89.      math.random(0, 255),\\\
  90.      math.random(0, 255),\\\
  91.      math.random(0, 255),\\\
  92.      math.random(0, 255),\\\
  93.      math.random(0, 255),\\\
  94.      math.random(0, 255),\\\
  95.      math.random(0, 255),\\\
  96.      math.random(0, 255),\\\
  97.      math.random(0, 255),\\\
  98.      math.random(0, 255),\\\
  99.    }\\\
  100. \\\
  101.  if hwaddr then\\\
  102.    assert(type(hwaddr)==\\\"string\\\", \\\"Expected hex string, got \\\"..type(hwaddr))\\\
  103.    -- Cleanup provided string, assume mac address, so start from back and cleanup until we've got 12 characters\\\
  104.    local i,str, hwaddr = #hwaddr, hwaddr, \\\"\\\"\\\
  105.    while i>0 and #hwaddr<12 do\\\
  106.      local c = str:sub(i,i):lower()\\\
  107.      if HEXES:find(c, 1, true) then \\\
  108.        -- valid HEX character, so append it\\\
  109.        hwaddr = c..hwaddr\\\
  110.      end\\\
  111.      i = i - 1\\\
  112.    end\\\
  113.    assert(#hwaddr == 12, \\\"Provided string did not contain at least 12 hex characters, retrieved '\\\"..hwaddr..\\\"' from '\\\"..str..\\\"'\\\")\\\
  114.    \\\
  115.    -- no split() in lua. :(\\\
  116.    bytes[11] = tonumber(hwaddr:sub(1, 2), 16)\\\
  117.    bytes[12] = tonumber(hwaddr:sub(3, 4), 16)\\\
  118.    bytes[13] = tonumber(hwaddr:sub(5, 6), 16)\\\
  119.    bytes[14] = tonumber(hwaddr:sub(7, 8), 16)\\\
  120.    bytes[15] = tonumber(hwaddr:sub(9, 10), 16)\\\
  121.    bytes[16] = tonumber(hwaddr:sub(11, 12), 16)\\\
  122.  end\\\
  123.  \\\
  124.  -- set the version\\\
  125.  bytes[7] = BITWISE(bytes[7], 0x0f, MATRIX_AND)\\\
  126.  bytes[7] = BITWISE(bytes[7], 0x40, MATRIX_OR)\\\
  127.  -- set the variant\\\
  128.  bytes[9] = BITWISE(bytes[7], 0x3f, MATRIX_AND)\\\
  129.  bytes[9] = BITWISE(bytes[7], 0x80, MATRIX_OR)\\\
  130.  return  INT2HEX(bytes[1])..INT2HEX(bytes[2])..INT2HEX(bytes[3])..INT2HEX(bytes[4])..\\\"-\\\"..\\\
  131.         INT2HEX(bytes[5])..INT2HEX(bytes[6])..\\\"-\\\"..\\\
  132.         INT2HEX(bytes[7])..INT2HEX(bytes[8])..\\\"-\\\"..\\\
  133.         INT2HEX(bytes[9])..INT2HEX(bytes[10])..\\\"-\\\"..\\\
  134.         INT2HEX(bytes[11])..INT2HEX(bytes[12])..INT2HEX(bytes[13])..INT2HEX(bytes[14])..INT2HEX(bytes[15])..INT2HEX(bytes[16])\\\
  135. end\\\
  136. \\\
  137. ----------------------------------------------------------------------------\\\
  138. -- Improved randomseed function.\\\
  139. -- Lua 5.1 and 5.2 both truncate the seed given if it exceeds the integer\\\
  140. -- range. If this happens, the seed will be 0 or 1 and all randomness will\\\
  141. -- be gone (each application run will generate the same sequence of random\\\
  142. -- numbers in that case). This improved version drops the most significant\\\
  143. -- bits in those cases to get the seed within the proper range again.\\\
  144. -- @param seed the random seed to set (integer from 0 - 2^32, negative values will be made positive)\\\
  145. -- @return the (potentially modified) seed used\\\
  146. -- @usage\\\
  147. -- local socket = require(\\\"socket\\\")  -- gettime() has higher precision than os.time()\\\
  148. -- local uuid = require(\\\"uuid\\\")\\\
  149. -- -- see also example at uuid.seed()\\\
  150. -- uuid.randomseed(socket.gettime()*10000)\\\
  151. -- print(\\\"here's a new uuid: \\\",uuid())\\\
  152. function M.randomseed(seed)\\\
  153.  seed = math.floor(math.abs(seed))\\\
  154.  if seed >= (2^bitsize) then\\\
  155.    -- integer overflow, so reduce to prevent a bad seed\\\
  156.    seed = seed - math.floor(seed / 2^bitsize) * (2^bitsize)\\\
  157.  end\\\
  158.  if lua_version < 5.2 then\\\
  159.    -- 5.1 uses (incorrect) signed int\\\
  160.    math.randomseed(seed - 2^(bitsize-1))\\\
  161.  else\\\
  162.    -- 5.2 uses (correct) unsigned int\\\
  163.    math.randomseed(seed)\\\
  164.  end\\\
  165.  return seed\\\
  166. end\\\
  167. \\\
  168. ----------------------------------------------------------------------------\\\
  169. -- Seeds the random generator.\\\
  170. -- It does so in 2 possible ways;\\\
  171. --\\\
  172. -- 1. use `os.time()`: this only offers resolution to one second (used when\\\
  173. -- LuaSocket hasn't been loaded yet\\\
  174. -- 2. use luasocket `gettime()` function, but it only does so when LuaSocket\\\
  175. -- has been required already.\\\
  176. -- @usage\\\
  177. -- local socket = require(\\\"socket\\\")  -- gettime() has higher precision than os.time()\\\
  178. -- -- LuaSocket loaded, so below line does the same as the example from randomseed()\\\
  179. -- uuid.seed()\\\
  180. -- print(\\\"here's a new uuid: \\\",uuid())\\\
  181. function M.seed()\\\
  182.    return M.randomseed(os.time())\\\
  183. end\\\
  184. \\\
  185. return setmetatable( M, { __call = function(self, hwaddr) return self.new(hwaddr) end} )\",\
  186.  pkg = \"--[[ \\\
  187. \\9ccPackager v1.0 by Shazz\\\
  188. \\9Feel free to modify, copy, distribute.\\\
  189. \\9Would be nice if you left this header here.\\\
  190. ]]\\\
  191. local unpackager = \\\"function makeDir(table,dir) if not fs.exists(dir) then fs.makeDir(dir) end for k, v in pairs(table) do if type(v)=='table' then makeDir(v,dir..'/'..k) else local fileH=fs.open(dir..'/'..k,'w') fileH.write(v) fileH.close() end end end tArgs={...} if #tArgs<1 then print('Usage: \\\\'..shell.getRunningProgram()..\\\\' <destination>') else makeDir(textutils.unserialize(c),shell.resolve(tArgs[1])) print('Succ: Successfully extracted package') end\\\"\\\
  192. function addFile(file)\\\
  193. \\9local fileH = fs.open(file, 'r')\\\
  194. \\9local content = fileH.readAll()\\\
  195. \\9fileH.close()\\\
  196. \\9return content\\\
  197. end\\\
  198. function addFolder(_dir)\\\
  199. \\9local dir = {}\\\
  200. \\9local files = fs.list(_dir)\\\
  201. \\9for i=1, #files, 1 do\\\
  202. \\9\\9if fs.isDir(_dir .. '/' .. files[i]) then\\\
  203. \\9\\9\\9dir[files[i]] = addFolder(_dir .. '/' .. files[i])\\\
  204. \\9\\9else\\\
  205. \\9\\9\\9dir[files[i]] = addFile(_dir .. '/' .. files[i])\\\
  206. \\9\\9end\\\
  207. \\9end\\\
  208. \\9return dir\\\
  209. end\\\
  210. tArgs = {...}\\\
  211. if(not fs.isDir(\\\"output\\\")) then error(\\\"output folder not found\\\",0) end\\\
  212. --if #tArgs < 2 then\\\
  213. --\\9print('Usage: packager <sourceDir> <destination>')\\\
  214. --else\\\
  215. \\9local sourceD = shell.resolve(\\\"/output\\\")\\\
  216. \\9local dest = shell.resolve(\\\"packed\\\")\\\
  217. \\9if fs.isDir(sourceD) then\\\
  218. \\9\\9if not fs.exists(dest) then\\\
  219. \\9\\9\\9local t = addFolder(sourceD)\\\
  220. \\9\\9\\9local fileH = fs.open(dest, 'w')\\\
  221. \\9\\9\\9if fileH then\\\
  222. \\9\\9\\9\\9fileH.write(\\\"local c=\\\" .. textutils.serialize(textutils.serialize(t)) .. unpackager)\\\
  223. \\9\\9\\9\\9fileH.close()\\\
  224. \\9\\9\\9\\9print('Succ: Successfully created package')\\\
  225. \\9\\9\\9else\\\
  226. \\9\\9\\9\\9print('Err: Invalid destination')\\\
  227. \\9\\9\\9end\\\
  228. \\9\\9else\\\
  229. \\9\\9\\9print('Err: Destination already exists')\\\
  230. \\9\\9end\\\
  231. \\9else\\\
  232. \\9\\9print('Err: Source is not a directory')\\\
  233. \\9end\\\
  234. --end\",\
  235.  build = \"local uuid = dofile(\\\"uuid\\\")\\\
  236. \\\
  237. local variables = {\\\
  238. \\9[\\\"{{uuid}}\\\"] = uuid.new(),\\\
  239. \\9[\\\"{{version}}\\\"] = \\\"0.1\\\",\\\
  240. \\9[\\\"{{title}}\\\"] = \\\"Test Build\\\"\\\
  241. }\\\
  242. \\\
  243. local function modifyContent(path)\\\
  244. \\9local handle = fs.open(path,\\\"r\\\")\\\
  245. \\9local content = handle.readAll()\\\
  246. \\9handle.close()\\\
  247. \\9for pattern,value in pairs(variables) do\\\
  248. \\9\\9content = string.gsub(content,pattern,value)\\\
  249. \\9end\\\
  250. \\9return content\\\
  251. end\\\
  252. \\\
  253. local function splitpath(path)\\\
  254. \\9local splitted = {}\\\
  255. \\9for match in path:gmatch(\\\"[^/]+\\\") do\\\
  256. \\9\\9splitted[#splitted+1] = match\\\
  257. \\9end\\\
  258. \\9return splitted\\\
  259. end\\\
  260. \\\
  261. local function buildPathFromTable(tbl)\\\
  262. \\9local str = \\\"\\\"\\\
  263. \\9for k,v in pairs(tbl) do\\\
  264. \\9\\9str = str..v..\\\"/\\\"\\\
  265. \\9end\\\
  266. \\9return str\\\
  267. end\\\
  268. \\\
  269. local function recursiveCopyAndModify(from,to)\\\
  270. \\9local list = fs.list(from)\\\
  271. \\9for k,v in pairs(list) do\\\
  272. \\9\\9if(fs.isDir(fs.combine(from,v))) then\\\
  273. \\9\\9\\9local path = fs.combine(from,v)\\\
  274. \\9\\9\\9local tbl = splitpath(path)\\\
  275. \\9\\9\\9tbl[1] = to\\\
  276. \\9\\9\\9path = buildPathFromTable(tbl)\\\
  277. \\9\\9\\9fs.makeDir(path)\\\
  278. \\9\\9\\9recursiveCopyAndModify(fs.combine(from,v),to)\\\
  279. \\9\\9else\\\
  280. \\9\\9\\9local path = \\\"\\\"\\\
  281. \\9\\9\\9local tb = splitpath(from)\\\
  282. \\9\\9\\9tb[1] = to\\\
  283. \\9\\9\\9path = buildPathFromTable(tb)\\\
  284. \\9\\9\\9local handle = fs.open(fs.combine(path,v),\\\"w\\\")\\\
  285. \\9\\9\\9handle.write(modifyContent(fs.combine(from,v)))\\\
  286. \\9\\9\\9handle.flush()\\\
  287. \\9\\9\\9handle.close()\\\
  288. \\9\\9end\\\
  289. \\9end\\\
  290. end\\\
  291. \\\
  292. local function build()\\\
  293. \\9if(not fs.exists(\\\"src\\\")) then\\\
  294. \\9\\9error(\\\"src folder doesn't exist\\\",0)\\\
  295. \\9end\\\
  296. \\\
  297. \\9if(fs.exists(\\\"output\\\") and fs.isDir(\\\"output\\\")) then\\\
  298. \\9\\9fs.delete(\\\"output\\\")\\\
  299. \\9end\\\
  300. \\\
  301. \\9fs.makeDir(\\\"output\\\")\\\
  302. \\9recursiveCopyAndModify(\\\"src\\\",\\\"output\\\")\\\
  303. \\\
  304. \\9if(fs.exists(\\\"pkg\\\")) then\\\
  305. \\9\\9shell.run(\\\"pkg\\\")\\\
  306. \\9end\\\
  307. end\\\
  308. \\\
  309. local ok,err = pcall(build)\\\
  310. if(not ok) then\\\
  311. \\9term.setTextColor(colors.red)\\\
  312. \\9print(err)\\\
  313. \\9term.setTextColor(colors.white)\\\
  314. \\9fs.delete(\\\"output\\\")\\\
  315. end\",\
  316.  src = {\
  317.    example = \"print(\\\"My name is {{title}} and I'm running version {{version}}\\\")\\\
  318. print(\\\"My assigned UUID is {{uuid}}\\\")\",\
  319.  },\
  320. }"function makeDir(table,dir) if not fs.exists(dir) then fs.makeDir(dir) end for k, v in pairs(table) do if type(v)=='table' then makeDir(v,dir..'/'..k) else local fileH=fs.open(dir..'/'..k,'w') fileH.write(v) fileH.close() end end end tArgs={...} if #tArgs<1 then print('Usage: '..shell.getRunningProgram()..' <destination>') else makeDir(textutils.unserialize(c),shell.resolve(tArgs[1])) print('Succ: Successfully extracted package') end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement