Skip to content
Snippets Groups Projects
Commit 8d2fe68c authored by dg's avatar dg
Browse files

Ensure consistency check of the various files of a savefile: zones saved...

Ensure consistency check of the various files of a savefile: zones saved between a save and a crash will not load


git-svn-id: http://svn.net-core.org/repos/t-engine4@1202 51575b47-30f0-44d4-a5cc-537603b46e54
parent 38fa511e
No related branches found
No related tags found
No related merge requests found
......@@ -43,6 +43,8 @@ function _M:init(keyhandler)
self.uniques = {}
self.__savefile_version_tokens = {}
self.__threads = {}
end
......@@ -64,6 +66,7 @@ function _M:defaultSavedFields(t)
energy_to_act=true, energy_per_tick=true, turn=true, paused=true, save_name=true,
always_target=true, gfxmode=true, uniques=true, object_known_types=true,
current_music=true, memory_levels=true, achievement_data=true, factions=true,
__savefile_version_tokens = true,
}
table.merge(def, t)
return def
......@@ -112,6 +115,17 @@ function _M:getPlayer()
return nil
end
--- Gets/increment the savefile version
-- @param token if "new" this will create a new allowed save token and return it. Otherwise this checks the token against the allowed ones and returns true if it is allowed
function _M:saveVersion(token)
if token == "new" then
token = util.uuid()
self.__savefile_version_tokens[token] = true
return token
end
return self.__savefile_version_tokens[token]
end
--- This is the "main game loop", do something here
function _M:tick()
local stop = {}
......
......@@ -51,7 +51,7 @@ function _M:push(savename, type, object, class)
local Savefile = require(class)
local id = Savefile["nameSave"..type:lower():capitalize()](Savefile, object)
self.pipe[#self.pipe+1] = {id=id, savename = savename, type=type, object=object:cloneFull(), baseobject=object, class=class}
self.pipe[#self.pipe+1] = {id=id, savename = savename, type=type, object=object:cloneFull(), baseobject=object, class=class, saveversion=game:saveVersion("new")}
if not self.co or coroutine.status(self.co) == "dead" then
self.co = coroutine.create(function() return self:doThread() end)
game:registerCoroutine("savefilepipe", self.co)
......@@ -79,6 +79,7 @@ function _M:doThread()
print("[SAVEFILE PIPE] new save running in the pipe:", p.savename, p.type, "::", p.id, "::", p.baseobject, "=>", p.object)
local save = Savefile.new(p.savename, true)
o.__saved_saveversion = p.saveversion
save["save"..p.type:lower():capitalize()](save, o, true)
save:close()
......@@ -131,5 +132,14 @@ function _M:doLoad(savename, type, class, ...)
local ret = save["load"..type:lower():capitalize()](save, ...)
save:close()
Savefile:setCurrent(cur)
-- Check for validity
if _G.type(ret) == "table" and type ~= "game" and type ~= "world" then
if not game:saveVersion(ret.__saved_saveversion) then
print("Loading savefile", savename, type, class," with id", ret.__saved_saveversion, "but current game does not know this token => ignoring")
return nil
end
end
return ret
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment