mirror of
https://github.com/vcmi/vcmi.git
synced 2024-12-24 22:14:36 +02:00
server: add buttons to start server and to move data to shared directory
kambala-decapitator/vcmi#9 kambala-decapitator/vcmi#15
This commit is contained in:
parent
9b3c2f2a82
commit
e20a6c3670
@ -12,10 +12,108 @@
|
||||
#include "../Global.h"
|
||||
#include "CVCMIServer.h"
|
||||
|
||||
#define SHARED_DATA_DIR @"GameData"
|
||||
|
||||
@interface ViewController : UIViewController
|
||||
@property (nonatomic, copy) NSURL *sharedPathURL;
|
||||
@property (nonatomic, copy) NSArray<NSURL *> *dataDirsInDocuments;
|
||||
@end
|
||||
|
||||
@implementation ViewController
|
||||
|
||||
- (void)viewDidLoad
|
||||
{
|
||||
[super viewDidLoad];
|
||||
|
||||
auto startServerButton = [UIButton buttonWithType:UIButtonTypeSystem];
|
||||
[startServerButton setTitle:@"Start Server" forState:UIControlStateNormal];
|
||||
[startServerButton addTarget:self action:@selector(startServer:) forControlEvents:UIControlEventTouchUpInside];
|
||||
[self.view addSubview:startServerButton];
|
||||
startServerButton.translatesAutoresizingMaskIntoConstraints = NO;
|
||||
[NSLayoutConstraint activateConstraints:@[
|
||||
[startServerButton.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
|
||||
[startServerButton.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor],
|
||||
]];
|
||||
|
||||
auto bundleID = NSBundle.mainBundle.bundleIdentifier;
|
||||
auto lastDotPos = [bundleID rangeOfString:@"." options:NSBackwardsSearch].location;
|
||||
auto groupID = [NSString stringWithFormat:@"group.%@.vcmi", [bundleID substringToIndex:lastDotPos]];
|
||||
auto fm = NSFileManager.defaultManager;
|
||||
self.sharedPathURL = [fm containerURLForSecurityApplicationGroupIdentifier:groupID];
|
||||
if (!self.sharedPathURL)
|
||||
{
|
||||
NSLog(@"shared path for group '%@' not available", groupID);
|
||||
return;
|
||||
}
|
||||
|
||||
auto dirEnumerator = [fm enumeratorAtURL:self.sharedPathURL includingPropertiesForKeys:@[NSURLNameKey] options:NSDirectoryEnumerationSkipsSubdirectoryDescendants errorHandler:nil];
|
||||
for (NSURL *fileURL in dirEnumerator)
|
||||
{
|
||||
NSString *filename;
|
||||
if ([fileURL getResourceValue:&filename forKey:NSURLNameKey error:nullptr] && [filename caseInsensitiveCompare:SHARED_DATA_DIR] == NSOrderedSame) {
|
||||
NSLog(SHARED_DATA_DIR @" dir already exists in the shared path");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
auto documentsURL = [fm URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nullptr];
|
||||
dirEnumerator = [fm enumeratorAtURL:documentsURL includingPropertiesForKeys:@[NSURLNameKey] options:NSDirectoryEnumerationSkipsSubdirectoryDescendants errorHandler:nil];
|
||||
auto dataDirs = [NSMutableArray<NSURL *> arrayWithCapacity:3];
|
||||
for (NSURL *fileURL in dirEnumerator)
|
||||
{
|
||||
NSString *filename;
|
||||
if (![fileURL getResourceValue:&filename forKey:NSURLNameKey error:nullptr])
|
||||
continue;
|
||||
if ([filename caseInsensitiveCompare:@"data"] == NSOrderedSame || [filename caseInsensitiveCompare:@"maps"] == NSOrderedSame || [filename caseInsensitiveCompare:@"mp3"] == NSOrderedSame)
|
||||
[dataDirs addObject:fileURL];
|
||||
}
|
||||
if (dataDirs.count < 3)
|
||||
{
|
||||
NSLog(@"not all required dirs are present, found only: %@", dataDirs);
|
||||
return;
|
||||
}
|
||||
self.dataDirsInDocuments = dataDirs;
|
||||
|
||||
auto moveDataButton = [UIButton buttonWithType:UIButtonTypeSystem];
|
||||
[moveDataButton setTitle:@"Move data to shared dir" forState:UIControlStateNormal];
|
||||
[moveDataButton addTarget:self action:@selector(moveDataToSharedDir:) forControlEvents:UIControlEventTouchUpInside];
|
||||
[self.view addSubview:moveDataButton];
|
||||
|
||||
moveDataButton.translatesAutoresizingMaskIntoConstraints = NO;
|
||||
[NSLayoutConstraint activateConstraints:@[
|
||||
[moveDataButton.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
|
||||
[moveDataButton.topAnchor constraintEqualToAnchor:startServerButton.bottomAnchor constant:10],
|
||||
]];
|
||||
}
|
||||
|
||||
- (void)startServer:(UIButton *)button
|
||||
{
|
||||
button.enabled = NO;
|
||||
[NSThread detachNewThreadWithBlock:^{
|
||||
NSThread.currentThread.name = @"CVCMIServer";
|
||||
CVCMIServer::create();
|
||||
dispatch_sync(dispatch_get_main_queue(), ^{
|
||||
button.enabled = YES;
|
||||
});
|
||||
}];
|
||||
}
|
||||
|
||||
- (void)moveDataToSharedDir:(UIButton *)button
|
||||
{
|
||||
button.enabled = NO;
|
||||
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
|
||||
auto fm = NSFileManager.defaultManager;
|
||||
auto destinationURL = [self.sharedPathURL URLByAppendingPathComponent:SHARED_DATA_DIR];
|
||||
[fm createDirectoryAtURL:destinationURL withIntermediateDirectories:YES attributes:nil error:nullptr];
|
||||
for (NSURL *dirURL in self.dataDirsInDocuments)
|
||||
[fm moveItemAtURL:dirURL toURL:[destinationURL URLByAppendingPathComponent:dirURL.lastPathComponent] error:nullptr];
|
||||
|
||||
dispatch_sync(dispatch_get_main_queue(), ^{
|
||||
[button removeFromSuperview];
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@ -31,12 +129,6 @@
|
||||
self.window.rootViewController = [ViewController new];
|
||||
[self.window makeKeyAndVisible];
|
||||
|
||||
[NSThread detachNewThreadWithBlock:^
|
||||
{
|
||||
NSThread.currentThread.name = @"CVCMIServer";
|
||||
NSLog(@"starting server from thread %@", NSThread.currentThread);
|
||||
CVCMIServer::create();
|
||||
}];
|
||||
return YES;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user