Advertisement
Jezilas

Lua C Emulator

Jan 27th, 2019
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 17.84 KB | None | 0 0
  1. --"Lua C" emulator in Lua by 3dsboy08
  2. --Credits to Josh for his amazing Lua API wrapper (https://github.com/NewVoids/LuaAPI/)
  3.  
  4. local unpack = unpack or table.unpack
  5. local cache = {}
  6. local memory, address = {states = {}, threads = {}}, 0x0000001
  7. local types = {
  8.    LUA_TNONE = -1,
  9.    LUA_TNIL = 0,
  10.    LUA_TBOOLEAN = 1,
  11.    LUA_TLIGHTUSERDATA = 2,
  12.    LUA_TNUMBER = 3,
  13.    LUA_TSTRING = 4,
  14.    LUA_TTABLE = 5,
  15.    LUA_TFUNCTION = 6,
  16.    LUA_TUSERDATA = 7,
  17.    LUA_TTHREAD = 8
  18. }
  19. local gcactions = {
  20.    'LUA_GCSTOP',
  21.    'LUA_GCRESTART',
  22.    'LUA_GCCOLLECT',
  23.    'LUA_GCCOUNT',
  24.    'LUA_GCCOUNTB',
  25.    'LUA_GCSTEP',
  26.    'LUA_GCSETPAUSE',
  27.    'LUA_GCSETSTEPMUL'
  28. }
  29. local sign = function(n)
  30.    return n < 0 and -1 or n > 0 and 1 or 0
  31. end
  32. local gettype = function(...)
  33.    if select('#', ...) == 0 then
  34.       return types.LUA_TNONE
  35.    elseif type(...) == 'nil' then
  36.       return types.LUA_TNIL
  37.    elseif type(...) == 'boolean' then
  38.       return types.LUA_TBOOLEAN
  39.    elseif type(...) == 'number' then
  40.       return types.LUA_TNUMBER
  41.    elseif type(...) == 'string' then
  42.       return types.LUA_TSTRING
  43.    elseif type(...) == 'table' then
  44.       return types.LUA_TTABLE
  45.    elseif type(...) == 'function' then
  46.       return types.LUA_TFUNCTION
  47.    elseif type(...) == 'userdata' and #... ~= 'fthread' then
  48.       return types.LUA_TUSERDATA
  49.    elseif type(...) == 'thread' or #... == 'fthread' then
  50.       return types.LUA_TTHREAD
  51.    end
  52.    return types.LUA_TLIGHTUSERDATA
  53. end
  54. local push = function(L, v)
  55.    L.stacksize = L.stacksize + 1
  56.    L.stack[L.stacksize] = v
  57. end
  58. local getaddress = function()
  59.    address = address + 0x01
  60.    return string.format('0x%07x', address)
  61. end
  62. local stackKey = function(L, i)
  63.    return (sign(i) == -1 and (L.stacksize + 1) + i) or i
  64. end
  65. local get = function(L, i)
  66.    return L.stack[(sign(i) == -1 and (L.stacksize + 1) + i) or i]
  67. end
  68. local set = function(L, i, v)
  69.    L.stack[(sign(i) == -1 and (L.stacksize + 1) + i) or i] = v
  70. end
  71. local shift = function(L, s, n)
  72.    if sign(s) == -1 then
  73.       for i = ((sign(n) == -1 and (L.stacksize + 1) + n) or n) + 1, L.stacksize do
  74.          L.stack[i - 1] = L.stack[i]
  75.       end
  76.       set(L,  L.stacksize, nil)
  77.       L.stacksize = L.stacksize - 1
  78.    else
  79.       local new = {}
  80.       for i = (sign(n) == -1 and L.stacksize - n or n), L.stacksize do
  81.          new[i + 1] = L.stack[i]
  82.       end
  83.       for k, v in next, new do
  84.          L.stack[k] = v
  85.       end
  86.       set(L, n, nil)
  87.       L.stacksize = L.stacksize + 1
  88.    end
  89. end
  90. local pop = function(L, i)
  91.    local val = get(L, i)
  92.    set(L, i, nil)
  93.    shift(L, -1, i)
  94.    return val
  95. end
  96. local lua_functions = {
  97.    LUA_MULTRET = -math.huge,
  98.    luaL_newstate = function()
  99.       local state = newproxy(true)
  100.       getmetatable(state).__tostring = function() return 'state: ' .. memory[state] end
  101.       getmetatable(state).__index = function(...) error('Lua state libraries not open (luaL_openlibs(L))', 0) end
  102.       memory[state] = state
  103.       return state
  104.    end,
  105.    luaL_openlibs = function(L)
  106.       getmetatable(L).__index = {stack = {}, stacksize = 0, memory = {}}
  107.       getmetatable(L).__newindex = getmetatable(L).__index
  108.       return L
  109.    end,
  110.    luaL_loadstring = function(L, s)
  111.       return loadstring(s)
  112.    end,
  113.    lua_close = function(L)
  114.       memory[L] = nil
  115.    end,
  116.    lua_getglobal = function(L, g)
  117.       push(L, getfenv(0)[g] or cache[g])
  118.    end,
  119.    lua_setglobal = function(L, n)
  120.       cache[n] = get(L, -1)
  121.    end,
  122.    lua_getfenv = function(L, n)
  123.       push(L, setmetatable(cache, {__index = getfenv(n)}))
  124.    end,
  125.    lua_setfenv = function(L, n)
  126.       cache = {}
  127.       local instr = pop(L, -1)
  128.       setfenv(n, instr)
  129.       push(L, (type(instr) ~= 'function' and type(instr) ~= 'thread' and type(instr) ~= 'userdata' and 0) or 1)
  130.    end,
  131.    lua_getfield = function(L, n, k)
  132.       push(L, get(L, n)[k])
  133.    end,
  134.    lua_gettable = function(L, i)
  135.       push(L, get(L, i)[pop(L, -1)])
  136.    end,
  137.    lua_getmetatable = function(L, n)
  138.       if getmetatable(get(L, n)) and get(L, n) then
  139.          push(L, getmetatable(get(L, n)))
  140.       else
  141.          return 0
  142.       end
  143.    end,
  144.    lua_setmetatable = function(L, n)
  145.       set(L, n, setmetatable(get(L, n), pop(L, -1)))
  146.    end,
  147.    lua_createtable = function(L, n)
  148.       push(L, {})
  149.    end,
  150.    lua_settable = function(L, n)
  151.       get(L, n)[pop(L, -2)] = pop(L, -1)
  152.    end,
  153.    lua_settop = function(L, n)
  154.       if stackKey(L, n) < L.stacksize then
  155.          for i = stackKey(L, n) + 1, L.stacksize do
  156.             shift(L, -1, -1)
  157.          end
  158.       else
  159.          for i = L.stacksize, stackKey(L, n) do
  160.             push(L, nil)
  161.          end
  162.       end
  163.    end,
  164.    lua_pop = function(L, n)
  165.       for i = 1, n do
  166.          shift(L, -1, -1)
  167.       end
  168.    end,
  169.    lua_setfield = function(L, n, k)
  170.       get(L, n)[k] = pop(L, -1)
  171.    end,
  172.    lua_gettop = function(L)
  173.       return L.stacksize
  174.    end,
  175.    lua_toboolean = function(L, n)
  176.       return get(L, n) and 1 or 0
  177.    end,
  178.    lua_tointeger = function(L, n)
  179.       return tonumber(get(L, n))
  180.    end,
  181.    lua_tonumber = function(L, n)
  182.       return tonumber(get(L, n))
  183.    end,
  184.    lua_tostring = function(L, n)
  185.       return tostring(get(L, n))
  186.    end,
  187.    lua_tolstring = function(L, n, len)
  188.       return len and string.sub(get(L, n), 1, len) or tostring(get(L, n))
  189.    end,
  190.    lua_iscfunction = function(L, n)
  191.       return pcall(string.dump, get(L, n)) and 1 or 0
  192.    end,
  193.    lua_isfunction = function(L, n)
  194.       return L, type(get(L, n)) == 'function' and 1 or 0
  195.    end,
  196.    lua_isnil = function(L, n)
  197.       return get(L, n) == nil and 1 or 0
  198.    end,
  199.    lua_isnoneornil = function(L, n)
  200.       return get(L, n) == nil and 1 or 0
  201.    end,
  202.    lua_isthread = function(L, n)
  203.       return type(get(L, n)) == 'thread' and 1 or 0
  204.    end,
  205.    lua_istable = function(L, n)
  206.       return type(get(L, n)) == 'table' and 1 or 0
  207.    end,
  208.    lua_isuserdata = function(L, n)
  209.       return type(get(L, n)) == 'userdata' and 1 or 0
  210.    end,
  211.    lua_isstring = function(L, n)
  212.       return type(get(L, n)) == 'string' and 1 or 0
  213.    end,
  214.    lua_isnumber = function(L, n)
  215.       return type(get(L, n)) == 'number' and 1 or 0
  216.    end,
  217.    lua_isboolean = function(L, n)
  218.       return type(get(L, n)) == 'boolean' and 1 or 0
  219.    end,
  220.    lua_lessthan = function(L, n1, n2)
  221.       return get(L, n1) < get(L, n2) and 1 or 0
  222.    end,
  223.    lua_equal = function(L, n1, n2)
  224.       return get(L, n1) == get(L, n2) and 1 or 0
  225.    end,
  226.    lua_rawequal = function(L, n1, n2)
  227.       return rawequal(n1, n2) and 1 or 0
  228.    end,
  229.    lua_rawgeti = function(L, i, n)
  230.       return rawget(get(L, i), n)
  231.    end,
  232.    lua_rawseti = function(L, i, n)
  233.       return rawset(get(L, i)), n, pop(L, -1)
  234.    end,
  235.    lua_gettop = function(L, i)
  236.       return L.stacksize
  237.    end,
  238.    lua_next = function(L, i)
  239.       local res = next(get(L, -1))
  240.       push(L, res == nil and types.LUA_TNIL or res, i)
  241.    end,
  242.    lua_type = function(L, ...)
  243.       return gettype(...)
  244.    end,
  245.    lua_typename = function(L, n)
  246.       local tvalue = gettype(get(L, n))
  247.       for typename, value in next, types do
  248.          if tvalue == value then
  249.             return typename
  250.          end
  251.       end
  252.       return 'LUA_TNONE'
  253.    end,
  254.    lua_newthread = function(L, nresults)
  255.       local thread = newproxy(true)
  256.       local cache = {}
  257.       local f = {}
  258.       memory[L][thread] = getaddress()
  259.       getmetatable(thread).__tostring = function() return 'fthread: ' .. memory[L][thread] end
  260.       getmetatable(thread).__index = cache
  261.       getmetatable(thread).__len = function(self) return 'fthread' end
  262.       getmetatable(thread).__call = function(self, c)
  263.          if not cache[c] then
  264.             local rthread = coroutine.create(c)
  265.             cache[rthread] = getaddress()
  266.             cache[c] = rthread
  267.             return rthread
  268.          else
  269.             return cache[c]
  270.          end
  271.       end
  272.       push(L, thread)
  273.    end,
  274.    lua_yield = function(L, nresults)
  275.       return select(coroutine.yield(), 1, nresults)
  276.    end,
  277.    lua_resume = function(L, narg)
  278.       local f = L.stack[L.stacksize - narg]
  279.       local t = L.stack[L.stacksize - narg - 1]
  280.       local args, n = {}, 0
  281.       for i = (L.stacksize + 1) - narg, L.stacksize do
  282.          n = n + 1
  283.          args[n] = L.stack[i]
  284.       end
  285.       for i = (L.stacksize + 1) - narg, L.stacksize do
  286.          shift(L, -1, i)
  287.       end
  288.       local val = {pcall(coroutine.resume, t(f), unpack(args, 1, n))}
  289.       local s, m = val[1], {select(2, unpack(val))}
  290.       shift(L, -1, -1)
  291.       shift(L, -1, -1)
  292.       push(L, select(2, unpack(val)))
  293.    end,
  294.    lua_pushboolean = function(L, b)
  295.       assert(type(b) == 'boolean', 'Argument type "' .. type(b) .. '" is incompatible with parameter of type boolean')
  296.       push(L, b)
  297.    end,
  298.    lua_pushnil = function(L)
  299.       push(L, nil)
  300.    end,
  301.    lua_pushnumber = function(L, n)
  302.       assert(type(n) == 'number', 'Argument type "' .. type(n) .. '" is incompatible with parameter of type number')
  303.       push(L, n)
  304.    end,
  305.    lua_pushstring = function(L, s)
  306.       assert(type(s) == 'string', 'Argument type "' .. type(s) .. '" is incompatible with parameter of type string')
  307.       push(L, s)
  308.    end,
  309.    lua_pushtable = function(L, t)
  310.       assert(type(t) == 'table', 'Argument type "' .. type(t) .. '" is incompatible with parameter of type table')
  311.       push(L, t)
  312.    end,
  313.    lua_pushvalue = function(L, n)
  314.       push(L, get(L, n))
  315.    end,
  316.    lua_pushclosure = function(L, c)
  317.       assert(type(c) == 'function', 'Argument type "' .. type(c) .. '" is incompatible with parameter of type function')
  318.       push(L, c)
  319.    end,
  320.    lua_remove = function(L, n)
  321.       pop(L, n)
  322.       shift(L, -1, n)
  323.    end,
  324.    lua_insert = function(L, n)
  325.       local element = get(L, -1)
  326.       shift(L, 1, n)
  327.       set(L, n, element)
  328.    end,
  329.    lua_pcall = function(L, nargs, nresults, errfunc)
  330.       local f = L.stack[L.stacksize - nargs]
  331.       local args, n = {}, 0
  332.       for i = (L.stacksize + 1) - nargs, L.stacksize do
  333.          n = n + 1
  334.          args[n] = L.stack[i]
  335.       end
  336.       for i = (L.stacksize + 1) - nargs, L.stacksize do
  337.          shift(L, -1, i)
  338.       end
  339.       local val = {pcall(f, unpack(args, 1, n))}
  340.       local s, m = val[1], {select(2, unpack(val))}
  341.       local r = {unpack(m, 1, nresults == -math.huge and #m or nresults)}
  342.       shift(L, -1, -1)
  343.       if not s and errfunc ~= 0 then
  344.          push(L, get(L, errfunc)(m))
  345.       else
  346.          push(L, select(2, unpack(val)))
  347.       end
  348.    end,
  349.    lua_call = function(L, nargs, nresults)
  350.       local f = L.stack[L.stacksize - nargs]
  351.       assert(type(f) == 'function', 'Unprotected error in call to Lua API (attempt to call a ' .. type(f) .. ' value)')
  352.       local args, n = {}, 0
  353.       for i = (L.stacksize + 1) - nargs, L.stacksize do
  354.          n = n + 1
  355.          args[n] = L.stack[i]
  356.       end
  357.       for i = (L.stacksize + 1) - nargs, L.stacksize do
  358.          shift(L, -1, i)
  359.       end
  360.       local val = {f(unpack(args, 1, n))}
  361.       local r = {unpack(val, 1, nresults == -math.huge and #val or nresults)}
  362.       shift(L, -1, -1)
  363.       push(L, unpack(r))
  364.    end,
  365.    emptystack = function(L)
  366.       L.stack = {}
  367.       L.stacksize = 0
  368.    end
  369. }
  370.  
  371. local function emu(scr)
  372.    local L = luaL_newstate()
  373.    luaL_openlibs(L)
  374.  
  375.    local function reconstruct_string(t, idx)
  376.     local ret = ""
  377.     for i=idx,#t do
  378.         ret = ret .. t[i] .. " "
  379.     end
  380.     return ret:sub(1, -2)
  381.    end
  382.  
  383.    local function convert_number(str, pc)
  384.     local res = tonumber(str)
  385.     assert(type(res) == "number", "invalid number (pc = " .. tostring(pc) .. ")")
  386.     return res
  387.    end
  388.  
  389.    local pc = 1
  390.    for line in scr:gmatch("([^\n]*)\n?") do
  391.     local args = {}
  392.     for arg in string.gmatch(line, "%S+") do table.insert(args, arg) end
  393.     if #args >= 1 then
  394.         if args[1] == "getglobal" then
  395.             assert(#args >= 2, "invalid amount of arguments (getglobal, pc = " .. tostring(pc) .. ")")
  396.             lua_getglobal(L, reconstruct_string(args, 2))
  397.         elseif args[1] == "getfield" then
  398.             assert(#args >= 3, "invalid amount of arguments (getfield, pc = " .. tostring(pc) .. ")")
  399.             lua_getfield(L, convert_number(args[2], pc), reconstruct_string(args, 3))
  400.         elseif args[1] == "setfield" then
  401.             assert(#args >= 3, "invalid amount of arguments (setfield, pc = " .. tostring(pc) .. ")")
  402.             lua_setfield(L, convert_number(args[2], pc), reconstruct_string(args, 3))
  403.         elseif args[1] == "pushvalue" then
  404.             assert(#args == 2, "invalid amount of arguments (pushvalue, pc = " .. tostring(pc) .. ")")
  405.             lua_pushvalue(L, convert_number(args[2], pc))
  406.         elseif args[1] == "pcall" then
  407.             assert(#args == 4, "invalid amount of arguments (pcall, pc = " .. tostring(pc) .. ")")
  408.             lua_pcall(L, convert_number(args[2], pc), convert_number(args[3], pc), convert_number(args[4], pc))
  409.         elseif args[1] == "call" then
  410.             assert(#args == 3, "invalid amount of arguments (call, pc = " .. tostring(pc) .. ")")
  411.             lua_pcall(L, convert_number(args[2], pc), convert_number(args[3], pc))
  412.         elseif args[1] == "pushnumber" then
  413.             assert(#args == 2, "invalid amount of arguments (pushnumber, pc = " .. tostring(pc) .. ")")
  414.             lua_pushnumber(L, convert_number(args[2], pc))
  415.         elseif args[1] == "pushboolean" or args[1] == "pushbool" then
  416.             assert(#args == 2, "invalid amount of arguments (pushboolean, pc = " .. tostring(pc) .. ")")
  417.             if args[2] == "true" then
  418.                 lua_pushboolean(L, true)
  419.             elseif args[2] == "false" then
  420.                 lua_pushboolean(L, false)
  421.             else
  422.                 error("invalid boolean, pc = " .. tostring(pc))
  423.             end
  424.         elseif args[1] == "pushnil" then
  425.             lua_pushnil(L)
  426.         elseif args[1] == "pushstring" then
  427.             assert(#args >= 2, "invalid amount of arguments (pushstring, pc = " .. tostring(pc) .. ")")
  428.             lua_pushstring(L, reconstruct_string(args, 2))
  429.         elseif args[1] == "settop" then
  430.             assert(#args == 2, "invalid amount of arguments (settop, pc = " .. tostring(pc) .. ")")
  431.             lua_settop(L, convert_number(args[2], pc))
  432.         elseif args[1] == "remove" then
  433.             assert(#args == 2, "invalid amount of arguments (remove, pc = " .. tostring(pc) .. ")")
  434.             lua_remove(L, convert_number(args[2], pc))
  435.         elseif args[1] == "pop" then
  436.             assert(#args == 2, "invalid amount of arguments (pop, pc = " .. tostring(pc) .. ")")
  437.             lua_pop(L, convert_number(args[2], pc))
  438.         elseif args[1] == "emptystack" then
  439.             emptystack(L)
  440.         end
  441.         pc = pc + 1
  442.     end
  443.    end
  444.  
  445.    lua_close(L)
  446. end
  447.  
  448. local femu = setfenv(emu, setmetatable(lua_functions, {__index = getfenv(1)}))
  449.  
  450. local function create_gui()
  451.     local luac_emu = Instance.new("ScreenGui")
  452.     local Frame = Instance.new("Frame")
  453.     local TopLabel = Instance.new("TextLabel")
  454.     local ScriptBox = Instance.new("TextBox")
  455.     local ExecuteButton = Instance.new("TextButton")
  456.    
  457.     luac_emu.Name = "luac_emu"
  458.     luac_emu.Parent = game:GetService("CoreGui")
  459.    
  460.     Frame.Parent = luac_emu
  461.     Frame.Active = true
  462.     Frame.BackgroundColor3 = Color3.new(0.176471, 0.176471, 0.176471)
  463.     Frame.BorderColor3 = Color3.new(0.176471, 0.176471, 0.176471)
  464.     Frame.Draggable = true
  465.     Frame.Position = UDim2.new(0, 400, 0, 230)
  466.     Frame.Size = UDim2.new(0, 440, 0, 330)
  467.    
  468.     TopLabel.Name = "TopLabel"
  469.     TopLabel.Parent = Frame
  470.     TopLabel.Active = true
  471.     TopLabel.BackgroundColor3 = Color3.new(0.109804, 0.109804, 0.109804)
  472.     TopLabel.BorderColor3 = Color3.new(0.109804, 0.109804, 0.109804)
  473.     TopLabel.Size = UDim2.new(0, 440, 0, 40)
  474.     TopLabel.Font = Enum.Font.SourceSansSemibold
  475.     TopLabel.Text = "Lua C Emulator"
  476.     TopLabel.TextColor3 = Color3.new(1, 1, 1)
  477.     TopLabel.TextSize = 16
  478.    
  479.     ScriptBox.Name = "ScriptBox"
  480.     ScriptBox.Parent = Frame
  481.     ScriptBox.BackgroundColor3 = Color3.new(0.109804, 0.109804, 0.109804)
  482.     ScriptBox.BorderColor3 = Color3.new(0.109804, 0.109804, 0.109804)
  483.     ScriptBox.Position = UDim2.new(0, 10, 0, 50)
  484.     ScriptBox.Size = UDim2.new(0, 420, 0, 220)
  485.     ScriptBox.Font = Enum.Font.SourceSansSemibold
  486.     ScriptBox.Text = ""
  487.     ScriptBox.TextColor3 = Color3.new(1, 1, 1)
  488.     ScriptBox.TextSize = 16
  489.     ScriptBox.TextXAlignment = Enum.TextXAlignment.Left
  490.     ScriptBox.TextYAlignment = Enum.TextYAlignment.Top
  491.    
  492.     ExecuteButton.Name = "ExecuteButton"
  493.     ExecuteButton.Parent = Frame
  494.     ExecuteButton.BackgroundColor3 = Color3.new(0.109804, 0.109804, 0.109804)
  495.     ExecuteButton.BorderColor3 = Color3.new(0.109804, 0.109804, 0.109804)
  496.     ExecuteButton.Position = UDim2.new(0, 10, 0, 280)
  497.     ExecuteButton.Size = UDim2.new(0, 420, 0, 40)
  498.     ExecuteButton.Font = Enum.Font.SourceSansBold
  499.     ExecuteButton.Text = "Execute"
  500.     ExecuteButton.TextColor3 = Color3.new(1, 1, 1)
  501.     ExecuteButton.TextSize = 16
  502.  
  503.     return Frame
  504. end
  505.  
  506. local GUI = create_gui()
  507.  
  508. GUI.ExecuteButton.MouseButton1Click:connect(function()
  509.     if GUI.ScriptBox.Text ~= "" then
  510.         local check, message = pcall(function() femu(GUI.ScriptBox.Text) end)
  511.         if check then
  512.             GUI.ExecuteButton.Text = "Executed!"
  513.             wait(3)
  514.             GUI.ExecuteButton.Text = "Execute"
  515.         else
  516.             GUI.ExecuteButton.Text = "Failed to execute. Check devconsole."
  517.             spawn(function()
  518.                 error(message)
  519.             end)
  520.             wait(3)
  521.             GUI.ExecuteButton.Text = "Execute"
  522.         end
  523.     end
  524. end)
  525.  
  526. local function gui_toggle(_, state, __)
  527.     if state == Enum.UserInputState.Begin then
  528.         if not GUI.Visible then GUI.Visible = true else GUI.Visible = false end
  529.     end
  530. end
  531.  
  532. game.ContextActionService:BindAction("luac_exec_o", gui_toggle, false, Enum.KeyCode.O)
  533. --this is for synapse gui ;)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement