BsRuntimeScriptObjects.cpp 17 KB

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