Advertisement
poulton0

save.lua

Dec 18th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. do
  2. local function exportstring( s )
  3. s = string.format( "%q",s )
  4. s = string.gsub( s,"\\\n","\\n" )
  5. s = string.gsub( s,"\r","\\r" )
  6. s = string.gsub( s,string.char(26),"\"..string.char(26)..\"" )
  7. return s
  8. end
  9. function table.save( tbl,filename )
  10. local charS,charE = " ","\n"
  11. local file,err
  12. if not filename then
  13. file = { write = function( self,newstr ) self.str = self.str..newstr end, str = "" }
  14. charS,charE = "",""
  15. elseif filename == true or filename == 1 then
  16. charS,charE,file = "","",io.tmpfile()
  17. else
  18. file,err = io.open( filename, "w" )
  19. if err then return _,err end
  20. end
  21. local tables,lookup = { tbl },{ [tbl] = 1 }
  22. file:write( "return {"..charE )
  23. for idx,t in ipairs( tables ) do
  24. if filename and filename ~= true and filename ~= 1 then
  25. file:write( "-- Table: {"..idx.."}"..charE )
  26. end
  27. file:write( "{"..charE )
  28. local thandled = {}
  29. for i,v in ipairs( t ) do
  30. thandled[i] = true
  31. if type( v ) ~= "userdata" then
  32. if type( v ) == "table" then
  33. if not lookup[v] then
  34. table.insert( tables, v )
  35. lookup[v] = #tables
  36. end
  37. file:write( charS.."{"..lookup[v].."},"..charE )
  38. elseif type( v ) == "function" then
  39. file:write( charS.."loadstring("..exportstring(string.dump( v )).."),"..charE )
  40. else
  41. local value = ( type( v ) == "string" and exportstring( v ) ) or tostring( v )
  42. file:write( charS..value..","..charE )
  43. end
  44. end
  45. end
  46. for i,v in pairs( t ) do
  47. if (not thandled[i]) and type( v ) ~= "userdata" then
  48. if type( i ) == "table" then
  49. if not lookup[i] then
  50. table.insert( tables,i )
  51. lookup[i] = #tables
  52. end
  53. file:write( charS.."[{"..lookup[i].."}]=" )
  54. else
  55. local index = ( type( i ) == "string" and "["..exportstring( i ).."]" ) or string.format( "[%d]",i )
  56. file:write( charS..index.."=" )
  57. end
  58. if type( v ) == "table" then
  59. if not lookup[v] then
  60. table.insert( tables,v )
  61. lookup[v] = #tables
  62. end
  63. file:write( "{"..lookup[v].."},"..charE )
  64. elseif type( v ) == "function" then
  65. file:write( "loadstring("..exportstring(string.dump( v )).."),"..charE )
  66. else
  67. local value = ( type( v ) == "string" and exportstring( v ) ) or tostring( v )
  68. file:write( value..","..charE )
  69. end
  70. end
  71. end
  72. file:write( "},"..charE )
  73. end
  74. file:write( "}" )
  75. if not filename then
  76. return file.str.."--|"
  77. elseif filename == true or filename == 1 then
  78. file:seek ( "set" )
  79. return file:read( "*a" ).."--|"
  80. else
  81. file:close()
  82. return 1
  83. end
  84. end
  85. function table.load( sfile )
  86. local tables, err, _
  87. if string.sub( sfile,-3,-1 ) == "--|" then
  88. tables,err = loadstring( sfile )
  89. else
  90. tables,err = loadfile( sfile )
  91. end
  92. if err then return _,err
  93. end
  94. tables = tables()
  95. for idx = 1,#tables do
  96. local tolinkv,tolinki = {},{}
  97. for i,v in pairs( tables[idx] ) do
  98. if type( v ) == "table" and tables[v[1]] then
  99. table.insert( tolinkv,{ i,tables[v[1]] } )
  100. end
  101. if type( i ) == "table" and tables[i[1]] then
  102. table.insert( tolinki,{ i,tables[i[1]] } )
  103. end
  104. end
  105. for _,v in ipairs( tolinkv ) do
  106. tables[idx][v[1]] = v[2]
  107. end
  108. for _,v in ipairs( tolinki ) do
  109. tables[idx][v[2]],tables[idx][v[1]] = tables[idx][v[1]],nil
  110. end
  111. end
  112. return tables[1]
  113. end
  114. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement