4373: Add UNICODE defines on windows, misc. unicode fixes r=def- a=Robyt3

Add `UNICODE` and `_UNICODE` defines on Windows and use either the A or the W variant of the Windows API methods explicitly where it was left unspecified.

I supposed this fixes `shell_execute` for unicode paths (e.g. username with unicode) and fixes debug logger output with unicode.

I renamed `logger_debugger` to `logger_win_debugger` to make it clear that it's only available on windows. 

I'm using the A-variant for `WSAStringToAddress` and `FormatMessage` because they don't really need to handle unicode, but there is probably a region specific winsock error message containing unicode.

Does not fix unicode commandline arguments.

## Checklist

- [X] Tested the change ingame
- [ ] Provided screenshots if it is a visual change
- [ ] Tested in combination with possibly related configuration options
- [ ] Written a unit test if it works standalone, system.c especially
- [ ] Considered possible null pointers and out of bounds array indexing
- [ ] Changed no physics that affect existing maps
- [ ] Tested the change with [ASan+UBSan or valgrind's memcheck](https://github.com/ddnet/ddnet/#using-addresssanitizer--undefinedbehavioursanitizer-or-valgrinds-memcheck) (optional)


Co-authored-by: Robert Müller <robert.mueller@uni-siegen.de>
This commit is contained in:
bors[bot] 2021-11-20 12:00:06 +00:00 committed by GitHub
commit 82b5f08ade
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 13 deletions

View file

@ -2769,6 +2769,10 @@ foreach(target ${TARGETS_OWN})
target_compile_options(${target} PRIVATE /wd4267) # Possible loss of data (size_t - int on win64).
target_compile_options(${target} PRIVATE /wd4800) # Implicit conversion of int to bool.
endif()
if(TARGET_OS STREQUAL "windows")
target_compile_definitions(${target} PRIVATE UNICODE) # Windows headers
target_compile_definitions(${target} PRIVATE _UNICODE) # C-runtime
endif()
if(OUR_FLAGS_OWN)
target_compile_options(${target} PRIVATE ${OUR_FLAGS_OWN})
endif()

View file

@ -160,11 +160,13 @@ void dbg_msg(const char *sys, const char *fmt, ...)
}
#if defined(CONF_FAMILY_WINDOWS)
static void logger_debugger(const char *line, void *user)
static void logger_win_debugger(const char *line, void *user)
{
(void)user;
OutputDebugString(line);
OutputDebugString("\n");
WCHAR wBuffer[512];
MultiByteToWideChar(CP_UTF8, 0, line, -1, wBuffer, sizeof(wBuffer));
OutputDebugStringW(wBuffer);
OutputDebugStringW(L"\n");
}
#endif
@ -280,7 +282,7 @@ void dbg_logger_stdout()
void dbg_logger_debugger()
{
#if defined(CONF_FAMILY_WINDOWS)
dbg_logger(logger_debugger, 0, 0);
dbg_logger(logger_win_debugger, 0, 0);
#endif
}
@ -1334,7 +1336,7 @@ int net_addr_from_str(NETADDR *addr, const char *string)
int size;
sa6.sin6_family = AF_INET6;
size = (int)sizeof(sa6);
if(WSAStringToAddress(buf, AF_INET6, NULL, (struct sockaddr *)&sa6, &size) != 0)
if(WSAStringToAddressA(buf, AF_INET6, NULL, (struct sockaddr *)&sa6, &size) != 0)
return -1;
}
#else
@ -1447,7 +1449,7 @@ static int priv_net_create_socket(int domain, int type, struct sockaddr *addr, i
#if defined(CONF_FAMILY_WINDOWS)
char buf[128];
int error = WSAGetLastError();
if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, 0, buf, sizeof(buf), 0) == 0)
if(FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, 0, buf, sizeof(buf), 0) == 0)
buf[0] = 0;
dbg_msg("net", "failed to create socket with domain %d and type %d (%d '%s')", domain, type, error, buf);
#else
@ -1484,7 +1486,7 @@ static int priv_net_create_socket(int domain, int type, struct sockaddr *addr, i
#if defined(CONF_FAMILY_WINDOWS)
char buf[128];
int error = WSAGetLastError();
if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, 0, buf, sizeof(buf), 0) == 0)
if(FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, 0, buf, sizeof(buf), 0) == 0)
buf[0] = 0;
dbg_msg("net", "failed to bind socket with domain %d and type %d (%d '%s')", domain, type, error, buf);
#else
@ -3559,14 +3561,16 @@ int pid()
PROCESS shell_execute(const char *file)
{
#if defined(CONF_FAMILY_WINDOWS)
SHELLEXECUTEINFOA info;
mem_zero(&info, sizeof(SHELLEXECUTEINFOA));
info.cbSize = sizeof(SHELLEXECUTEINFOA);
info.lpVerb = "open";
info.lpFile = file;
WCHAR wBuffer[512];
MultiByteToWideChar(CP_UTF8, 0, file, -1, wBuffer, sizeof(wBuffer));
SHELLEXECUTEINFOW info;
mem_zero(&info, sizeof(SHELLEXECUTEINFOW));
info.cbSize = sizeof(SHELLEXECUTEINFOW);
info.lpVerb = L"open";
info.lpFile = wBuffer;
info.nShow = SW_SHOWMINNOACTIVE;
info.fMask = SEE_MASK_NOCLOSEPROCESS;
ShellExecuteEx(&info);
ShellExecuteExW(&info);
return info.hProcess;
#elif defined(CONF_FAMILY_UNIX)
char *argv[2];