DeadGodHelper/save.lua

116 lines
2.4 KiB
Lua

local savegen = {}
function savegen:new()
local save = {}
save.settings = {}
save.settings.eid = true
save.settings.visual = true
save.settings.showonmodded = true
save.settings.showonblind = false
save.settings.maxid = 732
save.seen = {}
for i=1, save.settings.maxid do
save.seen[i] = false
end
save.seenmodded = {}
function save:touchid(id)
-- Non-modded
self.seen[id] = true
end
function save:touchname(name)
-- Modded, save by name
self.seenmodded[name] = true
end
function save:dump(json)
local data = {}
if self.settings then
data.settings = {}
data.settings.eid = self.settings.eid
data.settings.visual = self.settings.visual
data.settings.showonblind = self.settings.showonblind
data.settings.showonmodded = self.settings.showonmodded
end
if self.seen then
data.seen = {}
for i=1, #save.seen do
data.seen[i] = self.seen[i]
end
end
if self.seenmodded then
data.seenmodded = {}
for i,v in pairs(self.seenmodded) do
data.seenmodded[i] = v
end
end
return json.encode(data)
end
function save:load(json, text)
if save:islegacysave(text) then
save:legacyload(text)
return save
else
local data = json.decode(text)
if data.settings then
save.settings.eid = data.settings.eid
save.settings.visual = data.settings.visual
save.settings.showonblind = data.settings.showonblind
save.settings.showonmodded = data.settings.showonmodded
end
if data.seen then
for i=1, #data.seen do
save.seen[i] = data.seen[i]
end
end
if data.seenmodded then
for i,v in pairs(data.seenmodded) do
save.seenmodded[i] = v
end
end
end
end
function save:islegacysave(text)
-- Check if legacy (before modded items support) save
for i=1, math.min(#text,self.settings.maxid+3) do
local digit = string.sub(text,i,i)
if digit~="0" and digit~="1" then
-- Not a legacy save
return false
end
end
return true
end
function save:legacyload(text)
-- Pre modded items support
-- Change attributes based on the text
-- Symbol to bool
local f = function(x, i)
return x:sub(i,i)=="1"
end
if (#text>=3) then
-- First 3 are the settings
self.settings.eid = f(text,1)
self.settings.visual = f(text,2)
self.settings.showonblind = f(text,3)
end
local offset = 3
for id=1, self.settings.maxid do
self.seen[id] = f(text,offset+id)
end
end
return save
end
return savegen