JSSceneSerializable.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. if (fields.Contains(name))
  66. {
  67. HashMap<String, VariantType>::ConstIterator itr = fields.Find(name);
  68. variantType = itr->second_;
  69. }
  70. }
  71. }
  72. }
  73. if (variantType == VAR_NONE)
  74. return 0;
  75. if (variantType == VAR_QUATERNION)
  76. {
  77. Vector3 v3 = v.GetVector3();
  78. Quaternion q;
  79. q.FromEulerAngles(v3.x_, v3.y_, v3.z_);
  80. v = q;
  81. }
  82. else if (variantType == VAR_COLOR)
  83. {
  84. Vector4 v4 = v.GetVector4();
  85. Color c(v4.x_, v4.y_, v4.z_, v4.w_ );
  86. v = c;
  87. }
  88. else if (variantType == VAR_INT)
  89. {
  90. v = (int) v.GetFloat();
  91. }
  92. else if (variantType == VAR_RESOURCEREF)
  93. {
  94. RefCounted* ref = v.GetPtr();
  95. if (ref && ref->IsObject())
  96. {
  97. Object* o = (Object*) ref;
  98. // TODO: calling code must ensure we are a resource, can this be done here?
  99. Resource* resource = (Resource*) o;
  100. v = ResourceRef(resource->GetType(), resource->GetName());
  101. }
  102. }
  103. if (isAttr)
  104. {
  105. serial->SetAttribute(name, v);
  106. return 0;
  107. }
  108. // check dynamic
  109. if (jsc)
  110. {
  111. VariantMap& values = jsc->GetFieldValues();
  112. values[name] = v;
  113. }
  114. return 0;
  115. }
  116. static int Serializable_GetAttribute(duk_context* ctx)
  117. {
  118. const char* name = duk_to_string(ctx, 0);
  119. duk_push_this(ctx);
  120. Serializable* serial = js_to_class_instance<Serializable>(ctx, -1, 0);
  121. const Vector<AttributeInfo>* attrs = serial->GetAttributes();
  122. if (attrs)
  123. {
  124. for (unsigned i = 0; i < attrs->Size(); i++)
  125. {
  126. const AttributeInfo* attr = &attrs->At(i);
  127. if (!attr->name_.Compare(name))
  128. {
  129. // FIXME: this is a double lookup
  130. js_push_variant(ctx, serial->GetAttribute(name));
  131. return 1;
  132. }
  133. }
  134. }
  135. if (serial->GetType() == JSComponent::GetTypeStatic())
  136. {
  137. JSComponent* jsc = (JSComponent*) serial;
  138. JSComponentFile* file = jsc->GetComponentFile();
  139. if (file)
  140. {
  141. const HashMap<String, VariantType>& fields = file->GetFields();
  142. if (fields.Contains(name))
  143. {
  144. VariantMap& values = jsc->GetFieldValues();
  145. if (values.Contains(name))
  146. {
  147. js_push_variant(ctx, values[name]);
  148. return 1;
  149. }
  150. else
  151. {
  152. Variant v;
  153. file->GetDefaultFieldValue(name, v);
  154. js_push_variant(ctx, v);
  155. return 1;
  156. }
  157. }
  158. }
  159. }
  160. duk_push_undefined(ctx);
  161. return 1;
  162. }
  163. static const String& GetResourceRefClassName(Context* context, const ResourceRef& ref)
  164. {
  165. const HashMap<StringHash, SharedPtr<ObjectFactory>>& factories = context->GetObjectFactories();
  166. HashMap<StringHash, SharedPtr<ObjectFactory>>::ConstIterator itr = factories.Begin();
  167. while (itr != factories.End())
  168. {
  169. if (itr->first_ == ref.type_)
  170. {
  171. return itr->second_->GetTypeName();
  172. }
  173. itr++;
  174. }
  175. return String::EMPTY;
  176. }
  177. static int Serializable_GetAttributes(duk_context* ctx)
  178. {
  179. duk_push_this(ctx);
  180. Serializable* serial = js_to_class_instance<Serializable>(ctx, -1, 0);
  181. unsigned type = serial->GetType().Value();
  182. duk_get_global_string(ctx, "__atomic_scene_serializable_attributes");
  183. duk_get_prop_index(ctx, -1, type);
  184. // return cached array of attrinfo, unless JSComponent which has dynamic fields
  185. if (serial->GetType() != JSComponent::GetTypeStatic() && duk_is_object(ctx, -1))
  186. return 1;
  187. const Vector<AttributeInfo>* attrs = serial->GetAttributes();
  188. duk_push_array(ctx);
  189. duk_dup(ctx, -1);
  190. duk_put_prop_index(ctx, -4, type);
  191. unsigned count = 0;
  192. if (attrs)
  193. {
  194. count = attrs->Size();
  195. for (unsigned i = 0; i < attrs->Size(); i++)
  196. {
  197. const AttributeInfo* attr = &attrs->At(i);
  198. if (attr->mode_ & AM_NOEDIT)
  199. continue;
  200. duk_push_object(ctx);
  201. duk_push_number(ctx, (double) attr->type_);
  202. duk_put_prop_string(ctx, -2, "type");
  203. if (attr->type_ == VAR_RESOURCEREF)
  204. {
  205. if (attr->defaultValue_.GetType() == VAR_RESOURCEREF)
  206. {
  207. const ResourceRef& ref = attr->defaultValue_.GetResourceRef();
  208. const String& typeName = GetResourceRefClassName(serial->GetContext(), ref);
  209. if (typeName.Length())
  210. {
  211. duk_push_string(ctx, typeName.CString());
  212. duk_put_prop_string(ctx, -2, "resourceTypeName");
  213. }
  214. }
  215. }
  216. duk_push_string(ctx, attr->name_.CString());
  217. duk_put_prop_string(ctx, -2, "name");
  218. duk_push_number(ctx, (double) attr->mode_);
  219. duk_put_prop_string(ctx, -2, "mode");
  220. duk_push_string(ctx,attr->defaultValue_.ToString().CString());
  221. duk_put_prop_string(ctx, -2, "defaultValue");
  222. duk_push_boolean(ctx, 0);
  223. duk_put_prop_string(ctx, -2, "field");
  224. duk_push_array(ctx);
  225. const char** enumPtr = attr->enumNames_;
  226. unsigned enumCount = 0;
  227. if (enumPtr)
  228. {
  229. while (*enumPtr)
  230. {
  231. duk_push_string(ctx, *enumPtr);
  232. duk_put_prop_index(ctx, -2, enumCount++);
  233. enumPtr++;
  234. }
  235. }
  236. duk_put_prop_string(ctx, -2, "enumNames");
  237. // store attr object
  238. duk_put_prop_index(ctx, -2, i);
  239. }
  240. }
  241. // dynamic script fields
  242. if (serial->GetType() == JSComponent::GetTypeStatic())
  243. {
  244. JSComponent* jsc = (JSComponent*) serial;
  245. JSComponentFile* file = jsc->GetComponentFile();
  246. if (file)
  247. {
  248. const VariantMap& defaultFieldValues = file->GetDefaultFieldValues();
  249. const HashMap<String, VariantType>& fields = file->GetFields();
  250. if (fields.Size())
  251. {
  252. HashMap<String, VariantType>::ConstIterator itr = fields.Begin();
  253. while (itr != fields.End())
  254. {
  255. duk_push_object(ctx);
  256. duk_push_number(ctx, (double) itr->second_);
  257. duk_put_prop_string(ctx, -2, "type");
  258. if (itr->second_ == VAR_RESOURCEREF && defaultFieldValues.Contains(itr->first_))
  259. {
  260. if (defaultFieldValues[itr->first_]->GetType() == VAR_RESOURCEREF)
  261. {
  262. const ResourceRef& ref = defaultFieldValues[itr->first_]->GetResourceRef();
  263. const String& typeName = GetResourceRefClassName(serial->GetContext(), ref);
  264. if (typeName.Length())
  265. {
  266. duk_push_string(ctx, typeName.CString());
  267. duk_put_prop_string(ctx, -2, "resourceTypeName");
  268. }
  269. }
  270. }
  271. duk_push_string(ctx, itr->first_.CString());
  272. duk_put_prop_string(ctx, -2, "name");
  273. duk_push_number(ctx, (double) AM_DEFAULT);
  274. duk_put_prop_string(ctx, -2, "mode");
  275. duk_push_string(ctx,"");
  276. duk_put_prop_string(ctx, -2, "defaultValue");
  277. duk_push_boolean(ctx, 1);
  278. duk_put_prop_string(ctx, -2, "field");
  279. duk_push_array(ctx);
  280. duk_put_prop_string(ctx, -2, "enumNames");
  281. // store attr object
  282. duk_put_prop_index(ctx, -2, count++);
  283. itr++;
  284. }
  285. }
  286. }
  287. }
  288. return 1;
  289. }
  290. void jsapi_init_scene_serializable(JSVM* vm)
  291. {
  292. duk_context* ctx = vm->GetJSContext();
  293. // cached attr
  294. duk_push_object(ctx);
  295. duk_put_global_string(ctx, "__atomic_scene_serializable_attributes");
  296. js_class_get_prototype(ctx, "Atomic", "Serializable");
  297. duk_push_c_function(ctx, Serializable_GetAttributes, 0);
  298. duk_put_prop_string(ctx, -2, "getAttributes");
  299. duk_push_c_function(ctx, Serializable_GetAttribute, 1);
  300. duk_put_prop_string(ctx, -2, "getAttribute");
  301. duk_push_c_function(ctx, Serializable_SetAttribute, 2);
  302. duk_put_prop_string(ctx, -2, "setAttribute");
  303. duk_pop(ctx);
  304. }
  305. }