Skip to content

Commit 39af03a

Browse files
Remove all instances of E2 entity functions returning nil (#3461)
* First entities NULL fixes (stopped at HoloEntity) * Don't set entity default as nil (fixes gtable and array getters) * Fix ioInputEntity return * It looks like it was never supposed to return any entity * Fix a bunch of functions isConstrainedTo(e:) isConstrainedTo(e:n) isConstrainedTo(e:s) isConstrainedTo(e:sn) isWeldedTo(e:) isWeldedTo(e:n) * More saveguard * Fix keyClk * Fix/optimize lastSpoke * Fix npcGetTarget * More fixes * Fix signalSender * Simplify Co-authored-by: Denneisk <20892685+Denneisk@users.noreply.github.com> * Simplify more --------- Co-authored-by: Denneisk <20892685+Denneisk@users.noreply.github.com>
1 parent 7c7a69a commit 39af03a

File tree

12 files changed

+87
-95
lines changed

12 files changed

+87
-95
lines changed

lua/entities/gmod_wire_expression2/core/chat.lua

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
local IsValid = IsValid
55

66
local TextList = {
7-
last = { "", 0, nil }
7+
last = { "", 0, NULL }
88
}
99
local ChatAlert = {}
1010

@@ -20,7 +20,7 @@ end)
2020

2121
hook.Add("PlayerSay","Exp2TextReceiving", function(ply, text, teamchat)
2222
chipHideChat, chipChatReplacement = nil, nil
23-
23+
2424
local entry = { text, CurTime(), ply, teamchat }
2525
TextList[ply:EntIndex()] = entry
2626
TextList.last = entry
@@ -95,14 +95,7 @@ end
9595
--- Returns the last player to speak.
9696
[nodiscard, deprecated = "Use the chat event instead"]
9797
e2function entity lastSpoke()
98-
local entry = TextList.last
99-
if not entry then return nil end
100-
101-
local ply = entry[3]
102-
if not IsValid(ply) then return nil end
103-
if not ply:IsPlayer() then return nil end
104-
105-
return ply
98+
return TextList.last[3]
10699
end
107100

108101
--- Returns the last message in the chat log.

lua/entities/gmod_wire_expression2/core/constraint.lua

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,6 @@ local function caps(text)
1010
return capstext
1111
end
1212

13-
-- Returns con.Ent1 or con.Ent2, whichever is not equivalent to ent. Optionally subscripts con with num beforehand.
14-
local function ent1or2(ent,con,num)
15-
if not con then return nil end
16-
if num then
17-
con = con[num]
18-
if not con then return nil end
19-
end
20-
if con.Ent1==ent then return con.Ent2 end
21-
return con.Ent1
22-
end
23-
2413
--[[
2514
-- buildFilters
2615
@@ -235,59 +224,72 @@ e2function number entity:isConstrained()
235224
return 1
236225
end
237226

227+
-- Returns con.Ent1 or con.Ent2, whichever is not equivalent to ent. Optionally subscripts con with num beforehand.
228+
local function ent1or2(ent, con, num)
229+
if not con then return NULL end
230+
231+
if num then
232+
con = con[num]
233+
if not con then return NULL end
234+
end
235+
236+
local ent1 = con.Ent1
237+
if ent1 == ent then return con.Ent2 or NULL end
238+
239+
return ent1 or NULL
240+
end
241+
238242
--- Returns the first entity <this> was welded to.
239243
e2function entity entity:isWeldedTo()
240-
if not IsValid(this) then return self:throw("Invalid entity!", nil) end
241-
if not constraint.HasConstraints(this) then return nil end
244+
if not IsValid(this) then return self:throw("Invalid entity!", NULL) end
245+
if not constraint.HasConstraints(this) then return NULL end
242246

243-
local filter = {Weld=true} -- create filter directly, no need to call buildFilter here, since it's static
244-
return ent1or2(this,constraint_FindConstraint(this, filter))
247+
return ent1or2(this, constraint_FindConstraint(this, {Weld = true}))
245248
end
246249

247250
--- Returns the <index>th entity <this> was welded to.
248251
e2function entity entity:isWeldedTo(index)
249-
if not IsValid(this) then return self:throw("Invalid entity!", nil) end
250-
if not constraint.HasConstraints(this) then return nil end
252+
if not IsValid(this) then return self:throw("Invalid entity!", NULL) end
253+
if not constraint.HasConstraints(this) then return NULL end
251254

252-
local filter = {Weld=true} -- create filter directly, no need to call buildFilter here, since it's static
253-
return ent1or2(this,constraint_FindConstraints(this, filter), math.floor(index))
255+
return ent1or2(this, constraint_FindConstraints(this, {Weld = true}), math.floor(index))
254256
end
255257

256258
--- Returns the first entity <this> was constrained to.
257259
e2function entity entity:isConstrainedTo()
258-
if not IsValid(this) then return self:throw("Invalid entity!", nil) end
259-
if not constraint.HasConstraints(this) then return nil end
260+
if not IsValid(this) then return self:throw("Invalid entity!", NULL) end
261+
if not constraint.HasConstraints(this) then return NULL end
260262

261-
return ent1or2(this,constraint_GetTable(this),1)
263+
return ent1or2(this, constraint_GetTable(this), 1)
262264
end
263265

264266
--- Returns the <index>th entity <this> was constrained to.
265267
e2function entity entity:isConstrainedTo(index)
266-
if not IsValid(this) then return self:throw("Invalid entity!", nil) end
267-
if not constraint.HasConstraints(this) then return nil end
268+
if not IsValid(this) then return self:throw("Invalid entity!", NULL) end
269+
if not constraint.HasConstraints(this) then return NULL end
268270

269-
return ent1or2(this,constraint_GetTable(this), math.floor(index))
271+
return ent1or2(this, constraint_GetTable(this), math.floor(index))
270272
end
271273

272274
--- Returns the first entity <this> was constrained to with the given constraint type <constraintType>.
273275
e2function entity entity:isConstrainedTo(string constraintType)
274-
if not IsValid(this) then return self:throw("Invalid entity!", nil) end
275-
if not constraint.HasConstraints(this) then return nil end
276+
if not IsValid(this) then return self:throw("Invalid entity!", NULL) end
277+
if not constraint.HasConstraints(this) then return NULL end
276278

277-
return ent1or2(this,constraint_FindConstraint(this, buildFilter({constraintType})))
279+
return ent1or2(this, constraint_FindConstraint(this, buildFilter({constraintType})))
278280
end
279281

280282
--- Returns the <index>th entity <this> was constrained to with the given constraint type <constraintType>.
281283
e2function entity entity:isConstrainedTo(string constraintType, index)
282-
if not IsValid(this) then return self:throw("Invalid entity!", nil) end
283-
if not constraint.HasConstraints(this) then return nil end
284+
if not IsValid(this) then return self:throw("Invalid entity!", NULL) end
285+
if not constraint.HasConstraints(this) then return NULL end
284286

285-
return ent1or2(this,constraint_FindConstraints(this, buildFilter({constraintType})), math.floor(index))
287+
return ent1or2(this, constraint_FindConstraints(this, buildFilter({constraintType})), math.floor(index))
286288
end
287289

288290
--- Returns the '''entity''' <this> is parented to.
289291
e2function entity entity:parent()
290-
if not IsValid(this) then return nil end
292+
if not IsValid(this) then return self:throw("Invalid entity!", NULL) end
291293
return this:GetParent()
292294
end
293295

lua/entities/gmod_wire_expression2/core/egpfunctions.lua

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ e2function egpobject wirelink:egpBox( number index, vector2 pos, vector2 size )
128128
if (!EGP:IsAllowed( self, this )) then return NULL_EGPOBJECT end
129129
local bool, obj = egp_create("Box", { index = index, w = size[1], h = size[2], x = pos[1], y = pos[2] }, this)
130130
if (bool) then EGP:DoAction( this, self, "SendObject", obj ) Update(self,this) end
131-
return obj
131+
return obj
132132
end
133133

134134
--------------------------------------------------------
@@ -258,7 +258,7 @@ local function canCreateFont( ply, font, size )
258258
size = size or 18
259259

260260
EGP.PlayerFontCount[ply:SteamID64()] = EGP.PlayerFontCount[ply:SteamID64()] or { fonts = {}, count = 0 }
261-
local fontTable = EGP.PlayerFontCount[ply:SteamID64()]
261+
local fontTable = EGP.PlayerFontCount[ply:SteamID64()]
262262

263263
if fontTable.count >= 50 then return false end
264264

@@ -779,10 +779,14 @@ e2function void wirelink:egpParent( number index, entity parent )
779779
end
780780

781781
-- Returns the entity a tracker is parented to
782-
e2function entity wirelink:egpTrackerParent( number index )
782+
e2function entity wirelink:egpTrackerParent(number index)
783783
local bool, k, v = hasObject(this, index)
784+
784785
if bool and v.NeedsConstantUpdate then
785-
return (v.parententity and v.parententity:IsValid()) and v.parententity or nil
786+
local parent = v.parententity
787+
return IsValid(parent) and parent or NULL
788+
else
789+
return NULL
786790
end
787791
end
788792

@@ -1309,9 +1313,7 @@ end
13091313

13101314
-- Returns the screen which the queue finished sending items for
13111315
e2function entity egpQueueScreen()
1312-
if (EGP.RunByEGPQueue) then
1313-
return EGP.RunByEGPQueue_Ent
1314-
end
1316+
return EGP.RunByEGPQueue and EGP.RunByEGPQueue_Ent or NULL
13151317
end
13161318

13171319
-- Same as above, except returns wirelink
@@ -1323,9 +1325,7 @@ end
13231325

13241326
-- Returns the player which ordered the current items to be sent (This is usually yourself, but if you're sharing pp with someone it might be them. Good way to check if someone is fucking with your screens)
13251327
e2function entity egpQueuePlayer()
1326-
if (EGP.RunByEGPQueue) then
1327-
return EGP.RunByEGPQueue_ply
1328-
end
1328+
return EGP.RunByEGPQueue and EGP.RunByEGPQueue_ply or NULL
13291329
end
13301330

13311331
-- Returns 1 if the current execution was caused by the EGP queue system and the player <ply> was the player whom ordered the item to be sent (This is usually yourself, but if you're sharing pp with someone it might be them.)

lua/entities/gmod_wire_expression2/core/entity.lua

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
local wire_expression2_entity_trails_max = CreateConVar("wire_expression2_entity_trails_max", 30, FCVAR_ARCHIVE, "Max amount of trails a player can make. 0 - to disable trails. (Limit is shared between E2s of a player)", 0)
22

3-
registerType("entity", "e", nil,
3+
registerType("entity", "e", NULL,
44
nil,
55
function(self,output) return output or NULL end,
66
nil,
@@ -69,8 +69,7 @@ end
6969
--[[******************************************************************************]]
7070

7171
e2function entity entity(id)
72-
local ent = ents.GetByIndex(id)
73-
return IsValid(ent) and ent or nil
72+
return ents.GetByIndex(id)
7473
end
7574

7675
e2function number entity:id()
@@ -122,8 +121,8 @@ e2function string entity:model()
122121
end
123122

124123
e2function entity entity:owner()
125-
if not IsValid(this) then return self:throw("Invalid entity!", nil) end
126-
return getOwner(self, this)
124+
if not IsValid(this) then return self:throw("Invalid entity!", NULL) end
125+
return getOwner(self, this) or NULL
127126
end
128127

129128
__e2setcost(100)
@@ -889,12 +888,12 @@ end)
889888
__e2setcost(5)
890889

891890
e2function entity entity:driver()
892-
if not IsValid(this) or not this:IsVehicle() then return self:throw("Invalid vehicle!", nil) end
891+
if not IsValid(this) or not this:IsVehicle() then return self:throw("Invalid vehicle!", NULL) end
893892
return this:GetDriver()
894893
end
895894

896895
e2function entity entity:passenger()
897-
if not IsValid(this) or not this:IsVehicle() then return self:throw("Invalid vehicle!", nil) end
896+
if not IsValid(this) or not this:IsVehicle() then return self:throw("Invalid vehicle!", NULL) end
898897
return this:GetPassenger(0)
899898
end
900899

lua/entities/gmod_wire_expression2/core/find.lua

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -452,8 +452,8 @@ end
452452
--- Returns the player with the given name, this is an exception to the rule
453453
[nodiscard]
454454
e2function entity findPlayerByName(string name)
455-
if query_blocked(self, 1) then return nil end
456-
return findPlayer(name)
455+
if query_blocked(self, 1) then return NULL end
456+
return findPlayer(name) or NULL
457457
end
458458

459459
--- Returns the player with the given SteamID
@@ -519,7 +519,7 @@ end
519519

520520
--- Exclude entities with this model (or partial model name) from future finds
521521
e2function void findExcludeModel(string model)
522-
if not pcall(WireLib.CheckRegex, sample_string, model) then return self:throw("Search string too complex!", nil) end
522+
if not pcall(WireLib.CheckRegex, sample_string, model) then return self:throw("Search string too complex!", nil) end
523523
self.data.find.bl_model[string.lower(model)] = true
524524
invalidate_filters(self)
525525
end
@@ -800,26 +800,26 @@ end
800800
--- Returns the indexed entity from the previous find event (valid parameters are 1 to the number of entities found)
801801
[nodiscard]
802802
e2function entity findResult(index)
803-
return self.data.findlist[index]
803+
return self.data.findlist[index] or NULL
804804
end
805805

806806
--- Returns the closest entity to the given point from the previous find event
807807
[nodiscard]
808808
e2function entity findClosest(vector position)
809-
local closest = nil
810-
local dist = math.huge
809+
local closest, dist = NULL, math.huge
811810
self.prf = self.prf + #self.data.findlist * 10
812-
for _,ent in pairs(self.data.findlist) do
811+
812+
for _, ent in pairs(self.data.findlist) do
813813
if IsValid(ent) then
814-
local pos = ent:GetPos()
815-
local xd, yd, zd = pos.x-position[1], pos.y-position[2], pos.z-position[3]
816-
local curdist = xd*xd + yd*yd + zd*zd
814+
local curdist = ent:GetPos():Distance(position)
815+
817816
if curdist < dist then
818817
closest = ent
819818
dist = curdist
820819
end
821820
end
822821
end
822+
823823
return closest
824824
end
825825

@@ -837,7 +837,7 @@ end
837837
--- Equivalent to findResult(1)
838838
[nodiscard]
839839
e2function entity find()
840-
return self.data.findlist[1]
840+
return self.data.findlist[1] or NULL
841841
end
842842

843843
--[[************************************************************************]]--

lua/entities/gmod_wire_expression2/core/hologram.lua

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ local function CreateHolo(self, index, pos, scale, ang, color, model)
594594
end, self, index )
595595
end
596596

597-
if not IsValid(prop) then return nil end
597+
if not IsValid(prop) then return NULL end
598598

599599
if color then WireLib.SetColor(Holo.ent, Color(color[1],color[2],color[3],color[4] or 255)) end
600600

@@ -664,7 +664,7 @@ end
664664

665665
local function checkHoloCount(self)
666666
if PlayerAmount[self.uid] >= wire_holograms_max:GetInt() then
667-
return self:throw("You've hit the maximum amount of holograms!", true)
667+
return self:throw("You've hit the maximum amount of holograms!", NULL)
668668
end
669669
end
670670

@@ -718,8 +718,8 @@ e2function entity holoCreate(index, vector position, vector scale, angle ang, ve
718718

719719
position = Vector(position[1], position[2], position[3])
720720
ang = Angle(ang[1], ang[2], ang[3])
721-
local ret = CreateHolo(self, index, position, scale, ang, color)
722-
if IsValid(ret) then return ret end
721+
722+
return CreateHolo(self, index, position, scale, ang, color)
723723
end
724724

725725
e2function entity holoCreate(index, vector position, vector scale, angle ang)
@@ -728,10 +728,7 @@ e2function entity holoCreate(index, vector position, vector scale, angle ang)
728728
local Holo = CheckIndex(self, index, true)
729729
if not Holo and checkHoloCount(self) then return end
730730

731-
position = Vector(position[1], position[2], position[3])
732-
ang = Angle(ang[1], ang[2], ang[3])
733-
local ret = CreateHolo(self, index, position, scale, ang)
734-
if IsValid(ret) then return ret end
731+
return CreateHolo(self, index, position, scale, ang)
735732
end
736733

737734
e2function entity holoCreate(index, vector position, vector scale)
@@ -741,8 +738,8 @@ e2function entity holoCreate(index, vector position, vector scale)
741738
if not Holo and checkHoloCount(self) then return end
742739

743740
position = Vector(position[1],position[2],position[3])
744-
local ret = CreateHolo(self, index, position, scale)
745-
if IsValid(ret) then return ret end
741+
742+
return CreateHolo(self, index, position, scale)
746743
end
747744

748745
e2function entity holoCreate(index, vector position)
@@ -752,8 +749,8 @@ e2function entity holoCreate(index, vector position)
752749
if not Holo and checkHoloCount(self) then return end
753750

754751
position = Vector(position[1],position[2],position[3])
755-
local ret = CreateHolo(self, index, position)
756-
if IsValid(ret) then return ret end
752+
753+
return CreateHolo(self, index, position)
757754
end
758755

759756
e2function entity holoCreate(index)
@@ -762,8 +759,7 @@ e2function entity holoCreate(index)
762759
local Holo = CheckIndex(self, index, true)
763760
if not Holo and PlayerAmount[self.uid] >= wire_holograms_max:GetInt() then return end
764761

765-
local ret = CreateHolo(self, index)
766-
if IsValid(ret) then return ret end
762+
return CreateHolo(self, index)
767763
end
768764

769765
__e2setcost(20)
@@ -1328,7 +1324,8 @@ end
13281324
__e2setcost(2)
13291325
e2function entity holoEntity(index)
13301326
local Holo = CheckIndex(self, index)
1331-
if Holo and IsValid(Holo.ent) then return Holo.ent end
1327+
1328+
return Holo and Holo.ent or NULL
13321329
end
13331330

13341331
__e2setcost(30)

0 commit comments

Comments
 (0)