From 10cb567f06631631a76983ac84c8853384caeb26 Mon Sep 17 00:00:00 2001 From: natrim Date: Wed, 10 Oct 2012 14:22:24 +0300 Subject: [PATCH 1/5] add parent() and setParent(parent) to entity --- lua/starfall/libs_sh/ents.lua | 10 ++++++++++ lua/starfall/libs_sv/ents.lua | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/lua/starfall/libs_sh/ents.lua b/lua/starfall/libs_sh/ents.lua index 3122bed..abcabfc 100644 --- a/lua/starfall/libs_sh/ents.lua +++ b/lua/starfall/libs_sh/ents.lua @@ -308,6 +308,16 @@ function ents_methods:eyePos() return ent:EyePos() end +--- Gets the entity's parent +-- @shared +-- @return The parent entity +function ents_methods:parent() + SF.CheckType(self,ents_metamethods) + local ent = unwrap(self) + if not isValid(ent) then return nil, "invalid entity" end + return ent:GetParent() +end + -- ------------------------- Player Methods ------------------------- -- player_methods.__index = SF.Entities.Methods diff --git a/lua/starfall/libs_sv/ents.lua b/lua/starfall/libs_sv/ents.lua index b79e5b2..cd5cfa7 100644 --- a/lua/starfall/libs_sv/ents.lua +++ b/lua/starfall/libs_sv/ents.lua @@ -297,3 +297,37 @@ function ents_methods:enableGravity(grav) phys:Wake() return true end + +--- Sets the entity's parent +-- @param parent The entity we should parent to or nil for unparent +function ents_methods:setParent(parent) + SF.CheckType(self,ents_metatable) + local child = unwrap(self) + if not isValid(child) then return false, "entity not valid" end + if not canModify(SF.instance.player, child) or SF.instance.permissions:checkPermission("Modify All Entities") then return false, "access denied" end + + if parent then + SF.CheckType(parent,ents_metatable) + local parent = unwrap(parent) + if not isValid(parent) then return false, "parent entity not valid" end + + -- do not parent to self + if child == parent then return false, "cannot parent to self" end + + -- can we modify parent? + if not canModify(SF.instance.player, parent) or SF.instance.permissions:checkPermission("Modify All Entities") then return false, "parent access denied" end + + -- Prevent cyclic parenting ( = crashes ) + local checkparent = parent + while IsValid(checkparent:GetParent()) do + checkparent = checkparent:GetParent() + if checkparent == child then return false, "cyclic parenting detected" end + end + + child:SetParent(parent) + else + child:SetParent(nil) + end + + return true +end From c8bc85849f14c25e6c0e1b5f42e2a97155f2c306 Mon Sep 17 00:00:00 2001 From: natrim Date: Wed, 10 Oct 2012 19:35:00 +0300 Subject: [PATCH 2/5] cannot parent (to) player --- lua/starfall/libs_sv/ents.lua | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lua/starfall/libs_sv/ents.lua b/lua/starfall/libs_sv/ents.lua index cd5cfa7..0a7a5bf 100644 --- a/lua/starfall/libs_sv/ents.lua +++ b/lua/starfall/libs_sv/ents.lua @@ -304,6 +304,7 @@ function ents_methods:setParent(parent) SF.CheckType(self,ents_metatable) local child = unwrap(self) if not isValid(child) then return false, "entity not valid" end + if child:IsPlayer() then return false, "cannot parent player" end if not canModify(SF.instance.player, child) or SF.instance.permissions:checkPermission("Modify All Entities") then return false, "access denied" end if parent then @@ -313,6 +314,9 @@ function ents_methods:setParent(parent) -- do not parent to self if child == parent then return false, "cannot parent to self" end + + -- do not parent to players + if parent:IsPlayer() then return false, "cannot parent to players" end -- can we modify parent? if not canModify(SF.instance.player, parent) or SF.instance.permissions:checkPermission("Modify All Entities") then return false, "parent access denied" end From a122dedfc381ec1df93ba8d82113f141a2eaa6c5 Mon Sep 17 00:00:00 2001 From: natrim Date: Wed, 10 Oct 2012 19:41:07 +0300 Subject: [PATCH 3/5] add check for parenting success --- lua/starfall/libs_sv/ents.lua | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lua/starfall/libs_sv/ents.lua b/lua/starfall/libs_sv/ents.lua index 0a7a5bf..13614bb 100644 --- a/lua/starfall/libs_sv/ents.lua +++ b/lua/starfall/libs_sv/ents.lua @@ -299,7 +299,7 @@ function ents_methods:enableGravity(grav) end --- Sets the entity's parent --- @param parent The entity we should parent to or nil for unparent +-- @param parent The entity we should parent to or nil to deparent function ents_methods:setParent(parent) SF.CheckType(self,ents_metatable) local child = unwrap(self) @@ -329,8 +329,14 @@ function ents_methods:setParent(parent) end child:SetParent(parent) + + checkparent = child:GetParent() + if not checkparent:IsValid() and checkparent == parent then return false, "parenting failed" end else child:SetParent(nil) + + local checkparent = child:GetParent() + if checkparent:IsValid() then return false, "deparenting failed" end end return true From 5c9e51619b9a7db5d91dca7b47b3a1587553cfd5 Mon Sep 17 00:00:00 2001 From: Natrim Date: Thu, 11 Oct 2012 10:09:45 +0200 Subject: [PATCH 4/5] wrap the parent before return --- lua/starfall/libs_sh/ents.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/starfall/libs_sh/ents.lua b/lua/starfall/libs_sh/ents.lua index abcabfc..6a8e481 100644 --- a/lua/starfall/libs_sh/ents.lua +++ b/lua/starfall/libs_sh/ents.lua @@ -315,7 +315,7 @@ function ents_methods:parent() SF.CheckType(self,ents_metamethods) local ent = unwrap(self) if not isValid(ent) then return nil, "invalid entity" end - return ent:GetParent() + return wrap(ent:GetParent()) end -- ------------------------- Player Methods ------------------------- -- From f650107ca088ebbcc77ee494ac56a3ffda4ebad6 Mon Sep 17 00:00:00 2001 From: Natrim Date: Thu, 11 Oct 2012 10:11:53 +0200 Subject: [PATCH 5/5] parent() returns nil if entity not parented --- lua/starfall/libs_sh/ents.lua | 8 ++++++-- lua/starfall/libs_sv/ents.lua | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lua/starfall/libs_sh/ents.lua b/lua/starfall/libs_sh/ents.lua index 6a8e481..760be8a 100644 --- a/lua/starfall/libs_sh/ents.lua +++ b/lua/starfall/libs_sh/ents.lua @@ -310,12 +310,16 @@ end --- Gets the entity's parent -- @shared --- @return The parent entity +-- @return The parent entity or nil function ents_methods:parent() SF.CheckType(self,ents_metamethods) local ent = unwrap(self) if not isValid(ent) then return nil, "invalid entity" end - return wrap(ent:GetParent()) + local parent = ent:GetParent() + + if not parent and parent:IsValid() then return nil, "not parented" end + + return wrap(parent) end -- ------------------------- Player Methods ------------------------- -- diff --git a/lua/starfall/libs_sv/ents.lua b/lua/starfall/libs_sv/ents.lua index 13614bb..2e24b9e 100644 --- a/lua/starfall/libs_sv/ents.lua +++ b/lua/starfall/libs_sv/ents.lua @@ -331,12 +331,12 @@ function ents_methods:setParent(parent) child:SetParent(parent) checkparent = child:GetParent() - if not checkparent:IsValid() and checkparent == parent then return false, "parenting failed" end + if not checkparent and checkparent:IsValid() and checkparent == parent then return false, "parenting failed" end else child:SetParent(nil) local checkparent = child:GetParent() - if checkparent:IsValid() then return false, "deparenting failed" end + if checkparent and checkparent:IsValid() then return false, "deparenting failed" end end return true