Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Better ChatPrint and fix size limit #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 72 additions & 13 deletions lua/sh-tas-utils/betterchatprint.lua
Original file line number Diff line number Diff line change
@@ -1,48 +1,107 @@
local ARGS_LENGTH_BITS = 8
local BOOLEAN_BITS = 1
local COLOUR_CHANNEL_BITS = 8
local NULL_TERMINATOR_BITS = 8

local MAX_MESSAGE_SIZE_BITS = 4 * 1024 * 8
local MAX_ARGS = 2 ^ ARGS_LENGTH_BITS - 1

if SERVER then
util.AddNetworkString("TASUtils.ChatPrint")

local function encodeMsg(args)
for _, v in ipairs(args) do
--- Returns the size of a chat print net message in bits
---@param args any[]
---@return integer
function TASUtils.getChatPrintMessageSizeBits(args)
local size = ARGS_LENGTH_BITS

for i, v in ipairs(args) do
if i > MAX_ARGS then
break
end

size = size + BOOLEAN_BITS

if IsColor(v) then
size = size + COLOUR_CHANNEL_BITS * 3
else
size = size + #tostring(v) * 8 + NULL_TERMINATOR_BITS
end
end

return size
end

local function writeMessage(args)
net.WriteUInt(#args, ARGS_LENGTH_BITS)

for i, v in ipairs(args) do
if i > MAX_ARGS then
break
end

local isColor = IsColor(v)
net.WriteBool(isColor)

if isColor then
net.WriteColor(v, false)
net.WriteUInt(v.r, COLOUR_CHANNEL_BITS)
Vurv78 marked this conversation as resolved.
Show resolved Hide resolved
net.WriteUInt(v.g, COLOUR_CHANNEL_BITS)
net.WriteUInt(v.b, COLOUR_CHANNEL_BITS)
else
net.WriteString(tostring(v))
end
end
end

if net.BytesWritten() > 1024 * 64 then
error("Tried to send too much data", 3)
local function assertMessageSize(args)
local messageSize = TASUtils.getChatPrintMessageSizeBits(args)
if messageSize > MAX_MESSAGE_SIZE_BITS then
error(
string.format(
"Message size %d is greater than maximum of %d bits",
messageSize,
MAX_MESSAGE_SIZE_BITS
),
2
)
end
end

-- Takes a vararg of colours and things to print (each item must have a valid tostring metamethod present)
--- Takes a vararg of colours and things to print (each item must have a valid tostring metamethod)
---@param ... any
function TASUtils.Broadcast(...)
local args = { ... }

assertMessageSize(args)

net.Start("TASUtils.ChatPrint")
net.WriteUInt(#args, 16)
encodeMsg({ ... })
writeMessage(args)
net.Broadcast()
end

FindMetaTable("Player").ChatPrint = function(self, ...)
local args = { ... }

assertMessageSize(args)

net.Start("TASUtils.ChatPrint")
net.WriteUInt(#args, 16)
encodeMsg({ ... })
writeMessage(args)
net.Send(self)
end
else
net.Receive("TASUtils.ChatPrint", function()
local args = {}

for i = 1, net.ReadUInt(16) do
args[i] = net.ReadBool() and net.ReadColor(false)
or net.ReadString()
for i = 1, net.ReadUInt(ARGS_LENGTH_BITS) do
if net.ReadBool() then
args[i] = Color(
net.ReadUInt(COLOUR_CHANNEL_BITS),
net.ReadUInt(COLOUR_CHANNEL_BITS),
net.ReadUInt(COLOUR_CHANNEL_BITS)
)
else
args[i] = net.ReadString()
end
end

-- Display the parsed message
Expand Down