Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- imskyyc
- Apollo2 Server
- 11/20/2021
- ]]--
- local Server = {
- Systems = {},
- Logs = {
- Info = {},
- Warn = {},
- Error = {}
- }
- }
- local Settings = require(script.Settings); Server.Settings = Settings
- -- Script Globals --
- local oldPrint = print
- local oldWarn = warn
- local oldError = error
- local oldDebug = debug
- local debug = {
- output = function(...)
- for _, Message in pairs({...}) do
- if Settings.Debug then
- local CallingScript = debug.info(2, "s")
- oldWarn("[Apollo2_Server_" .. CallingScript .. ": DEBUG] " .. tostring(Message))
- end
- end
- end,
- }
- --// Cache old debug methods
- for Key, Value in pairs(oldDebug) do
- debug[Key] = Value
- end
- -- Overwrite old Globals --
- local print = function(...)
- for _, Message in pairs({...}) do
- local CallingScript = debug.info(2, "s")
- oldPrint("[Apollo2_Server_" .. CallingScript .. ": INFO] " .. tostring(Message))
- table.insert(Server.Logs.Info, {
- Stack = CallingScript,
- Message = Message,
- Level = 0
- })
- end
- end
- local warn = function(...)
- for _, Message in pairs({...}) do
- local CallingScript = debug.info(2, "s")
- oldWarn("[Apollo2_Server_" .. CallingScript .. ": WARN] " .. tostring(Message))
- table.insert(Server.Logs.Warn, {
- Stack = CallingScript,
- Message = Message,
- Level = 1
- })
- end
- end
- local error = function(...)
- for _, Message in pairs({...}) do
- local CallingScript = debug.info(2, "s")
- if Server.HTTP then
- Server.HTTP.Sentry.Push(CallingScript, "MINOR_ERROR", Message)
- end
- table.insert(Server.Logs.Error, {
- Stack = CallingScript,
- Message = Message,
- Level = 2
- })
- oldWarn("[Apollo2_Server_" .. CallingScript .. ": ERROR] " .. tostring(Message))
- end
- end
- -- Server Utility Functions --
- local PCall = function(Function, Critical, ...)
- local Ran, Return = pcall(Function, ...)
- if Ran then
- return Ran, Return
- else
- local CallingScript = debug.info(2, "s")
- if Server.HTTP then
- Server.HTTP.Sentry.Push(CallingScript, (Critical and "CRITICAL_ERROR") or "ERROR", Return)
- end
- table.insert(Server.Logs.Error, {
- Stack = CallingScript,
- Message = Return,
- Level = (Critical and 4) or 3
- })
- return false, Return
- end
- end
- local CPCall = function(Function, Critical, ...)
- coroutine.wrap(PCall)(Function, Critical, ...)
- end
- -- Server Instances --
- local Folder = script.Parent
- local Dependencies = Folder.Dependencies; Server.Dependencies = Dependencies
- local Systems = Folder.Systems
- local Modules = Folder.Modules; Server.Modules = Modules
- local Events = Folder.Events
- -- Server Tables --
- local Service = setmetatable({}, {
- __index = function(Self, Index)
- local CachedService = rawget(Self, Index)
- if CachedService then
- return CachedService
- end
- local ValidService, Service = PCall(game.GetService, false, game, Index)
- --local ValidService, Service = PCall(function()
- -- local Service = game:GetService(Index)
- -- return Service
- --end, false)
- if ValidService then
- Self[Index] = Service
- return Service
- else
- return false
- end
- end,
- __newindex = function(Self, Index, Value)
- debug.output("Set-Service-" .. tostring(Index))
- rawset(Self, Index, Value)
- end
- })
- local Environment = {
- Server = Server,
- Service = Service,
- oldPrint = oldPrint,
- oldWarn = oldWarn,
- oldError = oldError,
- oldDebug = oldDebug,
- print = print,
- warn = warn,
- error = error,
- debug = debug,
- PCall = PCall,
- CPCall = CPCall
- }
- -- Initialization Process --
- local Load = function()
- debug.output("Beginning Apollo2 loading process...")
- debug.output("Set-JobId")
- Server.JobId = (Service.RunService:IsStudio() and "Studio_Session_" .. tostring(math.random(1,1000))) or game.JobId
- -- Load Core Server Modules --
- debug.output("Load-Core")
- for _, Module in pairs(Modules.Server:GetChildren()) do
- local CoreName = tostring(Module.Name)
- debug.output("Require-" .. CoreName)
- local IsRunnable, CoreTable = PCall(require, true, Module)
- debug.output("Require-" .. CoreName .. tostring(IsRunnable and "Pass") or "Fail")
- if IsRunnable then
- debug.output("Set-" .. CoreName)
- Server[CoreName] = CoreTable
- debug.output("Run-" .. CoreName)
- PCall(CoreTable.__Load, true, Environment)
- end
- end
- -- Load Systems --
- debug.output("Load-Systems")
- for _, System in pairs(Systems:GetChildren()) do
- local SystemLoader = System:FindFirstChildOfClass("ModuleScript")
- if SystemLoader then
- local SystemName = tostring(System)
- local IsCritical = Service.CollectionService:HasTag(SystemLoader, "Critical")
- local IsDisabled = Service.CollectionService:HasTag(SystemLoader, "Disabled")
- debug.output("Require-" .. SystemName)
- local IsRunnable, SystemFunction = PCall(require, true, SystemLoader)
- debug.output("Require-" .. SystemName .. (IsRunnable and "Pass") or "Fail")
- if IsRunnable then
- debug.output("Run-" .. SystemName)
- local Ran, SystemTable = PCall(SystemFunction, IsCritical, Environment)
- if Ran then
- debug.output("Set-" .. SystemName)
- Server.Systems[SystemName] = SystemTable
- end
- end
- else
- warn("System: " .. tostring(System) .. " does not have a system loader.")
- end
- end
- -- Server Events --
- Server.Process.Events.Load(Events)
- print("Apollo2 Version " .. Settings.Version .. " loading process completed."
- .."\nErrors: " .. (tostring(#Server.Logs.Error) or "-1")
- .."\nWarnings: " .. (tostring(#Server.Logs.Warn) or "-1")
- )
- end
- PCall(Load, true)
Add Comment
Please, Sign In to add comment