main.mm 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. [self pause: false];
  84. [self showUnityWindow];
  85. }
  86. else
  87. {
  88. // full initialization from ground up
  89. [self frameworkWarmup: argc argv: argv];
  90. id app = [UIApplication sharedApplication];
  91. id appCtrl = [[NSClassFromString([NSString stringWithUTF8String: AppControllerClassName]) alloc] init];
  92. [appCtrl application: app didFinishLaunchingWithOptions: appLaunchOpts];
  93. [appCtrl applicationWillEnterForeground: app];
  94. [appCtrl applicationDidBecomeActive: app];
  95. }
  96. self->runCount += 1;
  97. }
  98. - (void)unloadApplication
  99. {
  100. UnityUnloadApplication();
  101. }
  102. - (void)quitApplication:(int)exitCode
  103. {
  104. UnityQuitApplication(exitCode);
  105. }
  106. - (void)showUnityWindow
  107. {
  108. [[[self appController] window] makeKeyAndVisible];
  109. }
  110. - (void)pause:(bool)pause
  111. {
  112. UnityPause(pause);
  113. }
  114. @end
  115. #if TARGET_IPHONE_SIMULATOR && TARGET_TVOS_SIMULATOR
  116. #include <pthread.h>
  117. extern "C" int pthread_cond_init$UNIX2003(pthread_cond_t *cond, const pthread_condattr_t *attr)
  118. { return pthread_cond_init(cond, attr); }
  119. extern "C" int pthread_cond_destroy$UNIX2003(pthread_cond_t *cond)
  120. { return pthread_cond_destroy(cond); }
  121. extern "C" int pthread_cond_wait$UNIX2003(pthread_cond_t *cond, pthread_mutex_t *mutex)
  122. { return pthread_cond_wait(cond, mutex); }
  123. extern "C" int pthread_cond_timedwait$UNIX2003(pthread_cond_t *cond, pthread_mutex_t *mutex,
  124. const struct timespec *abstime)
  125. { return pthread_cond_timedwait(cond, mutex, abstime); }
  126. #endif // TARGET_IPHONE_SIMULATOR && TARGET_TVOS_SIMULATOR