BsRuntimeScriptObjects.cpp 17 KB

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