BsMonoAssembly.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 <mono/jit/jit.h>
  6. #include <mono/metadata/assembly.h>
  7. namespace BansheeEngine
  8. {
  9. /** @addtogroup Mono
  10. * @{
  11. */
  12. /** Contains information about a single Mono (managed) assembly. */
  13. class BS_MONO_EXPORT MonoAssembly
  14. {
  15. public:
  16. /** @cond INTERNAL */
  17. /** Used for uniquely identifying a managed class, normally for use in containers. */
  18. struct ClassId
  19. {
  20. struct Hash
  21. {
  22. inline size_t operator()(const ClassId& v) const;
  23. };
  24. struct Equals
  25. {
  26. inline bool operator()(const ClassId &a, const ClassId &b) const;
  27. };
  28. ClassId();
  29. ClassId(const String& namespaceName, String name, ::MonoClass* genericInstance = nullptr);
  30. String namespaceName;
  31. String name;
  32. ::MonoClass* genericInstance;
  33. };
  34. /** @endcond */
  35. public:
  36. virtual ~MonoAssembly();
  37. /** Returns the name of this assembly. */
  38. const String& getName() const { return mName; }
  39. /**
  40. * Attempts to find a managed class with the specified namespace and name in this assembly. Returns null if one
  41. * cannot be found.
  42. */
  43. MonoClass* getClass(const String& namespaceName, const String& name) const;
  44. /** Converts an internal mono representation of a class into engine class. */
  45. MonoClass* getClass(::MonoClass* rawMonoClass) const;
  46. /** Returns a list of all classes in the assembly. */
  47. const Vector<MonoClass*>& getAllClasses() const;
  48. /**
  49. * Invokes a zero-parameter static method in the format "Class::Method". Used primarily for invoking an assembly
  50. * entry point.
  51. */
  52. void invoke(const String& functionName);
  53. private:
  54. friend class MonoManager;
  55. MonoAssembly(const String& path, const String& name);
  56. /**
  57. * Attempts to find a managed class with the specified namespace and name in this assembly. Registers a new class
  58. * using the provided raw class if one cannot be found. Returns null provided raw class is null.
  59. */
  60. MonoClass* getClass(const String& namespaceName, const String& name, ::MonoClass* rawMonoClass) const;
  61. /** Loads an assembly into the specified domain. */
  62. void load(MonoDomain* domain);
  63. /**
  64. * Initializes an assembly from an internal mono image.
  65. *
  66. * @note
  67. * Normally used for assemblies that were already loaded by the managed runtime as dependencies.
  68. */
  69. void loadFromImage(MonoImage* image);
  70. /**
  71. * Unloads the assembly and all the types associated with it. Caller must ensure not to use any types from this
  72. * assembly after it has been unloaded.
  73. */
  74. void unload();
  75. /**
  76. * Returns true if the provided name represents a generic class.
  77. *
  78. * @note This method only analyzes the name to determine if it is in generic class format.
  79. */
  80. bool isGenericClass(const String& name) const;
  81. String mName;
  82. String mPath;
  83. MonoImage* mMonoImage;
  84. ::MonoAssembly* mMonoAssembly;
  85. bool mIsLoaded;
  86. bool mIsDependency;
  87. mutable UnorderedMap<ClassId, MonoClass*, ClassId::Hash, ClassId::Equals> mClasses;
  88. mutable UnorderedMap<::MonoClass*, MonoClass*> mClassesByRaw;
  89. mutable bool mHaveCachedClassList;
  90. mutable Vector<MonoClass*> mCachedClassList;
  91. };
  92. /** @} */
  93. }