JSSceneSerializable.cpp 13 KB

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