Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. --[[
  2. Setup of on_process() function taken from the excellent example set by SLMod
  3. ]] --
  4.  
  5. log("Loading Witchcraft into the server environment...")
  6.  
  7. witchcraft = witchcraft or {}
  8.  
  9. witchcraft.oldfunc = {}
  10. witchcraft.oldfunc.on_process = server.on_process()
  11.  
  12.  
  13.  
  14. do
  15. witchcraft.host = "localhost"
  16. witchcraft.port = 3001
  17. local require = require
  18. local loadfile = loadfile
  19.  
  20. package.path = package.path..";.\\LuaSocket\\?.lua"
  21. package.cpath = package.cpath..";.\\LuaSocket\\?.dll"
  22.  
  23. local JSON = loadfile("Scripts\\JSON.lua")()
  24. witchcraft.JSON = JSON
  25. local socket = require("socket")
  26.  
  27. function witchcraft.step(arg, time)
  28. witchcraft.txbuf = witchcraft.txbuf .. '{"type":"dummy"}\n'
  29. if witchcraft.txbuf:len() > 0 then
  30. local bytes_sent = nil
  31. local ret1, ret2, ret3 = witchcraft.conn:send(witchcraft.txbuf)
  32. if ret1 then
  33. bytes_sent = ret1
  34. else
  35. --env.info("could not send witchcraft: "..ret2)
  36. if ret3 == 0 then
  37. if ret2 == "closed" then
  38. witchcraft.txbuf = '{"type":"dummy"}\n'
  39. witchcraft.rxbuf = ""
  40. witchcraft.lastUnitUpdateTime = 0
  41. witchcraft.conn = socket.tcp()
  42. witchcraft.conn:settimeout(.0001)
  43. --env.info("witchcraft: socket was closed")
  44. end
  45. --env.info("reconnecting to "..tostring(witchcraft.host)..":"..tostring(witchcraft.port))
  46. witchcraft.conn:connect(witchcraft.host, witchcraft.port)
  47. return
  48. end
  49. bytes_sent = ret3
  50. end
  51. witchcraft.txbuf = witchcraft.txbuf:sub(bytes_sent + 1)
  52. else
  53. if witchcraft.txidle_hook then
  54. local bool, err = pcall(witchcraft.txidle_hook)
  55. if not bool then
  56. --env.info("witchcraft.txidle_hook() failed: "..err)
  57. end
  58. end
  59. end
  60.  
  61. local line, err = witchcraft.conn:receive()
  62. if err then
  63. --env.info("witchcraft read error: "..err)
  64. else
  65. msg = JSON:decode(line)
  66. if msg.type == "lua" then
  67. local response_msg = {}
  68. response_msg.type = "luaresult"
  69. response_msg.name = msg.name
  70. local f, error_msg = loadstring(msg.code, msg.name)
  71. if f then
  72. witchcraft.context = {}
  73. witchcraft.context.arg = msg.arg
  74. setfenv(f, witchcraft.mission_env)
  75. response_msg.success, response_msg.result = pcall(f)
  76. else
  77. response_msg.success = false
  78. response_msg.result = tostring(error_msg)
  79. end
  80.  
  81. local response_string = ""
  82. local function encode_response()
  83. response_string = JSON:encode(response_msg):gsub("\n","").."\n"
  84. end
  85.  
  86. local success, result = pcall(encode_response)
  87. if not success then
  88. response_msg.success = false
  89. response_msg.result = tostring(result)
  90. encode_response()
  91. end
  92.  
  93. witchcraft.txbuf = witchcraft.txbuf .. response_string
  94. end
  95. end
  96.  
  97. end
  98.  
  99. witchcraft.start = function(mission_env_)
  100. witchcraft.mission_env = mission_env_
  101.  
  102. if not witchcraft.scheduled then
  103.  
  104.  
  105. witchcraft.lastUnitUpdateTime = 0
  106. witchcraft.unitUpdateInterval = 0
  107. witchcraft.txbuf = '{"type":"dummy"}\n'
  108. witchcraft.rxbuf = ""
  109. witchcraft.lastUnitUpdateTime = 0
  110. witchcraft.conn = socket.tcp()
  111. witchcraft.conn:settimeout(.0001)
  112. witchcraft.conn:connect(witchcraft.host, witchcraft.port)
  113. witchcraft.scheduled = true
  114.  
  115. end
  116. end
  117.  
  118. witchcraft.log = function(data)
  119. local msg = { ["type"] = "log", ["data"] = data };
  120. witchcraft.txbuf = witchcraft.txbuf .. JSON:encode(msg):gsub("\n","").."\n"
  121. end
  122.  
  123.  
  124. function server.on_process()
  125. if not witchcraft.started then
  126. witchcraft.start(_G)
  127. witchcraft.started = true
  128. end
  129. witchcraft.step(nil, 0)
  130. end
  131.  
  132. end
  133.  
  134. log("Witchcraft loaded")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement