DynamicLibEngineAPI.mm 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* SINGLE CPP FILE TO GENERATE SEAMLESS BRIDGE BETWEEN BINARIES < SHARED ENGINE LIBRARY WITH ABSTRACT EXTERN FUNCTIONS> | < PLAYER EXECUTABLE WITH ABSTRACT FUNCTION IMPLEMENTATION >
  2. 1. if building shared engine library this file will:
  3. define body for Unity* methods that proxy call to actual method
  4. actual method will be set later from outside with respective call to SetUnity*Body
  5. defines SetUnity*Body method to set actual method for call, theese functions are exported from library
  6. 2. if building player against shared engine library this file will:
  7. calls SetUnity*Body providing actual method to be called by shared engine library later
  8. wraps all SetUnity*Body calls in one single method SetAllUnityFunctionsForDynamicPlayerLib
  9. - notes:
  10. file will be included only if development / il2ccp and:
  11. - for xcode project if BuildSettings.UseDynamicPlayerLib is true
  12. - for player if (build.pl staticLib=1, jam BUILD_IOS_DYNAMIC_PLAYER=1)
  13. DynamicLibEngineAPI-functions.h include list of functions to proxy calls from player to trampoline
  14. - each function inlist is defined with UnityExternCall or UnityExternCall4StaticMember
  15. */
  16. // deal with __VA_ARGS__ to convert them to formated lists with provided M macro
  17. #define VA_ARGS_COUNT(...) INTERNAL_GET_ARG_COUNT_PRIVATE(0, ## __VA_ARGS__, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
  18. #define INTERNAL_GET_ARG_COUNT_PRIVATE(_0, _1_, _2_, _3_, _4_, _5_, _6_, _7_, _8_, _9_, _10_, _11_, _12_, _13_, _14_, _15_, _16_, _17_, _18_, _19_, _20_, count, ...) count
  19. #define JOIN_VA_ARGS_0(M, ...)
  20. #define JOIN_VA_ARGS_1(M, T1) M(T1,1)
  21. #define JOIN_VA_ARGS_2(M, T1, T2) M(T1,1), M(T2,2)
  22. #define JOIN_VA_ARGS_3(M, T1, T2, T3) M(T1,1), M(T2,2), M(T3,3)
  23. #define JOIN_VA_ARGS_4(M, T1, T2, T3, T4) M(T1,1), M(T2,2), M(T3,3), M(T4,4)
  24. #define JOIN_VA_ARGS_5(M, T1, T2, T3, T4, T5) M(T1,1), M(T2,2), M(T3,3), M(T4,4), M(T5,5)
  25. #define JOIN_VA_ARGS_6(M, T1, T2, T3, T4, T5, T6) M(T1,1), M(T2,2), M(T3,3), M(T4,4), M(T5,5), M(T6,6)
  26. #define JOIN_VA_ARGS_7(M, T1, T2, T3, T4, T5, T6, T7) M(T1,1), M(T2,2), M(T3,3), M(T4,4), M(T5,5), M(T6,6), M(T7,7)
  27. #define JOIN_VA_ARGS_8(M, T1, T2, T3, T4, T5, T6, T7, T8) M(T1,1), M(T2,2), M(T3,3), M(T4,4), M(T5,5), M(T6,6), M(T7,7), M(T8,8)
  28. #define JOIN_VA_ARGS_9(M, T1, T2, T3, T4, T5, T6, T7, T8, T9) M(T1,1), M(T2,2), M(T3,3), M(T4,4), M(T5,5), M(T6,6), M(T7,7), M(T8,8), M(T9,9)
  29. #define JOIN_VA_ARGS___(M, N, ...) JOIN_VA_ARGS_##N(M, __VA_ARGS__ )
  30. #define JOIN_VA_ARGS__(M, N, ...) JOIN_VA_ARGS___(M,N,__VA_ARGS__)
  31. #define JOIN_VA_ARGS_(M, ...) JOIN_VA_ARGS__(M,VA_ARGS_COUNT(__VA_ARGS__), __VA_ARGS__)
  32. #define JOIN_VA_ARGS(M, ...) JOIN_VA_ARGS_(M,__VA_ARGS__)
  33. // convert to function definition params:
  34. // egz: VA_ARGS_TO_PARAMS(int, char, bool) expands to: int p3, char p2, bool p1
  35. #define VA_JOIN_AS_PARAMS(type, index) type p##index
  36. #define VA_ARGS_TO_PARAMS(...) JOIN_VA_ARGS(VA_JOIN_AS_PARAMS,__VA_ARGS__)
  37. // convert to function call params
  38. // egz: VA_ARGS_TO_CALL(int,char,bool) exapnds to: p3, p2, p1
  39. #define VA_JOIN_AS_CALL(type, index) p##index
  40. #define VA_ARGS_TO_CALL(...) JOIN_VA_ARGS(VA_JOIN_AS_CALL,__VA_ARGS__)
  41. #ifndef UNITY_ENGINE_DYNAMICLIB_MODE
  42. #define UNITY_ENGINE_DYNAMICLIB_MODE 0
  43. #endif
  44. #if UNITY_ENGINE_DYNAMICLIB_MODE
  45. // [ part of Unity Player ]
  46. // this part generates Unity* functions that act as proxy to call actual function from trampoline
  47. // for each function in DynamicLibEngineAPI-functions.h will be generated proxy function
  48. // proxy for extern "C" function
  49. // egz: UnityExternCall(int, UnityTestFunctionName, int);
  50. // will expand to:
  51. // static int(*gPtrUnityTestFunctionName)(int) = nullptr;
  52. // extern "C" int UnityTestFunctionName(int p1) {
  53. // assert(gPtrUnityTestFunctionName) != nullptr);
  54. // return gPtrUnityTestFunctionName(p1);
  55. // }
  56. // __attribute__((visibility("default")))
  57. // extern "C" void SetUnityTestFunctionNameBody(decltype(&UnityTestFunctionName) fPtr) {
  58. // gPtrUnityTestFunctionName = fPtr;
  59. // }
  60. #define UnityExternCall(returnType, funcName, ...) \
  61. static returnType(*gPtr##funcName)(__VA_ARGS__) = nullptr; \
  62. extern "C" returnType funcName(VA_ARGS_TO_PARAMS(__VA_ARGS__)) {\
  63. assert(gPtr##funcName != nullptr); \
  64. return gPtr##funcName(VA_ARGS_TO_CALL(__VA_ARGS__)); \
  65. } \
  66. __attribute__((visibility("default"))) \
  67. extern "C" void Set##funcName##Body(decltype(&funcName) fPtr) { \
  68. gPtr##funcName = fPtr; \
  69. }
  70. // proxy for class static methods
  71. // egz: UnityExternCall4StaticMember(int, MyClass MyMethod, int);
  72. // will expand to:
  73. // static int(*gPtrMyClassMyMethod)(int) = nullptr;
  74. // int MyClass::MyMethod(int p1) {
  75. // assert(gPtrMyClassMyMethod) != nullptr);
  76. // return gPtrMyClassMyMethod(p1);
  77. // }
  78. // __attribute__((visibility("default")))
  79. // extern "C" void SetMyClassMyMethodBody(decltype(gPtrMyClassMyMethod) fPtr) {
  80. // gPtrMyClassMyMethod = fPtr;
  81. // }
  82. #define UnityExternCall4StaticMember(returnType, className, funcName, ...) \
  83. static returnType(*gPtr##className##funcName)(__VA_ARGS__) = nullptr; \
  84. returnType className::funcName(VA_ARGS_TO_PARAMS(__VA_ARGS__)) { \
  85. assert(gPtr##className##funcName != nullptr); \
  86. return gPtr##className##funcName(VA_ARGS_TO_CALL(__VA_ARGS__)); \
  87. } \
  88. __attribute__((visibility("default"))) \
  89. extern "C" void Set##className##funcName##Body(decltype(gPtr##className##funcName) fPtr) { \
  90. gPtr##className##funcName = fPtr; \
  91. }
  92. #include "PlatformDependent/iPhonePlayer/Trampoline/Classes/Unity/UnitySharedDecls.h"
  93. #include "PlatformDependent/iPhonePlayer/Trampoline/Classes/Unity/UnityRendering.h"
  94. #include "PlatformDependent/iPhonePlayer/TrampolineInterface.h"
  95. #include "Runtime/Graphics/DisplayManager.h"
  96. #include "Runtime/Input/LocationService.h"
  97. #import <UIKit/UIKit.h>
  98. #include "External/baselib/builds/Include/PreExternalInclude.h"
  99. #include <mach-o/ldsyms.h>
  100. #include "External/baselib/builds/Include/PostExternalInclude.h"
  101. #include "DynamicLibEngineAPI-functions.h"
  102. #undef UnityExternCall
  103. #undef UnityExternCall4StaticMember
  104. #else
  105. // [ part of Xcode project ]
  106. // for each function defined in DynamicLibEngineAPI-functions.h will be generated SetUnity*Body function
  107. // for extern "C" functions
  108. // egz: UnityExternCall(int, UnityTestFunctionName, int);
  109. // will expand to:
  110. // extern "C" UnityTestFunctionName(int);
  111. // extern "C" SetUnityTestFunctionName(decltype(&UnityTestFunctionName));
  112. #define UnityExternCall(returnType, funcName, ...) \
  113. extern "C" returnType funcName(__VA_ARGS__); \
  114. extern "C" void Set##funcName##Body(decltype(&funcName));
  115. // for class static method
  116. // egz: UnityExternCall4StaticMember(int, MyClass MyMethod, int);
  117. // will expand to:
  118. // extern "C" void SetMyClassMyMethodBody(decltype(&MyClass::MyMethod));
  119. #define UnityExternCall4StaticMember(returnType, className, funcName, ...) \
  120. extern "C" void Set##className##funcName##Body(decltype(&className::funcName));
  121. #include "UnityRendering.h"
  122. #include "Classes/iPhone_Sensors.h"
  123. #include "UndefinePlatforms.h"
  124. #include <mach-o/ldsyms.h>
  125. #include "RedefinePlatforms.h"
  126. #include "DynamicLibEngineAPI-functions.h"
  127. #undef UnityExternCall
  128. #undef UnityExternCall4StaticMember
  129. // single function to call every Set*Body function from DynamicLibEngineAPI-functions.h
  130. #define UnityExternCall(returnType, funcName, ...) Set##funcName##Body(funcName);
  131. #define UnityExternCall4StaticMember(returnType, className, funcName, ...) Set##className##funcName##Body(className::funcName)
  132. extern "C" void SetAllUnityFunctionsForDynamicPlayerLib()
  133. {
  134. #include "DynamicLibEngineAPI-functions.h"
  135. }
  136. #undef UnityExternCall
  137. #undef UnityExternCall4StaticMember
  138. #endif