Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ScriptComponent.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2010 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "precompiled.h"
19 
20 #include "ScriptComponent.h"
21 
24 
26  m_ScriptInterface(scriptInterface), m_Instance(CScriptValRooted(scriptInterface.GetContext(), instance))
27 {
28  // Cache the property detection for efficiency
31 
32  m_HasNullSerialize = false;
34  {
35  CScriptVal val;
36  if (m_ScriptInterface.GetProperty(m_Instance.get(), "Serialize", val) && JSVAL_IS_NULL(val.get()))
37  m_HasNullSerialize = true;
38  }
39 }
40 
42 {
43  m_ScriptInterface.SetProperty(m_Instance.get(), "entity", (int)ent, true, false);
44  m_ScriptInterface.SetProperty(m_Instance.get(), "template", paramNode, true, false);
46 }
47 
49 {
51 }
52 
53 void CComponentTypeScript::HandleMessage(const CMessage& msg, bool global)
54 {
55  const char* name = global ? msg.GetScriptGlobalHandlerName() : msg.GetScriptHandlerName();
56 
58 
59  if (!m_ScriptInterface.CallFunctionVoid(m_Instance.get(), name, msgVal))
60  LOGERROR(L"Script message handler %hs failed", name);
61 }
62 
64 {
65  // If the component set Serialize = null, then do no work here
67  return;
68 
69  // Support a custom "Serialize" function, which returns a new object that will be
70  // serialized instead of the component itself
72  {
73  CScriptVal val;
74  if (!m_ScriptInterface.CallFunction(m_Instance.get(), "Serialize", val))
75  LOGERROR(L"Script Serialize call failed");
76  serialize.ScriptVal("object", val);
77  }
78  else
79  {
80  serialize.ScriptVal("object", m_Instance.get());
81  }
82 }
83 
84 void CComponentTypeScript::Deserialize(const CParamNode& paramNode, IDeserializer& deserialize, entity_id_t ent)
85 {
86  // Support a custom "Deserialize" function, to which we pass the deserialized data
87  // instead of automatically adding the deserialized properties onto the object
89  {
90  CScriptVal val;
91 
92  // If Serialize = null, we'll still call Deserialize but with undefined argument
93  if (!m_HasNullSerialize)
94  deserialize.ScriptVal("object", val);
95 
96  if (!m_ScriptInterface.CallFunctionVoid(m_Instance.get(), "Deserialize", val))
97  LOGERROR(L"Script Deserialize call failed");
98  }
99  else
100  {
101  if (!m_HasNullSerialize)
102  {
103  // Use ScriptObjectAppend so we don't lose the carefully-constructed
104  // prototype/parent of this object
105  deserialize.ScriptObjectAppend("object", m_Instance.getRef());
106  }
107  }
108 
109  m_ScriptInterface.SetProperty(m_Instance.get(), "entity", (int)ent, true, false);
110  m_ScriptInterface.SetProperty(m_Instance.get(), "template", paramNode, true, false);
111 }
An entity initialisation parameter node.
Definition: ParamNode.h:112
jsval ToJSValCached(ScriptInterface &) const
virtual void ScriptObjectAppend(const char *name, jsval &obj)=0
Deserialize an object jsval, appending properties to object &#39;obj&#39;.
#define LOGERROR
Definition: CLogger.h:35
bool HasProperty(jsval obj, const char *name)
Check the named property has been defined on the given object.
Serialization interface; see serialization overview.
Definition: ISerializer.h:120
CComponentTypeScript(ScriptInterface &scriptInterface, jsval instance)
bool CallFunctionVoid(jsval val, const char *name)
Call the named property on the given object, with void return type and 0 arguments.
const jsval & get() const
Returns the current value.
Definition: ScriptVal.h:38
A trivial wrapper around a jsval.
Definition: ScriptVal.h:29
void ScriptVal(const char *name, jsval value)
Serialize a jsval.
Definition: ISerializer.cpp:95
void Serialize(ISerializer &serialize)
void Deserialize(const CParamNode &paramNode, IDeserializer &deserialize, entity_id_t ent)
bool GetProperty(jsval obj, const char *name, T &out)
Get the named property on the given object.
bool CallFunction(jsval val, const char *name, R &ret)
Call the named property on the given object, with return type R and 0 arguments.
virtual void ScriptVal(const char *name, jsval &out)=0
Deserialize a jsval, replacing &#39;out&#39;.
virtual const char * GetScriptGlobalHandlerName() const =0
ScriptInterface & m_ScriptInterface
jsval & getRef() const
Returns reference to the current value.
Definition: ScriptVal.cpp:52
jsval get() const
Returns the current value (or JSVAL_VOID if uninitialised).
Definition: ScriptVal.cpp:45
virtual const char * GetScriptHandlerName() const =0
Abstraction around a SpiderMonkey JSContext.
CScriptValRooted m_Instance
bool SetProperty(jsval obj, const char *name, const T &value, bool constant=false, bool enumerate=true)
Set the named property on the given object.
u32 entity_id_t
Entity ID type.
Definition: Entity.h:24
void HandleMessage(const CMessage &msg, bool global)
void Init(const CParamNode &paramNode, entity_id_t ent)
Deserialization interface; see serialization overview.
Definition: IDeserializer.h:34