BsMonoManager.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 Searches all loaded assemblies for the specified class.
  33. */
  34. MonoClass* findClass(const String& ns, const String& typeName);
  35. /**
  36. * @brief Searches all loaded assemblies for the specified class.
  37. */
  38. MonoClass* findClass(::MonoClass* rawMonoClass);
  39. /**
  40. * @brief Returns the current Mono domains.
  41. */
  42. MonoDomain* getDomain() const { return mScriptDomain; }
  43. /**
  44. * @brief Attempts to find a previously loaded assembly with the specified name.
  45. * Returns null if assembly cannot be found.
  46. */
  47. MonoAssembly* getAssembly(const String& name) const;
  48. /**
  49. * @brief Unloads the active domain all script assemblies are loaded in
  50. * and destroys any managed objects associated with it.
  51. */
  52. void unloadScriptDomain();
  53. /**
  54. * @brief Loads a new script domain. If another domain already exists it will be unloaded. This will also
  55. * restore any previously loaded assemblies.
  56. */
  57. void loadScriptDomain();
  58. /**
  59. * @brief Returns the path of the folder where Mono framework assemblies are located. Path is relative
  60. * to the application executable.
  61. */
  62. Path getFrameworkAssembliesFolder() const;
  63. /**
  64. * @brief Returns the path to the Mono /etc folder that is required for initializing Mono. Path is relative
  65. * to the application executable.
  66. */
  67. Path getMonoEtcFolder() 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. /**
  79. * @brief Triggered when the assembly domain and all relevant assemblies are about to be unloaded.
  80. */
  81. Event<void()> onDomainUnload;
  82. private:
  83. /**
  84. * @brief Initializes a previous loaded assembly.
  85. */
  86. void initializeAssembly(MonoAssembly& assembly);
  87. /**
  88. * @brief Returns a list of all types that will be initializes with their assembly gets loaded.
  89. */
  90. static UnorderedMap<String, Vector<ScriptMeta*>>& getScriptMetaData()
  91. {
  92. static UnorderedMap<String, Vector<ScriptMeta*>> mTypesToInitialize;
  93. return mTypesToInitialize;
  94. }
  95. UnorderedMap<String, MonoAssembly*> mAssemblies;
  96. MonoDomain* mScriptDomain;
  97. MonoDomain* mRootDomain;
  98. bool mIsCoreLoaded;
  99. };
  100. }