Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed SF table clutter in favor of using type metatables. #371

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lua/entities/starfall_screen/cl_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ net.Receive( "starfall_screen_used", function ( len )

if not IsValid( screen ) then return end

screen:runScriptHook( "starfallUsed", SF.Entities.Wrap( activator ) )
screen:runScriptHook( "starfallUsed", SF.GetTypeDef( "Entity" ).__wrap( activator ) )

-- Error message copying
if screen.error then
Expand Down
2 changes: 1 addition & 1 deletion lua/entities/starfall_screen/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ function ENT:Use ( activator )
net.Broadcast()
end
if self.sharedscreen then
self:runScriptHook( "starfallUsed", SF.Entities.Wrap( activator ) )
self:runScriptHook( "starfallUsed", SF.GetTypeDef( "Entity" ).__wrap( activator ) )
end
end

Expand Down
8 changes: 4 additions & 4 deletions lua/starfall/libs_cl/render.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ local clamp = math.Clamp
local max = math.max
local cam = cam
local dgetmeta = debug.getmetatable
local matrix_meta = SF.VMatrix.Metatable --debug.getregistry().VMatrix
local matrix_meta = SF.GetTypeDef( "VMatrix" ) --debug.getregistry().VMatrix

local v_unwrap = SF.VMatrix.Unwrap
local v_unwrap = matrix_meta.__unwrap

local currentcolor
local MATRIX_STACK_LIMIT = 8
Expand Down Expand Up @@ -533,7 +533,7 @@ function render_library.cursorPos( ply )
local screen = SF.instance.data.entity
if not screen then return nil end

ply = SF.Entities.Unwrap( ply )
ply = SF.GetTypeDef( "Entity" ).__unwrap( ply )

-- Get monitor screen pos & size
monitor = WireGPU_Monitors[ screen:GetModel() ]
Expand Down Expand Up @@ -697,7 +697,7 @@ function render_library.readPixel ( x, y )
SF.CheckType( y, "number" )

local r, g, b = render.ReadPixel( x, y )
return SF.Color.Wrap( Color( r, g, b, 255 ) )
return SF.GetTypeDef( "Color" ).__wrap( Color( r, g, b, 255 ) )
end

--- Called when a player uses the screen
Expand Down
7 changes: 0 additions & 7 deletions lua/starfall/libs_sh/angles.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
SF.Angles = {}

--- Angle Type
-- @shared
local ang_methods, ang_metamethods = SF.Typedef( "Angle" )
Expand All @@ -9,11 +7,6 @@ SF.DefaultEnvironment.Angle = function ( ... )
return wrap( Angle( ... ) )
end

SF.Angles.Wrap = wrap
SF.Angles.Unwrap = unwrap
SF.Angles.Methods = ang_methods
SF.Angles.Metatable = ang_metamethods

--- __newindex metamethod
function ang_metamethods.__newindex ( t, k, v )
if type( k ) == "number" and k >= 1 and k <= 3 then
Expand Down
7 changes: 0 additions & 7 deletions lua/starfall/libs_sh/color.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
SF.Color = {}

--- Color type
--@shared
local color_methods, color_metatable = SF.Typedef( "Color" )

local wrap, unwrap = SF.CreateWrapper( color_metatable, true, false, debug.getregistry().Color )

SF.Color.Methods = color_methods
SF.Color.Metatable = color_metatable
SF.Color.Wrap = wrap
SF.Color.Unwrap = unwrap

--- Same as the Gmod Color type
-- @name SF.DefaultEnvironment.Color
-- @class function
Expand Down
18 changes: 5 additions & 13 deletions lua/starfall/libs_sh/entities.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
-- Shared entity library functions
-------------------------------------------------------------------------------

SF.Entities = {}

--- Entity type
-- @shared
local ents_methods, ents_metamethods = SF.Typedef( "Entity" )
Expand All @@ -27,25 +25,19 @@ local materialBlacklist = {

-- ------------------------- Internal functions ------------------------- --

SF.Entities.Wrap = wrap
SF.Entities.Unwrap = unwrap
SF.Entities.Methods = ents_methods
SF.Entities.Metatable = ents_metamethods
SF.Entities.Library = ents_lib

--- Returns true if valid and is not the world, false if not
-- @param entity Entity to check
function SF.Entities.IsValid ( entity )
function ents_metamethods.IsValid ( entity )
return IsValid( entity ) and not entity:IsWorld()
end
local isValid = SF.Entities.IsValid
local isValid = ents_metamethods.IsValid

--- Gets the physics object of the entity
-- @return The physobj, or nil if the entity isn't valid or isn't vphysics
function SF.Entities.GetPhysObject ( ent )
function ents_metamethods.GetPhysObject ( ent )
return ( isValid( ent ) and ent:GetMoveType() == MOVETYPE_VPHYSICS and ent:GetPhysicsObject() ) or nil
end
local getPhysObject = SF.Entities.GetPhysObject
local getPhysObject = ents_metamethods.GetPhysObject

-- ------------------------- Library functions ------------------------- --

Expand All @@ -55,7 +47,7 @@ local getPhysObject = SF.Entities.GetPhysObject
function ents_lib.self ()
local ent = SF.instance.data.entity
if ent then
return SF.Entities.Wrap( ent )
return wrap( ent )
else return nil end
end

Expand Down
14 changes: 6 additions & 8 deletions lua/starfall/libs_sh/globaltables.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,29 @@
-- @shared
local gtables_library, gtables_metamethods = SF.Libraries.Register( "globaltables" )

SF.GlobalTables = {}

SF.GlobalTables.Global = {}
SF.GlobalTables.Players = {}
gtables_metamethods.Global = {}
gtables_metamethods.Players = {}

--- Global table shared by all instances on the same side.
-- @name gtables_library.global
-- @class table
gtables_library.global = SF.GlobalTables.Global
gtables_library.global = gtables_metamethods.Global

--- Player-unique global table.
-- @name gtables_library.player
-- @class table

hook.Add("PlayerInitialSpawn", "SF_GlobalTables_cn", function (ply)
SF.GlobalTables.Players [ ply ] = {}
gtables_metamethods.Players [ ply ] = {}
end)

hook.Add("PlayerDisconnected", "SF_GlobalTables_dc", function (ply)
SF.GlobalTables.Players [ ply ] = nil
gtables_metamethods.Players [ ply ] = nil
end)

function gtables_metamethods:__index ( k )
if k == "player" then
return SF.GlobalTables.Players[ SF.instance.player ]
return gtables_metamethods.Players[ SF.instance.player ]
else
return gtables_metamethods.__methods[ k ]
end
Expand Down
4 changes: 2 additions & 2 deletions lua/starfall/libs_sh/hook.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ end
-- @param ... Payload. These parameters will be used to call the hook functions
-- @return tbl A list of the resultset of each called hook
function hook_library.runRemote ( recipient, ... )
if recipient then SF.CheckType( recipient, SF.Entities.Metatable ) end
if recipient then SF.CheckType( recipient, SF.GetTypeDef( "Entity" ) ) end

local recipients
if recipient then
local ent = SF.Entities.Unwrap( recipient )
local ent = SF.GetTypeDef( "Entity" ).__unwrap( recipient )
if not ent.instance then SF.throw( "Entity has no starfall instance", 2 ) end
recipients = {
[ ent.instance ] = true
Expand Down
10 changes: 6 additions & 4 deletions lua/starfall/libs_sh/net.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,19 @@ function net_library.send ( target )
local sendfunc
local newtarget

local unwrap = SF.GetTypeDef( "Entity" ).__unwrap

if target then
if SF.GetType( target ) == "table" then
local nt = { }
for i = 1, #target do
SF.CheckType( SF.Entities.Unwrap( target[ i ] ), "Player", 1 )
nt[ i ] = SF.Entities.Unwrap( target[ i ] )
SF.CheckType( unwrap( target[ i ] ), "Player", 1 )
nt[ i ] = unwrap( target[ i ] )
end
sendfunc, newtarget = net.Send, nt
else
SF.CheckType( SF.Entities.Unwrap( target ), "Player", 1 ) -- TODO: unhacky this
sendfunc, newtarget = net.Send, SF.Entities.Unwrap( target )
SF.CheckType( unwrap( target ), "Player", 1 ) -- TODO: unhacky this
sendfunc, newtarget = net.Send, unwrap.__unwrap( target )
end
else
sendfunc = net.Broadcast
Expand Down
Loading