BsScriptAssemblyManager.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. #include "BsScriptAssemblyManager.h"
  2. #include "BsScriptResourceManager.h"
  3. #include "BsScriptGameObjectManager.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 "BsMonoUtil.h"
  12. #include "BsRTTIType.h"
  13. namespace BansheeEngine
  14. {
  15. ScriptAssemblyManager::ScriptAssemblyManager()
  16. :mBaseTypesInitialized(false), mSerializeObjectAttribute(nullptr), mDontSerializeFieldAttribute(nullptr),
  17. mComponentClass(nullptr), mSceneObjectClass(nullptr), mTextureClass(nullptr), mSpriteTextureClass(nullptr),
  18. mSerializeFieldAttribute(nullptr), mHideInInspectorAttribute(nullptr), mSystemArrayClass(nullptr), mSystemGenericListClass(nullptr),
  19. mSystemGenericDictionaryClass(nullptr), mManagedResourceClass(nullptr), mFontClass(nullptr)
  20. {
  21. }
  22. ScriptAssemblyManager::~ScriptAssemblyManager()
  23. {
  24. }
  25. Vector<String> ScriptAssemblyManager::getScriptAssemblies() const
  26. {
  27. Vector<String> initializedAssemblies;
  28. for (auto& assemblyPair : mAssemblyInfos)
  29. initializedAssemblies.push_back(assemblyPair.first);
  30. return initializedAssemblies;
  31. }
  32. void ScriptAssemblyManager::loadAssemblyInfo(const String& assemblyName)
  33. {
  34. if(!mBaseTypesInitialized)
  35. initializeBaseTypes();
  36. // Process all classes and fields
  37. UINT32 mUniqueTypeId = 1;
  38. MonoAssembly* curAssembly = MonoManager::instance().getAssembly(assemblyName);
  39. if(curAssembly == nullptr)
  40. return;
  41. std::shared_ptr<ManagedSerializableAssemblyInfo> assemblyInfo = bs_shared_ptr<ManagedSerializableAssemblyInfo>();
  42. assemblyInfo->mName = assemblyName;
  43. mAssemblyInfos[assemblyName] = assemblyInfo;
  44. // Populate class data
  45. const Vector<MonoClass*>& allClasses = curAssembly->getAllClasses();
  46. for(auto& curClass : allClasses)
  47. {
  48. if((curClass->isSubClassOf(mComponentClass) || curClass->isSubClassOf(mManagedResourceClass) ||
  49. curClass->hasAttribute(mSerializeObjectAttribute)) && curClass != mComponentClass && curClass != mManagedResourceClass)
  50. {
  51. std::shared_ptr<ManagedSerializableTypeInfoObject> typeInfo = bs_shared_ptr<ManagedSerializableTypeInfoObject>();
  52. typeInfo->mTypeNamespace = curClass->getNamespace();
  53. typeInfo->mTypeName = curClass->getTypeName();
  54. MonoType* monoType = mono_class_get_type(curClass->_getInternalClass());
  55. int monoPrimitiveType = mono_type_get_type(monoType);
  56. if(monoPrimitiveType == MONO_TYPE_VALUETYPE)
  57. typeInfo->mValueType = true;
  58. else
  59. typeInfo->mValueType = false;
  60. std::shared_ptr<ManagedSerializableObjectInfo> objInfo = bs_shared_ptr<ManagedSerializableObjectInfo>();
  61. objInfo->mTypeId = mUniqueTypeId++;
  62. objInfo->mTypeInfo = typeInfo;
  63. objInfo->mMonoClass = curClass;
  64. assemblyInfo->mTypeNameToId[objInfo->getFullTypeName()] = objInfo->mTypeId;
  65. assemblyInfo->mObjectInfos[objInfo->mTypeId] = objInfo;
  66. }
  67. }
  68. // Populate field data
  69. for(auto& curClassInfo : assemblyInfo->mObjectInfos)
  70. {
  71. std::shared_ptr<ManagedSerializableObjectInfo> objInfo = curClassInfo.second;
  72. UINT32 mUniqueFieldId = 1;
  73. const Vector<MonoField*>& fields = objInfo->mMonoClass->getAllFields();
  74. for(auto& field : fields)
  75. {
  76. if(field->isStatic())
  77. continue;
  78. ManagedSerializableTypeInfoPtr typeInfo = determineType(field->getType());
  79. if (typeInfo == nullptr)
  80. continue;
  81. std::shared_ptr<ManagedSerializableFieldInfo> fieldInfo = bs_shared_ptr<ManagedSerializableFieldInfo>();
  82. fieldInfo->mFieldId = mUniqueFieldId++;
  83. fieldInfo->mName = field->getName();
  84. fieldInfo->mMonoField = field;
  85. fieldInfo->mTypeInfo = typeInfo;
  86. MonoFieldVisibility visibility = field->getVisibility();
  87. if (visibility == MonoFieldVisibility::Public)
  88. {
  89. if (!field->hasAttribute(mDontSerializeFieldAttribute))
  90. fieldInfo->mFlags = (ScriptFieldFlags)((UINT32)fieldInfo->mFlags | (UINT32)ScriptFieldFlags::Serializable);
  91. if (!field->hasAttribute(mHideInInspectorAttribute))
  92. fieldInfo->mFlags = (ScriptFieldFlags)((UINT32)fieldInfo->mFlags | (UINT32)ScriptFieldFlags::Inspectable);
  93. }
  94. else
  95. {
  96. if (field->hasAttribute(mSerializeFieldAttribute))
  97. fieldInfo->mFlags = (ScriptFieldFlags)((UINT32)fieldInfo->mFlags | (UINT32)ScriptFieldFlags::Serializable);
  98. }
  99. objInfo->mFieldNameToId[fieldInfo->mName] = fieldInfo->mFieldId;
  100. objInfo->mFields[fieldInfo->mFieldId] = fieldInfo;
  101. }
  102. }
  103. // Form parent/child connections
  104. for(auto& curClass : assemblyInfo->mObjectInfos)
  105. {
  106. MonoClass* base = curClass.second->mMonoClass->getBaseClass();
  107. while(base != nullptr)
  108. {
  109. std::shared_ptr<ManagedSerializableObjectInfo> baseObjInfo;
  110. if(getSerializableObjectInfo(base->getNamespace(), base->getTypeName(), baseObjInfo))
  111. {
  112. curClass.second->mBaseClass = baseObjInfo;
  113. baseObjInfo->mDerivedClasses.push_back(curClass.second);
  114. break;
  115. }
  116. base = base->getBaseClass();
  117. }
  118. }
  119. // Finish object info initialization
  120. for (auto& curClassInfo : assemblyInfo->mObjectInfos)
  121. {
  122. std::shared_ptr<ManagedSerializableObjectInfo> objInfo = curClassInfo.second;
  123. objInfo->initialize();
  124. }
  125. }
  126. void ScriptAssemblyManager::refreshAssemblyInfo()
  127. {
  128. for (auto& assemblyInfoEntry : mAssemblyInfos)
  129. assemblyInfoEntry.second = nullptr;
  130. clearScriptObjects();
  131. for (auto& assemblyInfoEntry : mAssemblyInfos)
  132. {
  133. loadAssemblyInfo(assemblyInfoEntry.first);
  134. }
  135. }
  136. ManagedSerializableTypeInfoPtr ScriptAssemblyManager::determineType(MonoClass* monoClass)
  137. {
  138. if(!mBaseTypesInitialized)
  139. BS_EXCEPT(InvalidStateException, "Calling determineType without previously initializing base types.");
  140. MonoType* monoType = mono_class_get_type(monoClass->_getInternalClass());
  141. int monoPrimitiveType = mono_type_get_type(monoType);
  142. // Determine field type
  143. switch(monoPrimitiveType)
  144. {
  145. case MONO_TYPE_BOOLEAN:
  146. {
  147. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr<ManagedSerializableTypeInfoPrimitive>();
  148. typeInfo->mType = ScriptPrimitiveType::Bool;
  149. return typeInfo;
  150. }
  151. case MONO_TYPE_CHAR:
  152. {
  153. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr<ManagedSerializableTypeInfoPrimitive>();
  154. typeInfo->mType = ScriptPrimitiveType::Char;
  155. return typeInfo;
  156. }
  157. case MONO_TYPE_I1:
  158. {
  159. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr<ManagedSerializableTypeInfoPrimitive>();
  160. typeInfo->mType = ScriptPrimitiveType::I8;
  161. return typeInfo;
  162. }
  163. case MONO_TYPE_U1:
  164. {
  165. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr<ManagedSerializableTypeInfoPrimitive>();
  166. typeInfo->mType = ScriptPrimitiveType::U8;
  167. return typeInfo;
  168. }
  169. case MONO_TYPE_I2:
  170. {
  171. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr<ManagedSerializableTypeInfoPrimitive>();
  172. typeInfo->mType = ScriptPrimitiveType::I16;
  173. return typeInfo;
  174. }
  175. case MONO_TYPE_U2:
  176. {
  177. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr<ManagedSerializableTypeInfoPrimitive>();
  178. typeInfo->mType = ScriptPrimitiveType::U16;
  179. return typeInfo;
  180. }
  181. case MONO_TYPE_I4:
  182. {
  183. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr<ManagedSerializableTypeInfoPrimitive>();
  184. typeInfo->mType = ScriptPrimitiveType::I32;
  185. return typeInfo;
  186. }
  187. case MONO_TYPE_U4:
  188. {
  189. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr<ManagedSerializableTypeInfoPrimitive>();
  190. typeInfo->mType = ScriptPrimitiveType::U32;
  191. return typeInfo;
  192. }
  193. case MONO_TYPE_I8:
  194. {
  195. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr<ManagedSerializableTypeInfoPrimitive>();
  196. typeInfo->mType = ScriptPrimitiveType::I64;
  197. return typeInfo;
  198. }
  199. case MONO_TYPE_U8:
  200. {
  201. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr<ManagedSerializableTypeInfoPrimitive>();
  202. typeInfo->mType = ScriptPrimitiveType::U64;
  203. return typeInfo;
  204. }
  205. case MONO_TYPE_STRING:
  206. {
  207. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr<ManagedSerializableTypeInfoPrimitive>();
  208. typeInfo->mType = ScriptPrimitiveType::String;
  209. return typeInfo;
  210. }
  211. case MONO_TYPE_R4:
  212. {
  213. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr<ManagedSerializableTypeInfoPrimitive>();
  214. typeInfo->mType = ScriptPrimitiveType::Float;
  215. return typeInfo;
  216. }
  217. case MONO_TYPE_R8:
  218. {
  219. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr<ManagedSerializableTypeInfoPrimitive>();
  220. typeInfo->mType = ScriptPrimitiveType::Double;
  221. return typeInfo;
  222. }
  223. case MONO_TYPE_CLASS:
  224. if(monoClass->isSubClassOf(mTextureClass))
  225. {
  226. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr<ManagedSerializableTypeInfoPrimitive>();
  227. typeInfo->mType = ScriptPrimitiveType::TextureRef;
  228. return typeInfo;
  229. }
  230. else if(monoClass->isSubClassOf(mSpriteTextureClass))
  231. {
  232. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr<ManagedSerializableTypeInfoPrimitive>();
  233. typeInfo->mType = ScriptPrimitiveType::SpriteTextureRef;
  234. return typeInfo;
  235. }
  236. else if (monoClass->isSubClassOf(mManagedResourceClass))
  237. {
  238. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr<ManagedSerializableTypeInfoPrimitive>();
  239. typeInfo->mType = ScriptPrimitiveType::ManagedResourceRef;
  240. return typeInfo;
  241. }
  242. else if(monoClass->isSubClassOf(mSceneObjectClass))
  243. {
  244. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr<ManagedSerializableTypeInfoPrimitive>();
  245. typeInfo->mType = ScriptPrimitiveType::SceneObjectRef;
  246. return typeInfo;
  247. }
  248. else if(monoClass->isSubClassOf(mComponentClass))
  249. {
  250. std::shared_ptr<ManagedSerializableTypeInfoPrimitive> typeInfo = bs_shared_ptr<ManagedSerializableTypeInfoPrimitive>();
  251. typeInfo->mType = ScriptPrimitiveType::ComponentRef;
  252. return typeInfo;
  253. }
  254. else
  255. {
  256. std::shared_ptr<ManagedSerializableObjectInfo> objInfo;
  257. if (getSerializableObjectInfo(monoClass->getNamespace(), monoClass->getTypeName(), objInfo))
  258. return objInfo->mTypeInfo;
  259. }
  260. break;
  261. case MONO_TYPE_VALUETYPE:
  262. {
  263. std::shared_ptr<ManagedSerializableObjectInfo> objInfo;
  264. if (getSerializableObjectInfo(monoClass->getNamespace(), monoClass->getTypeName(), objInfo))
  265. return objInfo->mTypeInfo;
  266. }
  267. break;
  268. case MONO_TYPE_GENERICINST:
  269. if(monoClass->getFullName() == mSystemGenericListClass->getFullName()) // Full name is part of CIL spec, so it is just fine to compare like this
  270. {
  271. std::shared_ptr<ManagedSerializableTypeInfoList> typeInfo = bs_shared_ptr<ManagedSerializableTypeInfoList>();
  272. MonoProperty& itemProperty = monoClass->getProperty("Item");
  273. MonoClass* itemClass = itemProperty.getReturnType();
  274. if(itemClass != nullptr)
  275. typeInfo->mElementType = determineType(itemClass);
  276. return typeInfo;
  277. }
  278. else if(monoClass->getFullName() == mSystemGenericDictionaryClass->getFullName())
  279. {
  280. std::shared_ptr<ManagedSerializableTypeInfoDictionary> typeInfo = bs_shared_ptr<ManagedSerializableTypeInfoDictionary>();
  281. MonoMethod* getEnumerator = monoClass->getMethod("GetEnumerator");
  282. MonoClass* enumClass = getEnumerator->getReturnType();
  283. MonoProperty& currentProp = enumClass->getProperty("Current");
  284. MonoClass* keyValuePair = currentProp.getReturnType();
  285. MonoProperty& keyProperty = keyValuePair->getProperty("Key");
  286. MonoProperty& valueProperty = keyValuePair->getProperty("Value");
  287. MonoClass* keyClass = keyProperty.getReturnType();
  288. if(keyClass != nullptr)
  289. typeInfo->mKeyType = determineType(keyClass);
  290. MonoClass* valueClass = valueProperty.getReturnType();
  291. if(valueClass != nullptr)
  292. typeInfo->mValueType = determineType(valueClass);
  293. return typeInfo;
  294. }
  295. break;
  296. case MONO_TYPE_SZARRAY:
  297. case MONO_TYPE_ARRAY:
  298. {
  299. std::shared_ptr<ManagedSerializableTypeInfoArray> typeInfo = bs_shared_ptr<ManagedSerializableTypeInfoArray>();
  300. ::MonoClass* elementClass = mono_class_get_element_class(monoClass->_getInternalClass());
  301. if(elementClass != nullptr)
  302. {
  303. MonoClass* monoElementClass = MonoManager::instance().findClass(elementClass);
  304. if(monoElementClass != nullptr)
  305. typeInfo->mElementType = determineType(monoElementClass);
  306. }
  307. typeInfo->mRank = (UINT32)mono_class_get_rank(monoClass->_getInternalClass());
  308. return typeInfo;
  309. }
  310. }
  311. return nullptr;
  312. }
  313. void ScriptAssemblyManager::clearScriptObjects()
  314. {
  315. mBaseTypesInitialized = false;
  316. mSystemArrayClass = nullptr;
  317. mSystemGenericListClass = nullptr;
  318. mSystemGenericDictionaryClass = nullptr;
  319. mSerializeObjectAttribute = nullptr;
  320. mDontSerializeFieldAttribute = nullptr;
  321. mComponentClass = nullptr;
  322. mSceneObjectClass = nullptr;
  323. mManagedResourceClass = nullptr;
  324. mTextureClass = nullptr;
  325. mSpriteTextureClass = nullptr;
  326. mFontClass = nullptr;
  327. mSerializeFieldAttribute = nullptr;
  328. mHideInInspectorAttribute = nullptr;
  329. }
  330. void ScriptAssemblyManager::initializeBaseTypes()
  331. {
  332. // Get necessary classes for detecting needed class & field information
  333. MonoAssembly* corlib = MonoManager::instance().getAssembly("corlib");
  334. if(corlib == nullptr)
  335. BS_EXCEPT(InvalidStateException, "corlib assembly is not loaded.");
  336. MonoAssembly* bansheeEngineAssembly = MonoManager::instance().getAssembly(BansheeEngineAssemblyName);
  337. if(bansheeEngineAssembly == nullptr)
  338. BS_EXCEPT(InvalidStateException, String(BansheeEngineAssemblyName) + " assembly is not loaded.");
  339. mSystemArrayClass = corlib->getClass("System", "Array");
  340. if(mSystemArrayClass == nullptr)
  341. BS_EXCEPT(InvalidStateException, "Cannot find System.Array managed class.");
  342. mSystemGenericListClass = corlib->getClass("System.Collections.Generic", "List`1");
  343. if(mSystemGenericListClass == nullptr)
  344. BS_EXCEPT(InvalidStateException, "Cannot find List<T> managed class.");
  345. mSystemGenericDictionaryClass = corlib->getClass("System.Collections.Generic", "Dictionary`2");
  346. if(mSystemGenericDictionaryClass == nullptr)
  347. BS_EXCEPT(InvalidStateException, "Cannot find Dictionary<TKey, TValue> managed class.");
  348. mSerializeObjectAttribute = bansheeEngineAssembly->getClass("BansheeEngine", "SerializeObject");
  349. if(mSerializeObjectAttribute == nullptr)
  350. BS_EXCEPT(InvalidStateException, "Cannot find SerializableObject managed class.");
  351. mDontSerializeFieldAttribute = bansheeEngineAssembly->getClass("BansheeEngine", "DontSerializeField");
  352. if(mDontSerializeFieldAttribute == nullptr)
  353. BS_EXCEPT(InvalidStateException, "Cannot find DontSerializeField managed class.");
  354. mComponentClass = bansheeEngineAssembly->getClass("BansheeEngine", "Component");
  355. if(mComponentClass == nullptr)
  356. BS_EXCEPT(InvalidStateException, "Cannot find Component managed class.");
  357. mSceneObjectClass = bansheeEngineAssembly->getClass("BansheeEngine", "SceneObject");
  358. if(mSceneObjectClass == nullptr)
  359. BS_EXCEPT(InvalidStateException, "Cannot find SceneObject managed class.");
  360. mManagedResourceClass = bansheeEngineAssembly->getClass("BansheeEngine", "ManagedResource");
  361. if (mManagedResourceClass == nullptr)
  362. BS_EXCEPT(InvalidStateException, "Cannot find ManagedResource managed class.");
  363. mTextureClass = bansheeEngineAssembly->getClass("BansheeEngine", "Texture2D");
  364. if(mTextureClass == nullptr)
  365. BS_EXCEPT(InvalidStateException, "Cannot find Texture2D managed class.");
  366. mSpriteTextureClass = bansheeEngineAssembly->getClass("BansheeEngine", "SpriteTexture");
  367. if(mSpriteTextureClass == nullptr)
  368. BS_EXCEPT(InvalidStateException, "Cannot find SpriteTexture managed class.");
  369. mFontClass = bansheeEngineAssembly->getClass("BansheeEngine", "Font");
  370. if (mFontClass == nullptr)
  371. BS_EXCEPT(InvalidStateException, "Cannot find Font managed class.");
  372. mSerializeFieldAttribute = bansheeEngineAssembly->getClass("BansheeEngine", "SerializeField");
  373. if(mSerializeFieldAttribute == nullptr)
  374. BS_EXCEPT(InvalidStateException, "Cannot find SerializeField managed class.");
  375. mHideInInspectorAttribute = bansheeEngineAssembly->getClass("BansheeEngine", "HideInInspector");
  376. if(mHideInInspectorAttribute == nullptr)
  377. BS_EXCEPT(InvalidStateException, "Cannot find HideInInspector managed class.");
  378. mBaseTypesInitialized = true;
  379. }
  380. bool ScriptAssemblyManager::getSerializableObjectInfo(const String& ns, const String& typeName, std::shared_ptr<ManagedSerializableObjectInfo>& outInfo)
  381. {
  382. String fullName = ns + "." + typeName;
  383. for(auto& curAssembly : mAssemblyInfos)
  384. {
  385. auto iterFind = curAssembly.second->mTypeNameToId.find(fullName);
  386. if(iterFind != curAssembly.second->mTypeNameToId.end())
  387. {
  388. outInfo = curAssembly.second->mObjectInfos[iterFind->second];
  389. return true;
  390. }
  391. }
  392. return false;
  393. }
  394. bool ScriptAssemblyManager::hasSerializableObjectInfo(const String& ns, const String& typeName)
  395. {
  396. String fullName = ns + "." + typeName;
  397. for(auto& curAssembly : mAssemblyInfos)
  398. {
  399. auto iterFind = curAssembly.second->mTypeNameToId.find(fullName);
  400. if(iterFind != curAssembly.second->mTypeNameToId.end())
  401. return true;
  402. }
  403. return false;
  404. }
  405. }