|
| 1 | +local enabled = GmodScripts.MakeToggle( "wire_sent_spawn_alert", "Make wire log all SENT spawns", true ) |
| 2 | + |
| 3 | +hook.Add( "PreGamemodeLoaded", "CFC_GmodScripts_LoadWireSpawnStub", function() |
| 4 | + local logger = ulx and ulx.logSpawn or print |
| 5 | + |
| 6 | + local log = function( ply, entClass ) |
| 7 | + if IsValid( ply ) then |
| 8 | + logger( string.format( "%s<%s> spawned sent %s", ply:Nick(), ply:SteamID(), entClass ) ) |
| 9 | + else |
| 10 | + logger( string.format( "SENT was spawned without a valid owner:", entClass ) ) |
| 11 | + end |
| 12 | + end |
| 13 | + |
| 14 | + local replaceDupeFunctions = {} |
| 15 | + |
| 16 | + local wrapper = function( original ) |
| 17 | + local wrapped = function( ply, ... ) |
| 18 | + local result = original( ply, ... ) |
| 19 | + if not enabled:GetBool() then |
| 20 | + return result |
| 21 | + end |
| 22 | + |
| 23 | + if isentity( result ) then |
| 24 | + local entClass = result:GetClass() |
| 25 | + log( ply, entClass ) |
| 26 | + else |
| 27 | + print( ply, "unknown", result ) |
| 28 | + end |
| 29 | + |
| 30 | + return result |
| 31 | + end |
| 32 | + |
| 33 | + replaceDupeFunctions[original] = wrapped |
| 34 | + return wrapped |
| 35 | + end |
| 36 | + |
| 37 | + local dupeClasses = duplicator.EntityClasses |
| 38 | + |
| 39 | + local originalMakeWireEnt = WireLib.MakeWireEnt |
| 40 | + WireLib.MakeWireEnt = wrapper( originalMakeWireEnt ) |
| 41 | + _G.MakeWireMotorController = wrapper( _G.MakeWireMotorController ) |
| 42 | + _G.MakeWireHydraulicController = wrapper( _G.MakeWireHydraulicController ) |
| 43 | + _G.MakeWireExpression2 = wrapper( _G.MakeWireExpression2 ) |
| 44 | + |
| 45 | + -- Special case for gmod_wire_gate so we can figure out what action it is |
| 46 | + do |
| 47 | + local original = WireLib.MakeWireGate |
| 48 | + local gateStub = function( ply, pos, ang, model, action, ... ) |
| 49 | + local result = original( ply, pos, ang, model, action, ... ) |
| 50 | + if not enabled:GetBool() then |
| 51 | + return result |
| 52 | + end |
| 53 | + |
| 54 | + if isentity( result ) then |
| 55 | + local entClass = "gmod_wire_gate(" .. action .. ")" |
| 56 | + log( ply, entClass ) |
| 57 | + end |
| 58 | + |
| 59 | + return result |
| 60 | + end |
| 61 | + |
| 62 | + -- This fun bit of nonsense lets us stub MakeWireGate (which calls MakeWireEnt) and print which gate was spawned but without duplicate prints. |
| 63 | + -- For example, if I spawn an Entity gate, MakeWireGate wrapper prints: gmod_wire_gate(rd_entity), but then it calls the wrapped MakeWireEnt and prints: gmod_wire_gate again |
| 64 | + -- This code prevents that by allowing MakeWireGate to call the original MakeWireEnt |
| 65 | + local specialWireLib = setmetatable( { MakeWireEnt = originalMakeWireEnt }, { __index = WireLib } ) |
| 66 | + setfenv( original, setmetatable( { WireLib = specialWireLib }, { __index = _G } ) ) |
| 67 | + |
| 68 | + WireLib.MakeWireGate = gateStub |
| 69 | + dupeClasses.gmod_wire_gate.Func = gateStub |
| 70 | + end |
| 71 | + |
| 72 | + -- Overwrite specific dupe functions that use local functions |
| 73 | + dupeClasses.sent_deployableballoons.Func = wrapper( dupeClasses.sent_deployableballoons.Func ) |
| 74 | + dupeClasses.gmod_wire_egp.Func = wrapper( dupeClasses.gmod_wire_egp.Func ) |
| 75 | + dupeClasses.gmod_wire_egp_hud.Func = wrapper( dupeClasses.gmod_wire_egp_hud.Func ) |
| 76 | + dupeClasses.gmod_wire_egp_emitter.Func = wrapper( dupeClasses.gmod_wire_egp_emitter.Func ) |
| 77 | + |
| 78 | + -- I can't figure out how to load at the right time, so this loops over all dupe functions and |
| 79 | + -- replaces them with the wrapped version if they exist (we stub the global func after they already registered with duplicator idk) |
| 80 | + -- If you can find a hook that runs after autorun, but before weapons/entities/sents, then use that and remove this |
| 81 | + for _, data in pairs( dupeClasses ) do |
| 82 | + local wrapped = replaceDupeFunctions[data.Func] |
| 83 | + if wrapped then |
| 84 | + data.Func = wrapped |
| 85 | + end |
| 86 | + end |
| 87 | +end ) |
0 commit comments