From 5209a9213f209b5c0757b16702de9448cbdd4151 Mon Sep 17 00:00:00 2001 From: Earu Date: Sun, 31 Jul 2022 12:30:25 +0200 Subject: [PATCH] cursed code for a cursed problem --- lua/easychat/chathud.lua | 2 +- lua/easychat/easychat.lua | 57 ++++++++++++++++++++---- lua/easychat/modules/client/mentions.lua | 2 +- lua/easychat/modules/dm_tab.lua | 2 +- lua/easychat/modules/indications.lua | 4 +- 5 files changed, 54 insertions(+), 13 deletions(-) diff --git a/lua/easychat/chathud.lua b/lua/easychat/chathud.lua index e91262d..9e5d8ec 100644 --- a/lua/easychat/chathud.lua +++ b/lua/easychat/chathud.lua @@ -1400,7 +1400,7 @@ function chathud:AddText(...) self:InsertColorChange(team_col.r, team_col.g, team_col.b) if EC_PLAYER_PASTEL:GetBool() then - local nick = EasyChat.GetProperNick(arg) + local nick = ply:Nick() local pastel_col = EasyChat.PastelizeNick(nick) self:InsertColorChange(pastel_col.r, pastel_col.g, pastel_col.b) end diff --git a/lua/easychat/easychat.lua b/lua/easychat/easychat.lua index 49d3186..c23cfee 100644 --- a/lua/easychat/easychat.lua +++ b/lua/easychat/easychat.lua @@ -140,7 +140,48 @@ function EasyChat.GetProperNick(ply) return ec_markup.GetText(ply_nick, true) end -PLY.Nick = EasyChat.GetProperNick +local wrappers = {} +local wrapper_addr +local function make_nick_override_wrapper() + local native_nick = EasyChat.NativeNick + local function wrapper(ply) + local fn_addr = tostring(wrapper) + if not wrappers[fn_addr] then return native_nick(ply) end + + return EasyChat.GetProperNick(ply) + end + + wrapper_addr = tostring(wrapper) + wrappers[wrapper_addr] = true + + return wrapper +end + +local function rich_nick_wrapper(ply) + return EasyChat.NativeNick(ply) +end + +local clean_name_fns = { "Nick", "Name", "GetName", "GetNick" } +local tagged_name_fns = { "RichNick", "RichName", "GetRichName", "GetRichNick", "NickDecorated", "NameDecorated", "GetNameDecorated", "GetNickDecorated" } +local function check_nick_override_wrapper_status() + if wrapper_addr and wrapper_addr ~= tostring(PLY.Nick) then + wrappers[wrapper_addr] = nil + EasyChat.NativeNick = PLY.Nick + + local new_wrapper = make_nick_override_wrapper() + for _, fn_name in ipairs(clean_name_fns) do + PLY[fn_name] = new_wrapper + end + + for _, fn_name in ipairs(tagged_name_fns) do + PLY[fn_name] = rich_nick_wrapper + end + end + + timer.Simple(1, check_nick_override_wrapper_status) +end + +PLY.Nick = make_nick_override_wrapper() PLY.Name = PLY.Nick PLY.GetName = PLY.Nick PLY.GetNick = PLY.Nick @@ -149,9 +190,7 @@ PLY.RealNick = PLY.EngineNick PLY.RealName = PLY.EngineNick PLY.GetRealName = PLY.EngineNick -function PLY:RichNick() - return EasyChat.NativeNick(self) -end +PLY.RichNick = rich_nick_wrapper PLY.RichName = PLY.RichNick PLY.GetRichNick = PLY.RichNick PLY.GetRichName = PLY.RichNick @@ -160,6 +199,8 @@ PLY.GetNickDecorated = PLY.RichNick PLY.NickDecorated = PLY.RichNick PLY.NameDecorated = PLY.RichNick +timer.Simple(1, check_nick_override_wrapper_status) + local load_modules, get_modules = include("easychat/autoloader.lua") EasyChat.GetModules = get_modules -- maybe useful for modules? @@ -1090,7 +1131,7 @@ if CLIENT then end function EasyChat.UploadToImgur(img_base64, callback) - local ply_nick, ply_steamid = EasyChat.GetProperNick(LocalPlayer()), LocalPlayer():SteamID() + local ply_nick, ply_steamid = LocalPlayer():Nick(), LocalPlayer():SteamID() local params = { image = img_base64, type = "base64", @@ -1496,7 +1537,7 @@ if CLIENT then append_text(richtext, get_unknown_name(arg)) else local ply_col = EC_PLAYER_COLOR:GetBool() and team.GetColor(arg:Team()) or color_white - local nick = EasyChat.GetProperNick(arg) + local nick = arg:Nick() if EC_PLAYER_PASTEL:GetBool() then ply_col = EasyChat.PastelizeNick(nick) end @@ -1851,7 +1892,7 @@ if CLIENT then end local ply_col = EC_PLAYER_COLOR:GetBool() and team.GetColor(ply:Team()) or color_white - local stripped_ply_nick = EasyChat.GetProperNick(ply) + local stripped_ply_nick = ply:Nick() if EC_PLAYER_PASTEL:GetBool() then ply_col = EasyChat.PastelizeNick(stripped_ply_nick) end @@ -2129,7 +2170,7 @@ if CLIENT then local max_perc = 0 local res for _, ply in ipairs(player.GetAll()) do - local nick = EasyChat.GetProperNick(ply) + local nick = ply:Nick() local match = nick:lower():match(last_word:lower():PatternSafe()) if match and not text:EndsWith(nick) then local perc = #match / #nick diff --git a/lua/easychat/modules/client/mentions.lua b/lua/easychat/modules/client/mentions.lua index c370b8c..b972211 100644 --- a/lua/easychat/modules/client/mentions.lua +++ b/lua/easychat/modules/client/mentions.lua @@ -232,7 +232,7 @@ function mentions:IsMention(msg) local stripped_msg = ec_markup.GetText(msg):lower() if filter_match(stripped_msg) then return true end - local ply_name = EasyChat.GetProperNick(LocalPlayer()):lower():PatternSafe() + local ply_name = LocalPlayer():Nick():lower():PatternSafe() local nick_mention = stripped_msg:match(ply_name) local is_nick_match = not stripped_msg:match("^[%!%.%/]") and nick_mention return is_nick_match and #nick_mention > 1 diff --git a/lua/easychat/modules/dm_tab.lua b/lua/easychat/modules/dm_tab.lua index c2e8c0c..e785a2d 100644 --- a/lua/easychat/modules/dm_tab.lua +++ b/lua/easychat/modules/dm_tab.lua @@ -140,7 +140,7 @@ if CLIENT then local chat = { Player = ply, - Name = EasyChat.GetProperNick(ply), + Name = ply:Nick(), RichText = richtext, NewMessages = 0 } diff --git a/lua/easychat/modules/indications.lua b/lua/easychat/modules/indications.lua index ce500cb..193343a 100644 --- a/lua/easychat/modules/indications.lua +++ b/lua/easychat/modules/indications.lua @@ -72,7 +72,7 @@ if CLIENT then end, player = function(obj) return { - Name = EasyChat.GetProperNick(obj), + Name = obj:Nick(), Entity = obj, } end, @@ -118,7 +118,7 @@ if CLIENT then end end - local ply_name = EasyChat.GetProperNick(ply) + local ply_name = ply:Nick() local hook_name = TAG .. ply_name indicate_pos = data.WorldPos and data.WorldPos or indicate_pos if indicate_pos then