Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Setup of on_process() function taken from the excellent example set by SLMod
- ]] --
- log("Loading Witchcraft into the server environment...")
- witchcraft = witchcraft or {}
- witchcraft.oldfunc = {}
- witchcraft.oldfunc.on_process = server.on_process()
- do
- witchcraft.host = "localhost"
- witchcraft.port = 3001
- local require = require
- local loadfile = loadfile
- package.path = package.path..";.\\LuaSocket\\?.lua"
- package.cpath = package.cpath..";.\\LuaSocket\\?.dll"
- local JSON = loadfile("Scripts\\JSON.lua")()
- witchcraft.JSON = JSON
- local socket = require("socket")
- function witchcraft.step(arg, time)
- witchcraft.txbuf = witchcraft.txbuf .. '{"type":"dummy"}\n'
- if witchcraft.txbuf:len() > 0 then
- local bytes_sent = nil
- local ret1, ret2, ret3 = witchcraft.conn:send(witchcraft.txbuf)
- if ret1 then
- bytes_sent = ret1
- else
- --env.info("could not send witchcraft: "..ret2)
- if ret3 == 0 then
- if ret2 == "closed" then
- witchcraft.txbuf = '{"type":"dummy"}\n'
- witchcraft.rxbuf = ""
- witchcraft.lastUnitUpdateTime = 0
- witchcraft.conn = socket.tcp()
- witchcraft.conn:settimeout(.0001)
- --env.info("witchcraft: socket was closed")
- end
- --env.info("reconnecting to "..tostring(witchcraft.host)..":"..tostring(witchcraft.port))
- witchcraft.conn:connect(witchcraft.host, witchcraft.port)
- return
- end
- bytes_sent = ret3
- end
- witchcraft.txbuf = witchcraft.txbuf:sub(bytes_sent + 1)
- else
- if witchcraft.txidle_hook then
- local bool, err = pcall(witchcraft.txidle_hook)
- if not bool then
- --env.info("witchcraft.txidle_hook() failed: "..err)
- end
- end
- end
- local line, err = witchcraft.conn:receive()
- if err then
- --env.info("witchcraft read error: "..err)
- else
- msg = JSON:decode(line)
- if msg.type == "lua" then
- local response_msg = {}
- response_msg.type = "luaresult"
- response_msg.name = msg.name
- local f, error_msg = loadstring(msg.code, msg.name)
- if f then
- witchcraft.context = {}
- witchcraft.context.arg = msg.arg
- setfenv(f, witchcraft.mission_env)
- response_msg.success, response_msg.result = pcall(f)
- else
- response_msg.success = false
- response_msg.result = tostring(error_msg)
- end
- local response_string = ""
- local function encode_response()
- response_string = JSON:encode(response_msg):gsub("\n","").."\n"
- end
- local success, result = pcall(encode_response)
- if not success then
- response_msg.success = false
- response_msg.result = tostring(result)
- encode_response()
- end
- witchcraft.txbuf = witchcraft.txbuf .. response_string
- end
- end
- end
- witchcraft.start = function(mission_env_)
- witchcraft.mission_env = mission_env_
- if not witchcraft.scheduled then
- witchcraft.lastUnitUpdateTime = 0
- witchcraft.unitUpdateInterval = 0
- witchcraft.txbuf = '{"type":"dummy"}\n'
- witchcraft.rxbuf = ""
- witchcraft.lastUnitUpdateTime = 0
- witchcraft.conn = socket.tcp()
- witchcraft.conn:settimeout(.0001)
- witchcraft.conn:connect(witchcraft.host, witchcraft.port)
- witchcraft.scheduled = true
- end
- end
- witchcraft.log = function(data)
- local msg = { ["type"] = "log", ["data"] = data };
- witchcraft.txbuf = witchcraft.txbuf .. JSON:encode(msg):gsub("\n","").."\n"
- end
- function server.on_process()
- if not witchcraft.started then
- witchcraft.start(_G)
- witchcraft.started = true
- end
- witchcraft.step(nil, 0)
- end
- end
- log("Witchcraft loaded")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement