JSComponentFile.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  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/Core/Context.h>
  23. #include <Atomic/IO/Deserializer.h>
  24. #include <Atomic/IO/Log.h>
  25. #include <Atomic/Core/Profiler.h>
  26. #include <Atomic/Resource/ResourceCache.h>
  27. #include <Atomic/IO/Serializer.h>
  28. #include "JSComponentFile.h"
  29. #include "JSComponent.h"
  30. #include "JSVM.h"
  31. namespace Atomic
  32. {
  33. JSComponentFile::JSComponentFile(Context* context) :
  34. Resource(context),
  35. scriptClass_(false)
  36. {
  37. }
  38. JSComponentFile::~JSComponentFile()
  39. {
  40. }
  41. void JSComponentFile::RegisterObject(Context* context)
  42. {
  43. context->RegisterFactory<JSComponentFile>();
  44. }
  45. void JSComponentFile::GetDefaultFieldValue(const String& name, Variant& v)
  46. {
  47. v = Variant::EMPTY;
  48. VariantMap::ConstIterator itr = defaultFieldValues_.Find(name);
  49. if (itr == defaultFieldValues_.End())
  50. {
  51. HashMap<String, VariantType>::ConstIterator citr = fields_.Find(name);
  52. if (citr == fields_.End())
  53. return;
  54. switch (citr->second_)
  55. {
  56. case VAR_BOOL:
  57. v = false;
  58. break;
  59. case VAR_STRING:
  60. v = "";
  61. break;
  62. case VAR_FLOAT:
  63. v = 0.0f;
  64. break;
  65. case VAR_VECTOR3:
  66. v = Vector3::ZERO;
  67. break;
  68. default:
  69. break;
  70. }
  71. return;
  72. }
  73. v = itr->second_;
  74. }
  75. bool JSComponentFile::PushModule()
  76. {
  77. if (context_->GetEditorContext())
  78. return false;
  79. JSVM* vm = JSVM::GetJSVM(NULL);
  80. duk_context* ctx = vm->GetJSContext();
  81. const String& path = GetName();
  82. String pathName, fileName, ext;
  83. SplitPath(path, pathName, fileName, ext);
  84. pathName += "/" + fileName;
  85. duk_get_global_string(ctx, "require");
  86. duk_push_string(ctx, pathName.CString());
  87. if (duk_pcall(ctx, 1) != 0)
  88. {
  89. vm->SendJSErrorEvent();
  90. return false;
  91. }
  92. return true;
  93. }
  94. JSComponent* JSComponentFile::CreateJSComponent()
  95. {
  96. JSComponent* component = NULL;
  97. if (!scriptClass_)
  98. {
  99. component = new JSComponent(context_);
  100. }
  101. else
  102. {
  103. JSVM* vm = JSVM::GetJSVM(NULL);
  104. duk_context* ctx = vm->GetJSContext();
  105. PushModule();
  106. duk_new(ctx, 0);
  107. if (duk_is_object(ctx, -1))
  108. {
  109. component = js_to_class_instance<JSComponent>(ctx, -1, 0);
  110. component->scriptClassInstance_ = true;
  111. // store reference below so pop doesn't gc the component
  112. component->UpdateReferences();
  113. }
  114. duk_pop(ctx);
  115. }
  116. if (!component)
  117. {
  118. LOGERRORF("Failed to create script class from component file %s", GetName().CString());
  119. component = new JSComponent(context_);
  120. }
  121. component->SetComponentFile(this);
  122. return component;
  123. }
  124. bool JSComponentFile::InitModule()
  125. {
  126. if (context_->GetEditorContext())
  127. return true;
  128. JSVM* vm = JSVM::GetJSVM(NULL);
  129. duk_context* ctx = vm->GetJSContext();
  130. duk_idx_t top = duk_get_top(ctx);
  131. if (!PushModule())
  132. return false;
  133. if (duk_is_function(ctx, -1))
  134. {
  135. // TODO: verify JSComponent in prototype chain
  136. scriptClass_ = true;
  137. duk_set_top(ctx, top);
  138. return true;
  139. }
  140. if (!duk_is_object(ctx, -1))
  141. {
  142. duk_set_top(ctx, top);
  143. return false;
  144. }
  145. duk_set_top(ctx, top);
  146. return true;
  147. }
  148. bool JSComponentFile::BeginLoad(Deserializer& source)
  149. {
  150. if (!InitModule())
  151. return false;
  152. // TODO: cache these for player builds
  153. // FIXME: this won't work with obfusication or minimization
  154. unsigned dataSize = source.GetSize();
  155. if (!dataSize && !source.GetName().Empty())
  156. {
  157. LOGERROR("Zero sized component file in " + source.GetName());
  158. return false;
  159. }
  160. SharedArrayPtr<char> buffer(new char[dataSize + 1]);
  161. if (source.Read(buffer.Get(), dataSize) != dataSize)
  162. return false;
  163. buffer[dataSize] = '\0';
  164. String text = buffer.Get();
  165. text.Replace("\r", "");
  166. Vector<String> lines = text.Split('\n');
  167. String eval;
  168. bool valid = false;
  169. for (unsigned i = 0; i < lines.Size(); i++)
  170. {
  171. String line = lines[i];
  172. if (!eval.Length())
  173. {
  174. line = line.Trimmed();
  175. if (line.StartsWith("inspectorFields"))
  176. {
  177. eval = line;
  178. if (line.Contains("}"))
  179. {
  180. valid = true;
  181. break;
  182. }
  183. }
  184. else if (line.StartsWith("this.inspectorFields"))
  185. {
  186. eval = line.Substring(5);
  187. if (line.Contains("}"))
  188. {
  189. valid = true;
  190. break;
  191. }
  192. }
  193. else if (line.StartsWith("var inspectorFields"))
  194. {
  195. eval = line.Substring(4);
  196. if (line.Contains("}"))
  197. {
  198. valid = true;
  199. break;
  200. }
  201. }
  202. }
  203. else
  204. {
  205. eval += line;
  206. }
  207. if (line.Contains("}") && eval.Length())
  208. {
  209. valid = true;
  210. break;
  211. }
  212. }
  213. if (valid)
  214. {
  215. JSVM* vm = JSVM::GetJSVM(NULL);
  216. if (!vm)
  217. return true;
  218. duk_context* ctx = vm->GetJSContext();
  219. int top = duk_get_top(ctx);
  220. duk_push_string(ctx, eval.CString());
  221. duk_push_string(ctx, "eval");
  222. duk_compile(ctx, DUK_COMPILE_EVAL);
  223. if (duk_is_function(ctx, -1) && duk_pcall(ctx, 0) == DUK_EXEC_SUCCESS)
  224. {
  225. if (duk_is_object(ctx, -1))
  226. {
  227. duk_enum(ctx, -1, DUK_ENUM_OWN_PROPERTIES_ONLY);
  228. while (duk_next(ctx, -1, 1)) {
  229. String name = duk_get_string(ctx, -2);
  230. VariantType variantType = VAR_NONE;
  231. Variant defaultValue;
  232. if (duk_is_string(ctx, -1))
  233. {
  234. variantType = VAR_STRING;
  235. defaultValue = duk_to_string(ctx, -1);
  236. }
  237. else if (duk_is_number(ctx, -1))
  238. {
  239. variantType = VAR_FLOAT;
  240. defaultValue = (float) duk_to_number(ctx, -1);
  241. }
  242. else if (duk_is_array(ctx, -1))
  243. {
  244. if (duk_get_length(ctx, -1) > 0)
  245. {
  246. duk_get_prop_index(ctx, -1, 0);
  247. // TODO: class types
  248. variantType = (VariantType) duk_require_number(ctx, -1);
  249. duk_pop(ctx);
  250. }
  251. if (duk_get_length(ctx, -1) > 1)
  252. {
  253. duk_get_prop_index(ctx, -1, 1);
  254. // default value
  255. js_to_variant(ctx, -1, defaultValue);
  256. duk_pop(ctx);
  257. }
  258. }
  259. else if (duk_is_boolean(ctx, -1))
  260. {
  261. variantType = VAR_BOOL;
  262. defaultValue = duk_to_boolean(ctx, -1) ? true : false;
  263. }
  264. if (defaultValue.GetType() != VAR_NONE)
  265. {
  266. defaultFieldValues_[name] = defaultValue;
  267. }
  268. if (variantType != VAR_NONE)
  269. fields_[name] = variantType;
  270. duk_pop_2(ctx); // pop key value
  271. }
  272. duk_pop(ctx); // pop enum object
  273. }
  274. }
  275. duk_set_top(ctx, top);
  276. }
  277. SetMemoryUse(0);
  278. return true;
  279. }
  280. bool JSComponentFile::Save(Serializer& dest) const
  281. {
  282. return true;
  283. }
  284. }