hijack OnPlayerChat and add new hook (PrePlayerChat)

This commit is contained in:
Cynthia Foxwell 2023-12-18 21:19:00 -07:00
parent 682cfba243
commit 387d2606cb
4 changed files with 87 additions and 11 deletions

View File

@ -1,5 +1,9 @@
local CHATPRINT_COLOR = CreateClientConVar("cbox_chatprint_color", "151 211 255", true, false, "Color for system messages")
local JOINLEAVE_COLOR = CreateClientConVar("cbox_joinleave_color", "161 255 161", true, false, "Color for join/leave messages")
local TEAM_COLOR = CreateClientConVar("cbox_team_color", "24 161 35", true, false, "Color for the team tag")
local DEAD_COLOR = CreateClientConVar("cbox_dead_color", "255 24 35", true, false, "Color for the dead tag")
local color_white = Color(255, 255, 255)
local hookTable = {}
if cbox.hooks then
@ -16,6 +20,7 @@ end
---@alias ValidHooks
---| '"PreChatAddText"' # Runs before the true chat.AddText is called, allows modification to arguments
---| '"PrePlayerChat"' # Runs before player chat calls chat.AddText
---Adds a hook
---@param name ValidHooks
@ -34,7 +39,6 @@ function cbox.hooks.Remove(name, identifier)
hookTable[name][identifier] = nil
end
cbox.detours = cbox.detours or {}
cbox.detours.chat_AddText = cbox.detours.chat_AddText or chat.AddText
@ -77,3 +81,67 @@ hook.Add("ChatText", "cbox.chattext", function(index, name, text, type)
return true
end)
local function OverrideOnPlayerChat()
cbox.detours.OnPlayerChat = cbox.detours.OnPlayerChat or GAMEMODE.OnPlayerChat
function GAMEMODE:OnPlayerChat(ply, text, is_team, is_dead, ...)
local oldChatAddText = chat.AddText
local args = {}
chat.AddText = function(...)
args = {...}
end
cbox.detours.OnPlayerChat(self, ply, text, is_team, is_dead, ...)
chat.AddText = oldChatAddText
local new_args = {}
for i, arg in ipairs(args) do
new_args[i] = arg
if isstring(arg) then
if arg == "*DEAD* " then
new_args[i - 1] = cbox.utils.ParseColorStringToColor(DEAD_COLOR:GetString())
elseif arg == "(TEAM) " then
new_args[i - 1] = cbox.utils.ParseColorStringToColor(TEAM_COLOR:GetString())
elseif arg == "Console" then
table.insert(new_args, i, Color(160, 160, 160))
elseif arg:StartsWith(": ") and #arg > 2 then
local text = arg:sub(3)
new_args[i] = ": "
table.insert(new_args, i + 1, text)
end
end
end
local hooks = hookTable.PrePlayerChat or {}
for id, callback in next, hooks do
local function catch(err)
cbox.utils.RealmError(("Failed to run callback for PrePlayerChat hook %q:"):format(id), err)
end
local ok, ret = xpcall(callback, catch, new_args)
if not ok then continue end
if ret ~= nil then
if not istable(ret) then
cbox.utils.RealmError(("Got return for PrePlayerChat hook %q, but it was not a table."):format(id))
continue
end
new_args = ret
end
end
chat.AddText(unpack(new_args))
return true
end
end
hook.Add("Initialize", "cbox.onplayerchat", function()
OverrideOnPlayerChat()
end)
if GAMEMODE then
OverrideOnPlayerChat()
end

View File

@ -7,11 +7,9 @@ cbox.hooks.Add("PreChatAddText", "cbox.greentext", function(args)
if not ENABLED:GetBool() then return end
for i, arg in ipairs(args) do
if isstring(arg) and arg:StartsWith(": >") then
args[i] = arg:sub(3)
local prevArg = args[i - 1]
if isstring(prevArg) and prevArg == ": " and isstring(arg) and arg:StartsWith(">") then
table.insert(args, i, cbox.utils.ParseColorStringToColor(COLOR:GetString()))
table.insert(args, i, ": ")
table.insert(args, i, color_white)
break
end
end

View File

@ -8,10 +8,7 @@ local ALL_MESSAGES = CreateClientConVar("cbox_timestamps_all", "1", true, false,
local color_white = Color(255, 255, 255)
cbox.hooks.Add("PreChatAddText", "cbox.timestamps", function(args)
if not ENABLED:GetBool() then return end
if not ALL_MESSAGES:GetBool() then return end
local function MakeTimestamp(args)
local use24 = TRUETIME:GetBool()
local stamp = ""
@ -53,4 +50,18 @@ cbox.hooks.Add("PreChatAddText", "cbox.timestamps", function(args)
end
return new_args
end
cbox.hooks.Add("PreChatAddText", "cbox.timestamps", function(args)
if not ENABLED:GetBool() then return end
if not ALL_MESSAGES:GetBool() then return end
return MakeTimestamp(args)
end)
cbox.hooks.Add("PrePlayerChat", "cbox.timestamps", function(args)
if not ENABLED:GetBool() then return end
if ALL_MESSAGES:GetBool() then return end
return MakeTimestamp(args)
end)

View File

@ -115,8 +115,7 @@ hook.Add("OnChatAddText", "cbox.chatbox.history", function(args)
history:InsertColorChange(col.r, col.g, col.b, 255)
history:AppendText(arg:Name())
else
local col = cbox.utils.colors.BRAND
history:InsertColorChange(col.r, col.g, col.b, 255)
history:InsertColorChange(160, 160, 160, 255)
history:AppendText("???")
end
end