BsMonoManager.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsMonoPrerequisites.h"
  5. #include "BsScriptMeta.h"
  6. #include "BsModule.h"
  7. #include <mono/jit/jit.h>
  8. namespace BansheeEngine
  9. {
  10. /**
  11. * @brief Available Mono versions
  12. */
  13. enum class MonoVersion
  14. {
  15. v4_0,
  16. v4_5
  17. };
  18. /**
  19. * @brief Loads Mono script assemblies and manages script objects.
  20. */
  21. class BS_MONO_EXPORT MonoManager : public Module<MonoManager>
  22. {
  23. public:
  24. MonoManager();
  25. ~MonoManager();
  26. /**
  27. * @brief Loads a new assembly from the provided path.
  28. *
  29. * @param path Absolute path to the assembly .dll.
  30. * @param name Unique name for the assembly.
  31. */
  32. MonoAssembly& loadAssembly(const String& path, const String& name);
  33. /**
  34. * @brief Searches all loaded assemblies for the specified class.
  35. */
  36. MonoClass* findClass(const String& ns, const String& typeName);
  37. /**
  38. * @brief Searches all loaded assemblies for the specified class.
  39. */
  40. MonoClass* findClass(::MonoClass* rawMonoClass);
  41. /**
  42. * @brief Returns the current Mono domains.
  43. */
  44. MonoDomain* getDomain() const { return mScriptDomain; }
  45. /**
  46. * @brief Attempts to find a previously loaded assembly with the specified name.
  47. * Returns null if assembly cannot be found.
  48. */
  49. MonoAssembly* getAssembly(const String& name) const;
  50. /**
  51. * @brief Unloads the active domain all script assemblies are loaded in
  52. * and destroys any managed objects associated with it.
  53. */
  54. void unloadScriptDomain();
  55. /**
  56. * @brief Loads a new script domain. If another domain already exists it will be unloaded. This will also
  57. * restore any previously loaded assemblies.
  58. */
  59. void loadScriptDomain();
  60. /**
  61. * @brief Returns the path of the folder where Mono framework assemblies are located. Path is relative
  62. * to the application executable.
  63. */
  64. Path getFrameworkAssembliesFolder() const;
  65. /**
  66. * @brief Returns the path to the Mono /etc folder that is required for initializing Mono. Path is relative
  67. * to the application executable.
  68. */
  69. Path getMonoEtcFolder() const;
  70. /**
  71. * @brief Returns the absolute path to the Mono compiler.
  72. */
  73. Path getCompilerPath() const;
  74. /**
  75. * @brief Registers a new script type. This should be done before any assembly loading is done.
  76. * Upon assembly load these script types will be initialized with necessary information about their
  77. * managed counterparts.
  78. */
  79. static void registerScriptType(ScriptMeta* metaData);
  80. /**
  81. * @brief Triggered when the assembly domain and all relevant assemblies are about to be unloaded.
  82. */
  83. Event<void()> onDomainUnload;
  84. private:
  85. /**
  86. * @brief Initializes a previous loaded assembly.
  87. */
  88. void initializeAssembly(MonoAssembly& assembly);
  89. /**
  90. * @brief Returns a list of all types that will be initializes with their assembly gets loaded.
  91. */
  92. static UnorderedMap<String, Vector<ScriptMeta*>>& getScriptMetaData()
  93. {
  94. static UnorderedMap<String, Vector<ScriptMeta*>> mTypesToInitialize;
  95. return mTypesToInitialize;
  96. }
  97. UnorderedMap<String, MonoAssembly*> mAssemblies;
  98. MonoDomain* mScriptDomain;
  99. MonoDomain* mRootDomain;
  100. bool mIsCoreLoaded;
  101. };
  102. }