main.m 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * main.m, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #import "startSDL.h"
  11. #import <UIKit/UIKit.h>
  12. #import <objc/runtime.h>
  13. #include <stdlib.h>
  14. static void startSDLManually(int argc, char * argv[])
  15. {
  16. id<UIApplicationDelegate> appDelegate;
  17. @autoreleasepool {
  18. __auto_type sdlAppDelegateClass = NSClassFromString(@"SDLUIKitDelegate");
  19. NSCAssert(sdlAppDelegateClass != nil, @"SDL AppDelegate class doesn't exist");
  20. NSCAssert(class_conformsToProtocol(sdlAppDelegateClass, @protocol(UIApplicationDelegate)), @"SDL AppDelegate doesn't conform to UIApplicationDelegate");
  21. appDelegate = [sdlAppDelegateClass new];
  22. }
  23. UIApplication.sharedApplication.delegate = appDelegate;
  24. int result = startSDL(argc, argv, YES);
  25. exit(result);
  26. }
  27. int qt_main_wrapper(int argc, char * argv[]);
  28. int client_main(int argc, char * argv[])
  29. {
  30. NSInteger launchType;
  31. __auto_type envVar = getenv("VCMI_LAUNCH_TYPE");
  32. if (envVar)
  33. launchType = envVar[0] == '0' ? 0 : 1;
  34. else {
  35. @autoreleasepool {
  36. launchType = [NSUserDefaults.standardUserDefaults integerForKey:@"LaunchType"];
  37. }
  38. }
  39. if (launchType == 1)
  40. return startSDL(argc, argv, NO);
  41. @autoreleasepool {
  42. id __block startGameObserver = [NSNotificationCenter.defaultCenter addObserverForName:@"StartGame" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
  43. [NSNotificationCenter.defaultCenter removeObserver:startGameObserver];
  44. startGameObserver = nil;
  45. NSArray<NSString *> * args = note.userInfo[@"args"];
  46. const char * newArgv[args.count];
  47. NSUInteger i = 0;
  48. for (NSString * obj in args)
  49. newArgv[i++] = obj.UTF8String;
  50. startSDLManually(args.count, (char **)(newArgv));
  51. }];
  52. return qt_main_wrapper(argc, argv);
  53. }
  54. }