Changeset View
Changeset View
Standalone View
Standalone View
source/simulation2/scripting/ScriptComponent.cpp
Show All 20 Lines | |||||
#include "simulation2/serialization/ISerializer.h" | #include "simulation2/serialization/ISerializer.h" | ||||
#include "simulation2/serialization/IDeserializer.h" | #include "simulation2/serialization/IDeserializer.h" | ||||
CComponentTypeScript::CComponentTypeScript(const ScriptInterface& scriptInterface, JS::HandleValue instance) : | CComponentTypeScript::CComponentTypeScript(const ScriptInterface& scriptInterface, JS::HandleValue instance) : | ||||
m_ScriptInterface(scriptInterface), m_Instance(scriptInterface.GetJSRuntime(), instance) | m_ScriptInterface(scriptInterface), m_Instance(scriptInterface.GetJSRuntime(), instance) | ||||
{ | { | ||||
// Cache the property detection for efficiency | // Cache the property detection for efficiency | ||||
ScriptInterface::Request rq(m_ScriptInterface); | ScriptRequest rq(m_ScriptInterface); | ||||
m_HasCustomSerialize = m_ScriptInterface.HasProperty(m_Instance, "Serialize"); | m_HasCustomSerialize = m_ScriptInterface.HasProperty(m_Instance, "Serialize"); | ||||
m_HasCustomDeserialize = m_ScriptInterface.HasProperty(m_Instance, "Deserialize"); | m_HasCustomDeserialize = m_ScriptInterface.HasProperty(m_Instance, "Deserialize"); | ||||
m_HasNullSerialize = false; | m_HasNullSerialize = false; | ||||
if (m_HasCustomSerialize) | if (m_HasCustomSerialize) | ||||
{ | { | ||||
JS::RootedValue val(rq.cx); | JS::RootedValue val(rq.cx); | ||||
Show All 11 Lines | |||||
void CComponentTypeScript::Deinit() | void CComponentTypeScript::Deinit() | ||||
{ | { | ||||
m_ScriptInterface.CallFunctionVoid(m_Instance, "Deinit"); | m_ScriptInterface.CallFunctionVoid(m_Instance, "Deinit"); | ||||
} | } | ||||
void CComponentTypeScript::HandleMessage(const CMessage& msg, bool global) | void CComponentTypeScript::HandleMessage(const CMessage& msg, bool global) | ||||
{ | { | ||||
ScriptInterface::Request rq(m_ScriptInterface); | ScriptRequest rq(m_ScriptInterface); | ||||
const char* name = global ? msg.GetScriptGlobalHandlerName() : msg.GetScriptHandlerName(); | const char* name = global ? msg.GetScriptGlobalHandlerName() : msg.GetScriptHandlerName(); | ||||
JS::RootedValue msgVal(rq.cx, msg.ToJSValCached(m_ScriptInterface)); | JS::RootedValue msgVal(rq.cx, msg.ToJSValCached(m_ScriptInterface)); | ||||
if (!m_ScriptInterface.CallFunctionVoid(m_Instance, name, msgVal)) | if (!m_ScriptInterface.CallFunctionVoid(m_Instance, name, msgVal)) | ||||
LOGERROR("Script message handler %s failed", name); | LOGERROR("Script message handler %s failed", name); | ||||
} | } | ||||
void CComponentTypeScript::Serialize(ISerializer& serialize) | void CComponentTypeScript::Serialize(ISerializer& serialize) | ||||
{ | { | ||||
// If the component set Serialize = null, then do no work here | // If the component set Serialize = null, then do no work here | ||||
if (m_HasNullSerialize) | if (m_HasNullSerialize) | ||||
return; | return; | ||||
ScriptInterface::Request rq(m_ScriptInterface); | ScriptRequest rq(m_ScriptInterface); | ||||
// Support a custom "Serialize" function, which returns a new object that will be | // Support a custom "Serialize" function, which returns a new object that will be | ||||
// serialized instead of the component itself | // serialized instead of the component itself | ||||
if (m_HasCustomSerialize) | if (m_HasCustomSerialize) | ||||
{ | { | ||||
JS::RootedValue val(rq.cx); | JS::RootedValue val(rq.cx); | ||||
if (!m_ScriptInterface.CallFunction(m_Instance, "Serialize", &val)) | if (!m_ScriptInterface.CallFunction(m_Instance, "Serialize", &val)) | ||||
LOGERROR("Script Serialize call failed"); | LOGERROR("Script Serialize call failed"); | ||||
serialize.ScriptVal("object", &val); | serialize.ScriptVal("object", &val); | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
serialize.ScriptVal("object", &m_Instance); | serialize.ScriptVal("object", &m_Instance); | ||||
} | } | ||||
} | } | ||||
void CComponentTypeScript::Deserialize(const CParamNode& paramNode, IDeserializer& deserialize, entity_id_t ent) | void CComponentTypeScript::Deserialize(const CParamNode& paramNode, IDeserializer& deserialize, entity_id_t ent) | ||||
{ | { | ||||
ScriptInterface::Request rq(m_ScriptInterface); | ScriptRequest rq(m_ScriptInterface); | ||||
m_ScriptInterface.SetProperty(m_Instance, "entity", (int)ent, true, false); | m_ScriptInterface.SetProperty(m_Instance, "entity", (int)ent, true, false); | ||||
m_ScriptInterface.SetProperty(m_Instance, "template", paramNode, true, false); | m_ScriptInterface.SetProperty(m_Instance, "template", paramNode, true, false); | ||||
// Support a custom "Deserialize" function, to which we pass the deserialized data | // Support a custom "Deserialize" function, to which we pass the deserialized data | ||||
// instead of automatically adding the deserialized properties onto the object | // instead of automatically adding the deserialized properties onto the object | ||||
if (m_HasCustomDeserialize) | if (m_HasCustomDeserialize) | ||||
{ | { | ||||
Show All 19 Lines |
Wildfire Games · Phabricator