From c3173189242d02486e620fd72d309638a16ef425 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 6 Nov 2021 17:46:13 +1100 Subject: [PATCH 1/2] Fix chatlog overflow at 8.3 million characters, addresses #837 --- src/Chat.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/Chat.c b/src/Chat.c index b98a164f6..14b134980 100644 --- a/src/Chat.c +++ b/src/Chat.c @@ -53,6 +53,15 @@ static void AppendChatLogTime(void) { Chat_LogTime[logTimesCount++] = now; } +static void ClearChatLogs(void) { + if (Chat_LogTime != defaultLogTimes) Mem_Free(Chat_LogTime); + Chat_LogTime = defaultLogTimes; + logTimesCount = 0; + logTimesCapacity = CHAT_LOGTIMES_DEF_ELEMS; + + StringsBuffer_Clear(&Chat_Log); +} + static char logNameBuffer[STRING_SIZE]; static cc_string logName = String_FromArray(logNameBuffer); static char logPathBuffer[FILENAME_SIZE]; @@ -211,6 +220,15 @@ void Chat_Add(const cc_string* text) { Chat_AddOf(text, MSG_TYPE_NORMAL); } void Chat_AddOf(const cc_string* text, int msgType) { if (msgType == MSG_TYPE_NORMAL) { + /* Check for chat overflow (see issue #837) */ + /* This happens because Offset/Length are packed into a single 32 bit value, */ + /* with 9 bits used for length. Hence if offset exceeds 2^23 (8388608), it */ + /* overflows and earlier chat messages start wrongly appearing instead */ + if (Chat_Log.totalLength > 8388000) { + ClearChatLogs(); + Chat_AddRaw("&cChat log cleared as it hit 8.3 million character limit"); + } + StringsBuffer_Add(&Chat_Log, text); AppendChatLogTime(); AppendChatLog(text); @@ -665,12 +683,7 @@ static void OnFree(void) { ClearCPEMessages(); cmds_head = NULL; - if (Chat_LogTime != defaultLogTimes) Mem_Free(Chat_LogTime); - Chat_LogTime = defaultLogTimes; - logTimesCount = 0; - logTimesCapacity = CHAT_LOGTIMES_DEF_ELEMS; - - StringsBuffer_Clear(&Chat_Log); + ClearChatLogs(); StringsBuffer_Clear(&Chat_InputLog); } From 29d50596fd562f63038d94b1abdc5c9309ec71cf Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 6 Nov 2021 19:33:15 +1100 Subject: [PATCH 2/2] Direct3D11: Fix 30/60/120 limit modes not limiting, fix crash sometimes when closing debug build --- src/Graphics_D3D11.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Graphics_D3D11.c b/src/Graphics_D3D11.c index 825a7c49b..2a4dc394c 100644 --- a/src/Graphics_D3D11.c +++ b/src/Graphics_D3D11.c @@ -54,8 +54,7 @@ static void CreateDeviceAndSwapChain(void) { desc.BufferCount = 1; // todo see if BGRA slightly faster desc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; - desc.BufferDesc.RefreshRate.Numerator = 60; // TODO just leave at 0? check DXGI docs again - desc.BufferDesc.RefreshRate.Denominator = 1; + // RefreshRate intentionally left at 0 so display's refresh rate is used desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; desc.OutputWindow = WindowInfo.Handle; desc.SampleDesc.Count = 1; @@ -94,9 +93,13 @@ void Gfx_Free(void) { Gfx_FreeState(); IDXGISwapChain_Release(swapchain); ID3D11DeviceContext_Release(context); +#ifndef _DEBUG ID3D11Device_Release(device); -#ifdef _DEBUG +#else + ULONG refCount = ID3D11Device_Release(device); + if (refCount == 0) return; // device destroyed with no issues + ID3D11Debug *d3dDebug; static const GUID guid_d3dDebug = { 0x79cf2233, 0x7536, 0x4948,{ 0x9d, 0x36, 0x1e, 0x46, 0x92, 0xdc, 0x57, 0x60 } }; HRESULT hr = ID3D11Device_QueryInterface(device, &guid_d3dDebug, &d3dDebug); @@ -1011,9 +1014,10 @@ finished: } void Gfx_SetFpsLimit(cc_bool vsync, float minFrameMs) { - gfx_vsync = vsync; + gfx_minFrameMs = minFrameMs; + gfx_vsync = vsync; } -void Gfx_BeginFrame(void) { } +void Gfx_BeginFrame(void) { frameStart = Stopwatch_Measure(); } void Gfx_Clear(void) { OM_Clear(); } void Gfx_EndFrame(void) { @@ -1028,6 +1032,7 @@ void Gfx_EndFrame(void) { EndReducedPerformance(); if (hr) Logger_Abort2(hr, "Failed to swap buffers"); + if (gfx_minFrameMs) LimitFPS(); } cc_bool Gfx_WarnIfNecessary(void) { return false; }