IUnityInterface.h 8.8 KB

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