JSSceneSerializable.cpp 12 KB

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