ref: 920ffea9b631e712ff0826911db01a76edbe7521
parent: c6b8f1163708db85251daac24b2ffa6bea24a72a
author: Simon Howard <[email protected]>
date: Sat Feb 4 18:05:42 EST 2012
On Windows, convert the USER and USERNAME environment variables from OEM codepage to UTF-8 encoding. This should fix the case where the user has a username that includes non-ASCII characters (thanks Alexandre Xavier). Subversion-branch: /trunk/chocolate-doom Subversion-revision: 2495
--- a/setup/multiplayer.c
+++ b/setup/multiplayer.c
@@ -793,6 +793,28 @@
}
}
+#ifdef _WIN32
+
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+
+char *M_OEMToUTF8(const char *oem)
+{
+ unsigned int len = strlen(oem) + 1;
+ wchar_t *tmp;
+ char *result;
+
+ tmp = malloc(len * sizeof(wchar_t));
+ MultiByteToWideChar(CP_OEMCP, 0, oem, len, tmp, len);
+ result = malloc(len * 4);
+ WideCharToMultiByte(CP_UTF8, 0, tmp, len, result, len * 4, NULL, NULL);
+ free(tmp);
+
+ return result;
+}
+
+#endif
+
void SetPlayerNameDefault(void)
{
if (net_player_name == NULL)
@@ -804,6 +826,16 @@
{
net_player_name = getenv("USERNAME");
}
+
+ // On Windows, environment variables are in OEM codepage
+ // encoding, so convert to UTF8:
+
+#ifdef _WIN32
+ if (net_player_name != NULL)
+ {
+ net_player_name = M_OEMToUTF8(net_player_name);
+ }
+#endif
if (net_player_name == NULL)
{
--- a/src/m_misc.c
+++ b/src/m_misc.c
@@ -30,9 +30,9 @@
#include <ctype.h>
#include <errno.h>
-// for mkdir:
-
#ifdef _WIN32
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
#include <io.h>
#ifdef _MSC_VER
#include <direct.h>
@@ -205,4 +205,23 @@
|| sscanf(str, " 0%o", result) == 1
|| sscanf(str, " %d", result) == 1;
}
+
+#ifdef _WIN32
+
+char *M_OEMToUTF8(const char *oem)
+{
+ unsigned int len = strlen(oem) + 1;
+ wchar_t *tmp;
+ char *result;
+
+ tmp = malloc(len * sizeof(wchar_t));
+ MultiByteToWideChar(CP_OEMCP, 0, oem, len, tmp, len);
+ result = malloc(len * 4);
+ WideCharToMultiByte(CP_UTF8, 0, tmp, len, result, len * 4, NULL, NULL);
+ free(tmp);
+
+ return result;
+}
+
+#endif
--- a/src/m_misc.h
+++ b/src/m_misc.h
@@ -40,6 +40,7 @@
boolean M_FileExists(char *file);
long M_FileLength(FILE *handle);
boolean M_StrToInt(const char *str, int *result);
+char *M_OEMToUTF8(const char *ansi);
#endif
--- a/src/net_client.c
+++ b/src/net_client.c
@@ -33,6 +33,7 @@
#include "i_system.h"
#include "i_timer.h"
#include "m_argv.h"
+#include "m_misc.h"
#include "net_client.h"
#include "net_common.h"
#include "net_defs.h"
@@ -1273,6 +1274,17 @@
net_player_name = getenv("USER");
if (net_player_name == NULL)
net_player_name = getenv("USERNAME");
+
+ // On Windows, environment variables are in OEM codepage
+ // encoding, so convert to UTF8:
+
+#ifdef _WIN32
+ if (net_player_name != NULL)
+ {
+ net_player_name = M_OEMToUTF8(net_player_name);
+ }
+#endif
+
if (net_player_name == NULL)
net_player_name = "Player";
}