1
0
mirror of https://github.com/Earu/EasyChat.git synced 2025-03-04 03:13:20 -05:00

Allow markup on server

This commit is contained in:
Earu 2021-12-09 23:01:40 +01:00
parent acab042792
commit 54ad944cbe
7 changed files with 118 additions and 102 deletions

View File

@ -1,17 +1,17 @@
AddCSLuaFile("easychat/networking.lua")
AddCSLuaFile("easychat/server_config.lua")
AddCSLuaFile("easychat/migrations.lua")
AddCSLuaFile("easychat/unicode_transliterator.lua")
AddCSLuaFile("easychat/chathud.lua")
AddCSLuaFile("easychat/markup.lua")
AddCSLuaFile("easychat/easychat.lua")
AddCSLuaFile("easychat/autoloader.lua")
AddCSLuaFile("easychat/engine_chat_hack.lua")
AddCSLuaFile("easychat/client/font_extensions.lua")
AddCSLuaFile("easychat/client/blur_panel.lua")
AddCSLuaFile("easychat/client/unicode_transliterator.lua")
AddCSLuaFile("easychat/client/translator.lua")
AddCSLuaFile("easychat/client/expressions.lua")
AddCSLuaFile("easychat/client/chathud.lua")
AddCSLuaFile("easychat/client/markup.lua")
AddCSLuaFile("easychat/client/macro_processor.lua")
AddCSLuaFile("easychat/client/settings.lua")

View File

@ -10,22 +10,22 @@ local table_insert = _G.table.insert
local table_remove = _G.table.remove
local table_concat = _G.table.concat
local surface_SetDrawColor = _G.surface.SetDrawColor
local surface_SetTextColor = _G.surface.SetTextColor
local surface_GetTextSize = _G.surface.GetTextSize
local surface_SetFont = _G.surface.SetFont
local surface_SetTextPos = _G.surface.SetTextPos
local surface_DrawText = _G.surface.DrawText
local surface_CreateFont = _G.surface.CreateFont
local surface_GetLuaFonts = _G.surface.GetLuaFonts
local surface_SetMaterial = _G.surface.SetMaterial
local surface_DrawTexturedRect = _G.surface.DrawTexturedRect
local surface_SetDrawColor = CLIENT and _G.surface.SetDrawColor
local surface_SetTextColor = CLIENT and _G.surface.SetTextColor
local surface_GetTextSize = CLIENT and _G.surface.GetTextSize
local surface_SetFont = CLIENT and _G.surface.SetFont
local surface_SetTextPos = CLIENT and _G.surface.SetTextPos
local surface_DrawText = CLIENT and _G.surface.DrawText
local surface_CreateFont = CLIENT and _G.surface.CreateFont
local surface_GetLuaFonts = CLIENT and _G.surface.GetLuaFonts
local surface_SetMaterial = CLIENT and _G.surface.SetMaterial
local surface_DrawTexturedRect = CLIENT and _G.surface.DrawTexturedRect
local draw_GetFontHeight = _G.draw.GetFontHeight
local draw_NoTexture = _G.draw.NoTexture
local draw_GetFontHeight = CLIENT and _G.draw.GetFontHeight
local draw_NoTexture = CLIENT and _G.draw.NoTexture
local gui_mousex = _G.gui.MouseX
local gui_mousey = _G.gui.MouseY
local gui_mousex = CLIENT and _G.gui.MouseX
local gui_mousey = CLIENT and _G.gui.MouseY
--local render_OverrideBlend = _G.render.OverrideBlend
--local BLEND_ZERO, BLEND_ONE_MINUS_SRC_ALPHA = _G.BLEND_ZERO, _G.BLEND_ONE_MINUS_SRC_ALPHA
@ -90,82 +90,84 @@ engine_fonts_info["dermadefaultbold"] = {
}
local chathud = {
Parts = {},
SpecialPatterns = {},
Lines = {},
TagPattern = "<(.-)=%[?(.-)%]?>",
DefaultColor = Color(255, 255, 255),
DefaultFont = "ECHUDDefault",
DefaultShadowFont = "ECHUDShadowDefault",
FadeTime = 16,
FadeTimeEnd = 3,
-- default bounds for EasyChat
Pos = { X = 0, Y = 0 },
Size = { W = 400, H = 0 },
Lines = {},
Parts = {},
SpecialPatterns = {},
EmotePriorities = {},
TagPattern = "<(.-)=%[?(.-)%]?>",
ShouldClean = false,
DefaultColor = Color(255, 255, 255),
DefaultFont = "ECHUDDefault",
DefaultShadowFont = "ECHUDShadowDefault",
EmotePriorities = {},
}
local EC_HUD_TTL = GetConVar("easychat_hud_ttl")
local EC_HUD_FADELEN = GetConVar("easychat_hud_fadelen")
local EC_HUD_SMOOTH = GetConVar("easychat_hud_smooth")
chathud.FadeTime = EC_HUD_TTL:GetInt()
cvars.AddChangeCallback(EC_HUD_TTL:GetName(), function()
if CLIENT then
chathud.FadeTime = EC_HUD_TTL:GetInt()
end)
cvars.AddChangeCallback(EC_HUD_TTL:GetName(), function()
chathud.FadeTime = EC_HUD_TTL:GetInt()
end)
chathud.FadeTimeEnd = math_clamp(EC_HUD_FADELEN:GetInt(), 0, 5)
cvars.AddChangeCallback(EC_HUD_FADELEN:GetName(), function()
chathud.FadeTimeEnd = math_clamp(EC_HUD_FADELEN:GetInt(), 0, 5)
end)
cvars.AddChangeCallback(EC_HUD_FADELEN:GetName(), function()
chathud.FadeTimeEnd = math_clamp(EC_HUD_FADELEN:GetInt(), 0, 5)
end)
cvars.AddChangeCallback("easychat_hud_follow", function()
chathud:InvalidateLayout()
end)
cvars.AddChangeCallback("easychat_hud_follow", function()
chathud:InvalidateLayout()
end)
function chathud:ApplyCustomFontSettings()
if not file.Exists(CUSTOM_FONT_SETTINGS_PATH, "DATA") then return end
function chathud:ApplyCustomFontSettings()
if not file.Exists(CUSTOM_FONT_SETTINGS_PATH, "DATA") then return end
local json = file.Read(CUSTOM_FONT_SETTINGS_PATH, "DATA")
local data = util.JSONToTable(json)
local shadow_data = table_copy(data)
shadow_data.blursize = SHADOW_FONT_BLURSIZE
local json = file.Read(CUSTOM_FONT_SETTINGS_PATH, "DATA")
local data = util.JSONToTable(json)
local shadow_data = table_copy(data)
shadow_data.blursize = SHADOW_FONT_BLURSIZE
surface_CreateFont(self.DefaultFont, data)
surface_CreateFont(self.DefaultShadowFont, shadow_data)
surface_CreateFont(self.DefaultFont, data)
surface_CreateFont(self.DefaultShadowFont, shadow_data)
-- when this is called early, this function might not exists
if self.InvalidateLayout then
self:InvalidateLayout()
-- when this is called early, this function might not exists
if self.InvalidateLayout then
self:InvalidateLayout()
end
end
function chathud:UpdateFontSize(size)
surface_CreateFont(self.DefaultFont, {
font = "Roboto",
extended = true,
size = size,
weight = 530,
shadow = true,
read_speed = 100,
})
surface_CreateFont(self.DefaultShadowFont, {
font = "Roboto",
extended = true,
size = size,
weight = 530,
shadow = true,
blursize = SHADOW_FONT_BLURSIZE,
read_speed = 100,
})
self:ApplyCustomFontSettings()
end
chathud:UpdateFontSize(16)
end
function chathud:UpdateFontSize(size)
surface_CreateFont(self.DefaultFont, {
font = "Roboto",
extended = true,
size = size,
weight = 530,
shadow = true,
read_speed = 100,
})
surface_CreateFont(self.DefaultShadowFont, {
font = "Roboto",
extended = true,
size = size,
weight = 530,
shadow = true,
blursize = SHADOW_FONT_BLURSIZE,
read_speed = 100,
})
self:ApplyCustomFontSettings()
end
chathud:UpdateFontSize(16)
-- taken from https://github.com/notcake/glib/blob/master/lua/glib/unicode/utf8.lua#L15
local function utf8_byte(char, offset)
if char == "" then return -1 end
@ -304,15 +306,9 @@ function chathud:RegisterPart(name, part, pattern, exception_patterns)
end
new_part.Type = name
self.Parts[name] = new_part
if pattern then
self.SpecialPatterns[name] = {
Pattern = pattern,
ExceptionPatterns = exception_patterns or {}
}
end
if not blacklist[name] then
if CLIENT and not blacklist[name] then
local cvar_name = "easychat_tag_" .. name
local cvar = CreateClientConVar(cvar_name, new_part.Enabled and "1" or "0", true, false)
new_part.Enabled = cvar:GetBool()
@ -321,7 +317,12 @@ function chathud:RegisterPart(name, part, pattern, exception_patterns)
end)
end
self.Parts[name] = new_part
if pattern then
self.SpecialPatterns[name] = {
Pattern = pattern,
ExceptionPatterns = exception_patterns or {}
}
end
end
--[[-----------------------------------------------------------------------------
@ -500,6 +501,11 @@ function text_part:PostLinePush()
end
function text_part:ComputeSize()
if SERVER then
self.Size = { W = 0, H = 0 }
return
end
surface_SetFont(self.Font)
local w, h = surface_GetTextSize(self.Content)
self.Size = { W = w, H = h }
@ -700,7 +706,7 @@ function emote_part:Ctor(str)
self.Height = size
self.HasSetHeight = true
else
self.Height = draw_GetFontHeight(self.HUD.DefaultFont)
self.Height = SERVER and 32 or draw_GetFontHeight(self.HUD.DefaultFont)
end
if requested_provider then
@ -1431,7 +1437,7 @@ function chathud:InsertColorChange(r, g, b)
end
-- examples & help
do
if CLIENT then
concommand.Add("easychat_hud_examples", function()
local frame = EasyChat.CreateFrame()
frame:SetSize(640, 480)

View File

@ -269,6 +269,10 @@ if SERVER then
end
function EasyChat.Init()
EasyChat.Transliterator = include("easychat/unicode_transliterator.lua")
EasyChat.ChatHUD = include("easychat/chathud.lua")
include("easychat/markup.lua")
safe_hook_run("ECPreLoadModules")
load_modules()
safe_hook_run("ECPostLoadModules")
@ -517,15 +521,15 @@ if CLIENT then
EasyChat.Mode = 0
EasyChat.Modes = { [0] = default_chat_mode }
EasyChat.Expressions = include("easychat/client/expressions.lua")
EasyChat.Transliterator = include("easychat/client/unicode_transliterator.lua")
EasyChat.Transliterator = include("easychat/unicode_transliterator.lua")
EasyChat.Translator = include("easychat/client/translator.lua")
EasyChat.ChatHUD = include("easychat/client/chathud.lua")
EasyChat.ChatHUD = include("easychat/chathud.lua")
EasyChat.MacroProcessor = include("easychat/client/macro_processor.lua")
EasyChat.ModeCount = 0
include("easychat/client/blur_panel.lua")
include("easychat/client/settings.lua")
include("easychat/client/markup.lua")
include("easychat/markup.lua")
local ec_tabs = {}
local ec_convars = {}
@ -1145,7 +1149,7 @@ if CLIENT then
local function append_text(richtext, text)
if not EC_TAGS_IN_CHATBOX:GetBool() and ec_markup then
-- expensive but its not a behavior we want to encourage, so too bad :v
text = ec_markup.Parse(text):GetText()
text = ec_markup.GetText(text)
end
if richtext.HistoryName then
@ -1679,14 +1683,14 @@ if CLIENT then
EasyChat.Mode = 0
EasyChat.Modes = { [0] = default_chat_mode }
EasyChat.Expressions = include("easychat/client/expressions.lua")
EasyChat.Transliterator = include("easychat/client/unicode_transliterator.lua")
EasyChat.Transliterator = include("easychat/unicode_transliterator.lua")
EasyChat.Translator = include("easychat/client/translator.lua")
EasyChat.ChatHUD = include("easychat/client/chathud.lua")
EasyChat.ChatHUD = include("easychat/chathud.lua")
EasyChat.MacroProcessor = include("easychat/client/macro_processor.lua")
EasyChat.ModeCount = 0
include("easychat/client/settings.lua")
include("easychat/client/markup.lua")
include("easychat/markup.lua")
ec_convars = {}
ec_addtext_handles = {}
@ -2965,7 +2969,7 @@ if CLIENT then
if EasyChat.Config.AllowTagsInMessages then
table.insert(msg_components, ": " .. msg)
else
local stripped_msg = ec_markup and ec_markup.Parse(msg):GetText() or msg
local stripped_msg = ec_markup and ec_markup.GetText(msg) or msg
table.insert(msg_components, ": " .. stripped_msg)
end

View File

@ -36,7 +36,9 @@ function ec_markup.AdvancedParse(str, data)
local old_CreateComponent = obj.CreateComponent
function obj:CreateComponent(name, ...)
local component = old_CreateComponent(self, name, ...)
if smoothed_parts[name] and component then
if not component then return end
if smoothed_parts[name] then
-- disable smoothing of some parts
function component:ComputePos()
self.RealPos.Y = self.Pos.Y
@ -228,5 +230,9 @@ function ec_markup.CachePlayer(id, ply, callback)
return mk
end
function ec_markup.GetText(str)
return ec_markup.Parse(str, nil, true):GetText()
end
_G.ECMarkup = ec_markup.Parse
_G.ec_markup = ec_markup

View File

@ -229,7 +229,7 @@ function mentions:IsMention(msg)
local should_mention = EasyChat.SafeHookRun("ECShouldBeMention", msg)
if should_mention == false then return false end
local stripped_msg = ec_markup.Parse(msg):GetText():lower()
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()

View File

@ -1,19 +1,19 @@
local chathud = EasyChat.ChatHUD
local compile_expression = EasyChat.Expressions.Compile
local compile_expression = CLIENT and EasyChat.Expressions.Compile and function() return false end
local pcall = _G.pcall
local surface_SetDrawColor = surface.SetDrawColor
local surface_SetMaterial = surface.SetMaterial
local surface_DrawTexturedRect = surface.DrawTexturedRect
local surface_DrawRect = surface.DrawRect
local surface_DrawLine = surface.DrawLine
local surface_SetAlphaMultiplier = surface.SetAlphaMultiplier
local surface_GetAlphaMultiplier = surface.GetAlphaMultiplier
local surface_SetDrawColor = CLIENT and surface.SetDrawColor
local surface_SetMaterial = CLIENT and surface.SetMaterial
local surface_DrawTexturedRect = CLIENT and surface.DrawTexturedRect
local surface_DrawRect = CLIENT and surface.DrawRect
local surface_DrawLine = CLIENT and surface.DrawLine
local surface_SetAlphaMultiplier = CLIENT and surface.SetAlphaMultiplier
local surface_GetAlphaMultiplier = CLIENT and surface.GetAlphaMultiplier
local draw_NoTexture = draw.NoTexture
local draw_NoTexture = CLIENT and draw.NoTexture
local cam_PushModelMatrix = cam.PushModelMatrix
local cam_PopModelMatrix = cam.PopModelMatrix
local cam_PushModelMatrix = CLIENT and cam.PushModelMatrix
local cam_PopModelMatrix = CLIENT and cam.PopModelMatrix
local math_sin = math.sin
local math_abs = math.abs