BsMonoManager.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #pragma once
  2. #include "BsMonoPrerequisites.h"
  3. #include "BsScriptMeta.h"
  4. #include "BsModule.h"
  5. #include <mono/jit/jit.h>
  6. namespace BansheeEngine
  7. {
  8. /**
  9. * @brief Loads Mono script assemblies and manages script objects.
  10. */
  11. class BS_MONO_EXPORT MonoManager : public Module<MonoManager>
  12. {
  13. public:
  14. MonoManager();
  15. ~MonoManager();
  16. /**
  17. * @brief Loads a new assembly from the provided path.
  18. *
  19. * @param path Absolute path to the assembly .dll.
  20. * @param name Unique name for the assembly.
  21. */
  22. MonoAssembly& loadAssembly(const String& path, const String& name);
  23. /**
  24. * @brief Unloads a previously loaded assembly.
  25. *
  26. * @note You must perform JIT cleanup before unloading assemblies.
  27. */
  28. void unloadAssembly(MonoAssembly& assembly);
  29. /**
  30. * @brief Searches all loaded assemblies for the specified class.
  31. */
  32. MonoClass* findClass(const String& ns, const String& typeName);
  33. /**
  34. * @brief Searches all loaded assemblies for the specified class.
  35. */
  36. MonoClass* findClass(::MonoClass* rawMonoClass);
  37. /**
  38. * @brief Returns the type name of the provided object, with namespace.
  39. *
  40. * @param obj If non-null, the object to get the type name of.
  41. */
  42. String getFullTypeName(MonoObject* obj);
  43. /**
  44. * @brief Returns the namespace of the provided object.
  45. *
  46. * @param obj If non-null, the object to get the namespace of.
  47. */
  48. String getNamespace(MonoObject* obj);
  49. /**
  50. * @brief Returns the type name of the provided object, without namespace.
  51. *
  52. * @param obj If non-null, the object to get the type name of.
  53. */
  54. String getTypeName(MonoObject* obj);
  55. /**
  56. * @brief Returns the current Mono domains.
  57. */
  58. MonoDomain* getDomain() const { return mScriptDomain; }
  59. /**
  60. * @brief Attempts to find a previously loaded assembly with the specified name.
  61. * Returns null if assembly cannot be found.
  62. */
  63. MonoAssembly* getAssembly(const String& name) const;
  64. /**
  65. * @brief Registers a new script type. This should be done before any assembly loading is done.
  66. * Upon assembly load these script types will be initialized with necessary information about their
  67. * managed counterparts.
  68. */
  69. static void registerScriptType(ScriptMeta* metaData);
  70. private:
  71. static const String MONO_LIB_DIR;
  72. static const String MONO_ETC_DIR;
  73. /**
  74. * @brief Returns a list of all types that will be initializes with their assembly gets loaded.
  75. */
  76. static UnorderedMap<String, Vector<ScriptMeta*>>& getTypesToInitialize()
  77. {
  78. static UnorderedMap<String, Vector<ScriptMeta*>> mTypesToInitialize;
  79. return mTypesToInitialize;
  80. }
  81. UnorderedMap<String, MonoAssembly*> mAssemblies;
  82. MonoDomain* mScriptDomain;
  83. MonoDomain* mRootDomain;
  84. bool mIsCoreLoaded;
  85. };
  86. }