JSSceneSerializable.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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 (arrayIndex >= vector->Size())
  279. {
  280. duk_push_undefined(ctx);
  281. return 1;
  282. }
  283. const Variant& current = (*vector)[arrayIndex];
  284. if (current.GetType() != finfo->variantType_)
  285. {
  286. Variant value;
  287. js_push_default_variant(ctx, finfo->variantType_, value);
  288. (*vector)[arrayIndex] = value;
  289. }
  290. else
  291. {
  292. js_push_variant(ctx, current, arrayIndex);
  293. }
  294. }
  295. else
  296. {
  297. js_push_variant(ctx, *vptr, arrayIndex);
  298. }
  299. return 1;
  300. }
  301. else
  302. {
  303. if (finfo->isArray_)
  304. {
  305. if (arrayIndex < 0)
  306. {
  307. SharedPtr<ScriptVector> vector(new ScriptVector());
  308. js_push_class_object_instance(ctx, vector);
  309. return 1;
  310. }
  311. else
  312. {
  313. Variant value;
  314. js_push_default_variant(ctx, finfo->variantType_, value);
  315. VariantVector newVector;
  316. if (arrayIndex >= newVector.Size())
  317. {
  318. newVector.Resize(arrayIndex + 1);
  319. }
  320. jsc->GetFieldValues()[name] = newVector;
  321. return 1;
  322. }
  323. }
  324. Variant v;
  325. file->GetDefaultFieldValue(name, v, componentClassName);
  326. js_push_variant(ctx, v);
  327. return 1;
  328. }
  329. }
  330. }
  331. }
  332. duk_push_undefined(ctx);
  333. return 1;
  334. }
  335. static void GetDynamicAttributes(duk_context* ctx, unsigned& count, const VariantMap& defaultFieldValues,
  336. const FieldMap& fields,
  337. const EnumMap& enums,
  338. const FieldTooltipMap& tooltips)
  339. {
  340. if (fields.Size())
  341. {
  342. HashMap<String, FieldInfo>::ConstIterator itr = fields.Begin();
  343. while (itr != fields.End())
  344. {
  345. duk_push_object(ctx);
  346. duk_push_number(ctx, (double)itr->second_.variantType_);
  347. duk_put_prop_string(ctx, -2, "type");
  348. const FieldInfo& fieldInfo = itr->second_;
  349. if (fieldInfo.variantType_ == VAR_RESOURCEREF)
  350. {
  351. if (fieldInfo.resourceTypeName_.Length())
  352. {
  353. duk_push_string(ctx, fieldInfo.resourceTypeName_.CString());
  354. duk_put_prop_string(ctx, -2, "resourceTypeName");
  355. }
  356. else if (defaultFieldValues.Contains(fieldInfo.name_) && defaultFieldValues[fieldInfo.name_]->GetType() == VAR_RESOURCEREF)
  357. {
  358. if (const Variant* value = defaultFieldValues[fieldInfo.name_])
  359. {
  360. if (value->GetType() == VAR_RESOURCEREF)
  361. {
  362. const ResourceRef& ref = value->GetResourceRef();
  363. const String& typeName = JSVM::GetJSVM(ctx)->GetContext()->GetTypeName(ref.type_);
  364. if (typeName.Length())
  365. {
  366. duk_push_string(ctx, typeName.CString());
  367. duk_put_prop_string(ctx, -2, "resourceTypeName");
  368. }
  369. }
  370. }
  371. }
  372. }
  373. duk_push_string(ctx, itr->first_.CString());
  374. duk_put_prop_string(ctx, -2, "name");
  375. duk_push_number(ctx, (double)AM_DEFAULT);
  376. duk_put_prop_string(ctx, -2, "mode");
  377. duk_push_string(ctx, "");
  378. duk_put_prop_string(ctx, -2, "defaultValue");
  379. duk_push_boolean(ctx, 1);
  380. duk_put_prop_string(ctx, -2, "dynamic");
  381. duk_push_boolean(ctx, itr->second_.isArray_ ? 1 : 0);
  382. duk_put_prop_string(ctx, -2, "isArray");
  383. if (tooltips.Contains(itr->first_))
  384. {
  385. duk_push_string(ctx, tooltips[itr->first_]->CString());
  386. duk_put_prop_string(ctx, -2, "tooltip");
  387. }
  388. duk_push_array(ctx);
  389. if (enums.Contains(itr->first_))
  390. {
  391. unsigned enumCount = 0;
  392. const Vector<EnumInfo>* infos = enums[itr->first_];
  393. Vector<EnumInfo>::ConstIterator eitr = infos->Begin();
  394. while (eitr != infos->End())
  395. {
  396. duk_push_string(ctx, eitr->name_.CString());
  397. duk_put_prop_index(ctx, -2, enumCount++);
  398. eitr++;
  399. }
  400. }
  401. duk_put_prop_string(ctx, -2, "enumNames");
  402. // store attr object
  403. duk_put_prop_index(ctx, -2, count++);
  404. itr++;
  405. }
  406. }
  407. }
  408. static int Serializable_GetAttributes(duk_context* ctx)
  409. {
  410. duk_push_this(ctx);
  411. Serializable* serial = js_to_class_instance<Serializable>(ctx, -1, 0);
  412. unsigned type = serial->GetType().Value();
  413. duk_get_global_string(ctx, "__atomic_scene_serializable_attributes");
  414. duk_get_prop_index(ctx, -1, type);
  415. // return cached array of attrinfo, unless JSComponent which has dynamic fields
  416. if (serial->GetBaseType() != ScriptComponent::GetTypeStatic() && duk_is_object(ctx, -1))
  417. return 1;
  418. const Vector<AttributeInfo>* attrs = serial->GetAttributes();
  419. duk_push_array(ctx);
  420. duk_dup(ctx, -1);
  421. duk_put_prop_index(ctx, -4, type);
  422. unsigned count = 0;
  423. if (attrs)
  424. {
  425. count = attrs->Size();
  426. for (unsigned i = 0; i < attrs->Size(); i++)
  427. {
  428. const AttributeInfo* attr = &attrs->At(i);
  429. if (attr->mode_ & AM_NOEDIT)
  430. continue;
  431. duk_push_object(ctx);
  432. duk_push_number(ctx, (double)attr->type_);
  433. duk_put_prop_string(ctx, -2, "type");
  434. if (attr->type_ == VAR_RESOURCEREF)
  435. {
  436. if (attr->defaultValue_.GetType() == VAR_RESOURCEREF)
  437. {
  438. const ResourceRef& ref = attr->defaultValue_.GetResourceRef();
  439. const String& typeName = serial->GetContext()->GetTypeName(ref.type_);
  440. if (typeName.Length())
  441. {
  442. duk_push_string(ctx, typeName.CString());
  443. duk_put_prop_string(ctx, -2, "resourceTypeName");
  444. }
  445. }
  446. }
  447. if (attr->type_ == VAR_RESOURCEREFLIST)
  448. {
  449. if (attr->defaultValue_.GetType() == VAR_RESOURCEREFLIST)
  450. {
  451. const ResourceRefList& ref = attr->defaultValue_.GetResourceRefList();
  452. const String& typeName = serial->GetContext()->GetTypeName(ref.type_);
  453. if (typeName.Length())
  454. {
  455. duk_push_string(ctx, typeName.CString());
  456. duk_put_prop_string(ctx, -2, "resourceTypeName");
  457. }
  458. }
  459. }
  460. duk_push_string(ctx, attr->name_.CString());
  461. duk_put_prop_string(ctx, -2, "name");
  462. duk_push_number(ctx, (double)attr->mode_);
  463. duk_put_prop_string(ctx, -2, "mode");
  464. duk_push_string(ctx, attr->defaultValue_.ToString().CString());
  465. duk_put_prop_string(ctx, -2, "defaultValue");
  466. duk_push_boolean(ctx, 0);
  467. duk_put_prop_string(ctx, -2, "dynamic");
  468. duk_push_array(ctx);
  469. const char** enumPtr = attr->enumNames_;
  470. unsigned enumCount = 0;
  471. if (enumPtr)
  472. {
  473. while (*enumPtr)
  474. {
  475. duk_push_string(ctx, *enumPtr);
  476. duk_put_prop_index(ctx, -2, enumCount++);
  477. enumPtr++;
  478. }
  479. }
  480. duk_put_prop_string(ctx, -2, "enumNames");
  481. // store attr object
  482. duk_put_prop_index(ctx, -2, i);
  483. }
  484. }
  485. // dynamic script fields
  486. if (serial->GetBaseType() == ScriptComponent::GetTypeStatic())
  487. {
  488. ScriptComponent* jsc = (ScriptComponent*)serial;
  489. ScriptComponentFile* file = jsc->GetComponentFile();
  490. if (file)
  491. {
  492. const String& className = jsc->GetComponentClassName();
  493. const VariantMap& defaultFieldValues = file->GetDefaultFieldValues(className);
  494. const FieldMap& fields = file->GetFields(className);
  495. const FieldTooltipMap& fieldTooltips = file->GetFieldTooltips(className);
  496. const EnumMap& enums = file->GetEnums(className);
  497. GetDynamicAttributes(ctx, count, defaultFieldValues, fields, enums, fieldTooltips);
  498. }
  499. }
  500. return 1;
  501. }
  502. void jsapi_init_scene_serializable(JSVM* vm)
  503. {
  504. duk_context* ctx = vm->GetJSContext();
  505. // cached attr
  506. duk_push_object(ctx);
  507. duk_put_global_string(ctx, "__atomic_scene_serializable_attributes");
  508. js_class_get_prototype(ctx, "Atomic", "Serializable");
  509. duk_push_c_function(ctx, Serializable_GetAttributes, 0);
  510. duk_put_prop_string(ctx, -2, "getAttributes");
  511. duk_push_c_function(ctx, Serializable_GetAttribute, DUK_VARARGS);
  512. duk_put_prop_string(ctx, -2, "getAttribute");
  513. duk_push_c_function(ctx, Serializable_SetAttribute, DUK_VARARGS);
  514. duk_put_prop_string(ctx, -2, "setAttribute");
  515. duk_pop(ctx);
  516. }
  517. }