Skip to content

Commit

Permalink
Merge pull request #5774 from Web-eWorks/lua-serialize-persistent
Browse files Browse the repository at this point in the history
Add Lua "Persistent Object" Serialization
  • Loading branch information
Webster Sheets authored Mar 1, 2024
2 parents b01cc15 + f9dd8c2 commit 4b7509a
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 56 deletions.
16 changes: 10 additions & 6 deletions data/libs/CommodityType.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,12 @@ CommodityType.registry = {}
function CommodityType.RegisterCommodity(name, info)
assert(not CommodityType.registry[name])

CommodityType.registry[name] = CommodityType.New(name, info)
return CommodityType.registry[name]
local commodity = CommodityType.New(name, info)

CommodityType.registry[name] = commodity
Serializer:RegisterPersistent("CommodityType." .. name, commodity)

return commodity
end

-- Function: GetCommodity
Expand All @@ -122,14 +126,14 @@ end
-- Ensure loaded commodity types always point at the 'canonical' instance of the commodity;
-- commodity types not defined by the current version of the code will be loaded verbatim
function CommodityType.Unserialize(data)
local ct = CommodityType.GetCommodity(data.name)
setmetatable(data, CommodityType.meta)

if not ct then
if not CommodityType.registry[data.name] then
logWarning('Commodity type ' .. data.name .. ' could not be found, are you loading an outdated save?')
ct = CommodityType.RegisterCommodity(data.name, data)
CommodityType.registry[data.name] = data
end

return ct
return data
end

Serializer:RegisterClass('CommodityType', CommodityType)
Expand Down
7 changes: 7 additions & 0 deletions data/meta/Serializer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,11 @@ function Serializer:Register(key, serialize, unserialize) end
---@param class table
function Serializer:RegisterClass(key, class) end

-- Register a table as a "persistent" value. All references to the saved
-- instance of that table will be transparently replaced across savegames to
-- maintain table instance identity.
---@param key string
---@param value table
function Serializer:RegisterPersistent(key, value) end

return Serializer
30 changes: 30 additions & 0 deletions data/modules/Debug/TestSerialization.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,33 @@ end
Serializer:RegisterClass('TestClass', testClass)
Serializer:Register('TestSerialization', serialize, unserialize)
--]]

--[[
local persistentClass = utils.inherits(nil, 'TestClassPersistent')
local persistentInstance = persistentClass.New()
function persistentClass:Unserialize()
print("Unserialized a persistent object")
return setmetatable(self, persistentClass.meta)
end
Serializer:RegisterPersistent('TestPersistentObject', persistentInstance)
local function serialize()
local test_data = {
instance = persistentInstance
}
return test_data
end
local function unserialize(data)
print(persistentInstance)
print(data.instance)
print(persistentInstance == data.instance)
end
Serializer:RegisterClass('TestClassPersistent', persistentClass)
Serializer:Register('TestSerialization2', serialize, unserialize)
--]]
2 changes: 2 additions & 0 deletions src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ Game::Game(const Json &jsonObj) :

// Preparing the Lua stuff
Pi::luaSerializer->InitTableRefs();
Pi::luaSerializer->LoadPersistent(jsonObj);

GalacticEconomy::LoadFromJson(jsonObj);

Expand Down Expand Up @@ -214,6 +215,7 @@ void Game::ToJson(Json &jsonObj)
PROFILE_SCOPED()
// preparing the lua serializer
Pi::luaSerializer->InitTableRefs();
Pi::luaSerializer->SavePersistent(jsonObj);

// version
jsonObj["version"] = s_saveVersion;
Expand Down
Loading

0 comments on commit 4b7509a

Please sign in to comment.