Atlas - SDL_sysfilesystem.cpp

Home / ext / SDL / src / filesystem / gdk Lines: 1 | Size: 4266 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/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 24// System dependent filesystem routines 25 26extern "C" { 27#include "../SDL_sysfilesystem.h" 28} 29 30#include "../../core/windows/SDL_windows.h" 31#include <SDL3/SDL_hints.h> 32#include <SDL3/SDL_system.h> 33#include <SDL3/SDL_filesystem.h> 34#include <XGameSaveFiles.h> 35 36char * 37SDL_SYS_GetBasePath(void) 38{ 39 /* NOTE: This function is a UTF8 version of the Win32 SDL_GetBasePath()! 40 * The GDK actually _recommends_ the 'A' functions over the 'W' functions :o 41 */ 42 DWORD buflen = 128; 43 CHAR *path = NULL; 44 DWORD len = 0; 45 int i; 46 47 while (true) { 48 void *ptr = SDL_realloc(path, buflen * sizeof(CHAR)); 49 if (!ptr) { 50 SDL_free(path); 51 return NULL; 52 } 53 54 path = (CHAR *)ptr; 55 56 len = GetModuleFileNameA(NULL, path, buflen); 57 // if it truncated, then len >= buflen - 1 58 // if there was enough room (or failure), len < buflen - 1 59 if (len < buflen - 1) { 60 break; 61 } 62 63 // buffer too small? Try again. 64 buflen *= 2; 65 } 66 67 if (len == 0) { 68 SDL_free(path); 69 WIN_SetError("Couldn't locate our .exe"); 70 return NULL; 71 } 72 73 for (i = len - 1; i > 0; i--) { 74 if (path[i] == '\\') { 75 break; 76 } 77 } 78 79 SDL_assert(i > 0); // Should have been an absolute path. 80 path[i + 1] = '\0'; // chop off filename. 81 82 return path; 83} 84 85char *SDL_SYS_GetPrefPath(const char *org, const char *app) 86{ 87 XUserHandle user = NULL; 88 XAsyncBlock block = { 0 }; 89 char *folderPath; 90 HRESULT result; 91 const char *csid = SDL_GetHint("SDL_GDK_SERVICE_CONFIGURATION_ID"); 92 93 // This should be set before calling SDL_GetPrefPath! 94 if (!csid) { 95 SDL_LogWarn(SDL_LOG_CATEGORY_SYSTEM, "Set SDL_GDK_SERVICE_CONFIGURATION_ID before calling SDL_GetPrefPath!"); 96 return SDL_strdup("T:\\"); 97 } 98 99 if (!SDL_GetGDKDefaultUser(&user)) { 100 // Error already set, just return 101 return NULL; 102 } 103 104 if (FAILED(result = XGameSaveFilesGetFolderWithUiAsync(user, csid, &block))) { 105 WIN_SetErrorFromHRESULT("XGameSaveFilesGetFolderWithUiAsync", result); 106 return NULL; 107 } 108 109 folderPath = (char *)SDL_malloc(MAX_PATH); 110 do { 111 result = XGameSaveFilesGetFolderWithUiResult(&block, MAX_PATH, folderPath); 112 } while (result == E_PENDING); 113 if (FAILED(result)) { 114 WIN_SetErrorFromHRESULT("XGameSaveFilesGetFolderWithUiResult", result); 115 SDL_free(folderPath); 116 return NULL; 117 } 118 119 /* We aren't using 'app' here because the container rules are a lot more 120 * strict than the NTFS rules, so it will most likely be invalid :( 121 */ 122 SDL_strlcat(folderPath, "\\SDLPrefPath\\", MAX_PATH); 123 if (CreateDirectoryA(folderPath, NULL) == FALSE) { 124 if (GetLastError() != ERROR_ALREADY_EXISTS) { 125 WIN_SetError("CreateDirectoryA"); 126 SDL_free(folderPath); 127 return NULL; 128 } 129 } 130 return folderPath; 131} 132 133// TODO 134char *SDL_SYS_GetUserFolder(SDL_Folder folder) 135{ 136 SDL_Unsupported(); 137 return NULL; 138} 139 140char *SDL_SYS_GetCurrentDirectory(void) 141{ 142 const char *base = SDL_GetBasePath(); 143 if (!base) { 144 return NULL; 145 } 146 147 return SDL_strdup(base); 148} 149
[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.