CSComponentAssembly.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  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/IO/FileSystem.h>
  26. #include <Atomic/Core/Profiler.h>
  27. #include <Atomic/Resource/ResourceCache.h>
  28. #include <Atomic/IO/Serializer.h>
  29. #include "NETScriptEvents.h"
  30. #include "CSComponentAssembly.h"
  31. namespace Atomic
  32. {
  33. HashMap<StringHash, VariantType> CSComponentAssembly::typeMap_;
  34. CSComponentAssembly::CSComponentAssembly(Context* context) :
  35. ScriptComponentFile(context)
  36. {
  37. }
  38. CSComponentAssembly::~CSComponentAssembly()
  39. {
  40. }
  41. void CSComponentAssembly::InitTypeMap()
  42. {
  43. typeMap_["Boolean"] = VAR_BOOL;
  44. typeMap_["Int32"] = VAR_INT;
  45. typeMap_["UInt32"] = VAR_INT;
  46. typeMap_["Single"] = VAR_FLOAT;
  47. typeMap_["Double"] = VAR_DOUBLE;
  48. typeMap_["String"] = VAR_STRING;
  49. typeMap_["Vector2"] = VAR_VECTOR2;
  50. typeMap_["Vector3"] = VAR_VECTOR3;
  51. typeMap_["Vector4"] = VAR_VECTOR4;
  52. typeMap_["Quaternion"] = VAR_QUATERNION;
  53. typeMap_["IntVector2"] = VAR_INTVECTOR2;
  54. }
  55. CSComponent* CSComponentAssembly::CreateCSComponent(const String& classname)
  56. {
  57. return nullptr;
  58. }
  59. bool CSComponentAssembly::ParseComponentClassJSON(const JSONValue& json)
  60. {
  61. if (!typeMap_.Size())
  62. InitTypeMap();
  63. String className = json.Get("name").GetString();
  64. classNames_.Push(className);
  65. const JSONValue& jfields = json.Get("fields");
  66. PODVector<StringHash> enumsAdded;
  67. if (jfields.IsArray())
  68. {
  69. for (unsigned i = 0; i < jfields.GetArray().Size(); i++)
  70. {
  71. const JSONValue& jfield = jfields.GetArray().At(i);
  72. VariantType varType = VAR_NONE;
  73. bool isEnum = jfield.Get("isEnum").GetBool();
  74. String typeName = jfield.Get("typeName").GetString();
  75. String fieldName = jfield.Get("name").GetString();
  76. String defaultValue = jfield.Get("defaultValue").GetString();
  77. if (!defaultValue.Length())
  78. {
  79. JSONArray caPos = jfield.Get("caPos").GetArray();
  80. if (caPos.Size())
  81. defaultValue = caPos[0].GetString();
  82. }
  83. if (!defaultValue.Length())
  84. {
  85. JSONObject caNamed = jfield.Get("caNamed").GetObject();
  86. if (caNamed.Contains("DefaultValue"))
  87. defaultValue = caNamed["DefaultValue"].GetString();
  88. }
  89. if (isEnum && assemblyEnums_.Contains(typeName) && !enumsAdded.Contains(fieldName))
  90. {
  91. varType = VAR_INT;
  92. enumsAdded.Push(fieldName);
  93. const Vector<EnumInfo>& einfos = assemblyEnums_[typeName];
  94. for (unsigned i = 0; i < einfos.Size(); i++)
  95. AddEnum(/*typeName*/fieldName, einfos[i], className);
  96. }
  97. if (varType == VAR_NONE && typeMap_.Contains(typeName))
  98. varType = typeMap_[typeName];
  99. if (varType == VAR_NONE)
  100. {
  101. // FIXME: We need to be able to test if a type is a ResourceRef, this isn't really the way to achieve that
  102. const HashMap<StringHash, SharedPtr<ObjectFactory>>& factories = context_->GetObjectFactories();
  103. HashMap<StringHash, SharedPtr<ObjectFactory>>::ConstIterator itr = factories.Begin();
  104. while (itr != factories.End())
  105. {
  106. if (itr->second_->GetTypeName() == typeName)
  107. {
  108. varType = VAR_RESOURCEREF;
  109. break;
  110. }
  111. itr++;
  112. }
  113. if (varType == VAR_NONE)
  114. {
  115. ATOMIC_LOGERRORF("Component Class %s contains unmappable type %s in field %s",
  116. className.CString(), typeName.CString(), fieldName.CString());
  117. continue;
  118. }
  119. }
  120. if (!defaultValue.Length() && varType == VAR_RESOURCEREF)
  121. {
  122. // We still need a default value for ResourceRef's so we know the classtype
  123. AddDefaultValue(fieldName, ResourceRef(typeName), className);
  124. }
  125. else
  126. {
  127. Variant value;
  128. if (varType == VAR_RESOURCEREF)
  129. {
  130. ResourceRef rref(typeName);
  131. rref.name_ = defaultValue;
  132. value = rref;
  133. }
  134. else
  135. {
  136. value.FromString(varType, defaultValue);
  137. }
  138. AddDefaultValue(fieldName, value, className);
  139. }
  140. AddField(fieldName, varType, className);
  141. }
  142. }
  143. return true;
  144. }
  145. bool CSComponentAssembly::ParseAssemblyJSON(const JSONValue& json)
  146. {
  147. Clear();
  148. assemblyEnums_.Clear();
  149. classNames_.Clear();
  150. const JSONArray& enums = json.Get("enums").GetArray();
  151. // parse to all enums hash
  152. for (unsigned i = 0; i < enums.Size(); i++)
  153. {
  154. const JSONValue& ejson = enums.At(i);
  155. String enumName = ejson.Get("name").GetString();
  156. const JSONObject& evalues = ejson.Get("values").GetObject();
  157. JSONObject::ConstIterator itr = evalues.Begin();
  158. Vector<EnumInfo> values;
  159. while (itr != evalues.End())
  160. {
  161. EnumInfo info;
  162. info.name_ = itr->first_;
  163. info.value_ = itr->second_.GetInt();
  164. values.Push(info);
  165. itr++;
  166. }
  167. assemblyEnums_[enumName] = values;
  168. }
  169. const JSONArray& components = json.Get("components").GetArray();
  170. for (unsigned i = 0; i < components.Size(); i++)
  171. {
  172. const JSONValue& cjson = components.At(i);
  173. ParseComponentClassJSON(cjson);
  174. }
  175. return true;
  176. }
  177. void CSComponentAssembly::RegisterObject(Context* context)
  178. {
  179. context->RegisterFactory<CSComponentAssembly>();
  180. }
  181. bool CSComponentAssembly::BeginLoad(Deserializer& source)
  182. {
  183. fullAssemblyPath_ = source.GetName();
  184. VariantMap eventData;
  185. using namespace CSComponentAssemblyReference;
  186. eventData[P_ASSEMBLYPATH] = fullAssemblyPath_;
  187. SendEvent(E_CSCOMPONENTASSEMBLYREFERENCE, eventData);
  188. return true;
  189. }
  190. bool CSComponentAssembly::Save(Serializer& dest) const
  191. {
  192. return true;
  193. }
  194. }