BsMonoAssembly.h 3.5 KB

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