BsScriptAssemblyManager.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsScriptAssemblyManager.h"
  4. #include "BsManagedSerializableObjectInfo.h"
  5. #include "BsMonoManager.h"
  6. #include "BsMonoAssembly.h"
  7. #include "BsMonoClass.h"
  8. #include "BsMonoField.h"
  9. #include "BsMonoMethod.h"
  10. #include "BsMonoProperty.h"
  11. #include "BsScriptManagedResource.h"
  12. #include "BsScriptTexture2D.h"
  13. #include "BsScriptTexture3D.h"
  14. #include "BsScriptTextureCube.h"
  15. #include "BsScriptSpriteTexture.h"
  16. #include "BsScriptMaterial.h"
  17. #include "BsScriptMesh.h"
  18. #include "BsScriptFont.h"
  19. #include "BsScriptShader.h"
  20. #include "BsScriptShaderInclude.h"
  21. #include "BsScriptPlainText.h"
  22. #include "BsScriptScriptCode.h"
  23. #include "BsScriptStringTable.h"
  24. #include "BsScriptGUISkin.h"
  25. #include "BsScriptPhysicsMaterial.h"
  26. #include "BsScriptPhysicsMesh.h"
  27. #include "BsScriptAudioClip.h"
  28. #include "BsScriptPrefab.h"
  29. #include "BsScriptAnimationClip.h"
  30. namespace BansheeEngine
  31. {
  32. ScriptAssemblyManager::ScriptAssemblyManager()
  33. : mBaseTypesInitialized(false), mSystemArrayClass(nullptr), mSystemGenericListClass(nullptr)
  34. , mSystemGenericDictionaryClass(nullptr), mSystemTypeClass(nullptr), mComponentClass(nullptr)
  35. , mSceneObjectClass(nullptr), mMissingComponentClass(nullptr), mSerializeObjectAttribute(nullptr)
  36. , mDontSerializeFieldAttribute(nullptr), mSerializeFieldAttribute(nullptr), mHideInInspectorAttribute(nullptr)
  37. , mShowInInspectorAttribute(nullptr), mRangeAttribute(nullptr), mStepAttribute(nullptr)
  38. {
  39. }
  40. ScriptAssemblyManager::~ScriptAssemblyManager()
  41. {
  42. }
  43. Vector<String> ScriptAssemblyManager::getScriptAssemblies() const
  44. {
  45. Vector<String> initializedAssemblies;
  46. for (auto& assemblyPair : mAssemblyInfos)
  47. initializedAssemblies.push_back(assemblyPair.first);
  48. return initializedAssemblies;
  49. }
  50. void ScriptAssemblyManager::loadAssemblyInfo(const String& assemblyName)
  51. {
  52. if(!mBaseTypesInitialized)
  53. initializeBaseTypes();
  54. // Process all classes and fields
  55. UINT32 mUniqueTypeId = 1;
  56. MonoAssembly* curAssembly = MonoManager::instance().getAssembly(assemblyName);
  57. if(curAssembly == nullptr)
  58. return;
  59. SPtr<ManagedSerializableAssemblyInfo> assemblyInfo = bs_shared_ptr_new<ManagedSerializableAssemblyInfo>();
  60. assemblyInfo->mName = assemblyName;
  61. mAssemblyInfos[assemblyName] = assemblyInfo;
  62. MonoClass* managedResourceClass = ScriptManagedResource::getMetaData()->scriptClass;
  63. // Populate class data
  64. const Vector<MonoClass*>& allClasses = curAssembly->getAllClasses();
  65. for(auto& curClass : allClasses)
  66. {
  67. if ((curClass->isSubClassOf(mComponentClass) || curClass->isSubClassOf(managedResourceClass) ||
  68. curClass->hasAttribute(mSerializeObjectAttribute)) && curClass != mComponentClass && curClass != managedResourceClass)
  69. {
  70. SPtr<ManagedSerializableTypeInfoObject> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoObject>();
  71. typeInfo->mTypeNamespace = curClass->getNamespace();
  72. typeInfo->mTypeName = curClass->getTypeName();
  73. typeInfo->mTypeId = mUniqueTypeId++;
  74. MonoPrimitiveType monoPrimitiveType = MonoUtil::getPrimitiveType(curClass->_getInternalClass());
  75. if(monoPrimitiveType == MonoPrimitiveType::ValueType)
  76. typeInfo->mValueType = true;
  77. else
  78. typeInfo->mValueType = false;
  79. SPtr<ManagedSerializableObjectInfo> objInfo = bs_shared_ptr_new<ManagedSerializableObjectInfo>();
  80. objInfo->mTypeInfo = typeInfo;
  81. objInfo->mMonoClass = curClass;
  82. assemblyInfo->mTypeNameToId[objInfo->getFullTypeName()] = typeInfo->mTypeId;
  83. assemblyInfo->mObjectInfos[typeInfo->mTypeId] = objInfo;
  84. }
  85. }
  86. // Populate field & property data
  87. for(auto& curClassInfo : assemblyInfo->mObjectInfos)
  88. {
  89. SPtr<ManagedSerializableObjectInfo> objInfo = curClassInfo.second;
  90. UINT32 mUniqueFieldId = 1;
  91. const Vector<MonoField*>& fields = objInfo->mMonoClass->getAllFields();
  92. for(auto& field : fields)
  93. {
  94. if(field->isStatic())
  95. continue;
  96. SPtr<ManagedSerializableTypeInfo> typeInfo = getTypeInfo(field->getType());
  97. if (typeInfo == nullptr)
  98. continue;
  99. SPtr<ManagedSerializableFieldInfo> fieldInfo = bs_shared_ptr_new<ManagedSerializableFieldInfo>();
  100. fieldInfo->mFieldId = mUniqueFieldId++;
  101. fieldInfo->mName = field->getName();
  102. fieldInfo->mMonoField = field;
  103. fieldInfo->mTypeInfo = typeInfo;
  104. fieldInfo->mParentTypeId = objInfo->mTypeInfo->mTypeId;
  105. MonoMemberVisibility visibility = field->getVisibility();
  106. if (visibility == MonoMemberVisibility::Public)
  107. {
  108. if (!field->hasAttribute(mDontSerializeFieldAttribute))
  109. fieldInfo->mFlags |= ScriptFieldFlag::Serializable;
  110. if (!field->hasAttribute(mHideInInspectorAttribute))
  111. fieldInfo->mFlags |= ScriptFieldFlag::Inspectable;
  112. fieldInfo->mFlags |= ScriptFieldFlag::Animable;
  113. }
  114. else
  115. {
  116. if (field->hasAttribute(mSerializeFieldAttribute))
  117. fieldInfo->mFlags |= ScriptFieldFlag::Serializable;
  118. if (field->hasAttribute(mShowInInspectorAttribute))
  119. fieldInfo->mFlags |= ScriptFieldFlag::Inspectable;
  120. }
  121. if (field->hasAttribute(mRangeAttribute))
  122. fieldInfo->mFlags |= ScriptFieldFlag::Range;
  123. if (field->hasAttribute(mStepAttribute))
  124. fieldInfo->mFlags |= ScriptFieldFlag::Step;
  125. objInfo->mFieldNameToId[fieldInfo->mName] = fieldInfo->mFieldId;
  126. objInfo->mFields[fieldInfo->mFieldId] = fieldInfo;
  127. }
  128. const Vector<MonoProperty*>& properties = objInfo->mMonoClass->getAllProperties();
  129. for (auto& property : properties)
  130. {
  131. SPtr<ManagedSerializableTypeInfo> typeInfo = getTypeInfo(property->getReturnType());
  132. if (typeInfo == nullptr)
  133. continue;
  134. SPtr<ManagedSerializablePropertyInfo> propertyInfo = bs_shared_ptr_new<ManagedSerializablePropertyInfo>();
  135. propertyInfo->mFieldId = mUniqueFieldId++;
  136. propertyInfo->mName = property->getName();
  137. propertyInfo->mMonoProperty = property;
  138. propertyInfo->mTypeInfo = typeInfo;
  139. propertyInfo->mParentTypeId = objInfo->mTypeInfo->mTypeId;
  140. if (!property->isIndexed())
  141. {
  142. MonoMemberVisibility visibility = property->getVisibility();
  143. if (visibility == MonoMemberVisibility::Public)
  144. propertyInfo->mFlags |= ScriptFieldFlag::Animable;
  145. if (property->hasAttribute(mSerializeFieldAttribute))
  146. propertyInfo->mFlags |= ScriptFieldFlag::Serializable;
  147. if (property->hasAttribute(mShowInInspectorAttribute))
  148. propertyInfo->mFlags |= ScriptFieldFlag::Inspectable;
  149. }
  150. if (property->hasAttribute(mRangeAttribute))
  151. propertyInfo->mFlags |= ScriptFieldFlag::Range;
  152. if (property->hasAttribute(mStepAttribute))
  153. propertyInfo->mFlags |= ScriptFieldFlag::Step;
  154. objInfo->mFieldNameToId[propertyInfo->mName] = propertyInfo->mFieldId;
  155. objInfo->mFields[propertyInfo->mFieldId] = propertyInfo;
  156. }
  157. }
  158. // Form parent/child connections
  159. for(auto& curClass : assemblyInfo->mObjectInfos)
  160. {
  161. MonoClass* base = curClass.second->mMonoClass->getBaseClass();
  162. while(base != nullptr)
  163. {
  164. SPtr<ManagedSerializableObjectInfo> baseObjInfo;
  165. if(getSerializableObjectInfo(base->getNamespace(), base->getTypeName(), baseObjInfo))
  166. {
  167. curClass.second->mBaseClass = baseObjInfo;
  168. baseObjInfo->mDerivedClasses.push_back(curClass.second);
  169. break;
  170. }
  171. base = base->getBaseClass();
  172. }
  173. }
  174. }
  175. void ScriptAssemblyManager::clearAssemblyInfo()
  176. {
  177. clearScriptObjects();
  178. mAssemblyInfos.clear();
  179. }
  180. SPtr<ManagedSerializableTypeInfo> ScriptAssemblyManager::getTypeInfo(MonoClass* monoClass)
  181. {
  182. if(!mBaseTypesInitialized)
  183. BS_EXCEPT(InvalidStateException, "Calling getTypeInfo without previously initializing base types.");
  184. MonoPrimitiveType monoPrimitiveType = MonoUtil::getPrimitiveType(monoClass->_getInternalClass());
  185. // If enum get the enum base data type
  186. bool isEnum = MonoUtil::isEnum(monoClass->_getInternalClass());
  187. if (isEnum)
  188. monoPrimitiveType = MonoUtil::getEnumPrimitiveType(monoClass->_getInternalClass());
  189. // Determine field type
  190. switch(monoPrimitiveType)
  191. {
  192. case MonoPrimitiveType::Boolean:
  193. {
  194. SPtr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  195. typeInfo->mType = ScriptPrimitiveType::Bool;
  196. return typeInfo;
  197. }
  198. case MonoPrimitiveType::Char:
  199. {
  200. SPtr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  201. typeInfo->mType = ScriptPrimitiveType::Char;
  202. return typeInfo;
  203. }
  204. case MonoPrimitiveType::I8:
  205. {
  206. SPtr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  207. typeInfo->mType = ScriptPrimitiveType::I8;
  208. return typeInfo;
  209. }
  210. case MonoPrimitiveType::U8:
  211. {
  212. SPtr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  213. typeInfo->mType = ScriptPrimitiveType::U8;
  214. return typeInfo;
  215. }
  216. case MonoPrimitiveType::I16:
  217. {
  218. SPtr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  219. typeInfo->mType = ScriptPrimitiveType::I16;
  220. return typeInfo;
  221. }
  222. case MonoPrimitiveType::U16:
  223. {
  224. SPtr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  225. typeInfo->mType = ScriptPrimitiveType::U16;
  226. return typeInfo;
  227. }
  228. case MonoPrimitiveType::I32:
  229. {
  230. SPtr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  231. typeInfo->mType = ScriptPrimitiveType::I32;
  232. return typeInfo;
  233. }
  234. case MonoPrimitiveType::U32:
  235. {
  236. SPtr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  237. typeInfo->mType = ScriptPrimitiveType::U32;
  238. return typeInfo;
  239. }
  240. case MonoPrimitiveType::I64:
  241. {
  242. SPtr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  243. typeInfo->mType = ScriptPrimitiveType::I64;
  244. return typeInfo;
  245. }
  246. case MonoPrimitiveType::U64:
  247. {
  248. SPtr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  249. typeInfo->mType = ScriptPrimitiveType::U64;
  250. return typeInfo;
  251. }
  252. case MonoPrimitiveType::String:
  253. {
  254. SPtr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  255. typeInfo->mType = ScriptPrimitiveType::String;
  256. return typeInfo;
  257. }
  258. case MonoPrimitiveType::R32:
  259. {
  260. SPtr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  261. typeInfo->mType = ScriptPrimitiveType::Float;
  262. return typeInfo;
  263. }
  264. case MonoPrimitiveType::R64:
  265. {
  266. SPtr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoPrimitive>();
  267. typeInfo->mType = ScriptPrimitiveType::Double;
  268. return typeInfo;
  269. }
  270. case MonoPrimitiveType::Class:
  271. if(monoClass->isSubClassOf(ScriptResource::getMetaData()->scriptClass)) // Resource
  272. {
  273. SPtr<ManagedSerializableTypeInfoRef> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoRef>();
  274. typeInfo->mTypeNamespace = monoClass->getNamespace();
  275. typeInfo->mTypeName = monoClass->getTypeName();
  276. if(monoClass == ScriptResource::getMetaData()->scriptClass)
  277. typeInfo->mType = ScriptReferenceType::Resource;
  278. else if (monoClass->isSubClassOf(ScriptTexture2D::getMetaData()->scriptClass))
  279. typeInfo->mType = ScriptReferenceType::Texture2D;
  280. else if (monoClass->isSubClassOf(ScriptTexture3D::getMetaData()->scriptClass))
  281. typeInfo->mType = ScriptReferenceType::Texture3D;
  282. else if (monoClass->isSubClassOf(ScriptTextureCube::getMetaData()->scriptClass))
  283. typeInfo->mType = ScriptReferenceType::TextureCube;
  284. else if (monoClass->isSubClassOf(ScriptSpriteTexture::getMetaData()->scriptClass))
  285. typeInfo->mType = ScriptReferenceType::SpriteTexture;
  286. else if (monoClass->isSubClassOf(ScriptManagedResource::getMetaData()->scriptClass))
  287. typeInfo->mType = ScriptReferenceType::ManagedResource;
  288. else if (monoClass->isSubClassOf(ScriptShader::getMetaData()->scriptClass))
  289. typeInfo->mType = ScriptReferenceType::Shader;
  290. else if (monoClass->isSubClassOf(ScriptShaderInclude::getMetaData()->scriptClass))
  291. typeInfo->mType = ScriptReferenceType::ShaderInclude;
  292. else if (monoClass->isSubClassOf(ScriptMaterial::getMetaData()->scriptClass))
  293. typeInfo->mType = ScriptReferenceType::Material;
  294. else if (monoClass->isSubClassOf(ScriptMesh::getMetaData()->scriptClass))
  295. typeInfo->mType = ScriptReferenceType::Mesh;
  296. else if (monoClass->isSubClassOf(ScriptPlainText::getMetaData()->scriptClass))
  297. typeInfo->mType = ScriptReferenceType::PlainText;
  298. else if (monoClass->isSubClassOf(ScriptScriptCode::getMetaData()->scriptClass))
  299. typeInfo->mType = ScriptReferenceType::ScriptCode;
  300. else if (monoClass->isSubClassOf(ScriptPrefab::getMetaData()->scriptClass))
  301. typeInfo->mType = ScriptReferenceType::Prefab;
  302. else if (monoClass->isSubClassOf(ScriptFont::getMetaData()->scriptClass))
  303. typeInfo->mType = ScriptReferenceType::Font;
  304. else if (monoClass->isSubClassOf(ScriptStringTable::getMetaData()->scriptClass))
  305. typeInfo->mType = ScriptReferenceType::StringTable;
  306. else if (monoClass->isSubClassOf(ScriptGUISkin::getMetaData()->scriptClass))
  307. typeInfo->mType = ScriptReferenceType::GUISkin;
  308. else if (monoClass->isSubClassOf(ScriptPhysicsMaterial::getMetaData()->scriptClass))
  309. typeInfo->mType = ScriptReferenceType::PhysicsMaterial;
  310. else if (monoClass->isSubClassOf(ScriptPhysicsMesh::getMetaData()->scriptClass))
  311. typeInfo->mType = ScriptReferenceType::PhysicsMesh;
  312. else if (monoClass->isSubClassOf(ScriptAudioClip::getMetaData()->scriptClass))
  313. typeInfo->mType = ScriptReferenceType::AudioClip;
  314. else if (monoClass->isSubClassOf(ScriptAnimationClip::getMetaData()->scriptClass))
  315. typeInfo->mType = ScriptReferenceType::AnimationClip;
  316. else
  317. {
  318. assert(false && "Unrecognized resource type");
  319. }
  320. return typeInfo;
  321. }
  322. else if (monoClass->isSubClassOf(mSceneObjectClass) || monoClass->isSubClassOf(mComponentClass)) // Game object
  323. {
  324. SPtr<ManagedSerializableTypeInfoRef> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoRef>();
  325. typeInfo->mTypeNamespace = monoClass->getNamespace();
  326. typeInfo->mTypeName = monoClass->getTypeName();
  327. if (monoClass == mComponentClass)
  328. typeInfo->mType = ScriptReferenceType::Component;
  329. else if (monoClass->isSubClassOf(mSceneObjectClass))
  330. typeInfo->mType = ScriptReferenceType::SceneObject;
  331. else if (monoClass->isSubClassOf(mComponentClass))
  332. typeInfo->mType = ScriptReferenceType::ManagedComponent;
  333. return typeInfo;
  334. }
  335. else
  336. {
  337. SPtr<ManagedSerializableObjectInfo> objInfo;
  338. if (getSerializableObjectInfo(monoClass->getNamespace(), monoClass->getTypeName(), objInfo))
  339. return objInfo->mTypeInfo;
  340. }
  341. break;
  342. case MonoPrimitiveType::ValueType:
  343. {
  344. SPtr<ManagedSerializableObjectInfo> objInfo;
  345. if (getSerializableObjectInfo(monoClass->getNamespace(), monoClass->getTypeName(), objInfo))
  346. return objInfo->mTypeInfo;
  347. }
  348. break;
  349. case MonoPrimitiveType::Generic:
  350. if(monoClass->getFullName() == mSystemGenericListClass->getFullName()) // Full name is part of CIL spec, so it is just fine to compare like this
  351. {
  352. SPtr<ManagedSerializableTypeInfoList> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoList>();
  353. MonoProperty* itemProperty = monoClass->getProperty("Item");
  354. MonoClass* itemClass = itemProperty->getReturnType();
  355. if (itemClass != nullptr)
  356. typeInfo->mElementType = getTypeInfo(itemClass);
  357. if (typeInfo->mElementType == nullptr)
  358. return nullptr;
  359. return typeInfo;
  360. }
  361. else if(monoClass->getFullName() == mSystemGenericDictionaryClass->getFullName())
  362. {
  363. SPtr<ManagedSerializableTypeInfoDictionary> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoDictionary>();
  364. MonoMethod* getEnumerator = monoClass->getMethod("GetEnumerator");
  365. MonoClass* enumClass = getEnumerator->getReturnType();
  366. MonoProperty* currentProp = enumClass->getProperty("Current");
  367. MonoClass* keyValuePair = currentProp->getReturnType();
  368. MonoProperty* keyProperty = keyValuePair->getProperty("Key");
  369. MonoProperty* valueProperty = keyValuePair->getProperty("Value");
  370. MonoClass* keyClass = keyProperty->getReturnType();
  371. if(keyClass != nullptr)
  372. typeInfo->mKeyType = getTypeInfo(keyClass);
  373. MonoClass* valueClass = valueProperty->getReturnType();
  374. if(valueClass != nullptr)
  375. typeInfo->mValueType = getTypeInfo(valueClass);
  376. if (typeInfo->mKeyType == nullptr || typeInfo->mValueType == nullptr)
  377. return nullptr;
  378. return typeInfo;
  379. }
  380. break;
  381. case MonoPrimitiveType::Array:
  382. {
  383. SPtr<ManagedSerializableTypeInfoArray> typeInfo = bs_shared_ptr_new<ManagedSerializableTypeInfoArray>();
  384. ::MonoClass* elementClass = ScriptArray::getElementClass(monoClass->_getInternalClass());
  385. if(elementClass != nullptr)
  386. {
  387. MonoClass* monoElementClass = MonoManager::instance().findClass(elementClass);
  388. if(monoElementClass != nullptr)
  389. typeInfo->mElementType = getTypeInfo(monoElementClass);
  390. }
  391. if (typeInfo->mElementType == nullptr)
  392. return nullptr;
  393. typeInfo->mRank = ScriptArray::getRank(monoClass->_getInternalClass());
  394. return typeInfo;
  395. }
  396. default:
  397. break;
  398. }
  399. return nullptr;
  400. }
  401. void ScriptAssemblyManager::clearScriptObjects()
  402. {
  403. mBaseTypesInitialized = false;
  404. mSystemArrayClass = nullptr;
  405. mSystemGenericListClass = nullptr;
  406. mSystemGenericDictionaryClass = nullptr;
  407. mSystemTypeClass = nullptr;
  408. mSerializeObjectAttribute = nullptr;
  409. mDontSerializeFieldAttribute = nullptr;
  410. mComponentClass = nullptr;
  411. mSceneObjectClass = nullptr;
  412. mMissingComponentClass = nullptr;
  413. mSerializeFieldAttribute = nullptr;
  414. mHideInInspectorAttribute = nullptr;
  415. mShowInInspectorAttribute = nullptr;
  416. mRangeAttribute = nullptr;
  417. mStepAttribute = nullptr;
  418. }
  419. void ScriptAssemblyManager::initializeBaseTypes()
  420. {
  421. // Get necessary classes for detecting needed class & field information
  422. MonoAssembly* corlib = MonoManager::instance().getAssembly("corlib");
  423. if(corlib == nullptr)
  424. BS_EXCEPT(InvalidStateException, "corlib assembly is not loaded.");
  425. MonoAssembly* bansheeEngineAssembly = MonoManager::instance().getAssembly(ENGINE_ASSEMBLY);
  426. if(bansheeEngineAssembly == nullptr)
  427. BS_EXCEPT(InvalidStateException, String(ENGINE_ASSEMBLY) + " assembly is not loaded.");
  428. mSystemArrayClass = corlib->getClass("System", "Array");
  429. if(mSystemArrayClass == nullptr)
  430. BS_EXCEPT(InvalidStateException, "Cannot find System.Array managed class.");
  431. mSystemGenericListClass = corlib->getClass("System.Collections.Generic", "List`1");
  432. if(mSystemGenericListClass == nullptr)
  433. BS_EXCEPT(InvalidStateException, "Cannot find List<T> managed class.");
  434. mSystemGenericDictionaryClass = corlib->getClass("System.Collections.Generic", "Dictionary`2");
  435. if(mSystemGenericDictionaryClass == nullptr)
  436. BS_EXCEPT(InvalidStateException, "Cannot find Dictionary<TKey, TValue> managed class.");
  437. mSystemTypeClass = corlib->getClass("System", "Type");
  438. if (mSystemTypeClass == nullptr)
  439. BS_EXCEPT(InvalidStateException, "Cannot find Type managed class.");
  440. mSerializeObjectAttribute = bansheeEngineAssembly->getClass("BansheeEngine", "SerializeObject");
  441. if(mSerializeObjectAttribute == nullptr)
  442. BS_EXCEPT(InvalidStateException, "Cannot find SerializableObject managed class.");
  443. mDontSerializeFieldAttribute = bansheeEngineAssembly->getClass("BansheeEngine", "DontSerializeField");
  444. if(mDontSerializeFieldAttribute == nullptr)
  445. BS_EXCEPT(InvalidStateException, "Cannot find DontSerializeField managed class.");
  446. mRangeAttribute = bansheeEngineAssembly->getClass("BansheeEngine", "Range");
  447. if (mRangeAttribute == nullptr)
  448. BS_EXCEPT(InvalidStateException, "Cannot find Range managed class.");
  449. mStepAttribute = bansheeEngineAssembly->getClass("BansheeEngine", "Step");
  450. if (mStepAttribute == nullptr)
  451. BS_EXCEPT(InvalidStateException, "Cannot find Step managed class.");
  452. mComponentClass = bansheeEngineAssembly->getClass("BansheeEngine", "Component");
  453. if(mComponentClass == nullptr)
  454. BS_EXCEPT(InvalidStateException, "Cannot find Component managed class.");
  455. mMissingComponentClass = bansheeEngineAssembly->getClass("BansheeEngine", "MissingComponent");
  456. if (mMissingComponentClass == nullptr)
  457. BS_EXCEPT(InvalidStateException, "Cannot find MissingComponent managed class.");
  458. mSceneObjectClass = bansheeEngineAssembly->getClass("BansheeEngine", "SceneObject");
  459. if(mSceneObjectClass == nullptr)
  460. BS_EXCEPT(InvalidStateException, "Cannot find SceneObject managed class.");
  461. mSerializeFieldAttribute = bansheeEngineAssembly->getClass("BansheeEngine", "SerializeField");
  462. if(mSerializeFieldAttribute == nullptr)
  463. BS_EXCEPT(InvalidStateException, "Cannot find SerializeField managed class.");
  464. mHideInInspectorAttribute = bansheeEngineAssembly->getClass("BansheeEngine", "HideInInspector");
  465. if(mHideInInspectorAttribute == nullptr)
  466. BS_EXCEPT(InvalidStateException, "Cannot find HideInInspector managed class.");
  467. mShowInInspectorAttribute = bansheeEngineAssembly->getClass("BansheeEngine", "ShowInInspector");
  468. if (mShowInInspectorAttribute == nullptr)
  469. BS_EXCEPT(InvalidStateException, "Cannot find ShowInInspector managed class.");
  470. mBaseTypesInitialized = true;
  471. }
  472. bool ScriptAssemblyManager::getSerializableObjectInfo(const String& ns, const String& typeName, SPtr<ManagedSerializableObjectInfo>& outInfo)
  473. {
  474. String fullName = ns + "." + typeName;
  475. for(auto& curAssembly : mAssemblyInfos)
  476. {
  477. if (curAssembly.second == nullptr)
  478. continue;
  479. auto iterFind = curAssembly.second->mTypeNameToId.find(fullName);
  480. if(iterFind != curAssembly.second->mTypeNameToId.end())
  481. {
  482. outInfo = curAssembly.second->mObjectInfos[iterFind->second];
  483. return true;
  484. }
  485. }
  486. return false;
  487. }
  488. bool ScriptAssemblyManager::hasSerializableObjectInfo(const String& ns, const String& typeName)
  489. {
  490. String fullName = ns + "." + typeName;
  491. for(auto& curAssembly : mAssemblyInfos)
  492. {
  493. auto iterFind = curAssembly.second->mTypeNameToId.find(fullName);
  494. if(iterFind != curAssembly.second->mTypeNameToId.end())
  495. return true;
  496. }
  497. return false;
  498. }
  499. }