diff --git a/CS2RemoteConsole-client/src/main.cpp b/CS2RemoteConsole-client/src/main.cpp index ec9d1a7..8017e60 100644 --- a/CS2RemoteConsole-client/src/main.cpp +++ b/CS2RemoteConsole-client/src/main.cpp @@ -58,7 +58,7 @@ int main() } tui.init(); - tui.registerChannel(APPLICATION_SPECIAL_CHANNEL_ID, "Log", 4285057279); + tui.registerChannel(APPLICATION_SPECIAL_CHANNEL_ID, "Log", 0xD0D0D0FF, 0x2D0034FF); //light grey and purple tui.setupLoggerCallbackSink(); signal(SIGINT, signalHandler); @@ -79,7 +79,7 @@ int main() vconsole.setOnPRNTReceived([&](const PRNT& PRNT) { - tui.addConsoleMessage(PRNT.channelID, PRNT.message); + tui.addConsoleMessage(PRNT.channelID, PRNT.message, PRNT.color); }); spdlog::info("[Main] Starting {}", application_name); diff --git a/CS2RemoteConsole-client/src/tui/tui.cpp b/CS2RemoteConsole-client/src/tui/tui.cpp index 5a99b46..82b13be 100644 --- a/CS2RemoteConsole-client/src/tui/tui.cpp +++ b/CS2RemoteConsole-client/src/tui/tui.cpp @@ -79,12 +79,14 @@ void TUI::setCommandCallback(std::function callback) m_commandCallback = callback; } -void TUI::addConsoleMessage(int channelId, const std::string& message) -{ +void TUI::addConsoleMessage(int channelId, const std::string& message, uint32_t msgColor) //no background color required, this is only for PRNT as of now, which doesn't deliver background colors anyways +{ std::lock_guard lock(m_consoleMutex); ConsoleMessage cMessage; cMessage.channelId = channelId; + cMessage.color = _byteswap_ulong(msgColor); //byteswapping because apparently message colors have their bytes swapped relative to channel colors...Valve... cMessage.message = message; + cMessage.timestamp = std::chrono::system_clock::now(); m_consoleMessages.push_back(cMessage); @@ -95,7 +97,7 @@ void TUI::addConsoleMessage(int channelId, const std::string& message) m_consoleDirty = true; } -void TUI::registerChannel(int id, const std::string& name, uint32_t color) +void TUI::registerChannel(int id, const std::string& name, uint32_t color, uint32_t backgroundColor) { std::lock_guard lock(m_channelsMutex); @@ -110,10 +112,7 @@ void TUI::registerChannel(int id, const std::string& name, uint32_t color) { if (m_nextColorPairId < COLOR_PAIRS) { - short colorPairId = m_nextColorPairId++; - initializeColor(color, colorPairId); - m_colorCache[color] = colorPairId; - channel.colorPairId = colorPairId; + channel.colorPairId = initializeColor(color, backgroundColor); } else { @@ -173,21 +172,25 @@ void TUI::drawConsoleWindow() std::string prefix; short colorPairId = 0; + + if (channelIt != m_channels.end()) + { + colorPairId = channelIt->second.colorPairId; + prefix = "[" + channelIt->second.name + "] "; + } + else + { + prefix = "[Unknown] "; + } + if (currentMessage.channelId == APPLICATION_SPECIAL_CHANNEL_ID) { prefix = ""; - colorPairId = m_colorCache[4285057279]; } - else if (channelIt != m_channels.end()) + if (currentMessage.color) { - const auto& channel = channelIt->second; - prefix = "[" + channel.name + "] "; - colorPairId = channel.colorPairId; - } - else - { - prefix = "[Unknown] "; + colorPairId = initializeColor(currentMessage.color); } if (colorPairId != 0) @@ -353,21 +356,49 @@ short TUI::mapTo256Color(uint32_t color) round(b / 255.0 * 5)); } -void TUI::initializeColor(uint32_t color, short& colorPairId) +short TUI::initializeColor(uint32_t color, uint32_t backgroundColor) { + long long combinedColor = (((long long)color) << 32) | (backgroundColor & 0xffffffffL); + + short colorPairId = 0; + int r = (color >> 24) & 0xFF; int g = (color >> 16) & 0xFF; int b = (color >> 8) & 0xFF; - if (m_useExtendedColors) + int br = (backgroundColor >> 24) & 0xFF; + int bg = (backgroundColor >> 16) & 0xFF; + int bb = (backgroundColor >> 8) & 0xFF; + + if (m_colorCache.find(combinedColor) == m_colorCache.end()) { - int colorNumber = EXTENDED_COLOR_BASE + colorPairId; - init_color(colorNumber, r * 1000 / 255, g * 1000 / 255, b * 1000 / 255); - init_pair(colorPairId, colorNumber, -1); + colorPairId = m_nextColorPairId++; + if (m_useExtendedColors) + { + int colorNumber = EXTENDED_COLOR_BASE + m_nextColorId++; + int bColorNumber = -1; + if (backgroundColor) + bColorNumber = EXTENDED_COLOR_BASE + m_nextColorId++; + init_color(colorNumber, r * 1000 / 255, g * 1000 / 255, b * 1000 / 255); + init_color(bColorNumber, br * 1000 / 255, bg * 1000 / 255, bb * 1000 / 255); + color_content(colorNumber, (short*)&r, (short*)&g, (short*)&b); + init_pair(colorPairId, colorNumber, bColorNumber); + } + else + { + short nearestColor = mapTo256Color(color); + short bNearestColor = -1; + if (backgroundColor) + bNearestColor = mapTo256Color(backgroundColor); + init_pair(colorPairId, nearestColor, bNearestColor); + } + m_colorCache[combinedColor] = colorPairId; } else { - short nearestColor = mapTo256Color(color); - init_pair(colorPairId, nearestColor, -1); + + colorPairId = m_colorCache.find(combinedColor)->second; } + + return colorPairId; } diff --git a/CS2RemoteConsole-client/src/tui/tui.h b/CS2RemoteConsole-client/src/tui/tui.h index 9afc25f..e53474b 100644 --- a/CS2RemoteConsole-client/src/tui/tui.h +++ b/CS2RemoteConsole-client/src/tui/tui.h @@ -30,6 +30,7 @@ struct ConsoleChannel struct ConsoleMessage { int channelId; + uint32_t color; std::string message; std::chrono::system_clock::time_point timestamp; }; @@ -45,8 +46,8 @@ class TUI void shutdown(); void setCommandCallback(std::function callback); - void addConsoleMessage(int channelId, const std::string& message); - void registerChannel(int id, const std::string& name, uint32_t color); + void addConsoleMessage(int channelId, const std::string& message, uint32_t msgColor = 0); + void registerChannel(int id, const std::string& name, uint32_t color, uint32_t backgroundColor = 0); void setConsoleDirty(bool dirty); void setupLoggerCallbackSink(); @@ -55,8 +56,9 @@ class TUI static const int MOUSE_SCROLL_SPEED = 3; // Scroll speed multiplier for mouse wheel static const int EXTENDED_COLOR_BASE = 256; - std::unordered_map m_colorCache; + std::unordered_map m_colorCache; short m_nextColorPairId = 1; + int m_nextColorId = 1; bool m_useExtendedColors; WINDOW* m_consoleWindow; @@ -88,7 +90,7 @@ class TUI void scrollConsole(int direction); short mapTo256Color(uint32_t color); - void initializeColor(uint32_t color, short& colorPairId); + short initializeColor(uint32_t color, uint32_t backgroundColor = 0); }; #endif // TUI_H diff --git a/libvconsole/src/messages.h b/libvconsole/src/messages.h index 8c7939a..7b69e08 100644 --- a/libvconsole/src/messages.h +++ b/libvconsole/src/messages.h @@ -42,7 +42,8 @@ struct CHAN { struct PRNT { int32_t channelID; - uint8_t unknown[24]; + uint8_t unknown[20]; + uint32_t color; std::string message; }; diff --git a/libvconsole/src/vconsole.cpp b/libvconsole/src/vconsole.cpp index 4fc205a..21a9562 100644 --- a/libvconsole/src/vconsole.cpp +++ b/libvconsole/src/vconsole.cpp @@ -269,7 +269,8 @@ PRNT VConsole::parsePRNT(const std::vector& chunkBuf) PRNT prnt; const char* data = chunkBuf.data() + sizeof(VConChunk); prnt.channelID = ntohl(*reinterpret_cast(data)); - memcpy(prnt.unknown, data + 4, 24); + memcpy(prnt.unknown, data + 4, 20); + memcpy(&prnt.color, data + 12, 4); prnt.message = std::string(data + 28); prnt.message = stripNonAscii(prnt.message); // Strip non-ASCII characters return prnt;