JSSceneSerializable.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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/ScriptVector.h>
  29. #include <Atomic/Script/ScriptComponent.h>
  30. #include <Atomic/Script/ScriptComponentFile.h>
  31. #include "JSScene.h"
  32. #include "JSVM.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. // for setting array values
  57. int arrayIndex = -1;
  58. if (duk_get_top(ctx) > 2)
  59. {
  60. float _arrayIndex = (float) duk_get_number(ctx, 2);
  61. if (_arrayIndex >= 0)
  62. arrayIndex = _arrayIndex;
  63. }
  64. duk_push_this(ctx);
  65. Serializable* serial = js_to_class_instance<Serializable>(ctx, -1, 0);
  66. const Vector<AttributeInfo>* attributes = serial->GetAttributes();
  67. VariantType variantType = VAR_NONE;
  68. VariantType arrayVariantType = VAR_NONE;
  69. bool isAttr = false;
  70. if (attributes)
  71. {
  72. for (unsigned i = 0; i < attributes->Size(); i++)
  73. {
  74. const AttributeInfo* attr = &attributes->At(i);
  75. if (!attr->name_.Compare(name))
  76. {
  77. isAttr = true;
  78. variantType = attr->type_;
  79. break;
  80. }
  81. }
  82. }
  83. ScriptComponent* jsc = 0;
  84. FieldInfo *fieldInfo = 0;
  85. const HashMap<String, Vector<EnumInfo>>* enums = 0;
  86. // check dynamic
  87. if (!isAttr)
  88. {
  89. if (serial->GetBaseType() == ScriptComponent::GetTypeStatic())
  90. {
  91. jsc = (ScriptComponent*)serial;
  92. ScriptComponentFile* file = jsc->GetComponentFile();
  93. if (file)
  94. {
  95. const String& className = jsc->GetComponentClassName();
  96. const FieldMap& fields = file->GetFields(className);
  97. const HashMap<String, Vector<EnumInfo>>& enums = file->GetEnums(className);
  98. if (fieldInfo = fields[name])
  99. {
  100. variantType = fieldInfo->variantType_;
  101. if (fieldInfo->isArray_)
  102. {
  103. arrayVariantType = variantType;
  104. variantType = VAR_VARIANTVECTOR;
  105. }
  106. }
  107. }
  108. }
  109. }
  110. Variant v;
  111. js_to_variant(ctx, 1, v, variantType);
  112. if (enums && enums->Contains(name))
  113. {
  114. int idx = (int)v.GetFloat();
  115. if (idx > 0 && idx < (*enums)[name]->Size())
  116. {
  117. VariantMap& values = jsc->GetFieldValues();
  118. values[name] = (*enums)[name]->At(idx).value_;
  119. return 0;
  120. }
  121. }
  122. if (variantType == VAR_NONE)
  123. return 0;
  124. if (variantType == VAR_QUATERNION)
  125. {
  126. Vector3 v3 = v.GetVector3();
  127. Quaternion q;
  128. q.FromEulerAngles(v3.x_, v3.y_, v3.z_);
  129. v = q;
  130. }
  131. else if (variantType == VAR_COLOR)
  132. {
  133. Vector4 v4 = v.GetVector4();
  134. Color c(v4.x_, v4.y_, v4.z_, v4.w_);
  135. v = c;
  136. }
  137. else if (variantType == VAR_INT)
  138. {
  139. v = (int)v.GetFloat();
  140. }
  141. else if (variantType == VAR_RESOURCEREF)
  142. {
  143. RefCounted* ref = v.GetPtr();
  144. if (ref && ref->IsObject())
  145. {
  146. Object* o = (Object*)ref;
  147. // TODO: calling code must ensure we are a resource, can this be done here?
  148. Resource* resource = (Resource*)o;
  149. v = ResourceRef(resource->GetType(), resource->GetName());
  150. }
  151. else
  152. {
  153. v = ResourceRef();
  154. }
  155. }
  156. else if (variantType == VAR_VARIANTVECTOR)
  157. {
  158. if (arrayIndex >= 0)
  159. {
  160. assert(fieldInfo);
  161. const VariantMap& values = jsc->GetFieldValues();
  162. Variant *v2 = values[name];
  163. // we're setting an index value
  164. if (v2 && v2->GetType() == VAR_VARIANTVECTOR)
  165. {
  166. if (arrayIndex < v2->GetVariantVector().Size())
  167. {
  168. VariantVector* vector = v2->GetVariantVectorPtr();
  169. if (arrayVariantType == VAR_RESOURCEREF)
  170. {
  171. RefCounted* ref = v.GetPtr();
  172. if (ref && ref->IsObject())
  173. {
  174. Object* o = (Object*)ref;
  175. // TODO: calling code must ensure we are a resource, can this be done here?
  176. Resource* resource = (Resource*)o;
  177. if (resource->GetType() == StringHash(fieldInfo->resourceTypeName_))
  178. v = ResourceRef(resource->GetType(), resource->GetName());
  179. else
  180. v = ResourceRef();
  181. }
  182. else
  183. {
  184. v = ResourceRef();
  185. }
  186. }
  187. else if (v.GetType() == VAR_VECTOR4)
  188. {
  189. if (arrayVariantType == VAR_COLOR)
  190. {
  191. const Vector4& v4 = v.GetVector4();
  192. v = Color(v4.x_, v4.y_, v4.z_, v4.w_);
  193. }
  194. else if (arrayVariantType == VAR_QUATERNION)
  195. {
  196. const Vector4& v4 = v.GetVector4();
  197. v = Quaternion(v4.w_, v4.x_, v4.y_, v4.z_);
  198. }
  199. }
  200. else if (v.GetType() == VAR_FLOAT)
  201. {
  202. if (arrayVariantType == VAR_INT)
  203. v = (int)v.GetFloat();
  204. }
  205. (*vector)[arrayIndex] = v;
  206. jsc->GetFieldValues()[name] = *vector;
  207. return 0;
  208. }
  209. }
  210. }
  211. else
  212. {
  213. ScriptVector* vector = static_cast<ScriptVector*>(v.GetPtr());
  214. assert(vector && vector->GetClassID() == ScriptVector::GetClassIDStatic());
  215. VariantVector adapter;
  216. vector->AdaptToVector(adapter);
  217. v = adapter;
  218. }
  219. }
  220. if (isAttr)
  221. {
  222. serial->SetAttribute(name, v);
  223. return 0;
  224. }
  225. // check dynamic
  226. if (jsc)
  227. {
  228. VariantMap& values = jsc->GetFieldValues();
  229. values[name] = v;
  230. }
  231. return 0;
  232. }
  233. static int Serializable_GetAttribute(duk_context* ctx)
  234. {
  235. const char* name = duk_to_string(ctx, 0);
  236. // for getting array values
  237. int arrayIndex = -1;
  238. if (duk_get_top(ctx) > 1)
  239. {
  240. int _arrayIndex = (int)duk_get_number(ctx, 1);
  241. if (_arrayIndex >= 0)
  242. arrayIndex = _arrayIndex;
  243. }
  244. duk_push_this(ctx);
  245. Serializable* serial = js_to_class_instance<Serializable>(ctx, -1, 0);
  246. const Vector<AttributeInfo>* attrs = serial->GetAttributes();
  247. if (attrs)
  248. {
  249. for (unsigned i = 0; i < attrs->Size(); i++)
  250. {
  251. const AttributeInfo* attr = &attrs->At(i);
  252. if (!attr->name_.Compare(name))
  253. {
  254. // FIXME: this is a double lookup
  255. js_push_variant(ctx, serial->GetAttribute(name));
  256. return 1;
  257. }
  258. }
  259. }
  260. if (serial->GetBaseType() == ScriptComponent::GetTypeStatic())
  261. {
  262. ScriptComponent* jsc = (ScriptComponent*)serial;
  263. ScriptComponentFile* file = jsc->GetComponentFile();
  264. if (file)
  265. {
  266. const String& componentClassName = jsc->GetComponentClassName();
  267. const FieldMap& fields = file->GetFields(componentClassName);
  268. FieldInfo* finfo = fields[name];
  269. if (finfo)
  270. {
  271. const VariantMap& values = jsc->GetFieldValues();
  272. if (Variant* vptr = values[name])
  273. {
  274. if (finfo->isArray_ && arrayIndex >= 0)
  275. {
  276. assert(vptr->GetType() == VAR_VARIANTVECTOR);
  277. VariantVector* vector = vptr->GetVariantVectorPtr();
  278. if (finfo->fixedArraySize_)
  279. {
  280. if (vector->Size() != finfo->fixedArraySize_)
  281. {
  282. if (vector->Size() < finfo->fixedArraySize_)
  283. {
  284. Variant value;
  285. js_get_default_variant(finfo->variantType_, value);
  286. unsigned oldSize = vector->Size();
  287. vector->Resize(finfo->fixedArraySize_);
  288. for (unsigned i = oldSize; i < finfo->fixedArraySize_; i++)
  289. {
  290. (*vector)[i] = value;
  291. }
  292. }
  293. else
  294. {
  295. // truncate
  296. vector->Resize(finfo->fixedArraySize_);
  297. }
  298. }
  299. }
  300. if (arrayIndex >= vector->Size())
  301. {
  302. duk_push_undefined(ctx);
  303. return 1;
  304. }
  305. const Variant& current = (*vector)[arrayIndex];
  306. if (current.GetType() != finfo->variantType_)
  307. {
  308. Variant value;
  309. js_get_default_variant(finfo->variantType_, value);
  310. js_push_variant(ctx, value);
  311. (*vector)[arrayIndex] = value;
  312. }
  313. else
  314. {
  315. js_push_variant(ctx, current, arrayIndex);
  316. }
  317. }
  318. else
  319. {
  320. js_push_variant(ctx, *vptr, arrayIndex);
  321. }
  322. return 1;
  323. }
  324. else
  325. {
  326. if (finfo->isArray_)
  327. {
  328. if (arrayIndex < 0)
  329. {
  330. SharedPtr<ScriptVector> vector(new ScriptVector());
  331. if (finfo->fixedArraySize_)
  332. {
  333. VariantVector init(finfo->fixedArraySize_);
  334. init.Resize(finfo->fixedArraySize_);
  335. Variant value;
  336. js_get_default_variant(finfo->variantType_, value);
  337. for (unsigned i = 0; i < finfo->fixedArraySize_; i++)
  338. {
  339. init[i] = value;
  340. }
  341. jsc->GetFieldValues()[finfo->name_] = init;
  342. vector->AdaptFromVector(init);
  343. }
  344. js_push_class_object_instance(ctx, vector);
  345. return 1;
  346. }
  347. else
  348. {
  349. // we don't have the variant in the fieldmap
  350. ATOMIC_LOGERROR("Serializable_GetAttribute - indexing uninitialized array");
  351. duk_push_undefined(ctx);
  352. return 1;
  353. }
  354. }
  355. Variant v;
  356. file->GetDefaultFieldValue(name, v, componentClassName);
  357. js_push_variant(ctx, v);
  358. return 1;
  359. }
  360. }
  361. }
  362. }
  363. duk_push_undefined(ctx);
  364. return 1;
  365. }
  366. static void GetDynamicAttributes(duk_context* ctx, unsigned& count, const VariantMap& defaultFieldValues,
  367. const FieldMap& fields,
  368. const EnumMap& enums,
  369. const FieldTooltipMap& tooltips)
  370. {
  371. if (fields.Size())
  372. {
  373. HashMap<String, FieldInfo>::ConstIterator itr = fields.Begin();
  374. while (itr != fields.End())
  375. {
  376. duk_push_object(ctx);
  377. duk_push_number(ctx, (double)itr->second_.variantType_);
  378. duk_put_prop_string(ctx, -2, "type");
  379. const FieldInfo& fieldInfo = itr->second_;
  380. if (fieldInfo.variantType_ == VAR_RESOURCEREF)
  381. {
  382. if (fieldInfo.resourceTypeName_.Length())
  383. {
  384. duk_push_string(ctx, fieldInfo.resourceTypeName_.CString());
  385. duk_put_prop_string(ctx, -2, "resourceTypeName");
  386. }
  387. else if (defaultFieldValues.Contains(fieldInfo.name_) && defaultFieldValues[fieldInfo.name_]->GetType() == VAR_RESOURCEREF)
  388. {
  389. if (const Variant* value = defaultFieldValues[fieldInfo.name_])
  390. {
  391. if (value->GetType() == VAR_RESOURCEREF)
  392. {
  393. const ResourceRef& ref = value->GetResourceRef();
  394. const String& typeName = JSVM::GetJSVM(ctx)->GetContext()->GetTypeName(ref.type_);
  395. if (typeName.Length())
  396. {
  397. duk_push_string(ctx, typeName.CString());
  398. duk_put_prop_string(ctx, -2, "resourceTypeName");
  399. }
  400. }
  401. }
  402. }
  403. }
  404. duk_push_string(ctx, itr->first_.CString());
  405. duk_put_prop_string(ctx, -2, "name");
  406. duk_push_number(ctx, (double)AM_DEFAULT);
  407. duk_put_prop_string(ctx, -2, "mode");
  408. duk_push_string(ctx, "");
  409. duk_put_prop_string(ctx, -2, "defaultValue");
  410. duk_push_boolean(ctx, 1);
  411. duk_put_prop_string(ctx, -2, "dynamic");
  412. duk_push_boolean(ctx, itr->second_.isArray_ ? 1 : 0);
  413. duk_put_prop_string(ctx, -2, "isArray");
  414. duk_push_number(ctx, itr->second_.fixedArraySize_);
  415. duk_put_prop_string(ctx, -2, "fixedArraySize");
  416. if (tooltips.Contains(itr->first_))
  417. {
  418. duk_push_string(ctx, tooltips[itr->first_]->CString());
  419. duk_put_prop_string(ctx, -2, "tooltip");
  420. }
  421. duk_push_array(ctx);
  422. if (enums.Contains(itr->first_))
  423. {
  424. unsigned enumCount = 0;
  425. const Vector<EnumInfo>* infos = enums[itr->first_];
  426. Vector<EnumInfo>::ConstIterator eitr = infos->Begin();
  427. while (eitr != infos->End())
  428. {
  429. duk_push_string(ctx, eitr->name_.CString());
  430. duk_put_prop_index(ctx, -2, enumCount++);
  431. eitr++;
  432. }
  433. }
  434. duk_put_prop_string(ctx, -2, "enumNames");
  435. // store attr object
  436. duk_put_prop_index(ctx, -2, count++);
  437. itr++;
  438. }
  439. }
  440. }
  441. static int Serializable_GetAttributes(duk_context* ctx)
  442. {
  443. duk_push_this(ctx);
  444. Serializable* serial = js_to_class_instance<Serializable>(ctx, -1, 0);
  445. unsigned type = serial->GetType().Value();
  446. duk_get_global_string(ctx, "__atomic_scene_serializable_attributes");
  447. duk_get_prop_index(ctx, -1, type);
  448. // return cached array of attrinfo, unless JSComponent which has dynamic fields
  449. if (serial->GetBaseType() != ScriptComponent::GetTypeStatic() && duk_is_object(ctx, -1))
  450. return 1;
  451. const Vector<AttributeInfo>* attrs = serial->GetAttributes();
  452. duk_push_array(ctx);
  453. duk_dup(ctx, -1);
  454. duk_put_prop_index(ctx, -4, type);
  455. unsigned count = 0;
  456. if (attrs)
  457. {
  458. count = attrs->Size();
  459. for (unsigned i = 0; i < attrs->Size(); i++)
  460. {
  461. const AttributeInfo* attr = &attrs->At(i);
  462. if (attr->mode_ & AM_NOEDIT)
  463. continue;
  464. duk_push_object(ctx);
  465. duk_push_number(ctx, (double)attr->type_);
  466. duk_put_prop_string(ctx, -2, "type");
  467. if (attr->type_ == VAR_RESOURCEREF)
  468. {
  469. if (attr->defaultValue_.GetType() == VAR_RESOURCEREF)
  470. {
  471. const ResourceRef& ref = attr->defaultValue_.GetResourceRef();
  472. const String& typeName = serial->GetContext()->GetTypeName(ref.type_);
  473. if (typeName.Length())
  474. {
  475. duk_push_string(ctx, typeName.CString());
  476. duk_put_prop_string(ctx, -2, "resourceTypeName");
  477. }
  478. }
  479. }
  480. if (attr->type_ == VAR_RESOURCEREFLIST)
  481. {
  482. if (attr->defaultValue_.GetType() == VAR_RESOURCEREFLIST)
  483. {
  484. const ResourceRefList& ref = attr->defaultValue_.GetResourceRefList();
  485. const String& typeName = serial->GetContext()->GetTypeName(ref.type_);
  486. if (typeName.Length())
  487. {
  488. duk_push_string(ctx, typeName.CString());
  489. duk_put_prop_string(ctx, -2, "resourceTypeName");
  490. }
  491. }
  492. }
  493. duk_push_string(ctx, attr->name_.CString());
  494. duk_put_prop_string(ctx, -2, "name");
  495. duk_push_number(ctx, (double)attr->mode_);
  496. duk_put_prop_string(ctx, -2, "mode");
  497. duk_push_string(ctx, attr->defaultValue_.ToString().CString());
  498. duk_put_prop_string(ctx, -2, "defaultValue");
  499. duk_push_boolean(ctx, 0);
  500. duk_put_prop_string(ctx, -2, "dynamic");
  501. duk_push_array(ctx);
  502. const char** enumPtr = attr->enumNames_;
  503. unsigned enumCount = 0;
  504. if (enumPtr)
  505. {
  506. while (*enumPtr)
  507. {
  508. duk_push_string(ctx, *enumPtr);
  509. duk_put_prop_index(ctx, -2, enumCount++);
  510. enumPtr++;
  511. }
  512. }
  513. duk_put_prop_string(ctx, -2, "enumNames");
  514. // store attr object
  515. duk_put_prop_index(ctx, -2, i);
  516. }
  517. }
  518. // dynamic script fields
  519. if (serial->GetBaseType() == ScriptComponent::GetTypeStatic())
  520. {
  521. ScriptComponent* jsc = (ScriptComponent*)serial;
  522. ScriptComponentFile* file = jsc->GetComponentFile();
  523. if (file)
  524. {
  525. const String& className = jsc->GetComponentClassName();
  526. const VariantMap& defaultFieldValues = file->GetDefaultFieldValues(className);
  527. const FieldMap& fields = file->GetFields(className);
  528. const FieldTooltipMap& fieldTooltips = file->GetFieldTooltips(className);
  529. const EnumMap& enums = file->GetEnums(className);
  530. GetDynamicAttributes(ctx, count, defaultFieldValues, fields, enums, fieldTooltips);
  531. }
  532. }
  533. return 1;
  534. }
  535. void jsapi_init_scene_serializable(JSVM* vm)
  536. {
  537. duk_context* ctx = vm->GetJSContext();
  538. // cached attr
  539. duk_push_object(ctx);
  540. duk_put_global_string(ctx, "__atomic_scene_serializable_attributes");
  541. js_class_get_prototype(ctx, "Atomic", "Serializable");
  542. duk_push_c_function(ctx, Serializable_GetAttributes, 0);
  543. duk_put_prop_string(ctx, -2, "getAttributes");
  544. duk_push_c_function(ctx, Serializable_GetAttribute, DUK_VARARGS);
  545. duk_put_prop_string(ctx, -2, "getAttribute");
  546. duk_push_c_function(ctx, Serializable_SetAttribute, DUK_VARARGS);
  547. duk_put_prop_string(ctx, -2, "setAttribute");
  548. duk_pop(ctx);
  549. }
  550. }