CSComponentAssembly.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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/Core/Profiler.h>
  24. #include <Atomic/IO/Deserializer.h>
  25. #include <Atomic/IO/Serializer.h>
  26. #include <Atomic/IO/Log.h>
  27. #include <Atomic/IO/FileSystem.h>
  28. #include <Atomic/Resource/ResourceCache.h>
  29. #include <Atomic/Resource/JSONFile.h>
  30. #include <Atomic/Resource/ResourceMapRouter.h>
  31. #include <Atomic/Script/ScriptSystem.h>
  32. #include "NETScriptEvents.h"
  33. #include "CSComponentAssembly.h"
  34. namespace Atomic
  35. {
  36. HashMap<StringHash, VariantType> CSComponentAssembly::typeMap_;
  37. CSComponentAssembly::CSComponentAssembly(Context* context) :
  38. ScriptComponentFile(context)
  39. {
  40. }
  41. CSComponentAssembly::~CSComponentAssembly()
  42. {
  43. }
  44. void CSComponentAssembly::InitTypeMap()
  45. {
  46. typeMap_["Boolean"] = VAR_BOOL;
  47. typeMap_["Int32"] = VAR_INT;
  48. typeMap_["UInt32"] = VAR_INT;
  49. typeMap_["Single"] = VAR_FLOAT;
  50. typeMap_["Double"] = VAR_DOUBLE;
  51. typeMap_["String"] = VAR_STRING;
  52. typeMap_["Vector2"] = VAR_VECTOR2;
  53. typeMap_["Vector3"] = VAR_VECTOR3;
  54. typeMap_["Vector4"] = VAR_VECTOR4;
  55. typeMap_["Quaternion"] = VAR_QUATERNION;
  56. typeMap_["IntVector2"] = VAR_INTVECTOR2;
  57. typeMap_["Color"] = VAR_COLOR;
  58. }
  59. CSComponent* CSComponentAssembly::CreateCSComponent(const String& classname)
  60. {
  61. return nullptr;
  62. }
  63. bool CSComponentAssembly::ParseComponentClassJSON(const JSONValue& json)
  64. {
  65. if (!typeMap_.Size())
  66. InitTypeMap();
  67. String className = json.Get("name").GetString();
  68. classNames_.Push(className);
  69. const JSONValue& jfields = json.Get("fields");
  70. PODVector<StringHash> enumsAdded;
  71. if (jfields.IsArray())
  72. {
  73. for (unsigned i = 0; i < jfields.GetArray().Size(); i++)
  74. {
  75. const JSONValue& jfield = jfields.GetArray().At(i);
  76. VariantType varType = VAR_NONE;
  77. bool isEnum = jfield.Get("isEnum").GetBool();
  78. bool isArray = jfield.Get("isArray").GetBool();
  79. unsigned fixedArraySize = 0;
  80. String typeName = jfield.Get("typeName").GetString();
  81. String resourceTypeName;
  82. String fieldName = jfield.Get("name").GetString();
  83. String defaultValue = jfield.Get("defaultValue").GetString();
  84. String tooltip;
  85. if (!defaultValue.Length())
  86. {
  87. JSONArray caPos = jfield.Get("caPos").GetArray();
  88. if (caPos.Size())
  89. defaultValue = caPos[0].GetString();
  90. }
  91. JSONObject caNamed = jfield.Get("caNamed").GetObject();
  92. if (!defaultValue.Length())
  93. {
  94. if (caNamed.Contains("DefaultValue"))
  95. defaultValue = caNamed["DefaultValue"].GetString();
  96. }
  97. // tooltip
  98. if (caNamed.Contains("Tooltip"))
  99. {
  100. tooltip = caNamed["Tooltip"].GetString();
  101. }
  102. // fixed array size
  103. if (caNamed.Contains("ArraySize"))
  104. {
  105. fixedArraySize = (unsigned)ToInt(caNamed["ArraySize"].GetString().CString());
  106. }
  107. if (isEnum && assemblyEnums_.Contains(typeName) && !enumsAdded.Contains(fieldName))
  108. {
  109. varType = VAR_INT;
  110. enumsAdded.Push(fieldName);
  111. const Vector<EnumInfo>& einfos = assemblyEnums_[typeName];
  112. for (unsigned i = 0; i < einfos.Size(); i++)
  113. AddEnum(/*typeName*/fieldName, einfos[i], className);
  114. }
  115. if (varType == VAR_NONE && typeMap_.Contains(typeName))
  116. varType = typeMap_[typeName];
  117. if (varType == VAR_NONE)
  118. {
  119. // FIXME: We need to be able to test if a type is a ResourceRef, this isn't really the way to achieve that
  120. const HashMap<StringHash, SharedPtr<ObjectFactory>>& factories = context_->GetObjectFactories();
  121. HashMap<StringHash, SharedPtr<ObjectFactory>>::ConstIterator itr = factories.Begin();
  122. while (itr != factories.End())
  123. {
  124. if (itr->second_->GetFactoryTypeName() == typeName)
  125. {
  126. varType = VAR_RESOURCEREF;
  127. resourceTypeName = typeName;
  128. break;
  129. }
  130. itr++;
  131. }
  132. if (varType == VAR_NONE)
  133. {
  134. ATOMIC_LOGERRORF("Component Class %s contains unmappable type %s in field %s",
  135. className.CString(), typeName.CString(), fieldName.CString());
  136. continue;
  137. }
  138. }
  139. if (!isArray)
  140. {
  141. if (!defaultValue.Length() && varType == VAR_RESOURCEREF)
  142. {
  143. // We still need a default value for ResourceRef's so we know the classtype
  144. AddDefaultValue(fieldName, ResourceRef(typeName), className);
  145. }
  146. else
  147. {
  148. Variant value;
  149. if (varType == VAR_RESOURCEREF)
  150. {
  151. ResourceRef rref(typeName);
  152. rref.name_ = defaultValue;
  153. value = rref;
  154. }
  155. else
  156. {
  157. value.FromString(varType, defaultValue);
  158. }
  159. AddDefaultValue(fieldName, value, className);
  160. }
  161. }
  162. AddField(fieldName, varType, resourceTypeName, isArray, isEnum, fixedArraySize, className, tooltip);
  163. }
  164. }
  165. return true;
  166. }
  167. bool CSComponentAssembly::ParseAssemblyJSON(const JSONValue& json)
  168. {
  169. Clear();
  170. assemblyEnums_.Clear();
  171. classNames_.Clear();
  172. const JSONArray& enums = json.Get("enums").GetArray();
  173. // parse to all enums hash
  174. for (unsigned i = 0; i < enums.Size(); i++)
  175. {
  176. const JSONValue& ejson = enums.At(i);
  177. String enumName = ejson.Get("name").GetString();
  178. const JSONObject& evalues = ejson.Get("values").GetObject();
  179. JSONObject::ConstIterator itr = evalues.Begin();
  180. Vector<EnumInfo> values;
  181. while (itr != evalues.End())
  182. {
  183. EnumInfo info;
  184. info.name_ = itr->first_;
  185. info.value_ = itr->second_.GetInt();
  186. values.Push(info);
  187. itr++;
  188. }
  189. assemblyEnums_[enumName] = values;
  190. }
  191. const JSONArray& components = json.Get("components").GetArray();
  192. for (unsigned i = 0; i < components.Size(); i++)
  193. {
  194. const JSONValue& cjson = components.At(i);
  195. ParseComponentClassJSON(cjson);
  196. }
  197. return true;
  198. }
  199. void CSComponentAssembly::RegisterObject(Context* context)
  200. {
  201. context->RegisterFactory<CSComponentAssembly>();
  202. }
  203. bool CSComponentAssembly::BeginLoad(Deserializer& source)
  204. {
  205. // TODO: Assemblies in packages?
  206. File* sourceFile = (File*) &source;
  207. fullAssemblyPath_ = sourceFile->GetFullPath();
  208. String text = sourceFile->ReadText();
  209. JSONValue root;
  210. if (!JSONFile::ParseJSON(text, root, false))
  211. {
  212. ATOMIC_LOGERRORF("Failed to load assembly json for %s ", fullAssemblyPath_.CString());
  213. return false;
  214. }
  215. if (!ParseAssemblyJSON(root))
  216. {
  217. ATOMIC_LOGERRORF("Failed to parse assembly json for %s ", fullAssemblyPath_.CString());
  218. return false;
  219. }
  220. VariantMap eventData;
  221. using namespace CSComponentAssemblyReference;
  222. eventData[P_ASSEMBLYPATH] = fullAssemblyPath_;
  223. SendEvent(E_CSCOMPONENTASSEMBLYREFERENCE, eventData);
  224. return true;
  225. }
  226. bool CSComponentAssembly::Save(Serializer& dest) const
  227. {
  228. return true;
  229. }
  230. CSComponentAssembly* CSComponentAssembly::ResolveClassAssembly(const String& fullClassName)
  231. {
  232. Context* context = ScriptSystem::GetContext();
  233. assert(context);
  234. String classname = fullClassName;
  235. String csnamespace;
  236. // Handle namespaces
  237. if (fullClassName.Contains('.'))
  238. {
  239. StringVector elements = fullClassName.Split('.');
  240. if (elements.Size() <= 1)
  241. return 0;
  242. classname = elements.Back();
  243. elements.Pop();
  244. csnamespace = String::Joined(elements, ".");
  245. }
  246. ResourceCache* cache = context->GetSubsystem<ResourceCache>();
  247. PODVector<CSComponentAssembly*> assemblies;
  248. cache->GetResources<CSComponentAssembly>(assemblies);
  249. for (unsigned i = 0; i < assemblies.Size(); i++)
  250. {
  251. CSComponentAssembly* assembly = assemblies[i];
  252. // TODO: support namespaces
  253. const StringVector& classNames = assembly->GetClassNames();
  254. if (classNames.Contains(classname))
  255. {
  256. return assembly;
  257. }
  258. }
  259. return 0;
  260. }
  261. bool CSComponentAssembly::PreloadClassAssemblies()
  262. {
  263. ATOMIC_LOGINFO("Preloading Class Assemblies");
  264. Context* context = ScriptSystem::GetContext();
  265. if (!context)
  266. return false;
  267. ResourceMapRouter* resourceMap = context->GetSubsystem<ResourceMapRouter>();
  268. if (!resourceMap)
  269. {
  270. ATOMIC_LOGERROR("CSComponentAssembly::PreloadClassAssemblies - ResourceMapRouter subsystem unavailable");
  271. return false;
  272. }
  273. ResourceCache* cache = context->GetSubsystem<ResourceCache>();
  274. if (!cache)
  275. {
  276. ATOMIC_LOGERROR("CSComponentAssembly::PreloadClassAssemblies - ResourceCache subsystem unavailable");
  277. return false;
  278. }
  279. const HashMap<StringHash, String>* assemblies = resourceMap->GetTypeCacheMap("CSComponentAssembly");
  280. if (!assemblies)
  281. return true;
  282. HashMap<StringHash, String>::ConstIterator itr = assemblies->Begin();
  283. while (itr != assemblies->End())
  284. {
  285. if (!cache->GetResource<CSComponentAssembly>(itr->second_, false))
  286. {
  287. ATOMIC_LOGERRORF("CSComponentAssembly::PreloadClassAssemblies - Error getting resource %s", itr->second_.CString());
  288. return false;
  289. }
  290. itr++;
  291. }
  292. return true;
  293. }
  294. }