Skip to content

Commit

Permalink
fix(conhost): no unbounded growth of console buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
blattersturm committed Jun 14, 2021
1 parent 1477bde commit 16491b6
Showing 1 changed file with 25 additions and 38 deletions.
63 changes: 25 additions & 38 deletions code/components/conhost-v2/src/ConsoleHostGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include <se/Security.h>

#include <boost/circular_buffer.hpp>
#include <concurrent_queue.h>

bool IsNonProduction();
Expand Down Expand Up @@ -98,8 +99,8 @@ static CRGBA HSLToRGB(HSL hsl) {

struct FiveMConsoleBase
{
ImVector<char*> Items;
ImVector<char*> ItemKeys;
boost::circular_buffer<std::string> Items{ 2500 };
boost::circular_buffer<std::string> ItemKeys{ 2500 };

std::recursive_mutex ItemsMutex;

Expand All @@ -119,9 +120,8 @@ struct FiveMConsoleBase

{
std::unique_lock<std::recursive_mutex> lock(ItemsMutex);

ItemKeys.push_back(Strdup(key));
Items.push_back(Strdup(buf));
ItemKeys.push_back(key);
Items.push_back(buf);

OnAddLog(key, buf);
}
Expand All @@ -146,26 +146,25 @@ struct FiveMConsoleBase

virtual void DrawItem(int i, float alpha = 1.0f, bool fullBg = false)
{
const char* item = Items[i];
const char* key = ItemKeys[i];
const auto& item = Items[i];
auto key = ItemKeys[i];

if (strlen(key) > 0 && strlen(item) > 0)
if (strlen(key.c_str()) > 0 && strlen(item.c_str()) > 0)
{
ImGui::PushFont(consoleFontSmall);

auto hue = int{ HashRageString(key) % 360 };
auto hue = int{ HashRageString(key.c_str()) % 360 };
auto color = HSLToRGB(HSL{ hue, 0.8f, 0.4f });
color.alpha = alpha * 255.0f;

std::string refStr;

if (fullBg)
{
refStr = fmt::sprintf("[%s]:", key);
key = refStr.c_str();
key = fmt::sprintf("[%s]:", key);
}

auto textSize = ImGui::CalcTextSize(key);
auto textSize = ImGui::CalcTextSize(key.c_str());

auto dl = ImGui::GetCurrentContext()->CurrentWindow->DrawList;
auto startPos = ImGui::GetCursorScreenPos();
Expand All @@ -174,7 +173,7 @@ struct FiveMConsoleBase

if (fullBg)
{
itemSize = ImGui::CalcTextSize(item);
itemSize = ImGui::CalcTextSize(item.c_str());
itemSize.x += 8.0f;

color.alpha *= 0.8f;
Expand All @@ -184,7 +183,7 @@ struct FiveMConsoleBase
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + 4.0f);

//ImGui::PushStyleColor(ImGuiCol_Text, col);
ImGui::TextUnformatted(key);
ImGui::TextUnformatted(key.c_str());
//ImGui::PopStyleColor();

ImGui::PopFont();
Expand All @@ -194,23 +193,19 @@ struct FiveMConsoleBase

ImVec4 col = ImVec4(1.0f, 1.0f, 1.0f, alpha);

if (strstr(item, "[error]")) col = ImColor(1.0f, 0.4f, 0.4f, alpha);
else if (strncmp(item, "# ", 2) == 0) col = ImColor(1.0f, 0.78f, 0.58f, alpha);
if (strstr(item.c_str(), "[error]"))
col = ImColor(1.0f, 0.4f, 0.4f, alpha);
else if (strncmp(item.c_str(), "# ", 2) == 0)
col = ImColor(1.0f, 0.78f, 0.58f, alpha);
ImGui::PushStyleColor(ImGuiCol_Text, col);
ImGui::TextUnformatted(item);
ImGui::TextUnformatted(item.c_str());
ImGui::PopStyleColor();
}

virtual void ClearLog()
{
std::unique_lock<std::recursive_mutex> lock(ItemsMutex);

for (int i = 0; i < Items.Size; i++)
{
free(Items[i]);
free(ItemKeys[i]);
}

Items.clear();
ItemKeys.clear();
}
Expand Down Expand Up @@ -332,7 +327,7 @@ struct CfxBigConsole : FiveMConsoleBase
// and appending newly elements as they are inserted. This is left as a task to the user until we can manage to improve this example code!
// If your items are of variable size you may want to implement code similar to what ImGuiListClipper does. Or split your data into fixed height items to allow random-seeking into your list.
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(4, 1)); // Tighten spacing
ImGuiListClipper clipper(Items.Size);
ImGuiListClipper clipper(Items.size());
while (clipper.Step())
{
for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++)
Expand Down Expand Up @@ -636,26 +631,20 @@ struct MiniConsole : CfxBigConsole
{
while ((ItemTimes.size() > 1 && (t - ItemTimes[0]) > std::chrono::seconds(10)) || Items.size() > 14)
{
free(Items[0]);
free(ItemKeys[0]);

Items.erase(Items.begin());
ItemKeys.erase(ItemKeys.begin());
ItemTimes.erase(ItemTimes.begin());
}

if (ItemTimes.size() == 1 && ((t - ItemTimes[0]) > std::chrono::seconds(10)))
{
free(Items[0]);
free(ItemKeys[0]);

Items[0] = Strdup("");
ItemKeys[0] = Strdup("");
Items[0] = "";
ItemKeys[0] = "";
ItemTimes[0] = t;
}
}

ImGuiListClipper clipper(Items.Size);
ImGuiListClipper clipper(Items.size());
while (clipper.Step())
{
for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++)
Expand Down Expand Up @@ -809,7 +798,7 @@ void SendPrintMessage(const std::string& channel, const std::string& message)

for (auto& console : g_consoles)
{
if (console->Items.Size == 0)
if (console->Items.size() == 0)
{
console->AddLog("", "");
}
Expand All @@ -829,11 +818,9 @@ void SendPrintMessage(const std::string& channel, const std::string& message)
{
std::unique_lock<std::recursive_mutex> lock(console->ItemsMutex);

auto strRef = FiveMConsoleBase::Strdup((console->Items[console->Items.size() - 1] + str).c_str());
auto strRef = (console->Items[console->Items.size() - 1] + str);
std::swap(console->Items[console->Items.size() - 1], strRef);

free(strRef);

console->UpdateLog(console->Items.size() - 1);
}
}
Expand Down Expand Up @@ -890,7 +877,7 @@ static InitFunction initFunction([]()
{
std::unique_lock<std::recursive_mutex> lock(g_consoles[1]->ItemsMutex);

*should = *should || ((g_consoles[1]->Items.size() > 1) || (g_consoles[1]->Items[0] && *g_consoles[1]->Items[0]));
*should = *should || ((g_consoles[1]->Items.size() > 1) || (!g_consoles[1]->Items[0].empty()));
}
}, 999);

Expand Down

0 comments on commit 16491b6

Please sign in to comment.