diff options
Diffstat (limited to 'contrib/SDL-3.2.8/src/filesystem/cocoa/SDL_sysfilesystem.m')
| -rw-r--r-- | contrib/SDL-3.2.8/src/filesystem/cocoa/SDL_sysfilesystem.m | 240 |
1 files changed, 240 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/filesystem/cocoa/SDL_sysfilesystem.m b/contrib/SDL-3.2.8/src/filesystem/cocoa/SDL_sysfilesystem.m new file mode 100644 index 0000000..5818764 --- /dev/null +++ b/contrib/SDL-3.2.8/src/filesystem/cocoa/SDL_sysfilesystem.m | |||
| @@ -0,0 +1,240 @@ | |||
| 1 | /* | ||
| 2 | Simple DirectMedia Layer | ||
| 3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> | ||
| 4 | |||
| 5 | This software is provided 'as-is', without any express or implied | ||
| 6 | warranty. In no event will the authors be held liable for any damages | ||
| 7 | arising from the use of this software. | ||
| 8 | |||
| 9 | Permission is granted to anyone to use this software for any purpose, | ||
| 10 | including commercial applications, and to alter it and redistribute it | ||
| 11 | freely, subject to the following restrictions: | ||
| 12 | |||
| 13 | 1. The origin of this software must not be misrepresented; you must not | ||
| 14 | claim that you wrote the original software. If you use this software | ||
| 15 | in a product, an acknowledgment in the product documentation would be | ||
| 16 | appreciated but is not required. | ||
| 17 | 2. Altered source versions must be plainly marked as such, and must not be | ||
| 18 | misrepresented as being the original software. | ||
| 19 | 3. This notice may not be removed or altered from any source distribution. | ||
| 20 | */ | ||
| 21 | #include "SDL_internal.h" | ||
| 22 | |||
| 23 | #ifdef SDL_FILESYSTEM_COCOA | ||
| 24 | |||
| 25 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | ||
| 26 | // System dependent filesystem routines | ||
| 27 | |||
| 28 | #include "../SDL_sysfilesystem.h" | ||
| 29 | |||
| 30 | #include <Foundation/Foundation.h> | ||
| 31 | #include <sys/stat.h> | ||
| 32 | #include <sys/types.h> | ||
| 33 | |||
| 34 | char *SDL_SYS_GetBasePath(void) | ||
| 35 | { | ||
| 36 | @autoreleasepool { | ||
| 37 | NSBundle *bundle = [NSBundle mainBundle]; | ||
| 38 | const char *baseType = [[[bundle infoDictionary] objectForKey:@"SDL_FILESYSTEM_BASE_DIR_TYPE"] UTF8String]; | ||
| 39 | const char *base = NULL; | ||
| 40 | char *result = NULL; | ||
| 41 | |||
| 42 | if (baseType == NULL) { | ||
| 43 | baseType = "resource"; | ||
| 44 | } | ||
| 45 | if (SDL_strcasecmp(baseType, "bundle") == 0) { | ||
| 46 | base = [[bundle bundlePath] fileSystemRepresentation]; | ||
| 47 | } else if (SDL_strcasecmp(baseType, "parent") == 0) { | ||
| 48 | base = [[[bundle bundlePath] stringByDeletingLastPathComponent] fileSystemRepresentation]; | ||
| 49 | } else { | ||
| 50 | // this returns the exedir for non-bundled and the resourceDir for bundled apps | ||
| 51 | base = [[bundle resourcePath] fileSystemRepresentation]; | ||
| 52 | } | ||
| 53 | |||
| 54 | if (base) { | ||
| 55 | const size_t len = SDL_strlen(base) + 2; | ||
| 56 | result = (char *)SDL_malloc(len); | ||
| 57 | if (result != NULL) { | ||
| 58 | SDL_snprintf(result, len, "%s/", base); | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | return result; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | char *SDL_SYS_GetPrefPath(const char *org, const char *app) | ||
| 67 | { | ||
| 68 | @autoreleasepool { | ||
| 69 | char *result = NULL; | ||
| 70 | NSArray *array; | ||
| 71 | |||
| 72 | if (!app) { | ||
| 73 | SDL_InvalidParamError("app"); | ||
| 74 | return NULL; | ||
| 75 | } | ||
| 76 | if (!org) { | ||
| 77 | org = ""; | ||
| 78 | } | ||
| 79 | |||
| 80 | #ifndef SDL_PLATFORM_TVOS | ||
| 81 | array = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES); | ||
| 82 | #else | ||
| 83 | /* tvOS does not have persistent local storage! | ||
| 84 | * The only place on-device where we can store data is | ||
| 85 | * a cache directory that the OS can empty at any time. | ||
| 86 | * | ||
| 87 | * It's therefore very likely that save data will be erased | ||
| 88 | * between sessions. If you want your app's save data to | ||
| 89 | * actually stick around, you'll need to use iCloud storage. | ||
| 90 | */ | ||
| 91 | { | ||
| 92 | static bool shown = false; | ||
| 93 | if (!shown) { | ||
| 94 | shown = true; | ||
| 95 | SDL_LogCritical(SDL_LOG_CATEGORY_SYSTEM, "tvOS does not have persistent local storage! Use iCloud storage if you want your data to persist between sessions."); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | array = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); | ||
| 100 | #endif // !SDL_PLATFORM_TVOS | ||
| 101 | |||
| 102 | if ([array count] > 0) { // we only want the first item in the list. | ||
| 103 | NSString *str = [array objectAtIndex:0]; | ||
| 104 | const char *base = [str fileSystemRepresentation]; | ||
| 105 | if (base) { | ||
| 106 | const size_t len = SDL_strlen(base) + SDL_strlen(org) + SDL_strlen(app) + 4; | ||
| 107 | result = (char *)SDL_malloc(len); | ||
| 108 | if (result != NULL) { | ||
| 109 | char *ptr; | ||
| 110 | if (*org) { | ||
| 111 | SDL_snprintf(result, len, "%s/%s/%s/", base, org, app); | ||
| 112 | } else { | ||
| 113 | SDL_snprintf(result, len, "%s/%s/", base, app); | ||
| 114 | } | ||
| 115 | for (ptr = result + 1; *ptr; ptr++) { | ||
| 116 | if (*ptr == '/') { | ||
| 117 | *ptr = '\0'; | ||
| 118 | mkdir(result, 0700); | ||
| 119 | *ptr = '/'; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | mkdir(result, 0700); | ||
| 123 | } | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | return result; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | char *SDL_SYS_GetUserFolder(SDL_Folder folder) | ||
| 132 | { | ||
| 133 | @autoreleasepool { | ||
| 134 | #ifdef SDL_PLATFORM_TVOS | ||
| 135 | SDL_SetError("tvOS does not have persistent storage"); | ||
| 136 | return NULL; | ||
| 137 | #else | ||
| 138 | char *result = NULL; | ||
| 139 | const char* base; | ||
| 140 | NSArray *array; | ||
| 141 | NSSearchPathDirectory dir; | ||
| 142 | NSString *str; | ||
| 143 | char *ptr; | ||
| 144 | |||
| 145 | switch (folder) { | ||
| 146 | case SDL_FOLDER_HOME: | ||
| 147 | base = SDL_getenv("HOME"); | ||
| 148 | |||
| 149 | if (!base) { | ||
| 150 | SDL_SetError("No $HOME environment variable available"); | ||
| 151 | return NULL; | ||
| 152 | } | ||
| 153 | |||
| 154 | goto append_slash; | ||
| 155 | |||
| 156 | case SDL_FOLDER_DESKTOP: | ||
| 157 | dir = NSDesktopDirectory; | ||
| 158 | break; | ||
| 159 | |||
| 160 | case SDL_FOLDER_DOCUMENTS: | ||
| 161 | dir = NSDocumentDirectory; | ||
| 162 | break; | ||
| 163 | |||
| 164 | case SDL_FOLDER_DOWNLOADS: | ||
| 165 | dir = NSDownloadsDirectory; | ||
| 166 | break; | ||
| 167 | |||
| 168 | case SDL_FOLDER_MUSIC: | ||
| 169 | dir = NSMusicDirectory; | ||
| 170 | break; | ||
| 171 | |||
| 172 | case SDL_FOLDER_PICTURES: | ||
| 173 | dir = NSPicturesDirectory; | ||
| 174 | break; | ||
| 175 | |||
| 176 | case SDL_FOLDER_PUBLICSHARE: | ||
| 177 | dir = NSSharedPublicDirectory; | ||
| 178 | break; | ||
| 179 | |||
| 180 | case SDL_FOLDER_SAVEDGAMES: | ||
| 181 | SDL_SetError("Saved games folder not supported on Cocoa"); | ||
| 182 | return NULL; | ||
| 183 | |||
| 184 | case SDL_FOLDER_SCREENSHOTS: | ||
| 185 | SDL_SetError("Screenshots folder not supported on Cocoa"); | ||
| 186 | return NULL; | ||
| 187 | |||
| 188 | case SDL_FOLDER_TEMPLATES: | ||
| 189 | SDL_SetError("Templates folder not supported on Cocoa"); | ||
| 190 | return NULL; | ||
| 191 | |||
| 192 | case SDL_FOLDER_VIDEOS: | ||
| 193 | dir = NSMoviesDirectory; | ||
| 194 | break; | ||
| 195 | |||
| 196 | default: | ||
| 197 | SDL_SetError("Invalid SDL_Folder: %d", (int) folder); | ||
| 198 | return NULL; | ||
| 199 | }; | ||
| 200 | |||
| 201 | array = NSSearchPathForDirectoriesInDomains(dir, NSUserDomainMask, YES); | ||
| 202 | |||
| 203 | if ([array count] <= 0) { | ||
| 204 | SDL_SetError("Directory not found"); | ||
| 205 | return NULL; | ||
| 206 | } | ||
| 207 | |||
| 208 | str = [array objectAtIndex:0]; | ||
| 209 | base = [str fileSystemRepresentation]; | ||
| 210 | if (!base) { | ||
| 211 | SDL_SetError("Couldn't get folder path"); | ||
| 212 | return NULL; | ||
| 213 | } | ||
| 214 | |||
| 215 | append_slash: | ||
| 216 | result = SDL_malloc(SDL_strlen(base) + 2); | ||
| 217 | if (result == NULL) { | ||
| 218 | return NULL; | ||
| 219 | } | ||
| 220 | |||
| 221 | if (SDL_snprintf(result, SDL_strlen(base) + 2, "%s/", base) < 0) { | ||
| 222 | SDL_SetError("Couldn't snprintf folder path for Cocoa: %s", base); | ||
| 223 | SDL_free(result); | ||
| 224 | return NULL; | ||
| 225 | } | ||
| 226 | |||
| 227 | for (ptr = result + 1; *ptr; ptr++) { | ||
| 228 | if (*ptr == '/') { | ||
| 229 | *ptr = '\0'; | ||
| 230 | mkdir(result, 0700); | ||
| 231 | *ptr = '/'; | ||
| 232 | } | ||
| 233 | } | ||
| 234 | |||
| 235 | return result; | ||
| 236 | #endif // SDL_PLATFORM_TVOS | ||
| 237 | } | ||
| 238 | } | ||
| 239 | |||
| 240 | #endif // SDL_FILESYSTEM_COCOA | ||
