BsMonoManager.h 3.3 KB

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