CSComponentAssembly.cpp 12 KB

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