JSSceneSerializable.cpp 13 KB

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