main.mm 4.9 KB

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