SDL: gdk: GetBasePath should be a UTF8 version of Win32 GetBasePath

From 6f8a6a31ca4b3e44eec2dd312da747d7fe85f791 Mon Sep 17 00:00:00 2001
From: Ethan Lee <[EMAIL REDACTED]>
Date: Fri, 25 Aug 2023 10:47:10 -0400
Subject: [PATCH] gdk: GetBasePath should be a UTF8 version of Win32
 GetBasePath

---
 src/filesystem/gdk/SDL_sysfilesystem.cpp | 46 +++++++++++++++++++++++-
 1 file changed, 45 insertions(+), 1 deletion(-)

diff --git a/src/filesystem/gdk/SDL_sysfilesystem.cpp b/src/filesystem/gdk/SDL_sysfilesystem.cpp
index 30a2b3489e55..517524bdb2be 100644
--- a/src/filesystem/gdk/SDL_sysfilesystem.cpp
+++ b/src/filesystem/gdk/SDL_sysfilesystem.cpp
@@ -34,7 +34,51 @@
 char *
 SDL_GetBasePath(void)
 {
-    return SDL_strdup("G:\\");
+    /* NOTE: This function is a UTF8 version of the Win32 SDL_GetBasePath()!
+     * The GDK actually _recommends_ the 'A' functions over the 'W' functions :o
+     */
+    DWORD buflen = 128;
+    CHAR *path = NULL;
+    DWORD len = 0;
+    int i;
+
+    while (SDL_TRUE) {
+        void *ptr = SDL_realloc(path, buflen * sizeof(CHAR));
+        if (ptr == NULL) {
+            SDL_free(path);
+            SDL_OutOfMemory();
+            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;
+    }
+
+    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. */
+
+    return path;
 }
 
 char *