Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Lua "Persistent Object" Serialization #5774

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Gliese852 marked this conversation as resolved.
Show resolved Hide resolved

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
Loading