Explorar o código

gdk: Just use WIN_GetModulePath().

There's no need to use the "A" version of GetModuleFileName on GDK; it returns
a UTF-8 string directly on this platform, but we can still use the UTF-16 "W"
version and cut down on code duplication.

This code runs once and caches the results, so we can take the one-time string
conversion overhead.

(cherry picked from commit 6b780c5ff94b6a219e29fcdd38b80a9594ec4c5d)
Ryan C. Gordon hai 10 horas
pai
achega
11e13dc4a5
Modificáronse 1 ficheiros con 19 adicións e 46 borrados
  1. 19 46
      src/filesystem/gdk/SDL_sysfilesystem.cpp

+ 19 - 46
src/filesystem/gdk/SDL_sysfilesystem.cpp

@@ -33,61 +33,34 @@ extern "C" {
 #include <SDL3/SDL_filesystem.h>
 #include <XGameSaveFiles.h>
 
-char *
-SDL_SYS_GetBasePath(void)
+char *SDL_SYS_GetBasePath(void)
 {
-    /* NOTE: This function is a UTF8 version of the Win32 SDL_GetBasePath()!
-     * The GDK actually _recommends_ the 'A' functions over the 'W' functions :o
-     *
-     * !!! FIXME: But can we use WIN_GetModulePath anyhow? (or change WIN_GetModulePath to use GetModuleFileNameA when built for GDK?)
-     */
-    DWORD buflen = 128;
-    CHAR *path = NULL;
-    DWORD len = 0;
-    int i;
-
-    while (true) {
-        void *ptr = SDL_realloc(path, buflen * sizeof(CHAR));
-        if (!ptr) {
-            SDL_free(path);
-            return NULL;
-        }
-
-        path = (CHAR *)ptr;
-
-        len = GetModuleFileNameA(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;
+    char *path = WIN_GetModulePath(NULL);  // look up full path of the current process's EXE file.
+    if (!path) {
+        return NULL;  // error message was already set.
     }
 
-    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;
-        }
-    }
+    char *ptr = SDL_strrchr(path, '\\');
+    SDL_assert(ptr != NULL);  // Should have been an absolute path.
 
-    SDL_assert(i > 0);  // Should have been an absolute path.
-    path[i + 1] = '\0'; // chop off filename.
+    ptr[1] = '\0'; // chop off filename, leave '\\'.
 
-    return path;
+    ptr = (char *) SDL_realloc(path, ((size_t) (ptr - path)) + 2);  // try to shrink this allocation down a little.
+    return ptr ? ptr : path;  // return shrunk buffer if shrink worked out, unchanged original buffer if not.
 }
 
 char *SDL_SYS_GetExeName(void)
 {
-    SDL_Unsupported();  // !!! FIXME: use WIN_GetModulePath
-    return NULL;
+    char *path = WIN_GetModulePath(NULL);  // look up full path of the current process's EXE file.
+    if (!path) {
+        return NULL;  // error message was already set.
+    }
+
+    char *ptr = SDL_strrchr(path, '\\');
+    const size_t slen = SDL_strlen(ptr);  // counts null terminator because we're still sitting on path separator.
+    SDL_memmove(path, ptr + 1, slen);  // move filename string to start of SDL_realloc'd region.
+    ptr = (char *) SDL_realloc(path, slen);  // try to shrink this allocation down a little.
+    return ptr ? ptr : path;  // return shrunk buffer if shrink worked out, unchanged original buffer if not.
 }
 
 char *SDL_SYS_GetPrefPath(const char *org, const char *app)