Advertisement
killer64

Untitled

Dec 12th, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.36 KB | None | 0 0
  1. local log = print
  2. local function serializeImpl( t, tTracking )   
  3.     local sType = type(t)
  4.     if sType == "table" then
  5.         if tTracking[t] ~= nil then
  6.             error( "Cannot serialize table with recursive entries" )
  7.         end
  8.         tTracking[t] = true
  9.        
  10.         local result = "{"
  11.         for k,v in pairs(t) do
  12.             result = result..("["..serializeImpl(k, tTracking).."]="..serializeImpl(v, tTracking)..",")
  13.         end
  14.         result = result.."}"
  15.         return result
  16.        
  17.     elseif sType == "string" then
  18.         return string.format( "%q", t )
  19.    
  20.     elseif sType == "number" or sType == "boolean" or sType == "nil" then
  21.         return tostring(t)
  22.        
  23.     elseif sType == "function" then
  24.         log("Warning: Serializer is giving a dummy function")
  25.         return "function() end"
  26.        
  27.     else
  28.         error( "Cannot serialize type "..sType )
  29.        
  30.     end
  31. end
  32.  
  33. local function serialize( t )
  34.     local tTracking = {}
  35.     return serializeImpl( t, tTracking )
  36. end
  37.  
  38. local function unserialize( s )
  39.     local func, e = loadstring( "return "..s, "serialize" )
  40.     if not func then
  41.         return s
  42.     else
  43.         setfenv( func, {} )
  44.         return func()
  45.     end
  46. end
  47. local function getclientinfo(...)
  48.     local args = {...}
  49.     log("Sending: " .. serialize(args))
  50.     client:send(serialize({...}).."\n")
  51.     local reply,err = client:receive()
  52.     if err then
  53.         log("Error: " .. reply)
  54.     else
  55.         log("Received: " .. reply)
  56.         return unpack(unserialize(reply))
  57.     end
  58. end
  59. log("ccemu starting up")
  60. log("Loading socket")
  61. local socket = require("socket")
  62. log("Listening on port 2840")
  63. local server = assert(socket.bind("*",2840))
  64. log("Waiting for client")
  65. client = server:accept()
  66. log("Client connected")
  67. function print(...) return getclientinfo("print",...) end
  68. function write(...) return getclientinfo("write",...) end
  69. function read(...) return getclientinfo("read",...) end
  70. function os.execute() print("No fuck you") end
  71. print("Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio")
  72. while true do
  73.     write("> ")
  74.     local input = read()
  75.     local funct,err = loadstring(input)
  76.     if type(funct or nil) ~= "function" then
  77.         getclientinfo("term.setTextColor",16384)
  78.         print(err)
  79.         getclientinfo("term.setTextColor",1)
  80.     else
  81.         local data = {pcall(funct)}
  82.         if data[1] == true then
  83.             for i = 2,#data do
  84.                 print(data[i])
  85.             end
  86.         else
  87.             getclientinfo("term.setTextColor",16384)
  88.             print(data[2])
  89.             getclientinfo("term.setTextColor",1)
  90.         end
  91.     end
  92. end
  93. log("Closing client")
  94. client:close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement