Simple Directmedia Layer
at main 150 lines 4.3 kB view raw
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2024 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/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 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 if (!app) { 94 SDL_InvalidParamError("app"); 95 return NULL; 96 } 97 98 // This should be set before calling SDL_GetPrefPath! 99 if (!csid) { 100 SDL_LogWarn(SDL_LOG_CATEGORY_SYSTEM, "Set SDL_GDK_SERVICE_CONFIGURATION_ID before calling SDL_GetPrefPath!"); 101 return SDL_strdup("T:\\"); 102 } 103 104 if (!SDL_GetGDKDefaultUser(&user)) { 105 // Error already set, just return 106 return NULL; 107 } 108 109 if (FAILED(result = XGameSaveFilesGetFolderWithUiAsync(user, csid, &block))) { 110 WIN_SetErrorFromHRESULT("XGameSaveFilesGetFolderWithUiAsync", result); 111 return NULL; 112 } 113 114 folderPath = (char*) SDL_malloc(MAX_PATH); 115 do { 116 result = XGameSaveFilesGetFolderWithUiResult(&block, MAX_PATH, folderPath); 117 } while (result == E_PENDING); 118 if (FAILED(result)) { 119 WIN_SetErrorFromHRESULT("XGameSaveFilesGetFolderWithUiResult", result); 120 SDL_free(folderPath); 121 return NULL; 122 } 123 124 /* We aren't using 'app' here because the container rules are a lot more 125 * strict than the NTFS rules, so it will most likely be invalid :( 126 */ 127 SDL_strlcat(folderPath, "\\SDLPrefPath\\", MAX_PATH); 128 if (CreateDirectoryA(folderPath, NULL) == FALSE) { 129 if (GetLastError() != ERROR_ALREADY_EXISTS) { 130 WIN_SetError("CreateDirectoryA"); 131 SDL_free(folderPath); 132 return NULL; 133 } 134 } 135 return folderPath; 136} 137 138// TODO 139char *SDL_SYS_GetUserFolder(SDL_Folder folder) 140{ 141 SDL_Unsupported(); 142 return NULL; 143} 144 145// TODO 146char *SDL_SYS_GetCurrentDirectory(void) 147{ 148 SDL_Unsupported(); 149 return NULL; 150}