BsMonoAssembly.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 Attempts to find a managed class with the specified namespace and name
  37. * in this assembly. Returns null if one cannot be found.
  38. */
  39. MonoClass* getClass(const String& namespaceName, const String& name) const;
  40. /**
  41. * @brief Converts an internal mono representation of a class into engine class.
  42. */
  43. MonoClass* getClass(::MonoClass* rawMonoClass) const;
  44. /**
  45. * @brief Returns a list of all classes in the assembly.
  46. */
  47. const Vector<MonoClass*>& getAllClasses() const;
  48. /**
  49. * @brief Invokes a zero-parameter static method in the format
  50. * "Class::Method". Used primarily for invoking an assembly entry point.
  51. */
  52. void invoke(const String& functionName);
  53. private:
  54. friend class MonoManager;
  55. MonoAssembly();
  56. /**
  57. * @brief Loads an assembly from the specified path.
  58. */
  59. void load(const String& path, const String& name);
  60. /**
  61. * @brief Loads an assembly from an internal mono image.
  62. */
  63. void loadAsDependency(MonoImage* image, const String& name);
  64. /**
  65. * @brief Unloads the assembly and all the types associated with it.
  66. * Caller must ensure not to use any types from this assembly after
  67. * it has been unloaded.
  68. */
  69. void unload();
  70. /**
  71. * @brief Returns true if the provided name represents a generic class.
  72. *
  73. * @note This method only analyzes the name to determine if it is in generic class format.
  74. */
  75. bool isGenericClass(const String& name) const;
  76. String mName;
  77. MonoImage* mMonoImage;
  78. ::MonoAssembly* mMonoAssembly;
  79. bool mIsLoaded;
  80. bool mIsDependency;
  81. mutable UnorderedMap<ClassId, MonoClass*, ClassId::Hash, ClassId::Equals> mClasses;
  82. mutable UnorderedMap<::MonoClass*, MonoClass*> mClassesByRaw;
  83. mutable bool mHaveCachedClassList;
  84. mutable Vector<MonoClass*> mCachedClassList;
  85. };
  86. }