From 5a079a2d114f96d4847d1ee305d5b7c16eeec50e Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 27 Dec 2025 12:03:39 -0800 Subject: Initial commit --- .../src/filesystem/windows/SDL_sysfilesystem.c | 382 +++++++++++++++++++++ .../src/filesystem/windows/SDL_sysfsops.c | 231 +++++++++++++ 2 files changed, 613 insertions(+) create mode 100644 contrib/SDL-3.2.8/src/filesystem/windows/SDL_sysfilesystem.c create mode 100644 contrib/SDL-3.2.8/src/filesystem/windows/SDL_sysfsops.c (limited to 'contrib/SDL-3.2.8/src/filesystem/windows') diff --git a/contrib/SDL-3.2.8/src/filesystem/windows/SDL_sysfilesystem.c b/contrib/SDL-3.2.8/src/filesystem/windows/SDL_sysfilesystem.c new file mode 100644 index 0000000..39ba414 --- /dev/null +++ b/contrib/SDL-3.2.8/src/filesystem/windows/SDL_sysfilesystem.c @@ -0,0 +1,382 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "SDL_internal.h" + +#ifdef SDL_FILESYSTEM_WINDOWS + +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ +// System dependent filesystem routines + +#include "../SDL_sysfilesystem.h" + +#include "../../core/windows/SDL_windows.h" +#include +#include + +// These aren't all defined in older SDKs, so define them here +DEFINE_GUID(SDL_FOLDERID_Profile, 0x5E6C858F, 0x0E22, 0x4760, 0x9A, 0xFE, 0xEA, 0x33, 0x17, 0xB6, 0x71, 0x73); +DEFINE_GUID(SDL_FOLDERID_Desktop, 0xB4BFCC3A, 0xDB2C, 0x424C, 0xB0, 0x29, 0x7F, 0xE9, 0x9A, 0x87, 0xC6, 0x41); +DEFINE_GUID(SDL_FOLDERID_Documents, 0xFDD39AD0, 0x238F, 0x46AF, 0xAD, 0xB4, 0x6C, 0x85, 0x48, 0x03, 0x69, 0xC7); +DEFINE_GUID(SDL_FOLDERID_Downloads, 0x374de290, 0x123f, 0x4565, 0x91, 0x64, 0x39, 0xc4, 0x92, 0x5e, 0x46, 0x7b); +DEFINE_GUID(SDL_FOLDERID_Music, 0x4BD8D571, 0x6D19, 0x48D3, 0xBE, 0x97, 0x42, 0x22, 0x20, 0x08, 0x0E, 0x43); +DEFINE_GUID(SDL_FOLDERID_Pictures, 0x33E28130, 0x4E1E, 0x4676, 0x83, 0x5A, 0x98, 0x39, 0x5C, 0x3B, 0xC3, 0xBB); +DEFINE_GUID(SDL_FOLDERID_SavedGames, 0x4c5c32ff, 0xbb9d, 0x43b0, 0xb5, 0xb4, 0x2d, 0x72, 0xe5, 0x4e, 0xaa, 0xa4); +DEFINE_GUID(SDL_FOLDERID_Screenshots, 0xb7bede81, 0xdf94, 0x4682, 0xa7, 0xd8, 0x57, 0xa5, 0x26, 0x20, 0xb8, 0x6f); +DEFINE_GUID(SDL_FOLDERID_Templates, 0xA63293E8, 0x664E, 0x48DB, 0xA0, 0x79, 0xDF, 0x75, 0x9E, 0x05, 0x09, 0xF7); +DEFINE_GUID(SDL_FOLDERID_Videos, 0x18989B1D, 0x99B5, 0x455B, 0x84, 0x1C, 0xAB, 0x7C, 0x74, 0xE4, 0xDD, 0xFC); + +char *SDL_SYS_GetBasePath(void) +{ + DWORD buflen = 128; + WCHAR *path = NULL; + char *result = NULL; + DWORD len = 0; + int i; + + while (true) { + void *ptr = SDL_realloc(path, buflen * sizeof(WCHAR)); + if (!ptr) { + SDL_free(path); + return NULL; + } + + path = (WCHAR *)ptr; + + len = GetModuleFileNameW(NULL, path, buflen); + // if it truncated, then len >= buflen - 1 + // if there was enough room (or failure), len < buflen - 1 + if (len < buflen - 1) { + break; + } + + // buffer too small? Try again. + buflen *= 2; + } + + if (len == 0) { + SDL_free(path); + WIN_SetError("Couldn't locate our .exe"); + return NULL; + } + + for (i = len - 1; i > 0; i--) { + if (path[i] == '\\') { + break; + } + } + + SDL_assert(i > 0); // Should have been an absolute path. + path[i + 1] = '\0'; // chop off filename. + + result = WIN_StringToUTF8W(path); + SDL_free(path); + + return result; +} + +char *SDL_SYS_GetPrefPath(const char *org, const char *app) +{ + /* + * Vista and later has a new API for this, but SHGetFolderPath works there, + * and apparently just wraps the new API. This is the new way to do it: + * + * SHGetKnownFolderPath(SDL_FOLDERID_RoamingAppData, KF_FLAG_CREATE, + * NULL, &wszPath); + */ + + HRESULT hr = E_FAIL; + WCHAR path[MAX_PATH]; + char *result = NULL; + WCHAR *worg = NULL; + WCHAR *wapp = NULL; + size_t new_wpath_len = 0; + BOOL api_result = FALSE; + + if (!app) { + SDL_InvalidParamError("app"); + return NULL; + } + if (!org) { + org = ""; + } + + hr = SHGetFolderPathW(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, path); + if (!SUCCEEDED(hr)) { + WIN_SetErrorFromHRESULT("Couldn't locate our prefpath", hr); + return NULL; + } + + worg = WIN_UTF8ToStringW(org); + if (!worg) { + return NULL; + } + + wapp = WIN_UTF8ToStringW(app); + if (!wapp) { + SDL_free(worg); + return NULL; + } + + new_wpath_len = SDL_wcslen(worg) + SDL_wcslen(wapp) + SDL_wcslen(path) + 3; + + if ((new_wpath_len + 1) > MAX_PATH) { + SDL_free(worg); + SDL_free(wapp); + WIN_SetError("Path too long."); + return NULL; + } + + if (*worg) { + SDL_wcslcat(path, L"\\", SDL_arraysize(path)); + SDL_wcslcat(path, worg, SDL_arraysize(path)); + } + SDL_free(worg); + + api_result = CreateDirectoryW(path, NULL); + if (api_result == FALSE) { + if (GetLastError() != ERROR_ALREADY_EXISTS) { + SDL_free(wapp); + WIN_SetError("Couldn't create a prefpath."); + return NULL; + } + } + + SDL_wcslcat(path, L"\\", SDL_arraysize(path)); + SDL_wcslcat(path, wapp, SDL_arraysize(path)); + SDL_free(wapp); + + api_result = CreateDirectoryW(path, NULL); + if (api_result == FALSE) { + if (GetLastError() != ERROR_ALREADY_EXISTS) { + WIN_SetError("Couldn't create a prefpath."); + return NULL; + } + } + + SDL_wcslcat(path, L"\\", SDL_arraysize(path)); + + result = WIN_StringToUTF8W(path); + + return result; +} + +char *SDL_SYS_GetUserFolder(SDL_Folder folder) +{ + typedef HRESULT (WINAPI *pfnSHGetKnownFolderPath)(REFGUID /* REFKNOWNFOLDERID */, DWORD, HANDLE, PWSTR*); + HMODULE lib = LoadLibrary(L"Shell32.dll"); + pfnSHGetKnownFolderPath pSHGetKnownFolderPath = NULL; + char *result = NULL; + + if (lib) { + pSHGetKnownFolderPath = (pfnSHGetKnownFolderPath)GetProcAddress(lib, "SHGetKnownFolderPath"); + } + + if (pSHGetKnownFolderPath) { + GUID type; // KNOWNFOLDERID + HRESULT hr; + wchar_t *path; + + switch (folder) { + case SDL_FOLDER_HOME: + type = SDL_FOLDERID_Profile; + break; + + case SDL_FOLDER_DESKTOP: + type = SDL_FOLDERID_Desktop; + break; + + case SDL_FOLDER_DOCUMENTS: + type = SDL_FOLDERID_Documents; + break; + + case SDL_FOLDER_DOWNLOADS: + type = SDL_FOLDERID_Downloads; + break; + + case SDL_FOLDER_MUSIC: + type = SDL_FOLDERID_Music; + break; + + case SDL_FOLDER_PICTURES: + type = SDL_FOLDERID_Pictures; + break; + + case SDL_FOLDER_PUBLICSHARE: + SDL_SetError("Public share unavailable on Windows"); + goto done; + + case SDL_FOLDER_SAVEDGAMES: + type = SDL_FOLDERID_SavedGames; + break; + + case SDL_FOLDER_SCREENSHOTS: + type = SDL_FOLDERID_Screenshots; + break; + + case SDL_FOLDER_TEMPLATES: + type = SDL_FOLDERID_Templates; + break; + + case SDL_FOLDER_VIDEOS: + type = SDL_FOLDERID_Videos; + break; + + default: + SDL_SetError("Invalid SDL_Folder: %d", (int)folder); + goto done; + }; + + hr = pSHGetKnownFolderPath(&type, 0x00008000 /* KF_FLAG_CREATE */, NULL, &path); + if (SUCCEEDED(hr)) { + result = WIN_StringToUTF8W(path); + } else { + WIN_SetErrorFromHRESULT("Couldn't get folder", hr); + } + + } else { + int type; + HRESULT hr; + wchar_t path[MAX_PATH]; + + switch (folder) { + case SDL_FOLDER_HOME: + type = CSIDL_PROFILE; + break; + + case SDL_FOLDER_DESKTOP: + type = CSIDL_DESKTOP; + break; + + case SDL_FOLDER_DOCUMENTS: + type = CSIDL_MYDOCUMENTS; + break; + + case SDL_FOLDER_DOWNLOADS: + SDL_SetError("Downloads folder unavailable before Vista"); + goto done; + + case SDL_FOLDER_MUSIC: + type = CSIDL_MYMUSIC; + break; + + case SDL_FOLDER_PICTURES: + type = CSIDL_MYPICTURES; + break; + + case SDL_FOLDER_PUBLICSHARE: + SDL_SetError("Public share unavailable on Windows"); + goto done; + + case SDL_FOLDER_SAVEDGAMES: + SDL_SetError("Saved games unavailable before Vista"); + goto done; + + case SDL_FOLDER_SCREENSHOTS: + SDL_SetError("Screenshots folder unavailable before Vista"); + goto done; + + case SDL_FOLDER_TEMPLATES: + type = CSIDL_TEMPLATES; + break; + + case SDL_FOLDER_VIDEOS: + type = CSIDL_MYVIDEO; + break; + + default: + SDL_SetError("Unsupported SDL_Folder on Windows before Vista: %d", (int)folder); + goto done; + }; + + // Create the OS-specific folder if it doesn't already exist + type |= CSIDL_FLAG_CREATE; + +#if 0 + // Apparently the oldest, but not supported in modern Windows + HRESULT hr = SHGetSpecialFolderPath(NULL, path, type, TRUE); +#endif + + /* Windows 2000/XP and later, deprecated as of Windows 10 (still + available), available in Wine (tested 6.0.3) */ + hr = SHGetFolderPathW(NULL, type, NULL, SHGFP_TYPE_CURRENT, path); + + // use `== TRUE` for SHGetSpecialFolderPath + if (SUCCEEDED(hr)) { + result = WIN_StringToUTF8W(path); + } else { + WIN_SetErrorFromHRESULT("Couldn't get folder", hr); + } + } + + if (result) { + char *newresult = (char *) SDL_realloc(result, SDL_strlen(result) + 2); + + if (!newresult) { + SDL_free(result); + result = NULL; // will be returned + goto done; + } + + result = newresult; + SDL_strlcat(result, "\\", SDL_strlen(result) + 2); + } + +done: + if (lib) { + FreeLibrary(lib); + } + return result; +} + +char *SDL_SYS_GetCurrentDirectory(void) +{ + WCHAR *wstr = NULL; + DWORD buflen = 0; + while (true) { + const DWORD bw = GetCurrentDirectoryW(buflen, wstr); + if (bw == 0) { + WIN_SetError("GetCurrentDirectoryW failed"); + return NULL; + } else if (bw < buflen) { // we got it! + // make sure there's a path separator at the end. + SDL_assert(bw < (buflen + 2)); + if ((bw == 0) || (wstr[bw-1] != '\\')) { + wstr[bw] = '\\'; + wstr[bw + 1] = '\0'; + } + break; + } + + void *ptr = SDL_realloc(wstr, (bw + 1) * sizeof (WCHAR)); + if (!ptr) { + SDL_free(wstr); + return NULL; + } + wstr = (WCHAR *) ptr; + buflen = bw; + } + + char *retval = WIN_StringToUTF8W(wstr); + SDL_free(wstr); + return retval; +} + +#endif // SDL_FILESYSTEM_WINDOWS diff --git a/contrib/SDL-3.2.8/src/filesystem/windows/SDL_sysfsops.c b/contrib/SDL-3.2.8/src/filesystem/windows/SDL_sysfsops.c new file mode 100644 index 0000000..9c48ba9 --- /dev/null +++ b/contrib/SDL-3.2.8/src/filesystem/windows/SDL_sysfsops.c @@ -0,0 +1,231 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include "SDL_internal.h" + +#if defined(SDL_FSOPS_WINDOWS) + +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ +// System dependent filesystem routines + +#include "../../core/windows/SDL_windows.h" +#include "../SDL_sysfilesystem.h" + +bool SDL_SYS_EnumerateDirectory(const char *path, SDL_EnumerateDirectoryCallback cb, void *userdata) +{ + SDL_EnumerationResult result = SDL_ENUM_CONTINUE; + if (*path == '\0') { // if empty (completely at the root), we need to enumerate drive letters. + const DWORD drives = GetLogicalDrives(); + char name[] = { 0, ':', '\\', '\0' }; + for (int i = 'A'; (result == SDL_ENUM_CONTINUE) && (i <= 'Z'); i++) { + if (drives & (1 << (i - 'A'))) { + name[0] = (char) i; + result = cb(userdata, "", name); + } + } + } else { + // you need a wildcard to enumerate through FindFirstFileEx(), but the wildcard is only checked in the + // filename element at the end of the path string, so always tack on a "\\*" to get everything, and + // also prevent any wildcards inserted by the app from being respected. + char *pattern = NULL; + int patternlen = SDL_asprintf(&pattern, "%s\\\\", path); // we'll replace that second '\\' in the trimdown. + if ((patternlen == -1) || (!pattern)) { + return false; + } + + // trim down to a single path separator at the end, in case the caller added one or more. + patternlen--; + while ((patternlen >= 0) && ((pattern[patternlen] == '\\') || (pattern[patternlen] == '/'))) { + pattern[patternlen--] ='\0'; + } + pattern[++patternlen] = '\\'; + pattern[++patternlen] = '*'; + pattern[++patternlen] = '\0'; + + WCHAR *wpattern = WIN_UTF8ToStringW(pattern); + if (!wpattern) { + SDL_free(pattern); + return false; + } + + pattern[--patternlen] = '\0'; // chop off the '*' so we just have the dirname with a path separator. + + WIN32_FIND_DATAW entw; + HANDLE dir = FindFirstFileExW(wpattern, FindExInfoStandard, &entw, FindExSearchNameMatch, NULL, 0); + SDL_free(wpattern); + if (dir == INVALID_HANDLE_VALUE) { + SDL_free(pattern); + return WIN_SetError("Failed to enumerate directory"); + } + + do { + const WCHAR *fn = entw.cFileName; + + if (fn[0] == '.') { // ignore "." and ".." + if ((fn[1] == '\0') || ((fn[1] == '.') && (fn[2] == '\0'))) { + continue; + } + } + + char *utf8fn = WIN_StringToUTF8W(fn); + if (!utf8fn) { + result = SDL_ENUM_FAILURE; + } else { + result = cb(userdata, pattern, utf8fn); + SDL_free(utf8fn); + } + } while ((result == SDL_ENUM_CONTINUE) && (FindNextFileW(dir, &entw) != 0)); + + FindClose(dir); + SDL_free(pattern); + } + + return (result != SDL_ENUM_FAILURE); +} + +bool SDL_SYS_RemovePath(const char *path) +{ + WCHAR *wpath = WIN_UTF8ToStringW(path); + if (!wpath) { + return false; + } + + WIN32_FILE_ATTRIBUTE_DATA info; + if (!GetFileAttributesExW(wpath, GetFileExInfoStandard, &info)) { + SDL_free(wpath); + if (GetLastError() == ERROR_FILE_NOT_FOUND) { + // Note that ERROR_PATH_NOT_FOUND means a parent dir is missing, and we consider that an error. + return true; // thing is already gone, call it a success. + } + return WIN_SetError("Couldn't get path's attributes"); + } + + const int isdir = (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); + const BOOL rc = isdir ? RemoveDirectoryW(wpath) : DeleteFileW(wpath); + SDL_free(wpath); + if (!rc) { + return WIN_SetError("Couldn't remove path"); + } + return true; +} + +bool SDL_SYS_RenamePath(const char *oldpath, const char *newpath) +{ + WCHAR *woldpath = WIN_UTF8ToStringW(oldpath); + if (!woldpath) { + return false; + } + + WCHAR *wnewpath = WIN_UTF8ToStringW(newpath); + if (!wnewpath) { + SDL_free(woldpath); + return false; + } + + const BOOL rc = MoveFileExW(woldpath, wnewpath, MOVEFILE_REPLACE_EXISTING); + SDL_free(wnewpath); + SDL_free(woldpath); + if (!rc) { + return WIN_SetError("Couldn't rename path"); + } + return true; +} + +bool SDL_SYS_CopyFile(const char *oldpath, const char *newpath) +{ + WCHAR *woldpath = WIN_UTF8ToStringW(oldpath); + if (!woldpath) { + return false; + } + + WCHAR *wnewpath = WIN_UTF8ToStringW(newpath); + if (!wnewpath) { + SDL_free(woldpath); + return false; + } + + const BOOL rc = CopyFileExW(woldpath, wnewpath, NULL, NULL, NULL, COPY_FILE_ALLOW_DECRYPTED_DESTINATION|COPY_FILE_NO_BUFFERING); + SDL_free(wnewpath); + SDL_free(woldpath); + if (!rc) { + return WIN_SetError("Couldn't copy path"); + } + return true; +} + +bool SDL_SYS_CreateDirectory(const char *path) +{ + WCHAR *wpath = WIN_UTF8ToStringW(path); + if (!wpath) { + return false; + } + + DWORD rc = CreateDirectoryW(wpath, NULL); + if (!rc && (GetLastError() == ERROR_ALREADY_EXISTS)) { + WIN32_FILE_ATTRIBUTE_DATA winstat; + if (GetFileAttributesExW(wpath, GetFileExInfoStandard, &winstat)) { + if (winstat.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { + rc = 1; // exists and is already a directory: cool. + } + } + } + + SDL_free(wpath); + if (!rc) { + return WIN_SetError("Couldn't create directory"); + } + return true; +} + +bool SDL_SYS_GetPathInfo(const char *path, SDL_PathInfo *info) +{ + WCHAR *wpath = WIN_UTF8ToStringW(path); + if (!wpath) { + return false; + } + + WIN32_FILE_ATTRIBUTE_DATA winstat; + const BOOL rc = GetFileAttributesExW(wpath, GetFileExInfoStandard, &winstat); + SDL_free(wpath); + if (!rc) { + return WIN_SetError("Can't stat"); + } + + if (winstat.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { + info->type = SDL_PATHTYPE_DIRECTORY; + info->size = 0; + } else if (winstat.dwFileAttributes & (FILE_ATTRIBUTE_OFFLINE | FILE_ATTRIBUTE_DEVICE)) { + info->type = SDL_PATHTYPE_OTHER; + info->size = ((((Uint64) winstat.nFileSizeHigh) << 32) | winstat.nFileSizeLow); + } else { + info->type = SDL_PATHTYPE_FILE; + info->size = ((((Uint64) winstat.nFileSizeHigh) << 32) | winstat.nFileSizeLow); + } + + info->create_time = SDL_TimeFromWindows(winstat.ftCreationTime.dwLowDateTime, winstat.ftCreationTime.dwHighDateTime); + info->modify_time = SDL_TimeFromWindows(winstat.ftLastWriteTime.dwLowDateTime, winstat.ftLastWriteTime.dwHighDateTime); + info->access_time = SDL_TimeFromWindows(winstat.ftLastAccessTime.dwLowDateTime, winstat.ftLastAccessTime.dwHighDateTime); + + return true; +} + +#endif // SDL_FSOPS_WINDOWS + -- cgit v1.2.3