kirda

Client

Apr 6th, 2021 (edited)
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.12 KB | None | 0 0
  1. --[[
  2. AUTHOR: kirda @imKirda
  3. DATE: April 7 2021
  4.  
  5. DESC:
  6.     Loads framework on the client
  7.  
  8. API:
  9. SHARED:
  10.     void fireRemoteEvent(string, any...)
  11.         - Fires remote event with given name to the server if found
  12.         - Passes next parameters as tuple to the fire parameter
  13.    
  14.     any... awaitRemoteEvent(string)
  15.         - Yields until remote event with given name fires
  16.         - Returns what the remote event yielded
  17.    
  18.     any... invokeRemoteFunction(string, any...)
  19.         - Invokes server with function with given name if found
  20.         - Passes next parameters as tuple to invoke parameter and returns result
  21.    
  22.     RBXScriptConnection onRemoteEvent(string, function)
  23.         - Connects given function to event with given name
  24.         - Returns the connection
  25.    
  26.     void onRemoteFunction(string, function)
  27.         - Sets function with given name's invoke callback to given function
  28.  
  29. --]]
  30. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  31. local Players = game:GetService("Players")
  32.  
  33. local Framework do
  34.     local framework_module = ReplicatedStorage:FindFirstChild("@framework")
  35.    
  36.     if framework_module then
  37.         Framework = require(framework_module)
  38.     else
  39.         error("Could not find '@framework' module in 'ReplicatedStorage'")
  40.     end
  41. end
  42.  
  43. local Internal = Framework.getInternal()
  44.  
  45. local communication_folder = Internal.assertFolder("Communication", ReplicatedStorage.Client)
  46. local modules_folder = Internal.assertFolder("Modules", ReplicatedStorage.Client)
  47.  
  48. local Shared = Internal.getShared()
  49. local Client = {
  50.     Functions = {},
  51.     Bindable = {
  52.         Functions = {},
  53.         Events = {},
  54.     },
  55.     Modules = {},
  56.    
  57.     CommunicationFolder = communication_folder,
  58.     ModulesFolder = modules_folder,
  59. }
  60.  
  61. function Client.Functions.fireRemoteEvent(event_name, ...)
  62.     Framework.assertType(event_name, "string", 1)
  63.    
  64.     local event = Shared.Functions.getRemoteEvent(event_name)
  65.    
  66.     if event then
  67.         event:FireServer(...)
  68.     else
  69.         error(("'%s' is not an existing remote event"):format(event_name), 2)
  70.     end
  71. end
  72.  
  73. function Client.Functions.awaitRemoteEvent(event_name)
  74.     Framework.assertType(event_name, "string", 1)
  75.    
  76.     local event = Shared.Functions.getRemoteEvent(event_name)
  77.    
  78.     if event then
  79.         return event.OnClientEvent:Wait()
  80.     else
  81.         error(("'%s' is not an existing remote event"):format(event_name), 2)
  82.     end
  83. end
  84.  
  85. function Client.Functions.invokeRemoteFunction(function_name, ...)
  86.     Framework.assertType(function_name, "string", 1)
  87.    
  88.     local _function = Shared.Functions.getRemoteFunction(function_name)
  89.    
  90.     if _function then
  91.         return _function:InvokeServer(...)
  92.     else
  93.         error(("'%s' is not an existing remote function"):format(function_name), 2)
  94.     end
  95. end
  96.  
  97. function Client.Functions.onRemoteEvent(event_name, callback)
  98.     Framework.assertType(event_name, "string", 1)
  99.     Framework.assertType(callback, "function", 2)
  100.    
  101.     local event = Shared.Functions.getRemoteEvent(event_name)
  102.    
  103.     if event then
  104.         return event.OnClientEvent:Connect(callback)
  105.     else
  106.         error(("'%s' is not an existing remote event"):format(event_name), 2)
  107.     end
  108. end
  109.  
  110. function Client.Functions.onRemoteFunction(function_name, callback)
  111.     Framework.assertType(function_name, "string", 1)
  112.     Framework.assertType(callback, "function", 2)
  113.    
  114.     local _function = Shared.Functions.getRemoteFunction(function_name)
  115.    
  116.     if _function then
  117.         _function.OnClientInvoke = callback
  118.     else
  119.         error(("'%s' is not an existing remote function"):format(function_name), 2)
  120.     end
  121. end
  122.  
  123. --[[
  124.     Main entry function
  125.     Loads framework on the client
  126. --]]
  127. local function init()
  128.     Internal.lockInternal()
  129.     Internal.initialize()
  130.    
  131.     --[[
  132.         Fires when server registers new event
  133.         Registers the event on client
  134.     --]]
  135.     Shared.Network.Events["@register_remote"].OnClientEvent:Connect(function(remote)
  136.         local remote_directory
  137.         local remote_name = remote.Name
  138.        
  139.         if remote:IsA("RemoteEvent") then
  140.             remote_directory = Shared.Network.Events
  141.         elseif remote:IsA("RemoteFunction") then
  142.             remote_directory = Shared.Network.Functions
  143.         else
  144.             error("Attempt to register non-remote event")
  145.         end
  146.        
  147.         remote_directory[remote_name] = remote
  148.     end)
  149.    
  150.     Internal.connect(Client)
  151.     Internal.finishInitializationPeriod()
  152.     Internal.start()
  153.     Internal.clear()
  154. end
  155.  
  156. init()
Add Comment
Please, Sign In to add comment