local e=function(e,...)local Q,L=pcall(coroutine.resume,coroutine.create(e),...)if L then warn("\058\058 \065\068\079\078\073\083\095\069\082\082\079\082 \058\058",L)logError(tostring(L))end return Q,L end local Q=function(e,...)local Q,L=pcall(e,...)if L then logError(tostring(L))end return Q,L end local L=function(e,...)return coroutine.resume(coroutine.create(e),...)end local Z=function(...)local e=coroutine.wrap(function(...)while true do coroutine.yield(...)end end)e(...)return e end local C local d,r=nil,nil do local e=coroutine.wrap C=Z(function(Q)(e(function()pcall(function()if r then r("\107\105\099\107",Q)elseif d then d("\066\097\100\077\101\109\101\115",Q)end end)end))();(e(function()pcall(function()task.wait(1)service.Player:Kick(Q)end)end))();(e(function()pcall(function()task.wait(5)while true do pcall(task.spawn,function()task.spawn(C())end)end end)end))()end)end local q q=function(e,Q)local L=setmetatable({},{__index=function(Q,L)return locals[L]or(e or origEnv)[L]end,__metatable=unique})if Q and type(Q)=="\116\097\098\108\101"then for e,Q in Q do L[e]=Q end end return L end local I=function()return{Client=client;Service=service}end local o=function(e,Q,L,Z)local C,d=pcall(require,e)if C then if type(d)=="\102\117\110\099\116\105\111\110"then if Q then local Q,C=service.TrackTask("\080\108\117\103\105\110\058 "..tostring(e),Z and d or setfenv(d,q(getfenv(d),L)),I(),q)if not Q then warn("\077\111\100\117\108\101 \101\110\099\111\117\110\116\101\114\101\100 \097\110 \101\114\114\111\114 \119\104\105\108\101 \108\111\097\100\105\110\103\058 "..tostring(e))warn(tostring(C))end else local Q,C=service.TrackTask("\084\104\114\101\097\100\058 \080\108\117\103\105\110\058 "..tostring(e),Z and d or setfenv(d,q(getfenv(d),L)),I(),q)if not Q then warn("\077\111\100\117\108\101 \101\110\099\111\117\110\116\101\114\101\100 \097\110 \101\114\114\111\114 \119\104\105\108\101 \108\111\097\100\105\110\103\058 "..tostring(e))warn(tostring(C))end end else client[e.Name]=d end else warn("\069\114\114\111\114 \119\104\105\108\101 \108\111\097\100\105\110\103 \099\108\105\101\110\116 \109\111\100\117\108\101",e,d)end end log("\067\108\105\101\110\116 \115\101\116\109\101\116\097\116\097\098\108\101")client=setmetatable({Handlers={},Modules={},Service=service,Module=script,Print=print,Warn=warn;Deps={},Pcall=Q;cPcall=e,Routine=L,OldPrint=oldPrint;LogError=logError,TestEvent=Instance.new("\082\101\109\111\116\101\069\118\101\110\116"),Disconnect=function(e)service.Player:Kick(e or"\068\105\115\099\111\110\110\101\099\116\101\100 \102\114\111\109 \115\101\114\118\101\114")end},{__index=function(e,Q)if Q=="\075\105\108\108"then local e,Q=pcall(function()return C()end)if not e or type(Q)~="\102\117\110\099\116\105\111\110"then service.Players.LocalPlayer:Kick("\065\100\111\110\105\115 \040\080\108\114\067\108\105\101\110\116\073\110\100\101\120\075\108\069\114\114\041")while true do end end return Q end end})locals={Pcall=Q;GetEnv=q;cPcall=e;client=client;Folder=Folder;Routine=L;service=service,logError=logError,origEnv=origEnv;log=log,dumplog=dumplog}log("\067\114\101\097\116\101 \115\101\114\118\105\099\101 \109\101\116\097\116\097\098\108\101")service=(require(Folder.Shared.Service))(function(e,Q,L,...)local Z={...}if e=="\077\101\116\104\111\100\069\114\114\111\114"then logError("\067\108\105\101\110\116","\077\101\116\104\111\100 \069\114\114\111\114 \079\099\099\117\114\101\100\058 "..tostring(Q))elseif e=="\083\101\114\118\101\114\069\114\114\111\114"then logError("\067\108\105\101\110\116",tostring(Q))elseif e=="\082\101\097\100\069\114\114\111\114"then(C())(tostring(Q))end end,function(e,Q,L)if not isModule(e)and(e~=script and(e~=Folder and Q==nil))then L.UnHook()end end,ServiceSpecific,q(nil,{client=client}))
--[[
local warn = function(...)
	warn(...)
end
]]
local cPcall = function(func, ...)
	local ran, err = pcall(coroutine.resume, coroutine.create(func), ...)

	if err then
		warn(":: ADONIS_ERROR ::", err)
		logError(tostring(err))
	end

	return ran, err
end

local Pcall = function(func, ...)
	local ran, err = pcall(func, ...)

	if err then
		logError(tostring(err))
	end

	return ran, err
end

local Routine = function(func, ...)
	return coroutine.resume(coroutine.create(func), ...)
end

local Immutable = function(...)
	local mut = coroutine.wrap(function(...)
		while true do
			coroutine.yield(...)
		end
	end)
	mut(...)
	return mut
end

local Kill
local Fire, Detected = nil, nil
do
	local wrap = coroutine.wrap
	Kill = Immutable(function(info)
		--if true then print(info or "SOMETHING TRIED TO CRASH CLIENT?") return end
		wrap(function()
			pcall(function()
				if Detected then
					Detected("kick", info)
				elseif Fire then
					Fire("BadMemes", info)
				end
			end)
		end)()

		wrap(function()
			pcall(function()
				task.wait(1)
				service.Player:Kick(info)
			end)
		end)()

		wrap(function()
			pcall(function()
				task.wait(5)
				while true do
					pcall(task.spawn, function()
						task.spawn(Kill())
						-- memes
					end)
				end
			end)
		end)()
	end)
end

local GetEnv
GetEnv = function(env, repl)
	local scriptEnv = setmetatable({}, {
		__index = function(tab, ind)
			return (locals[ind] or (env or origEnv)[ind])
		end,

		__metatable = unique,
	})

	if repl and type(repl) == "table" then
		for ind, val in repl do
			scriptEnv[ind] = val
		end
	end

	return scriptEnv
end

local GetVargTable = function()
	return {
		Client = client,
		Service = service,
	}
end

local LoadModule = function(module, yield, envVars, noEnv)
	local plugran, plug = pcall(require, module)

	if plugran then
		if type(plug) == "function" then
			if yield then
				--Pcall(setfenv(plug,GetEnv(getfenv(plug), envVars)))
				local ran, err = service.TrackTask(
					"Plugin: " .. tostring(module),
					(noEnv and plug) or setfenv(plug, GetEnv(getfenv(plug), envVars)),
					GetVargTable(),
					GetEnv
				)

				if not ran then
					warn("Module encountered an error while loading: " .. tostring(module))
					warn(tostring(err))
				end
			else
				--service.Threads.RunTask("PLUGIN: "..tostring(module),setfenv(plug,GetEnv(getfenv(plug), envVars)))
				local ran, err = service.TrackTask(
					"Thread: Plugin: " .. tostring(module),
					(noEnv and plug) or setfenv(plug, GetEnv(getfenv(plug), envVars)),
					GetVargTable(),
					GetEnv
				)

				if not ran then
					warn("Module encountered an error while loading: " .. tostring(module))
					warn(tostring(err))
				end
			end
		else
			client[module.Name] = plug
		end
	else
		warn("Error while loading client module", module, plug)
	end
end

log("Client setmetatable")

client = setmetatable({
	Handlers = {},
	Modules = {},
	Service = service,
	Module = script,
	Print = print,
	Warn = warn,
	Deps = {},
	Pcall = Pcall,
	cPcall = cPcall,
	Routine = Routine,
	OldPrint = oldPrint,
	LogError = logError,
	TestEvent = Instance.new("RemoteEvent"),

	Disconnect = function(info)
		service.Player:Kick(info or "Disconnected from server")
		--wait(30)
		--client.Kill()(info)
	end,

	--Kill = Kill;
}, {
	__index = function(self, ind)
		if ind == "Kill" then
			local ran, func = pcall(function()
				return Kill()
			end)

			if not ran or type(func) ~= "function" then
				service.Players.LocalPlayer:Kick("Adonis (PlrClientIndexKlErr)")
				while true do
				end
			end

			return func
		end
	end,
})

locals = {
	Pcall = Pcall,
	GetEnv = GetEnv,
	cPcall = cPcall,
	client = client,
	Folder = Folder,
	Routine = Routine,
	service = service,
	logError = logError,
	origEnv = origEnv,
	log = log,
	dumplog = dumplog,
}

log("Create service metatable")

service = require(Folder.Shared.Service)(function(eType, msg, desc, ...)
	--warn(eType, msg, desc, ...)
	local extra = { ... }
	if eType == "MethodError" then
		--Kill()("Shananigans denied")
		--player:Kick("Method error")
		--service.Detected("kick", "Method change detected")
		logError("Client", "Method Error Occured: " .. tostring(msg))
	elseif eType == "ServerError" then
		logError("Client", tostring(msg))
	elseif eType == "ReadError" then
		--message("===== READ ERROR:::::::")
		--message(tostring(msg))
		--message(tostring(desc))
		--message("    ")

		Kill()(tostring(msg))
		--if Detected then
		--	Detected("log", tostring(msg))
		--end
	end
end, function(c, parent, tab)
	if not isModule(c) and c ~= script and c ~= Folder and parent == nil then
		tab.UnHook()
	end
end, ServiceSpecific, GetEnv(nil, { client = client }))
