Fix freezing on windows 98

This commit is contained in:
UnknownShadow200 2021-01-19 22:28:52 +11:00
parent 5c6051c317
commit db564a72f9
4 changed files with 42 additions and 28 deletions

View File

@ -338,11 +338,14 @@ cc_uint64 Stopwatch_Measure(void) {
#if defined CC_BUILD_WIN #if defined CC_BUILD_WIN
cc_result Directory_Create(const cc_string* path) { cc_result Directory_Create(const cc_string* path) {
WCHAR str[NATIVE_STR_LEN]; WCHAR str[NATIVE_STR_LEN];
BOOL success; cc_result res;
Platform_EncodeUtf16(str, path); Platform_EncodeUtf16(str, path);
success = CreateDirectoryW(str, NULL); if (CreateDirectoryW(str, NULL)) return 0;
return success ? 0 : GetLastError(); if ((res = GetLastError()) != ERROR_CALL_NOT_IMPLEMENTED) return res;
Platform_Utf16ToAnsi(str);
return CreateDirectoryA((LPCSTR)str, NULL) ? 0 : GetLastError();
} }
int File_Exists(const cc_string* path) { int File_Exists(const cc_string* path) {
@ -408,7 +411,7 @@ static cc_result DoFile(cc_file* file, const cc_string* path, DWORD access, DWOR
if ((res = GetLastError()) != ERROR_CALL_NOT_IMPLEMENTED) return res; if ((res = GetLastError()) != ERROR_CALL_NOT_IMPLEMENTED) return res;
/* Windows 9x does not support W API functions */ /* Windows 9x does not support W API functions */
Platform_EncodeAnsi(str, path); Platform_Utf16ToAnsi(str);
*file = CreateFileA((LPCSTR)str, access, FILE_SHARE_READ, NULL, createMode, 0, NULL); *file = CreateFileA((LPCSTR)str, access, FILE_SHARE_READ, NULL, createMode, 0, NULL);
return *file != INVALID_HANDLE_VALUE ? 0 : GetLastError(); return *file != INVALID_HANDLE_VALUE ? 0 : GetLastError();
} }
@ -1018,16 +1021,23 @@ cc_result Socket_Poll(cc_socket s, int mode, cc_bool* success) {
*-----------------------------------------------------Process/Module------------------------------------------------------* *-----------------------------------------------------Process/Module------------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
#if defined CC_BUILD_WIN #if defined CC_BUILD_WIN
static cc_result Process_RawStart(const WCHAR* path, WCHAR* args) { static cc_result Process_RawStart(WCHAR* path, WCHAR* args) {
STARTUPINFOW si = { 0 }; STARTUPINFOW si = { 0 };
PROCESS_INFORMATION pi = { 0 }; PROCESS_INFORMATION pi = { 0 };
BOOL ok; cc_result res;
si.cb = sizeof(STARTUPINFOW); si.cb = sizeof(STARTUPINFOW);
ok = CreateProcessW(path, args, NULL, NULL, false, 0, NULL, NULL, &si, &pi);
if (!ok) return GetLastError();
/* Don't leak memory for proess return code */ if (CreateProcessW(path, args, NULL, NULL,
false, 0, NULL, NULL, &si, &pi)) goto success;
//if ((res = GetLastError()) != ERROR_CALL_NOT_IMPLEMENTED) return res;
//Platform_Utf16ToAnsi(path);
//if (CreateProcessA((LPCSTR)path, args, NULL, NULL,
// false, 0, NULL, NULL, &si, &pi)) goto success;
return GetLastError();
success:
/* Don't leak memory for process return code */
CloseHandle(pi.hProcess); CloseHandle(pi.hProcess);
CloseHandle(pi.hThread); CloseHandle(pi.hThread);
return 0; return 0;
@ -1551,13 +1561,12 @@ int Platform_EncodeUtf16(void* data, const cc_string* src) {
return src->length * 2; return src->length * 2;
} }
int Platform_EncodeAnsi(void* data, const cc_string* src) { void Platform_Utf16ToAnsi(void* data) {
char* dst = (char*)data; WCHAR* src = (WCHAR*)data;
if (src->length > FILENAME_SIZE) Logger_Abort("String too long to expand"); char* dst = (char*)data;
Mem_Copy(dst, src->buffer, src->length); while (*src) { *dst++ = *src++; }
dst[src->length] = '\0'; *dst = '\0';
return src->length;
} }
static void Platform_InitStopwatch(void) { static void Platform_InitStopwatch(void) {

View File

@ -35,9 +35,8 @@ extern const cc_result ReturnCode_DirectoryExists;
/* Encodes a string in UTF16 format, also null terminating the string. */ /* Encodes a string in UTF16 format, also null terminating the string. */
/* Returns the number of bytes written, excluding trailing NULL terminator. */ /* Returns the number of bytes written, excluding trailing NULL terminator. */
int Platform_EncodeUtf16(void* data, const cc_string* src); int Platform_EncodeUtf16(void* data, const cc_string* src);
/* Encodes a string in ansi format, also null terminating the string. */ /* Converts a null terminated WCHAR* to char* in-place */
/* Returns the number of bytes written, excluding trailing NULL terminator. */ void Platform_Utf16ToAnsi(void* data);
int Platform_EncodeAnsi(void* data, const cc_string* src);
#else #else
/* Encodes a string in UTF8 format, also null terminating the string. */ /* Encodes a string in UTF8 format, also null terminating the string. */
/* Returns the number of bytes written, excluding trailing NULL terminator. */ /* Returns the number of bytes written, excluding trailing NULL terminator. */

View File

@ -344,7 +344,7 @@ static void ExtractFromFile(const cc_string* filename) {
if (res) { if (res) {
/* Game shows a dialog if default.zip is missing */ /* Game shows a dialog if default.zip is missing */
Game_DefaultZipMissing |= res == ReturnCode_FileNotFound Game_DefaultZipMissing |= res == ReturnCode_FileNotFound
&& String_CaselessEquals(filename, &defaultZip); && String_CaselessEquals(filename, &defaultZip);
Logger_SysWarn2(res, "opening", &path); return; Logger_SysWarn2(res, "opening", &path); return;
} }

View File

@ -674,7 +674,7 @@ static LRESULT CALLBACK Window_Procedure(HWND handle, UINT message, WPARAM wPara
case WM_DESTROY: case WM_DESTROY:
WindowInfo.Exists = false; WindowInfo.Exists = false;
UnregisterClass(CC_WIN_CLASSNAME, win_instance); UnregisterClassW(CC_WIN_CLASSNAME, win_instance);
if (win_DC) ReleaseDC(win_handle, win_DC); if (win_DC) ReleaseDC(win_handle, win_DC);
break; break;
@ -837,7 +837,7 @@ void Window_Show(void) {
} }
int Window_GetWindowState(void) { int Window_GetWindowState(void) {
DWORD s = GetWindowLong(win_handle, GWL_STYLE); DWORD s = GetWindowLongW(win_handle, GWL_STYLE);
if ((s & WS_MINIMIZE)) return WINDOW_STATE_MINIMISED; if ((s & WS_MINIMIZE)) return WINDOW_STATE_MINIMISED;
if ((s & WS_MAXIMIZE) && (s & WS_POPUP)) return WINDOW_STATE_FULLSCREEN; if ((s & WS_MAXIMIZE) && (s & WS_POPUP)) return WINDOW_STATE_FULLSCREEN;
@ -851,7 +851,7 @@ static void ToggleFullscreen(cc_bool fullscreen, UINT finalShow) {
suppress_resize = true; suppress_resize = true;
{ {
ShowWindow(win_handle, SW_RESTORE); /* reset maximised state */ ShowWindow(win_handle, SW_RESTORE); /* reset maximised state */
SetWindowLong(win_handle, GWL_STYLE, style); SetWindowLongW(win_handle, GWL_STYLE, style);
SetWindowPos(win_handle, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); SetWindowPos(win_handle, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
ShowWindow(win_handle, finalShow); ShowWindow(win_handle, finalShow);
Window_ProcessEvents(); Window_ProcessEvents();
@ -881,7 +881,7 @@ cc_result Window_ExitFullscreen(void) {
void Window_SetSize(int width, int height) { void Window_SetSize(int width, int height) {
DWORD style = GetWindowLong(win_handle, GWL_STYLE); DWORD style = GetWindowLongW(win_handle, GWL_STYLE);
RECT rect = { 0, 0, width, height }; RECT rect = { 0, 0, width, height };
AdjustWindowRect(&rect, style, false); AdjustWindowRect(&rect, style, false);
@ -890,15 +890,21 @@ void Window_SetSize(int width, int height) {
} }
void Window_Close(void) { void Window_Close(void) {
PostMessage(win_handle, WM_CLOSE, 0, 0); PostMessageW(win_handle, WM_CLOSE, 0, 0);
} }
void Window_ProcessEvents(void) { void Window_ProcessEvents(void) {
HWND foreground; HWND foreground;
MSG msg; MSG msg;
while (PeekMessage(&msg, NULL, 0, 0, 1)) {
TranslateMessage(&msg); if (is_ansiWindow) {
DispatchMessage(&msg); while (PeekMessageA(&msg, NULL, 0, 0, 1)) {
TranslateMessage(&msg); DispatchMessageA(&msg);
}
} else {
while (PeekMessageW(&msg, NULL, 0, 0, 1)) {
TranslateMessage(&msg); DispatchMessageW(&msg);
}
} }
foreground = GetForegroundWindow(); foreground = GetForegroundWindow();