do -- declare local variables -- // exportstring( string ) -- // returns a "Lua" portable version of the string local function exportstring(s) return string.format("%q", s) end -- // The Save Function function table.save(tbl, filename) local charS, charE = " ", "\n" local file, err = io.open(filename, "wb") if err then return err end -- initiate variables for save procedure local tables, lookup = {tbl}, { [tbl] = 1 } file:write("return {" .. charE) for idx, t in ipairs(tables) do file:write("-- Table: {" .. idx .. "}" .. charE) file:write("{" .. charE) local thandled = {} for i, v in ipairs(t) do thandled[i] = true local stype = type(v) -- only handle value if stype == "table" then if not lookup[v] then table.insert(tables, v) lookup[v] = #tables end file:write(charS .. "{" .. lookup[v] .. "}," .. charE) elseif stype == "string" then file:write(charS .. exportstring(v) .. "," .. charE) elseif stype == "number" then file:write(charS .. tostring(v) .. "," .. charE) end end for i, v in pairs(t) do -- escape handled values if (not thandled[i]) then local str = "" local stype = type(i) -- handle index if stype == "table" then if not lookup[i] then table.insert(tables, i) lookup[i] = #tables end str = charS .. "[{" .. lookup[i] .. "}]=" elseif stype == "string" then str = charS .. "[" .. exportstring(i) .. "]=" elseif stype == "number" then str = charS .. "[" .. tostring(i) .. "]=" end if str ~= "" then stype = type(v) -- handle value if stype == "table" then if not lookup[v] then table.insert(tables, v) lookup[v] = #tables end file:write(str .. "{" .. lookup[v] .. "}," .. charE) elseif stype == "string" then file:write(str .. exportstring(v) .. "," .. charE) elseif stype == "number" then file:write(str .. tostring(v) .. "," .. charE) end end end end file:write("}," .. charE) end file:write("}") file:close() end -- // The Load Function function table.load(sfile) local ftables, err = loadfile(sfile) if err then return _, err end local tables = ftables() for idx = 1, #tables do local tolinki = {} for i, v in pairs(tables[idx]) do if type(v) == "table" then tables[idx][i] = tables[v[1]] end if type(i) == "table" and tables[i[1]] then table.insert(tolinki, {i, tables[i[1]]}) end end -- link indices for _, v in ipairs(tolinki) do tables[idx][v[2]], tables[idx][v[1]] = tables[idx][v[1]], nil end end return tables[1] end -- close do function table.SaveTableContent(file, obj, isfirst) local szType = type(obj); print(szType); if szType == "number" then file:write(obj); elseif szType == "string" then file:write(string.format("\'%s\'", obj)); elseif szType == "boolean" then local str = obj and "true" or "false" file:write(str); elseif szType == "table" then -- 把table的内容格式化写入文件 isfirst = isfirst + 1 if isfirst > 3 then file:write("{"); for i, v in pairs(obj) do table.SaveTableContent(file, v, isfirst); file:write(","); end file:write("}"); else local sortParams = {} local keyType = "" for kv in pairs(obj) do if kv ~= nil then table.insert(sortParams, kv) keyType = type(kv) end end if keyType == "number" then table.sort(sortParams, function(a, b) return a < b end) elseif keyType == "string" then table.sort(sortParams, function(a, b) error(string.find("1222", "Id")) if a == "Id" or (string.find(a, "Id") ~= nil and string.find(b,"Id") == nil)then return true else return a < b end end) end file:write("{\n"); for _, v in pairs(sortParams) do file:write("["); table.SaveTableContent(file, v, isfirst); file:write("]="); table.SaveTableContent(file, obj[v], isfirst); file:write(", \n"); end file:write("}"); end else error("can't serialize a " .. szType); end end function table.SaveTable(luaTable, path, TableName) local file, err = io.open(path, "wb") if err then return err end assert(file); local oneLine = string.format("local %s=", TableName) file:write(oneLine); table.SaveTableContent(file, luaTable, 1); file:write(string.format("\n return %s", TableName)); file:close(); end end