Skip to content

Commit fb3f5ba

Browse files
authored
Channel Colors Refactor
refactor: made it possible to set a background color in initializeColor() and color cache accordingly feat: non-game console output is now a different color refactor: moved byteswap to tui addConsoleMessage
2 parents 1ee229c + 5266cbc commit fb3f5ba

File tree

5 files changed

+66
-31
lines changed

5 files changed

+66
-31
lines changed

CS2RemoteConsole-client/src/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ int main()
5858
}
5959

6060
tui.init();
61-
tui.registerChannel(APPLICATION_SPECIAL_CHANNEL_ID, "Log", 4285057279);
61+
tui.registerChannel(APPLICATION_SPECIAL_CHANNEL_ID, "Log", 0xD0D0D0FF, 0x2D0034FF); //light grey and purple
6262
tui.setupLoggerCallbackSink();
6363

6464
signal(SIGINT, signalHandler);
@@ -79,7 +79,7 @@ int main()
7979

8080
vconsole.setOnPRNTReceived([&](const PRNT& PRNT)
8181
{
82-
tui.addConsoleMessage(PRNT.channelID, PRNT.message);
82+
tui.addConsoleMessage(PRNT.channelID, PRNT.message, PRNT.color);
8383
});
8484

8585
spdlog::info("[Main] Starting {}", application_name);

CS2RemoteConsole-client/src/tui/tui.cpp

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,14 @@ void TUI::setCommandCallback(std::function<void(const std::string&)> callback)
7979
m_commandCallback = callback;
8080
}
8181

82-
void TUI::addConsoleMessage(int channelId, const std::string& message)
83-
{
82+
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
83+
{
8484
std::lock_guard<std::mutex> lock(m_consoleMutex);
8585
ConsoleMessage cMessage;
8686
cMessage.channelId = channelId;
87+
cMessage.color = _byteswap_ulong(msgColor); //byteswapping because apparently message colors have their bytes swapped relative to channel colors...Valve...
8788
cMessage.message = message;
89+
8890
cMessage.timestamp = std::chrono::system_clock::now();
8991

9092
m_consoleMessages.push_back(cMessage);
@@ -95,7 +97,7 @@ void TUI::addConsoleMessage(int channelId, const std::string& message)
9597
m_consoleDirty = true;
9698
}
9799

98-
void TUI::registerChannel(int id, const std::string& name, uint32_t color)
100+
void TUI::registerChannel(int id, const std::string& name, uint32_t color, uint32_t backgroundColor)
99101
{
100102
std::lock_guard<std::mutex> lock(m_channelsMutex);
101103

@@ -110,10 +112,7 @@ void TUI::registerChannel(int id, const std::string& name, uint32_t color)
110112
{
111113
if (m_nextColorPairId < COLOR_PAIRS)
112114
{
113-
short colorPairId = m_nextColorPairId++;
114-
initializeColor(color, colorPairId);
115-
m_colorCache[color] = colorPairId;
116-
channel.colorPairId = colorPairId;
115+
channel.colorPairId = initializeColor(color, backgroundColor);
117116
}
118117
else
119118
{
@@ -173,21 +172,25 @@ void TUI::drawConsoleWindow()
173172

174173
std::string prefix;
175174
short colorPairId = 0;
175+
176+
if (channelIt != m_channels.end())
177+
{
178+
colorPairId = channelIt->second.colorPairId;
179+
prefix = "[" + channelIt->second.name + "] ";
180+
}
181+
else
182+
{
183+
prefix = "[Unknown] ";
184+
}
185+
176186

177187
if (currentMessage.channelId == APPLICATION_SPECIAL_CHANNEL_ID)
178188
{
179189
prefix = "";
180-
colorPairId = m_colorCache[4285057279];
181190
}
182-
else if (channelIt != m_channels.end())
191+
if (currentMessage.color)
183192
{
184-
const auto& channel = channelIt->second;
185-
prefix = "[" + channel.name + "] ";
186-
colorPairId = channel.colorPairId;
187-
}
188-
else
189-
{
190-
prefix = "[Unknown] ";
193+
colorPairId = initializeColor(currentMessage.color);
191194
}
192195

193196
if (colorPairId != 0)
@@ -353,21 +356,49 @@ short TUI::mapTo256Color(uint32_t color)
353356
round(b / 255.0 * 5));
354357
}
355358

356-
void TUI::initializeColor(uint32_t color, short& colorPairId)
359+
short TUI::initializeColor(uint32_t color, uint32_t backgroundColor)
357360
{
361+
long long combinedColor = (((long long)color) << 32) | (backgroundColor & 0xffffffffL);
362+
363+
short colorPairId = 0;
364+
358365
int r = (color >> 24) & 0xFF;
359366
int g = (color >> 16) & 0xFF;
360367
int b = (color >> 8) & 0xFF;
361368

362-
if (m_useExtendedColors)
369+
int br = (backgroundColor >> 24) & 0xFF;
370+
int bg = (backgroundColor >> 16) & 0xFF;
371+
int bb = (backgroundColor >> 8) & 0xFF;
372+
373+
if (m_colorCache.find(combinedColor) == m_colorCache.end())
363374
{
364-
int colorNumber = EXTENDED_COLOR_BASE + colorPairId;
365-
init_color(colorNumber, r * 1000 / 255, g * 1000 / 255, b * 1000 / 255);
366-
init_pair(colorPairId, colorNumber, -1);
375+
colorPairId = m_nextColorPairId++;
376+
if (m_useExtendedColors)
377+
{
378+
int colorNumber = EXTENDED_COLOR_BASE + m_nextColorId++;
379+
int bColorNumber = -1;
380+
if (backgroundColor)
381+
bColorNumber = EXTENDED_COLOR_BASE + m_nextColorId++;
382+
init_color(colorNumber, r * 1000 / 255, g * 1000 / 255, b * 1000 / 255);
383+
init_color(bColorNumber, br * 1000 / 255, bg * 1000 / 255, bb * 1000 / 255);
384+
color_content(colorNumber, (short*)&r, (short*)&g, (short*)&b);
385+
init_pair(colorPairId, colorNumber, bColorNumber);
386+
}
387+
else
388+
{
389+
short nearestColor = mapTo256Color(color);
390+
short bNearestColor = -1;
391+
if (backgroundColor)
392+
bNearestColor = mapTo256Color(backgroundColor);
393+
init_pair(colorPairId, nearestColor, bNearestColor);
394+
}
395+
m_colorCache[combinedColor] = colorPairId;
367396
}
368397
else
369398
{
370-
short nearestColor = mapTo256Color(color);
371-
init_pair(colorPairId, nearestColor, -1);
399+
400+
colorPairId = m_colorCache.find(combinedColor)->second;
372401
}
402+
403+
return colorPairId;
373404
}

CS2RemoteConsole-client/src/tui/tui.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ struct ConsoleChannel
3030
struct ConsoleMessage
3131
{
3232
int channelId;
33+
uint32_t color;
3334
std::string message;
3435
std::chrono::system_clock::time_point timestamp;
3536
};
@@ -45,8 +46,8 @@ class TUI
4546
void shutdown();
4647

4748
void setCommandCallback(std::function<void(const std::string&)> callback);
48-
void addConsoleMessage(int channelId, const std::string& message);
49-
void registerChannel(int id, const std::string& name, uint32_t color);
49+
void addConsoleMessage(int channelId, const std::string& message, uint32_t msgColor = 0);
50+
void registerChannel(int id, const std::string& name, uint32_t color, uint32_t backgroundColor = 0);
5051
void setConsoleDirty(bool dirty);
5152
void setupLoggerCallbackSink();
5253

@@ -55,8 +56,9 @@ class TUI
5556
static const int MOUSE_SCROLL_SPEED = 3; // Scroll speed multiplier for mouse wheel
5657
static const int EXTENDED_COLOR_BASE = 256;
5758

58-
std::unordered_map<uint32_t, short> m_colorCache;
59+
std::unordered_map<long long, short> m_colorCache;
5960
short m_nextColorPairId = 1;
61+
int m_nextColorId = 1;
6062
bool m_useExtendedColors;
6163

6264
WINDOW* m_consoleWindow;
@@ -88,7 +90,7 @@ class TUI
8890
void scrollConsole(int direction);
8991

9092
short mapTo256Color(uint32_t color);
91-
void initializeColor(uint32_t color, short& colorPairId);
93+
short initializeColor(uint32_t color, uint32_t backgroundColor = 0);
9294
};
9395

9496
#endif // TUI_H

libvconsole/src/messages.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ struct CHAN {
4242

4343
struct PRNT {
4444
int32_t channelID;
45-
uint8_t unknown[24];
45+
uint8_t unknown[20];
46+
uint32_t color;
4647
std::string message;
4748
};
4849

libvconsole/src/vconsole.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,8 @@ PRNT VConsole::parsePRNT(const std::vector<char>& chunkBuf)
269269
PRNT prnt;
270270
const char* data = chunkBuf.data() + sizeof(VConChunk);
271271
prnt.channelID = ntohl(*reinterpret_cast<const int32_t*>(data));
272-
memcpy(prnt.unknown, data + 4, 24);
272+
memcpy(prnt.unknown, data + 4, 20);
273+
memcpy(&prnt.color, data + 12, 4);
273274
prnt.message = std::string(data + 28);
274275
prnt.message = stripNonAscii(prnt.message); // Strip non-ASCII characters
275276
return prnt;

0 commit comments

Comments
 (0)