kirda

Server

Apr 6th, 2021 (edited)
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.48 KB | None | 0 0
  1. --[[
  2. AUTHOR: kirda @imKirda
  3. DATE: April 7 2021
  4.  
  5. DESC:
  6.     Loads framework on the server
  7.    
  8. API:
  9. SHARED:
  10.     void registerRemoteEvent(string, RemoteEvent)
  11.         - Registers remote event with given name if one does not alreay exist
  12.         - If second argument is nil, creates new remote event
  13.    
  14.     void registerRemoteFunction(string, RemoteFunction)
  15.         - Registers remote function with given name if does not already exist
  16.         - If second argument is nil, creates new remote function
  17.    
  18.     void deleteRemoteEvent(string)
  19.         - Destroys remote event with given name if found
  20.    
  21.     void deleteRemoteFunction(string)
  22.         - Destroys remote function with given name if found
  23.    
  24.     void fireRemoteEvent(string, Player, any...)
  25.         - Fires remote event with given name to given client if found
  26.         - Passes next parameters as tuple to the event fire parameter
  27.    
  28.     any... invokeRemoteFunction(string, Player, any...)
  29.         - Invokes remote function with given name to given client if found
  30.         - Passes next parameters as tuple to the invoke parameter, returns result
  31.    
  32.     void fireRemoteEventAll(string, ...)
  33.         - Fires remote event with given name to all clients
  34.         - Passes next parameters as tuple to the fire parameter
  35.    
  36.     RBXScriptConnection onRemoteEvent(string, function)
  37.         - Connects given function to event with given name
  38.         - Returns the connection
  39.    
  40.     void onRemoteFunction(string, function)
  41.         - Sets function with given name's invoke callback to given function
  42.    
  43. --]]
  44. local ServerScriptService = game:GetService("ServerScriptService")
  45. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  46. local ServerStorage = game:GetService("ServerStorage")
  47.  
  48. local Framework do
  49.     local framework_module = ReplicatedStorage:FindFirstChild("@framework")
  50.    
  51.     if framework_module then
  52.         Framework = require(framework_module)
  53.     else
  54.         error("Could not find '@framework' module in 'ReplicatedStorage'")
  55.     end
  56. end
  57.  
  58. local Internal = Framework.getInternal()
  59.  
  60. local communication_folder = Internal.assertFolder("Communication", ServerStorage)
  61. local modules_folder = Internal.assertFolder("Modules", ServerScriptService)
  62.  
  63. local Shared = Internal.getShared()
  64. local Server = {
  65.     Functions = {},
  66.     Bindable = {
  67.         Functions = {},
  68.         Events = {},
  69.     },
  70.     Modules = {},
  71.    
  72.     CommunicationFolder = communication_folder,
  73.     ModulesFolder = modules_folder,
  74. }
  75.  
  76. function Server.Functions.registerRemoteEvent(event_name, event_instance)
  77.     Framework.assertType(event_name, "string", 1)
  78.    
  79.     if Shared.Functions.getRemoteEvent(event_name) then
  80.         error(("'%s' remote event is already registered"):format(event_name), 2)
  81.     end
  82.    
  83.     local event do
  84.         if event_instance then
  85.             Framework.assertType(event_instance, "RemoteEvent", 2)
  86.            
  87.             Internal.setInstanceName(event_instance, event_name)
  88.             event = event_instance
  89.         else
  90.             event = Instance.new("RemoteEvent")
  91.             event.Name = event_name
  92.         end
  93.     end
  94.    
  95.     Internal.setInstanceParent(event, Shared.NetworkFolder)
  96.     Shared.Network.Events[event_name] = event
  97.     Shared.Network.Events["@register_remote"]:FireAllClients(event)
  98. end
  99.  
  100. function Server.Functions.registerRemoteFunction(function_name, function_instance)
  101.     Framework.assertType(function_name, "string", 1)
  102.    
  103.     if Shared.Functions.getRemoteFunction(function_name) then
  104.         error(("'%s' remote function is already registered"):format(function_name), 2)
  105.     end
  106.    
  107.     local _function do
  108.         if function_instance then
  109.             Framework.assertType(function_name, "RemoteFunction", 2)
  110.            
  111.             Internal.setInstanceName(function_instance, function_name)
  112.             _function = function_instance
  113.         else
  114.             _function = Instance.new("RemoteFunction")
  115.             _function.Name = function_name
  116.         end
  117.     end
  118.    
  119.     Internal.setInstanceParent(_function, Shared.NetworkFolder)
  120.     Shared.Network.Functions[function_name] = _function
  121.     Shared.Network.Events["@register_remote"]:FireAllClients(_function)
  122. end
  123.  
  124. function Server.Functions.deleteRemoteEvent(event_name)
  125.     Framework.assertType(event_name, "string", 1)
  126.    
  127.     local event = Shared.Functions.getRemoteEvent(event_name)
  128.    
  129.     if event then
  130.         event:Destroy()
  131.     else
  132.         error(("'%s' is not an existing remote event"):format(event_name), 2)
  133.     end
  134. end
  135.  
  136. function Server.Functions.deleteRemoteFunction(function_name)
  137.     Framework.assertType(function_name, "string", 1)
  138.    
  139.     local _function = Shared.Functions.getRemoteFunction(function_name)
  140.    
  141.     if _function then
  142.         _function:Destroy()
  143.     else
  144.         error(("'%s' is not an existing remote functions"):format(function_name), 2)
  145.     end
  146. end
  147.  
  148. function Server.Functions.fireRemoteEvent(event_name, client, ...)
  149.     Framework.assertType(event_name, "string", 1)
  150.     Framework.assertType(client, "Player", 2)
  151.    
  152.     local event = Shared.Functions.getRemoteEvent(event_name)
  153.    
  154.     if event then
  155.         event:FireClient(client, ...)
  156.     else
  157.         error(("'%s' is not an existing remote event"):format(event_name), 2)
  158.     end
  159. end
  160.  
  161. function Server.Functions.fireRemoteEventAll(event_name, ...)
  162.     Framework.assertType(event_name, "string", 1)
  163.    
  164.     local event = Shared.Functions.getRemoteEvent(event_name)
  165.    
  166.     if event then
  167.         event:FireAllClients(...)
  168.     else
  169.         error(("'%s' is not an existing remote event"):format(event_name), 2)
  170.     end
  171. end
  172.  
  173. function Server.Functions.invokeRemoteFunction(function_name, client, ...)
  174.     Framework.assertType(function_name, "string", 1)
  175.     Framework.assertType(client, "Player", 2)
  176.    
  177.     local _function = Shared.Functions.getRemoteFunction(function_name)
  178.    
  179.     if _function then
  180.         return _function:InvokeClient(client, ...)
  181.     else
  182.         error(("'%s' is not an existing remote function"):format(function_name), 2)
  183.     end
  184. end
  185.  
  186. function Server.Functions.onRemoteEvent(event_name, callback)
  187.     Framework.assertType(event_name, "string", 1)
  188.     Framework.assertType(callback, "function", 2)
  189.    
  190.     local event = Shared.Functions.getRemoteEvent(event_name)
  191.    
  192.     if event then
  193.         return event.OnServerEvent:Connect(callback)
  194.     else
  195.         error(("'%s' is not an existing remote event"):format(event_name), 2)
  196.     end
  197. end
  198.  
  199. function Server.Functions.onRemoteFunction(function_name, callback)
  200.     Framework.assertType(function_name, "string", 1)
  201.     Framework.assertType(callback, "function", 2)
  202.    
  203.     local _function = Shared.Functions.getRemoteFunction(function_name)
  204.    
  205.     if _function then
  206.         _function.OnServerInvoke = callback
  207.     else
  208.         error(("'%s' is not an existing remote function"):format(function_name), 2)
  209.     end
  210. end
  211.  
  212. --[[
  213.     Main entry function
  214.     Loads framework on the server
  215. --]]
  216. local function init()
  217.     Internal.lockInternal()
  218.     Internal.initialize()
  219.     Internal.connect(Server)
  220.     Internal.finishInitializationPeriod()
  221.     Internal.start()
  222.     Internal.clear()
  223. end
  224.  
  225. init()
Add Comment
Please, Sign In to add comment