Atlas - SDL_sysfilesystem.m

Home / ext / SDL / src / filesystem / cocoa Lines: 1 | Size: 7299 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 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 34char *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 66char *SDL_SYS_GetPrefPath(const char *org, const char *app) 67{ 68 @autoreleasepool { 69 char *result = NULL; 70 NSArray *array; 71 72#ifndef SDL_PLATFORM_TVOS 73 array = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES); 74#else 75 /* tvOS does not have persistent local storage! 76 * The only place on-device where we can store data is 77 * a cache directory that the OS can empty at any time. 78 * 79 * It's therefore very likely that save data will be erased 80 * between sessions. If you want your app's save data to 81 * actually stick around, you'll need to use iCloud storage. 82 */ 83 { 84 static bool shown = false; 85 if (!shown) { 86 shown = true; 87 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."); 88 } 89 } 90 91 array = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 92#endif // !SDL_PLATFORM_TVOS 93 94 if ([array count] > 0) { // we only want the first item in the list. 95 NSString *str = [array objectAtIndex:0]; 96 const char *base = [str fileSystemRepresentation]; 97 if (base) { 98 const size_t len = SDL_strlen(base) + SDL_strlen(org) + SDL_strlen(app) + 4; 99 result = (char *)SDL_malloc(len); 100 if (result != NULL) { 101 if (*org) { 102 SDL_snprintf(result, len, "%s/%s/%s/", base, org, app); 103 } else { 104 SDL_snprintf(result, len, "%s/%s/", base, app); 105 } 106 for (char *ptr = result + 1; *ptr; ptr++) { 107 if (*ptr == '/') { 108 *ptr = '\0'; 109 mkdir(result, 0700); 110 *ptr = '/'; 111 } 112 } 113 mkdir(result, 0700); 114 } 115 } 116 } 117 118 return result; 119 } 120} 121 122char *SDL_SYS_GetUserFolder(SDL_Folder folder) 123{ 124 @autoreleasepool { 125#ifdef SDL_PLATFORM_TVOS 126 SDL_SetError("tvOS does not have persistent storage"); 127 return NULL; 128#else 129 char *result = NULL; 130 const char *base; 131 NSArray *array; 132 NSSearchPathDirectory dir; 133 NSString *str; 134 char *ptr; 135 136 switch (folder) { 137 case SDL_FOLDER_HOME: 138 base = SDL_getenv("HOME"); 139 140 if (!base) { 141 SDL_SetError("No $HOME environment variable available"); 142 return NULL; 143 } 144 145 goto append_slash; 146 147 case SDL_FOLDER_DESKTOP: 148 dir = NSDesktopDirectory; 149 break; 150 151 case SDL_FOLDER_DOCUMENTS: 152 dir = NSDocumentDirectory; 153 break; 154 155 case SDL_FOLDER_DOWNLOADS: 156 dir = NSDownloadsDirectory; 157 break; 158 159 case SDL_FOLDER_MUSIC: 160 dir = NSMusicDirectory; 161 break; 162 163 case SDL_FOLDER_PICTURES: 164 dir = NSPicturesDirectory; 165 break; 166 167 case SDL_FOLDER_PUBLICSHARE: 168 dir = NSSharedPublicDirectory; 169 break; 170 171 case SDL_FOLDER_SAVEDGAMES: 172 SDL_SetError("Saved games folder not supported on Cocoa"); 173 return NULL; 174 175 case SDL_FOLDER_SCREENSHOTS: 176 SDL_SetError("Screenshots folder not supported on Cocoa"); 177 return NULL; 178 179 case SDL_FOLDER_TEMPLATES: 180 SDL_SetError("Templates folder not supported on Cocoa"); 181 return NULL; 182 183 case SDL_FOLDER_VIDEOS: 184 dir = NSMoviesDirectory; 185 break; 186 187 default: 188 SDL_SetError("Invalid SDL_Folder: %d", (int) folder); 189 return NULL; 190 }; 191 192 array = NSSearchPathForDirectoriesInDomains(dir, NSUserDomainMask, YES); 193 194 if ([array count] <= 0) { 195 SDL_SetError("Directory not found"); 196 return NULL; 197 } 198 199 str = [array objectAtIndex:0]; 200 base = [str fileSystemRepresentation]; 201 if (!base) { 202 SDL_SetError("Couldn't get folder path"); 203 return NULL; 204 } 205 206append_slash: 207 result = SDL_malloc(SDL_strlen(base) + 2); 208 if (result == NULL) { 209 return NULL; 210 } 211 212 if (SDL_snprintf(result, SDL_strlen(base) + 2, "%s/", base) < 0) { 213 SDL_SetError("Couldn't snprintf folder path for Cocoa: %s", base); 214 SDL_free(result); 215 return NULL; 216 } 217 218 for (ptr = result + 1; *ptr; ptr++) { 219 if (*ptr == '/') { 220 *ptr = '\0'; 221 mkdir(result, 0700); 222 *ptr = '/'; 223 } 224 } 225 226 return result; 227#endif // SDL_PLATFORM_TVOS 228 } 229} 230 231#endif // SDL_FILESYSTEM_COCOA 232
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.