CSComponentAssembly.cpp 11 KB

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