BsMonoManager.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 Available Mono versions
  10. */
  11. enum class MonoVersion
  12. {
  13. v4_0,
  14. v4_5
  15. };
  16. /**
  17. * @brief Loads Mono script assemblies and manages script objects.
  18. */
  19. class BS_MONO_EXPORT MonoManager : public Module<MonoManager>
  20. {
  21. public:
  22. MonoManager();
  23. ~MonoManager();
  24. /**
  25. * @brief Loads a new assembly from the provided path.
  26. *
  27. * @param path Absolute path to the assembly .dll.
  28. * @param name Unique name for the assembly.
  29. */
  30. MonoAssembly& loadAssembly(const String& path, const String& name);
  31. /**
  32. * @brief Unloads a previously loaded assembly.
  33. *
  34. * @note You must perform JIT cleanup before unloading assemblies.
  35. */
  36. void unloadAssembly(MonoAssembly& assembly);
  37. /**
  38. * @brief Searches all loaded assemblies for the specified class.
  39. */
  40. MonoClass* findClass(const String& ns, const String& typeName);
  41. /**
  42. * @brief Searches all loaded assemblies for the specified class.
  43. */
  44. MonoClass* findClass(::MonoClass* rawMonoClass);
  45. /**
  46. * @brief Returns the current Mono domains.
  47. */
  48. MonoDomain* getDomain() const { return mScriptDomain; }
  49. /**
  50. * @brief Attempts to find a previously loaded assembly with the specified name.
  51. * Returns null if assembly cannot be found.
  52. */
  53. MonoAssembly* getAssembly(const String& name) const;
  54. /**
  55. * @brief Unloads the active domain all script assemblies are loaded in
  56. * and destroys any managed objects associated with it.
  57. */
  58. void unloadScriptDomain();
  59. /**
  60. * @brief Loads a new script domain. If another domain already exists it will be unloaded. This will also
  61. * restore any previously loaded assemblies.
  62. */
  63. void loadScriptDomain();
  64. /**
  65. * @brief Returns the absolute location of the folder where Mono framework assemblies are located.
  66. */
  67. Path getFrameworkAssembliesFolder() const;
  68. /**
  69. * @brief Returns the absolute path to the Mono compiler.
  70. */
  71. Path getCompilerPath() const;
  72. /**
  73. * @brief Registers a new script type. This should be done before any assembly loading is done.
  74. * Upon assembly load these script types will be initialized with necessary information about their
  75. * managed counterparts.
  76. */
  77. static void registerScriptType(ScriptMeta* metaData);
  78. private:
  79. /**
  80. * @brief Initializes a previous loaded assembly.
  81. */
  82. void initializeAssembly(MonoAssembly& assembly);
  83. /**
  84. * @brief Returns a list of all types that will be initializes with their assembly gets loaded.
  85. */
  86. static UnorderedMap<String, Vector<ScriptMeta*>>& getTypesToInitialize()
  87. {
  88. static UnorderedMap<String, Vector<ScriptMeta*>> mTypesToInitialize;
  89. return mTypesToInitialize;
  90. }
  91. UnorderedMap<String, MonoAssembly*> mAssemblies;
  92. MonoDomain* mScriptDomain;
  93. MonoDomain* mRootDomain;
  94. bool mIsCoreLoaded;
  95. };
  96. }