CIOSUtils.m 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * CIOSUtils.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 "CIOSUtils.h"
  11. #import <Foundation/Foundation.h>
  12. static NSString *standardPathNative(NSSearchPathDirectory directory)
  13. {
  14. return [NSFileManager.defaultManager URLForDirectory:directory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:NULL].path;
  15. }
  16. static const char *standardPath(NSSearchPathDirectory directory) { return standardPathNative(directory).fileSystemRepresentation; }
  17. const char *ios_documentsPath() { return standardPath(NSDocumentDirectory); }
  18. const char *ios_cachesPath() { return standardPath(NSCachesDirectory); }
  19. NSURL *sharedContainerURL()
  20. {
  21. static NSURL *sharedPathURL;
  22. static dispatch_once_t onceToken;
  23. dispatch_once(&onceToken, ^{
  24. __auto_type bundleID = NSBundle.mainBundle.bundleIdentifier;
  25. __auto_type lastDotPos = [bundleID rangeOfString:@"." options:NSBackwardsSearch].location;
  26. __auto_type groupID = [NSString stringWithFormat:@"group.%@.vcmi", [bundleID substringToIndex:lastDotPos]];
  27. sharedPathURL = [NSFileManager.defaultManager containerURLForSecurityApplicationGroupIdentifier:groupID];
  28. });
  29. return sharedPathURL;
  30. }
  31. NSURL *sharedGameDataURL() { return [sharedContainerURL() URLByAppendingPathComponent:@"GameData"]; }
  32. const char *ios_sharedDataPath() { return sharedGameDataURL().fileSystemRepresentation; }
  33. #if TARGET_OS_SIMULATOR
  34. const char *ios_hostApplicationSupportPath()
  35. {
  36. static NSString *applicationSupportPath;
  37. static dispatch_once_t onceToken;
  38. dispatch_once(&onceToken, ^{
  39. __auto_type cachesPath = standardPathNative(NSCachesDirectory);
  40. __auto_type afterMacOsHomeDirPos = [cachesPath rangeOfString:@"Library/Developer"].location;
  41. NSCAssert(afterMacOsHomeDirPos != NSNotFound, @"simulator directory location is not under user's home directory: %@", cachesPath);
  42. applicationSupportPath = [[cachesPath substringToIndex:afterMacOsHomeDirPos] stringByAppendingPathComponent:@"Library/Application Support/vcmi"].stringByResolvingSymlinksInPath;
  43. });
  44. return applicationSupportPath.fileSystemRepresentation;
  45. }
  46. #endif
  47. const char *ios_bundlePath() { return NSBundle.mainBundle.bundlePath.fileSystemRepresentation; }
  48. const char *ios_frameworksPath() { return NSBundle.mainBundle.privateFrameworksPath.fileSystemRepresentation; }
  49. const char *ios_bundleIdentifier() { return NSBundle.mainBundle.bundleIdentifier.UTF8String; }