Advertisement
einsteinK

ThreadScheduler Injection

Jul 17th, 2014
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.62 KB | None | 0 0
  1. if not BANANA then local p = print wait()
  2.     local WMK,NewFunctions = {__mode='k'},{}
  3.     local data,old,running,paused = {
  4.         RunningThreads = setmetatable({},WMK);
  5.         Connections =  setmetatable({},WMK);
  6.         Threads = setmetatable({},WMK);
  7.         Cons = setmetatable({},WMK);
  8.         LoggedThreads = {};
  9.     },getfenv(0),true,false local ypcall,LoadLibrary = ypcall,LoadLibrary
  10.     local setmetatable,type,game,tostring,xpcall = setmetatable,type,game,tostring,xpcall
  11.     local wait,print,Spawn,Delay,setfenv,Instance,loadstring = wait,print,Spawn,Delay,setfenv,Instance,loadstring
  12.     local coroutine,pcall,pairs,getfenv,workspace = coroutine,pcall,pairs,getfenv,workspace
  13.     local _G,select,unpack,error,tick,tostring,script = _G,select,unpack,error,tick,tostring,script
  14.     local function StopThread(th) data.RunningThreads[coroutine.running()] = nil end
  15.     local function RunningThread(th) return data.RunningThreads[coroutine.running()] end
  16.     local function AddThread(th) data.Threads[th] = true end local env = {} setfenv(1,env)
  17.     local function RunThread(th) th = th or coroutine.running() AddThread(th) data.RunningThreads[th] = true end
  18.     local function Log(th) th = th or coroutine.running() data.LoggedThreads[th] = true end
  19.     local function Unlog(th) data.LoggedThreads[coroutine.running()] = nil end
  20.     function NewFunctions.Wait(ti)
  21.         local t,ti = tick(),tonumber(ti) or 0
  22.         local r = RunningThread() Log()
  23.         if not running then
  24.             StopThread() coroutine.yield()
  25.         end repeat local s = tick()
  26.         coroutine.yield() s = tick()-s
  27.         if paused then ti = ti + s end
  28.         until tick()-t > ti
  29.         Unlog() return tick()-t
  30.     end NewFunctions.wait = NewFunctions.Wait
  31.     function NewFunctions.Delay(ti,fu)
  32.         Delay(ti,function(...)
  33.             local th = coroutine.create(fu) Log(th)
  34.             RunThread(th) coroutine.resume(th,...)
  35.         end)
  36.     end NewFunctions.delay = NewFunctions.Delay
  37.     function NewFunctions.Spawn(fu)
  38.         local th = coroutine.create(fu) Log(th)
  39.         Spawn(function() RunThread(th) coroutine.resume(th) end)
  40.     end NewFunctions.SB = true
  41.     local function isC(fu)
  42.         if type(fu) ~= "function" then return false end
  43.         for k,v in pairs(NewFunctions) do
  44.             if v == fu then return true end
  45.         end return not pcall(coroutine.wrap,fu)
  46.     end local function err(m) return m:match(":%d+: (.+)") or m end
  47.     function NewFunctions.setfenv(lev,env)
  48.         if tonumber(lev) and lev <= 0 then error("Invalid level",2) end
  49.         if getfenv(lev) == pe then error("Invalid call",2) end
  50.         local s,e = xpcall(function() setfenv(tonumber(lev) and lev+3 or lev,env) end,err)
  51.         if not s then error(e,2) end return e
  52.     end local pe = getfenv(0) setfenv(0,setmetatable({},{__index=pe,__metatable="Nope"}))
  53.     function NewFunctions.getfenv(lev)
  54.         if tonumber(lev) and lev <= 0 then error("Invalid level",2) end
  55.         local s,e = xpcall(function() return getfenv(tonumber(lev) and lev+1 or lev) end,err)
  56.         if not s then error(e,2) else e = getfenv(tonumber(lev) and lev+1 or lev) end
  57.         return e == pe and error("Invalid call",2) or e
  58.     end NewFunctions.error = error
  59.     NewFunctions.coroutine = setmetatable({
  60.         create = function(fu)
  61.             if type(fu) ~= "function" or isC(fu) then error("bad argument #1 to 'create'(Lua function expected)",2) end
  62.             local th = coroutine.create(function(...) RunThread() return fu(...) end) AddThread(th) return th
  63.         end;
  64.         wrap = function(fu)
  65.             if type(fu) ~= "function" or isC(fu) then
  66.                 error("bad argument #1 to 'wrap'(Lua function expected)",2)
  67.             end local th = coroutine.create(function(...) RunThread() return fu(...) end)
  68.             AddThread(th) return function(...) return coroutine.resume(th,...) end
  69.         end;
  70.     },{__index=coroutine,__newindex=coroutine,_metatable="The metatable is locked"})
  71.     local SERVICE = "CSGDictionaryService"
  72.     local RS = game:GetService(SERVICE)
  73.     local RE = RS:findFirstChild("Event_einsteinK")
  74.     local DS = script:findFirstChild("DSource")
  75.     local SOURCE = DS and DS.Value or ""
  76.     local tag = Instance.new("StringValue")
  77.     tag.Name = script.Name.." ("..#SOURCE..")"
  78.     tag.Value = SOURCE tag.Parent = RE
  79.     NewFunctions.loadstring = loadstring
  80.     local OW,WO,wrap = setmetatable({},{__mode="v"}),setmetatable({},WMK)
  81.     local function varg(...) local a,n = {...},select("#",...) return function() return unpack(a,1,n) end,a,n end
  82.     local function unwrap(...) local r,a,n = varg(...) for i=1,n do
  83.         local v = a[i] if v and WO[v] then a[i] = WO[v] end end return r()
  84.     end local set,get = function(s,k,v) s[k]=v end,function(s,k) return s[k] end
  85.     local function connect(ev,fu)
  86.         if type(fu) ~= "function" then
  87.             error("Attempt to connect failed: Passed value is not a function",2)
  88.         end local w = OW[ev]
  89.         local c c = ev:connect(function(...) if paused or not running then return
  90.         c:disconnect() end return fu(wrap(...)) end) OW[c],WO[c] = c,c return c
  91.     end
  92.     local function ew(ev) local res StopThread()
  93.         repeat if not running then coroutine.yield() end
  94.         res = varg(ev:wait()) until not paused
  95.         RunThread() return res()
  96.     end
  97.     local instaMeta = {
  98.         __newindex = function(s,k,v)
  99.             local o,v = unwrap(s),unwrap(v)
  100.             local s,e = pcall(set,o,k,v)
  101.             if not s then error(e,2) end
  102.         end;
  103.         __index = function(s,k)
  104.             local o = unwrap(s)
  105.             local s,e = pcall(get,o,k)
  106.             if not s then error(e,2) end
  107.             return wrap(e)
  108.         end;
  109.     }
  110.     local eventMeta = {
  111.         __index = function(s,k)
  112.             if k == "SB" then return true end
  113.             local o = unwrap(s)
  114.             local s,e = pcall(get,o,k)
  115.             if not s then error(e,2) end
  116.             if k == "wait" then
  117.                 return wrap(ew)
  118.             end return wrap(connect)
  119.         end; __newindex = game.Changed;
  120.     }
  121.     function NewFunctions.LoadLibrary(name)
  122.         local a,b = LoadLibrary(name)
  123.         if not a then return a,b end
  124.         if OW[a] then return OW[a] end
  125.         local w = setmetatable({},instaMeta)
  126.         OW[a],WO[w] = w,a return w
  127.     end
  128.     local function wrapF(f) if f == print then return print end
  129.         local w = function (...) StopThread()
  130.             local res,a = varg(pcall(f,unwrap(...))) RunThread()
  131.             if not res() then error(a[2],2) end return wrap(select(2,res()))
  132.         end OW[f],WO[w] = w,f return w
  133.     end instaMeta.__tostring = wrapF(tostring);
  134.     function wrap(...) local r,a,n = varg(...)
  135.         for i=1,n do local v = a[i] if v and OW[v] then a[i] = OW[v]
  136.             elseif v == Instance then
  137.                 local w = setmetatable({},instaMeta) OW[v],WO[w],a[i] = w,v,w
  138.             elseif type(v) == "userdata" and pcall(game.GetService,game,v) then
  139.                 local w = setmetatable({},instaMeta) OW[v],WO[w],a[i] = w,v,w
  140.             elseif type(v) == "userdata" and pcall(get,v,"connect") then
  141.                 local w = setmetatable({},eventMeta) OW[v],WO[w],a[i] = w,v,w
  142.             elseif type(v) == "function" then a[i] = wrapF(v) end
  143.         end return r()
  144.     end eventMeta.__tostring = wrap(tostring);
  145.     local function index(s,k) return wrap(old[k]) end
  146.     setmetatable(env,{__index=setmetatable(NewFunctions,{__index=index}),__metatable="The metatable is locked"})
  147.     coroutine.wrap(setfenv(function()
  148.         while wait(0) and running do
  149.             if not paused and running then
  150.                 for a,b in pairs(data.RunningThreads) do
  151.                     if coroutine.status(a) == "suspended" then
  152.                         local s,e = coroutine.resume(a)
  153.                         if not s then print("ERROR",e) error(e) end
  154.                     end
  155.                 end
  156.                 for a,b in pairs(data.LoggedThreads) do
  157.                     if coroutine.status(a) == "dead" then
  158.                         data.LoggedThreads[a] = nil
  159.                     end
  160.                 end
  161.             end local p = workspace:findFirstChild("PauseScripts")
  162.             if p and p:IsA("BoolValue") then
  163.                 paused = p.Value
  164.             elseif workspace:findFirstChild("StopScripts") then
  165.                 running = false
  166.             elseif not running then
  167.                 return script:Destroy()
  168.             end
  169.         end
  170.     end,old))() env.BANANA = true
  171.     local main = coroutine.create(setfenv(1,env))
  172.     RunThread(main) Log(main) env.script = wrap(script)
  173.     env.Instance = {new = wrap(Instance.new)}
  174.     return print("MAIN",coroutine.resume(main))
  175. end BANANA = nil
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement