JSSceneSerializable.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. Variant v;
  56. js_to_variant(ctx, 1, v);
  57. duk_push_this(ctx);
  58. Serializable* serial = js_to_class_instance<Serializable>(ctx, -1, 0);
  59. const Vector<AttributeInfo>* attributes = serial->GetAttributes();
  60. VariantType variantType = VAR_NONE;
  61. bool isAttr = false;
  62. if (attributes)
  63. {
  64. for (unsigned i = 0; i < attributes->Size(); i++)
  65. {
  66. const AttributeInfo* attr = &attributes->At(i);
  67. if (!attr->name_.Compare(name))
  68. {
  69. isAttr = true;
  70. variantType = attr->type_;
  71. break;
  72. }
  73. }
  74. }
  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 const String& GetResourceRefClassName(Context* context, const ResourceRef& ref)
  197. {
  198. const HashMap<StringHash, SharedPtr<ObjectFactory>>& factories = context->GetObjectFactories();
  199. HashMap<StringHash, SharedPtr<ObjectFactory>>::ConstIterator itr = factories.Begin();
  200. while (itr != factories.End())
  201. {
  202. if (itr->first_ == ref.type_)
  203. {
  204. return itr->second_->GetTypeName();
  205. }
  206. itr++;
  207. }
  208. return String::EMPTY;
  209. }
  210. static void GetDynamicAttributes(duk_context* ctx, unsigned& count, const VariantMap& defaultFieldValues,
  211. const FieldMap& fields,
  212. const EnumMap& enums)
  213. {
  214. if (fields.Size())
  215. {
  216. HashMap<String, VariantType>::ConstIterator itr = fields.Begin();
  217. while (itr != fields.End())
  218. {
  219. duk_push_object(ctx);
  220. duk_push_number(ctx, (double) itr->second_);
  221. duk_put_prop_string(ctx, -2, "type");
  222. if (itr->second_ == VAR_RESOURCEREF && defaultFieldValues.Contains(itr->first_))
  223. {
  224. if (defaultFieldValues[itr->first_]->GetType() == VAR_RESOURCEREF)
  225. {
  226. const ResourceRef& ref = defaultFieldValues[itr->first_]->GetResourceRef();
  227. const String& typeName = GetResourceRefClassName(JSVM::GetJSVM(ctx)->GetContext(), ref);
  228. if (typeName.Length())
  229. {
  230. duk_push_string(ctx, typeName.CString());
  231. duk_put_prop_string(ctx, -2, "resourceTypeName");
  232. }
  233. }
  234. }
  235. duk_push_string(ctx, itr->first_.CString());
  236. duk_put_prop_string(ctx, -2, "name");
  237. duk_push_number(ctx, (double) AM_DEFAULT);
  238. duk_put_prop_string(ctx, -2, "mode");
  239. duk_push_string(ctx,"");
  240. duk_put_prop_string(ctx, -2, "defaultValue");
  241. duk_push_boolean(ctx, 1);
  242. duk_put_prop_string(ctx, -2, "field");
  243. duk_push_array(ctx);
  244. if (enums.Contains(itr->first_))
  245. {
  246. unsigned enumCount = 0;
  247. const Vector<EnumInfo>* infos = enums[itr->first_];
  248. Vector<EnumInfo>::ConstIterator eitr = infos->Begin();
  249. while (eitr != infos->End())
  250. {
  251. duk_push_string(ctx, eitr->name_.CString());
  252. duk_put_prop_index(ctx, -2, enumCount++);
  253. eitr++;
  254. }
  255. }
  256. duk_put_prop_string(ctx, -2, "enumNames");
  257. // store attr object
  258. duk_put_prop_index(ctx, -2, count++);
  259. itr++;
  260. }
  261. }
  262. }
  263. static int Serializable_GetAttributes(duk_context* ctx)
  264. {
  265. duk_push_this(ctx);
  266. Serializable* serial = js_to_class_instance<Serializable>(ctx, -1, 0);
  267. unsigned type = serial->GetType().Value();
  268. duk_get_global_string(ctx, "__atomic_scene_serializable_attributes");
  269. duk_get_prop_index(ctx, -1, type);
  270. // return cached array of attrinfo, unless JSComponent which has dynamic fields
  271. if (serial->GetBaseType() != ScriptComponent::GetTypeStatic() && duk_is_object(ctx, -1))
  272. return 1;
  273. const Vector<AttributeInfo>* attrs = serial->GetAttributes();
  274. duk_push_array(ctx);
  275. duk_dup(ctx, -1);
  276. duk_put_prop_index(ctx, -4, type);
  277. unsigned count = 0;
  278. if (attrs)
  279. {
  280. count = attrs->Size();
  281. for (unsigned i = 0; i < attrs->Size(); i++)
  282. {
  283. const AttributeInfo* attr = &attrs->At(i);
  284. if (attr->mode_ & AM_NOEDIT)
  285. continue;
  286. duk_push_object(ctx);
  287. duk_push_number(ctx, (double) attr->type_);
  288. duk_put_prop_string(ctx, -2, "type");
  289. if (attr->type_ == VAR_RESOURCEREF)
  290. {
  291. if (attr->defaultValue_.GetType() == VAR_RESOURCEREF)
  292. {
  293. const ResourceRef& ref = attr->defaultValue_.GetResourceRef();
  294. const String& typeName = GetResourceRefClassName(serial->GetContext(), ref);
  295. if (typeName.Length())
  296. {
  297. duk_push_string(ctx, typeName.CString());
  298. duk_put_prop_string(ctx, -2, "resourceTypeName");
  299. }
  300. }
  301. }
  302. duk_push_string(ctx, attr->name_.CString());
  303. duk_put_prop_string(ctx, -2, "name");
  304. duk_push_number(ctx, (double) attr->mode_);
  305. duk_put_prop_string(ctx, -2, "mode");
  306. duk_push_string(ctx,attr->defaultValue_.ToString().CString());
  307. duk_put_prop_string(ctx, -2, "defaultValue");
  308. duk_push_boolean(ctx, 0);
  309. duk_put_prop_string(ctx, -2, "field");
  310. duk_push_array(ctx);
  311. const char** enumPtr = attr->enumNames_;
  312. unsigned enumCount = 0;
  313. if (enumPtr)
  314. {
  315. while (*enumPtr)
  316. {
  317. duk_push_string(ctx, *enumPtr);
  318. duk_put_prop_index(ctx, -2, enumCount++);
  319. enumPtr++;
  320. }
  321. }
  322. duk_put_prop_string(ctx, -2, "enumNames");
  323. // store attr object
  324. duk_put_prop_index(ctx, -2, i);
  325. }
  326. }
  327. // dynamic script fields
  328. if (serial->GetBaseType() == ScriptComponent::GetTypeStatic())
  329. {
  330. ScriptComponent* jsc = (ScriptComponent*) serial;
  331. ScriptComponentFile* file = jsc->GetComponentFile();
  332. if (file)
  333. {
  334. const String& className = jsc->GetComponentClassName();
  335. const VariantMap& defaultFieldValues = file->GetDefaultFieldValues(className);
  336. const FieldMap& fields = file->GetFields(className);
  337. const EnumMap& enums = file->GetEnums(className);
  338. GetDynamicAttributes(ctx, count, defaultFieldValues, fields, enums);
  339. }
  340. }
  341. return 1;
  342. }
  343. void jsapi_init_scene_serializable(JSVM* vm)
  344. {
  345. duk_context* ctx = vm->GetJSContext();
  346. // cached attr
  347. duk_push_object(ctx);
  348. duk_put_global_string(ctx, "__atomic_scene_serializable_attributes");
  349. js_class_get_prototype(ctx, "Atomic", "Serializable");
  350. duk_push_c_function(ctx, Serializable_GetAttributes, 0);
  351. duk_put_prop_string(ctx, -2, "getAttributes");
  352. duk_push_c_function(ctx, Serializable_GetAttribute, 1);
  353. duk_put_prop_string(ctx, -2, "getAttribute");
  354. duk_push_c_function(ctx, Serializable_SetAttribute, 2);
  355. duk_put_prop_string(ctx, -2, "setAttribute");
  356. duk_pop(ctx);
  357. }
  358. }