JSComponentFile.cpp 8.0 KB

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