JSSceneSerializable.cpp 20 KB

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