Skip to content

Commit

Permalink
Rev. B63
Browse files Browse the repository at this point in the history
  • Loading branch information
hoontee committed Jul 5, 2024
1 parent 5e6244c commit 21070bd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 54 deletions.
74 changes: 34 additions & 40 deletions Pronghorn/Remotes/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
╚═══════════════════════════════════════════════╝
]]

local Remotes = {}
local Remotes = {Server = {}, Client = {}}

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Dependencies
Expand All @@ -29,15 +29,18 @@ local TypeChecker = require(script.TypeChecker)
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

-- Types
type GenericRemote = typeof(setmetatable({} :: {
Fire: (self: GenericRemote, players: Player | {Player}, ...any) -> (...any);
FireAll: (self: GenericRemote, ...any) -> ();
FireAllExcept: (self: GenericRemote, ignorePlayer: Player, ...any) -> ();
SetListener: (self: GenericRemote, newFunction: (Player, ...any) -> (...any)) -> ();
AddListener: (self: GenericRemote, newFunction: (Player, ...any) -> ()) -> RBXScriptConnection;
Connect: (self: GenericRemote, func: (...any) -> ()) -> RBXScriptConnection;
type ServerRemote = {
Fire: (self: ServerRemote, players: Player | {Player}, ...any) -> (...any);
FireAll: (self: ServerRemote, ...any) -> ();
FireAllExcept: (self: ServerRemote, ignorePlayer: Player, ...any) -> ();
SetListener: (self: ServerRemote, newFunction: (Player, ...any) -> (...any)) -> ();
AddListener: (self: ServerRemote, newFunction: (Player, ...any) -> ()) -> RBXScriptConnection;
}
type ClientRemote = typeof(setmetatable({} :: {
Fire: (self: ClientRemote, ...any) -> (...any);
Connect: (self: ClientRemote, func: (...any) -> ()) -> RBXScriptConnection;
}, {} :: {
__call: (self: GenericRemote, context: any, ...any) -> (...any)
__call: (self: ClientRemote, context: any, ...any) -> (...any)
}))

-- Objects
Expand All @@ -57,12 +60,17 @@ local function connectEventClient(remote: RemoteFunction | UnreliableRemoteEvent
local moduleName: string = (remote :: any).Parent.Name
local debugPrintText = `{moduleName}:{remote.Name}`
local actions: any = {}
local metaTable: any = {}

if not Remotes[moduleName] then
Remotes[moduleName] = {}
local metaTable: any = {
__call = function(_, context: any, ...: any)
if context ~= Remotes.Client[moduleName] then error(`Must call {moduleName}:{remote.Name}() with a colon`, 0) end
return actions:Fire(...)
end;
}

if not Remotes.Client[moduleName] then
Remotes.Client[moduleName] = {}
end
Remotes[moduleName][remote.Name] = setmetatable(actions, metaTable) :: GenericRemote
Remotes.Client[moduleName][remote.Name] = setmetatable(actions, metaTable) :: ClientRemote

if remote:IsA("RemoteFunction") then

Expand All @@ -75,13 +83,6 @@ local function connectEventClient(remote: RemoteFunction | UnreliableRemoteEvent
return remote:InvokeServer(...)
end

metaTable.__call = function(_, context: any, ...: any)
if context == Remotes[moduleName] then
return actions:Fire(...)
end
return actions:Fire(context, ...)
end

elseif remote:IsA("UnreliableRemoteEvent") or remote:IsA("RemoteEvent") then
local lastServerTime: number;

Expand All @@ -97,13 +98,6 @@ local function connectEventClient(remote: RemoteFunction | UnreliableRemoteEvent
Print(getEnvironment(), debugPrintText, {...});
(remote :: RemoteEvent):FireServer(...)
end

metaTable.__call = function(_, context: any, ...: any)
if context == Remotes[moduleName] then
return actions:Fire(...)
end
return actions:Fire(context, ...)
end
end
end

Expand All @@ -121,26 +115,26 @@ end
--- @error Remotes.CreateToClient: Parameter 'remoteType' expected 'nil | Unreliable" | "Reliable" | "Returns"', got '{remoteType}' -- Incorrect usage.
--- @error Creating remotes under the ModuleScript name '{moduleName}' would overwrite a function -- Not allowed.
--- @error Remote '{name}' already created in '{moduleName}' -- Duplicate.
function Remotes:CreateToClient(name: string, requiredParameterTypes: {string}, remoteType: ("Unreliable" | "Reliable" | "Returns")?): GenericRemote
function Remotes.Server:CreateToClient(name: string, requiredParameterTypes: {string}, remoteType: ("Unreliable" | "Reliable" | "Returns")?): ServerRemote
if RunService:IsClient() then error("Remotes cannot be created on the client", 0) end
if type(requiredParameterTypes) ~= "table" then error(`Remotes.CreateToClient: Parameter 'requiredParameterTypes' expected type '\{string}', got '{typeof(requiredParameterTypes)}'`, 0) end
if remoteType ~= nil and remoteType ~= "Unreliable" and remoteType ~= "Reliable" and remoteType ~= "Returns" then error(`Remotes.CreateToClient: Parameter 'remoteType' expected 'nil | "Unreliable" | "Reliable" | "Returns"', got '{remoteType}'`, 0) end

local split = (debug.info(2, "s") :: string):split(".")
local moduleName = split[#split]

if type(Remotes[moduleName]) == "function" then error(`Creating remotes under the ModuleScript name '{moduleName}' would overwrite a function`, 0) end
if Remotes[moduleName] and Remotes[moduleName][name] then error(`Remote '{name}' already created in '{moduleName}'`, 0) end
if type(Remotes.Server[moduleName]) == "function" then error(`Creating remotes under the ModuleScript name '{moduleName}' would overwrite a function`, 0) end
if Remotes.Server[moduleName] and Remotes.Server[moduleName][name] then error(`Remote '{name}' already created in '{moduleName}'`, 0) end

local environment = "[" .. moduleName .. "]"
local serverFolder = remotesFolder:FindFirstChild(moduleName) or New.Instance("Folder", remotesFolder, moduleName)
local remote = New.Instance(if remoteType == "Returns" then "RemoteFunction" elseif remoteType == "Unreliable" then "UnreliableRemoteEvent" else "RemoteEvent", serverFolder, name)
local actions: any = {}

if not Remotes[moduleName] then
Remotes[moduleName] = {}
if not Remotes.Server[moduleName] then
Remotes.Server[moduleName] = {}
end
Remotes[moduleName][remote.Name] = actions :: GenericRemote
Remotes.Server[moduleName][remote.Name] = actions :: ServerRemote

if remoteType == "Returns" then
actions.Fire = function(_, player: Player, ...: any)
Expand Down Expand Up @@ -192,7 +186,7 @@ end
--- @error Remotes.CreateToClient: Parameter 'remoteType' expected 'nil | Unreliable" | "Reliable" | "Returns"', got '{remoteType}' -- Incorrect usage.
--- @error Creating remotes under the ModuleScript name '{moduleName}' would overwrite a function -- Not allowed.
--- @error Remote '{name}' already created in '{moduleName}' -- Duplicate.
function Remotes:CreateToServer(name: string, requiredParameterTypes: {string}, remoteType: ("Unreliable" | "Reliable" | "Returns")?, func: (Player, ...any) -> (...any)?): GenericRemote
function Remotes.Server:CreateToServer(name: string, requiredParameterTypes: {string}, remoteType: ("Unreliable" | "Reliable" | "Returns")?, func: (Player, ...any) -> (...any)?): ServerRemote
if RunService:IsClient() then error("Remotes cannot be created on the client", 0) end
if type(requiredParameterTypes) ~= "table" then error(`Remotes.CreateToServer: Parameter 'requiredParameterTypes' expected type '\{string}', got '{typeof(requiredParameterTypes)}'`, 0) end
if remoteType ~= nil and remoteType ~= "Unreliable" and remoteType ~= "Reliable" and remoteType ~= "Returns" then error(`Remotes.CreateToClient: Parameter 'remoteType' expected 'nil | "Unreliable" | "Reliable" | "Returns"', got '{remoteType}'`, 0) end
Expand All @@ -201,17 +195,17 @@ function Remotes:CreateToServer(name: string, requiredParameterTypes: {string},
local split = (debug.info(2, "s") :: string):split(".")
local moduleName = split[#split]

if type(Remotes[moduleName]) == "function" then error(`Creating remotes under the ModuleScript name '{moduleName}' would overwrite a function`, 0) end
if Remotes[moduleName] and Remotes[moduleName][name] then error(`Remote '{name}' already created in '{moduleName}'`, 0) end
if type(Remotes.Server[moduleName]) == "function" then error(`Creating remotes under the ModuleScript name '{moduleName}' would overwrite a function`, 0) end
if Remotes.Server[moduleName] and Remotes.Server[moduleName][name] then error(`Remote '{name}' already created in '{moduleName}'`, 0) end

local serverFolder = remotesFolder:FindFirstChild(moduleName) or New.Instance("Folder", remotesFolder, moduleName)
local remote = New.Instance(if remoteType == "Returns" then "RemoteFunction" elseif remoteType == "Unreliable" then "UnreliableRemoteEvent" else "RemoteEvent", serverFolder, name)
local actions: any = {}

if not Remotes[moduleName] then
Remotes[moduleName] = {}
if not Remotes.Server[moduleName] then
Remotes.Server[moduleName] = {}
end
Remotes[moduleName][remote.Name] = actions :: GenericRemote
Remotes.Server[moduleName][remote.Name] = actions :: ServerRemote

local function getTypeCheckedFunction(newFunction: (Player, ...any) -> (...any))
return function(player: Player, ...: any)
Expand Down
2 changes: 1 addition & 1 deletion Pronghorn/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
║ ██████▀██▓▌▀▌ ▄ ▄▓▌▐▓█▌ ║
║ ║
║ ║
║ Pronghorn Framework Rev. B62
║ Pronghorn Framework Rev. B63
║ https://github.com/Iron-Stag-Games/Pronghorn ║
║ GNU Lesser General Public License v2.1 ║
║ ║
Expand Down
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,25 @@ No Controllers or Services, just Modules and Remotes.
## Remotes
```lua
-- Creation
local exampleRemote = Remotes:CreateToClient(name: string, requiredParameterTypes: {string}, remoteType: ("Unreliable" | "Reliable" | "Returns")?): any
local exampleRemote = Remotes:CreateToServer(name: string, requiredParameterTypes: {string}, remoteType: ("Unreliable" | "Reliable" | "Returns")?, func: (Player, ...any) -> (...any)?): any
local exampleRemote = Remotes.Server:CreateToClient(name: string, requiredParameterTypes: {string}, remoteType: ("Unreliable" | "Reliable" | "Returns")?): any
local exampleRemote = Remotes.Server:CreateToServer(name: string, requiredParameterTypes: {string}, remoteType: ("Unreliable" | "Reliable" | "Returns")?, func: (Player, ...any) -> (...any)?): any

-- Server Invocation Absolute
Remotes.ExampleModule.ExampleRemote:Fire(players: Player | {Player}, ...)
Remotes.ExampleModule.ExampleRemote:FireAll(...)
Remotes.ExampleModule.ExampleRemote:FireAllExcept(ignorePlayer: Player, ....)
Remotes.Server.ExampleModule.ExampleRemote:Fire(players: Player | {Player}, ...)
Remotes.Server.ExampleModule.ExampleRemote:FireAll(...)
Remotes.Server.ExampleModule.ExampleRemote:FireAllExcept(ignorePlayer: Player, ....)

-- Server Invocation Shortcut
exampleRemote:Fire(players: Player | {Player}, ...)
exampleRemote:FireAll(...)
exampleRemote:FireAllExcept(ignorePlayer: Player, ....)

-- Client Invocation Absolute
Remotes.ExampleModule:ExampleRemote(...)
Remotes.ExampleModule.ExampleRemote:Fire(...)
Remotes.Client.ExampleModule:ExampleRemote(...)
Remotes.Client.ExampleModule.ExampleRemote:Fire(...)

-- Client Invocation Shortcut
local exampleRemote = Remotes.ExampleModule.ExampleRemote
local exampleRemote = Remotes.Client.ExampleModule.ExampleRemote
exampleRemote:Fire(...)
```

Expand Down Expand Up @@ -170,11 +170,11 @@ end
```lua
-- On Server

local tableCounted = Remotes:CreateToClient("TableCounted", {"string"})
local tableCounted = Remotes.Server:CreateToClient("TableCounted", {"string"})
-- Second parameter is nil, so this Remote is non-returning.

Remotes:CreateToServer("CountTable", {"table"}, "Returns", function(player: Player, tableToCount: {any})
Remotes.ExampleServerModule.TableCounted:FireAll(player.Name) -- Absolute method
Remotes.Server:CreateToServer("CountTable", {"table"}, "Returns", function(player: Player, tableToCount: {any})
Remotes.Server.ExampleServerModule.TableCounted:FireAll(player.Name) -- Absolute method
tableCounted:FireAll(player.Name) -- Shortcut method
return #tableToCount
end)
Expand All @@ -183,12 +183,12 @@ end)
```lua
-- On Client

Remotes.ExampleServerModule.TableCounted:Connect(function(playerName: string)
Remotes.Client.ExampleServerModule.TableCounted:Connect(function(playerName: string)
Print(playerName, "requested a Table to be counted.")
end)

function ExampleClientModule:Deferred()
Print(Remotes.ExampleServerModule:CountTable({"A", "B", "C"}))
Print(Remotes.Client.ExampleServerModule:CountTable({"A", "B", "C"}))
end
```

Expand Down

0 comments on commit 21070bd

Please sign in to comment.