diff --git a/Common/Cpp/Sockets/ClientSocket_WinSocket.cpp b/Common/Cpp/Sockets/ClientSocket_WinSocket.cpp index cfc5602942..37f630c19b 100644 --- a/Common/Cpp/Sockets/ClientSocket_WinSocket.cpp +++ b/Common/Cpp/Sockets/ClientSocket_WinSocket.cpp @@ -126,10 +126,10 @@ size_t ClientSocket_WinSocket::send(const void* data, size_t bytes, std::chrono: int current = (int)std::min(bytes, BLOCK_SIZE); int current_sent = ::send(socket, ptr, current, 0); if (current_sent != SOCKET_ERROR){ + // A partial send is not an error: advance and keep looping + // until everything is written or the deadline/state ends + // the send, the same way the POSIX and Qt backends do. sent += current_sent; - if (current_sent != current){ - return sent; - } ptr += current_sent; bytes -= current_sent; continue; diff --git a/Common/Cpp/Sockets/ServerSocket_WinSocket.cpp b/Common/Cpp/Sockets/ServerSocket_WinSocket.cpp index 7779c00122..8f1b1d4d24 100644 --- a/Common/Cpp/Sockets/ServerSocket_WinSocket.cpp +++ b/Common/Cpp/Sockets/ServerSocket_WinSocket.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include "Common/Cpp/Concurrency/ThreadPool.h" #include "ServerSocket_WinSocket.h" @@ -191,7 +192,12 @@ size_t ServerSocket_WinSocket::send(const void* data, size_t bytes, std::chrono: return sent; } - int current_sent = ::send(socket, ptr, (int)bytes, 0); + // "::send" takes an "int" length, so chunk the send the same + // way the WinSocket client does; sending a payload larger than + // 2 GiB in one call would wrap the length negative. + constexpr size_t BLOCK_SIZE = (size_t)1 << 30; + const int current = (int)std::min(bytes, BLOCK_SIZE); + int current_sent = ::send(socket, ptr, current, 0); if (current_sent != SOCKET_ERROR){ sent += (size_t)current_sent; ptr += current_sent; diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryQuestDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryQuestDetector.cpp index 378cb5236f..68fd546569 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryQuestDetector.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryQuestDetector.cpp @@ -10,6 +10,7 @@ #include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" #include "CommonTools/Images/ImageFilter.h" #include "VideoGames/PokemonSV/Inference/PokemonSV_BlueberryQuestReader.h" +#include "Tests/ImageUnitTest.h" #include "PokemonSV_BlueberryQuestDetector.h" namespace PokemonAutomation{ @@ -108,8 +109,73 @@ bool BlueberryQuestWatcher::process_frame(const ImageViewRGB32& screen, WallCloc } +// One quest slot on a full frame of the quest panel. +struct BlueberryQuestScenario{ + std::string image; + BlueberryQuestDetector::QuestPosition position; + std::string expected_slug; + size_t width; + size_t height; +}; + +class Test_BlueberryQuestDetector : public ImageUnitTest{ +public: + Test_BlueberryQuestDetector(BlueberryQuestScenario scenario) + : ImageUnitTest( + "PokemonSV::BlueberryQuestDetector - " + scenario.image + + " - slot " + std::to_string((int)scenario.position + 1) + + " - " + std::to_string(scenario.width), + {scenario.image} + ) + , m_scenario(std::move(scenario)) + {} + + virtual UnitTestResult run_on_images( + Logger& logger, CancellableScope&, + const std::vector& images + ) const override{ + ImageRGB32 image = images[0].scale_to(m_scenario.width, m_scenario.height); + BlueberryQuestDetector detector(logger, COLOR_RED, Language::ChineseTraditional, m_scenario.position); + const std::string actual = detector.detect_quest(image); + if (actual != m_scenario.expected_slug){ + return "Expected quest \"" + m_scenario.expected_slug + "\", read \"" + actual + "\""; + } + return true; + } +private: + const BlueberryQuestScenario m_scenario; +}; + +// Every slot of one Traditional Chinese quest panel frame, at 1080p and 720p. +static void add_tests_BlueberryQuestPanel( + UnitTestDatabase& database, + const std::string& image, const std::string (&slugs)[4] +){ + using enum BlueberryQuestDetector::QuestPosition; + const BlueberryQuestDetector::QuestPosition positions[] = {FIRST, SECOND, THIRD, FOURTH}; + const std::pair sizes[] = {{1920, 1080}, {1280, 720}}; + for (size_t slot = 0; slot < 4; slot++){ + for (const auto& [width, height] : sizes){ + database.add(BlueberryQuestScenario{ + .image = image, + .position = positions[slot], + .expected_slug = slugs[slot], + .width = width, + .height = height, + }); + } + } +} +void add_tests_BlueberryQuestDetector(UnitTestDatabase& database){ + // Singleplayer without a red quest shows 3 quests, so the fourth slot reads + // as nothing. "catch-any" reads "捕捉1隻..." on screen where the dictionary + // has "捕捉一隻...". + add_tests_BlueberryQuestPanel(database, "PokemonSV/BlueberryPoints/6750.png", {"catch-any", "sneak-up", "auto-10", ""}); + // The first frame after rerolling the first quest. + add_tests_BlueberryQuestPanel(database, "PokemonSV/BlueberryPoints/6740.png", {"photo-swim", "sneak-up", "auto-10", ""}); +} diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryQuestDetector.h b/SerialPrograms/Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryQuestDetector.h index 2dcb501a2c..414f22e55b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryQuestDetector.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryQuestDetector.h @@ -9,6 +9,7 @@ #include "Common/Cpp/Logging/AbstractLogger.h" #include "Common/Cpp/Color.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" #include "CommonFramework/Language.h" @@ -75,6 +76,9 @@ class BlueberryQuestWatcher : public VisualInferenceCallback{ }; +void add_tests_BlueberryQuestDetector(UnitTestDatabase& database); + + } diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp index 80f1d0b6f8..17685b40e0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp @@ -31,6 +31,7 @@ #include "CommonFramework/ImageTypes/ImageRGB32.h" #include "Tests/ImageUnitTest.h" #include "Inference/PokemonSV_BlueberryPointsReader.h" +#include "Inference/PokemonSV_BlueberryQuestDetector.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -180,6 +181,7 @@ void add_tests(UnitTestDatabase& database){ add_tests_NormalBattleMenus(database); add_tests_LetsGoKillDetector(database); add_tests_TeraEncounterAction(database); + add_tests_BlueberryQuestDetector(database); }