Skip to content

Commit

Permalink
Add WireLib.Net and WireLib.Notify
Browse files Browse the repository at this point in the history
  • Loading branch information
Denneisk committed Dec 8, 2023
1 parent 4edf127 commit 82d7304
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lua/autorun/wire_load.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ if SERVER then
-- shared includes
AddCSLuaFile("wire/wire_paths.lua")
AddCSLuaFile("wire/wireshared.lua")
AddCSLuaFile("wire/sh_wirenet.lua")
AddCSLuaFile("wire/wiregates.lua")
AddCSLuaFile("wire/wiremonitors.lua")
AddCSLuaFile("wire/gpulib.lua")
Expand Down Expand Up @@ -75,6 +76,7 @@ end
-- shared includes
include("wire/sh_modelplug.lua")
include("wire/wireshared.lua")
include("wire/sh_wirenet.lua")
include("wire/wire_paths.lua")
include("wire/wiregates.lua")
include("wire/wiremonitors.lua")
Expand Down
4 changes: 2 additions & 2 deletions lua/entities/gmod_wire_expression2/core/extpp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ local function parseParameters(raw, trace)
return parsed, true, name
elseif raw_param:match("^%s*%.%.%.%s*$") then -- Variadic lua parameter
assert(k == len, "PP syntax error: Ellipses (...) must be the last argument.")
ErrorNoHalt("Warning: Use of variadic parameter with ExtPP is not recommended and deprecated. Instead use ...<name> (which passes a table) or the `args` variable " .. trace .. "\n")
WireLib.NotifyAll("Use of variadic parameter with ExtPP is not recommended and deprecated. Instead use ...<name> (which passes a table) or the `args` variable " .. trace .. "\n", 2)
return parsed, true
else
local typename, argname = string.match(raw_param, "^%s*(" .. p_typename .. ")%s+(" .. p_argname .. ")%s*$")
Expand Down Expand Up @@ -169,7 +169,7 @@ function E2Lib.ExtPP.Pass2(contents, filename)
elseif ret ~= "void" and not getTypeId(ret) then
error("PP syntax error: Invalid return type: '" .. ret .. "' " .. trace)
elseif RemovedOperators[name] then -- Old operator that no longer is needed.
ErrorNoHalt("Warning: Operator " .. name .. " is now redundant. Ignoring registration. " .. trace .. "\n")
WireLib.NotifyAll("Operator " .. name .. " is now redundant. Ignoring registration. " .. trace .. "\n", 2)
local pivot = parseAttributes(attributes, trace) and a_begin - 1 or h_begin - 1
table.insert(output, contents:sub(lastpos, pivot)) -- Insert code from before header.
changed, lastpos = true, h_end -- Mark as changed and remove function header.
Expand Down
33 changes: 32 additions & 1 deletion lua/wire/client/cl_wirelib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,35 @@ if old_renderhalos ~= nil then
end)
else
ErrorNoHalt("Wiremod RenderHalos detour failed (RenderHalos hook not found)!")
end
end

-- Notify --

local severity2color = {
[0] = color_white,
[1] = color_white,
[2] = Color(255, 88, 1),
[3] = Color(255, 32, 0)
}

local WIREMOD_COLOR = Color(1, 168, 255)

local severity2title = {
[0] = { "" },
[1] = { WIREMOD_COLOR, "[Wiremod]: " },
[2] = { WIREMOD_COLOR, "[Wiremod ", severity2color[2], "WARNING", WIREMOD_COLOR, "]: " },
[3] = { WIREMOD_COLOR, "[Wiremod ", severity2color[3], "ERROR", WIREMOD_COLOR, "]: " },
}

WireLib.Net.Receive("notify", function()
local args = {}
local severity = net.ReadUInt(4)
for k, v in ipairs(severity2title[severity]) do
args[k] = v
end
local n = #args
args[n + 1] = net.ReadBool() and net.ReadColor(false) or severity2color[severity]
args[n + 2] = util.Decompress(net.ReadData(net.ReadUInt(11)))
chat.AddText(unpack(args))
chat.PlaySound()
end)
47 changes: 47 additions & 0 deletions lua/wire/server/wirelib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1403,3 +1403,50 @@ if not WireLib.PatchedDuplicator then
return unpack(result)
end
end

-- Notify --

local net_start = WireLib.Net.Start
local severity2color = {

Check warning on line 1410 in lua/wire/server/wirelib.lua

View workflow job for this annotation

GitHub Actions / lint

"Unused variable"

Unused variable: severity2color
[0] = color_white,
[1] = color_white,
[2] = Color(255, 88, 1),
[3] = Color(255, 32, 0)
}

--- Sends a colored message to the player's chat.
---@param ply Player
---@param msg string
---@param severity WireLib.NotifySeverity?
---@param color Color?
function WireLib.Notify(ply, msg, severity, color)
if not severity then severity = 1 end

net_start("notify")
net.WriteUInt(severity, 4)
net.WriteBool(color ~= nil)
if color ~= nil then net.WriteColor(color, false) end
local data = util.Compress(msg)
local datal = math.min(#data, 2048)
net.WriteUInt(datal, 11)
net.WriteData(data, datal)
net.Send(ply)
end

--- Sends a colored message to all players' chats.
---@param msg string
---@param severity WireLib.NotifySeverity? A value from WireLib.NotifySeverity
---@param color Color?
function WireLib.NotifyAll(msg, severity, color)
if not severity then severity = 1 end

net_start("notify")
net.WriteUInt(severity, 4)
net.WriteBool(color ~= nil)
if color ~= nil then net.WriteColor(color, false) end
local data = util.Compress(msg)
local datal = math.min(#data, 2048)
net.WriteUInt(datal, 11)
net.WriteData(data, datal)
net.Broadcast()
end
45 changes: 45 additions & 0 deletions lua/wire/sh_wirenet.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
--[[
Consolidated networking library for Wiremod.
Trivial Networking - A wrapper for sending very small net messages that don't need a whole networkstring.
]]

local Net = WireLib.Net or {}
WireLib.Net = Net

-- Trivial Networking

if SERVER then
util.AddNetworkString("wirelib_net_message")
end

local registered_handlers = {}

local function msg_handler(len, ply)
local handler = registered_handlers[net.ReadString()]
if handler then
handler(len, ply)
else
error("WireLib.Net tried to receive message that is not registered.")
end
end

net.Receive("wirelib_net_message", msg_handler)

--- Starts a trivial net message within a normal Gmod net context.
--- Use net functions normally after this. <br>
--- WARNING: the entire name string is networked to identify the message. Keep it simple. Names are case insensitive.
---@param name string
---@param unreliable boolean?
function Net.Start(name, unreliable)
name = name:lower()
net.Start("wirelib_net_message", unreliable)
net.WriteString(name)
end

--- Receives a trivial net message.
--- Use net functions normally in the callback.
---@param name string
---@param callback fun(len:string, ply:Player)
function Net.Receive(name, callback)
registered_handlers[name:lower()] = callback
end
10 changes: 10 additions & 0 deletions lua/wire/wireshared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1201,3 +1201,13 @@ hook.Add("PlayerDisconnected", "WireLib_PlayerDisconnect", function(ply)
tbl[ply] = nil
end
end)

-- Notify --

---@enum WireLib.NotifySeverity
WireLib.NotifySeverity = {
None = 0,
Info = 1,
Warning = 2,
Error = 3
}

0 comments on commit 82d7304

Please sign in to comment.