forked from mirrors/0ad
Correctly serialize/deserialize user-defined JS objects.
Generalize component/AI serialization system to any user-defined JS object. This includes Vector2D/3D, fixing an old issue. As with components/AI, JS Objects may implement a Serialize/Deserialize function to store custom data instead of the default, which attemps to serialize all enumerable properties. Fixes #4698 Differential Revision: https://code.wildfiregames.com/D2746 This was SVN commit r24462.
This commit is contained in:
@@ -25,19 +25,6 @@
|
||||
CComponentTypeScript::CComponentTypeScript(const ScriptInterface& scriptInterface, JS::HandleValue instance) :
|
||||
m_ScriptInterface(scriptInterface), m_Instance(scriptInterface.GetGeneralJSContext(), instance)
|
||||
{
|
||||
// Cache the property detection for efficiency
|
||||
ScriptRequest rq(m_ScriptInterface);
|
||||
|
||||
m_HasCustomSerialize = m_ScriptInterface.HasProperty(m_Instance, "Serialize");
|
||||
m_HasCustomDeserialize = m_ScriptInterface.HasProperty(m_Instance, "Deserialize");
|
||||
|
||||
m_HasNullSerialize = false;
|
||||
if (m_HasCustomSerialize)
|
||||
{
|
||||
JS::RootedValue val(rq.cx);
|
||||
if (m_ScriptInterface.GetProperty(m_Instance, "Serialize", &val) && val.isNull())
|
||||
m_HasNullSerialize = true;
|
||||
}
|
||||
}
|
||||
|
||||
void CComponentTypeScript::Init(const CParamNode& paramNode, entity_id_t ent)
|
||||
@@ -66,54 +53,18 @@ void CComponentTypeScript::HandleMessage(const CMessage& msg, bool global)
|
||||
|
||||
void CComponentTypeScript::Serialize(ISerializer& serialize)
|
||||
{
|
||||
// If the component set Serialize = null, then do no work here
|
||||
if (m_HasNullSerialize)
|
||||
return;
|
||||
|
||||
ScriptRequest rq(m_ScriptInterface);
|
||||
|
||||
// Support a custom "Serialize" function, which returns a new object that will be
|
||||
// serialized instead of the component itself
|
||||
if (m_HasCustomSerialize)
|
||||
{
|
||||
JS::RootedValue val(rq.cx);
|
||||
if (!m_ScriptInterface.CallFunction(m_Instance, "Serialize", &val))
|
||||
LOGERROR("Script Serialize call failed");
|
||||
serialize.ScriptVal("object", &val);
|
||||
}
|
||||
else
|
||||
{
|
||||
serialize.ScriptVal("object", &m_Instance);
|
||||
}
|
||||
serialize.ScriptVal("comp", &m_Instance);
|
||||
}
|
||||
|
||||
void CComponentTypeScript::Deserialize(const CParamNode& paramNode, IDeserializer& deserialize, entity_id_t ent)
|
||||
{
|
||||
ScriptRequest rq(m_ScriptInterface);
|
||||
|
||||
deserialize.ScriptObjectAssign("comp", m_Instance);
|
||||
|
||||
m_ScriptInterface.SetProperty(m_Instance, "entity", (int)ent, true, false);
|
||||
m_ScriptInterface.SetProperty(m_Instance, "template", paramNode, true, false);
|
||||
|
||||
// Support a custom "Deserialize" function, to which we pass the deserialized data
|
||||
// instead of automatically adding the deserialized properties onto the object
|
||||
if (m_HasCustomDeserialize)
|
||||
{
|
||||
JS::RootedValue val(rq.cx);
|
||||
|
||||
// If Serialize = null, we'll still call Deserialize but with undefined argument
|
||||
if (!m_HasNullSerialize)
|
||||
deserialize.ScriptVal("object", &val);
|
||||
|
||||
if (!m_ScriptInterface.CallFunctionVoid(m_Instance, "Deserialize", val))
|
||||
LOGERROR("Script Deserialize call failed");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!m_HasNullSerialize)
|
||||
{
|
||||
// Use ScriptObjectAppend so we don't lose the carefully-constructed
|
||||
// prototype/parent of this object
|
||||
deserialize.ScriptObjectAppend("object", m_Instance);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user