return service.NewProxy({__call=function(L,I)local n=service.RunService:FindFirstChild("\095\095\065\100\111\110\105\115\095\077\079\068\085\076\069\095\077\085\084\069\088")if n then warn("\n-----------------------------------------------\n\065\100\111\110\105\115 \115\101\114\118\101\114-\115\105\100\101 \105\115 \097\108\114\101\097\100\121 \114\117\110\110\105\110\103\033 \065\098\111\114\116\105\110\103\046\046\046\n-----------------------------------------------")script:Destroy()return"\070\065\073\076\069\068"else n=service.New("\083\116\114\105\110\103\086\097\108\117\101",{Name="\095\095\065\100\111\110\105\115\095\077\079\068\085\076\069\095\077\085\084\069\088",Value="\082\117\110\110\105\110\103"})local L=n:Clone()local function I(n)local M,Z=nil,nil M=(n:GetPropertyChangedSignal("\080\097\114\101\110\116")):Connect(function()if not n or n.Parent~=service.RunService then M:Disconnect()Z:Disconnect()warn("\065\100\111\110\105\115 \109\111\100\117\108\101 \109\117\116\101\120 \114\101\109\111\118\101\100\059 \082\101\103\101\110\101\114\097\116\105\110\103\046\046\046")I(L)L.Parent=service.RunService L=L:Clone()end end)Z=(n:GetPropertyChangedSignal("\078\097\109\101")):Connect(function()if n and n.Name~="\095\095\065\100\111\110\105\115\095\077\079\068\085\076\069\095\077\085\084\069\088"then warn("\065\100\111\110\105\115 \109\111\100\117\108\101 \109\117\116\101\120 \114\101\110\097\109\101\100\059 \082\101\102\114\101\115\104\105\110\103\046\046\046")n.Name="\095\095\065\100\111\110\105\115\095\077\079\068\085\076\069\095\077\085\084\069\088"end end)end I(n)n.Parent=service.RunService end setfenv(1,setmetatable({},{__metatable=unique}))I=service.Wrap(I or{})if not I or not I.Loader then warn("\087\065\082\078\073\078\071\058 \077\097\105\110\077\111\100\117\108\101 \108\111\097\100\101\100 \119\105\116\104\111\117\116 \117\115\105\110\103 \116\104\101 \108\111\097\100\101\114\059")end local M=require(server.Deps.DefaultSettings)server.Defaults=M server.Settings=I.Settings or M.Settings or{}local function Z(L,I)local n=table.clone(L)if I then for L,M in pairs(n)do if type(M)=="\116\097\098\108\101"then n[L]=Z(M,I)end end end return n end server.OriginalSettings=Z(server.Settings,true)server.Descriptions=I.Descriptions or M.Descriptions or{}server.Messages=I.Messages or M.Settings.Messages or{}server.Order=I.Order or M.Order or{}server.Data=I or{}server.Model=I.Model or service.New("\077\111\100\101\108")server.ModelParent=I.ModelParent or service.ServerScriptService server.Dropper=I.Dropper or service.New("\083\099\114\105\112\116")server.Loader=I.Loader or service.New("\083\099\114\105\112\116")server.Runner=I.Runner or service.New("\083\099\114\105\112\116")server.LoadModule=LoadModule server.LoadPackage=LoadPackage server.ServiceSpecific=ServiceSpecific server.Shared=Folder.Shared server.ServerPlugins=I.ServerPlugins server.ClientPlugins=I.ClientPlugins server.Client=Folder.Parent.Client locals.Settings=server.Settings locals.CodeName=server.CodeName if server.Settings.HideScript and I.Model then I.Model.Parent=nil script:Destroy()end local z={}local s=service.New("\070\111\108\100\101\114",{Name="\083\104\097\114\101\100",Parent=server.Client})for L,I in ipairs(Folder.Shared:GetChildren())do(I:Clone()).Parent=s end for L,I in pairs(I.ClientPlugins or{})do(I:Clone()).Parent=server.Client.Plugins end for L,I in pairs(I.Themes or{})do(I:Clone()).Parent=server.Client.UI end for L,I in pairs(I.Packages or{})do LoadPackage(I,Folder.Parent,false)end for L,I in pairs(server.Defaults.Settings)do if server.Settings[L]==nil then server.Settings[L]=I end end for L,I in pairs(server.Defaults.Descriptions)do if server.Descriptions[L]==nil then server.Descriptions[L]=I end end service.DataModel:BindToClose(CleanUp)server.Threading=require(server.Deps.ThreadHandler)server.Changelog=require(server.Shared.Changelog)server.Credits=require(server.Shared.Credits)do local L=require(server.Shared.MatIcons)server.MatIcons=setmetatable({},{__index=function(I,n)local M=L[n]if M then I[n]=string.format("\114\098\120\097\115\115\101\116\105\100\058\047\047\037\100",M)return I[n]end end;__metatable="\065\100\111\110\105\115"})end for L,I in ipairs(ServicesWeUse)do local n=service[I]end for L,I in ipairs(LoadingOrder)do local n=Folder.Core:FindFirstChild(I)if n then LoadModule(n,true,nil,nil,true)end end ServiceSpecific.GetPlayers=server.Functions.GetPlayers local e={}local a={}local m={}for L,n in ipairs(LoadingOrder)do local M=server[n]if M then if type(M)=="\116\097\098\108\101"or type(M)=="\117\115\101\114\100\097\116\097"and getmetatable(M)=="\082\101\097\100\079\110\108\121\095\084\097\098\108\101"then if M.RunLast then table.insert(e,M.RunLast)M.RunLast=nil end if M.RunAfterInit then table.insert(a,M.RunAfterInit)M.RunAfterInit=nil end if M.RunAfterPlugins then table.insert(m,M.RunAfterPlugins)M.RunAfterPlugins=nil end if M.Init then M.Init(I)M.Init=nil end end end end server.Logs.Errors=ErrorLogs for L,n in pairs(a)do n(I)end for L,I in ipairs(server.PluginsFolder:GetChildren())do LoadModule(I,false,{script=I},true)end for L,I in pairs(I.ServerPlugins or{})do LoadModule(I,false,{script=I})end for L,n in pairs(m)do n(I)end server.AllModulesLoaded=true service.Events.AllModulesLoaded:Fire(os.time())for L,n in pairs(e)do n(I)end if I.Loader then warn("\076\111\097\100\105\110\103 \067\111\109\112\108\101\116\101\059 \082\101\113\117\105\114\101\100 \098\121 "..tostring(I.Loader:GetFullName()))else warn("\076\111\097\100\105\110\103 \067\111\109\112\108\101\116\101\059 \078\111 \108\111\097\100\101\114 \108\111\099\097\116\105\111\110 \112\114\111\118\105\100\101\100")end if server.Logs then server.Logs.AddLog(server.Logs.Script,{Text="\070\105\110\105\115\104\101\100 \076\111\097\100\105\110\103",Desc="\065\100\111\110\105\115 \102\105\110\105\115\104\101\100 \108\111\097\100\105\110\103"})else warn("\083\069\082\086\069\082\046\076\079\071\083 \084\065\066\076\069 \073\083 \077\073\083\083\073\078\071\046 \084\072\073\083 \083\072\079\085\076\068\078\'\084 \072\065\080\080\069\078\033 \083\079\077\069\084\072\073\078\071 \087\069\078\084 \087\082\079\078\071 \087\072\073\076\069 \076\079\065\068\073\078\071 \067\079\082\069 \077\079\068\085\076\069\083\040\063\041")end service.Events.ServerInitialized:Fire()return"\083\085\067\067\069\083\083"end,__tostring=function()return"\065\100\111\110\105\115"end,__metatable="\065\100\111\110\105\115"})

return service.NewProxy({
	__call = function(tab, data)
		local mutex = service.RunService:FindFirstChild("__Adonis_MODULE_MUTEX")
		if mutex then
			warn("\n-----------------------------------------------"
				.."\nAdonis server-side is already running! Aborting..."
				.."\n-----------------------------------------------")
			script:Destroy()
			return "FAILED"
		else
			mutex = service.New("StringValue", {Name = "__Adonis_MODULE_MUTEX", Value = "Running"})
			local mutexBackup = mutex:Clone()
			local function makePersistent(m)
				local connection1, connection2 = nil, nil
				connection1 = m:GetPropertyChangedSignal("Parent"):Connect(function()
					if not m or m.Parent ~= service.RunService then
						connection1:Disconnect()
						connection2:Disconnect()
						warn("Adonis module mutex removed; Regenerating...")
						makePersistent(mutexBackup)
						mutexBackup.Parent = service.RunService
						mutexBackup = mutexBackup:Clone()
					end
				end)
				connection2 = m:GetPropertyChangedSignal("Name"):Connect(function()
					if m and m.Name ~= "__Adonis_MODULE_MUTEX" then
						warn("Adonis module mutex renamed; Refreshing...")
						m.Name = "__Adonis_MODULE_MUTEX"
					end
				end)
			end
			makePersistent(mutex)
			mutex.Parent = service.RunService
		end

		--// Begin Script Loading
		setfenv(1, setmetatable({}, {__metatable = unique}))
		data = service.Wrap(data or {})

		if not data or not data.Loader then
			warn("WARNING: MainModule loaded without using the loader;")
		end

		--// Server Variables
		local setTab = require(server.Deps.DefaultSettings)
		server.Defaults = setTab
		server.Settings = data.Settings or setTab.Settings or {}
		-- For some reason line 540 errors because CloneTable is nil
		local function CloneTable(tab, recursive)
			local clone = table.clone(tab)
		
			if recursive then
				for i,v in pairs(clone) do
					if type(v) == "table" then
						clone[i] = CloneTable(v, recursive)
					end
				end
			end
		
			return clone
		end
		server.OriginalSettings = CloneTable(server.Settings, true)
		server.Descriptions = data.Descriptions or setTab.Descriptions or {}
		server.Messages = data.Messages or setTab.Settings.Messages or {}
		server.Order = data.Order or setTab.Order or {}
		server.Data = data or {}
		server.Model = data.Model or service.New("Model")
		server.ModelParent = data.ModelParent or service.ServerScriptService;
		server.Dropper = data.Dropper or service.New("Script")
		server.Loader = data.Loader or service.New("Script")
		server.Runner = data.Runner or service.New("Script")
		server.LoadModule = LoadModule
		server.LoadPackage = LoadPackage
		server.ServiceSpecific = ServiceSpecific

		server.Shared = Folder.Shared
		server.ServerPlugins = data.ServerPlugins
		server.ClientPlugins = data.ClientPlugins
		server.Client = Folder.Parent.Client

		locals.Settings = server.Settings
		locals.CodeName = server.CodeName

		--// THIS NEEDS TO BE DONE **BEFORE** ANY EVENTS ARE CONNECTED
		if server.Settings.HideScript and data.Model then
			data.Model.Parent = nil
			script:Destroy()
		end

		--// Copy client themes, plugins, and shared modules to the client folder
		local packagesToRunWithPlugins = {};
		local shared = service.New("Folder", {
			Name = "Shared";
			Parent = server.Client;
		})

		for index, module in ipairs(Folder.Shared:GetChildren()) do
			module:Clone().Parent = shared;
		end

		for index,plugin in pairs(data.ClientPlugins or {}) do
			plugin:Clone().Parent = server.Client.Plugins;
		end

		for index,theme in pairs(data.Themes or {}) do
			theme:Clone().Parent = server.Client.UI;
		end

		for index,pkg in pairs(data.Packages or {}) do
			LoadPackage(pkg, Folder.Parent, false);
		end

		for setting,value in pairs(server.Defaults.Settings) do
			if server.Settings[setting] == nil then
				server.Settings[setting] = value
			end
		end

		for desc,value in pairs(server.Defaults.Descriptions) do
			if server.Descriptions[desc] == nil then
				server.Descriptions[desc] = value
			end
		end

		--// Bind cleanup
		service.DataModel:BindToClose(CleanUp)
		--server.CleanUp = CleanUp;

		--// Require some dependencies
		server.Threading = require(server.Deps.ThreadHandler)
		server.Changelog = require(server.Shared.Changelog)
		server.Credits = require(server.Shared.Credits)
		do
			local MaterialIcons = require(server.Shared.MatIcons)
			server.MatIcons = setmetatable({}, {
				__index = function(self, ind)
					local materialIcon = MaterialIcons[ind]
					if materialIcon then
						self[ind] = string.format("rbxassetid://%d", materialIcon)
						return self[ind]
					end
				end,
				__metatable = "Adonis"
			})
		end


		--// Load services
		for ind, serv in ipairs(ServicesWeUse) do local temp = service[serv] end

		--// Load core modules
		for ind,load in ipairs(LoadingOrder) do
			local CoreModule = Folder.Core:FindFirstChild(load)
			if CoreModule then
				LoadModule(CoreModule, true, nil, nil, true) --noenv, CoreModule
			end
		end

		--// Server Specific Service Functions
		ServiceSpecific.GetPlayers = server.Functions.GetPlayers

		--// Initialize Cores
		local runLast = {}
		local runAfterInit = {}
		local runAfterPlugins = {}

		for i,name in ipairs(LoadingOrder) do
			local core = server[name]

			if core then
				if type(core) == "table" or (type(core) == "userdata" and getmetatable(core) == "ReadOnly_Table") then
					if core.RunLast then
						table.insert(runLast, core.RunLast);
						core.RunLast = nil;
					end

					if core.RunAfterInit then
						table.insert(runAfterInit, core.RunAfterInit);
						core.RunAfterInit = nil;
					end

					if core.RunAfterPlugins then
						table.insert(runAfterPlugins, core.RunAfterPlugins);
						core.RunAfterPlugins = nil;
					end

					if core.Init then
						core.Init(data);
						core.Init = nil;
					end
				end
			end
		end

		--// Variables that rely on core modules being initialized
		server.Logs.Errors = ErrorLogs

		--// Load any afterinit functions from modules (init steps that require other modules to have finished loading)
		for i,f in pairs(runAfterInit) do
			f(data);
		end

		--// Load Plugins
		for index,plugin in ipairs(server.PluginsFolder:GetChildren()) do
			LoadModule(plugin, false, {script = plugin}, true); --noenv
		end

		for index,plugin in pairs(data.ServerPlugins or {}) do
			LoadModule(plugin, false, {script = plugin});
		end

		--// We need to do some stuff *after* plugins are loaded (in case we need to be able to account for stuff they may have changed before doing something, such as determining the max length of remote commands)
		for i,f in pairs(runAfterPlugins) do
			f(data);
		end

		--// Below can be used to determine when all modules and plugins have finished loading; service.Events.AllModulesLoaded:Connect(function() doSomething end)
		server.AllModulesLoaded = true;
		service.Events.AllModulesLoaded:Fire(os.time());

		--// Queue handler
		--service.StartLoop("QueueHandler","Heartbeat",service.ProcessQueue)

		--// Stuff to run after absolutely everything else has had a chance to run and initialize and all that
		for i,f in pairs(runLast) do
			f(data);
		end

		if data.Loader then
			warn("Loading Complete; Required by "..tostring(data.Loader:GetFullName()))
		else
			warn("Loading Complete; No loader location provided")
		end

		if server.Logs then
			server.Logs.AddLog(server.Logs.Script, {
				Text = "Finished Loading";
				Desc = "Adonis finished loading";
			})
		else
			warn("SERVER.LOGS TABLE IS MISSING. THIS SHOULDN'T HAPPEN! SOMETHING WENT WRONG WHILE LOADING CORE MODULES(?)");
		end
		service.Events.ServerInitialized:Fire();

		return "SUCCESS"
	end;
	__tostring = function()
		return "Adonis"
	end;
	__metatable = "Adonis";
})
