zahar0401

Settings library for Orbital

Jun 26th, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.62 KB | None | 0 0
  1. local libcode = [[
  2.  
  3. local tSettings = {}
  4.  
  5. function set( sName, value )
  6.     if type( sName ) ~= "string" then error( "bad argument #1 (expected string, got " .. type( sName ) .. ")", 2 ) end
  7.    
  8.     local sValueTy = type(value)
  9.     if sValueTy ~= "number" and sValueTy ~= "string" and sValueTy ~= "boolean" and sValueTy ~= "table" then
  10.         error( "bad argument #2 (expected value, got " .. sValueTy .. ")", 2 )
  11.     end
  12.     if sValueTy == "table" then
  13.         -- Ensure value is serializeable
  14.         value = textutils.unserialize( textutils.serialize(value) )
  15.     end
  16.     tSettings[ sName ] = value
  17. end
  18.  
  19. local copy
  20. function copy( value )
  21.     if type(value) == "table" then
  22.         local result = {}
  23.         for k,v in pairs(value) do
  24.             result[k] = copy(v)
  25.         end
  26.         return result
  27.     else
  28.         return value
  29.     end
  30. end
  31.  
  32. function get( sName, default )
  33.     if type(sName) ~= "string" then
  34.         error( "bad argument #1 (expected string, got " .. type( sName ) .. ")", 2 )
  35.     end
  36.     local result = tSettings[ sName ]
  37.     if result ~= nil then
  38.         return copy(result)
  39.     else
  40.         return default
  41.     end
  42. end
  43.  
  44. function unset( sName )
  45.     if type(sName) ~= "string" then
  46.         error( "bad argument #1 (expected string, got " .. type( sName ) .. ")", 2 )
  47.     end
  48.     tSettings[ sName ] = nil
  49. end
  50.  
  51. function clear()
  52.     tSettings = {}
  53. end
  54.  
  55. function getNames()
  56.     local result = {}
  57.     for k,v in pairs( tSettings ) do
  58.         result[ #result + 1 ] = k
  59.     end
  60.     table.sort(result)
  61.     return result
  62. end
  63.  
  64. function load( sPath )
  65.     if type(sPath) ~= "string" then
  66.         error( "bad argument #1 (expected string, got " .. type( sPath ) .. ")", 2 )
  67.     end
  68.     local file = fs.open( sPath, "r" )
  69.     if not file then
  70.         return false
  71.     end
  72.  
  73.     local sText = file.readAll()
  74.     file.close()
  75.  
  76.     local tFile = textutils.unserialize( sText )
  77.     if type(tFile) ~= "table" then
  78.         return false
  79.     end
  80.  
  81.     for k,v in pairs(tFile) do
  82.         if type(k) == "string" and
  83.            (type(v) == "string" or type(v) == "number" or type(v) == "boolean" or type(v) == "table") then
  84.             set( k, v )
  85.         end
  86.     end
  87.  
  88.     return true
  89. end
  90.  
  91. function save( sPath )
  92.     if type(sPath) ~= "string" then
  93.         error( "bad argument #1 (expected string, got " .. type( sPath ) .. ")", 2 )
  94.     end
  95.     local file = fs.open( sPath, "w" )
  96.     if not file then
  97.         return false
  98.     end
  99.  
  100.     file.write( textutils.serialize( tSettings ) )
  101.     file.close()
  102.  
  103.     return true
  104. end
  105. ]]
  106.  
  107. syscall(0x00000004, "settings", libcode)
  108.  
  109. return 1
Advertisement
Add Comment
Please, Sign In to add comment