Skip to content

Commit

Permalink
IsValid updates (#1662)
Browse files Browse the repository at this point in the history
* IsValid updates

* Fixes

* Fixes

* Add a few that were missed
  • Loading branch information
thegrb93 authored Mar 11, 2024
1 parent 96aeeba commit 361dd71
Show file tree
Hide file tree
Showing 27 changed files with 285 additions and 291 deletions.
7 changes: 4 additions & 3 deletions lua/entities/starfall_hologram/cl_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 9 additions & 7 deletions lua/entities/starfall_hud/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -83,15 +85,15 @@ 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

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
Expand Down
14 changes: 8 additions & 6 deletions lua/entities/starfall_processor/cl_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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 )
Expand Down
14 changes: 8 additions & 6 deletions lua/entities/starfall_processor/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -223,22 +225,22 @@ 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)

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)
Expand All @@ -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))
Expand Down
21 changes: 11 additions & 10 deletions lua/entities/starfall_processor/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ ENT.States = {
None = 3,
}

local IsValid = FindMetaTable("Entity").IsValid

function ENT:Compile()
if self.instance then
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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.")
Expand Down Expand Up @@ -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))
Expand All @@ -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
Expand Down
11 changes: 7 additions & 4 deletions lua/entities/starfall_prop/cl_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lua/entities/starfall_screen/cl_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions lua/entities/starfall_screen/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading

0 comments on commit 361dd71

Please sign in to comment.