Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.31 KB | None | 0 0
  1. local _spook = { }
  2.  
  3. local files = { "Extensions", "Instances", "Modules" }
  4. local extLoaded = false
  5. local vars = require(script:WaitForChild("Vars"))
  6.  
  7. for _, file in pairs, files do
  8.     script:WaitForChild(file)
  9. end
  10.  
  11. function CopyTable(input)
  12.     local result = { }
  13.    
  14.     if type(input) == "table" then
  15.         for index, value in next, input, nil do
  16.             result[CopyTable(index)] = CopyTable(value)
  17.         end
  18.        
  19.         setmetatable(result, CopyTable(getmetatable(input)))
  20.     else
  21.         result = input
  22.     end
  23.    
  24.     return result
  25. end
  26.  
  27. function Wrap(input, data)
  28.     local proxy, data = { }, CopyTable(data)
  29.    
  30.     proxy.__userdata = input
  31.     data.__userdata = input
  32.    
  33.     setmetatable(proxy, {
  34.         __index = function(self, index)
  35.             local member = rawget(data, index)
  36.            
  37.             if member == nil then
  38.                 local success, result = pcall(function() return rawget(data, "__userdata")[index] end)
  39.                
  40.                 if success then
  41.                     if type(result) == "function" then
  42.                         return function(_, ...)
  43.                             return result(rawget(data, "__userdata"), ...)
  44.                         end
  45.                     else
  46.                         return result
  47.                     end
  48.                 end
  49.             else
  50.                 if type(member) == "function" then
  51.                     return function(_, ...)
  52.                         return member(proxy, ...)
  53.                     end
  54.                 else
  55.                     return member
  56.                 end
  57.             end
  58.         end,
  59.         __newindex = function(self, index, value)
  60.             local member = rawget(data, index)
  61.            
  62.             if member == nil then
  63.                 local success, _ = pcall(function() return rawget(proxy, "__userdata") end)
  64.                
  65.                 if success then
  66.                     rawget(proxy, "__userdata")[index] = value
  67.                 end
  68.             else
  69.                 rawset(data, index, value)
  70.             end
  71.         end,
  72.         __metatable = { }
  73.     })
  74.    
  75.     return proxy
  76. end
  77.  
  78. return function()
  79.     local env = getfenv(2)
  80.    
  81.     -- built-in
  82.     do
  83.         _spook.var = function(query)
  84.             local result = vars[query]
  85.            
  86.             if result ~= nil then
  87.                 return result
  88.             else
  89.                 warn("** Failed to load clientvar", query)
  90.             end
  91.         end
  92.        
  93.         _spook.wrap = function(input, data)
  94.             return Wrap(input, data)
  95.         end
  96.     end
  97.    
  98.     -- enums
  99.     do
  100.         local new = { }
  101.        
  102.         for index, value in next, require(script:WaitForChild("Enums")) do
  103.             new[index] = value
  104.         end
  105.        
  106.         setmetatable(new, { __index = Enum })
  107.        
  108.         env["Enum"] = new
  109.     end
  110.    
  111.     -- require
  112.     do
  113.         env["require"] = function(query)
  114.             if type(query) == "string" then
  115.                 return require(script.Modules:WaitForChild(query))
  116.             end
  117.            
  118.             return require(query)
  119.         end
  120.     end
  121.    
  122.     -- instance lib
  123.     do
  124.         env["Instance"] = {
  125.             new = function(class, parent)
  126.                 local result = script.Instances:FindFirstChild(class)
  127.                
  128.                 if result then
  129.                     local data = require(result)
  130.                     local userdata, api = data.__userdata, data.__api
  131.                    
  132.                     if userdata and type(userdata) == "userdata" then
  133.                         local clone = userdata:Clone()
  134.                         clone.Parent = parent
  135.                        
  136.                         return Wrap(clone, api)
  137.                     else
  138.                         return api
  139.                     end
  140.                 else
  141.                     return Instance.new(class, parent)
  142.                 end
  143.             end
  144.         }
  145.     end
  146.    
  147.     -- printing
  148.     do
  149.         env["print"] = function(...)
  150.             if vars.g_print_enabled then
  151.                 if vars.g_printPrefix_enabled then
  152.                     print(env.script.Name, "-", ...)
  153.                 else
  154.                     print(...)
  155.                 end
  156.             end
  157.         end
  158.     end
  159.    
  160.     -- extensions
  161.     do
  162.         if not extLoaded then
  163.             extLoaded = true
  164.            
  165.             for index, value in next, require(script.Extensions) do
  166.                 _spook[index] = value
  167.             end
  168.         end
  169.     end
  170.    
  171.     return _spook
  172. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement