BsRuntimeScriptObjects.cpp 17 KB

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