JSComponentFile.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. {
  35. }
  36. JSComponentFile::~JSComponentFile()
  37. {
  38. }
  39. void JSComponentFile::RegisterObject(Context* context)
  40. {
  41. context->RegisterFactory<JSComponentFile>();
  42. }
  43. void JSComponentFile::GetDefaultFieldValue(const String& name, Variant& v)
  44. {
  45. v = Variant::EMPTY;
  46. VariantMap::ConstIterator itr = defaultFieldValues_.Find(name);
  47. if (itr == defaultFieldValues_.End())
  48. {
  49. HashMap<String, VariantType>::ConstIterator citr = fields_.Find(name);
  50. if (citr == fields_.End())
  51. return;
  52. switch (citr->second_)
  53. {
  54. case VAR_BOOL:
  55. v = false;
  56. break;
  57. case VAR_STRING:
  58. v = "";
  59. break;
  60. case VAR_FLOAT:
  61. v = 0.0f;
  62. break;
  63. case VAR_VECTOR3:
  64. v = Vector3::ZERO;
  65. break;
  66. default:
  67. break;
  68. }
  69. return;
  70. }
  71. v = itr->second_;
  72. }
  73. bool JSComponentFile::BeginLoad(Deserializer& source)
  74. {
  75. // TODO: cache these for player builds
  76. unsigned dataSize = source.GetSize();
  77. if (!dataSize && !source.GetName().Empty())
  78. {
  79. LOGERROR("Zero sized component file in " + source.GetName());
  80. return false;
  81. }
  82. SharedArrayPtr<char> buffer(new char[dataSize + 1]);
  83. if (source.Read(buffer.Get(), dataSize) != dataSize)
  84. return false;
  85. buffer[dataSize] = '\0';
  86. String text = buffer.Get();
  87. text.Replace("\r", "");
  88. Vector<String> lines = text.Split('\n');
  89. String eval;
  90. bool valid = false;
  91. for (unsigned i = 0; i < lines.Size(); i++)
  92. {
  93. String line = lines[i];
  94. if (!eval.Length())
  95. {
  96. line = line.Trimmed();
  97. if (line.StartsWith("exports.fields"))
  98. {
  99. eval = line.Substring(8);
  100. if (line.Contains("}"))
  101. {
  102. valid = true;
  103. break;
  104. }
  105. }
  106. }
  107. else
  108. {
  109. eval += line;
  110. }
  111. if (line.Contains("}") && eval.Length())
  112. {
  113. valid = true;
  114. break;
  115. }
  116. }
  117. if (valid)
  118. {
  119. JSVM* vm = JSVM::GetJSVM(NULL);
  120. if (!vm)
  121. return true;
  122. duk_context* ctx = vm->GetJSContext();
  123. int top = duk_get_top(ctx);
  124. duk_push_string(ctx, eval.CString());
  125. duk_push_string(ctx, "eval");
  126. duk_compile(ctx, DUK_COMPILE_EVAL);
  127. if (duk_is_function(ctx, -1) && duk_pcall(ctx, 0) == DUK_EXEC_SUCCESS)
  128. {
  129. if (duk_is_object(ctx, -1))
  130. {
  131. duk_enum(ctx, -1, DUK_ENUM_OWN_PROPERTIES_ONLY);
  132. while (duk_next(ctx, -1, 1)) {
  133. String name = duk_get_string(ctx, -2);
  134. VariantType variantType = VAR_NONE;
  135. Variant defaultValue;
  136. if (duk_is_string(ctx, -1))
  137. {
  138. variantType = VAR_STRING;
  139. defaultValue = duk_to_string(ctx, -1);
  140. }
  141. else if (duk_is_number(ctx, -1))
  142. {
  143. variantType = VAR_FLOAT;
  144. defaultValue = (float) duk_to_number(ctx, -1);
  145. }
  146. else if (duk_is_array(ctx, -1))
  147. {
  148. if (duk_get_length(ctx, -1) > 0)
  149. {
  150. duk_get_prop_index(ctx, -1, 0);
  151. // TODO: class types
  152. variantType = (VariantType) duk_require_number(ctx, -1);
  153. duk_pop(ctx);
  154. }
  155. if (duk_get_length(ctx, -1) > 1)
  156. {
  157. duk_get_prop_index(ctx, -1, 1);
  158. // default value
  159. js_to_variant(ctx, -1, defaultValue);
  160. duk_pop(ctx);
  161. }
  162. }
  163. else if (duk_is_boolean(ctx, -1))
  164. {
  165. variantType = VAR_BOOL;
  166. defaultValue = duk_to_boolean(ctx, -1) ? true : false;
  167. }
  168. if (defaultValue.GetType() != VAR_NONE)
  169. {
  170. defaultFieldValues_[name] = defaultValue;
  171. }
  172. if (variantType != VAR_NONE)
  173. fields_[name] = variantType;
  174. duk_pop_2(ctx); // pop key value
  175. }
  176. duk_pop(ctx); // pop enum object
  177. }
  178. }
  179. duk_set_top(ctx, top);
  180. }
  181. SetMemoryUse(0);
  182. return true;
  183. }
  184. bool JSComponentFile::Save(Serializer& dest) const
  185. {
  186. return true;
  187. }
  188. }