JSSceneSerializable.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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. #include "JSScene.h"
  27. #include "JSComponent.h"
  28. #include "JSVM.h"
  29. // Refactor so we don't need to include AtomicNET here
  30. #include <AtomicNET/NETCore/CSComponent.h>
  31. #include <AtomicNET/NETCore/NETAssemblyFile.h>
  32. #include <AtomicNET/NETCore/NETComponentClass.h>
  33. namespace Atomic
  34. {
  35. /*
  36. /// Attribute type.
  37. VariantType type_;
  38. /// Name.
  39. String name_;
  40. /// Byte offset from start of object.
  41. unsigned offset_;
  42. /// Enum names.
  43. const char** enumNames_;
  44. /// Helper object for accessor mode.
  45. SharedPtr<AttributeAccessor> accessor_;
  46. /// Default value for network replication.
  47. Variant defaultValue_;
  48. /// Attribute mode: whether to use for serialization, network replication, or both.
  49. unsigned mode_;
  50. /// Attribute data pointer if elsewhere than in the Serializable.
  51. void* ptr_;
  52. */
  53. static int Serializable_SetAttribute(duk_context* ctx)
  54. {
  55. const char* name = duk_to_string(ctx, 0);
  56. Variant v;
  57. js_to_variant(ctx, 1, v);
  58. duk_push_this(ctx);
  59. Serializable* serial = js_to_class_instance<Serializable>(ctx, -1, 0);
  60. const Vector<AttributeInfo>* attributes = serial->GetAttributes();
  61. VariantType variantType = VAR_NONE;
  62. bool isAttr = false;
  63. if (attributes)
  64. {
  65. for (unsigned i = 0; i < attributes->Size(); i++)
  66. {
  67. const AttributeInfo* attr = &attributes->At(i);
  68. if (!attr->name_.Compare(name))
  69. {
  70. isAttr = true;
  71. variantType = attr->type_;
  72. break;
  73. }
  74. }
  75. }
  76. JSComponent* jsc = NULL;
  77. CSComponent* csc = NULL;
  78. // check dynamic
  79. if (!isAttr)
  80. {
  81. if (serial->GetType() == JSComponent::GetTypeStatic())
  82. {
  83. jsc = (JSComponent*) serial;
  84. JSComponentFile* file = jsc->GetComponentFile();
  85. if (file)
  86. {
  87. const HashMap<String, VariantType>& fields = file->GetFields();
  88. const HashMap<String, Vector<EnumInfo>>& enums = file->GetEnums();
  89. if (fields.Contains(name))
  90. {
  91. HashMap<String, VariantType>::ConstIterator itr = fields.Find(name);
  92. variantType = itr->second_;
  93. if (enums.Contains(name))
  94. {
  95. int idx = (int) v.GetFloat();
  96. if (idx > 0 && idx < enums[name]->Size())
  97. {
  98. VariantMap& values = jsc->GetFieldValues();
  99. values[name] = enums[name]->At(idx).value_;
  100. return 0;
  101. }
  102. }
  103. }
  104. }
  105. }
  106. else if (serial->GetType() == CSComponent::GetTypeStatic())
  107. {
  108. csc = (CSComponent*) serial;
  109. NETAssemblyFile* afile = csc->GetAssemblyFile();
  110. if (afile)
  111. {
  112. NETComponentClass* componentClass = afile->GetComponentClass(csc->GetComponentClassName());
  113. if (componentClass)
  114. {
  115. const HashMap<String, VariantType>& fields = componentClass->GetFields();
  116. const HashMap<String, Vector<EnumInfo>>& enums = componentClass->GetEnums();
  117. if (fields.Contains(name))
  118. {
  119. HashMap<String, VariantType>::ConstIterator itr = fields.Find(name);
  120. variantType = itr->second_;
  121. if (enums.Contains(name))
  122. {
  123. int idx = (int) v.GetFloat();
  124. if (idx > 0 && idx < enums[name]->Size())
  125. {
  126. VariantMap& values = csc->GetFieldValues();
  127. values[name] = enums[name]->At(idx).value_;
  128. return 0;
  129. }
  130. }
  131. }
  132. }
  133. }
  134. }
  135. }
  136. if (variantType == VAR_NONE)
  137. return 0;
  138. if (variantType == VAR_QUATERNION)
  139. {
  140. Vector3 v3 = v.GetVector3();
  141. Quaternion q;
  142. q.FromEulerAngles(v3.x_, v3.y_, v3.z_);
  143. v = q;
  144. }
  145. else if (variantType == VAR_COLOR)
  146. {
  147. Vector4 v4 = v.GetVector4();
  148. Color c(v4.x_, v4.y_, v4.z_, v4.w_ );
  149. v = c;
  150. }
  151. else if (variantType == VAR_INT)
  152. {
  153. v = (int) v.GetFloat();
  154. }
  155. else if (variantType == VAR_RESOURCEREF)
  156. {
  157. RefCounted* ref = v.GetPtr();
  158. if (ref && ref->IsObject())
  159. {
  160. Object* o = (Object*) ref;
  161. // TODO: calling code must ensure we are a resource, can this be done here?
  162. Resource* resource = (Resource*) o;
  163. v = ResourceRef(resource->GetType(), resource->GetName());
  164. }
  165. }
  166. if (isAttr)
  167. {
  168. serial->SetAttribute(name, v);
  169. return 0;
  170. }
  171. // check dynamic
  172. if (jsc)
  173. {
  174. VariantMap& values = jsc->GetFieldValues();
  175. values[name] = v;
  176. }
  177. if (csc)
  178. {
  179. VariantMap& values = csc->GetFieldValues();
  180. values[name] = v;
  181. }
  182. return 0;
  183. }
  184. static int Serializable_GetAttribute(duk_context* ctx)
  185. {
  186. const char* name = duk_to_string(ctx, 0);
  187. duk_push_this(ctx);
  188. Serializable* serial = js_to_class_instance<Serializable>(ctx, -1, 0);
  189. const Vector<AttributeInfo>* attrs = serial->GetAttributes();
  190. if (attrs)
  191. {
  192. for (unsigned i = 0; i < attrs->Size(); i++)
  193. {
  194. const AttributeInfo* attr = &attrs->At(i);
  195. if (!attr->name_.Compare(name))
  196. {
  197. // FIXME: this is a double lookup
  198. js_push_variant(ctx, serial->GetAttribute(name));
  199. return 1;
  200. }
  201. }
  202. }
  203. if (serial->GetType() == JSComponent::GetTypeStatic())
  204. {
  205. JSComponent* jsc = (JSComponent*) serial;
  206. JSComponentFile* file = jsc->GetComponentFile();
  207. if (file)
  208. {
  209. const HashMap<String, VariantType>& fields = file->GetFields();
  210. if (fields.Contains(name))
  211. {
  212. VariantMap& values = jsc->GetFieldValues();
  213. if (values.Contains(name))
  214. {
  215. js_push_variant(ctx, values[name]);
  216. return 1;
  217. }
  218. else
  219. {
  220. Variant v;
  221. file->GetDefaultFieldValue(name, v);
  222. js_push_variant(ctx, v);
  223. return 1;
  224. }
  225. }
  226. }
  227. }
  228. if (serial->GetType() == CSComponent::GetTypeStatic())
  229. {
  230. CSComponent* csc = (CSComponent*) serial;
  231. NETAssemblyFile* afile = csc->GetAssemblyFile();
  232. if (afile)
  233. {
  234. NETComponentClass* componentClass = afile->GetComponentClass(csc->GetComponentClassName());
  235. if (componentClass)
  236. {
  237. const HashMap<String, VariantType>& fields = componentClass->GetFields();
  238. if (fields.Contains(name))
  239. {
  240. VariantMap& values = csc->GetFieldValues();
  241. if (values.Contains(name))
  242. {
  243. js_push_variant(ctx, values[name]);
  244. return 1;
  245. }
  246. else
  247. {
  248. Variant v;
  249. componentClass->GetDefaultFieldValue(name, v);
  250. js_push_variant(ctx, v);
  251. return 1;
  252. }
  253. }
  254. }
  255. }
  256. }
  257. duk_push_undefined(ctx);
  258. return 1;
  259. }
  260. static const String& GetResourceRefClassName(Context* context, const ResourceRef& ref)
  261. {
  262. const HashMap<StringHash, SharedPtr<ObjectFactory>>& factories = context->GetObjectFactories();
  263. HashMap<StringHash, SharedPtr<ObjectFactory>>::ConstIterator itr = factories.Begin();
  264. while (itr != factories.End())
  265. {
  266. if (itr->first_ == ref.type_)
  267. {
  268. return itr->second_->GetTypeName();
  269. }
  270. itr++;
  271. }
  272. return String::EMPTY;
  273. }
  274. static void GetDynamicAttributes(duk_context* ctx, unsigned& count, const VariantMap& defaultFieldValues,
  275. const HashMap<String, VariantType>& fields,
  276. const HashMap<String, Vector<EnumInfo>>& enums)
  277. {
  278. if (fields.Size())
  279. {
  280. HashMap<String, VariantType>::ConstIterator itr = fields.Begin();
  281. while (itr != fields.End())
  282. {
  283. duk_push_object(ctx);
  284. duk_push_number(ctx, (double) itr->second_);
  285. duk_put_prop_string(ctx, -2, "type");
  286. if (itr->second_ == VAR_RESOURCEREF && defaultFieldValues.Contains(itr->first_))
  287. {
  288. if (defaultFieldValues[itr->first_]->GetType() == VAR_RESOURCEREF)
  289. {
  290. const ResourceRef& ref = defaultFieldValues[itr->first_]->GetResourceRef();
  291. const String& typeName = GetResourceRefClassName(JSVM::GetJSVM(ctx)->GetContext(), ref);
  292. if (typeName.Length())
  293. {
  294. duk_push_string(ctx, typeName.CString());
  295. duk_put_prop_string(ctx, -2, "resourceTypeName");
  296. }
  297. }
  298. }
  299. duk_push_string(ctx, itr->first_.CString());
  300. duk_put_prop_string(ctx, -2, "name");
  301. duk_push_number(ctx, (double) AM_DEFAULT);
  302. duk_put_prop_string(ctx, -2, "mode");
  303. duk_push_string(ctx,"");
  304. duk_put_prop_string(ctx, -2, "defaultValue");
  305. duk_push_boolean(ctx, 1);
  306. duk_put_prop_string(ctx, -2, "field");
  307. duk_push_array(ctx);
  308. if (enums.Contains(itr->first_))
  309. {
  310. unsigned enumCount = 0;
  311. const Vector<EnumInfo>* infos = enums[itr->first_];
  312. Vector<EnumInfo>::ConstIterator eitr = infos->Begin();
  313. while (eitr != infos->End())
  314. {
  315. duk_push_string(ctx, eitr->name_.CString());
  316. duk_put_prop_index(ctx, -2, enumCount++);
  317. eitr++;
  318. }
  319. }
  320. duk_put_prop_string(ctx, -2, "enumNames");
  321. // store attr object
  322. duk_put_prop_index(ctx, -2, count++);
  323. itr++;
  324. }
  325. }
  326. }
  327. static int Serializable_GetAttributes(duk_context* ctx)
  328. {
  329. duk_push_this(ctx);
  330. Serializable* serial = js_to_class_instance<Serializable>(ctx, -1, 0);
  331. unsigned type = serial->GetType().Value();
  332. duk_get_global_string(ctx, "__atomic_scene_serializable_attributes");
  333. duk_get_prop_index(ctx, -1, type);
  334. // return cached array of attrinfo, unless JSComponent which has dynamic fields
  335. if (serial->GetType() != JSComponent::GetTypeStatic() && duk_is_object(ctx, -1))
  336. return 1;
  337. const Vector<AttributeInfo>* attrs = serial->GetAttributes();
  338. duk_push_array(ctx);
  339. duk_dup(ctx, -1);
  340. duk_put_prop_index(ctx, -4, type);
  341. unsigned count = 0;
  342. if (attrs)
  343. {
  344. count = attrs->Size();
  345. for (unsigned i = 0; i < attrs->Size(); i++)
  346. {
  347. const AttributeInfo* attr = &attrs->At(i);
  348. if (attr->mode_ & AM_NOEDIT)
  349. continue;
  350. duk_push_object(ctx);
  351. duk_push_number(ctx, (double) attr->type_);
  352. duk_put_prop_string(ctx, -2, "type");
  353. if (attr->type_ == VAR_RESOURCEREF)
  354. {
  355. if (attr->defaultValue_.GetType() == VAR_RESOURCEREF)
  356. {
  357. const ResourceRef& ref = attr->defaultValue_.GetResourceRef();
  358. const String& typeName = GetResourceRefClassName(serial->GetContext(), ref);
  359. if (typeName.Length())
  360. {
  361. duk_push_string(ctx, typeName.CString());
  362. duk_put_prop_string(ctx, -2, "resourceTypeName");
  363. }
  364. }
  365. }
  366. duk_push_string(ctx, attr->name_.CString());
  367. duk_put_prop_string(ctx, -2, "name");
  368. duk_push_number(ctx, (double) attr->mode_);
  369. duk_put_prop_string(ctx, -2, "mode");
  370. duk_push_string(ctx,attr->defaultValue_.ToString().CString());
  371. duk_put_prop_string(ctx, -2, "defaultValue");
  372. duk_push_boolean(ctx, 0);
  373. duk_put_prop_string(ctx, -2, "field");
  374. duk_push_array(ctx);
  375. const char** enumPtr = attr->enumNames_;
  376. unsigned enumCount = 0;
  377. if (enumPtr)
  378. {
  379. while (*enumPtr)
  380. {
  381. duk_push_string(ctx, *enumPtr);
  382. duk_put_prop_index(ctx, -2, enumCount++);
  383. enumPtr++;
  384. }
  385. }
  386. duk_put_prop_string(ctx, -2, "enumNames");
  387. // store attr object
  388. duk_put_prop_index(ctx, -2, i);
  389. }
  390. }
  391. // dynamic script fields
  392. if (serial->GetType() == JSComponent::GetTypeStatic())
  393. {
  394. JSComponent* jsc = (JSComponent*) serial;
  395. JSComponentFile* file = jsc->GetComponentFile();
  396. if (file)
  397. {
  398. const VariantMap& defaultFieldValues = file->GetDefaultFieldValues();
  399. const HashMap<String, VariantType>& fields = file->GetFields();
  400. const HashMap<String, Vector<EnumInfo>>& enums = file->GetEnums();
  401. GetDynamicAttributes(ctx, count, defaultFieldValues, fields, enums);
  402. }
  403. }
  404. else if (serial->GetType() == CSComponent::GetTypeStatic())
  405. {
  406. CSComponent* csc = (CSComponent*) serial;
  407. NETAssemblyFile* afile = csc->GetAssemblyFile();
  408. if (afile)
  409. {
  410. NETComponentClass* componentClass = afile->GetComponentClass(csc->GetComponentClassName());
  411. if (componentClass)
  412. {
  413. const VariantMap& defaultFieldValues = componentClass->GetDefaultFieldValues();
  414. const HashMap<String, VariantType>& fields = componentClass->GetFields();
  415. const HashMap<String, Vector<EnumInfo>>& enums = componentClass->GetEnums();
  416. GetDynamicAttributes(ctx, count, defaultFieldValues, fields, enums);
  417. }
  418. }
  419. }
  420. return 1;
  421. }
  422. void jsapi_init_scene_serializable(JSVM* vm)
  423. {
  424. duk_context* ctx = vm->GetJSContext();
  425. // cached attr
  426. duk_push_object(ctx);
  427. duk_put_global_string(ctx, "__atomic_scene_serializable_attributes");
  428. js_class_get_prototype(ctx, "Atomic", "Serializable");
  429. duk_push_c_function(ctx, Serializable_GetAttributes, 0);
  430. duk_put_prop_string(ctx, -2, "getAttributes");
  431. duk_push_c_function(ctx, Serializable_GetAttribute, 1);
  432. duk_put_prop_string(ctx, -2, "getAttribute");
  433. duk_push_c_function(ctx, Serializable_SetAttribute, 2);
  434. duk_put_prop_string(ctx, -2, "setAttribute");
  435. duk_pop(ctx);
  436. }
  437. }