diff --git a/lua/entities/starfall_hologram/cl_init.lua b/lua/entities/starfall_hologram/cl_init.lua index 3f46a622a..4d662b8c8 100644 --- a/lua/entities/starfall_hologram/cl_init.lua +++ b/lua/entities/starfall_hologram/cl_init.lua @@ -6,6 +6,7 @@ ENT.DefaultMaterial = Material( "hunter/myplastic" ) ENT.Material = ENT.DefaultMaterial local VECTOR_PLAYER_COLOR_DISABLED = Vector(-1, -1, -1) +local IsValid = FindMetaTable("Entity").IsValid function ENT:Initialize() self.clips = {} @@ -76,7 +77,7 @@ function ENT:Draw(flags) prevClip = render.EnableClipping(true) for _, clip in pairs(selfTbl.clips) do local clipent = clip.entity - if clipent and clipent:IsValid() then + if IsValid(clipent) then local norm = clipent:LocalToWorld(clip.normal) - clipent:GetPos() render.PushCustomClipPlane(norm, norm:Dot(clipent:LocalToWorld(clip.origin))) else @@ -167,12 +168,12 @@ local function ShowHologramOwners() local name = "No Owner" local steamID = "" local ply = SF.Permissions.getOwner(ent) - if ply and ply:IsValid() then + if IsValid(ply) then name = ply:Name() steamID = ply:SteamID() else ply = ent.SFHoloOwner - if ply and ply:IsValid() then + if IsValid(ply) then name = ply:Name() steamID = ply:SteamID() end diff --git a/lua/entities/starfall_hud/init.lua b/lua/entities/starfall_hud/init.lua index 4198ec74c..cbabc6559 100644 --- a/lua/entities/starfall_hud/init.lua +++ b/lua/entities/starfall_hud/init.lua @@ -7,6 +7,8 @@ util.AddNetworkString("starfall_hud_set_enabled") local vehiclelinks = SF.EntityTable("vehicleLinks") SF.HudVehicleLinks = vehiclelinks +local IsValid = FindMetaTable("Entity").IsValid + function ENT:Initialize() self.BaseClass.Initialize(self) self:PhysicsInit(SOLID_VPHYSICS) @@ -23,7 +25,7 @@ function ENT:UpdateTransmitState() end function ENT:Use(ply) - if not (self.link and self.link:IsValid()) then ply:ChatPrint("This hud isn't linked to a chip!") return end + if not IsValid(self.link) then ply:ChatPrint("This hud isn't linked to a chip!") return end local enabled = not self.enabled[ply] self.enabled[ply] = enabled or nil SF.EnableHud(ply, self.link, self, enabled) @@ -44,8 +46,8 @@ local function vehicleEnableHud(ply, vehicle, enabled) local huds = vehiclelinks[vehicle] if huds then for v in pairs(huds) do - if v:IsValid() then - if v.link and v.link:IsValid() then + if IsValid(v) then + if IsValid(v.link) then SF.EnableHud(ply, v.link, vehicle, enabled) end else @@ -61,12 +63,12 @@ hook.Add("PlayerLeaveVehicle", "Starfall_HUD", function(ply, vehicle) vehicleEna function ENT:PreEntityCopy() if self.EntityMods then self.EntityMods.SFLink = nil end local info = {} - if (self.link and self.link:IsValid()) then + if IsValid(self.link) then info.link = self.link:EntIndex() end local linkedvehicles = {} for k, huds in pairs(vehiclelinks) do - if huds[self] and k:IsValid() then + if huds[self] and IsValid(k) then linkedvehicles[#linkedvehicles + 1] = k:EntIndex() end end @@ -83,7 +85,7 @@ function ENT:PostEntityPaste(ply, ent, CreatedEntities) local info = ent.EntityMods.SFLink if info.link then local e = CreatedEntities[info.link] - if (e and e:IsValid()) then + if IsValid(e) then SF.LinkEnt(self, e) end end @@ -91,7 +93,7 @@ function ENT:PostEntityPaste(ply, ent, CreatedEntities) if info.linkedvehicles then for k, v in pairs(info.linkedvehicles) do local e = CreatedEntities[v] - if (e and e:IsValid()) then + if IsValid(e) then self:LinkVehicle(e) end end diff --git a/lua/entities/starfall_processor/cl_init.lua b/lua/entities/starfall_processor/cl_init.lua index 0fc2bd5df..448dafe36 100644 --- a/lua/entities/starfall_processor/cl_init.lua +++ b/lua/entities/starfall_processor/cl_init.lua @@ -4,6 +4,8 @@ DEFINE_BASECLASS("base_gmodentity") ENT.RenderGroup = RENDERGROUP_BOTH +local IsValid = FindMetaTable("Entity").IsValid + function ENT:Initialize() self.name = "Generic ( No-Name )" self.OverlayFade = 0 @@ -18,7 +20,7 @@ function ENT:OnRemove() instance:runScriptHook("removed") timer.Simple(0, function() - if not self:IsValid() then + if not IsValid(self) then instance:deinitialize() self.instance = nil end @@ -130,7 +132,7 @@ else end hook.Add("StarfallError", "StarfallErrorReport", function(_, owner, client, main_file, message, traceback, should_notify) - if not (owner and owner:IsValid()) then return end + if not IsValid(owner) then return end local local_player = LocalPlayer() if owner == local_player then if not client or client == owner then @@ -182,7 +184,7 @@ end) net.Receive("starfall_processor_kill", function() local target = net.ReadEntity() - if target:IsValid() and target:GetClass()=="starfall_processor" then + if IsValid(target) and target:GetClass()=="starfall_processor" then target:Error({message = "Killed by admin", traceback = ""}) end end) @@ -191,14 +193,14 @@ net.Receive("starfall_processor_used", function(len) local chip = net.ReadEntity() local used = net.ReadEntity() local activator = net.ReadEntity() - if not (chip and chip:IsValid()) then return end - if not (used and used:IsValid()) then return end + if not IsValid(chip) then return end + if not IsValid(used) then return end local instance = chip.instance if not instance then return end instance:runScriptHook("starfallused", instance.WrapObject( activator ), instance.WrapObject( used )) - if activator == LocalPlayer() and instance.player ~= SF.Superuser and instance.permissionRequest and instance.permissionRequest.showOnUse and not SF.Permissions.permissionRequestSatisfied( instance ) and not IsValid(SF.permPanel) then + if activator == LocalPlayer() and instance.player ~= SF.Superuser and instance.permissionRequest and instance.permissionRequest.showOnUse and not SF.Permissions.permissionRequestSatisfied( instance ) and not (SF.permPanel and SF.permPanel:IsValid()) then local pnl = vgui.Create("SFChipPermissions") if pnl then pnl:OpenForChip( chip ) diff --git a/lua/entities/starfall_processor/init.lua b/lua/entities/starfall_processor/init.lua index dc99f4776..c00326c2f 100644 --- a/lua/entities/starfall_processor/init.lua +++ b/lua/entities/starfall_processor/init.lua @@ -2,6 +2,8 @@ AddCSLuaFile("cl_init.lua") AddCSLuaFile("shared.lua") include("shared.lua") +local IsValid = FindMetaTable("Entity").IsValid + function ENT:Initialize() self:PhysicsInit(SOLID_VPHYSICS) self:SetMoveType(MOVETYPE_VPHYSICS) @@ -22,7 +24,7 @@ end function ENT:SetCustomModel(model) if self:GetModel() == model then return end - if self:GetParent():IsValid() then + if IsValid(self:GetParent()) then self:SetModel(model) else local constraints = constraint.GetTable(self) @@ -163,7 +165,7 @@ local function EntityLookup(CreatedEntities) if id == nil then return default end if id == 0 then return game.GetWorld() end local ent = CreatedEntities[id] - if (ent and ent:IsValid()) then return ent else return default end + if IsValid(ent) then return ent else return default end end end function ENT:PostEntityPaste(ply, ent, CreatedEntities) @@ -223,7 +225,7 @@ util.AddNetworkString("starfall_processor_clinit") -- Request code from the chip. If the chip doesn't have code yet add player to list to send when there is code. net.Receive("starfall_processor_download", function(len, ply) local proc = net.ReadEntity() - if ply:IsValid() and proc:IsValid() then + if IsValid(ply) and IsValid(proc) then proc:SendCode(ply) end end) @@ -231,14 +233,14 @@ end) net.Receive("starfall_processor_link", function(len, ply) local entIndex = net.ReadUInt(16) local linked = Entity(entIndex) - if linked.link and linked.link:IsValid() then + if IsValid(linked.link) then SF.LinkEnt(linked, linked.link, ply) end end) net.Receive("starfall_processor_kill", function(len, ply) local target = net.ReadEntity() - if ply:IsAdmin() and target:IsValid() and target:GetClass()=="starfall_processor" then + if ply:IsAdmin() and IsValid(target) and target:GetClass()=="starfall_processor" then target:Error({message = "Killed by admin", traceback = ""}) net.Start("starfall_processor_kill") net.WriteEntity(target) @@ -248,7 +250,7 @@ end) net.Receive("starfall_processor_clinit", function(len, ply) local proc = net.ReadEntity() - if ply:IsValid() and proc:IsValid() then + if IsValid(ply) and IsValid(proc) then local instance = proc.instance if instance then instance:runScriptHook("clientinitialized", instance.Types.Player.Wrap(ply)) diff --git a/lua/entities/starfall_processor/shared.lua b/lua/entities/starfall_processor/shared.lua index 8114108ae..0a284da08 100644 --- a/lua/entities/starfall_processor/shared.lua +++ b/lua/entities/starfall_processor/shared.lua @@ -16,6 +16,7 @@ ENT.States = { None = 3, } +local IsValid = FindMetaTable("Entity").IsValid function ENT:Compile() if self.instance then @@ -44,7 +45,7 @@ function ENT:Compile() self.instance = instance instance.runOnError = function(err) -- Have to make sure it's valid because the chip can be deleted before deinitialization and trigger errors - if self:IsValid() then + if IsValid(self) then self:Error(err) end end @@ -191,7 +192,7 @@ properties.Add( "starfall", { Order = 999, MenuIcon = "icon16/wrench.png", -- We should create an icon Filter = function( self, ent, ply ) - if not (ent and ent:IsValid()) then return false end + if not IsValid(ent) then return false end if not gamemode.Call( "CanProperty", ply, "starfall", ent ) then return false end return ent.Starfall or ent.link and ent.link.Starfall end, @@ -225,7 +226,7 @@ net.Receive("starfall_hud_set_enabled" , function() local chip = net.ReadEntity() local activator = net.ReadEntity() local enabled = net.ReadBool() - if ply:IsValid() and ply:IsPlayer() and chip:IsValid() and chip.ActiveHuds then + if IsValid(ply) and ply:IsPlayer() and IsValid(chip) and chip.ActiveHuds then SF.EnableHud(ply, chip, activator, enabled, true) end end) @@ -255,14 +256,14 @@ end if SERVER then function SF.EnableHud(ply, chip, activator, enabled, dontsync) local huds = chip.ActiveHuds - if activator and activator:IsValid() then + if IsValid(activator) then local n = "SF_HUD"..ply:EntIndex()..":"..activator:EntIndex() local lockController = isVehicleOrHudControlsLocked(activator) local function disconnect(sync) huds[ply] = nil hook.Remove("EntityRemoved", n) ply:SetViewEntity() - if lockController and lockController:IsValid() and lockController.link and lockController.link:IsValid() then + if IsValid(lockController) and IsValid(lockController.link) then net.Start("starfall_lock_control") net.WriteEntity(lockController.link) net.WriteBool(false) @@ -276,7 +277,7 @@ if SERVER then if enabled then huds[ply] = true hook.Add("EntityRemoved",n,function(e) if e==ply or e==activator then disconnect(true) end end) - if lockController and lockController:IsValid() and lockController.link and lockController.link:IsValid() then + if IsValid(lockController) and IsValid(lockController.link) then net.Start("starfall_lock_control") net.WriteEntity(lockController.link) net.WriteBool(true) @@ -300,7 +301,7 @@ else chip.ActiveHuds[ply] = enabled if changed then - local enabledBy = chip.owner and chip.owner:IsValid() and (" by "..chip.owner:Nick()) or "" + local enabledBy = IsValid(chip.owner) and (" by "..chip.owner:Nick()) or "" if enabled then if (Hint_FirstPrint) then LocalPlayer():ChatPrint("Starfall HUD enabled"..enabledBy..". NOTE: Type 'sf_hud_unlink' in the console to disconnect yourself from all HUDs.") @@ -337,13 +338,13 @@ function SF.LinkEnt(self, ent, transmit) local oldlink = self.link self.link = ent - if oldlink and oldlink:IsValid() then + if IsValid(oldlink) then local instance = oldlink.instance if instance then instance:runScriptHook("componentunlinked", instance.WrapObject(self)) end end - if ent and ent:IsValid() then + if IsValid(ent) then local instance = ent.instance if instance then instance:runScriptHook("componentlinked", instance.WrapObject(self)) @@ -353,7 +354,7 @@ function SF.LinkEnt(self, ent, transmit) if SERVER and (changed or transmit) then net.Start("starfall_processor_link") net.WriteUInt(self:EntIndex(), 16) - net.WriteUInt(ent and ent:IsValid() and ent:EntIndex() or 0, 16) + net.WriteUInt(IsValid(ent) and ent:EntIndex() or 0, 16) if transmit then net.Send(transmit) else net.Broadcast() end end end diff --git a/lua/entities/starfall_prop/cl_init.lua b/lua/entities/starfall_prop/cl_init.lua index 48bb87ed0..f84003d70 100644 --- a/lua/entities/starfall_prop/cl_init.lua +++ b/lua/entities/starfall_prop/cl_init.lua @@ -4,6 +4,9 @@ ENT.RenderGroup = RENDERGROUP_OPAQUE ENT.DefaultMaterial = Material( "models/wireframe" ) ENT.Material = ENT.DefaultMaterial +local IsValid = FindMetaTable("Entity").IsValid +local IsValidPhys = FindMetaTable("PhysObj").IsValid + function ENT:Initialize() self.rendermesh = Mesh(self.Material) self.rendermeshloaded = false @@ -20,7 +23,7 @@ end function ENT:Think() local physobj = self:GetPhysicsObject() - if physobj:IsValid() then + if IsValidPhys(physobj) then physobj:SetPos( self:GetPos() ) physobj:SetAngles( self:GetAngles() ) physobj:EnableMotion(false) @@ -55,7 +58,7 @@ function ENT:OnRemove() local mesh = self.rendermesh if mesh then timer.Simple(0, function() - if not self:IsValid() then + if not IsValid(self) then mesh:Destroy() end end) @@ -67,7 +70,7 @@ net.Receive("starfall_custom_prop", function() local self, data local function applyData() - if not (self and self:IsValid() and data and not self.rendermeshloaded) then return end + if not (IsValid(self) and data and not self.rendermeshloaded) then return end local stream = SF.StringStream(data) local physmesh = {} local mins, maxs = Vector(math.huge, math.huge, math.huge), Vector(-math.huge, -math.huge, -math.huge) @@ -88,7 +91,7 @@ net.Receive("starfall_custom_prop", function() self:BuildPhysics(physmesh) local phys = self:GetPhysicsObject() - if phys:IsValid() then + if IsValidPhys(phys) then local convexes = phys:GetMeshConvexes() local rendermesh = convexes[1] for i=2, #convexes do diff --git a/lua/entities/starfall_screen/cl_init.lua b/lua/entities/starfall_screen/cl_init.lua index 5d8cc4484..4b1e98e44 100644 --- a/lua/entities/starfall_screen/cl_init.lua +++ b/lua/entities/starfall_screen/cl_init.lua @@ -3,6 +3,7 @@ include("shared.lua") ENT.RenderGroup = RENDERGROUP_BOTH local render = render +local IsValid = FindMetaTable("Entity").IsValid surface.CreateFont("Starfall_ErrorFont", { font = "arial", @@ -58,7 +59,7 @@ function ENT:SetScreenMatrix(info) end function ENT:RenderScreen() - if (self.link and self.link:IsValid()) then + if IsValid(self.link) then local instance = self.link.instance if instance then if SF.Permissions.hasAccess(instance, nil, "render.screen") then diff --git a/lua/entities/starfall_screen/init.lua b/lua/entities/starfall_screen/init.lua index ed4585aa1..becf8fc65 100644 --- a/lua/entities/starfall_screen/init.lua +++ b/lua/entities/starfall_screen/init.lua @@ -2,6 +2,7 @@ AddCSLuaFile('cl_init.lua') AddCSLuaFile('shared.lua') include('shared.lua') +local IsValid = FindMetaTable("Entity").IsValid function ENT:Initialize() self.BaseClass.Initialize(self) @@ -44,7 +45,7 @@ end function ENT:PreEntityCopy() if self.EntityMods then self.EntityMods.SFLink = nil end - if (self.link and self.link:IsValid()) then + if IsValid(self.link) then duplicator.StoreEntityModifier(self, "SFLink", { link = self.link:EntIndex() }) end end @@ -54,7 +55,7 @@ function ENT:PostEntityPaste(ply, ent, CreatedEntities) local info = ent.EntityMods.SFLink if info.link then local e = CreatedEntities[info.link] - if (e and e:IsValid()) then + if IsValid(e) then SF.LinkEnt(self, e) end end diff --git a/lua/starfall/libs_sh/builtins.lua b/lua/starfall/libs_sh/builtins.lua index 61fd0641c..8899c5e7a 100644 --- a/lua/starfall/libs_sh/builtins.lua +++ b/lua/starfall/libs_sh/builtins.lua @@ -1,6 +1,7 @@ -- Global to all starfalls local checkluatype = SF.CheckLuaType local dgetmeta = debug.getmetatable +local IsValid = FindMetaTable("Entity").IsValid SF.Permissions.registerPrivilege("console.command", "Console command", "Allows the starfall to run console commands") @@ -369,7 +370,7 @@ if CLIENT then function builtins_library.sendPermissionRequest() if not SF.IsHUDActive(instance.entity) then SF.Throw("Player isn't connected to HUD!", 2) end if sentPermRequest then SF.Throw("Can only send the permission request once!", 2) end - if instance.permissionRequest and not SF.Permissions.permissionRequestSatisfied( instance ) and not IsValid(SF.permPanel) then + if instance.permissionRequest and not SF.Permissions.permissionRequestSatisfied( instance ) and not (SF.permPanel and SF.permPanel:IsValid()) then sentPermRequest = true local pnl = vgui.Create("SFChipPermissions") if pnl then @@ -605,7 +606,7 @@ else function builtins_library.setName(name) checkluatype(name, TYPE_STRING) local e = instance.entity - if (e and e:IsValid()) then + if IsValid(e) then e.name = string.sub(name, 1, 256) end end @@ -616,7 +617,7 @@ else function builtins_library.setAuthor(author) checkluatype(author, TYPE_STRING) local e = instance.entity - if (e and e:IsValid()) then + if IsValid(e) then e.author = string.sub(author, 1, 256) end end @@ -1185,7 +1186,7 @@ function builtins_library.enableHud(ply, active) SF.EnableHud(ply, instance.entity, nil, active) else local vehicle = ply:GetVehicle() - if vehicle:IsValid() and SF.Permissions.getOwner(vehicle)==instance.player then + if IsValid(vehicle) and SF.Permissions.getOwner(vehicle)==instance.player then SF.EnableHud(ply, instance.entity, vehicle, active) else SF.Throw("Player must be sitting in owner's vehicle or be owner of the chip!", 2) diff --git a/lua/starfall/libs_sh/darkrp2.lua b/lua/starfall/libs_sh/darkrp2.lua index d24b90cbc..b70ba9953 100644 --- a/lua/starfall/libs_sh/darkrp2.lua +++ b/lua/starfall/libs_sh/darkrp2.lua @@ -11,6 +11,7 @@ end) local checkluatype = SF.CheckLuaType local checkpattern = SF.CheckPattern local registerprivilege = SF.Permissions.registerPrivilege +local IsValid = FindMetaTable("Entity").IsValid -- Under normal circumstances, an API change could introduce security -- vulnerabilities. Suppose a DarkRP function changes to also return a Player @@ -94,8 +95,8 @@ function requestClass:__tostring() return string.format( "%s from %s to %s, expiring in %.02f seconds", DarkRP.formatMoney(self.amount), - self.sender:IsValid() and self.sender:SteamID() or "", - self.receiver:IsValid() and self.receiver:SteamID() or "", + IsValid(self.sender) and self.sender:SteamID() or "", + IsValid(self.receiver) and self.receiver:SteamID() or "", self.expiry-CurTime() ) end @@ -175,7 +176,7 @@ if SERVER then for sender, requestsForPlayer in pairs(self.requests) do if IsValid(sender) then for receiver, request in pairs(requestsForPlayer) do - if not receiver:IsValid() then + if not IsValid(receiver) then self:pop(sender, receiver, true) printDebug("SF: Removed money request because the receiver was invalid.", request) if request.callbackFailure then @@ -225,7 +226,7 @@ if SERVER then -- Console commands for managing money requests local function chatPrint(ply, ...) local message = string.format(...) - if ply:IsValid() and ply:IsPlayer() then + if IsValid(ply) and ply:IsPlayer() then ply:PrintMessage(HUD_PRINTCONSOLE, message) else print(message) @@ -237,14 +238,14 @@ if SERVER then return chatPrint(executor, "sf_moneyrequest: malformed parameters (do \"help sf_moneyrequest\")") end sender = sender == 0 and executor or Entity(sender) - if not (sender:IsValid() and sender:IsPlayer()) then + if not (IsValid(sender) and sender:IsPlayer()) then return chatPrint(executor, "sf_moneyrequest: invalid sender") end receiver = Entity(receiver) - if not (receiver:IsValid() and receiver:IsPlayer()) then + if not (IsValid(receiver) and receiver:IsPlayer()) then return chatPrint(executor, "sf_moneyrequest: invalid receiver") end - if executor:IsValid() and sender ~= executor and not executor:IsSuperAdmin() then + if IsValid(executor) and sender ~= executor and not executor:IsSuperAdmin() then return chatPrint(executor, "sf_moneyrequest: only superadmins can interact with other people's money requests") end local request = manager:pop(sender, receiver) @@ -380,7 +381,7 @@ else net.Receive("sf_moneyrequest2", function() local request = manager:receive() local receiver, amount, expiry, message = request.receiver, request.amount, request.expiry, request.message - if not receiver:IsValid() or amount == 0 or expiry <= CurTime() then + if not IsValid(receiver) or amount == 0 or expiry <= CurTime() then printDebug("SF: Ignoring malformed request.", request) end printDebug("SF: Received money request.", request) diff --git a/lua/starfall/libs_sh/entities.lua b/lua/starfall/libs_sh/entities.lua index 067cd6bb7..2dc57426d 100644 --- a/lua/starfall/libs_sh/entities.lua +++ b/lua/starfall/libs_sh/entities.lua @@ -1,6 +1,9 @@ -- Global to all starfalls local checkluatype = SF.CheckLuaType local registerprivilege = SF.Permissions.registerPrivilege +local IsValid = FindMetaTable("Entity").IsValid +local IsValidPhys = FindMetaTable("PhysObj").IsValid +local IsWorld = FindMetaTable("Entity").IsWorld registerprivilege("entities.setParent", "Parent", "Allows the user to parent an entity to another entity", { entities = {} }) registerprivilege("entities.setRenderProperty", "RenderProperty", "Allows the user to change the rendering of an entity", { client = (CLIENT and {} or nil), entities = {} }) @@ -74,7 +77,7 @@ local swrap, sunwrap = instance.Types.SurfaceInfo.Wrap, instance.Types.SurfaceIn local function getent(self) local ent = eunwrap(self) - if ent:IsValid() or ent:IsWorld() then + if IsValid(ent) or IsWorld(ent) then return ent else SF.Throw("Entity is not valid.", 3) @@ -86,8 +89,7 @@ instance.Types.Entity.GetEntity = getent -- @return string String representation of the entity function ent_meta:__tostring() local ent = eunwrap(self) - if not ent then return "(null entity)" - else return tostring(ent) end + return IsValid(ent) and tostring(ent) or "(null entity)" end -- ------------------------- Methods ------------------------- -- @@ -733,7 +735,7 @@ end -- @return PhysObj The main physics object of the entity function ents_methods:getPhysicsObject() local ent = getent(self) - if ent:IsWorld() then SF.Throw("Cannot get the world physobj.", 2) end + if IsWorld(ent) then SF.Throw("Cannot get the world physobj.", 2) end return pwrap(ent:GetPhysicsObject()) end @@ -820,14 +822,7 @@ end -- @shared -- @return boolean True if valid, false if not function ents_methods:isValid() - local ent = eunwrap(self) - if ent then - local isValid = ent.IsValid - if isValid then - return isValid(ent) - end - end - return false + return IsValid(eunwrap(self)) end --- Checks if an entity is a player. @@ -945,7 +940,7 @@ if SERVER then local plys = {} for ply, _ in pairs(ent.ErroredPlayers) do - if ply:IsValid() then + if IsValid(ply) then table.insert(plys, plywrap(ply)) end end @@ -1175,7 +1170,7 @@ end function ents_methods:getMassCenter() local ent = getent(self) local phys = ent:GetPhysicsObject() - if not phys:IsValid() then SF.Throw("Physics object is invalid", 2) end + if not IsValidPhys(phys) then SF.Throw("Physics object is invalid", 2) end return vwrap(phys:GetMassCenter()) end @@ -1185,7 +1180,7 @@ end function ents_methods:getMassCenterW() local ent = getent(self) local phys = ent:GetPhysicsObject() - if not phys:IsValid() then SF.Throw("Physics object is invalid", 2) end + if not IsValidPhys(phys) then SF.Throw("Physics object is invalid", 2) end return vwrap(ent:LocalToWorld(phys:GetMassCenter())) end @@ -1202,7 +1197,7 @@ end function ents_methods:getMass() local ent = getent(self) local phys = ent:GetPhysicsObject() - if not phys:IsValid() then SF.Throw("Physics object is invalid", 2) end + if not IsValidPhys(phys) then SF.Throw("Physics object is invalid", 2) end return phys:GetMass() end @@ -1213,7 +1208,7 @@ end function ents_methods:getInertia() local ent = getent(self) local phys = ent:GetPhysicsObject() - if not phys:IsValid() then SF.Throw("Physics object is invalid", 2) end + if not IsValidPhys(phys) then SF.Throw("Physics object is invalid", 2) end return vwrap(phys:GetInertia()) end @@ -1238,7 +1233,7 @@ end -- @return Vector The angular velocity as a vector function ents_methods:getAngleVelocity() local phys = getent(self):GetPhysicsObject() - if not phys:IsValid() then SF.Throw("Physics object is invalid", 2) end + if not IsValidPhys(phys) then SF.Throw("Physics object is invalid", 2) end return vwrap(phys:GetAngleVelocity()) end @@ -1247,7 +1242,7 @@ end -- @return Angle The angular velocity as an angle function ents_methods:getAngleVelocityAngle() local phys = getent(self):GetPhysicsObject() - if not phys:IsValid() then SF.Throw("Physics object is invalid", 2) end + if not IsValidPhys(phys) then SF.Throw("Physics object is invalid", 2) end local vec = phys:GetAngleVelocity() return awrap(Angle(vec.y, vec.z, vec.x)) end diff --git a/lua/starfall/libs_sh/hologram.lua b/lua/starfall/libs_sh/hologram.lua index 651bbc1d2..1ca863992 100644 --- a/lua/starfall/libs_sh/hologram.lua +++ b/lua/starfall/libs_sh/hologram.lua @@ -1,6 +1,7 @@ -- Global to all starfalls local checkluatype = SF.CheckLuaType local registerprivilege = SF.Permissions.registerPrivilege +local IsValid = FindMetaTable("Entity").IsValid registerprivilege("hologram.modify", "Modify holograms", "Allows the user to modify holograms", { entities = {} }) registerprivilege("hologram.create", "Create hologram", "Allows the user to create holograms", CLIENT and { client = {} } or nil) @@ -78,7 +79,7 @@ end) local function getholo(self) local ent = unwrap(self) - if ent:IsValid() then + if IsValid(ent) then return ent else SF.Throw("Entity is not valid.", 3) @@ -116,7 +117,7 @@ function hologram_library.create(pos, ang, model, scale) local holoent if SERVER then holoent = ents.Create("starfall_hologram") - if holoent and holoent:IsValid() then + if IsValid(holoent) then holoent:SetPos(SF.clampPos(pos)) holoent:SetAngles(ang) holoent:SetModel(model) @@ -132,7 +133,7 @@ function hologram_library.create(pos, ang, model, scale) end else holoent = ents.CreateClientside("starfall_hologram") - if holoent and holoent:IsValid() then + if IsValid(holoent) then holoent.SFHoloOwner = ply holoent:SetPos(SF.clampPos(pos)) @@ -216,7 +217,7 @@ else holo:SetPos(pos) local sfParent = holo.sfParent - if sfParent and sfParent.parent and sfParent.parent:IsValid() then + if sfParent and IsValid(sfParent.parent) then sfParent:updateTransform() end end @@ -232,7 +233,7 @@ else holo:SetAngles(angle) local sfParent = holo.sfParent - if sfParent and sfParent.parent and sfParent.parent:IsValid() then + if sfParent and IsValid(sfParent.parent) then sfParent:updateTransform() end end @@ -513,7 +514,7 @@ function hologram_methods:remove() if CLIENT and instance.data.render.isRendering then SF.Throw("Cannot remove while in rendering hook!", 2) end local holo = getholo(self) - if not (holo:IsValid() and holo.IsSFHologram) then SF.Throw("Invalid hologram!", 2) end + if not (IsValid(holo) and holo.IsSFHologram) then SF.Throw("Invalid hologram!", 2) end checkpermission(instance, holo, "hologram.create") entList:remove(instance, holo) end diff --git a/lua/starfall/libs_sh/net.lua b/lua/starfall/libs_sh/net.lua index 9b5cd2c30..236631b1e 100644 --- a/lua/starfall/libs_sh/net.lua +++ b/lua/starfall/libs_sh/net.lua @@ -1,6 +1,7 @@ -- Global to all starfalls local net = net local checkluatype = SF.CheckLuaType +local IsValid = FindMetaTable("Entity").IsValid local streams = SF.EntityTable("playerStreams") local netBurst = SF.BurstObject("net", "net message", 5, 10, "Regen rate of net message burst in kB/sec.", "The net message burst limit in kB.", 1000 * 8) @@ -12,7 +13,7 @@ end net.Receive("SF_netmessage", function(len, ply) local ent = net.ReadEntity() - if ent:IsValid() then + if IsValid(ent) then local instance = ent.instance if instance and instance.runScriptHook then local name = net.ReadString() @@ -90,30 +91,21 @@ end --- Send a net message from client->server, or server->client. -- @shared --- @param Entity|table|nil target Optional target location to send the net message. Entity or table of targets. If nil, sends to server on client +-- @param Player|table|nil target Optional target location to send the net message. Player or table of targets. If nil, sends to server on client -- @param boolean? unreliable Optional choose whether it's more important for the message to actually reach its destination (false) or reach it as fast as possible (true). function net_library.send(target, unreliable) - if target~=nil then checkluatype(target, TYPE_TABLE) end if unreliable~=nil then checkluatype(unreliable, TYPE_BOOL) end if not netStarted then SF.Throw("net message not started", 2) end local newtarget - if SERVER then - if target then - newtarget = instance.UnwrapObject(target) - if newtarget then - if not (newtarget.IsValid and newtarget.IsPlayer and newtarget:IsValid() and newtarget:IsPlayer()) then SF.Throw("Invalid player", 2) end - else - if #target == 0 then SF.Throw("Send array is empty", 2) end - newtarget = {} - for i = 1, #target do - local pl = eunwrap(target[i]) - if pl:IsValid() and pl:IsPlayer() then - newtarget[i] = pl - else - SF.Throw("Invalid player inside send array", 2) - end - end + if SERVER and target then + checkluatype(target, TYPE_TABLE) + if debug.getmetatable(target)==instance.Types.Player then + newtarget = instance.Types.Player.GetPlayer(target) + else + newtarget = {} + for i, pl in ipairs(target) do + newtarget[i] = instance.Types.Player.GetPlayer(pl) end end end diff --git a/lua/starfall/libs_sh/npc.lua b/lua/starfall/libs_sh/npc.lua index d8f8a46a6..84579d299 100644 --- a/lua/starfall/libs_sh/npc.lua +++ b/lua/starfall/libs_sh/npc.lua @@ -1,6 +1,7 @@ -- Global to all starfalls local checkluatype = SF.CheckLuaType local registerprivilege = SF.Permissions.registerPrivilege +local IsValid = FindMetaTable("Entity").IsValid if SERVER then -- Register privileges @@ -26,27 +27,19 @@ local vec_meta, vwrap, vunwrap = instance.Types.Vector, instance.Types.Vector.Wr local getent instance:AddHook("initialize", function() - getent = instance.Types.Entity.GetEntity + getent = ent_meta.GetEntity + npc_meta.__tostring = ent_meta.__tostring end) local function getnpc(self) local ent = unwrap(self) - if ent:IsValid() then + if IsValid(ent) then return ent else SF.Throw("Entity is not valid.", 3) end end ---- Turns a NPC object into a string. --- @return string String representing the NPC. -function npc_meta:__tostring() - local ent = unwrap(self) - if not ent then return "(null entity)" - else return tostring(ent) end -end - - if SERVER then --- Adds a relationship to the npc -- @server @@ -101,7 +94,7 @@ if SERVER then checkpermission(instance, npc, "npcs.giveweapon") local weapon = npc:GetActiveWeapon() - if (weapon:IsValid()) then + if IsValid(weapon) then if (weapon:GetClass() == "weapon_" .. wep) then return end weapon:Remove() end diff --git a/lua/starfall/libs_sh/physobj.lua b/lua/starfall/libs_sh/physobj.lua index 29dd70c0e..ed6d751a9 100644 --- a/lua/starfall/libs_sh/physobj.lua +++ b/lua/starfall/libs_sh/physobj.lua @@ -1,5 +1,6 @@ -- Global to all starfalls local checkluatype = SF.CheckLuaType +local IsValidPhys = FindMetaTable("PhysObj").IsValid local function checknumber(n) if n<-1e12 or n>1e12 or n~=n then @@ -40,7 +41,7 @@ local mtx_meta, mwrap, munwrap = instance.Types.VMatrix, instance.Types.VMatrix. -- @shared -- @return boolean If the physics object is valid function physobj_methods:isValid() - return unwrap(self):IsValid() + return IsValidPhys(unwrap(self)) end --- Gets the entity attached to the physics object diff --git a/lua/starfall/libs_sh/players.lua b/lua/starfall/libs_sh/players.lua index 182915b79..fc7d637f1 100644 --- a/lua/starfall/libs_sh/players.lua +++ b/lua/starfall/libs_sh/players.lua @@ -2,6 +2,7 @@ local checkluatype = SF.CheckLuaType local checkvalidnumber = SF.CheckValidNumber local registerprivilege = SF.Permissions.registerPrivilege +local IsValid = FindMetaTable("Entity").IsValid local playerMaxScale if SERVER then @@ -87,10 +88,18 @@ SF.RegisterType("Player", false, true, FindMetaTable("Player"), "Entity") return function(instance) local checkpermission = instance.player ~= SF.Superuser and SF.Permissions.check or function() end +local player_methods, player_meta, wrap, unwrap = instance.Types.Player.Methods, instance.Types.Player, instance.Types.Player.Wrap, instance.Types.Player.Unwrap +local owrap, ounwrap = instance.WrapObject, instance.UnwrapObject +local ent_meta, ewrap, eunwrap = instance.Types.Entity, instance.Types.Entity.Wrap, instance.Types.Entity.Unwrap +local vec_meta, vwrap, vunwrap = instance.Types.Vector, instance.Types.Vector.Wrap, instance.Types.Vector.Unwrap +local ang_meta, awrap, aunwrap = instance.Types.Angle, instance.Types.Angle.Wrap, instance.Types.Angle.Unwrap +local wep_meta, wwrap, wunwrap = instance.Types.Weapon, instance.Types.Weapon.Wrap, instance.Types.Weapon.Unwrap +local veh_meta, vhwrap, vhunwrap = instance.Types.Vehicle, instance.Types.Vehicle.Wrap, instance.Types.Vehicle.Unwrap local getent instance:AddHook("initialize", function() - getent = instance.Types.Entity.GetEntity + getent = ent_meta.GetEntity + player_meta.__tostring = ent_meta.__tostring end) if SERVER then @@ -103,33 +112,15 @@ if SERVER then end) end - -local player_methods, player_meta, wrap, unwrap = instance.Types.Player.Methods, instance.Types.Player, instance.Types.Player.Wrap, instance.Types.Player.Unwrap -local owrap, ounwrap = instance.WrapObject, instance.UnwrapObject -local ent_meta, ewrap, eunwrap = instance.Types.Entity, instance.Types.Entity.Wrap, instance.Types.Entity.Unwrap -local vec_meta, vwrap, vunwrap = instance.Types.Vector, instance.Types.Vector.Wrap, instance.Types.Vector.Unwrap -local ang_meta, awrap, aunwrap = instance.Types.Angle, instance.Types.Angle.Wrap, instance.Types.Angle.Unwrap -local wep_meta, wwrap, wunwrap = instance.Types.Weapon, instance.Types.Weapon.Wrap, instance.Types.Weapon.Unwrap -local veh_meta, vhwrap, vhunwrap = instance.Types.Vehicle, instance.Types.Vehicle.Wrap, instance.Types.Vehicle.Unwrap - - local function getply(self) local ent = unwrap(self) - if ent:IsValid() then + if IsValid(ent) then return ent else SF.Throw("Entity is not valid.", 3) end end -instance.Types.Player.GetPlayer = getply - - -function player_meta:__tostring() - local ent = unwrap(self) - if not ent:IsValid() then return "(null entity)" - else return tostring(ent) end -end - +player_meta.GetPlayer = getply -- ------------------------------------------------------------------------- -- --- Returns whether the player is alive diff --git a/lua/starfall/libs_sh/vehicles.lua b/lua/starfall/libs_sh/vehicles.lua index 149fb7762..85414b09f 100644 --- a/lua/starfall/libs_sh/vehicles.lua +++ b/lua/starfall/libs_sh/vehicles.lua @@ -1,6 +1,7 @@ -- Global to all starfalls local checkluatype = SF.CheckLuaType local registerprivilege = SF.Permissions.registerPrivilege +local IsValid = FindMetaTable("Entity").IsValid if SERVER then -- Register privileges @@ -22,25 +23,22 @@ return function(instance) local checkpermission = instance.player ~= SF.Superuser and SF.Permissions.check or function() end local vehicle_methods, vehicle_meta, wrap, unwrap = instance.Types.Vehicle.Methods, instance.Types.Vehicle, instance.Types.Vehicle.Wrap, instance.Types.Vehicle.Unwrap +local ent_meta, ewrap, eunwrap = instance.Types.Entity, instance.Types.Entity.Wrap, instance.Types.Entity.Unwrap local pwrap = instance.Types.Player.Wrap +instance:AddHook("initialize", function() + vehicle_meta.__tostring = ent_meta.__tostring +end) + local function getveh(self) local ent = unwrap(self) - if ent:IsValid() then + if IsValid(ent) then return ent else SF.Throw("Entity is not valid.", 3) end end ---- Turns a vehicle into a string. --- @return string String representing the vehicle. -function vehicle_meta:__tostring() - local ent = unwrap(self) - if not ent then return "(null entity)" - else return tostring(ent) end -end - --- Returns the driver of the vehicle -- @return Player Driver of vehicle function vehicle_methods:getDriver() @@ -60,7 +58,7 @@ if SERVER then -- @server function vehicle_methods:ejectDriver() local driver = getveh(self):GetDriver() - if driver:IsValid() then + if IsValid(driver) then driver:ExitVehicle() end end @@ -71,7 +69,7 @@ if SERVER then local ent = getveh(self) checkpermission(instance, ent, "vehicle.kill") local driver = ent:GetDriver() - if driver:IsValid() then + if IsValid(driver) then driver:Kill() end end @@ -84,7 +82,7 @@ if SERVER then local ent = getveh(self) checkpermission(instance, ent, "vehicle.strip") local driver = ent:GetDriver() - if driver:IsValid() then + if IsValid(driver) then if class then driver:StripWeapon(class) else diff --git a/lua/starfall/libs_sh/weapons.lua b/lua/starfall/libs_sh/weapons.lua index 7ee151f65..8ba64c2f9 100644 --- a/lua/starfall/libs_sh/weapons.lua +++ b/lua/starfall/libs_sh/weapons.lua @@ -1,5 +1,6 @@ -- Global to all starfalls local checkluatype = SF.CheckLuaType +local IsValid = FindMetaTable("Entity").IsValid --- Weapon type @@ -14,107 +15,111 @@ return function(instance) local checkpermission = instance.player ~= SF.Superuser and SF.Permissions.check or function() end local weapon_methods, weapon_meta, wrap, unwrap = instance.Types.Weapon.Methods, instance.Types.Weapon, instance.Types.Weapon.Wrap, instance.Types.Weapon.Unwrap +local ent_meta, ewrap, eunwrap = instance.Types.Entity, instance.Types.Entity.Wrap, instance.Types.Entity.Unwrap +instance:AddHook("initialize", function() + weapon_meta.__tostring = ent_meta.__tostring +end) ---- Turns a weapon into a string. --- @return string String representing the weapon. -function weapon_meta:__tostring() +local function getwep(self) local ent = unwrap(self) - if not ent then return "(null entity)" - else return tostring(ent) end + if IsValid(ent) then + return ent + else + SF.Throw("Entity is not valid.", 3) + end end - -- ------------------------------------------------------------------------- -- --- Returns Ammo in primary clip -- @shared -- @return number Amount of ammo function weapon_methods:clip1() - return unwrap(self):Clip1() + return getwep(self):Clip1() end --- Returns Maximum ammo in primary clip -- @shared -- @return number Amount of ammo function weapon_methods:maxClip1() - return unwrap(self):GetMaxClip1() + return getwep(self):GetMaxClip1() end --- Returns Ammo in secondary clip -- @shared -- @return number Amount of ammo function weapon_methods:clip2() - return unwrap(self):Clip2() + return getwep(self):Clip2() end --- Returns Maximum ammo in secondary clip -- @shared -- @return number Amount of ammo function weapon_methods:maxClip2() - return unwrap(self):GetMaxClip2() + return getwep(self):GetMaxClip2() end --- Returns the sequence enumeration number that the weapon is playing. Must be used on a view model. -- @shared -- @return number Current activity function weapon_methods:getActivity() - return unwrap(self):GetActivity() + return getwep(self):GetActivity() end --- Returns the hold type of the weapon. -- @shared -- @return string Holdtype function weapon_methods:getHoldType() - return unwrap(self):GetHoldType() + return getwep(self):GetHoldType() end --- Gets the next time the weapon can primary fire. -- @shared -- @return number The time, relative to CurTime function weapon_methods:getNextPrimaryFire() - return unwrap(self):GetNextPrimaryFire() + return getwep(self):GetNextPrimaryFire() end --- Gets the next time the weapon can secondary fire. -- @shared -- @return number The time, relative to CurTime function weapon_methods:getNextSecondaryFire() - return unwrap(self):GetNextSecondaryFire() + return getwep(self):GetNextSecondaryFire() end --- Gets the primary ammo type of the given weapon. -- @shared -- @return number Ammo number type function weapon_methods:getPrimaryAmmoType() - return unwrap(self):GetPrimaryAmmoType() + return getwep(self):GetPrimaryAmmoType() end --- Gets the secondary ammo type of the given weapon. -- @shared -- @return number Ammo number type function weapon_methods:getSecondaryAmmoType() - return unwrap(self):GetSecondaryAmmoType() + return getwep(self):GetSecondaryAmmoType() end --- Returns whether the weapon is visible -- @shared -- @return boolean Whether the weapon is visible or not function weapon_methods:isWeaponVisible() - return unwrap(self):IsWeaponVisible() + return getwep(self):IsWeaponVisible() end --- Returns the time since a weapon was last fired at a float variable -- @shared -- @return number Time the weapon was last shot function weapon_methods:lastShootTime() - return unwrap(self):LastShootTime() + return getwep(self):LastShootTime() end --- Returns the tool mode of the toolgun -- @shared -- @return string The tool mode of the toolgun function weapon_methods:getToolMode() - local ent = unwrap(self) + local ent = getwep(self) return ent:GetClass()=="gmod_tool" and ent.Mode or "" end @@ -123,14 +128,14 @@ if CLIENT then -- @client -- @return string Display name of weapon function weapon_methods:getPrintName() - return unwrap(self):GetPrintName() + return getwep(self):GetPrintName() end --- Returns if the weapon is carried by the local player. -- @client -- @return boolean Whether or not the weapon is carried by the local player function weapon_methods:isCarriedByLocalPlayer() - return unwrap(self):IsCarriedByLocalPlayer() + return getwep(self):IsCarriedByLocalPlayer() end end diff --git a/lua/starfall/libs_sv/constraint.lua b/lua/starfall/libs_sv/constraint.lua index 6c9002b18..33c6029ce 100644 --- a/lua/starfall/libs_sv/constraint.lua +++ b/lua/starfall/libs_sv/constraint.lua @@ -1,6 +1,8 @@ -- Global to all starfalls local checkluatype = SF.CheckLuaType local registerprivilege = SF.Permissions.registerPrivilege +local IsValid = FindMetaTable("Entity").IsValid +local IsWorld = FindMetaTable("Entity").IsWorld -- Register privileges registerprivilege("constraints.weld", "Weld", "Allows the user to weld two entities", { entities = {} }) @@ -60,10 +62,10 @@ function constr_meta:__tostring() end local function check_constr_perms(ent) - if ent.Ent1 and not ent.Ent1:IsWorld() then checkpermission(instance, ent.Ent1, "entities.remove", 3) end - if ent.Ent2 and not ent.Ent2:IsWorld() then checkpermission(instance, ent.Ent2, "entities.remove", 3) end - if ent.Ent3 and not ent.Ent3:IsWorld() then checkpermission(instance, ent.Ent3, "entities.remove", 3) end - if ent.Ent4 and not ent.Ent4:IsWorld() then checkpermission(instance, ent.Ent4, "entities.remove", 3) end + if ent.Ent1 and not IsWorld(ent.Ent1) then checkpermission(instance, ent.Ent1, "entities.remove", 3) end + if ent.Ent2 and not IsWorld(ent.Ent2) then checkpermission(instance, ent.Ent2, "entities.remove", 3) end + if ent.Ent3 and not IsWorld(ent.Ent3) then checkpermission(instance, ent.Ent3, "entities.remove", 3) end + if ent.Ent4 and not IsWorld(ent.Ent4) then checkpermission(instance, ent.Ent4, "entities.remove", 3) end end --- Removes the constraint @@ -88,17 +90,13 @@ function constr_methods:isValid() end local function checkConstraint(e, t) - if e then - if e:IsValid() then - if e:GetMoveType() == MOVETYPE_VPHYSICS then - checkpermission(instance, e, t) - else - SF.Throw("Can only constrain entities with physics", 3) - end - elseif not e:IsWorld() then - SF.Throw("Invalid Entity", 3) + if IsValid(e) then + if e:GetMoveType() == MOVETYPE_VPHYSICS then + checkpermission(instance, e, t) + else + SF.Throw("Can only constrain entities with physics", 3) end - else + elseif not IsWorld(e) then SF.Throw("Invalid Entity", 3) end end @@ -494,7 +492,7 @@ function constraint_library.setElasticLength(index, e, length) if e.Elastics then local con = e.Elastics[index] - if (con and con:IsValid()) then + if IsValid(con) then con:Fire("SetSpringLength", length, 0) end end @@ -516,7 +514,7 @@ function constraint_library.setElasticDamping(index, e, damping) if e.Elastics then local con = e.Elastics[index] - if (con and con:IsValid()) then + if IsValid(con) then con:Fire("SetSpringDamping", damping, 0) end end @@ -538,7 +536,7 @@ function constraint_library.setElasticConstant(index, e, constant) if e.Elastics then local con = e.Elastics[index] - if (con and con:IsValid()) then + if IsValid(con) then con:Fire("SetSpringConstant", constant, 0) end end diff --git a/lua/starfall/libs_sv/entities.lua b/lua/starfall/libs_sv/entities.lua index 8c516bee1..621949490 100644 --- a/lua/starfall/libs_sv/entities.lua +++ b/lua/starfall/libs_sv/entities.lua @@ -2,6 +2,9 @@ local checkluatype = SF.CheckLuaType local registerprivilege = SF.Permissions.registerPrivilege local haspermission = SF.Permissions.hasAccess +local IsValid = FindMetaTable("Entity").IsValid +local IsValidPhys = FindMetaTable("PhysObj").IsValid +local isentity = isentity local huge = math.huge local abs = math.abs @@ -47,11 +50,11 @@ local cunwrap = instance.Types.Color.Unwrap local getent local collisionlisteners = {} instance:AddHook("initialize", function() - getent = instance.Types.Entity.GetEntity + getent = ent_meta.GetEntity end) instance:AddHook("deinitialize", function() for ent in pairs(collisionlisteners) do - if ent:IsValid() then + if IsValid(ent) then if ent:GetClass() ~= "starfall_prop" then ent:RemoveCallback("PhysicsCollide", ent.SF_CollisionCallback) ent.SF_CollisionCallback = nil @@ -221,7 +224,7 @@ function ents_methods:setAngleVelocity(angvel) checkvector(angvel) local phys = ent:GetPhysicsObject() - if not phys:IsValid() then SF.Throw("Physics object is invalid", 2) end + if not IsValidPhys(phys) then SF.Throw("Physics object is invalid", 2) end checkpermission(instance, ent, "entities.applyForce") @@ -236,7 +239,7 @@ function ents_methods:addAngleVelocity(angvel) checkvector(angvel) local phys = ent:GetPhysicsObject() - if not phys:IsValid() then SF.Throw("Physics object is invalid", 2) end + if not IsValidPhys(phys) then SF.Throw("Physics object is invalid", 2) end checkpermission(instance, ent, "entities.applyForce") @@ -273,7 +276,7 @@ function ents_methods:applyForceCenter(vec) checkvector(vec) local phys = ent:GetPhysicsObject() - if not phys:IsValid() then SF.Throw("Physics object is invalid", 2) end + if not IsValidPhys(phys) then SF.Throw("Physics object is invalid", 2) end checkpermission(instance, ent, "entities.applyForce") @@ -292,7 +295,7 @@ function ents_methods:applyForceOffset(force, position) checkvector(position) local phys = ent:GetPhysicsObject() - if not phys:IsValid() then SF.Throw("Physics object is invalid", 2) end + if not IsValidPhys(phys) then SF.Throw("Physics object is invalid", 2) end checkpermission(instance, ent, "entities.applyForce") @@ -308,7 +311,7 @@ function ents_methods:applyAngForce(ang) checkvector(ang) local phys = ent:GetPhysicsObject() - if not phys:IsValid() then SF.Throw("Physics object is invalid", 2) end + if not IsValidPhys(phys) then SF.Throw("Physics object is invalid", 2) end checkpermission(instance, ent, "entities.applyForce") @@ -348,7 +351,7 @@ function ents_methods:applyTorque(torque) checkvector(torque) local phys = ent:GetPhysicsObject() - if not phys:IsValid() then SF.Throw("Physics object is invalid", 2) end + if not IsValidPhys(phys) then SF.Throw("Physics object is invalid", 2) end checkpermission(instance, ent, "entities.applyForce") @@ -454,7 +457,7 @@ function ents_methods:addVelocity(vel) checkvector(vel) local phys = ent:GetPhysicsObject() - if not phys:IsValid() then SF.Throw("Physics object is invalid", 2) end + if not IsValidPhys(phys) then SF.Throw("Physics object is invalid", 2) end checkpermission(instance, ent, "entities.applyForce") @@ -556,7 +559,7 @@ function ents_methods:setMass(mass) if ent:IsPlayer() then SF.Throw("Target is a player!", 2) end checkluatype(mass, TYPE_NUMBER) local phys = ent:GetPhysicsObject() - if not phys:IsValid() then SF.Throw("Physics object is invalid", 2) end + if not IsValidPhys(phys) then SF.Throw("Physics object is invalid", 2) end checkpermission(instance, ent, "entities.setMass") @@ -572,7 +575,7 @@ function ents_methods:setInertia(vec) if ent:IsPlayer() then SF.Throw("Target is a player!", 2) end checkpermission(instance, ent, "entities.setInertia") local phys = ent:GetPhysicsObject() - if not phys:IsValid() then SF.Throw("Physics object is invalid", 2) end + if not IsValidPhys(phys) then SF.Throw("Physics object is invalid", 2) end vec = vunwrap(vec) checkvector(vec) @@ -590,7 +593,7 @@ function ents_methods:setPhysMaterial(mat) if ent:IsPlayer() then SF.Throw("Target is a player!", 2) end checkluatype(mat, TYPE_STRING) local phys = ent:GetPhysicsObject() - if not phys:IsValid() then SF.Throw("Physics object is invalid", 2) end + if not IsValidPhys(phys) then SF.Throw("Physics object is invalid", 2) end checkpermission(instance, ent, "entities.setMass") @@ -602,7 +605,7 @@ end function ents_methods:getPhysMaterial() local ent = getent(self) local phys = ent:GetPhysicsObject() - if not phys:IsValid() then SF.Throw("Physics object is invalid", 2) end + if not IsValidPhys(phys) then SF.Throw("Physics object is invalid", 2) end return phys:GetMaterial() end @@ -612,7 +615,7 @@ end function ents_methods:isValidPhys() local ent = getent(self) local phys = ent:GetPhysicsObject() - return phys:IsValid() + return IsValidPhys(phys) end --- Returns true if the entity is being held by a player. Either by Physics gun, Gravity gun or Use-key. @@ -636,7 +639,7 @@ function ents_methods:enableGravity(grav) local ent = getent(self) if ent:IsPlayer() then SF.Throw("Target is a player!", 2) end local phys = ent:GetPhysicsObject() - if not phys:IsValid() then SF.Throw("Physics object is invalid", 2) end + if not IsValidPhys(phys) then SF.Throw("Physics object is invalid", 2) end checkpermission(instance, ent, "entities.enableGravity") @@ -650,7 +653,7 @@ function ents_methods:enableDrag(drag) local ent = getent(self) if ent:IsPlayer() then SF.Throw("Target is a player!", 2) end local phys = ent:GetPhysicsObject() - if not phys:IsValid() then SF.Throw("Physics object is invalid", 2) end + if not IsValidPhys(phys) then SF.Throw("Physics object is invalid", 2) end checkpermission(instance, ent, "entities.enableDrag") @@ -663,7 +666,7 @@ function ents_methods:enableMotion(move) local ent = getent(self) if ent:IsPlayer() then SF.Throw("Target is a player!", 2) end local phys = ent:GetPhysicsObject() - if not phys:IsValid() then SF.Throw("Physics object is invalid", 2) end + if not IsValidPhys(phys) then SF.Throw("Physics object is invalid", 2) end checkpermission(instance, ent, "entities.enableMotion") @@ -682,7 +685,7 @@ end function ents_methods:isFrozen() local ent = getent(self) local phys = ent:GetPhysicsObject() - if not phys:IsValid() then SF.Throw("Physics object is invalid", 2) end + if not IsValidPhys(phys) then SF.Throw("Physics object is invalid", 2) end return not phys:IsMoveable() end @@ -693,7 +696,7 @@ function ents_methods:enableSphere(enabled, radius) local ent = getent(self) if ent:GetClass() ~= "prop_physics" then SF.Throw("This function only works for prop_physics", 2) end local phys = ent:GetPhysicsObject() - if not phys:IsValid() then SF.Throw("Physics object is invalid", 2) end + if not IsValidPhys(phys) then SF.Throw("Physics object is invalid", 2) end checkpermission(instance, ent, "entities.enableMotion") local ismove = phys:IsMoveable() @@ -745,12 +748,13 @@ function ents_methods:isWeldedTo() local constr = constraint.FindConstraint(ent, "Weld") if constr then return owrap(constr.Ent1 == ent and constr.Ent2 or constr.Ent1) - else - local parent = ent:GetParent() - if parent:IsValid() then - return owrap(parent) - end end + + local parent = ent:GetParent() + if IsValid(parent) then + return owrap(parent) + end + return nil end @@ -764,7 +768,7 @@ function ents_methods:getAllConstrained(filter) local function recursive_find(ent) if entity_lookup[ent] then return end entity_lookup[ent] = true - if ent:IsValid() then + if IsValid(ent) then entity_table[#entity_table + 1] = owrap(ent) local constraints = constraint.GetTable(ent) for _, v in pairs(constraints) do @@ -783,7 +787,7 @@ function ents_methods:getAllConstrained(filter) if not filter or filter.Wire then if istable(ent.Inputs) then for _, v in pairs(ent.Inputs) do - if isentity(v.Src) and v.Src:IsValid() then + if isentity(v.Src) and IsValid(v.Src) then recursive_find(v.Src) end end @@ -792,7 +796,7 @@ function ents_methods:getAllConstrained(filter) for _, v in pairs(ent.Outputs) do if istable(v.Connected) then for _, v in pairs(v.Connected) do - if isentity(v.Entity) and v.Entity:IsValid() then + if isentity(v.Entity) and IsValid(v.Entity) then recursive_find(v.Entity) end end @@ -854,7 +858,7 @@ function ents_methods:setUnbreakable(on) checkpermission(instance, ent, "entities.canTool") if ent:GetClass() ~= "prop_physics" then SF.Throw("setUnbreakable can only be used on prop_physics", 2) end - if not (SF.UnbreakableFilter and SF.UnbreakableFilter:IsValid()) then + if not IsValid(SF.UnbreakableFilter) then local FilterDamage = ents.FindByName("FilterDamage")[1] if not FilterDamage then FilterDamage = ents.Create( "filter_activator_name" ) diff --git a/lua/starfall/libs_sv/nextbot.lua b/lua/starfall/libs_sv/nextbot.lua index 11912ad44..da0a02e16 100644 --- a/lua/starfall/libs_sv/nextbot.lua +++ b/lua/starfall/libs_sv/nextbot.lua @@ -64,20 +64,20 @@ local entList = SF.EntManager("nextbots", "nextbots", 30, "The number of props a return function(instance) local checkpermission = instance.player ~= SF.Superuser and SF.Permissions.check or function() end +local ents_methods, ent_meta, ewrap, eunwrap = instance.Types.Entity.Methods, instance.Types.Entity, instance.Types.Entity.Wrap, instance.Types.Entity.Unwrap local nextbot_library, nb_meta, nb_methods = instance.Libraries.nextbot, instance.Types.NextBot, instance.Types.NextBot.Methods local vec_meta, vwrap, vunwrap = instance.Types.Vector, instance.Types.Vector.Wrap, instance.Types.Vector.Unwrap local navarea_methods, navarea_meta, navwrap, navunwrap = instance.Types.NavArea.Methods, instance.Types.NavArea, instance.Types.NavArea.Wrap, instance.Types.NavArea.Unwrap local nbwrap, nbunwrap = instance.Types.NextBot.Wrap, instance.Types.NextBot.Unwrap +instance:AddHook("initialize", function() + nb_meta.__tostring = ent_meta.__tostring +end) instance:AddHook("deinitialize", function() entList:deinitialize(instance, true) end) -function nb_meta:__tostring() - return "NextBot" -end - --- Creates a customizable NextBot -- @server -- @param Vector spawnpos The position the nextbot will be spawned at. diff --git a/lua/starfall/libs_sv/prop.lua b/lua/starfall/libs_sv/prop.lua index 302cee197..8ffcc1ae4 100755 --- a/lua/starfall/libs_sv/prop.lua +++ b/lua/starfall/libs_sv/prop.lua @@ -1,6 +1,8 @@ -- Global to all starfalls local checkluatype = SF.CheckLuaType local registerprivilege = SF.Permissions.registerPrivilege +local IsValid = FindMetaTable("Entity").IsValid +local IsValidPhys = FindMetaTable("PhysObj").IsValid -- Register privileges registerprivilege("prop.create", "Create prop", "Allows the user to create props") @@ -75,7 +77,7 @@ function props_library.create(pos, ang, model, frozen) for I = 0, propent:GetPhysicsObjectCount() - 1 do local obj = propent:GetPhysicsObjectNum(I) - if obj:IsValid() then + if IsValidPhys(obj) then obj:EnableMotion(not frozen) end end @@ -124,7 +126,7 @@ function props_library.createRagdoll(model, frozen) if frozen then for I = 0, ent:GetPhysicsObjectCount() - 1 do local obj = ent:GetPhysicsObjectNum(I) - if obj:IsValid() then + if IsValidPhys(obj) then obj:EnableMotion(false) end end @@ -212,7 +214,7 @@ function props_library.createCustom(pos, ang, vertices, frozen) entList:register(instance, propent) local physobj = propent:GetPhysicsObject() - if not physobj:IsValid() then + if not IsValidPhys(physobj) then SF.Throw("Custom prop generated with invalid physics object!", 2) end @@ -288,7 +290,7 @@ function props_library.createComponent(pos, ang, class, model, frozen) for I = 0, comp:GetPhysicsObjectCount() - 1 do local obj = comp:GetPhysicsObjectNum(I) - if obj:IsValid() then + if IsValidPhys(obj) then obj:EnableMotion(not frozen) end end @@ -376,7 +378,7 @@ function props_library.createSeat(pos, ang, model, frozen) entList:register(instance, prop) local phys = prop:GetPhysicsObject() - if phys:IsValid() then + if IsValidPhys(phys) then phys:EnableMotion(not frozen) end @@ -435,7 +437,7 @@ function props_library.createSent(pos, ang, class, frozen, data) end entity = ents.Create(swep.ClassName) - if entity and entity:IsValid() then + if IsValid(entity) then entity:SetPos(pos) entity:SetAngles(ang) entity:Spawn() @@ -454,7 +456,7 @@ function props_library.createSent(pos, ang, class, frozen, data) entity = sent.t.SpawnFunction( sent.t, ply, SF.dumbTrace(NULL, pos), class ) else entity = ents.Create( class ) - if entity and entity:IsValid() then + if IsValid(entity) then entity:SetPos(pos) entity:SetAngles(ang) entity:Spawn() @@ -471,7 +473,7 @@ function props_library.createSent(pos, ang, class, frozen, data) entity = ents.Create(npc.Class) - if entity and entity:IsValid() then + if IsValid(entity) then if (npc.Model) then entity:SetModel(npc.Model) end @@ -503,7 +505,7 @@ function props_library.createSent(pos, ang, class, frozen, data) entity = ents.Create(vehicle.Class) - if entity and entity:IsValid() then + if IsValid(entity) then entity:SetModel(vehicle.Model) if (vehicle.Model == "models/buggy.mdl") then entity:SetKeyValue("vehiclescript", "scripts/vehicles/jeep_test.txt") @@ -639,13 +641,13 @@ function props_library.createSent(pos, ang, class, frozen, data) end end - if entity and entity:IsValid() then + if IsValid(entity) then entList:register(instance, entity) if CPPI then entity:CPPISetOwner(ply == SF.Superuser and NULL or ply) end local phys = entity:GetPhysicsObject() - if phys:IsValid() then + if IsValidPhys(phys) then phys:EnableMotion(not frozen) end diff --git a/lua/starfall/libs_sv/wire.lua b/lua/starfall/libs_sv/wire.lua index 45fd6db19..c0e43dfe6 100644 --- a/lua/starfall/libs_sv/wire.lua +++ b/lua/starfall/libs_sv/wire.lua @@ -1,6 +1,7 @@ local checkluatype = SF.CheckLuaType local registerprivilege = SF.Permissions.registerPrivilege local debug_getmetatable = debug.getmetatable +local IsValid = FindMetaTable("Entity").IsValid -- Register privileges registerprivilege("wire.setOutputs", "Set outputs", "Allows the user to specify the set of outputs") @@ -44,12 +45,24 @@ return function(instance) if not (WireLib and WireLib.CreateInputs) then return end local checkpermission = instance.player ~= SF.Superuser and SF.Permissions.check or function() end +local wire_library = instance.Libraries.wire + +local owrap, ounwrap = instance.WrapObject, instance.UnwrapObject +local ents_methods, ent_meta, ewrap, eunwrap = instance.Types.Entity.Methods, instance.Types.Entity, instance.Types.Entity.Wrap, instance.Types.Entity.Unwrap +local wirelink_methods, wirelink_meta, wlwrap, wlunwrap = instance.Types.Wirelink.Methods, instance.Types.Wirelink, instance.Types.Wirelink.Wrap, instance.Types.Wirelink.Unwrap +local vec_meta, vwrap, vunwrap = instance.Types.Vector, instance.Types.Vector.Wrap, instance.Types.Vector.Unwrap +local vec2_meta, v2wrap, v2unwrap = instance.Types.Vector2, instance.Types.Vector2.Wrap, instance.Types.Vector2.Unwrap +local ang_meta, awrap, aunwrap = instance.Types.Angle, instance.Types.Angle.Wrap, instance.Types.Angle.Unwrap +local col_meta, cwrap, cunwrap = instance.Types.Color, instance.Types.Color.Wrap, instance.Types.Color.Unwrap +local wirelink_meta, wlwrap, wlunwrap = instance.Types.Wirelink, instance.Types.Wirelink.Wrap, instance.Types.Wirelink.Unwrap +local COLOR_WHITE = Color(255, 255, 255) + local wirecache = {} local wirecachevals = {} local getent instance:AddHook("initialize", function() - getent = instance.Types.Entity.GetEntity + getent = ent_meta.GetEntity local ent = instance.entity if ent.Inputs == nil then @@ -87,22 +100,9 @@ instance:AddHook("initialize", function() end end) - -local wire_library = instance.Libraries.wire - -local owrap, ounwrap = instance.WrapObject, instance.UnwrapObject -local ents_methods, ent_meta, ewrap, eunwrap = instance.Types.Entity.Methods, instance.Types.Entity, instance.Types.Entity.Wrap, instance.Types.Entity.Unwrap -local wirelink_methods, wirelink_meta, wlwrap, wlunwrap = instance.Types.Wirelink.Methods, instance.Types.Wirelink, instance.Types.Wirelink.Wrap, instance.Types.Wirelink.Unwrap -local vec_meta, vwrap, vunwrap = instance.Types.Vector, instance.Types.Vector.Wrap, instance.Types.Vector.Unwrap -local vec2_meta, v2wrap, v2unwrap = instance.Types.Vector2, instance.Types.Vector2.Wrap, instance.Types.Vector2.Unwrap -local ang_meta, awrap, aunwrap = instance.Types.Angle, instance.Types.Angle.Wrap, instance.Types.Angle.Unwrap -local col_meta, cwrap, cunwrap = instance.Types.Color, instance.Types.Color.Wrap, instance.Types.Color.Unwrap -local wirelink_meta, wlwrap, wlunwrap = instance.Types.Wirelink, instance.Types.Wirelink.Wrap, instance.Types.Wirelink.Unwrap -local COLOR_WHITE = Color(255, 255, 255) - local function getwl(self) local wl = wlunwrap(self) - if wl:IsValid() then + if IsValid(wl) then return wl else SF.Throw("Wirelink is not valid", 3) @@ -497,8 +497,8 @@ function wire_library.create(entI, entO, inputname, outputname, width, color, ma local entI = eunwrap(entI) local entO = eunwrap(entO) - if not (entI and entI:IsValid()) then SF.Throw("Invalid source") end - if not (entO and entO:IsValid()) then SF.Throw("Invalid target") end + if not IsValid(entI) then SF.Throw("Invalid source") end + if not IsValid(entO) then SF.Throw("Invalid target") end checkpermission(instance, entI, "wire.createWire") checkpermission(instance, entO, "wire.createWire") @@ -528,7 +528,7 @@ end function wire_library.delete(entI, inputname) checkluatype(inputname, TYPE_STRING) - local entI = getent(entI) + entI = getent(entI) checkpermission(instance, entI, "wire.deleteWire") @@ -541,14 +541,12 @@ end local function parseEntity(ent, io) if ent then - ent = eunwrap(ent) + ent = getent(ent) checkpermission(instance, ent, "wire.get" .. io) else - ent = instance.entity or nil + ent = instance.entity end - if not (ent and ent:IsValid()) then SF.Throw("Invalid source") end - local names, types = {}, {} for k, v in pairs(ent[io]) do if k ~= "" then @@ -580,8 +578,7 @@ end -- @param Entity ent Wire entity -- @return Wirelink Wirelink of the entity function wire_library.getWirelink(ent) - ent = eunwrap(ent) - if not ent:IsValid() then return end + ent = getent(ent) checkpermission(instance, ent, "wire.wirelink") if not ent.extended then @@ -606,9 +603,7 @@ wirelink_meta.__index = function(self, k) if wirelink_methods[k] then return wirelink_methods[k] else - local wl = wlunwrap(self) - if not wl:IsValid() or not wl.extended then return end -- TODO: What is wl.extended? - + local wl = getwl(self) if isnumber(k) then return wl.ReadCell and wl:ReadCell(k) or nil else @@ -624,8 +619,7 @@ end -- @param any val Value to set at the index wirelink_meta.__newindex = function(self, k, v) checkpermission(instance, nil, "wire.wirelink.write") - local wl = wlunwrap(self) - if not wl:IsValid() or not wl.extended then return end -- TODO: What is wl.extended? + local wl = getwl(self) if isnumber(k) then checkluatype(v, TYPE_NUMBER) if not wl.WriteCell then return @@ -724,7 +718,7 @@ function wirelink_methods:isWired(name) checkluatype(name, TYPE_STRING) local wl = getwl(self) local input = wl.Inputs[name] - if input and input.Src and input.Src:IsValid() then return true + if input and IsValid(input.Src) then return true else return false end end @@ -735,7 +729,7 @@ function wirelink_methods:getWiredTo(name) checkluatype(name, TYPE_STRING) local wl = getwl(self) local input = wl.Inputs[name] - if input and input.Src and input.Src:IsValid() then + if input and IsValid(input.Src) then return owrap(input.Src) end end @@ -747,7 +741,7 @@ function wirelink_methods:getWiredToName(name) checkluatype(name, TYPE_STRING) local wl = getwl(self) local input = wl.Inputs[name] - if input and input.Src and input.Src:IsValid() then + if input and IsValid(input.Src) then return input.SrcId end end diff --git a/lua/starfall/permissions/providers_cl/client.lua b/lua/starfall/permissions/providers_cl/client.lua index f39701ca0..90ee3080e 100644 --- a/lua/starfall/permissions/providers_cl/client.lua +++ b/lua/starfall/permissions/providers_cl/client.lua @@ -1,5 +1,7 @@ --- Provides permissions for clients +local IsValid = FindMetaTable("Entity").IsValid + local P = {} P.id = "client" P.name = "Client Permissions" @@ -13,7 +15,7 @@ P.checks = { end, function(instance, target, key) local owner = instance.player - return LocalPlayer()==owner or (owner and owner:IsValid() and owner:GetFriendStatus()=="friend"), "This function can only be used on the player's or their friends' chips" + return LocalPlayer()==owner or (IsValid(owner) and owner:GetFriendStatus()=="friend"), "This function can only be used on the player's or their friends' chips" end, "allow", "block" diff --git a/lua/starfall/permissions/providers_sh/entity.lua b/lua/starfall/permissions/providers_sh/entity.lua index 35b978ad4..bb4b5ed10 100644 --- a/lua/starfall/permissions/providers_sh/entity.lua +++ b/lua/starfall/permissions/providers_sh/entity.lua @@ -1,6 +1,6 @@ --- Provides permissions for entities based on CPPI if present -local isentity = isentity +local IsValid = FindMetaTable("Entity").IsValid local owneraccess @@ -26,7 +26,7 @@ hook.Add("Initialize","SF_PPInitialize",function() if SERVER then P.checks = { function(instance, target) - if isentity(target) and target:IsValid() then + if IsValid(target) then if target == instance.player and owneraccess:GetBool() then return true end if instance.player:IsSuperAdmin() then return true end if target:CPPIGetOwner()==instance.player then @@ -39,7 +39,7 @@ hook.Add("Initialize","SF_PPInitialize",function() end end, function(instance, target) - if isentity(target) and target:IsValid() then + if IsValid(target) then if target == instance.player and owneraccess:GetBool() then return true end if target:CPPICanTool(instance.player, "starfall_ent_lib") then return true @@ -51,7 +51,7 @@ hook.Add("Initialize","SF_PPInitialize",function() end end, function(instance, target) - if isentity(target) and target:IsValid() then + if IsValid(target) then if target == instance.player and owneraccess:GetBool() then return true end if target:CPPICanPhysgun(instance.player) then return true @@ -67,7 +67,7 @@ hook.Add("Initialize","SF_PPInitialize",function() else P.checks = { function(instance, target) - if isentity(target) and target:IsValid() then + if IsValid(target) then if target==instance.player or LocalPlayer()==instance.player or instance.player:IsSuperAdmin() then return true end if target:CPPIGetOwner()==instance.player then return true @@ -79,7 +79,7 @@ hook.Add("Initialize","SF_PPInitialize",function() end end, function(instance, target) - if isentity(target) and target:IsValid() then + if IsValid(target) then if target==instance.player or LocalPlayer()==instance.player or instance.player:IsSuperAdmin() then return true end if target:CPPICanTool(instance.player, "starfall_ent_lib") then return true @@ -91,7 +91,7 @@ hook.Add("Initialize","SF_PPInitialize",function() end end, function(instance, target) - if isentity(target) and target:IsValid() then + if IsValid(target) then if target==instance.player or LocalPlayer()==instance.player or instance.player:IsSuperAdmin() then return true end if target:CPPICanPhysgun(instance.player) then return true @@ -112,7 +112,7 @@ hook.Add("Initialize","SF_PPInitialize",function() if SERVER then P.checks = { function(instance, target) - if isentity(target) and target:IsValid() then + if IsValid(target) then if target == instance.player and owneraccess:GetBool() then return true end if instance.player:IsSuperAdmin() then return true end if P.props[target]==instance.player then @@ -125,7 +125,7 @@ hook.Add("Initialize","SF_PPInitialize",function() end end, function(instance, target) - if isentity(target) and target:IsValid() then + if IsValid(target) then if target == instance.player and owneraccess:GetBool() then return true end local pos = target:GetPos() if hook.Run("CanTool", instance.player, SF.dumbTrace(target), "starfall_ent_lib") ~= false then @@ -138,7 +138,7 @@ hook.Add("Initialize","SF_PPInitialize",function() end end, function(instance, target) - if isentity(target) and target:IsValid() then + if IsValid(target) then if target == instance.player and owneraccess:GetBool() then return true end if hook.Run("PhysgunPickup", instance.player, target) ~= false then -- Some mods expect a release when there's a pickup involved. @@ -167,7 +167,7 @@ hook.Add("Initialize","SF_PPInitialize",function() if(cleanup) then local backupcleanupAdd = cleanup.Add function cleanup.Add(ply, enttype, ent) - if ent and ent:IsValid() and ply:IsPlayer() then + if IsValid(ent) and ply:IsPlayer() then PropOwn(ply, ent) end backupcleanupAdd(ply, enttype, ent) @@ -204,7 +204,7 @@ hook.Add("Initialize","SF_PPInitialize",function() else P.checks = { function(instance, target) - if isentity(target) and target:IsValid() then + if IsValid(target) then if target==instance.player or LocalPlayer()==instance.player or instance.player:IsSuperAdmin() then return true end local owner = target:GetNWEntity("SFPP") if owner ~= NULL then diff --git a/lua/starfall/sflib.lua b/lua/starfall/sflib.lua index c32e422f7..0cab6bfce 100644 --- a/lua/starfall/sflib.lua +++ b/lua/starfall/sflib.lua @@ -8,6 +8,7 @@ SF.ResourceCounters = {} SF.Superuser = {IsValid = function() return false end, SteamID64 = function() return "Superuser" end} local dgetmeta = debug.getmetatable local TypeID = TypeID +local IsValid = FindMetaTable("Entity").IsValid -- Make sure this is done after metatables have been set hook.Add("InitPostEntity","SF_SanitizeTypeMetatables",function() @@ -108,7 +109,7 @@ function SF.EntityTable(key, destructor, dontwait) else e:CallOnRemove("SF_" .. key, function() timer.Simple(0, function() - if t[e] and not e:IsValid() then + if t[e] and not IsValid(e) then if destructor then destructor(e, v) end t[e] = nil end @@ -161,7 +162,7 @@ SF.BurstObject = { return ret end, use = function(self, ply, amount) - if ply:IsValid() or ply==SF.Superuser then + if IsValid(ply) or ply==SF.Superuser then local obj = self:get(ply) local new = self:calc(obj) - amount if new < 0 and ply~=SF.Superuser then @@ -173,7 +174,7 @@ SF.BurstObject = { end end, check = function(self, ply) - if ply:IsValid() or ply==SF.Superuser then + if IsValid(ply) or ply==SF.Superuser then local obj = self:get(ply) obj.val = self:calc(obj) return obj.val @@ -220,7 +221,7 @@ setmetatable(SF.BurstObject, SF.BurstObject) SF.LimitObject = { __index = { use = function(self, ply, amount) - if ply:IsValid() or ply==SF.Superuser then + if IsValid(ply) or ply==SF.Superuser then local obj = self:get(ply) local new = obj.val + amount if new > self.max and ply~=SF.Superuser then @@ -232,7 +233,7 @@ SF.LimitObject = { end end, checkuse = function(self, ply, amount) - if ply:IsValid() or ply==SF.Superuser then + if IsValid(ply) or ply==SF.Superuser then local obj = self:get(ply) if obj.val + amount > self.max and ply~=SF.Superuser then SF.Throw("The ".. self.name .." limit has been reached. (".. self.max ..")", 3) @@ -242,7 +243,7 @@ SF.LimitObject = { end end, check = function(self, ply) - if ply:IsValid() or ply==SF.Superuser then + if IsValid(ply) or ply==SF.Superuser then return self.max - self:get(ply).val else SF.Throw("Invalid starfall user", 3) @@ -300,7 +301,7 @@ SF.EntManager = { self:free(instance.player, -1) end, remove = function(self, instance, ent) - if ent:IsValid() then + if IsValid(ent) then if self.nocallonremove then self:onremove(instance, ent) else @@ -563,7 +564,7 @@ SF.Parent = { }, setParent = function(self, parent, type, param) - if self.parent and self.parent:IsValid() then + if IsValid(self.parent) then self.parent.sfParent.children[self.ent] = nil self:removeParent() end @@ -585,11 +586,11 @@ SF.Parent = { fix = function(self) local cleanup = true - if self.parent and self.parent:IsValid() then + if IsValid(self.parent) then cleanup = false end for child, data in pairs(self.children) do - if child:IsValid() then + if IsValid(child) then data:applyTransform() data:applyParent() cleanup = false @@ -1090,7 +1091,7 @@ end function SF.EntIsReady(ent) if ent:IsWorld() then return true end - if ent:IsValid() then + if IsValid(ent) then -- https://github.com/Facepunch/garrysmod-issues/issues/3127 local class = ent:GetClass() if class=="player" then @@ -1144,7 +1145,7 @@ local playerinithooks = {} hook.Add("PlayerInitialSpawn","SF_PlayerInitialize",function(ply) local n = "SF_WaitForPlayerInit"..ply:EntIndex() hook.Add("SetupMove", n, function(ply2, mv, cmd) - if ply:IsValid() then + if IsValid(ply) then if ply == ply2 and not cmd:IsForced() then for _, v in ipairs(playerinithooks) do v(ply) end hook.Remove("SetupMove", n) @@ -1310,7 +1311,7 @@ function SF.CheckModel(model, player, prop) if #model > 260 then return false end model = SF.NormalizePath(string.lower(model)) if string.GetExtensionFromFilename(model) == "mdl" and (CLIENT or (util.IsValidModel(model) and (not prop or util.IsValidProp(model)))) then - if player and player:IsValid() then + if IsValid(player) then if hook.Run("PlayerSpawnObject", player, model)~=false then return model end @@ -1339,7 +1340,7 @@ local drawEntityClasses = { ["prop_vehicle_prisoner_pod"] = true, } function SF.CanDrawEntity(ent) - return drawEntityClasses[ent:GetClass()] and not ent:GetParent():IsValid() and ent.RenderOverride==nil + return drawEntityClasses[ent:GetClass()] and not IsValid(ent:GetParent()) and ent.RenderOverride==nil end --- Chooses whether to use absolute or relative path @@ -1393,7 +1394,7 @@ end function SF.ParentChainTooLong(parent, child) local index = parent local parentLength = 0 - while index:IsValid() do + while IsValid(index) do if index == child then return true end parentLength = parentLength + 1 index = index:GetParent() @@ -1490,7 +1491,7 @@ if SERVER then util.AddNetworkString("starfall_print") function SF.AddNotify(ply, msg, notifyType, duration, sound) - if not (ply and ply:IsValid()) then return end + if not IsValid(ply) then return end net.Start("starfall_addnotify") net.WriteString(string.sub(msg, 1, 1024)) @@ -1541,7 +1542,7 @@ else function SF.HTTPNotify(ply, url) local plyStr - if ply:IsValid() then + if IsValid(ply) then plyStr = ply:Nick() .. " [" .. ply:SteamID() .. "]" elseif ply == SF.Superuser then plyStr = "Superuser" @@ -1957,7 +1958,7 @@ do -- Command to reload the libraries concommand.Add("sf_reloadlibrary", function(ply, com, arg) - if ply:IsValid() and not ply:IsSuperAdmin() then return end + if IsValid(ply) and not ply:IsSuperAdmin() then return end local name = arg[1] if not name then return end name = string.lower(name) diff --git a/lua/starfall/transfer.lua b/lua/starfall/transfer.lua index b8dfeb9fa..a2bf30d19 100644 --- a/lua/starfall/transfer.lua +++ b/lua/starfall/transfer.lua @@ -1,4 +1,6 @@ +local IsValid = FindMetaTable("Entity").IsValid + -- Net extension stuff function net.ReadStarfall(ply, callback) local sfdata = {files = {}} @@ -127,7 +129,7 @@ if SERVER then net.Receive("starfall_error", function(_, ply) local chip = net.ReadEntity() - if not (chip and chip:IsValid()) then return end + if not IsValid(chip) then return end if chip.ErroredPlayers[ply] then return end chip.ErroredPlayers[ply] = true @@ -165,7 +167,7 @@ if SERVER then net.ReadStarfall(ply, function(ok, sfdata) if not ok then return end - if not (sf:IsValid() and sf:GetClass() == "starfall_processor" and sf.sfdata) then return end + if not (IsValid(sf) and sf:GetClass() == "starfall_processor" and sf.sfdata) then return end if sf.sfdata.mainfile ~= sfdata.mainfile or sf.sfdata.owner ~= ply then return end sfdata.owner = ply sf:SetupFiles(sfdata) @@ -194,7 +196,7 @@ else function SF.SendError(chip, message, traceback) local owner, is_blocked = chip.owner, false - if owner and owner:IsValid() then + if IsValid(owner) then is_blocked = SF.BlockedUsers:isBlocked(owner:SteamID()) end net.Start("starfall_error") @@ -207,16 +209,16 @@ else net.Receive("starfall_error", function() local chip = net.ReadEntity() - if not chip:IsValid() then return end + if not IsValid(chip) then return end local owner = net.ReadEntity() - if not owner:IsValid() then return end + if not IsValid(owner) then return end local mainfile = net.ReadString() local message = net.ReadString() local traceback = net.ReadString() local client, should_notify if net.ReadBool() then client = net.ReadEntity() - if not client:IsValid() then return end + if not IsValid(client) then return end should_notify = net.ReadBool() end hook.Run("StarfallError", chip, owner, client, mainfile, message, traceback, should_notify)