SkyyC

Apollo2 Framework Server Loader

Dec 31st, 2021 (edited)
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.59 KB | None | 0 0
  1. --[[
  2.  
  3.     imskyyc
  4.     Apollo2 Server
  5.     11/20/2021
  6.  
  7. ]]--
  8.  
  9. local Server = {
  10.     Systems = {},
  11.     Logs = {
  12.         Info = {},
  13.         Warn = {},
  14.         Error = {}
  15.     }
  16. }
  17.  
  18. local Settings = require(script.Settings); Server.Settings = Settings
  19.  
  20. -- Script Globals --
  21. local oldPrint = print
  22. local oldWarn  = warn
  23. local oldError = error
  24. local oldDebug = debug
  25.  
  26. local debug = {
  27.     output = function(...)
  28.         for _, Message in pairs({...}) do
  29.             if Settings.Debug then
  30.                 local CallingScript = debug.info(2, "s")
  31.                 oldWarn("[Apollo2_Server_" .. CallingScript .. ": DEBUG] " .. tostring(Message))
  32.             end
  33.         end
  34.     end,
  35. }
  36.  
  37.     --// Cache old debug methods
  38.     for Key, Value in pairs(oldDebug) do
  39.         debug[Key] = Value
  40.     end
  41.  
  42.  
  43.  
  44.  
  45. -- Overwrite old Globals --
  46. local print = function(...)
  47.     for _, Message in pairs({...}) do
  48.         local CallingScript = debug.info(2, "s")
  49.         oldPrint("[Apollo2_Server_" .. CallingScript .. ": INFO] " .. tostring(Message))
  50.        
  51.         table.insert(Server.Logs.Info, {
  52.             Stack = CallingScript,
  53.             Message = Message,
  54.             Level = 0
  55.         })
  56.     end
  57. end
  58.  
  59. local warn = function(...)
  60.     for _, Message in pairs({...}) do
  61.         local CallingScript = debug.info(2, "s")
  62.         oldWarn("[Apollo2_Server_" .. CallingScript .. ": WARN] " .. tostring(Message))
  63.        
  64.         table.insert(Server.Logs.Warn, {
  65.             Stack = CallingScript,
  66.             Message = Message,
  67.             Level = 1
  68.         })
  69.     end
  70. end
  71.  
  72. local error = function(...)
  73.     for _, Message in pairs({...}) do
  74.         local CallingScript = debug.info(2, "s")
  75.        
  76.         if Server.HTTP then
  77.             Server.HTTP.Sentry.Push(CallingScript, "MINOR_ERROR", Message)
  78.         end
  79.        
  80.         table.insert(Server.Logs.Error, {
  81.             Stack = CallingScript,
  82.             Message = Message,
  83.             Level = 2
  84.         })
  85.        
  86.         oldWarn("[Apollo2_Server_" .. CallingScript .. ": ERROR] " .. tostring(Message))
  87.     end
  88. end
  89.  
  90.  
  91.  
  92.  
  93. -- Server Utility Functions --
  94. local PCall = function(Function, Critical, ...)
  95.     local Ran, Return = pcall(Function, ...)
  96.    
  97.     if Ran then
  98.         return Ran, Return
  99.     else
  100.         local CallingScript = debug.info(2, "s")
  101.        
  102.         if Server.HTTP then
  103.             Server.HTTP.Sentry.Push(CallingScript, (Critical and "CRITICAL_ERROR") or "ERROR", Return)
  104.         end
  105.        
  106.         table.insert(Server.Logs.Error, {
  107.             Stack = CallingScript,
  108.             Message = Return,
  109.             Level = (Critical and 4) or 3
  110.         })
  111.        
  112.         return false, Return
  113.     end
  114. end
  115.  
  116. local CPCall = function(Function, Critical, ...)
  117.     coroutine.wrap(PCall)(Function, Critical, ...)
  118. end
  119.  
  120.  
  121.  
  122.  
  123. -- Server Instances --
  124. local Folder = script.Parent
  125. local Dependencies = Folder.Dependencies; Server.Dependencies = Dependencies
  126. local Systems = Folder.Systems
  127. local Modules = Folder.Modules; Server.Modules = Modules
  128. local Events = Folder.Events
  129.  
  130.  
  131.  
  132.  
  133. -- Server Tables --
  134. local Service = setmetatable({}, {
  135.     __index = function(Self, Index)
  136.         local CachedService = rawget(Self, Index)
  137.        
  138.         if CachedService then
  139.             return CachedService
  140.         end
  141.        
  142.         local ValidService, Service = PCall(game.GetService, false, game, Index)
  143.         --local ValidService, Service = PCall(function()
  144.         --  local Service = game:GetService(Index)
  145.         --  return Service
  146.         --end, false)
  147.        
  148.         if ValidService then
  149.             Self[Index] = Service
  150.             return Service
  151.         else
  152.             return false
  153.         end
  154.     end,
  155.    
  156.     __newindex = function(Self, Index, Value)
  157.         debug.output("Set-Service-" .. tostring(Index))
  158.         rawset(Self, Index, Value)
  159.     end
  160. })
  161.  
  162. local Environment = {
  163.     Server = Server,
  164.     Service = Service,
  165.    
  166.     oldPrint = oldPrint,
  167.     oldWarn  = oldWarn,
  168.     oldError = oldError,
  169.     oldDebug = oldDebug,
  170.    
  171.     print = print,
  172.     warn  = warn,
  173.     error = error,
  174.     debug = debug,
  175.    
  176.     PCall = PCall,
  177.     CPCall = CPCall
  178. }
  179.  
  180.  
  181.  
  182.  
  183. -- Initialization Process --
  184. local Load = function()
  185.     debug.output("Beginning Apollo2 loading process...")
  186.    
  187.     debug.output("Set-JobId")
  188.     Server.JobId = (Service.RunService:IsStudio() and "Studio_Session_" .. tostring(math.random(1,1000))) or game.JobId
  189.    
  190.     -- Load Core Server Modules --
  191.     debug.output("Load-Core")
  192.     for _, Module in pairs(Modules.Server:GetChildren()) do
  193.         local CoreName = tostring(Module.Name)
  194.        
  195.         debug.output("Require-" .. CoreName)
  196.        
  197.         local IsRunnable, CoreTable = PCall(require, true, Module)
  198.         debug.output("Require-" .. CoreName .. tostring(IsRunnable and "Pass") or "Fail")
  199.        
  200.         if IsRunnable then
  201.             debug.output("Set-" .. CoreName)
  202.             Server[CoreName] = CoreTable
  203.            
  204.             debug.output("Run-" .. CoreName)
  205.             PCall(CoreTable.__Load, true, Environment)
  206.         end
  207.     end
  208.    
  209.     -- Load Systems --
  210.     debug.output("Load-Systems")
  211.     for _, System in pairs(Systems:GetChildren()) do
  212.         local SystemLoader = System:FindFirstChildOfClass("ModuleScript")
  213.        
  214.         if SystemLoader then
  215.             local SystemName = tostring(System)
  216.             local IsCritical = Service.CollectionService:HasTag(SystemLoader, "Critical")
  217.             local IsDisabled = Service.CollectionService:HasTag(SystemLoader, "Disabled")
  218.            
  219.             debug.output("Require-" .. SystemName)
  220.            
  221.             local IsRunnable, SystemFunction = PCall(require, true, SystemLoader)
  222.             debug.output("Require-" .. SystemName .. (IsRunnable and "Pass") or "Fail")
  223.            
  224.             if IsRunnable then
  225.                 debug.output("Run-" .. SystemName)
  226.                 local Ran, SystemTable = PCall(SystemFunction, IsCritical, Environment)
  227.                
  228.                 if Ran then
  229.                     debug.output("Set-" .. SystemName)
  230.                     Server.Systems[SystemName] = SystemTable
  231.                 end
  232.             end
  233.         else
  234.             warn("System: " .. tostring(System) .. " does not have a system loader.")
  235.         end
  236.     end
  237.    
  238.     -- Server Events --
  239.     Server.Process.Events.Load(Events)
  240.    
  241.     print("Apollo2 Version " .. Settings.Version .. " loading process completed."
  242.         .."\nErrors: " .. (tostring(#Server.Logs.Error) or "-1")
  243.         .."\nWarnings: " .. (tostring(#Server.Logs.Warn) or "-1")
  244.     )
  245. end
  246.  
  247. PCall(Load, true)
Add Comment
Please, Sign In to add comment