main.mm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * main_ios.mm, 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 <UIKit/UIKit.h>
  11. #include "../Global.h"
  12. #include "CVCMIServer.h"
  13. #import "../lib/CIOSUtils.h"
  14. @interface ViewController : UIViewController
  15. @property (nonatomic, copy) NSArray<NSURL *> *dataDirsInDocuments;
  16. @end
  17. @implementation ViewController
  18. - (void)viewDidLoad
  19. {
  20. [super viewDidLoad];
  21. auto startServerButton = [UIButton buttonWithType:UIButtonTypeSystem];
  22. [startServerButton setTitle:@"Start Server" forState:UIControlStateNormal];
  23. [startServerButton addTarget:self action:@selector(startServer:) forControlEvents:UIControlEventTouchUpInside];
  24. [self.view addSubview:startServerButton];
  25. startServerButton.translatesAutoresizingMaskIntoConstraints = NO;
  26. [NSLayoutConstraint activateConstraints:@[
  27. [startServerButton.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
  28. [startServerButton.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor],
  29. ]];
  30. auto fm = NSFileManager.defaultManager;
  31. auto sharedGameDataUrl = sharedGameDataURL();
  32. if (!sharedGameDataUrl || [fm fileExistsAtPath:sharedGameDataUrl.path])
  33. return;
  34. auto documentsURL = [fm URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nullptr];
  35. auto dirEnumerator = [fm enumeratorAtURL:documentsURL includingPropertiesForKeys:@[NSURLNameKey] options:NSDirectoryEnumerationSkipsSubdirectoryDescendants errorHandler:nil];
  36. auto dataDirs = [NSMutableArray<NSURL *> arrayWithCapacity:3];
  37. for (NSURL *fileURL in dirEnumerator)
  38. {
  39. NSString *filename;
  40. if (![fileURL getResourceValue:&filename forKey:NSURLNameKey error:nullptr])
  41. continue;
  42. if ([filename caseInsensitiveCompare:@"data"] == NSOrderedSame || [filename caseInsensitiveCompare:@"maps"] == NSOrderedSame || [filename caseInsensitiveCompare:@"mp3"] == NSOrderedSame)
  43. [dataDirs addObject:fileURL];
  44. }
  45. if (dataDirs.count < 3)
  46. return;
  47. self.dataDirsInDocuments = dataDirs;
  48. auto moveDataButton = [UIButton buttonWithType:UIButtonTypeSystem];
  49. [moveDataButton setTitle:@"Move data to shared dir" forState:UIControlStateNormal];
  50. [moveDataButton addTarget:self action:@selector(moveDataToSharedDir:) forControlEvents:UIControlEventTouchUpInside];
  51. [self.view addSubview:moveDataButton];
  52. moveDataButton.translatesAutoresizingMaskIntoConstraints = NO;
  53. [NSLayoutConstraint activateConstraints:@[
  54. [moveDataButton.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
  55. [moveDataButton.topAnchor constraintEqualToAnchor:startServerButton.bottomAnchor constant:10],
  56. ]];
  57. }
  58. - (void)startServer:(UIButton *)button
  59. {
  60. button.enabled = NO;
  61. [NSThread detachNewThreadWithBlock:^{
  62. NSThread.currentThread.name = @"CVCMIServer";
  63. CVCMIServer::create();
  64. dispatch_sync(dispatch_get_main_queue(), ^{
  65. button.enabled = YES;
  66. });
  67. }];
  68. }
  69. - (void)moveDataToSharedDir:(UIButton *)button
  70. {
  71. [button removeFromSuperview];
  72. dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
  73. auto fm = NSFileManager.defaultManager;
  74. auto destinationURL = sharedGameDataURL();
  75. [fm createDirectoryAtURL:destinationURL withIntermediateDirectories:YES attributes:nil error:nullptr];
  76. for (NSURL *dirURL in self.dataDirsInDocuments)
  77. [fm moveItemAtURL:dirURL toURL:[destinationURL URLByAppendingPathComponent:dirURL.lastPathComponent] error:nullptr];
  78. });
  79. }
  80. @end
  81. @interface AppDelegate : UIResponder <UIApplicationDelegate>
  82. @property (nonatomic, strong) UIWindow *window;
  83. @end
  84. @implementation AppDelegate
  85. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  86. {
  87. self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
  88. self.window.rootViewController = [ViewController new];
  89. [self.window makeKeyAndVisible];
  90. return YES;
  91. }
  92. @end
  93. int main(int argc, char * argv[])
  94. {
  95. @autoreleasepool
  96. {
  97. return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
  98. }
  99. }