Savage_Me55iah

CC_API_tablemanip

Jul 19th, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.94 KB | None | 0 0
  1. --Script by Savage_Me55iah of CraftAU.com.au--
  2.  
  3. --Other Savage_Me55iah scripts @ http://pastebin.com/u/Savage_Me55iah
  4.  
  5. --INSTRUCTIONS ON USE--
  6. --Instruction video on http://youtube.com/savagemessiah5
  7.  
  8. --Contact Savage_Me55iah @ CraftAU.com.au or http://www.computercraft.info/forums2/ with suggestions/fixes/bugs
  9.  
  10. local scriptName = "tablemanip"
  11. fileDetail = fileDetail or {}
  12. fileDetail[scriptName] = {["version"] = 0.1, ["pastebin"] = "1FxrRa1w"}
  13.  
  14. local tArgs = {...}
  15. if tArgs[1] == "update" then
  16.     print("UPDATING") shell.run("pastebin get "..fileDetail[scriptName].pastebin.." "..scriptName.."_update")
  17.     if fs.exists(scriptName.."_update") then fs.remove(shell.getRunningProgram()) fs.copy(scriptName.."_update", shell.getRunningProgram()) fs.remove(scriptName.."_update") print("UPDATE SUCCESS") else print("UPDATE FAILED") end
  18. end
  19.  
  20. -----------------------------------------------------------------------------------------------------------
  21. -- Variables --
  22. -----------------------------------------------------------------------------------------------------------
  23.  
  24. local datastore = "Datastore/"
  25.  
  26. -----------------------------------------------------------------------------------------------------------
  27. -- API Functions --
  28. -----------------------------------------------------------------------------------------------------------
  29.  
  30. -- table = tablemanip.Shift(functionNum, up/down, table) -- [1] returns function ran correctly [2] returns result
  31. -- will shift an numerical table, up > down
  32. function Shift(param_functionNum, param_function, param_table)
  33.     local result, functionNum = {false, false}, param_functionNum.."/007"
  34.     local tempTable = {}
  35.     if booleanmanip.variableCheck(functionNum, {"string"}, {param_function})[2] and booleanmanip.variableCheck(functionNum, {"table"}, {param_table})[2] then
  36.         result, tempTable = {true, {}}, table.sort(param_table)
  37.         for a,b in pairs(tempTable) do
  38.             if type(a) == "number" and param_function == "up" then
  39.                 table.insert(result[2], a + 1, b)
  40.             elseif type(a) == "number" and param_function == "down" then
  41.                 table.insert(result[2], a - 1, b)
  42.             else
  43.                 table.insert(result[2], a, b)
  44.         end end
  45.     else
  46.         logger.logger(functionNum, {"Function Error, Expected: table = tablemanip.Shift(functionNum, up/down, table)"}, 2, " ")
  47.     end
  48.     return result
  49. end
  50.  
  51. -- table = tablemanip.import(tableName) -- [1] returns function ran correctly [2] returns result
  52. -- returns table from an external/previously exported table
  53. function import(param_functionNum, param_tableName)
  54.     local result, functionNum = {false}, param_functionNum.."/007"
  55.     if booleanmanip.variableCheck(functionNum, {"string"}, {param_tableName})[2] then
  56.         if fs.exists(datastore..param_tableName) then
  57.             local file = fs.open(datastore..param_tableName, 'r')
  58.             result = {true, textutils.unserialize(file:readAll())}
  59.             file:close()
  60.         else
  61.             result[1] = true
  62.         end
  63.     else
  64.         logger.logger(functionNum, {"Function Error, Expected: table = tablemanip.import(tableName)"}, 1, " ")
  65.     end
  66.     return result
  67. end
  68.  
  69. -- tablemanip.export(param_tableName, table) -- [1] returns function ran correctly [2] returns result
  70. -- returns boolean, exports to the datastore directory
  71. function export(param_functionNum, param_tableName, param_table)
  72.     local result, functionNum = {false, false}, param_functionNum.."/007"
  73.     if booleanmanip.variableCheck(functionNum, {"string"}, {param_tableName})[2] and booleanmanip.variableCheck(functionNum, {"table"}, {param_table})[2] then
  74.         result[1] = logger.saveToFile(functionNum, datastore..param_tableName, 'w', textutils.serialize(param_table))[1]
  75.     else
  76.         logger.logger(functionNum, {"Function Error, Expected: tablemanip.export(param_tableName, table)"}, 1, " ")
  77.     end
  78.     return result
  79. end
  80.  
  81. -----------------------------------------------------------------------------------------------------------
  82. -- Old Functions --
  83. -----------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment