From 850051615936fbd7f4053095db616bdb3c26ad17 Mon Sep 17 00:00:00 2001 From: MysterD Date: Sun, 14 May 2023 18:33:14 -0700 Subject: [PATCH] Add wide character support to language file handling --- Makefile | 2 +- src/pc/djui/djui_panel_language.c | 14 ++++++++------ src/pc/os/os.h | 6 ++++++ src/pc/os/os_other.h | 10 ++++++++++ src/pc/os/os_win.c | 20 ++++++++++++++++++++ src/pc/os/os_win.h | 13 +++++++++++++ 6 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 src/pc/os/os.h create mode 100644 src/pc/os/os_other.h create mode 100644 src/pc/os/os_win.c create mode 100644 src/pc/os/os_win.h diff --git a/Makefile b/Makefile index f686467f..09352e6a 100644 --- a/Makefile +++ b/Makefile @@ -534,7 +534,7 @@ SRC_DIRS := src src/engine src/game src/audio src/bass_audio src/menu src/buffer BIN_DIRS := bin bin/$(VERSION) # PC files -SRC_DIRS += src/pc src/pc/gfx src/pc/audio src/pc/controller src/pc/fs src/pc/fs/packtypes src/pc/mods src/dev src/pc/network src/pc/network/packets src/pc/network/socket src/pc/network/coopnet src/pc/utils src/pc/utils/miniz src/pc/djui src/pc/lua src/pc/lua/utils +SRC_DIRS += src/pc src/pc/gfx src/pc/audio src/pc/controller src/pc/fs src/pc/fs/packtypes src/pc/mods src/dev src/pc/network src/pc/network/packets src/pc/network/socket src/pc/network/coopnet src/pc/utils src/pc/utils/miniz src/pc/djui src/pc/lua src/pc/lua/utils src/pc/os ifeq ($(DISCORD_SDK),1) SRC_DIRS += src/pc/discord diff --git a/src/pc/djui/djui_panel_language.c b/src/pc/djui/djui_panel_language.c index 4e66bcae..52f8c8c9 100644 --- a/src/pc/djui/djui_panel_language.c +++ b/src/pc/djui/djui_panel_language.c @@ -8,6 +8,7 @@ #include "pc/debuglog.h" #include "pc/utils/misc.h" #include "pc/configfile.h" +#include "pc/os/os.h" extern bool directory_sanity_check(struct dirent* dir, char* dirPath, char* outPath); static bool sTrue = true; @@ -83,8 +84,9 @@ void djui_panel_language_create(struct DjuiBase* caller) { snprintf(lpath, SYS_MAX_PATH, "%s/lang", sys_exe_path()); // open directory - struct dirent* dir = NULL; - DIR* d = opendir(lpath); + os_dirent* dir = NULL; + + OS_DIR* d = os_opendir(lpath); if (!d) { LOG_ERROR("Could not open directory '%s'", lpath); @@ -106,10 +108,10 @@ void djui_panel_language_create(struct DjuiBase* caller) { // iterate char path[SYS_MAX_PATH] = { 0 }; - while ((dir = readdir(d)) != NULL) { + while ((dir = os_readdir(d)) != NULL) { // sanity check / fill path[] - if (!directory_sanity_check(dir, lpath, path)) { continue; } - snprintf(path, SYS_MAX_PATH, "%s", dir->d_name); + //if (!directory_sanity_check(dir, lpath, path)) { continue; } + snprintf(path, SYS_MAX_PATH, "%s", os_get_dir_name(dir)); // strip the name before the . char* c = path; @@ -125,7 +127,7 @@ void djui_panel_language_create(struct DjuiBase* caller) { if (!strcmp(path, "English")) { chkEnglish = checkbox; } } - closedir(d); + os_closedir(d); if (!foundMatch && chkEnglish) { chkEnglish->value = &sTrue; diff --git a/src/pc/os/os.h b/src/pc/os/os.h new file mode 100644 index 00000000..9d6a2792 --- /dev/null +++ b/src/pc/os/os.h @@ -0,0 +1,6 @@ +#pragma once +#if defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined(WIN64) +#include "os_win.h" +#else +#include "os_other.h" +#endif \ No newline at end of file diff --git a/src/pc/os/os_other.h b/src/pc/os/os_other.h new file mode 100644 index 00000000..e1cf232b --- /dev/null +++ b/src/pc/os/os_other.h @@ -0,0 +1,10 @@ +#pragma once + +#include + +#define os_dirent struct dirent +#define OS_DIR DIR +#define os_opendir(_x) opendir(_x) +#define os_readdir(_x) readdir(_x) +#define os_closedir(_x) closedir(_x) +#define os_get_dir_name(_x) _x->d_name diff --git a/src/pc/os/os_win.c b/src/pc/os/os_win.c new file mode 100644 index 00000000..cd755630 --- /dev/null +++ b/src/pc/os/os_win.c @@ -0,0 +1,20 @@ +#if defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined(WIN64) + +#include +#include +#include "os_win.h" +#include "pc/platform.h" + +OS_DIR* os_opendir(char* path) { + wchar_t wpath[SYS_MAX_PATH] = { 0 }; + MultiByteToWideChar(CP_UTF8, 0, path, -1, wpath, SYS_MAX_PATH); + return _wopendir(wpath); +} + +char* os_get_dir_name(os_dirent* dir) { + static char path[SYS_MAX_PATH] = { 0 }; + snprintf(path, SYS_MAX_PATH, "%ls", dir->d_name); + return path; +} + +#endif \ No newline at end of file diff --git a/src/pc/os/os_win.h b/src/pc/os/os_win.h new file mode 100644 index 00000000..c3254b82 --- /dev/null +++ b/src/pc/os/os_win.h @@ -0,0 +1,13 @@ +#pragma once + +#include +#include + +#define os_dirent struct _wdirent +#define OS_DIR _WDIR + +#define os_closedir(_x) _wclosedir(_x) +#define os_readdir(_x) _wreaddir(_x) + +OS_DIR* os_opendir(char* path); +char* os_get_dir_name(os_dirent* dir);