Skip to content

Commit

Permalink
use pac hooks when possible
Browse files Browse the repository at this point in the history
should help prevent showing stuff when pac is disabled
  • Loading branch information
Techbot121 committed Dec 21, 2023
1 parent 33a0fd8 commit 0a6c46e
Show file tree
Hide file tree
Showing 25 changed files with 141 additions and 131 deletions.
7 changes: 4 additions & 3 deletions lua/autorun/pac_restart.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ if SERVER then
return
end

local sv_allowcslua = GetConVar('sv_allowcslua')
local sv_allowcslua = GetConVar("sv_allowcslua")
local prefer_local_version = CreateClientConVar("pac_restart_prefer_local_version", "0")

function _G.pac_ReloadParts()
Expand Down Expand Up @@ -92,7 +92,7 @@ function _G.pac_Restart()

if pac and pac.Disable then
pacLocal.Message("removing all traces of pac3 from lua")
pac.Disable()
pac.Disable(true)
pac.Panic()

if pace and pace.Editor then
Expand All @@ -110,7 +110,8 @@ function _G.pac_Restart()

for hook_name, hooks in pairs(hook.GetTable()) do
for id, func in pairs(hooks) do
if isstring(id) and (id:StartWith("pace_") or id:StartWith("pac_") or id:StartWith("pac3_") or id:StartWith("pacx_")) then
local lower = isstring(id) and string.lower(id)
if lower and (lower:StartWith("pace_") or lower:StartWith("pac_") or lower:StartWith("pac3_") or lower:StartWith("pacx_")) then
hook.Remove(hook_name, id)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lua/pac3/core/client/ear_grab_animation.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- see https://github.com/Facepunch/garrysmod/blob/master/garrysmod/gamemodes/base/gamemode/animations.lua#L235
hook.Add("PostGamemodeLoaded", "pac_ear_grab_animation",function()
pac.AddHook("PostGamemodeLoaded", "ear_grab_animation", function()
if GAMEMODE.GrabEarAnimation then -- only add it if it exists
local original_ear_grab_animation = GAMEMODE.GrabEarAnimation
GAMEMODE.GrabEarAnimation = function(_, ply)
Expand Down
13 changes: 7 additions & 6 deletions lua/pac3/core/client/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ do
pac_enable:SetBool(true)
end

function pac.Disable()
function pac.Disable(temp)
pac.EnableDrawnEntities(false)
pac.DisableAddedHooks()
pac.CallHook("Disable")
pac_enable:SetBool(false)
if not temp then
pac_enable:SetBool(false)
end
end
end

include("util.lua")
include("class.lua")

Expand All @@ -58,7 +59,7 @@ include("ear_grab_animation.lua")

pac.LoadParts()

hook.Add("OnEntityCreated", "pac_init", function(ent)
pac.AddHook("OnEntityCreated", "init", function(ent)
local ply = LocalPlayer()
if not ply:IsValid() then return end

Expand All @@ -67,8 +68,8 @@ hook.Add("OnEntityCreated", "pac_init", function(ent)
pac.LocalHands = pac.LocalPlayer:GetHands()

pac.in_initialize = true
hook.Run("pac_Initialized")
pac.CallHook("Initialized")
pac.in_initialize = nil

hook.Remove("OnEntityCreated", "pac_init")
pac.RemoveHook("OnEntityCreated", "init")
end)
18 changes: 9 additions & 9 deletions lua/pac3/core/client/integration_tools.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ end
do
local force_draw_localplayer = false

hook.Add("ShouldDrawLocalPlayer", "pac_draw_2d_entity", function()
pac.AddHook("ShouldDrawLocalPlayer", "draw_2d_entity", function()
if force_draw_localplayer == true then
return true
end
Expand Down Expand Up @@ -244,7 +244,7 @@ function pac.AddEntityClassListener(class, session, check_func, draw_dist)
draw_dist = 0
check_func = check_func or function(ent) return ent:GetClass() == class end

local id = "pac_auto_attach_" .. class
local id = "auto_attach_" .. class

local weapons = {}
local function weapon_think()
Expand Down Expand Up @@ -282,7 +282,7 @@ function pac.AddEntityClassListener(class, session, check_func, draw_dist)
if ent:IsValid() and check_func(ent) then
if ent:IsWeapon() then
weapons[ent:EntIndex()] = ent
hook.Add("Think", id, weapon_think)
pac.AddHook("Think", id, weapon_think)
else
pac.SetupENT(ent)
ent:AttachPACSession(session)
Expand All @@ -302,8 +302,8 @@ function pac.AddEntityClassListener(class, session, check_func, draw_dist)
created(ent)
end

hook.Add("EntityRemoved", id, removed)
hook.Add("OnEntityCreated", id, created)
pac.AddHook("EntityRemoved", id, removed)
pac.AddHook("OnEntityCreated", id, created)
end

function pac.RemoveEntityClassListener(class, session, check_func)
Expand All @@ -319,11 +319,11 @@ function pac.RemoveEntityClassListener(class, session, check_func)
end
end

local id = "pac_auto_attach_" .. class
local id = "auto_attach_" .. class

hook.Remove("Think", id)
hook.Remove("EntityRemoved", id)
hook.Remove("OnEntityCreated", id)
pac.RemoveHook("Think", id)
pac.RemoveHook("EntityRemoved", id)
pac.RemoveHook("OnEntityCreated", id)
end

timer.Simple(0, function()
Expand Down
4 changes: 2 additions & 2 deletions lua/pac3/core/client/part_pool.lua
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,8 @@ pac.AddHook("Think", "events", function()
lamp:SetAngles( pac.LocalPlayer:EyeAngles() )
lamp:Update()

hook.Add("PostRender", "pac_flashlight_stuck_fix", function()
hook.Remove("PostRender", "pac_flashlight_stuck_fix")
pac.AddHook("PostRender", "flashlight_stuck_fix", function()
pac.RemoveHook("PostRender", "flashlight_stuck_fix")
lamp:Remove()
end)

Expand Down
16 changes: 8 additions & 8 deletions lua/pac3/core/client/parts/damage_zone.lua
Original file line number Diff line number Diff line change
Expand Up @@ -719,16 +719,16 @@ end)


function PART:OnHide()
hook.Remove(self.RenderingHook, "pace_draw_hitbox"..self.UniqueID)
pac.RemoveHook(self.RenderingHook, "pace_draw_hitbox"..self.UniqueID)
for _,v in pairs(renderhooks) do
hook.Remove(v, "pace_draw_hitbox"..self.UniqueID)
pac.RemoveHook(v, "pace_draw_hitbox"..self.UniqueID)
end
end

function PART:OnRemove()
hook.Remove(self.RenderingHook, "pace_draw_hitbox")
pac.RemoveHook(self.RenderingHook, "pace_draw_hitbox")
for _,v in pairs(renderhooks) do
hook.Remove(v, "pace_draw_hitbox")
pac.RemoveHook(v, "pace_draw_hitbox")
end
end

Expand All @@ -738,16 +738,16 @@ function PART:PreviewHitbox()

if previousRenderingHook ~= self.RenderingHook then
for _,v in pairs(renderhooks) do
hook.Remove(v, "pace_draw_hitbox"..self.UniqueID)
pac.RemoveHook(v, "pace_draw_hitbox"..self.UniqueID)
end
previousRenderingHook = self.RenderingHook
end

if not self.Preview then return end

hook.Add(self.RenderingHook, "pace_draw_hitbox"..self.UniqueID, function()
if not self.Preview then hook.Remove(self.RenderingHook, "pace_draw_hitbox"..self.UniqueID) end
if not IsValid(self) then hook.Remove(self.RenderingHook, "pace_draw_hitbox"..self.UniqueID) end
pac.AddHook(self.RenderingHook, "pace_draw_hitbox"..self.UniqueID, function()
if not self.Preview then pac.RemoveHook(self.RenderingHook, "pace_draw_hitbox"..self.UniqueID) end
if not IsValid(self) then pac.RemoveHook(self.RenderingHook, "pace_draw_hitbox"..self.UniqueID) end
self:GetWorldPosition()
if self.HitboxMode == "Box" then
local mins = Vector(-self.Radius, -self.Radius, -self.Length)
Expand Down
10 changes: 5 additions & 5 deletions lua/pac3/core/client/parts/legacy/model.lua
Original file line number Diff line number Diff line change
Expand Up @@ -662,13 +662,13 @@ do
self.Mesh = nil

local real_model = modelPath
local ret = hook.Run("pac_model:SetModel", self, modelPath, self.ModelFallback)
local ret = pac.CallHook("model:SetModel", self, modelPath, self.ModelFallback)
if ret == nil then
real_model = pac.FilterInvalidModel(real_model,self.ModelFallback)
real_model = pac.FilterInvalidModel(real_model, self.ModelFallback)
else
modelPath = ret or modelPath
real_model = modelPath
real_model = pac.FilterInvalidModel(real_model,self.ModelFallback)
real_model = pac.FilterInvalidModel(real_model, self.ModelFallback)
end

self.Model = modelPath
Expand All @@ -682,7 +682,7 @@ do
end
end

local NORMAL = Vector(1,1,1)
local NORMAL = Vector(1, 1, 1)

function PART:CheckScale()
-- RenderMultiply doesn't work with this..
Expand All @@ -704,7 +704,7 @@ function PART:SetAlternativeScaling(b)
end

function PART:SetScale(var)
var = var or Vector(1,1,1)
var = var or Vector(1, 1, 1)

self.Scale = var

Expand Down
2 changes: 1 addition & 1 deletion lua/pac3/core/client/parts/model.lua
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ function PART:ProcessModelChange()
end
)
else
local status, reason = hook.Run('PAC3AllowMDLDownload', self:GetPlayerOwner(), self, path)
local status, reason = hook.Run("PAC3AllowMDLDownload", self:GetPlayerOwner(), self, path)

if ALLOW_TO_MDL:GetBool() and status ~= false then
self.loading = "downloading mdl zip"
Expand Down
14 changes: 7 additions & 7 deletions lua/pac3/core/client/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,17 @@ local function start_test(name, done)
end

function test.Setup()
hook.Add("ShouldDrawLocalPlayer", "pac_test", function() return true end)
pac.AddHook("ShouldDrawLocalPlayer", "test", function() return true end)
end

function test.Teardown()
hook.Remove("ShouldDrawLocalPlayer", "pac_test")
pac.RemoveHook("ShouldDrawLocalPlayer", "test")
end

function test.Run(done) error("test.Run is not defined") end
function test.Remove()
hook.Remove("ShouldDrawLocalPlayer", "pac_test")
hook.Remove("Think", "pac_test_coroutine")
pac.RemoveHook("ShouldDrawLocalPlayer", "test")
pac.RemoveHook("Think", "test_coroutine")

if test.done then return end

Expand Down Expand Up @@ -172,7 +172,7 @@ local function start_test(name, done)
test.Remove()
end

hook.Add("Think", "pac_test_coroutine", function()
pac.AddHook("Think", "test_coroutine", function()
if not test.co then return end

local ok, err = coroutine.resume(test.co)
Expand Down Expand Up @@ -213,7 +213,7 @@ concommand.Add("pac_test", function(ply, _, args)

local current_test = nil

hook.Add("Think", "pac_tests", function()
pac.AddHook("Think", "tests", function()
if current_test then
if current_test.time < os.clock() then
msg_error("test ", current_test.name, " timed out")
Expand All @@ -232,7 +232,7 @@ concommand.Add("pac_test", function(ply, _, args)
local name = table.remove(tests, 1)
if not name then
msg("finished testing")
hook.Remove("Think", "pac_tests")
pac.RemoveHook("Think", "tests")
return
end

Expand Down
25 changes: 17 additions & 8 deletions lua/pac3/core/client/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -364,28 +364,29 @@ do -- hook helpers
pac.added_hooks = pac.added_hooks or {}

function pac.AddHook(event_name, id, func, priority)
id = "pac_" .. id
id = isstring(id) and "pac_" .. id or id

if not DLib and not ULib then
priority = nil
end

if pac.IsEnabled() then
print("adding", event_name, id)
hook.Add(event_name, id, func, priority)
print("adding hooks", event_name .. tostring(id))
pac.added_hooks[event_name .. tostring(id)] = {event_name = event_name, id = id, func = func, priority = priority}
end

pac.added_hooks[event_name .. id] = {event_name = event_name, id = id, func = func, priority = priority}
end

function pac.RemoveHook(event_name, id)
id = "pac_" .. id
id = "pac_" .. tostring(id)

local data = pac.added_hooks[event_name .. id]

if data then
hook.Remove(data.event_name, data.id)

print("removing", data.event_name, data.id)
pac.added_hooks[event_name .. id] = nil
print("removing hooks", event_name .. id)
end
end

Expand All @@ -395,13 +396,21 @@ do -- hook helpers

function pac.EnableAddedHooks()
for _, data in pairs(pac.added_hooks) do
hook.Add(data.event_name, data.id, data.func, data.priority)
if ispanel(data.id) and not IsValid(data.id) then -- Panels can be NULL and are (probably) already removed
pac.added_hooks[data.event_name .. tostring(data.id)] = nil
else
hook.Add(data.event_name, data.id, data.func, data.priority)
end
end
end

function pac.DisableAddedHooks()
for _, data in pairs(pac.added_hooks) do
hook.Remove(data.event_name, data.id)
if ispanel(data.id) and not IsValid(data.id) then -- Panels can be NULL and are already removed
pac.added_hooks[data.event_name .. tostring(data.id)] = nil
else
hook.Remove(data.event_name, data.id)
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lua/pac3/core/server/in_skybox.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
hook.Add("InitPostEntity","pac_get_sky_camera",function()
pac.AddHook("InitPostEntity", "get_sky_camera", function()
local sky_camera = ents.FindByClass("sky_camera")[1]
if sky_camera then
local nwVarName = "pac_in_skybox"
Expand Down
6 changes: 3 additions & 3 deletions lua/pac3/core/server/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ pac.resource = include("pac3/libraries/resource.lua")

CreateConVar("has_pac3", "1", {FCVAR_NOTIFY})
CreateConVar("pac_allow_blood_color", "1", {FCVAR_NOTIFY}, "Allow to use custom blood color")
CreateConVar('pac_allow_mdl', '1', CLIENT and {FCVAR_REPLICATED} or {FCVAR_ARCHIVE, FCVAR_REPLICATED}, 'Allow to use custom MDLs')
CreateConVar('pac_allow_mdl_entity', '1', CLIENT and {FCVAR_REPLICATED} or {FCVAR_ARCHIVE, FCVAR_REPLICATED}, 'Allow to use custom MDLs as Entity')
CreateConVar("pac_allow_mdl", "1", CLIENT and {FCVAR_REPLICATED} or {FCVAR_ARCHIVE, FCVAR_REPLICATED}, "Allow to use custom MDLs")
CreateConVar("pac_allow_mdl_entity", "1", CLIENT and {FCVAR_REPLICATED} or {FCVAR_ARCHIVE, FCVAR_REPLICATED}, "Allow to use custom MDLs as Entity")

include("util.lua")

Expand All @@ -20,4 +20,4 @@ include("net_messages.lua")
include("test_suite_backdoor.lua")
include("in_skybox.lua")

hook.Run("pac_Initialized")
pac.CallHook("Initialized")
2 changes: 1 addition & 1 deletion lua/pac3/core/server/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function pac.AddHook(str, id, func, priority)
local status, a, b, c, d, e, f, g = pcall(func, ...)

if not status then
pac.Message('Error on hook ' .. str .. ' (' .. id .. ')! ', a)
pac.Message("Error on hook " .. str .. " (" .. id .. ")! ", a)
return
end

Expand Down
12 changes: 6 additions & 6 deletions lua/pac3/core/shared/entity_mutator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -301,15 +301,15 @@ if SERVER then
end
end

hook.Add("PlayerInitialSpawn", "pac_entity_mutators_spawn", function(ply)
local id = "pac_entity_mutators_spawn" .. ply:UniqueID()
hook.Add( "SetupMove", id, function(movingPly, _, cmd)
pac.AddHook("PlayerInitialSpawn", "entity_mutators_spawn", function(ply)
local id = "entity_mutators_spawn" .. ply:UniqueID()
pac.AddHook( "SetupMove", id, function(movingPly, _, cmd)
if not ply:IsValid() then
hook.Remove("SetupMove", id)
pac.RemoveHook("SetupMove", id)
elseif movingPly == ply and not cmd:IsForced() then
emut.ReplicateMutatorsForPlayer(ply)

hook.Remove("SetupMove", id)
pac.RemoveHook("SetupMove", id)
end
end)
end)
Expand All @@ -323,7 +323,7 @@ function emut.RemoveMutationsForPlayer(ply)
end
end

hook.Add("EntityRemoved", "pac_entity_mutators_left", function(ent)
pac.AddHook("EntityRemoved", "entity_mutators_left", function(ent)
if not IsValid(ent) then return end
if ent:IsPlayer() then
if Player(ent:UserID()) == NULL then
Expand Down
Loading

0 comments on commit 0a6c46e

Please sign in to comment.