Advertisement
Guest User

Untitled

a guest
Feb 5th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.50 KB | None | 0 0
  1. local guiClick = {}
  2.  
  3. mainform = nil
  4. local text1, lblRooms, btnGetRooms, ddRooms, btnQuit, btnJoin, btnHost
  5. local txtUser, txtPass, lblUser, lblPass, ddRamCode, lblRamCode
  6. local lblPort, txtPort
  7. config = {}
  8.  
  9.  
  10. function strsplit(inputstr, sep, max)
  11. if not inputstr then
  12. return {}
  13. end
  14.  
  15. if not sep then
  16. sep = ","
  17. end
  18. local t={} ; i=1
  19. for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  20. if max and i > max then
  21. if t[i] then
  22. t[i] = t[i] .. sep .. str
  23. else
  24. t[i] = str
  25. end
  26. else
  27. t[i] = str
  28. i = i + 1
  29. end
  30. end
  31. return t
  32. end
  33.  
  34.  
  35. local sync = require("bizhawk-co-op\\sync")
  36.  
  37.  
  38. --Add a line to the output. Inserts a timestamp to the string
  39. function printOutput(str)
  40. local text = forms.gettext(text1)
  41. local pos = #text
  42. forms.setproperty(text1, "SelectionStart", pos)
  43.  
  44. str = string.gsub (str, "\n", "\r\n")
  45. str = "[" .. os.date("%H:%M:%S", os.time()) .. "] " .. str
  46. if pos > 0 then
  47. str = "\r\n" .. str
  48. end
  49.  
  50. forms.setproperty(text1, "SelectedText", str)
  51. end
  52.  
  53.  
  54. host = require("bizhawk-co-op\\host")
  55.  
  56.  
  57. local roomlist = false
  58. function refreshRooms()
  59. roomlist = host.getRooms()
  60. if roomlist then
  61. roomlist['(Custom IP)']='(Custom IP)'
  62. forms.setdropdownitems(ddRooms, roomlist)
  63. else
  64. forms.setdropdownitems(ddRooms,
  65. {
  66. ['(Custom IP)']='(Custom IP)'
  67. })
  68. end
  69.  
  70. updateGUI()
  71. end
  72.  
  73.  
  74. --Reloads all the info on the form. Disables any inappropriate components
  75. function updateGUI()
  76. if host.status == 'Idle' then
  77. if forms.setdropdownitems and roomlist then
  78. forms.setproperty(ddRooms, 'Enabled', true)
  79. else
  80. forms.setproperty(ddRooms, 'Enabled', false)
  81. end
  82. forms.setproperty(btnGetRooms, "Enabled", true)
  83. forms.setproperty(ddRamCode, "Enabled", true)
  84. forms.setproperty(txtUser, "Enabled", true)
  85. forms.setproperty(txtPass, "Enabled", true)
  86. forms.setproperty(txtPort, "Enabled", true)
  87. forms.setproperty(btnQuit, "Enabled", false)
  88. forms.setproperty(btnJoin, "Enabled", true)
  89. forms.setproperty(btnHost, "Enabled", true)
  90. forms.settext(btnHost, "Create Room")
  91. forms.setproperty(btnHost, "Enabled", true)
  92.  
  93. if forms.gettext(ddRooms) == '(Custom IP)' then
  94. forms.setproperty(txtIP, "Enabled", true)
  95. else
  96. forms.setproperty(txtIP, "Enabled", false)
  97. end
  98. else
  99. forms.setproperty(btnGetRooms, "Enabled", false)
  100. forms.setproperty(ddRamCode, "Enabled", false)
  101. forms.setproperty(ddRooms, "Enabled", false)
  102. forms.setproperty(txtUser, "Enabled", false)
  103. forms.setproperty(txtPass, "Enabled", false)
  104. forms.setproperty(txtPort, "Enabled", false)
  105. forms.setproperty(btnQuit, "Enabled", true)
  106. forms.setproperty(btnJoin, "Enabled", false)
  107. forms.setproperty(btnHost, "Enabled", true)
  108. if host.locked then
  109. forms.settext(btnHost, "Unlock Room")
  110. else
  111. forms.settext(btnHost, "Lock Room")
  112. end
  113. forms.setproperty(txtIP, "Enabled", false)
  114. end
  115. end
  116.  
  117.  
  118. --If the script ends, makes sure the sockets and form are closed
  119. event.onexit(function () host.close(); forms.destroy(mainform) end)
  120.  
  121.  
  122. --Load the changes from the form and disable any appropriate components
  123. function prepareConnection()
  124. if roomlist then
  125. config.room = forms.gettext(ddRooms)
  126. else
  127. config.room = ''
  128. end
  129. config.ramcode = forms.gettext(ddRamCode)
  130. config.user = forms.gettext(txtUser)
  131. config.pass = forms.gettext(txtPass)
  132. config.port = forms.gettext(txtPort)
  133. config.hostname = forms.gettext(txtIP)
  134. end
  135.  
  136.  
  137. --Quit/Disconnect click handle for the quit button
  138. function leaveRoom()
  139. if (host.connected()) then
  140. sendMessage["Quit"] = true
  141. else
  142. host.close()
  143. end
  144. end
  145.  
  146.  
  147. --Returns a list of files in a given directory
  148. function os.dir(dir)
  149. local files = {}
  150. local f = assert(io.popen('dir \"' .. dir .. '\" /b ', 'r'))
  151. for file in f:lines() do
  152. table.insert(files, file)
  153. end
  154. f:close()
  155. return files
  156. end
  157.  
  158.  
  159. --Create the form
  160. mainform = forms.newform(310, 356, "Bizhawk Co-op")
  161.  
  162. text1 = forms.textbox(mainform, "", 263, 105, nil, 16, 199, true, true, 'Vertical')
  163. forms.setproperty(text1, "ReadOnly", true)
  164. --forms.setproperty(text1, "MaxLength", 32767)
  165.  
  166.  
  167. if forms.setdropdownitems then -- can't update list prior to bizhawk 1.12.0
  168. btnGetRooms = forms.button(mainform, "Refresh Rooms", refreshRooms, 220, 10, 60, 23)
  169. ddRooms = forms.dropdown(mainform, {['(Fetching rooms...)']='(Fetching rooms...)'}, 80, 11, 135, 20)
  170. forms.setproperty(ddRooms, 'Enabled', false)
  171. guiClick["Refresh Rooms"] = refreshRooms;
  172. else
  173. btnGetRooms = forms.button(mainform, "", function() end, 15, 10, 60, 23)
  174. forms.setproperty(btnGetRooms, 'Enabled', false)
  175.  
  176. roomlist = host.getRooms()
  177. if roomlist then
  178. ddRooms = forms.dropdown(mainform, roomlist, 80, 11, 200, 20)
  179. forms.setproperty(ddRooms, 'Enabled', true)
  180. else
  181. ddRooms = forms.dropdown(mainform, {['(Custom IP)']='(Custom IP)'}, 80, 11, 200, 20)
  182. forms.setproperty(ddRooms, 'Enabled', false)
  183. end
  184. end
  185. lblRooms = forms.label(mainform, "Rooms:", 34, 13)
  186.  
  187. txtIP = forms.textbox(mainform, "", 200, 20, nil, 80, 40, false, false)
  188. txtUser = forms.textbox(mainform, "", 200, 20, nil, 80, 64, false, false)
  189. txtPass = forms.textbox(mainform, "", 200, 20, nil, 80, 88, false, false)
  190. txtPort = forms.textbox(mainform, '50000', 200, 20, nil, 80, 112, false, false)
  191. ddRamCode = forms.dropdown(mainform, os.dir("bizhawk-co-op\\ramcontroller"), 80, 138, 200, 10)
  192. lblIP = forms.label(mainform, "Host IP:", 32, 42)
  193. lblUser = forms.label(mainform, "Username:", 19, 66)
  194. lblPass = forms.label(mainform, "Password:", 21, 90)
  195. lblPort = forms.label(mainform, "Port:", 48, 115)
  196. lblRamCode = forms.label(mainform, "Game Script:", 10, 140)
  197.  
  198. forms.setproperty(txtPass, 'PasswordChar', '*')
  199.  
  200.  
  201. btnQuit = forms.button(mainform, "Leave Room", leaveRoom,
  202. 15, 166, 85, 25)
  203. forms.setproperty(btnQuit, 'Enabled', false)
  204. btnHost = forms.button(mainform, "Create Room",
  205. function() prepareConnection(); guiClick["Host Server"] = host.start end,
  206. 105, 166, 85, 25)
  207. btnJoin = forms.button(mainform, "Join Room",
  208. function() prepareConnection(); guiClick["Join Server"] = host.join end,
  209. 195, 166, 85, 25)
  210.  
  211. sendMessage = {}
  212. local thread
  213.  
  214. updateGUI()
  215.  
  216. local threads = {}
  217.  
  218. emu.yield()
  219. emu.yield()
  220.  
  221. ---------------------
  222. -- Main loop --
  223. ---------------------
  224. while 1 do
  225. --End script if form is closed
  226. if forms.gettext(mainform) == "" then
  227. return
  228. end
  229.  
  230. if (forms.gettext(ddRooms) ~= prevRoom) then
  231. prevRoom = forms.gettext(ddRooms)
  232. updateGUI()
  233. end
  234.  
  235. host.listen()
  236.  
  237. --Create threads for the function requests from the form
  238. for k,v in pairs(guiClick) do
  239. threads[coroutine.create(v)] = k
  240. end
  241. guiClick = {}
  242.  
  243. --Run the threads
  244. for k,v in pairs(threads) do
  245. if coroutine.status(k) == "dead" then
  246. threads[k] = nil
  247. else
  248. local status, err = coroutine.resume(k)
  249. if (status == false) then
  250. if (err ~= nil) then
  251. printOutput("Error during " .. v .. ": " .. err)
  252. else
  253. printOutput("Error during " .. v .. ": No error message")
  254. end
  255. end
  256. end
  257. end
  258.  
  259. --If connected, run the syncinputs thread
  260. if host.connected() then
  261. --If the thread didn't yield, then create a new one
  262. if thread == nil or coroutine.status(thread) == "dead" then
  263. thread = coroutine.create(sync.syncRAM)
  264. end
  265. local status, err = coroutine.resume(thread)
  266.  
  267. if (status == false and err ~= nil) then
  268. printOutput("Error during sync: " .. tostring(err))
  269. end
  270. end
  271.  
  272. -- 2 Emu Yields = 1 Frame Advance
  273. --If game is paused, then yield will not frame advance
  274. emu.yield()
  275. emu.yield()
  276. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement