BsMonoManager.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 main (scripting) 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. /** Returns the absolute path of the folder where Mono framework assemblies are located. */
  46. Path getFrameworkAssembliesFolder() const;
  47. /** Returns the absolute path to the Mono /etc folder that is required for initializing Mono. */
  48. Path getMonoEtcFolder() const;
  49. /** Returns the absolute path to the Mono compiler managed executable. */
  50. Path getCompilerPath() const;
  51. /** Returns the absolute path to the executable capable of executing managed assemblies. */
  52. Path getMonoExecPath() const;
  53. /**
  54. * Registers a new script type. This should be done before any assembly loading is done. Upon assembly load these
  55. * script types will be initialized with necessary information about their managed counterparts.
  56. */
  57. static void registerScriptType(ScriptMeta* metaData, const ScriptMeta& localMetaData);
  58. /** Triggered when the assembly domain and all relevant assemblies are about to be unloaded. */
  59. Event<void()> onDomainUnload;
  60. private:
  61. struct ScriptMetaInfo
  62. {
  63. ScriptMeta* metaData;
  64. ScriptMeta localMetaData;
  65. };
  66. /** Initializes a previous loaded assembly. */
  67. void initializeAssembly(MonoAssembly& assembly);
  68. /** Returns a list of all types that will be initializes with their assembly gets loaded. */
  69. static UnorderedMap<String, Vector<ScriptMetaInfo>>& getScriptMetaData()
  70. {
  71. static UnorderedMap<String, Vector<ScriptMetaInfo>> mTypesToInitialize;
  72. return mTypesToInitialize;
  73. }
  74. UnorderedMap<String, MonoAssembly*> mAssemblies;
  75. MonoDomain* mScriptDomain;
  76. MonoDomain* mRootDomain;
  77. MonoAssembly* mCorlibAssembly;
  78. };
  79. /** @} */
  80. }