JSSceneSerializable.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/Resource/ResourceCache.h>
  23. #include <Atomic/IO/File.h>
  24. #include <Atomic/Scene/Node.h>
  25. #include <Atomic/Scene/Scene.h>
  26. // These serialization functions need to operate on various script classes
  27. // including JS and C#, so use the base classes and avoid bringing in derived specifics
  28. #include <Atomic/Script/ScriptComponent.h>
  29. #include <Atomic/Script/ScriptComponentFile.h>
  30. #include "JSScene.h"
  31. #include "JSVM.h"
  32. namespace Atomic
  33. {
  34. /*
  35. /// Attribute type.
  36. VariantType type_;
  37. /// Name.
  38. String name_;
  39. /// Byte offset from start of object.
  40. unsigned offset_;
  41. /// Enum names.
  42. const char** enumNames_;
  43. /// Helper object for accessor mode.
  44. SharedPtr<AttributeAccessor> accessor_;
  45. /// Default value for network replication.
  46. Variant defaultValue_;
  47. /// Attribute mode: whether to use for serialization, network replication, or both.
  48. unsigned mode_;
  49. /// Attribute data pointer if elsewhere than in the Serializable.
  50. void* ptr_;
  51. */
  52. static int Serializable_SetAttribute(duk_context* ctx)
  53. {
  54. const char* name = duk_to_string(ctx, 0);
  55. duk_push_this(ctx);
  56. Serializable* serial = js_to_class_instance<Serializable>(ctx, -1, 0);
  57. const Vector<AttributeInfo>* attributes = serial->GetAttributes();
  58. VariantType variantType = VAR_NONE;
  59. bool isAttr = false;
  60. if (attributes)
  61. {
  62. for (unsigned i = 0; i < attributes->Size(); i++)
  63. {
  64. const AttributeInfo* attr = &attributes->At(i);
  65. if (!attr->name_.Compare(name))
  66. {
  67. isAttr = true;
  68. variantType = attr->type_;
  69. break;
  70. }
  71. }
  72. }
  73. Variant v;
  74. js_to_variant(ctx, 1, v, variantType);
  75. ScriptComponent* jsc = NULL;
  76. // check dynamic
  77. if (!isAttr)
  78. {
  79. if (serial->GetBaseType() == ScriptComponent::GetTypeStatic())
  80. {
  81. jsc = (ScriptComponent*) serial;
  82. ScriptComponentFile* file = jsc->GetComponentFile();
  83. if (file)
  84. {
  85. const String& className = jsc->GetComponentClassName();
  86. const HashMap<String, VariantType>& fields = file->GetFields(className);
  87. const HashMap<String, Vector<EnumInfo>>& enums = file->GetEnums(className);
  88. if (VariantType *fvType = fields[name])
  89. {
  90. variantType = *fvType;
  91. if (enums.Contains(name))
  92. {
  93. int idx = (int) v.GetFloat();
  94. if (idx > 0 && idx < enums[name]->Size())
  95. {
  96. VariantMap& values = jsc->GetFieldValues();
  97. values[name] = enums[name]->At(idx).value_;
  98. return 0;
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }
  105. if (variantType == VAR_NONE)
  106. return 0;
  107. if (variantType == VAR_QUATERNION)
  108. {
  109. Vector3 v3 = v.GetVector3();
  110. Quaternion q;
  111. q.FromEulerAngles(v3.x_, v3.y_, v3.z_);
  112. v = q;
  113. }
  114. else if (variantType == VAR_COLOR)
  115. {
  116. Vector4 v4 = v.GetVector4();
  117. Color c(v4.x_, v4.y_, v4.z_, v4.w_ );
  118. v = c;
  119. }
  120. else if (variantType == VAR_INT)
  121. {
  122. v = (int) v.GetFloat();
  123. }
  124. else if (variantType == VAR_RESOURCEREF)
  125. {
  126. RefCounted* ref = v.GetPtr();
  127. if (ref && ref->IsObject())
  128. {
  129. Object* o = (Object*) ref;
  130. // TODO: calling code must ensure we are a resource, can this be done here?
  131. Resource* resource = (Resource*) o;
  132. v = ResourceRef(resource->GetType(), resource->GetName());
  133. }
  134. }
  135. if (isAttr)
  136. {
  137. serial->SetAttribute(name, v);
  138. return 0;
  139. }
  140. // check dynamic
  141. if (jsc)
  142. {
  143. VariantMap& values = jsc->GetFieldValues();
  144. values[name] = v;
  145. }
  146. return 0;
  147. }
  148. static int Serializable_GetAttribute(duk_context* ctx)
  149. {
  150. const char* name = duk_to_string(ctx, 0);
  151. duk_push_this(ctx);
  152. Serializable* serial = js_to_class_instance<Serializable>(ctx, -1, 0);
  153. const Vector<AttributeInfo>* attrs = serial->GetAttributes();
  154. if (attrs)
  155. {
  156. for (unsigned i = 0; i < attrs->Size(); i++)
  157. {
  158. const AttributeInfo* attr = &attrs->At(i);
  159. if (!attr->name_.Compare(name))
  160. {
  161. // FIXME: this is a double lookup
  162. js_push_variant(ctx, serial->GetAttribute(name));
  163. return 1;
  164. }
  165. }
  166. }
  167. if (serial->GetBaseType() == ScriptComponent::GetTypeStatic())
  168. {
  169. ScriptComponent* jsc = (ScriptComponent*) serial;
  170. ScriptComponentFile* file = jsc->GetComponentFile();
  171. if (file)
  172. {
  173. const String& componentClassName = jsc->GetComponentClassName();
  174. const FieldMap& fields = file->GetFields(componentClassName);
  175. if (fields.Contains(name))
  176. {
  177. const VariantMap& values = jsc->GetFieldValues();
  178. if (Variant* vptr = values[name])
  179. {
  180. js_push_variant(ctx, *vptr);
  181. return 1;
  182. }
  183. else
  184. {
  185. Variant v;
  186. file->GetDefaultFieldValue(name, v, componentClassName);
  187. js_push_variant(ctx, v);
  188. return 1;
  189. }
  190. }
  191. }
  192. }
  193. duk_push_undefined(ctx);
  194. return 1;
  195. }
  196. static void GetDynamicAttributes(duk_context* ctx, unsigned& count, const VariantMap& defaultFieldValues,
  197. const FieldMap& fields,
  198. const EnumMap& enums,
  199. const FieldTooltipMap& tooltips)
  200. {
  201. if (fields.Size())
  202. {
  203. HashMap<String, VariantType>::ConstIterator itr = fields.Begin();
  204. while (itr != fields.End())
  205. {
  206. duk_push_object(ctx);
  207. duk_push_number(ctx, (double) itr->second_);
  208. duk_put_prop_string(ctx, -2, "type");
  209. if (itr->second_ == VAR_RESOURCEREF && defaultFieldValues.Contains(itr->first_))
  210. {
  211. if (defaultFieldValues[itr->first_]->GetType() == VAR_RESOURCEREF)
  212. {
  213. const ResourceRef& ref = defaultFieldValues[itr->first_]->GetResourceRef();
  214. const String& typeName = JSVM::GetJSVM(ctx)->GetContext()->GetTypeName(ref.type_);
  215. if (typeName.Length())
  216. {
  217. duk_push_string(ctx, typeName.CString());
  218. duk_put_prop_string(ctx, -2, "resourceTypeName");
  219. }
  220. }
  221. }
  222. duk_push_string(ctx, itr->first_.CString());
  223. duk_put_prop_string(ctx, -2, "name");
  224. duk_push_number(ctx, (double) AM_DEFAULT);
  225. duk_put_prop_string(ctx, -2, "mode");
  226. duk_push_string(ctx,"");
  227. duk_put_prop_string(ctx, -2, "defaultValue");
  228. duk_push_boolean(ctx, 1);
  229. duk_put_prop_string(ctx, -2, "dynamic");
  230. if (tooltips.Contains(itr->first_))
  231. {
  232. duk_push_string(ctx, tooltips[itr->first_]->CString());
  233. duk_put_prop_string(ctx, -2, "tooltip");
  234. }
  235. duk_push_array(ctx);
  236. if (enums.Contains(itr->first_))
  237. {
  238. unsigned enumCount = 0;
  239. const Vector<EnumInfo>* infos = enums[itr->first_];
  240. Vector<EnumInfo>::ConstIterator eitr = infos->Begin();
  241. while (eitr != infos->End())
  242. {
  243. duk_push_string(ctx, eitr->name_.CString());
  244. duk_put_prop_index(ctx, -2, enumCount++);
  245. eitr++;
  246. }
  247. }
  248. duk_put_prop_string(ctx, -2, "enumNames");
  249. // store attr object
  250. duk_put_prop_index(ctx, -2, count++);
  251. itr++;
  252. }
  253. }
  254. }
  255. static int Serializable_GetAttributes(duk_context* ctx)
  256. {
  257. duk_push_this(ctx);
  258. Serializable* serial = js_to_class_instance<Serializable>(ctx, -1, 0);
  259. unsigned type = serial->GetType().Value();
  260. duk_get_global_string(ctx, "__atomic_scene_serializable_attributes");
  261. duk_get_prop_index(ctx, -1, type);
  262. // return cached array of attrinfo, unless JSComponent which has dynamic fields
  263. if (serial->GetBaseType() != ScriptComponent::GetTypeStatic() && duk_is_object(ctx, -1))
  264. return 1;
  265. const Vector<AttributeInfo>* attrs = serial->GetAttributes();
  266. duk_push_array(ctx);
  267. duk_dup(ctx, -1);
  268. duk_put_prop_index(ctx, -4, type);
  269. unsigned count = 0;
  270. if (attrs)
  271. {
  272. count = attrs->Size();
  273. for (unsigned i = 0; i < attrs->Size(); i++)
  274. {
  275. const AttributeInfo* attr = &attrs->At(i);
  276. if (attr->mode_ & AM_NOEDIT)
  277. continue;
  278. duk_push_object(ctx);
  279. duk_push_number(ctx, (double) attr->type_);
  280. duk_put_prop_string(ctx, -2, "type");
  281. if (attr->type_ == VAR_RESOURCEREF)
  282. {
  283. if (attr->defaultValue_.GetType() == VAR_RESOURCEREF)
  284. {
  285. const ResourceRef& ref = attr->defaultValue_.GetResourceRef();
  286. const String& typeName = serial->GetContext()->GetTypeName(ref.type_);
  287. if (typeName.Length())
  288. {
  289. duk_push_string(ctx, typeName.CString());
  290. duk_put_prop_string(ctx, -2, "resourceTypeName");
  291. }
  292. }
  293. }
  294. if (attr->type_ == VAR_RESOURCEREFLIST)
  295. {
  296. if (attr->defaultValue_.GetType() == VAR_RESOURCEREFLIST)
  297. {
  298. const ResourceRefList& ref = attr->defaultValue_.GetResourceRefList();
  299. const String& typeName = serial->GetContext()->GetTypeName(ref.type_);
  300. if (typeName.Length())
  301. {
  302. duk_push_string(ctx, typeName.CString());
  303. duk_put_prop_string(ctx, -2, "resourceTypeName");
  304. }
  305. }
  306. }
  307. duk_push_string(ctx, attr->name_.CString());
  308. duk_put_prop_string(ctx, -2, "name");
  309. duk_push_number(ctx, (double) attr->mode_);
  310. duk_put_prop_string(ctx, -2, "mode");
  311. duk_push_string(ctx,attr->defaultValue_.ToString().CString());
  312. duk_put_prop_string(ctx, -2, "defaultValue");
  313. duk_push_boolean(ctx, 0);
  314. duk_put_prop_string(ctx, -2, "dynamic");
  315. duk_push_array(ctx);
  316. const char** enumPtr = attr->enumNames_;
  317. unsigned enumCount = 0;
  318. if (enumPtr)
  319. {
  320. while (*enumPtr)
  321. {
  322. duk_push_string(ctx, *enumPtr);
  323. duk_put_prop_index(ctx, -2, enumCount++);
  324. enumPtr++;
  325. }
  326. }
  327. duk_put_prop_string(ctx, -2, "enumNames");
  328. // store attr object
  329. duk_put_prop_index(ctx, -2, i);
  330. }
  331. }
  332. // dynamic script fields
  333. if (serial->GetBaseType() == ScriptComponent::GetTypeStatic())
  334. {
  335. ScriptComponent* jsc = (ScriptComponent*) serial;
  336. ScriptComponentFile* file = jsc->GetComponentFile();
  337. if (file)
  338. {
  339. const String& className = jsc->GetComponentClassName();
  340. const VariantMap& defaultFieldValues = file->GetDefaultFieldValues(className);
  341. const FieldMap& fields = file->GetFields(className);
  342. const FieldTooltipMap& fieldTooltips = file->GetFieldTooltips(className);
  343. const EnumMap& enums = file->GetEnums(className);
  344. GetDynamicAttributes(ctx, count, defaultFieldValues, fields, enums, fieldTooltips);
  345. }
  346. }
  347. return 1;
  348. }
  349. void jsapi_init_scene_serializable(JSVM* vm)
  350. {
  351. duk_context* ctx = vm->GetJSContext();
  352. // cached attr
  353. duk_push_object(ctx);
  354. duk_put_global_string(ctx, "__atomic_scene_serializable_attributes");
  355. js_class_get_prototype(ctx, "Atomic", "Serializable");
  356. duk_push_c_function(ctx, Serializable_GetAttributes, 0);
  357. duk_put_prop_string(ctx, -2, "getAttributes");
  358. duk_push_c_function(ctx, Serializable_GetAttribute, 1);
  359. duk_put_prop_string(ctx, -2, "getAttribute");
  360. duk_push_c_function(ctx, Serializable_SetAttribute, 2);
  361. duk_put_prop_string(ctx, -2, "setAttribute");
  362. duk_pop(ctx);
  363. }
  364. }