From 90bcbd99440e675edd58655d160d15ce417175fc Mon Sep 17 00:00:00 2001 From: NepDisk Date: Sun, 9 Feb 2025 12:47:02 -0500 Subject: [PATCH] Sdldraganddrop utf8 on windows https://github.com/Indev450/SRB2Kart-Saturn/pull/60/ --- src/sdl/i_video.cpp | 2 +- src/w_wad.c | 38 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/sdl/i_video.cpp b/src/sdl/i_video.cpp index 0a614d3bc..19ece061e 100644 --- a/src/sdl/i_video.cpp +++ b/src/sdl/i_video.cpp @@ -1191,7 +1191,7 @@ void I_GetEvent(void) break; case SDL_DROPFILE: dropped_filedir = evt.drop.file; - P_AddWadFile(dropped_filedir); + P_AddWadFile(dropped_filedir, WC_AUTO); SDL_free(dropped_filedir); // Free dropped_filedir memory break; case SDL_QUIT: diff --git a/src/w_wad.c b/src/w_wad.c index dc8927d96..044fb1e35 100644 --- a/src/w_wad.c +++ b/src/w_wad.c @@ -149,6 +149,40 @@ void W_Shutdown(void) static char filenamebuf[MAX_WADPATH]; +// This #if is copied from filesrch.c, so not sure if it is 100% suitable for +// this +#if defined (_WIN32) && !defined (_XBOX) +//#define WIN32_LEAN_AND_MEAN +#define RPC_NO_WINDOWS_H +#include + +// Windows can't open utf-8 path so it must be converted to utf-16 +FILE* fopen_utf8(const char* filename, const char* mode) +{ + static const int MY_PATH_MAX = 2048; + WCHAR nameW[MY_PATH_MAX]; + memset(nameW, 0, sizeof(WCHAR)*MY_PATH_MAX); + WCHAR modeW[16]; + memset(modeW, 0, sizeof(WCHAR)*16); + // the following function converts the UTF-8 filename to UTF-16 (WCHAR) nameW + int len = MultiByteToWideChar(CP_UTF8, 0, filename, -1, nameW, MY_PATH_MAX); + if(len > 0 && MultiByteToWideChar(CP_UTF8, 0, mode, -1, modeW, 16) > 0) + { + // using _wfopen_s() shuts up MSVC's complaints + // about _wfopen() being unsafe.. + FILE* ret = NULL; + if(_wfopen_s(&ret, nameW, modeW) == 0) + return ret; + } + return NULL; +} + +#else + +#define fopen_utf8 fopen + +#endif + // W_OpenWadFile // Helper function for opening the WAD file. // Returns the FILE * handle for the file, or NULL if not found or could not be opened @@ -169,7 +203,7 @@ FILE *W_OpenWadFile(const char **filename, boolean useerrors) } // open wad file - if ((handle = fopen(*filename, "rb")) == NULL) + if ((handle = fopen_utf8(*filename, "rb")) == NULL) { // If we failed to load the file with the path as specified by // the user, strip the directories and search for the file. @@ -179,7 +213,7 @@ FILE *W_OpenWadFile(const char **filename, boolean useerrors) // in filenamebuf == *filename. if (findfile(filenamebuf, NULL, true)) { - if ((handle = fopen(*filename, "rb")) == NULL) + if ((handle = fopen_utf8(*filename, "rb")) == NULL) { if (useerrors) CONS_Alert(CONS_ERROR, M_GetText("Can't open %s\n"), *filename);