1
0
forked from mirrors/0ad

Fix OOS errors when placing buildings.

Allow script components to supply custom serialization objects.

This was SVN commit r7571.
This commit is contained in:
Ykkrosh
2010-05-22 23:02:07 +00:00
parent 57b7822539
commit 5c3a59234c
6 changed files with 57 additions and 15 deletions
@@ -60,17 +60,25 @@ void CComponentTypeScript::HandleMessage(const CSimContext& UNUSED(context), con
void CComponentTypeScript::Serialize(ISerializer& serialize)
{
serialize.ScriptVal("object", m_Instance);
// TODO: it should be possible for scripts to provide their own (de)serialization
// functions, for efficiency. (e.g. "function Serialize() { return a JS object containing
// just the important state for this component }")
// Or alternatively perhaps just have a way to mark certain fields as transient,
// and a post-deserialize callback to reinitialise them.
// Support a custom "Serialize" function, which returns a new object that will be
// serialized instead of the component itself
if (m_ScriptInterface.HasProperty(m_Instance, "Serialize"))
{
CScriptValRooted val;
if (!m_ScriptInterface.CallFunction(m_Instance, "Serialize", val))
LOGERROR(L"Script Serialize call failed");
serialize.ScriptVal("object", val);
}
else
{
serialize.ScriptVal("object", m_Instance);
}
}
void CComponentTypeScript::Deserialize(const CSimContext& UNUSED(context), const CParamNode& paramNode, IDeserializer& deserialize, entity_id_t ent)
{
// TODO: maybe we want to allow a script Deserialize() function, to mirror the Serialize() above
// Use ScriptObjectAppend so we don't lose the carefully-constructed
// prototype/parent of this object
deserialize.ScriptObjectAppend(m_Instance);