BsMonoManager.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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, const ScriptMeta& localMetaData);
  61. /** Triggered when the assembly domain and all relevant assemblies are about to be unloaded. */
  62. Event<void()> onDomainUnload;
  63. private:
  64. struct ScriptMetaInfo
  65. {
  66. ScriptMeta* metaData;
  67. ScriptMeta localMetaData;
  68. };
  69. /** Initializes a previous loaded assembly. */
  70. void initializeAssembly(MonoAssembly& assembly);
  71. /** Returns a list of all types that will be initializes with their assembly gets loaded. */
  72. static UnorderedMap<String, Vector<ScriptMetaInfo>>& getScriptMetaData()
  73. {
  74. static UnorderedMap<String, Vector<ScriptMetaInfo>> mTypesToInitialize;
  75. return mTypesToInitialize;
  76. }
  77. UnorderedMap<String, MonoAssembly*> mAssemblies;
  78. MonoDomain* mScriptDomain;
  79. MonoDomain* mRootDomain;
  80. bool mIsCoreLoaded;
  81. };
  82. /** @} */
  83. }