Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local c="{\
- uuid = \"---------------------------------------------------------------------------------------\\\
- -- Copyright 2012 Rackspace (original), 2013 Thijs Schreijer (modifications)\\\
- -- \\\
- -- Licensed under the Apache License, Version 2.0 (the \\\"License\\\");\\\
- -- you may not use this file except in compliance with the License.\\\
- -- You may obtain a copy of the License at\\\
- -- \\\
- -- http://www.apache.org/licenses/LICENSE-2.0\\\
- -- \\\
- -- Unless required by applicable law or agreed to in writing, software\\\
- -- distributed under the License is distributed on an \\\"AS-IS\\\" BASIS,\\\
- -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\\\
- -- See the License for the specific language governing permissions and\\\
- -- limitations under the License.\\\
- -- \\\
- -- see http://www.ietf.org/rfc/rfc4122.txt\\\
- --\\\
- -- Note that this is not a true version 4 (random) UUID. Since `os.time()` precision is only 1 second, it would be hard\\\
- -- to guarantee spacial uniqueness when two hosts generate a uuid after being seeded during the same second. This\\\
- -- is solved by using the node field from a version 1 UUID. It represents the mac address.\\\
- -- \\\
- -- 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.\\\
- -- Regarding the above mention on `os.time()`; the modifications use the `socket.gettime()` function from LuaSocket\\\
- -- if available and hence reduce that problem (provided LuaSocket has been loaded before uuid).\\\
- \\\
- local M = {}\\\
- \\\
- local bitsize = 32 -- bitsize assumed for Lua VM. See randomseed function below.\\\
- local lua_version = tonumber(_VERSION:match(\\\"%d%.*%d*\\\")) -- grab Lua version used\\\
- \\\
- local MATRIX_AND = {{0,0},{0,1} }\\\
- local MATRIX_OR = {{0,1},{1,1}}\\\
- local HEXES = '0123456789abcdef'\\\
- \\\
- -- performs the bitwise operation specified by truth matrix on two numbers.\\\
- local function BITWISE(x, y, matrix)\\\
- local z = 0\\\
- local pow = 1\\\
- while x > 0 or y > 0 do\\\
- z = z + (matrix[x%2+1][y%2+1] * pow)\\\
- pow = pow * 2\\\
- x = math.floor(x/2)\\\
- y = math.floor(y/2)\\\
- end\\\
- return z\\\
- end\\\
- \\\
- local function INT2HEX(x)\\\
- local s,base = '',16\\\
- local d\\\
- while x > 0 do\\\
- d = x % base + 1\\\
- x = math.floor(x/base)\\\
- s = string.sub(HEXES, d, d)..s\\\
- end\\\
- while #s < 2 do s = \\\"0\\\" .. s end\\\
- return s\\\
- end\\\
- \\\
- ----------------------------------------------------------------------------\\\
- -- Creates a new uuid. Either provide a unique hex string, or make sure the\\\
- -- random seed is properly set. The module table itself is a shortcut to this\\\
- -- function, so `my_uuid = uuid.new()` equals `my_uuid = uuid()`.\\\
- --\\\
- -- For proper use there are 3 options;\\\
- --\\\
- -- 1. first require `luasocket`, then call `uuid.seed()`, and request a uuid using no \\\
- -- parameter, eg. `my_uuid = uuid()`\\\
- -- 2. use `uuid` without `luasocket`, set a random seed using `uuid.randomseed(some_good_seed)`, \\\
- -- and request a uuid using no parameter, eg. `my_uuid = uuid()`\\\
- -- 3. use `uuid` without `luasocket`, and request a uuid using an unique hex string, \\\
- -- eg. `my_uuid = uuid(my_networkcard_macaddress)`\\\
- --\\\
- -- @return a properly formatted uuid string\\\
- -- @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!\\\
- -- @usage\\\
- -- local uuid = require(\\\"uuid\\\")\\\
- -- print(\\\"here's a new uuid: \\\",uuid())\\\
- function M.new(hwaddr)\\\
- -- bytes are treated as 8bit unsigned bytes.\\\
- local bytes = {\\\
- math.random(0, 255),\\\
- math.random(0, 255),\\\
- math.random(0, 255),\\\
- math.random(0, 255),\\\
- math.random(0, 255),\\\
- math.random(0, 255),\\\
- math.random(0, 255),\\\
- math.random(0, 255),\\\
- math.random(0, 255),\\\
- math.random(0, 255),\\\
- math.random(0, 255),\\\
- math.random(0, 255),\\\
- math.random(0, 255),\\\
- math.random(0, 255),\\\
- math.random(0, 255),\\\
- math.random(0, 255),\\\
- }\\\
- \\\
- if hwaddr then\\\
- assert(type(hwaddr)==\\\"string\\\", \\\"Expected hex string, got \\\"..type(hwaddr))\\\
- -- Cleanup provided string, assume mac address, so start from back and cleanup until we've got 12 characters\\\
- local i,str, hwaddr = #hwaddr, hwaddr, \\\"\\\"\\\
- while i>0 and #hwaddr<12 do\\\
- local c = str:sub(i,i):lower()\\\
- if HEXES:find(c, 1, true) then \\\
- -- valid HEX character, so append it\\\
- hwaddr = c..hwaddr\\\
- end\\\
- i = i - 1\\\
- end\\\
- assert(#hwaddr == 12, \\\"Provided string did not contain at least 12 hex characters, retrieved '\\\"..hwaddr..\\\"' from '\\\"..str..\\\"'\\\")\\\
- \\\
- -- no split() in lua. :(\\\
- bytes[11] = tonumber(hwaddr:sub(1, 2), 16)\\\
- bytes[12] = tonumber(hwaddr:sub(3, 4), 16)\\\
- bytes[13] = tonumber(hwaddr:sub(5, 6), 16)\\\
- bytes[14] = tonumber(hwaddr:sub(7, 8), 16)\\\
- bytes[15] = tonumber(hwaddr:sub(9, 10), 16)\\\
- bytes[16] = tonumber(hwaddr:sub(11, 12), 16)\\\
- end\\\
- \\\
- -- set the version\\\
- bytes[7] = BITWISE(bytes[7], 0x0f, MATRIX_AND)\\\
- bytes[7] = BITWISE(bytes[7], 0x40, MATRIX_OR)\\\
- -- set the variant\\\
- bytes[9] = BITWISE(bytes[7], 0x3f, MATRIX_AND)\\\
- bytes[9] = BITWISE(bytes[7], 0x80, MATRIX_OR)\\\
- return INT2HEX(bytes[1])..INT2HEX(bytes[2])..INT2HEX(bytes[3])..INT2HEX(bytes[4])..\\\"-\\\"..\\\
- INT2HEX(bytes[5])..INT2HEX(bytes[6])..\\\"-\\\"..\\\
- INT2HEX(bytes[7])..INT2HEX(bytes[8])..\\\"-\\\"..\\\
- INT2HEX(bytes[9])..INT2HEX(bytes[10])..\\\"-\\\"..\\\
- INT2HEX(bytes[11])..INT2HEX(bytes[12])..INT2HEX(bytes[13])..INT2HEX(bytes[14])..INT2HEX(bytes[15])..INT2HEX(bytes[16])\\\
- end\\\
- \\\
- ----------------------------------------------------------------------------\\\
- -- Improved randomseed function.\\\
- -- Lua 5.1 and 5.2 both truncate the seed given if it exceeds the integer\\\
- -- range. If this happens, the seed will be 0 or 1 and all randomness will\\\
- -- be gone (each application run will generate the same sequence of random\\\
- -- numbers in that case). This improved version drops the most significant\\\
- -- bits in those cases to get the seed within the proper range again.\\\
- -- @param seed the random seed to set (integer from 0 - 2^32, negative values will be made positive)\\\
- -- @return the (potentially modified) seed used\\\
- -- @usage\\\
- -- local socket = require(\\\"socket\\\") -- gettime() has higher precision than os.time()\\\
- -- local uuid = require(\\\"uuid\\\")\\\
- -- -- see also example at uuid.seed()\\\
- -- uuid.randomseed(socket.gettime()*10000)\\\
- -- print(\\\"here's a new uuid: \\\",uuid())\\\
- function M.randomseed(seed)\\\
- seed = math.floor(math.abs(seed))\\\
- if seed >= (2^bitsize) then\\\
- -- integer overflow, so reduce to prevent a bad seed\\\
- seed = seed - math.floor(seed / 2^bitsize) * (2^bitsize)\\\
- end\\\
- if lua_version < 5.2 then\\\
- -- 5.1 uses (incorrect) signed int\\\
- math.randomseed(seed - 2^(bitsize-1))\\\
- else\\\
- -- 5.2 uses (correct) unsigned int\\\
- math.randomseed(seed)\\\
- end\\\
- return seed\\\
- end\\\
- \\\
- ----------------------------------------------------------------------------\\\
- -- Seeds the random generator.\\\
- -- It does so in 2 possible ways;\\\
- --\\\
- -- 1. use `os.time()`: this only offers resolution to one second (used when\\\
- -- LuaSocket hasn't been loaded yet\\\
- -- 2. use luasocket `gettime()` function, but it only does so when LuaSocket\\\
- -- has been required already.\\\
- -- @usage\\\
- -- local socket = require(\\\"socket\\\") -- gettime() has higher precision than os.time()\\\
- -- -- LuaSocket loaded, so below line does the same as the example from randomseed()\\\
- -- uuid.seed()\\\
- -- print(\\\"here's a new uuid: \\\",uuid())\\\
- function M.seed()\\\
- return M.randomseed(os.time())\\\
- end\\\
- \\\
- return setmetatable( M, { __call = function(self, hwaddr) return self.new(hwaddr) end} )\",\
- pkg = \"--[[ \\\
- \\9ccPackager v1.0 by Shazz\\\
- \\9Feel free to modify, copy, distribute.\\\
- \\9Would be nice if you left this header here.\\\
- ]]\\\
- 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\\\"\\\
- function addFile(file)\\\
- \\9local fileH = fs.open(file, 'r')\\\
- \\9local content = fileH.readAll()\\\
- \\9fileH.close()\\\
- \\9return content\\\
- end\\\
- function addFolder(_dir)\\\
- \\9local dir = {}\\\
- \\9local files = fs.list(_dir)\\\
- \\9for i=1, #files, 1 do\\\
- \\9\\9if fs.isDir(_dir .. '/' .. files[i]) then\\\
- \\9\\9\\9dir[files[i]] = addFolder(_dir .. '/' .. files[i])\\\
- \\9\\9else\\\
- \\9\\9\\9dir[files[i]] = addFile(_dir .. '/' .. files[i])\\\
- \\9\\9end\\\
- \\9end\\\
- \\9return dir\\\
- end\\\
- tArgs = {...}\\\
- if(not fs.isDir(\\\"output\\\")) then error(\\\"output folder not found\\\",0) end\\\
- --if #tArgs < 2 then\\\
- --\\9print('Usage: packager <sourceDir> <destination>')\\\
- --else\\\
- \\9local sourceD = shell.resolve(\\\"/output\\\")\\\
- \\9local dest = shell.resolve(\\\"packed\\\")\\\
- \\9if fs.isDir(sourceD) then\\\
- \\9\\9if not fs.exists(dest) then\\\
- \\9\\9\\9local t = addFolder(sourceD)\\\
- \\9\\9\\9local fileH = fs.open(dest, 'w')\\\
- \\9\\9\\9if fileH then\\\
- \\9\\9\\9\\9fileH.write(\\\"local c=\\\" .. textutils.serialize(textutils.serialize(t)) .. unpackager)\\\
- \\9\\9\\9\\9fileH.close()\\\
- \\9\\9\\9\\9print('Succ: Successfully created package')\\\
- \\9\\9\\9else\\\
- \\9\\9\\9\\9print('Err: Invalid destination')\\\
- \\9\\9\\9end\\\
- \\9\\9else\\\
- \\9\\9\\9print('Err: Destination already exists')\\\
- \\9\\9end\\\
- \\9else\\\
- \\9\\9print('Err: Source is not a directory')\\\
- \\9end\\\
- --end\",\
- build = \"local uuid = dofile(\\\"uuid\\\")\\\
- \\\
- local variables = {\\\
- \\9[\\\"{{uuid}}\\\"] = uuid.new(),\\\
- \\9[\\\"{{version}}\\\"] = \\\"0.1\\\",\\\
- \\9[\\\"{{title}}\\\"] = \\\"Test Build\\\"\\\
- }\\\
- \\\
- local function modifyContent(path)\\\
- \\9local handle = fs.open(path,\\\"r\\\")\\\
- \\9local content = handle.readAll()\\\
- \\9handle.close()\\\
- \\9for pattern,value in pairs(variables) do\\\
- \\9\\9content = string.gsub(content,pattern,value)\\\
- \\9end\\\
- \\9return content\\\
- end\\\
- \\\
- local function splitpath(path)\\\
- \\9local splitted = {}\\\
- \\9for match in path:gmatch(\\\"[^/]+\\\") do\\\
- \\9\\9splitted[#splitted+1] = match\\\
- \\9end\\\
- \\9return splitted\\\
- end\\\
- \\\
- local function buildPathFromTable(tbl)\\\
- \\9local str = \\\"\\\"\\\
- \\9for k,v in pairs(tbl) do\\\
- \\9\\9str = str..v..\\\"/\\\"\\\
- \\9end\\\
- \\9return str\\\
- end\\\
- \\\
- local function recursiveCopyAndModify(from,to)\\\
- \\9local list = fs.list(from)\\\
- \\9for k,v in pairs(list) do\\\
- \\9\\9if(fs.isDir(fs.combine(from,v))) then\\\
- \\9\\9\\9local path = fs.combine(from,v)\\\
- \\9\\9\\9local tbl = splitpath(path)\\\
- \\9\\9\\9tbl[1] = to\\\
- \\9\\9\\9path = buildPathFromTable(tbl)\\\
- \\9\\9\\9fs.makeDir(path)\\\
- \\9\\9\\9recursiveCopyAndModify(fs.combine(from,v),to)\\\
- \\9\\9else\\\
- \\9\\9\\9local path = \\\"\\\"\\\
- \\9\\9\\9local tb = splitpath(from)\\\
- \\9\\9\\9tb[1] = to\\\
- \\9\\9\\9path = buildPathFromTable(tb)\\\
- \\9\\9\\9local handle = fs.open(fs.combine(path,v),\\\"w\\\")\\\
- \\9\\9\\9handle.write(modifyContent(fs.combine(from,v)))\\\
- \\9\\9\\9handle.flush()\\\
- \\9\\9\\9handle.close()\\\
- \\9\\9end\\\
- \\9end\\\
- end\\\
- \\\
- local function build()\\\
- \\9if(not fs.exists(\\\"src\\\")) then\\\
- \\9\\9error(\\\"src folder doesn't exist\\\",0)\\\
- \\9end\\\
- \\\
- \\9if(fs.exists(\\\"output\\\") and fs.isDir(\\\"output\\\")) then\\\
- \\9\\9fs.delete(\\\"output\\\")\\\
- \\9end\\\
- \\\
- \\9fs.makeDir(\\\"output\\\")\\\
- \\9recursiveCopyAndModify(\\\"src\\\",\\\"output\\\")\\\
- \\\
- \\9if(fs.exists(\\\"pkg\\\")) then\\\
- \\9\\9shell.run(\\\"pkg\\\")\\\
- \\9end\\\
- end\\\
- \\\
- local ok,err = pcall(build)\\\
- if(not ok) then\\\
- \\9term.setTextColor(colors.red)\\\
- \\9print(err)\\\
- \\9term.setTextColor(colors.white)\\\
- \\9fs.delete(\\\"output\\\")\\\
- end\",\
- src = {\
- example = \"print(\\\"My name is {{title}} and I'm running version {{version}}\\\")\\\
- print(\\\"My assigned UUID is {{uuid}}\\\")\",\
- },\
- }"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