From 4d063f6061542921e66e8aeb23426d501fa0a7e1 Mon Sep 17 00:00:00 2001 From: Ashafix Date: Sun, 24 Dec 2017 00:24:22 +0100 Subject: [PATCH 1/2] new library for communicating with other programs via Lua --- .../Lua/UnitTests/TestCommunication_All.lua | 149 ++++++ .../UnitTests/TestCommunication_Simple.lua | 91 ++++ BizHawk.Client.EmuHawk/ArgParser.cs | 57 ++- .../BizHawk.Client.EmuHawk.csproj | 15 +- BizHawk.Client.EmuHawk/Communication.cs | 445 ++++++++++++++++++ BizHawk.Client.EmuHawk/GlobalWin.cs | 3 + BizHawk.Client.EmuHawk/MainForm.cs | 2 +- .../Libraries/EmuLuaLibrary.Communication.cs | 161 +++++++ 8 files changed, 916 insertions(+), 7 deletions(-) create mode 100644 Assets/Lua/UnitTests/TestCommunication_All.lua create mode 100644 Assets/Lua/UnitTests/TestCommunication_Simple.lua create mode 100644 BizHawk.Client.EmuHawk/Communication.cs create mode 100644 BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Communication.cs diff --git a/Assets/Lua/UnitTests/TestCommunication_All.lua b/Assets/Lua/UnitTests/TestCommunication_All.lua new file mode 100644 index 00000000000..798ef22642c --- /dev/null +++ b/Assets/Lua/UnitTests/TestCommunication_All.lua @@ -0,0 +1,149 @@ +function round(num, numDecimalPlaces) + local mult = 10^(numDecimalPlaces or 0) + return math.floor(num * mult + 0.5) / mult +end + +function get_baseline() + i = 100 + client.reboot_core() + t = os.clock() + + while i > 0 do + emu.frameadvance(); + i = i - 1; + end + baseline = os.clock() - t + print('Baseline: ' .. round(baseline, 3) .. " secs") + return baseline +end + +function test_mmf() + i = 100 + client.reboot_core() + t = os.clock() + while i > 0 do + emu.frameadvance(); + comm.mmfScreenshot() + i = i - 1; + end + print('Memory mapped files: ' .. round((os.clock() - t - baseline), 3) .. " secs") +end + +function test_http() + print("Testing HTTP server") + client.reboot_core() + i = 100 + t = os.clock() + + while i > 0 do + emu.frameadvance() + comm.httpTestGet() + i = i - 1; + end + print('HTTP get: ' .. round((os.clock() - t - baseline), 3) .. " secs") + + client.reboot_core() + i = 100 + t = os.clock() + + while i > 0 do + emu.frameadvance() + comm.httpPostScreenshot() + i = i - 1; + end + print('HTTP post: ' .. round((os.clock() - t - baseline), 3) .. " secs") + +end + +function test_socket() + + i = 100 + client.reboot_core() + t = os.clock() + while i > 0 do + emu.frameadvance() + comm.SocketServerScreenShot() + i = i - 1; + end + print('Socket server: ' .. round((os.clock() - t - baseline), 3) .. " secs") +end + +function test_socketresponse() + best_time = -100 + timeouts = {1, 2, 3, 4, 5, 10, 20, 25, 50, 100, 250, 500, 1000} + comm.SocketServerSetTimeout(1000); + resp = comm.SocketServerScreenShotResponse() + for t, timeout in ipairs(timeouts) do + comm.SocketServerSetTimeout(timeout); + client.reboot_core() + print("Trying to find minimal timeout for Socket server") + i = 100 + t = os.clock() + while i > 0 do + emu.frameadvance() + resp = comm.SocketServerScreenShotResponse() + if resp ~= 'ack' then + i = -100 + print(resp) + print("Failed to a get a proper response") + end + i = i - 1; + end + if i > -100 then + print("Best timeout: " .. timeout .. " msecs") + print("Best time: " .. round((os.clock() - t - baseline), 3) .. " secs") + break + end + end + +end + +function test_http_response() + err = false + print("Testing HTTP server response") + client.reboot_core() + i = 100 + + while i > 0 do + emu.frameadvance() + resp = comm.httpTestGet() + if resp ~= "

hi!

" then + print("Failed to get correct HTTP get response") + print(resp) + i = 0 + err = true + end + i = i - 1; + end + if not err then + print("HTTP GET looks fine: No errors occurred") + end + + client.reboot_core() + i = 100 + err = false; + while i > 0 do + emu.frameadvance() + resp = comm.httpPostScreenshot() + if resp ~= "OK" then + print("Failed to get correct HTTP post response") + print(resp) + i = 0 + err = true + end + i = i - 1; + end + if not err then + print("HTTP POST looks fine: No errors occurred") + end +end + +baseline = get_baseline() +test_socket() +test_mmf() +test_http() +print("#####################") +test_http_response() +test_socketresponse() +print() + diff --git a/Assets/Lua/UnitTests/TestCommunication_Simple.lua b/Assets/Lua/UnitTests/TestCommunication_Simple.lua new file mode 100644 index 00000000000..b7b5fac6472 --- /dev/null +++ b/Assets/Lua/UnitTests/TestCommunication_Simple.lua @@ -0,0 +1,91 @@ +print("##########################################################") +getUrl = comm.httpGetGetUrl() +print("GET URL: " .. getUrl) + +postUrl = comm.httpGetPostUrl() +print("POST URL: " .. postUrl) + +print("\nChecking GET URL change") +error = false +comm.httpSetGetUrl('a') +if (getUrl ~= comm.httpGetGetUrl()) then + comm.httpSetGetUrl(getUrl) + error = (getUrl ~= comm.httpGetGetUrl()) +else + error = true +end + +if error == false then + print("Get URL was successfully changed") +else + print("Error while changing Get URL") +end + + +print("\nChecking POST URL change") +error = false +comm.httpSetPostUrl('a') +if (postUrl ~= comm.httpGetPostUrl()) then + comm.httpSetPostUrl(postUrl) + error = (postUrl ~= comm.httpGetPostUrl()) +else + error = true +end + +if error == false then + print("Post URL was successfully changed") +else + print("Error while changing Post URL") +end + +print("\nChecking GET request") +getResponse = comm.httpGet("http://tasvideos.org/BizHawk.html") +if string.find(getResponse, "Bizhawk") then + print("GET seems to work") +else + print("Either the Bizhawk site is down or the GET does not work") +end + +print("\nChecking memory mapped filed") + +size = comm.mmfScreenshot() +if size > 0 then + print("Memory mapped file was successfully written") +else + print("Failed to write memory mapped file") +end + +mmf_filename = comm.mmfGetFilename() +print("MMF filename: " .. mmf_filename) +comm.mmfSetFilename("deleteme.tmp") +error = false +if (mmf_filename ~= comm.mmfGetFilename()) then + comm.mmfSetFilename(mmf_filename) + error = (mmf_filename ~= comm.mmfGetFilename()) +else + error = true +end +if error == false then + print("MMF filename successfully changed") +else + print("MMF filename change failed") +end + +print("Writing to MMF") + +message = "ABC" +resp_n = tonumber(comm.mmfWrite(mmf_filename, message)) +if (resp_n ~= string.len(message)) then + print("Failed to write to MMF") +else + resp = comm.mmfRead(mmf_filename, string.len(message)) + if (resp ~= message) then + print("Failed to read from MMF") + else + print("MMF read and read OK") + end +end + + +print("\nTests finished") +print("Please run TestCommunication_All.lua with the supplied Python server for a more comprehensive test") diff --git a/BizHawk.Client.EmuHawk/ArgParser.cs b/BizHawk.Client.EmuHawk/ArgParser.cs index eae8c11fdfd..0912c68ba93 100644 --- a/BizHawk.Client.EmuHawk/ArgParser.cs +++ b/BizHawk.Client.EmuHawk/ArgParser.cs @@ -27,11 +27,14 @@ class ArgParser public bool luaConsole = false; public int socket_port = 9999; public string socket_ip = null; + public string mmf_filename = null; + public string URL_get = null; + public string URL_post = null; + + public void ParseArguments(string[] args) - public void parseArguments(string[] args) - { - for (int i = 0; i") @@ -62,8 +65,8 @@ public void parseArguments(string[] args) } else if (arg.StartsWith("--dump-frames=")) { - var list = arg.Substring(arg.IndexOf('=') + 1); - var items = list.Split(','); + string list = arg.Substring(arg.IndexOf('=') + 1); + string[] items = list.Split(','); _currAviWriterFrameList = new HashSet(); foreach (string item in items) { @@ -110,11 +113,55 @@ public void parseArguments(string[] args) { socket_ip = arg.Substring(arg.IndexOf('=') + 1); } + else if (arg.StartsWith("--mmf=")) + { + mmf_filename = args[i].Substring(args[i].IndexOf('=') + 1); + } + else if (arg.StartsWith("--url_get=")) + { + URL_get = args[i].Substring(args[i].IndexOf('=') + 1); + } + else if (arg.StartsWith("--url_post=")) + { + URL_post = args[i].Substring(args[i].IndexOf('=') + 1); + } else { cmdRom = args[i]; } } + ////initialize HTTP communication + if (URL_get != null || URL_post != null) + { + if (URL_get != null) + { + GlobalWin.httpCommunication.initialized = true; + GlobalWin.httpCommunication.SetGetUrl(URL_get); + } + if (URL_post != null) + { + GlobalWin.httpCommunication.initialized = true; + GlobalWin.httpCommunication.SetPostUrl(URL_post); + } + } + //inititalize socket server + if (socket_ip != null && socket_port > -1) + { + GlobalWin.socketServer.initialized = true; + GlobalWin.socketServer.SetIp(socket_ip, socket_port); + } + else if (socket_ip != null) + { + GlobalWin.socketServer.initialized = true; + GlobalWin.socketServer.SetIp(socket_ip); + } + + //initialize mapped memory files + if (mmf_filename != null) + { + GlobalWin.memoryMappedFiles.initialized = true; + GlobalWin.memoryMappedFiles.SetFilename(mmf_filename); + } } } } diff --git a/BizHawk.Client.EmuHawk/BizHawk.Client.EmuHawk.csproj b/BizHawk.Client.EmuHawk/BizHawk.Client.EmuHawk.csproj index 2a860dd0823..67110934c6b 100644 --- a/BizHawk.Client.EmuHawk/BizHawk.Client.EmuHawk.csproj +++ b/BizHawk.Client.EmuHawk/BizHawk.Client.EmuHawk.csproj @@ -36,7 +36,6 @@ 1.0.0.%2a false true - None true @@ -92,6 +91,7 @@ 3.5 + @@ -193,6 +193,7 @@ BizBoxInfoControl.cs + Component @@ -870,6 +871,7 @@ NewHexEditor.cs + @@ -2153,6 +2155,17 @@ + + + {662901FC-6951-4854-9EB2-D9A2570F2B2E} + 5 + 1 + 0 + tlbimp + False + True + +