IUnityInterface.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #pragma once
  2. // Unity native plugin API
  3. // Compatible with C99
  4. #if defined(__CYGWIN32__)
  5. #define UNITY_INTERFACE_API __stdcall
  6. #define UNITY_INTERFACE_EXPORT __declspec(dllexport)
  7. #elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) || defined(WINAPI_FAMILY)
  8. #define UNITY_INTERFACE_API __stdcall
  9. #define UNITY_INTERFACE_EXPORT __declspec(dllexport)
  10. #elif defined(__MACH__) || defined(__ANDROID__) || defined(__linux__)
  11. #define UNITY_INTERFACE_API
  12. #define UNITY_INTERFACE_EXPORT
  13. #else
  14. #define UNITY_INTERFACE_API
  15. #define UNITY_INTERFACE_EXPORT
  16. #endif
  17. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  18. // IUnityInterface is a registry of interfaces we choose to expose to plugins.
  19. //
  20. // USAGE:
  21. // ---------
  22. // To retrieve an interface a user can do the following from a plugin, assuming they have the header file for the interface:
  23. //
  24. // IMyInterface * ptr = registry->Get<IMyInterface>();
  25. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  26. // Unity Interface GUID
  27. // Ensures global uniqueness.
  28. //
  29. // Template specialization is used to produce a means of looking up a GUID from its interface type at compile time.
  30. // The net result should compile down to passing around the GUID.
  31. //
  32. // UNITY_REGISTER_INTERFACE_GUID should be placed in the header file of any interface definition outside of all namespaces.
  33. // The interface structure and the registration GUID are all that is required to expose the interface to other systems.
  34. struct UnityInterfaceGUID
  35. {
  36. #ifdef __cplusplus
  37. UnityInterfaceGUID(unsigned long long high, unsigned long long low)
  38. : m_GUIDHigh(high)
  39. , m_GUIDLow(low)
  40. {
  41. }
  42. UnityInterfaceGUID(const UnityInterfaceGUID& other)
  43. {
  44. m_GUIDHigh = other.m_GUIDHigh;
  45. m_GUIDLow = other.m_GUIDLow;
  46. }
  47. UnityInterfaceGUID& operator=(const UnityInterfaceGUID& other)
  48. {
  49. m_GUIDHigh = other.m_GUIDHigh;
  50. m_GUIDLow = other.m_GUIDLow;
  51. return *this;
  52. }
  53. bool Equals(const UnityInterfaceGUID& other) const { return m_GUIDHigh == other.m_GUIDHigh && m_GUIDLow == other.m_GUIDLow; }
  54. bool LessThan(const UnityInterfaceGUID& other) const { return m_GUIDHigh < other.m_GUIDHigh || (m_GUIDHigh == other.m_GUIDHigh && m_GUIDLow < other.m_GUIDLow); }
  55. #endif
  56. unsigned long long m_GUIDHigh;
  57. unsigned long long m_GUIDLow;
  58. };
  59. #ifdef __cplusplus
  60. inline bool operator==(const UnityInterfaceGUID& left, const UnityInterfaceGUID& right) { return left.Equals(right); }
  61. inline bool operator!=(const UnityInterfaceGUID& left, const UnityInterfaceGUID& right) { return !left.Equals(right); }
  62. inline bool operator<(const UnityInterfaceGUID& left, const UnityInterfaceGUID& right) { return left.LessThan(right); }
  63. inline bool operator>(const UnityInterfaceGUID& left, const UnityInterfaceGUID& right) { return right.LessThan(left); }
  64. inline bool operator>=(const UnityInterfaceGUID& left, const UnityInterfaceGUID& right) { return !operator<(left, right); }
  65. inline bool operator<=(const UnityInterfaceGUID& left, const UnityInterfaceGUID& right) { return !operator>(left, right); }
  66. #else
  67. typedef struct UnityInterfaceGUID UnityInterfaceGUID;
  68. #endif
  69. #ifdef __cplusplus
  70. #define UNITY_DECLARE_INTERFACE(NAME) \
  71. struct NAME : IUnityInterface
  72. // Generic version of GetUnityInterfaceGUID to allow us to specialize it
  73. // per interface below. The generic version has no actual implementation
  74. // on purpose.
  75. //
  76. // If you get errors about return values related to this method then
  77. // you have forgotten to include UNITY_REGISTER_INTERFACE_GUID with
  78. // your interface, or it is not visible at some point when you are
  79. // trying to retrieve or add an interface.
  80. template<typename TYPE>
  81. inline const UnityInterfaceGUID GetUnityInterfaceGUID();
  82. // This is the macro you provide in your public interface header
  83. // outside of a namespace to allow us to map between type and GUID
  84. // without the user having to worry about it when attempting to
  85. // add or retrieve and interface from the registry.
  86. #define UNITY_REGISTER_INTERFACE_GUID(HASHH, HASHL, TYPE) \
  87. template<> \
  88. inline const UnityInterfaceGUID GetUnityInterfaceGUID<TYPE>() \
  89. { \
  90. return UnityInterfaceGUID(HASHH,HASHL); \
  91. }
  92. // Same as UNITY_REGISTER_INTERFACE_GUID but allows the interface to live in
  93. // a particular namespace. As long as the namespace is visible at the time you call
  94. // GetUnityInterfaceGUID< INTERFACETYPE >() or you explicitly qualify it in the template
  95. // calls this will work fine, only the macro here needs to have the additional parameter
  96. #define UNITY_REGISTER_INTERFACE_GUID_IN_NAMESPACE(HASHH, HASHL, TYPE, NAMESPACE) \
  97. const UnityInterfaceGUID TYPE##_GUID(HASHH, HASHL); \
  98. template<> \
  99. inline const UnityInterfaceGUID GetUnityInterfaceGUID< NAMESPACE :: TYPE >() \
  100. { \
  101. return UnityInterfaceGUID(HASHH,HASHL); \
  102. }
  103. // These macros allow for C compatibility in user code.
  104. #define UNITY_GET_INTERFACE_GUID(TYPE) GetUnityInterfaceGUID< TYPE >()
  105. #else
  106. #define UNITY_DECLARE_INTERFACE(NAME) \
  107. typedef struct NAME NAME; \
  108. struct NAME
  109. // NOTE: This has the downside that one some compilers it will not get stripped from all compilation units that
  110. // can see a header containing this constant. However, it's only for C compatibility and thus should have
  111. // minimal impact.
  112. #define UNITY_REGISTER_INTERFACE_GUID(HASHH, HASHL, TYPE) \
  113. const UnityInterfaceGUID TYPE##_GUID = {HASHH, HASHL};
  114. // In general namespaces are going to be a problem for C code any interfaces we expose in a namespace are
  115. // not going to be usable from C.
  116. #define UNITY_REGISTER_INTERFACE_GUID_IN_NAMESPACE(HASHH, HASHL, TYPE, NAMESPACE)
  117. // These macros allow for C compatibility in user code.
  118. #define UNITY_GET_INTERFACE_GUID(TYPE) TYPE##_GUID
  119. #endif
  120. // Using this in user code rather than INTERFACES->Get<TYPE>() will be C compatible for those places in plugins where
  121. // this may be needed. Unity code itself does not need this.
  122. #define UNITY_GET_INTERFACE(INTERFACES, TYPE) (TYPE*)INTERFACES->GetInterfaceSplit (UNITY_GET_INTERFACE_GUID(TYPE).m_GUIDHigh, UNITY_GET_INTERFACE_GUID(TYPE).m_GUIDLow);
  123. #ifdef __cplusplus
  124. struct IUnityInterface
  125. {
  126. };
  127. #else
  128. typedef void IUnityInterface;
  129. #endif
  130. typedef struct IUnityInterfaces
  131. {
  132. // Returns an interface matching the guid.
  133. // Returns nullptr if the given interface is unavailable in the active Unity runtime.
  134. IUnityInterface* (UNITY_INTERFACE_API * GetInterface)(UnityInterfaceGUID guid);
  135. // Registers a new interface.
  136. void(UNITY_INTERFACE_API * RegisterInterface)(UnityInterfaceGUID guid, IUnityInterface * ptr);
  137. // Split APIs for C
  138. IUnityInterface* (UNITY_INTERFACE_API * GetInterfaceSplit)(unsigned long long guidHigh, unsigned long long guidLow);
  139. void(UNITY_INTERFACE_API * RegisterInterfaceSplit)(unsigned long long guidHigh, unsigned long long guidLow, IUnityInterface * ptr);
  140. #ifdef __cplusplus
  141. // Helper for GetInterface.
  142. template<typename INTERFACE>
  143. INTERFACE* Get()
  144. {
  145. return static_cast<INTERFACE*>(GetInterface(GetUnityInterfaceGUID<INTERFACE>()));
  146. }
  147. // Helper for RegisterInterface.
  148. template<typename INTERFACE>
  149. void Register(IUnityInterface* ptr)
  150. {
  151. RegisterInterface(GetUnityInterfaceGUID<INTERFACE>(), ptr);
  152. }
  153. #endif
  154. } IUnityInterfaces;
  155. #ifdef __cplusplus
  156. extern "C" {
  157. #endif
  158. // If exported by a plugin, this function will be called when the plugin is loaded.
  159. void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginLoad(IUnityInterfaces* unityInterfaces);
  160. // If exported by a plugin, this function will be called when the plugin is about to be unloaded.
  161. void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginUnload();
  162. #ifdef __cplusplus
  163. }
  164. #endif
  165. struct RenderSurfaceBase;
  166. typedef struct RenderSurfaceBase* UnityRenderBuffer;
  167. typedef unsigned int UnityTextureID;