BsMonoManager.h 3.0 KB

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