BsMonoAssembly.h 3.3 KB

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