JSSceneSerializable.cpp 24 KB

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