Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MessageTypeConversions.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2013 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 "ps/CLogger.h"
23 
24 #include "js/jsapi.h"
25 
26 #define TOJSVAL_SETUP() \
27  JSObject* obj = JS_NewObject(scriptInterface.GetContext(), NULL, NULL, NULL); \
28  if (! obj) \
29  return JSVAL_VOID;
30 
31 #define SET_MSG_PROPERTY(name) \
32  do { \
33  jsval prop = ScriptInterface::ToJSVal(scriptInterface.GetContext(), this->name); \
34  if (! JS_SetProperty(scriptInterface.GetContext(), obj, #name, &prop)) \
35  return JSVAL_VOID; \
36  } while (0);
37 
38 #define FROMJSVAL_SETUP() \
39  if (! JSVAL_IS_OBJECT(val)) \
40  return NULL; \
41  JSObject* obj = JSVAL_TO_OBJECT(val); \
42  jsval prop;
43 
44 #define GET_MSG_PROPERTY(type, name) \
45  if (! JS_GetProperty(scriptInterface.GetContext(), obj, #name, &prop)) \
46  return NULL; \
47  type name; \
48  if (! ScriptInterface::FromJSVal(scriptInterface.GetContext(), prop, name)) \
49  return NULL;
50 
51 jsval CMessage::ToJSValCached(ScriptInterface& scriptInterface) const
52 {
53  if (m_Cached.uninitialised())
54  m_Cached = CScriptValRooted(scriptInterface.GetContext(), ToJSVal(scriptInterface));
55 
56  return m_Cached.get();
57 }
58 
59 ////////////////////////////////
60 
61 jsval CMessageTurnStart::ToJSVal(ScriptInterface& scriptInterface) const
62 {
63  TOJSVAL_SETUP();
64  return OBJECT_TO_JSVAL(obj);
65 }
66 
68 {
69  return new CMessageTurnStart();
70 }
71 
72 ////////////////////////////////
73 
74 #define MESSAGE_1(name, t0, a0) \
75  jsval CMessage##name::ToJSVal(ScriptInterface& scriptInterface) const \
76  { \
77  TOJSVAL_SETUP(); \
78  SET_MSG_PROPERTY(a0); \
79  return OBJECT_TO_JSVAL(obj); \
80  } \
81  CMessage* CMessage##name::FromJSVal(ScriptInterface& scriptInterface, jsval val) \
82  { \
83  FROMJSVAL_SETUP(); \
84  GET_MSG_PROPERTY(t0, a0); \
85  return new CMessage##name(a0); \
86  }
87 
88 MESSAGE_1(Update, fixed, turnLength)
89 MESSAGE_1(Update_MotionFormation, fixed, turnLength)
90 MESSAGE_1(Update_MotionUnit, fixed, turnLength)
91 MESSAGE_1(Update_Final, fixed, turnLength)
92 
93 ////////////////////////////////
94 
95 jsval CMessageInterpolate::ToJSVal(ScriptInterface& scriptInterface) const
96 {
97  TOJSVAL_SETUP();
98  SET_MSG_PROPERTY(deltaSimTime);
99  SET_MSG_PROPERTY(offset);
100  SET_MSG_PROPERTY(deltaRealTime);
101  return OBJECT_TO_JSVAL(obj);
102 }
103 
105 {
106  FROMJSVAL_SETUP();
108  GET_MSG_PROPERTY(float, offset);
111 }
112 
113 ////////////////////////////////
114 
116 {
117  LOGWARNING(L"CMessageRenderSubmit::ToJSVal not implemented");
118  return JSVAL_VOID;
119 }
120 
122 {
123  LOGWARNING(L"CMessageRenderSubmit::FromJSVal not implemented");
124  return NULL;
125 }
126 
127 ////////////////////////////////
128 
130 {
131  LOGWARNING(L"CMessageProgressiveLoad::ToJSVal not implemented");
132  return JSVAL_VOID;
133 }
134 
136 {
137  LOGWARNING(L"CMessageProgressiveLoad::FromJSVal not implemented");
138  return NULL;
139 }
140 
141 ////////////////////////////////
142 
143 jsval CMessageCreate::ToJSVal(ScriptInterface& scriptInterface) const
144 {
145  TOJSVAL_SETUP();
147  return OBJECT_TO_JSVAL(obj);
148 }
149 
151 {
152  FROMJSVAL_SETUP();
154  return new CMessageCreate(entity);
155 }
156 
157 ////////////////////////////////
158 
159 jsval CMessageDestroy::ToJSVal(ScriptInterface& scriptInterface) const
160 {
161  TOJSVAL_SETUP();
163  return OBJECT_TO_JSVAL(obj);
164 }
165 
167 {
168  FROMJSVAL_SETUP();
170  return new CMessageDestroy(entity);
171 }
172 
173 ////////////////////////////////
174 
176 {
177  TOJSVAL_SETUP();
181  return OBJECT_TO_JSVAL(obj);
182 }
183 
185 {
186  FROMJSVAL_SETUP();
190  return new CMessageOwnershipChanged(entity, from, to);
191 }
192 
193 ////////////////////////////////
194 
196 {
197  TOJSVAL_SETUP();
203  return OBJECT_TO_JSVAL(obj);
204 }
205 
207 {
208  FROMJSVAL_SETUP();
210  GET_MSG_PROPERTY(bool, inWorld);
214  return new CMessagePositionChanged(entity, inWorld, x, z, a);
215 }
216 
217 ////////////////////////////////
218 
219 jsval CMessageMotionChanged::ToJSVal(ScriptInterface& scriptInterface) const
220 {
221  TOJSVAL_SETUP();
224  return OBJECT_TO_JSVAL(obj);
225 }
226 
228 {
229  FROMJSVAL_SETUP();
230  GET_MSG_PROPERTY(bool, starting);
231  GET_MSG_PROPERTY(bool, error);
232  return new CMessageMotionChanged(starting, error);
233 }
234 
235 ////////////////////////////////
236 
238 {
239  TOJSVAL_SETUP();
244  return OBJECT_TO_JSVAL(obj);
245 }
246 
248 {
249  FROMJSVAL_SETUP();
250  GET_MSG_PROPERTY(int32_t, i0);
251  GET_MSG_PROPERTY(int32_t, j0);
252  GET_MSG_PROPERTY(int32_t, i1);
253  GET_MSG_PROPERTY(int32_t, j1);
254  return new CMessageTerrainChanged(i0, i1, j0, j1);
255 }
256 
257 ////////////////////////////////
258 
260 {
261  TOJSVAL_SETUP();
262  return OBJECT_TO_JSVAL(obj);
263 }
264 
266 {
267  return new CMessageTerritoriesChanged();
268 }
269 
270 ////////////////////////////////
271 
272 jsval CMessageRangeUpdate::ToJSVal(ScriptInterface& scriptInterface) const
273 {
274  TOJSVAL_SETUP();
278  return OBJECT_TO_JSVAL(obj);
279 }
280 
282 {
283  LOGWARNING(L"CMessageRangeUpdate::FromJSVal not implemented");
284  return NULL;
285 }
286 
287 ////////////////////////////////
288 
289 jsval CMessagePathResult::ToJSVal(ScriptInterface& UNUSED(scriptInterface)) const
290 {
291  LOGWARNING(L"CMessagePathResult::ToJSVal not implemented");
292  return JSVAL_VOID;
293 }
294 
296 {
297  LOGWARNING(L"CMessagePathResult::FromJSVal not implemented");
298  return NULL;
299 }
300 
301 ////////////////////////////////
302 
304 {
305  TOJSVAL_SETUP();
308  return OBJECT_TO_JSVAL(obj);
309 }
310 
312 {
313  FROMJSVAL_SETUP();
314  GET_MSG_PROPERTY(std::wstring, component);
317 }
318 
319 ////////////////////////////////
320 
322 {
323  TOJSVAL_SETUP();
327  return OBJECT_TO_JSVAL(obj);
328 }
329 
331 {
332  FROMJSVAL_SETUP();
337 }
338 
339 ////////////////////////////////
340 
341 jsval CMessageMinimapPing::ToJSVal(ScriptInterface& scriptInterface) const
342 {
343  TOJSVAL_SETUP();
344  return OBJECT_TO_JSVAL(obj);
345 }
346 
348 {
349  return new CMessageMinimapPing();
350 }
351 
352 ////////////////////////////////////////////////////////////////
353 
354 CMessage* CMessageFromJSVal(int mtid, ScriptInterface& scriptingInterface, jsval val)
355 {
356  switch (mtid)
357  {
358 #define MESSAGE(name) case MT_##name: return CMessage##name::FromJSVal(scriptingInterface, val);
359 #define INTERFACE(name)
360 #define COMPONENT(name)
361 #include "simulation2/TypeList.h"
362 #undef COMPONENT
363 #undef INTERFACE
364 #undef MESSAGE
365  }
366 
367  return NULL;
368 }
A simple fixed-point number class.
Definition: Fixed.h:115
bool uninitialised() const
Returns whether the value is uninitialised.
Definition: ScriptVal.cpp:63
virtual jsval ToJSVal(ScriptInterface &scriptInterface) const
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
static CMessage * FromJSVal(ScriptInterface &, jsval val)
entity_id_t entity
Definition: MessageTypes.h:221
jsval ToJSValCached(ScriptInterface &) const
static CMessage * FromJSVal(ScriptInterface &, jsval val)
virtual jsval ToJSVal(ScriptInterface &) const =0
virtual jsval ToJSVal(ScriptInterface &scriptInterface) const
static CMessage * FromJSVal(ScriptInterface &, jsval val)
virtual jsval ToJSVal(ScriptInterface &scriptInterface) const
std::vector< entity_id_t > removed
Definition: MessageTypes.h:321
CMessageMotionChanged(bool starting, bool error)
Definition: MessageTypes.h:270
static CMessage * FromJSVal(ScriptInterface &, jsval val)
virtual jsval ToJSVal(ScriptInterface &scriptInterface) const
static CMessage * FromJSVal(ScriptInterface &, jsval val)
CMessageCreate(entity_id_t entity)
Definition: MessageTypes.h:196
virtual jsval ToJSVal(ScriptInterface &scriptInterface) const
float deltaSimTime
Elapsed simulation time since previous interpolate, in seconds.
Definition: MessageTypes.h:134
CMessageOwnershipChanged(entity_id_t entity, player_id_t from, player_id_t to)
Definition: MessageTypes.h:229
static CMessage * FromJSVal(ScriptInterface &, jsval val)
int32_t player_id_t
valid player IDs are non-negative (see ICmpOwnership)
Definition: Player.h:24
float deltaRealTime
Elapsed real time since previous interpolate, in seconds.
Definition: MessageTypes.h:138
#define MESSAGE_1(name, t0, a0)
CMessageTerrainChanged(int32_t i0, int32_t j0, int32_t i1, int32_t j1)
Definition: MessageTypes.h:287
#define LOGWARNING
Definition: CLogger.h:34
static CMessage * FromJSVal(ScriptInterface &, jsval val)
virtual jsval ToJSVal(ScriptInterface &scriptInterface) const
virtual jsval ToJSVal(ScriptInterface &scriptInterface) const
CMessageInterpolate(float deltaSimTime, float offset, float deltaRealTime)
Definition: MessageTypes.h:127
#define TOJSVAL_SETUP()
#define GET_MSG_PROPERTY(type, name)
static CMessage * FromJSVal(ScriptInterface &, jsval val)
virtual jsval ToJSVal(ScriptInterface &scriptInterface) const
CMessageVisionRangeChanged(entity_id_t entity, entity_pos_t oldRange, entity_pos_t newRange)
Definition: MessageTypes.h:393
static CMessage * FromJSVal(ScriptInterface &, jsval val)
float offset
Range [0, 1] (inclusive); fractional time of current frame between previous/next simulation turns...
Definition: MessageTypes.h:136
CMessageDestroy(entity_id_t entity)
Definition: MessageTypes.h:216
static CMessage * FromJSVal(ScriptInterface &, jsval val)
entity_id_t entity
Definition: MessageTypes.h:201
virtual jsval ToJSVal(ScriptInterface &scriptInterface) const
CMessagePositionChanged(entity_id_t entity, bool inWorld, entity_pos_t x, entity_pos_t z, entity_angle_t a)
Definition: MessageTypes.h:250
static CMessage * FromJSVal(ScriptInterface &, jsval val)
CMessage * CMessageFromJSVal(int mtid, ScriptInterface &scriptingInterface, jsval val)
virtual jsval ToJSVal(ScriptInterface &scriptInterface) const
#define SET_MSG_PROPERTY(name)
CMessageTechnologyModification(std::wstring component, player_id_t player)
Definition: MessageTypes.h:376
virtual jsval ToJSVal(ScriptInterface &scriptInterface) const
static CMessage * FromJSVal(ScriptInterface &, jsval val)
jsval ToJSVal(T &Native)
Definition: JSConversions.h:87
virtual jsval ToJSVal(ScriptInterface &scriptInterface) const
jsval get() const
Returns the current value (or JSVAL_VOID if uninitialised).
Definition: ScriptVal.cpp:45
static CMessage * FromJSVal(ScriptInterface &, jsval val)
Abstraction around a SpiderMonkey JSContext.
CScriptValRooted m_Cached
Definition: Message.h:37
static CMessage * FromJSVal(ScriptInterface &, jsval val)
static CMessage * FromJSVal(ScriptInterface &, jsval val)
virtual jsval ToJSVal(ScriptInterface &scriptInterface) const
Prepare for rendering a new frame (set up model positions etc).
Definition: MessageTypes.h:122
u32 entity_id_t
Entity ID type.
Definition: Entity.h:24
virtual jsval ToJSVal(ScriptInterface &scriptInterface) const
static CMessage * FromJSVal(ScriptInterface &, jsval val)
virtual jsval ToJSVal(ScriptInterface &scriptInterface) const
#define FROMJSVAL_SETUP()
JSContext * GetContext() const
std::vector< entity_id_t > added
Definition: MessageTypes.h:320