main.mm 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include "RegisterFeatures.h"
  2. #include <csignal>
  3. #include "UnityInterface.h"
  4. #include "../UnityFramework/UnityFramework.h"
  5. void UnityInitTrampoline();
  6. // WARNING: this MUST be c decl (NSString ctor will be called after +load, so we cant really change its value)
  7. const char* AppControllerClassName = "UnityAppController";
  8. #if UNITY_USES_DYNAMIC_PLAYER_LIB
  9. extern "C" void SetAllUnityFunctionsForDynamicPlayerLib();
  10. #endif
  11. extern "C" void UnitySetExecuteMachHeader(const MachHeader* header);
  12. extern "C" __attribute__((visibility("default"))) NSString* const kUnityDidUnload;
  13. extern "C" __attribute__((visibility("default"))) NSString* const kUnityDidQuit;
  14. @implementation UnityFramework
  15. {
  16. int runCount;
  17. }
  18. UnityFramework* _gUnityFramework = nil;
  19. + (UnityFramework*)getInstance
  20. {
  21. if (_gUnityFramework == nil)
  22. {
  23. _gUnityFramework = [[UnityFramework alloc] init];
  24. }
  25. return _gUnityFramework;
  26. }
  27. - (UnityAppController*)appController
  28. {
  29. return GetAppController();
  30. }
  31. - (void)setExecuteHeader:(const MachHeader*)header
  32. {
  33. UnitySetExecuteMachHeader(header);
  34. }
  35. - (void)sendMessageToGOWithName:(const char*)goName functionName:(const char*)name message:(const char*)msg
  36. {
  37. UnitySendMessage(goName, name, msg);
  38. }
  39. - (void)registerFrameworkListener:(id<UnityFrameworkListener>)obj
  40. {
  41. #define REGISTER_SELECTOR(sel, notif_name) \
  42. if([obj respondsToSelector:sel]) \
  43. [[NSNotificationCenter defaultCenter] addObserver:obj selector:sel name:notif_name object:nil];
  44. REGISTER_SELECTOR(@selector(unityDidUnload:), kUnityDidUnload);
  45. REGISTER_SELECTOR(@selector(unityDidQuit:), kUnityDidQuit);
  46. #undef REGISTER_SELECTOR
  47. }
  48. - (void)unregisterFrameworkListener:(id<UnityFrameworkListener>)obj
  49. {
  50. [[NSNotificationCenter defaultCenter] removeObserver: obj name: kUnityDidUnload object: nil];
  51. [[NSNotificationCenter defaultCenter] removeObserver: obj name: kUnityDidQuit object: nil];
  52. }
  53. - (void)frameworkWarmup:(int)argc argv:(char*[])argv
  54. {
  55. #if UNITY_USES_DYNAMIC_PLAYER_LIB
  56. SetAllUnityFunctionsForDynamicPlayerLib();
  57. #endif
  58. UnityInitTrampoline();
  59. UnityInitRuntime(argc, argv);
  60. RegisterFeatures();
  61. // iOS terminates open sockets when an application enters background mode.
  62. // The next write to any of such socket causes SIGPIPE signal being raised,
  63. // even if the request has been done from scripting side. This disables the
  64. // signal and allows Mono to throw a proper C# exception.
  65. std::signal(SIGPIPE, SIG_IGN);
  66. }
  67. - (void)setDataBundleId:(const char*)bundleId
  68. {
  69. UnitySetDataBundleDirWithBundleId(bundleId);
  70. }
  71. - (void)runUIApplicationMainWithArgc:(int)argc argv:(char*[])argv
  72. {
  73. self->runCount += 1;
  74. [self frameworkWarmup: argc argv: argv];
  75. UIApplicationMain(argc, argv, nil, [NSString stringWithUTF8String: AppControllerClassName]);
  76. }
  77. - (void)runEmbeddedWithArgc:(int)argc argv:(char*[])argv appLaunchOpts:(NSDictionary*)appLaunchOpts
  78. {
  79. if (self->runCount)
  80. {
  81. // initialize from partial unload ( sceneLessMode & onPause )
  82. UnityLoadApplicationFromSceneLessState();
  83. UnitySuppressPauseMessage();
  84. [self pause: false];
  85. [self showUnityWindow];
  86. }
  87. else
  88. {
  89. // full initialization from ground up
  90. [self frameworkWarmup: argc argv: argv];
  91. id app = [UIApplication sharedApplication];
  92. id appCtrl = [[NSClassFromString([NSString stringWithUTF8String: AppControllerClassName]) alloc] init];
  93. [appCtrl application: app didFinishLaunchingWithOptions: appLaunchOpts];
  94. [appCtrl applicationWillEnterForeground: app];
  95. [appCtrl applicationDidBecomeActive: app];
  96. }
  97. self->runCount += 1;
  98. }
  99. - (void)unloadApplication
  100. {
  101. UnityUnloadApplication();
  102. }
  103. - (void)quitApplication:(int)exitCode
  104. {
  105. UnityQuitApplication(exitCode);
  106. }
  107. - (void)showUnityWindow
  108. {
  109. [[[self appController] window] makeKeyAndVisible];
  110. }
  111. - (void)pause:(bool)pause
  112. {
  113. UnityPause(pause);
  114. }
  115. @end
  116. #if TARGET_IPHONE_SIMULATOR && TARGET_TVOS_SIMULATOR
  117. #include <pthread.h>
  118. extern "C" int pthread_cond_init$UNIX2003(pthread_cond_t *cond, const pthread_condattr_t *attr)
  119. { return pthread_cond_init(cond, attr); }
  120. extern "C" int pthread_cond_destroy$UNIX2003(pthread_cond_t *cond)
  121. { return pthread_cond_destroy(cond); }
  122. extern "C" int pthread_cond_wait$UNIX2003(pthread_cond_t *cond, pthread_mutex_t *mutex)
  123. { return pthread_cond_wait(cond, mutex); }
  124. extern "C" int pthread_cond_timedwait$UNIX2003(pthread_cond_t *cond, pthread_mutex_t *mutex,
  125. const struct timespec *abstime)
  126. { return pthread_cond_timedwait(cond, mutex, abstime); }
  127. #endif // TARGET_IPHONE_SIMULATOR && TARGET_TVOS_SIMULATOR