Skip to content

Commit

Permalink
Channel Colors Refactor
Browse files Browse the repository at this point in the history
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
  • Loading branch information
theokyr authored Jul 6, 2024
2 parents 1ee229c + 5266cbc commit fb3f5ba
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 31 deletions.
4 changes: 2 additions & 2 deletions CS2RemoteConsole-client/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
77 changes: 54 additions & 23 deletions CS2RemoteConsole-client/src/tui/tui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,14 @@ void TUI::setCommandCallback(std::function<void(const std::string&)> 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<std::mutex> 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);
Expand All @@ -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<std::mutex> lock(m_channelsMutex);

Expand All @@ -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
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
10 changes: 6 additions & 4 deletions CS2RemoteConsole-client/src/tui/tui.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct ConsoleChannel
struct ConsoleMessage
{
int channelId;
uint32_t color;
std::string message;
std::chrono::system_clock::time_point timestamp;
};
Expand All @@ -45,8 +46,8 @@ class TUI
void shutdown();

void setCommandCallback(std::function<void(const std::string&)> 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();

Expand All @@ -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<uint32_t, short> m_colorCache;
std::unordered_map<long long, short> m_colorCache;
short m_nextColorPairId = 1;
int m_nextColorId = 1;
bool m_useExtendedColors;

WINDOW* m_consoleWindow;
Expand Down Expand Up @@ -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
3 changes: 2 additions & 1 deletion libvconsole/src/messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
3 changes: 2 additions & 1 deletion libvconsole/src/vconsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ PRNT VConsole::parsePRNT(const std::vector<char>& chunkBuf)
PRNT prnt;
const char* data = chunkBuf.data() + sizeof(VConChunk);
prnt.channelID = ntohl(*reinterpret_cast<const int32_t*>(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;
Expand Down

0 comments on commit fb3f5ba

Please sign in to comment.