diff --git a/.github/workflows/cpp-ci-serial-programs-base.yml b/.github/workflows/cpp-ci-serial-programs-base.yml index e83ca269e5..7d785d469e 100644 --- a/.github/workflows/cpp-ci-serial-programs-base.yml +++ b/.github/workflows/cpp-ci-serial-programs-base.yml @@ -257,7 +257,9 @@ jobs: uses: actions/checkout@v7 if: inputs.run-tests with: - repository: 'PokemonAutomation/CommandLineTests' + # The fork's fixtures: upstream's plus the SystemKeyboardDetector and HomeTileReader screenshots. + repository: 'dayfine/CommandLineTests' + ref: 'eae4697738d5fe18975e77c05a4e01216bed68e7' path: 'CommandLineTests' # The unit tests assume the downloadable resources they need are already downloaded diff --git a/.github/workflows/story-data.yml b/.github/workflows/story-data.yml index 8576b19548..045f398256 100644 --- a/.github/workflows/story-data.yml +++ b/.github/workflows/story-data.yml @@ -15,7 +15,7 @@ jobs: with: repository: dayfine/PokemonSimulation # Synthetic contract cases, not an approved map/resource pin. - ref: 866613fa03e5c4a5eb7e57c91b59fafb44fc7ee3 + ref: 4e023f3d57022c68018f95f6fc0a4c0722d53adf path: publisher - uses: actions/setup-python@v6 with: @@ -28,3 +28,7 @@ jobs: - run: cmake -S devtools/story-data-tests -B build-story-data -DCMAKE_BUILD_TYPE=Debug - run: cmake --build build-story-data --config Debug -j4 - run: ctest --test-dir build-story-data -C Debug --output-on-failure + - if: runner.os != 'Windows' + run: ./build-story-data/StoryDataTests publisher/data/story/wire-conformance.json + - if: runner.os == 'Windows' + run: ./build-story-data/Debug/StoryDataTests.exe publisher/data/story/wire-conformance.json diff --git a/Common/Cpp/HttpServer/HttpServer.cpp b/Common/Cpp/HttpServer/HttpServer.cpp new file mode 100644 index 0000000000..b4c97cbab1 --- /dev/null +++ b/Common/Cpp/HttpServer/HttpServer.cpp @@ -0,0 +1,821 @@ +/* HTTP Server + * + * From: https://github.com/PokemonAutomation/ + * + * Implementation notes: + * - The socket's receive/disconnect callbacks only append data to the + * per-connection buffer and flip flags. One "connection task" per + * connection (from the thread pool) does all parsing and writing, so + * the handler may run for a long time without stalling the socket's + * receive loop. + * - Every write goes through "AbstractServerSocket::send()" with the + * connection's socket id (captured at accept time), so a response + * that outlives its connection - the peer disconnected and the next + * client was accepted in the meantime - is refused by the socket + * instead of reaching the new client's stream. + * - "Expect: 100-continue" is answered as soon as the headers are + * validated, while the body is still pending (a waiting client is + * blocked on that interim response). + * - Task handles live in "m_tasks" under a dedicated "m_task_lock" + * (the socket library's "m_task_lock" pattern): destroying an + * "AsyncTask" handle blocks until that task finishes, and the task's + * final cleanup takes "m_lock", so a handle must never be destroyed + * while holding "m_lock". "on_connect()" may therefore find the + * previous connection's task still finishing (for example, still in a + * long handler call); its handle is kept and reaped later. + * - "stop()" marks the active connection dead before it waits for the + * in-flight task: the socket suppresses its disconnect callback for a + * close_connection() close, so without the mark a handler polling + * "connection_alive()" would never see the shutdown. "stop()" then + * moves the socket out under "m_lock" and destroys it after releasing + * the lock: "~ServerSocket" joins the socket's worker threads, and + * their callbacks take "m_lock". + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Concurrency/ThreadPool.h" +#include "Common/Cpp/HttpServer/HttpServer.h" +#include "Common/Cpp/Sockets/ServerSocket.h" + +namespace PokemonAutomation{ + +namespace{ + + +constexpr size_t MAX_REQUEST_LINE_BYTES = 8192; +constexpr size_t MAX_HEADER_SECTION_BYTES = 65536; +constexpr size_t MAX_HEADER_COUNT = 100; +constexpr size_t MAX_BODY_BYTES = 1u << 20; // 1 MiB +// The total per-connection receive buffer; enforced when data is +// appended, not at parse time. +constexpr size_t MAX_BUFFER_BYTES = 2u << 20; + +// A stalled peer is declared dead after this; loopback writes of a few MB +// finish in milliseconds, so this is very generous. +constexpr std::chrono::milliseconds WRITE_TIMEOUT{30000}; + + +bool is_token_char(char c){ + return std::isalnum((unsigned char)c) != 0 + || std::strchr("-!#$%&'*+.^_`|~", c) != nullptr; +} + + +std::string to_lower(std::string value){ + std::transform(value.begin(), value.end(), value.begin(), [](unsigned char c){ + return (char)std::tolower(c); + }); + return value; +} + + +// Strips the leading and trailing spaces and tabs (OWS). +std::string trim_ows(std::string value){ + static const char OWS[] = " \t"; + const auto begin = value.find_first_not_of(OWS); + if (begin == std::string::npos){ + return ""; + } + const auto end = value.find_last_not_of(OWS); + return value.substr(begin, end - begin + 1); +} + + +// The concrete HandlerContext: "connection_alive()" is simply the +// connection's dead flag. +struct HandlerContextImpl : AbstractHttpServer::HandlerContext{ + explicit HandlerContextImpl(const std::atomic& dead) + : m_dead(dead) + {} + + virtual bool connection_alive() const override{ + return !m_dead.load(std::memory_order_acquire); + } + +private: + const std::atomic& m_dead; +}; + + + +} // anonymous namespace + + + +const char* HttpServer::status_reason(int status){ + switch (status){ + case 100: + return "Continue"; + case 200: + return "OK"; + case 201: + return "Created"; + case 204: + return "No Content"; + case 301: + return "Moved Permanently"; + case 302: + return "Found"; + case 304: + return "Not Modified"; + case 400: + return "Bad Request"; + case 401: + return "Unauthorized"; + case 403: + return "Forbidden"; + case 404: + return "Not Found"; + case 405: + return "Method Not Allowed"; + case 408: + return "Request Timeout"; + case 411: + return "Length Required"; + case 413: + return "Payload Too Large"; + case 426: + return "Upgrade Required"; + case 500: + return "Internal Server Error"; + case 501: + return "Not Implemented"; + case 503: + return "Service Unavailable"; + case 505: + return "Version Not Supported"; + default: + return "Unknown Status"; + } +} + + +std::string HttpServer::format_response(const Response& response, bool keep_alive){ + std::string out; + out += "HTTP/1.1 "; + out += std::to_string(response.status); + out += " "; + const char* reason = response.reason.empty() ? status_reason(response.status) : response.reason.c_str(); + out += reason; + out += "\r\n"; + + for (const auto& header : response.headers){ + // The server sets both of these below. + if (to_lower(header.first) == "content-length" || + to_lower(header.first) == "connection" + ){ + continue; + } + out += header.first; + out += ": "; + out += header.second; + out += "\r\n"; + } + + out += "Content-Length: "; + out += std::to_string(response.body.size()); + out += "\r\n"; + out += "Connection: "; + out += keep_alive ? "keep-alive" : "close"; + out += "\r\n\r\n"; + out += response.body; + + return out; +} + + + +HttpServer::HttpServer(ThreadPool& thread_pool) + : m_thread_pool(thread_pool) +{} + + +HttpServer::~HttpServer(){ + stop(); +} + + +void HttpServer::start(const std::string& address, uint16_t port, Handler handler){ + AbstractServerSocket* socket = nullptr; + { + std::lock_guard lg(m_lock); + if (m_socket != nullptr){ + return; + } + m_handler = std::move(handler); + m_stop_requested.store(false, std::memory_order_release); + m_socket = std::make_unique(m_thread_pool); + // Register before start(): the accept thread is dispatched by + // start() and may report on_listen_finished() as soon as it runs. + m_socket->add_listener(*this); + socket = m_socket.get(); + } + + // Asynchronous: on_listen_finished() fires on a thread from the pool. + socket->start(address, port); +} + + +void HttpServer::stop(){ + m_stop_requested.store(true, std::memory_order_release); + + AbstractServerSocket* socket = nullptr; + { + std::lock_guard lg(m_lock); + socket = m_socket.get(); + } + if (socket == nullptr){ + return; + } + + // Mark the active connection dead: the socket suppresses its + // disconnect callback for a close_connection() close, so without + // this a handler polling connection_alive() would never see the + // shutdown, and the wait for it below would never end. + { + std::lock_guard lg(m_lock); + if (m_connection != nullptr){ + m_connection->dead.store(true, std::memory_order_release); + m_connection->cv.notify_all(); + } + } + + // Kill the active connection, if any: it cancels in-flight writes and + // makes the socket's connection thread exit. The socket keeps + // listening until it is destroyed below, so it may accept a few more + // clients in the meantime; on_connect() sees m_stop_requested and + // closes them without dispatching a connection task. + socket->close_connection(); + + // Wait for the in-flight connection tasks. The handles are moved out + // under m_task_lock (never destroyed while holding m_lock: a task's + // final cleanup takes m_lock, and destroying a handle blocks until + // the task finishes). Loop, because a task may have been dispatched + // between the previous ones being joined and the socket being + // destroyed. + while (true){ + std::vector tasks; + { + std::lock_guard lg(m_task_lock); + tasks.swap(m_tasks); + } + if (tasks.empty()){ + break; + } + for (auto& task : tasks){ + task.wait_and_ignore_exceptions(); + } + } + + // Take the socket out under m_lock and destroy it after releasing + // the lock: "~ServerSocket" joins the socket's worker threads, and + // their callbacks (on_connect and friends) take m_lock. + std::unique_ptr owned; + { + std::lock_guard lg(m_lock); + owned = std::move(m_socket); + m_connection = nullptr; + m_handler = Handler(); + } +} + + +bool HttpServer::is_running() const{ + std::lock_guard lg(m_lock); + return (m_socket != nullptr); +} + + + +void HttpServer::on_listen_finished(const std::string& error_message){ + m_listeners.run_method(&Listener::on_listen_finished, error_message); +} + + +void HttpServer::on_connect(){ + AbstractServerSocket* socket = nullptr; + std::shared_ptr conn; + { + std::lock_guard lg(m_lock); + socket = m_socket.get(); + if (socket != nullptr && + !m_stop_requested.load(std::memory_order_acquire) + ){ + conn = std::make_shared(); + // The previous socket connection is over (the socket only + // accepts a new client once the previous one is gone), but + // its task may still be finishing (for example, still inside + // a long handler call), and the socket's on_disconnect + // callback for it may not have run yet: it runs after the + // socket has already moved back to listening. Mark it dead + // now, before the new connection takes over the socket's + // active fd, so the old task discards its response instead + // of delivering it to the new client. + if (m_connection != nullptr){ + m_connection->dead.store(true, std::memory_order_release); + m_connection->cv.notify_all(); + } + m_connection = conn; + // The token of the connection this one will serve; send() + // refuses to write through a socket that has moved on. + conn->socket_id = socket->connection_id(); + } + } + if (conn == nullptr){ + // Stopping (or the socket is gone): close the connection without + // a connection task. + if (socket != nullptr){ + socket->close_connection(); + } + }else{ + // Dispatch outside m_lock: the task's final cleanup takes + // m_lock, and so does stop(). The task handles live in m_tasks + // under a dedicated m_task_lock (the socket library's + // m_task_lock pattern): destroying an AsyncTask handle blocks + // until its task finishes, so a handle must never be destroyed + // while holding m_lock. + try{ + AsyncTask task = m_thread_pool.dispatch( + [this, conn, socket]{ + connection_task(conn, socket); + } + ); + std::lock_guard lg(m_task_lock); + // Reap finished handles (destroying one that is already + // finished is a no-op). Keep unfinished ones: the + // previous connection's task may still be finishing up + // (for example, still inside a long handler call) by the + // time this connection is accepted; it holds only its + // own (dead) connection. + m_tasks.erase( + std::remove_if(m_tasks.begin(), m_tasks.end(), [](const AsyncTask& task){ + return task.is_finished(); + }), + m_tasks.end() + ); + m_tasks.push_back(std::move(task)); + }catch (...){ + // The task could not be started: drop the connection and + // keep listening. + { + std::lock_guard lg(m_lock); + if (m_connection.get() == conn.get()){ + m_connection = nullptr; + } + } + socket->close_connection(); + } + } + m_listeners.run_method(&Listener::on_connect); +} + + +void HttpServer::on_connect_rejected(){ + m_listeners.run_method(&Listener::on_connect_rejected); +} + + +void HttpServer::on_receive_data(const void* data, size_t bytes){ + std::shared_ptr conn; + AbstractServerSocket* socket = nullptr; + { + std::lock_guard lg(m_lock); + conn = m_connection; + socket = m_socket.get(); + } + if (conn == nullptr){ + return; + } + bool exceeded = false; + { + std::lock_guard lg(conn->lock); + exceeded = conn->buffer.size() + bytes > MAX_BUFFER_BYTES; + if (!exceeded){ + const char* ptr = (const char*)data; + conn->buffer.append(ptr, bytes); + } + } + conn->cv.notify_all(); + if (exceeded && socket != nullptr){ + // The stream is longer than we are willing to buffer. It is + // likely in the middle of a request, so there is no reliable + // response left to send: just end the connection. ("socket" can + // be null only while stop() is tearing the socket down, in which + // case there is no connection task left to wake up.) + end_connection(conn, socket); + } +} + + +void HttpServer::on_disconnect(const std::string& error_message){ + std::shared_ptr conn; + { + std::lock_guard lg(m_lock); + // The socket may already be serving the next client by the time + // this callback runs: the socket's connection thread sets the + // state back to LISTENING before it reports the disconnect, so a + // fast reconnect can be accepted in between. In that case this + // disconnect is about the previous connection, which on_connect() + // has already marked dead; the current one belongs to the new + // client and must not be killed. + if (m_socket == nullptr || + m_socket->state() != AbstractServerSocket::State::CONNECTED + ){ + conn = m_connection; + } + } + if (conn != nullptr){ + conn->dead.store(true, std::memory_order_release); + conn->cv.notify_all(); + } + m_listeners.run_method(&Listener::on_disconnect, error_message); +} + + + +void HttpServer::connection_task( + const std::shared_ptr& conn, + AbstractServerSocket* socket +){ + // The handler may be replaced or cleared by start()/stop() while this + // task is waiting; a copy taken under m_lock is stable for the task's + // whole lifetime, because stop() waits for the task before clearing it. + Handler handler; + { + std::lock_guard lg(m_lock); + handler = m_handler; + } + + // "Expect: 100-continue" is answered at most once, and only while + // the body is still pending. + bool sent_continue = false; + + while (true){ + if (m_stop_requested.load(std::memory_order_acquire) || + conn->dead.load(std::memory_order_acquire) + ){ + break; + } + + ParsedRequest parsed; + size_t buffer_size; + { + std::lock_guard lg(conn->lock); + parsed = try_parse(conn->buffer); + buffer_size = conn->buffer.size(); + } + + if (parsed.state == ParseState::NEED_BODY){ + // The headers are complete and valid, but the body has not + // fully arrived. A client that sent "Expect: 100-continue" is + // still waiting for this before sending the body: answer it + // now, not after the (still missing) body arrives. (If the + // whole request was already buffered, the state is READY and + // the final response alone moves the client on; RFC 9110). + if (parsed.expect_continue && !sent_continue){ + static const char CONTINUE_RESPONSE[] = "HTTP/1.1 100 Continue\r\n\r\n"; + if (!write_raw(conn, socket, CONTINUE_RESPONSE)){ + break; + } + sent_continue = true; + } + } + + if (parsed.state == ParseState::NEED_MORE || + parsed.state == ParseState::NEED_BODY + ){ + // Wait for the rest of this request, a disconnect, or a + // stop. The predicate requires the buffer to have grown past + // what the parse just saw: with a partial request, + // "!buffer.empty()" alone is permanently true and would spin + // this loop at 100% CPU until the request completes. + std::unique_lock lg(conn->lock); + conn->cv.wait_for(lg, std::chrono::milliseconds(10), [this, &conn, buffer_size]{ + return conn->buffer.size() != buffer_size + || conn->dead.load(std::memory_order_relaxed) + || m_stop_requested.load(std::memory_order_relaxed); + }); + continue; + } + + if (parsed.state == ParseState::ERROR_RESPONSE){ + Response response; + response.status = parsed.error_status; + response.headers = std::move(parsed.error_headers); + response.body = parsed.error_reason + "\n"; + write_response(conn, socket, response, false); + end_connection(conn, socket); + break; + } + + // A complete request is ready to be handled. + Response response; + { + HandlerContextImpl context(conn->dead); + try{ + if (handler){ + handler(parsed.request, response, context); + }else{ + response.status = 500; + response.body = "No handler is registered.\n"; + response.connection_close = true; + } + }catch (...){ + response = Response(); + response.status = 500; + response.body = "The handler threw an exception.\n"; + response.connection_close = true; + } + } + + // The connection may have closed or been replaced by a new + // client while the handler was running; discard the response in + // that case. + if (conn->dead.load(std::memory_order_acquire) || + m_stop_requested.load(std::memory_order_acquire) + ){ + break; + } + + const bool keep_alive = parsed.http_1_1 + && !parsed.connection_close_requested + && !response.connection_close; + if (!write_response(conn, socket, response, keep_alive)){ + break; + } + if (!keep_alive){ + end_connection(conn, socket); + break; + } + // Keep-alive: the next request (possibly already buffered) is + // picked up at the top of the loop. + } + + conn->dead.store(true, std::memory_order_release); + { + std::lock_guard lg(m_lock); + if (m_connection.get() == conn.get()){ + m_connection = nullptr; + } + } +} + + +HttpServer::ParsedRequest HttpServer::try_parse(std::string& buffer){ + ParsedRequest result; + + auto error = [&result](int status, const char* reason, std::vector> headers = {}){ + result.state = ParseState::ERROR_RESPONSE; + result.error_status = status; + result.error_reason = reason; + result.error_headers = std::move(headers); + }; + + // The header section ends at the blank line. "header_end" points at + // the "\r\n" that terminates the last header (or the request line + // when there are no headers). + const size_t header_end = buffer.find("\r\n\r\n"); + if (header_end == std::string::npos){ + if (buffer.size() > MAX_HEADER_SECTION_BYTES){ + error(400, "Malformed request."); + } + return result; // NEED_MORE: the headers have not fully arrived yet. + } + // The same limit applies to a complete header section: the + // in-progress check above only fires while the terminator is still + // missing, so a large section that arrives in one piece would + // otherwise sail through. + if (header_end + 4 > MAX_HEADER_SECTION_BYTES){ + error(400, "Malformed request."); + return result; + } + + const size_t line_end = buffer.find("\r\n"); + if (line_end == std::string::npos){ + error(400, "Malformed request."); + return result; + } + if (line_end > MAX_REQUEST_LINE_BYTES){ + error(400, "Request line too long."); + return result; + } + + // "METHOD SP TARGET SP VERSION" + const std::string line = buffer.substr(0, line_end); + const size_t sp1 = line.find(' '); + if (sp1 == std::string::npos || sp1 == 0){ + error(400, "Malformed request."); + return result; + } + std::string method = line.substr(0, sp1); + for (char c : method){ + if (c < 'A' || c > 'Z'){ + error(400, "Malformed request."); + return result; + } + } + const size_t sp2 = line.find(' ', sp1 + 1); + if (sp2 == std::string::npos){ + error(400, "Malformed request."); + return result; + } + std::string target = line.substr(sp1 + 1, sp2 - sp1 - 1); + if (target.empty()){ + error(400, "Malformed request."); + return result; + } + for (char c : target){ + if (c == ' ' || c == '\r' || c == '\n' || c == '\0'){ + error(400, "Malformed request."); + return result; + } + } + const std::string version = line.substr(sp2 + 1); + const bool http_1_1 = (version == "HTTP/1.1"); + const bool http_1_0 = (version == "HTTP/1.0"); + if (!http_1_1 && !http_1_0){ + error(505, "HTTP version not supported."); + return result; + } + + // Headers: [line_end + 2, header_end) + const size_t section_begin = line_end + 2; + std::string section; + if (header_end >= section_begin){ + section = buffer.substr(section_begin, header_end - section_begin); + } + size_t header_count = 0; + size_t pos = 0; + while (pos < section.size()){ + const size_t eol = section.find("\r\n", pos); + const size_t length = (eol == std::string::npos) ? section.size() - pos : eol - pos; + const std::string header_line = section.substr(pos, length); + + if (header_line.empty() || ++header_count > MAX_HEADER_COUNT){ + error(400, "Malformed request."); + return result; + } + + const size_t colon = header_line.find(':'); + if (colon == std::string::npos || colon == 0){ + error(400, "Malformed request."); + return result; + } + std::string name = header_line.substr(0, colon); + for (char c : name){ + if (!is_token_char(c)){ + error(400, "Malformed request."); + return result; + } + } + std::string value = trim_ows(header_line.substr(colon + 1)); + for (char c : value){ + if ((unsigned char)c < 0x20 || (unsigned char)c == 0x7F){ + error(400, "Malformed request."); + return result; + } + } + name = to_lower(std::move(name)); + if (name == "content-length" && result.request.headers.count("content-length") != 0){ + error(400, "Duplicate content-length."); + return result; + } + result.request.headers[std::move(name)] = std::move(value); + + if (eol == std::string::npos){ + break; + } + pos = eol + 2; + } + + // Special headers. + if (result.request.headers.count("transfer-encoding") != 0){ + // "Not Implemented" rather than "Length Required": chunked + // encoding is a (unsupported) representation of the body, not a + // missing length (RFC 9112). + error(501, "Chunked transfer encoding is not supported."); + return result; + } + + uint64_t content_length = 0; + { + const auto content_length_it = result.request.headers.find("content-length"); + if (content_length_it != result.request.headers.end()){ + const std::string& value = content_length_it->second; + if (value.empty() || value.size() > 10){ + error(400, "Malformed content-length."); + return result; + } + for (char c : value){ + if (c < '0' || c > '9'){ + error(400, "Malformed content-length."); + return result; + } + } + content_length = std::stoull(value); + if (content_length > MAX_BODY_BYTES){ + error(413, "Request body too large."); + return result; + } + } + } + + const auto connection_it = result.request.headers.find("connection"); + if (connection_it != result.request.headers.end() && + to_lower(connection_it->second).find("close") != std::string::npos + ){ + result.connection_close_requested = true; + } + + const auto expect_it = result.request.headers.find("expect"); + if (expect_it != result.request.headers.end() && + to_lower(expect_it->second).find("100-continue") != std::string::npos + ){ + result.expect_continue = true; + } + + const size_t consumed = header_end + 4 + (size_t)content_length; + if (buffer.size() < consumed){ + // NEED_BODY: the headers are complete and valid, the body has + // not fully arrived yet. In this state the buffer holds nothing + // but the bytes of this one request ("consumed" is at most a + // header section plus the body cap), so the buffer cannot exceed + // MAX_BUFFER_BYTES here; the overall cap is enforced when data + // is appended instead. + result.state = ParseState::NEED_BODY; + return result; + } + + result.state = ParseState::READY; + result.request.method = std::move(method); + const size_t query = target.find('?'); + result.request.path = target.substr(0, query); + if (query != std::string::npos){ + result.request.query = target.substr(query + 1); + } + result.request.body = buffer.substr(header_end + 4, content_length); + buffer.erase(0, consumed); + result.http_1_1 = http_1_1; + return result; +} + + +void HttpServer::end_connection( + const std::shared_ptr& conn, + AbstractServerSocket* socket +){ + conn->dead.store(true, std::memory_order_release); + conn->cv.notify_all(); + // Close the socket's connection only while it still belongs to this + // connection: by the time a task notices its own connection died, + // the socket may already be serving the next client, and closing + // unconditionally would kill that client's connection instead. + { + std::lock_guard lg(m_lock); + if (m_connection.get() != conn.get()){ + return; + } + } + socket->close_connection(); +} + + +bool HttpServer::write_raw( + const std::shared_ptr& conn, + AbstractServerSocket* socket, + const std::string& bytes +){ + // "conn->socket_id" is the socket's connection token when this + // connection was accepted: if the socket has moved on to the next + // client since then, the send is refused instead of being delivered + // into the new client's socket. + const size_t sent = socket->send(bytes.data(), bytes.size(), WRITE_TIMEOUT, conn->socket_id); + if (sent != bytes.size()){ + // The peer went away, or the write stalled: the connection is + // unusable either way. + end_connection(conn, socket); + return false; + } + return true; +} + + +bool HttpServer::write_response( + const std::shared_ptr& conn, + AbstractServerSocket* socket, + const Response& response, + bool keep_alive +){ + return write_raw(conn, socket, format_response(response, keep_alive)); +} + + + +} diff --git a/Common/Cpp/HttpServer/HttpServer.h b/Common/Cpp/HttpServer/HttpServer.h new file mode 100644 index 0000000000..a770ea88fb --- /dev/null +++ b/Common/Cpp/HttpServer/HttpServer.h @@ -0,0 +1,169 @@ +/* HTTP Server + * + * From: https://github.com/PokemonAutomation/ + * + * The "AbstractServerSocket" implementation of "AbstractHttpServer": a + * minimal HTTP/1.1 server on the platform's default server socket, for a + * single local client that issues one request at a time. The request/ + * response types, the handler contract, and the threading rules are in + * "AbstractHttpServer.h"; this file documents the implementation. + * + */ + +#ifndef PokemonAutomation_HttpServer_H +#define PokemonAutomation_HttpServer_H + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Concurrency/AsyncTask.h" +#include "Common/Cpp/Concurrency/ConditionVariable.h" +#include "Common/Cpp/Concurrency/Mutex.h" +#include "Common/Cpp/HttpServer/AbstractHttpServer.h" +#include "Common/Cpp/Sockets/AbstractServerSocket.h" + +namespace PokemonAutomation{ + +class ThreadPool; + + + +class HttpServer : public AbstractHttpServer, private AbstractServerSocket::Listener{ +public: + // The interface's types, re-exported under this class's name. + using AbstractHttpServer::Request; + using AbstractHttpServer::Response; + using AbstractHttpServer::HandlerContext; + using AbstractHttpServer::Handler; + using AbstractHttpServer::Listener; + + +public: + explicit HttpServer(ThreadPool& thread_pool); + ~HttpServer(); + + virtual void start(const std::string& address, uint16_t port, Handler handler) override; + virtual void stop() override; + virtual bool is_running() const override; + + +private: + // AbstractServerSocket::Listener. + virtual void on_listen_finished(const std::string& error_message) override; + virtual void on_connect() override; + virtual void on_connect_rejected() override; + virtual void on_receive_data(const void* data, size_t bytes) override; + virtual void on_disconnect(const std::string& error_message) override; + + +private: + // One request that is waiting to be parsed out of the buffer. + enum class ParseState{ + NEED_MORE, + NEED_BODY, + READY, + // Named "ERROR_RESPONSE" rather than "ERROR" because "ERROR" is + // a macro on Windows (winerror.h). + ERROR_RESPONSE, + }; + + struct ParsedRequest{ + ParseState state = ParseState::NEED_MORE; + + // ERROR_RESPONSE + int error_status = 400; + std::string error_reason; + std::vector> error_headers; + + // READY / NEED_BODY + Request request; + bool http_1_1 = true; + bool connection_close_requested = false; + bool expect_continue = false; + }; + + // Per-connection state. The connection task is the only thread that + // parses or writes; "on_receive_data"/"on_disconnect" only append + // data and flip flags. + struct Connection{ + Mutex lock; + ConditionVariable cv; + std::string buffer; + std::atomic dead{false}; + + // The server socket's connection_id() when this connection was + // accepted. Passed to every send(), so a write that outlives the + // connection is refused instead of reaching the next client. + uint64_t socket_id = 0; + }; + + // Runs on a thread from the thread pool, one instance per + // connection. + void connection_task( + const std::shared_ptr& conn, + AbstractServerSocket* socket + ); + + // Parses one complete request out of "buffer", removing it on + // success. Must be called with "conn->lock" held. + static ParsedRequest try_parse(std::string& buffer); + + // Marks the connection dead and closes it at the socket level. No-op + // if the socket is no longer connected to this client. + void end_connection( + const std::shared_ptr& conn, + AbstractServerSocket* socket + ); + + // Writes "bytes" to the connection completely; returns false (and + // ends the connection) if the peer went away or the write stalled. + bool write_raw( + const std::shared_ptr& conn, + AbstractServerSocket* socket, + const std::string& bytes + ); + + // Formats and writes the final response. + bool write_response( + const std::shared_ptr& conn, + AbstractServerSocket* socket, + const Response& response, + bool keep_alive + ); + + // The standard reason phrase for "status", or "Unknown Status". + static const char* status_reason(int status); + + // Builds the wire format of a response. + static std::string format_response(const Response& response, bool keep_alive); + + +private: + ThreadPool& m_thread_pool; + + // Guards m_socket, m_connection, and m_handler. Never held across a + // blocking wait, a task destruction, or a socket destruction. + mutable Mutex m_lock; + std::unique_ptr m_socket; + std::shared_ptr m_connection; + Handler m_handler; + + // Guards m_tasks. Never held while an AsyncTask destructor runs: + // the destructor blocks until the task finishes, and the task's + // final cleanup takes m_lock. + Mutex m_task_lock; + std::vector m_tasks; + + // Set by "stop()"; checked by the connection tasks between requests + // and by "HandlerContext". + std::atomic m_stop_requested{false}; +}; + + + +} +#endif diff --git a/Common/Cpp/HttpServer/HttpServer_Tests.cpp b/Common/Cpp/HttpServer/HttpServer_Tests.cpp new file mode 100644 index 0000000000..65b4ca31f9 --- /dev/null +++ b/Common/Cpp/HttpServer/HttpServer_Tests.cpp @@ -0,0 +1,1372 @@ +/* HTTP Server Tests + * + * From: https://github.com/PokemonAutomation/ + * + * Loopback tests that run a real "HttpServer" (the platform's default + * server socket backend) on 127.0.0.1 and drive it with raw + * "ClientSocket" clients. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Concurrency/AsyncTask.h" +#include "Common/Cpp/Concurrency/ConditionVariable.h" +#include "Common/Cpp/Concurrency/Mutex.h" +#include "Common/Cpp/Concurrency/ThreadPool.h" +#include "Common/Cpp/HttpServer/HttpServer.h" +#include "Common/Cpp/Sockets/ClientSocket.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "HttpServer_Tests.h" + +namespace PokemonAutomation{ + +namespace{ + + +struct Waiter{ + Mutex lock; + ConditionVariable cv; + + bool wait_for(std::chrono::milliseconds timeout, const std::function& pred){ + std::unique_lock lg(lock); + return cv.wait_for(lg, timeout, pred); + } + + void notify(){ + std::lock_guard lg(lock); + cv.notify_all(); + } +}; + + +// Long enough that a moderately loaded machine can make it, short enough +// that a real regression is still reported within the CI timeouts. +constexpr std::chrono::seconds HTTP_TEST_WAIT{60}; + + +// A handler gate: handlers notify "started" when they begin, and block on +// "wait_for_release()" until the test releases them. +struct HandlerGate{ + Waiter started; + + Mutex lock; + ConditionVariable cv; + std::atomic released{false}; + + void wait_for_release(std::chrono::milliseconds timeout){ + std::unique_lock lg(lock); + cv.wait_for(lg, timeout, [this]{ + return released.load(std::memory_order_relaxed); + }); + } + + void release(){ + released.store(true, std::memory_order_release); + std::lock_guard lg(lock); + cv.notify_all(); + } +}; + + +// A raw TCP client that accumulates everything it receives. +// "received" and the error strings are protected by "waiter.lock": the +// socket's receive thread writes them, the test thread reads them. +struct TestClient{ + Waiter waiter; + std::string received; + std::atomic connected{0}; + std::atomic disconnected{0}; + std::string connect_error; + std::string disconnect_error; + + struct Listener : AbstractClientSocket::Listener{ + TestClient* client; + + virtual void on_connect_finished(const std::string& error_message) override{ + { + std::lock_guard lg(client->waiter.lock); + client->connect_error = error_message; + } + client->connected++; + client->waiter.notify(); + } + + virtual void on_receive_data(const void* data, size_t bytes) override{ + { + std::lock_guard lg(client->waiter.lock); + client->received.append((const char*)data, bytes); + } + client->waiter.notify(); + } + + virtual void on_disconnect(const std::string& error_message) override{ + { + std::lock_guard lg(client->waiter.lock); + client->disconnect_error = error_message; + } + client->disconnected++; + client->waiter.notify(); + } + } listener_impl; + + ClientSocket socket; + + explicit TestClient(ThreadPool& thread_pool) + : socket(thread_pool) + { + listener_impl.client = this; + socket.add_listener(listener_impl); + } + + bool connect(uint16_t port){ + socket.connect("127.0.0.1", port); + return waiter.wait_for( + HTTP_TEST_WAIT, + [this]{ return connected.load() >= 1; } + ); + } + + bool send_all(const std::string& bytes){ + size_t total = 0; + while (total < bytes.size()){ + const size_t sent = socket.send(bytes.data() + total, bytes.size() - total); + if (sent == 0){ + return false; + } + total += sent; + } + return true; + } + + bool wait_for_bytes(size_t n){ + // The predicate runs with "waiter.lock" held, so it may read + // "received" directly. + return waiter.wait_for( + HTTP_TEST_WAIT, + [this, n]{ return received.size() >= n; } + ); + } + + // A consistent copy of everything received so far, for assertions. + std::string received_snapshot(){ + std::lock_guard lg(waiter.lock); + return received; + } +}; + + +struct TestServerListener : HttpServer::Listener{ + Waiter waiter; + std::atomic listen_finished{0}; + // Protected by "waiter.lock" (written by the socket's thread). + std::string last_listen_error; + std::atomic connected{0}; + std::atomic rejected{0}; + std::atomic disconnected{0}; + + virtual void on_listen_finished(const std::string& error_message) override{ + { + std::lock_guard lg(waiter.lock); + last_listen_error = error_message; + } + listen_finished++; + waiter.notify(); + } + + std::string last_listen_error_snapshot(){ + std::lock_guard lg(waiter.lock); + return last_listen_error; + } + + virtual void on_connect() override{ + connected++; + waiter.notify(); + } + + virtual void on_connect_rejected() override{ + rejected++; + waiter.notify(); + } + + virtual void on_disconnect(const std::string& error_message) override{ + disconnected++; + waiter.notify(); + } +}; + + +class TestHttpServer : public UnitTest{ +public: + TestHttpServer(ThreadPool& thread_pool, std::string name) + : UnitTest(std::move(name)) + , m_thread_pool(thread_pool) + { + m_threads = 2; + } + + +protected: + // Starts the server with "handler" on a free loopback port, waiting + // for the listen to finish. "handler" is copied on each attempt. + bool start_server(HttpServer& server, TestServerListener& listener, HttpServer::Handler handler, uint16_t& port) const{ + for (int attempt = 0; attempt < 8; attempt++){ + uint16_t candidate; + { + std::lock_guard lg(m_rng_lock); + candidate = (uint16_t)(40000 + m_rng() % 20000); + } + const int finished_before = listener.listen_finished.load(); + server.start("127.0.0.1", candidate, handler); + if (!listener.waiter.wait_for( + HTTP_TEST_WAIT, + [finished_before, &listener]{ + return listener.listen_finished.load() > finished_before; + } + )){ + server.stop(); + continue; + } + if (listener.last_listen_error_snapshot().empty()){ + port = candidate; + return true; + } + // A failed listen leaves the server "started but not + // listening"; stop() cleans that up before the next attempt. + server.stop(); + } + return false; + } + +protected: + ThreadPool& m_thread_pool; + +private: + mutable Mutex m_rng_lock; + mutable std::mt19937 m_rng{std::random_device{}()}; +}; + + +// A POST request is parsed (method, path, query, headers, body) and the +// response carries the extra headers and the exact body; the connection +// stays keep-alive. +class Test_Http_PostEcho : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + auto handler = [](const HttpServer::Request& request, HttpServer::Response& response, HttpServer::HandlerContext&){ + response.status = 200; + response.headers.push_back({"X-Test", "yes"}); + response.body = request.path + "|" + request.query + "|" + request.header("X-Client") + "|" + request.body; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else if (!server_listener.waiter.wait_for( + HTTP_TEST_WAIT, + [&server_listener]{ return server_listener.connected.load() >= 1; } + )){ + error = "The server did not call on_connect"; + }else if (!client.send_all( + "POST /mcp?session=abc HTTP/1.1\r\n" + "Host: 127.0.0.1\r\n" + "X-Client: unit-test\r\n" + "Content-Length: 5\r\n" + "\r\n" + "hello" + )){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 200 OK\r\n" + "X-Test: yes\r\n" + "Content-Length: 32\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "/mcp|session=abc|unit-test|hello"; + if (!client.wait_for_bytes(expected.size())){ + error = "The client did not receive the full response"; + }else if (client.received_snapshot() != expected){ + error = "Bad response:\n" + client.received_snapshot(); + } + } + // The client goes out of scope here and closes the socket. + } + + if (error.empty() && + !server_listener.waiter.wait_for( + HTTP_TEST_WAIT, + [&server_listener]{ return server_listener.disconnected.load() >= 1; } + ) + ){ + error = "The server did not call on_disconnect"; + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// Two requests sent back to back on one connection (the second is already +// buffered while the first is being answered): both must be answered, in +// order. +class Test_Http_PipelinedKeepAlive : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + auto handler = [](const HttpServer::Request& request, HttpServer::Response& response, HttpServer::HandlerContext&){ + response.status = 200; + response.body = "r" + request.body; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else if (!client.send_all( + "POST /a HTTP/1.1\r\n" + "Content-Length: 3\r\n" + "\r\n" + "one" + "POST /b HTTP/1.1\r\n" + "Content-Length: 3\r\n" + "\r\n" + "two" + )){ + error = "The client did not send the requests"; + }else{ + const std::string expected = + "HTTP/1.1 200 OK\r\n" + "Content-Length: 4\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "rone" + "HTTP/1.1 200 OK\r\n" + "Content-Length: 4\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "rtwo"; + if (!client.wait_for_bytes(expected.size())){ + error = "The client did not receive both responses (got " + std::to_string(client.received_snapshot().size()) + " of " + std::to_string(expected.size()) + " bytes)"; + }else if (client.received_snapshot() != expected){ + error = "Bad response:\n" + client.received_snapshot(); + } + } + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// "Connection: close" in the request: the response says so, the server +// closes the connection, and it keeps serving new clients. +class Test_Http_ConnectionClose : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + auto handler = [](const HttpServer::Request&, HttpServer::Response& response, HttpServer::HandlerContext&){ + response.status = 200; + response.body = "done"; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Content-Length: 1\r\n" + "Connection: close\r\n" + "\r\n" + "x" + )){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 200 OK\r\n" + "Content-Length: 4\r\n" + "Connection: close\r\n" + "\r\n" + "done"; + if (!client.wait_for_bytes(expected.size())){ + error = "The client did not receive the full response"; + }else if (client.received_snapshot() != expected){ + error = "Bad response:\n" + client.received_snapshot(); + }else if (!client.waiter.wait_for( + HTTP_TEST_WAIT, + [&client]{ return client.disconnected.load() >= 1; } + )){ + error = "The server did not close the connection"; + } + } + } + + if (error.empty()){ + // The server must keep listening for a new client. + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not reconnect"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Content-Length: 1\r\n" + "\r\n" + "y" + )){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 200 OK\r\n" + "Content-Length: 4\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "done"; + if (!client.wait_for_bytes(expected.size()) || client.received_snapshot() != expected){ + error = "The reconnected client did not get a good response:\n" + client.received_snapshot(); + } + } + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// A request line that is not "METHOD SP TARGET SP VERSION" gets a 400 and +// a closed connection; the server recovers and serves a new client. +class Test_Http_BadRequest : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + auto handler = [](const HttpServer::Request&, HttpServer::Response& response, HttpServer::HandlerContext&){ + response.status = 200; + response.body = "ok"; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else if (!client.send_all("GARBAGE\r\n\r\n")){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 400 Bad Request\r\n" + "Content-Length: 19\r\n" + "Connection: close\r\n" + "\r\n" + "Malformed request.\n"; + if (!client.wait_for_bytes(expected.size()) || client.received_snapshot() != expected){ + error = "Bad error response:\n" + client.received_snapshot(); + } + } + } + + if (error.empty()){ + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not reconnect"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Content-Length: 0\r\n" + "\r\n" + )){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 200 OK\r\n" + "Content-Length: 2\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "ok"; + if (!client.wait_for_bytes(expected.size()) || client.received_snapshot() != expected){ + error = "The server did not recover:\n" + client.received_snapshot(); + } + } + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// Chunked request bodies are not supported: 501 and a closed connection, +// and the handler is never called. +class Test_Http_ChunkedRejected : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + std::atomic handler_called{false}; + auto handler = [&handler_called](const HttpServer::Request&, HttpServer::Response& response, HttpServer::HandlerContext&){ + handler_called = true; + response.status = 200; + response.body = "ok"; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Transfer-Encoding: chunked\r\n" + "\r\n" + )){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 501 Not Implemented\r\n" + "Content-Length: 44\r\n" + "Connection: close\r\n" + "\r\n" + "Chunked transfer encoding is not supported.\n"; + if (!client.wait_for_bytes(expected.size()) || client.received_snapshot() != expected){ + error = "Bad error response:\n" + client.received_snapshot(); + } + } + } + + if (error.empty() && handler_called.load()){ + error = "The handler was called for a rejected request"; + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// A content-length above the body limit is rejected with 413 before the +// (never sent) body arrives, and the handler is never called. +class Test_Http_BodyTooLarge : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + std::atomic handler_called{false}; + auto handler = [&handler_called](const HttpServer::Request&, HttpServer::Response& response, HttpServer::HandlerContext&){ + handler_called = true; + response.status = 200; + response.body = "ok"; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Content-Length: 2097152\r\n" + "\r\n" + )){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 413 Payload Too Large\r\n" + "Content-Length: 24\r\n" + "Connection: close\r\n" + "\r\n" + "Request body too large.\n"; + if (!client.wait_for_bytes(expected.size()) || client.received_snapshot() != expected){ + error = "Bad error response:\n" + client.received_snapshot(); + } + } + } + + if (error.empty() && handler_called.load()){ + error = "The handler was called for a rejected request"; + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// A complete header section over the size limit is rejected with 400, +// even though it arrives in one piece (the in-progress limit check only +// fires while the terminator is still missing). +class Test_Http_HeadersTooLarge : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + std::atomic handler_called{false}; + auto handler = [&handler_called](const HttpServer::Request&, HttpServer::Response& response, HttpServer::HandlerContext&){ + handler_called = true; + response.status = 200; + response.body = "ok"; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + // 50 headers of about 1.5 KiB each: about 75 KiB of headers - + // under the 100-header count limit, over the 64 KiB size limit. + std::string request = "POST / HTTP/1.1\r\n"; + for (int i = 0; i < 50; i++){ + request += "X-Filler-"; + request += std::to_string(i); + request += ": "; + request += std::string(1490, 'a'); + request += "\r\n"; + } + request += "\r\n"; + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else if (!client.send_all(request)){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 400 Bad Request\r\n" + "Content-Length: 19\r\n" + "Connection: close\r\n" + "\r\n" + "Malformed request.\n"; + if (!client.wait_for_bytes(expected.size()) || client.received_snapshot() != expected){ + error = "Bad error response:\n" + client.received_snapshot(); + } + } + } + + if (error.empty() && handler_called.load()){ + error = "The handler was called for a rejected request"; + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// A connection that sends more data than the receive buffer cap (about +// 2 MiB) is closed without a response, and the server keeps listening. +class Test_Http_BufferCap : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + std::atomic handler_calls{0}; + auto handler = [&handler_calls](const HttpServer::Request&, HttpServer::Response& response, HttpServer::HandlerContext&){ + handler_calls++; + response.status = 200; + response.body = "ok"; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else{ + // A never-completing request far larger than the buffer + // cap: the server closes the connection without a + // response. (The send may fail partway through once the + // server has hung up; that is expected.) + const std::string flood(3u << 20, 'A'); + client.send_all(flood); + if (!client.waiter.wait_for( + HTTP_TEST_WAIT, + [&client]{ return client.disconnected.load() >= 1; } + )){ + error = "The server did not close the over-cap connection"; + } + } + } + + if (error.empty() && handler_calls.load() != 0){ + error = "The handler was called for a capped connection"; + } + + if (error.empty()){ + // The server must still be listening afterwards. + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect after the cap test"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Content-Length: 0\r\n" + "\r\n" + )){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 200 OK\r\n" + "Content-Length: 2\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "ok"; + if (!client.wait_for_bytes(expected.size()) || client.received_snapshot() != expected){ + error = "The server did not answer after the cap test:\n" + client.received_snapshot(); + } + } + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// "Expect: 100-continue" is answered with "100 Continue" as soon as the +// headers are validated - before the body arrives, which is what a +// client waiting for the interim response is waiting for - and the +// final response carries the body. +class Test_Http_ExpectContinue : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + auto handler = [](const HttpServer::Request& request, HttpServer::Response& response, HttpServer::HandlerContext&){ + response.status = 200; + response.body = request.body; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Content-Length: 5\r\n" + "Expect: 100-continue\r\n" + "\r\n" + )){ + error = "The client did not send the headers"; + }else{ + const std::string continue_response = "HTTP/1.1 100 Continue\r\n\r\n"; + if (!client.wait_for_bytes(continue_response.size()) + || client.received_snapshot().compare(0, continue_response.size(), continue_response) != 0 + ){ + error = "The server did not answer the interim response:\n" + client.received_snapshot(); + }else if (!client.send_all("hello")){ + error = "The client did not send the body"; + }else{ + const std::string expected = + "HTTP/1.1 100 Continue\r\n\r\n" + "HTTP/1.1 200 OK\r\n" + "Content-Length: 5\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "hello"; + if (!client.wait_for_bytes(expected.size()) || client.received_snapshot() != expected){ + error = "Bad response:\n" + client.received_snapshot(); + } + } + } + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// The client disconnects while the handler is still running: the handler +// sees it via "HandlerContext::connection_alive()", its response is +// discarded, and the server keeps serving new clients. +class Test_Http_DisconnectDuringHandler : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + HandlerGate gate; + std::atomic call_count{0}; + std::atomic first_call_alive{false}; + std::atomic first_call_saw_disconnect{false}; + + auto handler = [&gate, &call_count, &first_call_alive, &first_call_saw_disconnect]( + const HttpServer::Request&, + HttpServer::Response& response, + HttpServer::HandlerContext& context + ){ + const int call = call_count.fetch_add(1) + 1; + if (call == 1){ + first_call_alive = context.connection_alive(); + gate.started.notify(); + gate.wait_for_release(HTTP_TEST_WAIT); + first_call_saw_disconnect = !context.connection_alive(); + } + response.status = 200; + response.body = "ok"; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Content-Length: 1\r\n" + "\r\n" + "x" + )){ + error = "The client did not send the request"; + }else if (!gate.started.wait_for( + HTTP_TEST_WAIT, + [&first_call_alive]{ return first_call_alive.load(); } + )){ + error = "The handler did not start"; + } + // The client goes out of scope here and closes the socket, + // while the handler is still blocked on the gate. + } + + if (error.empty() && + !server_listener.waiter.wait_for( + HTTP_TEST_WAIT, + [&server_listener]{ return server_listener.disconnected.load() >= 1; } + ) + ){ + error = "The server did not call on_disconnect"; + } + + if (error.empty()){ + gate.release(); + } + + if (error.empty()){ + // The server must keep serving new clients. + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not reconnect"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Content-Length: 1\r\n" + "\r\n" + "y" + )){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 200 OK\r\n" + "Content-Length: 2\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "ok"; + if (!client.wait_for_bytes(expected.size()) || client.received_snapshot() != expected){ + error = "The server did not recover:\n" + client.received_snapshot(); + } + } + } + + if (error.empty()){ + // Wait for the first handler to have recorded its observation. + if (!gate.started.wait_for( + HTTP_TEST_WAIT, + [&first_call_saw_disconnect, &call_count]{ + return call_count.load() >= 1 && first_call_saw_disconnect.load(); + } + )){ + error = "The handler did not record the disconnect"; + } + } + + if (error.empty()){ + if (!first_call_alive.load()){ + error = "The handler did not see the connection as alive on entry"; + }else if (!first_call_saw_disconnect.load()){ + error = "The handler did not see the disconnect via connection_alive()"; + } + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// The client disconnects while the handler is still running, and a new +// client connects before the handler returns: the old task must discard +// its response instead of writing it into the new client's socket. +class Test_Http_ReconnectWhileHandlerRunning : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + HandlerGate gate; + std::atomic first_call_alive{false}; + + auto handler = [&gate, &first_call_alive]( + const HttpServer::Request& request, + HttpServer::Response& response, + HttpServer::HandlerContext& context + ){ + if (request.path == "/a"){ + first_call_alive = context.connection_alive(); + gate.started.notify(); + gate.wait_for_release(HTTP_TEST_WAIT); + } + response.status = 200; + response.body = (request.path == "/a") ? "A" : "ok"; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + TestClient client_a(m_thread_pool); + TestClient client_b(m_thread_pool); + { + if (!client_a.connect(port)){ + error = "The first client did not connect"; + }else if (!client_a.send_all( + "POST /a HTTP/1.1\r\n" + "Content-Length: 1\r\n" + "\r\n" + "x" + )){ + error = "The first client did not send the request"; + }else if (!gate.started.wait_for( + HTTP_TEST_WAIT, + [&first_call_alive]{ return first_call_alive.load(); } + )){ + error = "The handler did not start"; + } + } + + if (error.empty()){ + // Disconnect the first client (closing its socket while it is + // still in scope, so its connection task keeps running) and + // wait for the server to report the disconnect. + client_a.socket.close(); + if (!server_listener.waiter.wait_for( + HTTP_TEST_WAIT, + [&server_listener]{ return server_listener.disconnected.load() >= 1; } + )){ + error = "The server did not report the first client's disconnect"; + } + } + + if (error.empty()){ + // Connect the second client while the first connection's + // handler is still blocked on the gate. Waiting for the + // second on_connect guarantees the second connection has + // replaced the first one in the server by the time the gate + // is released. + if (!client_b.connect(port)){ + error = "The second client did not connect"; + }else if (!server_listener.waiter.wait_for( + HTTP_TEST_WAIT, + [&server_listener]{ return server_listener.connected.load() >= 2; } + )){ + error = "The server did not accept the second client"; + } + } + + if (error.empty()){ + gate.release(); + } + + if (error.empty()){ + // The second client's first bytes must be its own response: + // the first client's late response was discarded, not + // written into this socket. + if (!client_b.send_all( + "POST /b HTTP/1.1\r\n" + "Content-Length: 1\r\n" + "\r\n" + "y" + )){ + error = "The second client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 200 OK\r\n" + "Content-Length: 2\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "ok"; + if (!client_b.wait_for_bytes(expected.size()) || client_b.received_snapshot() != expected){ + error = "The second client received a stray response:\n" + client_b.received_snapshot(); + } + } + } + + if (error.empty() && !first_call_alive.load()){ + error = "The handler did not see the connection as alive on entry"; + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// "stop()" while the handler is still running blocks until the handler +// returns, and "start()" works afterwards. +class Test_Http_StopDuringHandler : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + HandlerGate gate; + std::atomic handler_started{false}; + std::atomic saw_disconnect_on_stop{false}; + + auto handler = [&gate, &handler_started, &saw_disconnect_on_stop]( + const HttpServer::Request&, + HttpServer::Response& response, + HttpServer::HandlerContext& context + ){ + handler_started = true; + gate.started.notify(); + gate.wait_for_release(HTTP_TEST_WAIT); + // Then stay alive while the connection is alive: the pattern + // a long-running tool call uses. stop() marks the connection + // dead before it waits for this handler (the socket's + // close_connection() close suppresses the disconnect + // callback), so the loop must terminate - without that mark + // this would spin forever and stop() would hang. + while (context.connection_alive()){ + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + saw_disconnect_on_stop = !context.connection_alive(); + response.status = 200; + response.body = "late"; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Content-Length: 1\r\n" + "\r\n" + "x" + )){ + error = "The client did not send the request"; + }else if (!gate.started.wait_for( + HTTP_TEST_WAIT, + [&handler_started]{ return handler_started.load(); } + )){ + error = "The handler did not start"; + } + } + + if (error.empty()){ + // stop() blocks until the in-flight handler returns. Release + // the handler from a pool thread so this thread can call it. + // The handle is kept (dispatch is [[nodiscard]]); its + // destructor waits for the trivial release task to finish. + AsyncTask releaser_task = m_thread_pool.dispatch([&gate]{ + gate.release(); + }); + server.stop(); + if (server.is_running()){ + error = "The server is still running after stop()"; + } + } + + if (error.empty() && !saw_disconnect_on_stop.load()){ + error = "The handler did not see the connection as gone after stop()"; + } + + if (error.empty()){ + // Start again (on a fresh port) and serve a fresh request. + auto simple_handler = [](const HttpServer::Request&, HttpServer::Response& response, HttpServer::HandlerContext&){ + response.status = 200; + response.body = "ok"; + }; + if (!start_server(server, server_listener, simple_handler, port)){ + error = "Failed to restart the server"; + }else{ + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect to the restarted server"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Content-Length: 0\r\n" + "\r\n" + )){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 200 OK\r\n" + "Content-Length: 2\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "ok"; + if (!client.wait_for_bytes(expected.size()) || client.received_snapshot() != expected){ + error = "The restarted server did not answer:\n" + client.received_snapshot(); + } + } + } + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// While one client is connected, a second one is rejected. +class Test_Http_RejectSecondClient : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + auto handler = [](const HttpServer::Request&, HttpServer::Response& response, HttpServer::HandlerContext&){ + response.status = 200; + response.body = "ok"; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient first(m_thread_pool); + if (!first.connect(port)){ + error = "The first client did not connect"; + }else if (!server_listener.waiter.wait_for( + HTTP_TEST_WAIT, + [&server_listener]{ return server_listener.connected.load() >= 1; } + )){ + error = "The server did not call on_connect"; + }else{ + TestClient second(m_thread_pool); + second.connect(port); // May complete at the TCP level; the server closes it. + if (!server_listener.waiter.wait_for( + HTTP_TEST_WAIT, + [&server_listener]{ return server_listener.rejected.load() >= 1; } + )){ + error = "The server did not call on_connect_rejected"; + } + } + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + + +} // anonymous namespace + + +void add_tests_HttpServer(UnitTestDatabase& database, ThreadPool& thread_pool){ + database.add( + thread_pool, "HttpServer - POST request/response with headers, query and body" + ); + database.add( + thread_pool, "HttpServer - Two pipelined requests on one keep-alive connection" + ); + database.add( + thread_pool, "HttpServer - Connection: close and reconnection" + ); + database.add( + thread_pool, "HttpServer - Malformed request line" + ); + database.add( + thread_pool, "HttpServer - Chunked request bodies are rejected" + ); + database.add( + thread_pool, "HttpServer - Oversized content-length is rejected" + ); + database.add( + thread_pool, "HttpServer - An oversized header section is rejected" + ); + database.add( + thread_pool, "HttpServer - A connection over the buffer cap is closed" + ); + database.add( + thread_pool, "HttpServer - Expect: 100-continue" + ); + database.add( + thread_pool, "HttpServer - Disconnect while the handler is running" + ); + database.add( + thread_pool, "HttpServer - Reconnect while the previous handler is still running" + ); + database.add( + thread_pool, "HttpServer - stop() while the handler is running" + ); + database.add( + thread_pool, "HttpServer - A second client is rejected" + ); +} + + + +} diff --git a/Common/Cpp/HttpServer/HttpServer_Tests.h b/Common/Cpp/HttpServer/HttpServer_Tests.h new file mode 100644 index 0000000000..6fe6c25cb1 --- /dev/null +++ b/Common/Cpp/HttpServer/HttpServer_Tests.h @@ -0,0 +1,28 @@ +/* HTTP Server Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_HttpServer_Tests_H +#define PokemonAutomation_HttpServer_Tests_H + +namespace PokemonAutomation{ + +class ThreadPool; +class UnitTestDatabase; + + +// Loopback tests that run a real "HttpServer" (the platform's default +// server socket backend) on 127.0.0.1 and drive it with raw "ClientSocket" +// clients: request/response parsing, keep-alive, pipelining, +// "Connection: close", "Expect: 100-continue", error responses, and the +// lifecycle (disconnect during a handler, "stop()" during a handler, +// restart, second-client rejection). `thread_pool` runs the server and +// the test workers; it must be a long-lived pool (e.g. +// `GlobalThreadPools::unlimited_normal()`). +void add_tests_HttpServer(UnitTestDatabase& database, ThreadPool& thread_pool); + + +} +#endif diff --git a/Common/Cpp/Sockets/AbstractServerSocket.h b/Common/Cpp/Sockets/AbstractServerSocket.h index 4139a0f120..1c2da259ef 100644 --- a/Common/Cpp/Sockets/AbstractServerSocket.h +++ b/Common/Cpp/Sockets/AbstractServerSocket.h @@ -13,6 +13,7 @@ #define PokemonAutomation_Sockets_AbstractServerSocket_H #include +#include #include #include "Common/Cpp/ListenerSet.h" @@ -58,6 +59,18 @@ class AbstractServerSocket{ return m_state.load(std::memory_order_relaxed); } + // A token identifying the connection the socket is currently + // serving. It changes every time a new client is accepted, and it + // is 0 when no connection is active. Capture it when a write + // starts and pass it to "send()", so that a write that outlives its + // connection (the peer disconnected and a new client was accepted + // in the meantime) refuses to reach the new client's socket. + // Implementations keep the underlying counter monotonic (never + // reset it when a connection closes, or a queued write for the old + // connection could match the next connection's id); 0 is only the + // "no connection is active" report, never a counter value. + virtual uint64_t connection_id() = 0; + // Binds to "address" and listens on "port". // Use "0.0.0.0" to accept connections on all interfaces. virtual void start(const std::string& address, uint16_t port) = 0; @@ -69,7 +82,10 @@ class AbstractServerSocket{ virtual size_t send( const void* data, size_t bytes, - std::chrono::milliseconds timeout = std::chrono::milliseconds::max() + std::chrono::milliseconds timeout = std::chrono::milliseconds::max(), + // The connection_id() captured when this write started. 0 skips + // the check: the call applies to whatever connection is active. + uint64_t connection = 0 ) = 0; diff --git a/Common/Cpp/Sockets/ServerSocket_POSIX.cpp b/Common/Cpp/Sockets/ServerSocket_POSIX.cpp index 75b2a14ead..ad38549eb6 100644 --- a/Common/Cpp/Sockets/ServerSocket_POSIX.cpp +++ b/Common/Cpp/Sockets/ServerSocket_POSIX.cpp @@ -83,6 +83,14 @@ void ServerSocket_POSIX::start(const std::string& address, uint16_t port){ } } +uint64_t ServerSocket_POSIX::connection_id(){ + std::lock_guard lg(m_lock); + return m_state.load(std::memory_order_relaxed) == State::CONNECTED + ? m_connection_id + : 0; +} + + void ServerSocket_POSIX::close_connection(){ int socket; { @@ -159,16 +167,16 @@ void ServerSocket_POSIX::close_and_wait() noexcept{ } } -size_t ServerSocket_POSIX::send(const void* data, size_t bytes, std::chrono::milliseconds timeout){ +size_t ServerSocket_POSIX::send(const void* data, size_t bytes, std::chrono::milliseconds timeout, uint64_t connection){ int socket; { std::lock_guard lg(m_lock); - socket = m_state.load(std::memory_order_relaxed) == State::CONNECTED - ? m_client - : -1; - } - if (socket == -1){ - return 0; + if (m_state.load(std::memory_order_relaxed) != State::CONNECTED + || (connection != 0 && m_connection_id != connection) + ){ + return 0; + } + socket = m_client; } const char* ptr = (const char*)data; @@ -184,7 +192,12 @@ size_t ServerSocket_POSIX::send(const void* data, size_t bytes, std::chrono::mil while (bytes > 0){ { std::lock_guard lg(m_lock); - if (m_state.load(std::memory_order_relaxed) != State::CONNECTED){ + // The connection may have been replaced (or closed) since the + // last iteration: in that case this write must stop rather + // than reach the new client's socket. + if (m_state.load(std::memory_order_relaxed) != State::CONNECTED + || (connection != 0 && m_connection_id != connection) + ){ return sent; } } @@ -359,6 +372,7 @@ void ServerSocket_POSIX::accept_thread_loop(std::string address, uint16_t port, fcntl(client_socket, F_SETFL, O_NONBLOCK); m_client = client_socket; m_close_requested = false; + m_connection_id++; m_state.store(State::CONNECTED, std::memory_order_release); accepted = true; } diff --git a/Common/Cpp/Sockets/ServerSocket_POSIX.h b/Common/Cpp/Sockets/ServerSocket_POSIX.h index 6786a631fc..f08b83ca68 100644 --- a/Common/Cpp/Sockets/ServerSocket_POSIX.h +++ b/Common/Cpp/Sockets/ServerSocket_POSIX.h @@ -42,9 +42,15 @@ class ServerSocket_POSIX final : public AbstractServerSocket{ virtual size_t send( const void* data, size_t bytes, - std::chrono::milliseconds timeout = std::chrono::milliseconds::max() + std::chrono::milliseconds timeout = std::chrono::milliseconds::max(), + uint64_t connection = 0 ) override; + // A token identifying the connection the socket is currently + // serving. It changes every time a new client is accepted, and it + // is 0 when no connection is active. + virtual uint64_t connection_id() override; + private: @@ -87,6 +93,12 @@ class ServerSocket_POSIX final : public AbstractServerSocket{ int m_client = -1; bool m_close_requested = false; + // A connection token, incremented (under m_lock) on every accept. + // send() compares it against the token the caller captured, so a + // write that outlived its connection cannot reach the client that + // replaced it. 0 means no connection is active. + uint64_t m_connection_id = 0; + // One task per start() call. start() must never destroy the previous // task's AsyncTask here: its destructor blocks until the task has // finished, and a task from an earlier attempt may still be running diff --git a/Common/Cpp/Sockets/ServerSocket_Qt.cpp b/Common/Cpp/Sockets/ServerSocket_Qt.cpp index fd48c72001..359a45b1f1 100644 --- a/Common/Cpp/Sockets/ServerSocket_Qt.cpp +++ b/Common/Cpp/Sockets/ServerSocket_Qt.cpp @@ -174,13 +174,16 @@ void ServerSocket_Qt::close_and_wait() noexcept{ m_close_requested = false; } -size_t ServerSocket_Qt::send(const void* data, size_t bytes, std::chrono::milliseconds timeout){ +size_t ServerSocket_Qt::send(const void* data, size_t bytes, std::chrono::milliseconds timeout, uint64_t connection){ QTcpSocket* socket; { std::lock_guard lg(m_lock); - socket = m_state.load(std::memory_order_relaxed) == State::CONNECTED - ? m_socket - : nullptr; + if (m_state.load(std::memory_order_relaxed) != State::CONNECTED + || (connection != 0 && m_connection_id != connection) + ){ + return 0; + } + socket = m_socket; } if (socket == nullptr){ return 0; @@ -188,6 +191,7 @@ size_t ServerSocket_Qt::send(const void* data, size_t bytes, std::chrono::millis QSharedPointer send_data = QSharedPointer::create(); send_data->packet.assign((const char*)data, bytes); + send_data->connection = connection; // "time_point::max()" (the infinite default) is never reached; using // it also avoids overflowing "now() + timeout". send_data->deadline = @@ -213,7 +217,8 @@ size_t ServerSocket_Qt::send(const void* data, size_t bytes, std::chrono::millis std::lock_guard lg(m_lock); registered = m_state.load(std::memory_order_relaxed) == State::CONNECTED - && m_socket != nullptr; + && m_socket != nullptr + && (connection == 0 || m_connection_id == connection); if (registered){ m_inflight_sends.push_back(send_data); } @@ -310,6 +315,7 @@ void ServerSocket_Qt::on_new_connection(){ }else{ m_socket = sock; m_close_requested = false; + m_connection_id++; m_state.store(State::CONNECTED, std::memory_order_release); accepted = true; rejected = false; @@ -434,11 +440,28 @@ void ServerSocket_Qt::on_close_connection(QSharedPointer comple completion->cv.notify_all(); } +uint64_t ServerSocket_Qt::connection_id(){ + std::lock_guard lg(m_lock); + return m_state.load(std::memory_order_relaxed) == State::CONNECTED + ? m_connection_id + : 0; +} + + void ServerSocket_Qt::on_send(QSharedPointer send_data){ QTcpSocket* sock; { std::lock_guard lg(m_lock); - sock = m_socket; + if (send_data->connection != 0 + && m_connection_id != send_data->connection + ){ + // The connection this send was queued for was replaced (or + // torn down) while it was waiting in the queue: the write + // loop must not reach into the new connection's socket. + sock = nullptr; + }else{ + sock = m_socket; + } } { diff --git a/Common/Cpp/Sockets/ServerSocket_Qt.h b/Common/Cpp/Sockets/ServerSocket_Qt.h index 0bcf70c0a6..370152e98c 100644 --- a/Common/Cpp/Sockets/ServerSocket_Qt.h +++ b/Common/Cpp/Sockets/ServerSocket_Qt.h @@ -54,6 +54,12 @@ class ServerSocket_Qt final : public QObject, public AbstractServerSocket{ // "close_and_wait()"/"close_connection()" cancel the send. bool done = false; + // The connection_id() the send was queued for; 0 skips the + // check. "on_send" refuses to write if the connection was + // replaced while the send was queued, so it cannot reach the + // new client's socket. + uint64_t connection = 0; + Mutex lock; ConditionVariable cv; }; @@ -86,9 +92,15 @@ class ServerSocket_Qt final : public QObject, public AbstractServerSocket{ virtual size_t send( const void* data, size_t bytes, - std::chrono::milliseconds timeout = std::chrono::milliseconds::max() + std::chrono::milliseconds timeout = std::chrono::milliseconds::max(), + uint64_t connection = 0 ) override; + // A token identifying the connection the socket is currently + // serving. It changes every time a new client is accepted, and it + // is 0 when no connection is active. + virtual uint64_t connection_id() override; + signals: void internal_send(QSharedPointer send_data); @@ -142,6 +154,12 @@ class ServerSocket_Qt final : public QObject, public AbstractServerSocket{ bool m_close_requested = false; bool m_starting = false; + // A connection token, incremented (under m_lock) on every accept. + // "send()" compares it against the token the caller captured, so a + // write that outlived its connection cannot reach the client that + // replaced it. 0 means no connection is active. + uint64_t m_connection_id = 0; + // The "send()" calls that are queued on the event thread but not // finished yet, so that "close_and_wait()"/"close_connection()" can // cancel diff --git a/Common/Cpp/Sockets/ServerSocket_WinSocket.cpp b/Common/Cpp/Sockets/ServerSocket_WinSocket.cpp index 55889f4b38..066a76ab24 100644 --- a/Common/Cpp/Sockets/ServerSocket_WinSocket.cpp +++ b/Common/Cpp/Sockets/ServerSocket_WinSocket.cpp @@ -94,6 +94,14 @@ void ServerSocket_WinSocket::start(const std::string& address, uint16_t port){ } } +uint64_t ServerSocket_WinSocket::connection_id(){ + std::lock_guard lg(m_lock); + return m_state.load(std::memory_order_relaxed) == State::CONNECTED + ? m_connection_id + : 0; +} + + void ServerSocket_WinSocket::close_connection(){ SOCKET socket; { @@ -170,13 +178,16 @@ void ServerSocket_WinSocket::close_and_wait() noexcept{ } } -size_t ServerSocket_WinSocket::send(const void* data, size_t bytes, std::chrono::milliseconds timeout){ +size_t ServerSocket_WinSocket::send(const void* data, size_t bytes, std::chrono::milliseconds timeout, uint64_t connection){ SOCKET socket; { std::lock_guard lg(m_lock); - socket = m_state.load(std::memory_order_relaxed) == State::CONNECTED - ? m_client - : INVALID_SOCKET; + if (m_state.load(std::memory_order_relaxed) != State::CONNECTED + || (connection != 0 && m_connection_id != connection) + ){ + return 0; + } + socket = m_client; } if (socket == INVALID_SOCKET){ return 0; @@ -195,7 +206,12 @@ size_t ServerSocket_WinSocket::send(const void* data, size_t bytes, std::chrono: while (bytes > 0){ { std::lock_guard lg(m_lock); - if (m_state.load(std::memory_order_relaxed) != State::CONNECTED){ + // The connection may have been replaced (or closed) since the + // last iteration: in that case this write must stop rather + // than reach the new client's socket. + if (m_state.load(std::memory_order_relaxed) != State::CONNECTED + || (connection != 0 && m_connection_id != connection) + ){ return sent; } } @@ -379,6 +395,7 @@ void ServerSocket_WinSocket::accept_thread_loop(std::string address, uint16_t po ioctlsocket(client_socket, FIONBIO, &client_non_blocking); m_client = client_socket; m_close_requested = false; + m_connection_id++; m_state.store(State::CONNECTED, std::memory_order_release); accepted = true; } diff --git a/Common/Cpp/Sockets/ServerSocket_WinSocket.h b/Common/Cpp/Sockets/ServerSocket_WinSocket.h index 6b68519ab8..f478e69b46 100644 --- a/Common/Cpp/Sockets/ServerSocket_WinSocket.h +++ b/Common/Cpp/Sockets/ServerSocket_WinSocket.h @@ -45,9 +45,15 @@ class ServerSocket_WinSocket final : public AbstractServerSocket{ virtual size_t send( const void* data, size_t bytes, - std::chrono::milliseconds timeout = std::chrono::milliseconds::max() + std::chrono::milliseconds timeout = std::chrono::milliseconds::max(), + uint64_t connection = 0 ) override; + // A token identifying the connection the socket is currently + // serving. It changes every time a new client is accepted, and it + // is 0 when no connection is active. + virtual uint64_t connection_id() override; + private: // Winsock has to be started before any of its functions can be used, and @@ -96,6 +102,12 @@ class ServerSocket_WinSocket final : public AbstractServerSocket{ SOCKET m_client = INVALID_SOCKET; bool m_close_requested = false; + // A connection token, incremented (under m_lock) on every accept. + // send() compares it against the token the caller captured, so a + // write that outlived its connection cannot reach the client that + // replaced it. 0 means no connection is active. + uint64_t m_connection_id = 0; + // One task per start() call. start() must never destroy the previous // task's AsyncTask here: its destructor blocks until the task has // finished, and a task from an earlier attempt may still be running diff --git a/Common/Cpp/Sockets/Sockets_Tests.cpp b/Common/Cpp/Sockets/Sockets_Tests.cpp index 7864a7d34a..3fe3bd915d 100644 --- a/Common/Cpp/Sockets/Sockets_Tests.cpp +++ b/Common/Cpp/Sockets/Sockets_Tests.cpp @@ -822,6 +822,117 @@ class Test_Sockets_RejectSecondClient : public TestSockets +class Test_Sockets_StaleSend : public TestSockets{ +public: + using TestSockets::TestSockets; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + ServerListener server_listener; + ServerSocketType server(this->m_thread_pool); + server.add_listener(server_listener); + + uint16_t port; + if (!this->start_server(server, server_listener, port)){ + return "Failed to start the server"; + } + + std::string error; + if (server.connection_id() != 0){ + error = "connection_id() was not 0 before any connection"; + } + + uint64_t first_id = 0; + { + ClientListener client_listener; + ClientSocketType client(this->m_thread_pool); + client.add_listener(client_listener); + + if (!this->connect_client(client, client_listener, port, 1)){ + error = "The first client did not finish connecting"; + }else if (!server_listener.waiter.wait_for( + SOCKET_TEST_WAIT, + [&server_listener]{ return server_listener.connected.load() >= 1; } + )){ + error = "The server did not call on_connect"; + }else{ + first_id = server.connection_id(); + if (first_id == 0){ + error = "The server did not assign a connection id"; + } + client.close(); + } + } + + if (error.empty() && !server_listener.waiter.wait_for( + SOCKET_TEST_WAIT, + [&server_listener]{ return server_listener.disconnected.load() >= 1; } + )){ + error = "The server did not call on_disconnect"; + } + + // The counter is monotonic and not reset, but with no active + // connection the id must report 0. + if (error.empty() && server.connection_id() != 0){ + error = "connection_id() was not 0 while no connection was active"; + } + + uint64_t second_id = 0; + { + ClientListener client_listener; + ClientSocketType client(this->m_thread_pool); + client.add_listener(client_listener); + + if (!this->connect_client(client, client_listener, port, 1)){ + error = "The second client did not finish connecting"; + }else if (!server_listener.waiter.wait_for( + SOCKET_TEST_WAIT, + [&server_listener]{ return server_listener.connected.load() >= 2; } + )){ + error = "The server did not call on_connect for the second client"; + }else{ + second_id = server.connection_id(); + if (second_id == first_id){ + error = "The connection id did not change after reconnect"; + } + // A send for the replaced connection must be refused, + // not delivered into the new client's socket. + if (error.empty() && + server.send("stale", 5, std::chrono::milliseconds::max(), first_id) != 0 + ){ + error = "A send for a replaced connection was not refused"; + } + // A send for the current connection still goes through. + if (error.empty() && + server.send("hello", 5, std::chrono::milliseconds::max(), second_id) != 5 + ){ + error = "The send for the current connection was not delivered"; + } + if (error.empty() && !client_listener.waiter.wait_for( + SOCKET_TEST_WAIT, + [&client_listener]{ return client_listener.received.size() >= 5; } + )){ + error = "The second client did not receive the data"; + }else if (error.empty() && + std::memcmp(client_listener.received.data(), "hello", 5) != 0 + ){ + error = "The second client received the wrong data"; + } + client.close(); + } + } + + if (!error.empty()){ + return error; + } + return true; + } +}; + + // Starting a second server on an already-bound port must fail through // on_listen_finished and leave the server not running. template @@ -2676,6 +2787,9 @@ void add_socket_tests(UnitTestDatabase& database, ThreadPool& thread_pool, const database.add>( thread_pool, prefix + "Reject a second client" ); + database.add>( + thread_pool, prefix + "A send for a replaced connection is refused" + ); database.add>( thread_pool, prefix + "Port in use" ); diff --git a/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.cpp b/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.cpp index 9a615e8b4c..abf5aae895 100644 --- a/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.cpp +++ b/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.cpp @@ -4,6 +4,7 @@ * */ +#include #include "Common/Cpp/Json/JsonValue.h" #include "Common/Cpp/Json/JsonObject.h" #include "CommonFramework/GlobalAutoPaths.h" @@ -15,7 +16,10 @@ namespace PokemonAutomation{ -SpriteDatabase::SpriteDatabase(const char* sprite_path, const char* json_path) +SpriteDatabase::SpriteDatabase( + const char* sprite_path, const char* json_path, + const std::function& mirror_horizontally +) : m_backing_image(RESOURCE_PATH() + sprite_path) { std::string path = RESOURCE_PATH() + json_path; @@ -37,6 +41,16 @@ SpriteDatabase::SpriteDatabase(const char* sprite_path, const char* json_path) JsonObject& obj = item.second.to_object_throw(path); int y = (int)obj.get_integer_throw("top", path); int x = (int)obj.get_integer_throw("left", path); + if (mirror_horizontally && mirror_horizontally(slug)){ + if (x < 0 || y < 0 || (size_t)(x + width) > m_backing_image.width() || (size_t)(y + height) > m_backing_image.height()){ + throw FileException(nullptr, PA_CURRENT_FUNCTION, "Sprite out of bounds: " + slug, path); + } + for (int r = y; r < y + (int)height; r++){ + for (int c0 = x, c1 = x + (int)width - 1; c0 < c1; c0++, c1--){ + std::swap(m_backing_image.pixel(c0, r), m_backing_image.pixel(c1, r)); + } + } + } ImageViewRGB32 sprite = extract_box_reference(m_backing_image, ImagePixelBox(x, y, x + width, y + height)); m_database.emplace( diff --git a/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.h b/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.h index 3136ffd6ec..3213479227 100644 --- a/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.h +++ b/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.h @@ -7,6 +7,7 @@ #ifndef PokemonAutomation_CommonTools_Resources_SpriteCompositeImage_H #define PokemonAutomation_CommonTools_Resources_SpriteCompositeImage_H +#include #include #include "CommonFramework/ImageTypes/ImageRGB32.h" @@ -32,7 +33,14 @@ class SpriteDatabase{ // (next pokemon) ... // } // } - SpriteDatabase(const char* sprite_path, const char* json_path); + // + // If "mirror_horizontally" is set, each sprite for which it returns true + // is flipped left-to-right in place. This avoids storing a separate + // flipped copy of the image. + SpriteDatabase( + const char* sprite_path, const char* json_path, + const std::function& mirror_horizontally = nullptr + ); public: struct Sprite{ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEventLog.h b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEventLog.h similarity index 70% rename from SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEventLog.h rename to SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEventLog.h index 62f34e2f6b..2d3039a5e7 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEventLog.h +++ b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEventLog.h @@ -2,31 +2,22 @@ * * From: https://github.com/PokemonAutomation/ * - * Split out of Pokemon_ScreenEvents.h, unchanged; it includes this header. + * The screen events of a stream of frames, in order and timed. * */ -#ifndef PokemonAutomation_Pokemon_ScreenEventLog_H -#define PokemonAutomation_Pokemon_ScreenEventLog_H +#ifndef PokemonAutomation_CommonTools_ScreenEvents_ScreenEventLog_H +#define PokemonAutomation_CommonTools_ScreenEvents_ScreenEventLog_H -#include +#include #include #include -#include #include #include -#include "Common/Cpp/Concurrency/Mutex.h" #include "Common/Cpp/Time.h" -#include "CommonFramework/ImageTools/ImageStats.h" -#include "CommonFramework/ImageTypes/ImageViewRGB32.h" -#include "CommonFramework/VideoPipeline/VideoFeed.h" -#include "CommonTools/InferenceCallbacks/VisualInferenceCallback.h" -#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" -#include "NintendoSwitch/Inference/NintendoSwitch_HomeMenuDetector.h" -#include "Pokemon_ScreenFades.h" +#include "ScreenEventTypes.h" namespace PokemonAutomation{ -namespace Pokemon{ // Whether an event started or stopped showing. @@ -51,8 +42,8 @@ constexpr size_t SCREEN_EVENT_LOG_CAPACITY = 256; // The events of a stream of frames, in order and timed: what rose and fell // when, not only what was seen (ScreenEventWatcher::seen()). The judges that -// care about order read it (Pokemon_Interrupts.h): a flash then a battle is -// an encounter, a box then a battle a trainer's sight. +// care about order read it: a flash then a battle is an encounter, a box +// then a battle a trainer's sight. // // It keeps the newest `capacity` changes and counts the ones it dropped. // Not thread-safe; ScreenEventWatcher guards its own. @@ -95,6 +86,5 @@ std::string describe(const ScreenEventLog& log, WallClock origin); -} // namespace Pokemon -} // namespace PokemonAutomation +} // namespace PokemonAutomation #endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEventTypes.h b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEventTypes.h similarity index 76% rename from SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEventTypes.h rename to SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEventTypes.h index c71d62e8ed..4b7f543a70 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEventTypes.h +++ b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEventTypes.h @@ -2,30 +2,21 @@ * * From: https://github.com/PokemonAutomation/ * - * Split out of Pokemon_ScreenEvents.h, unchanged; it includes this header. + * The words a program uses for what the screen shows: a fade, a dialogue + * box, a battle starting. ScreenEvents.h watches a stream for them. * */ -#ifndef PokemonAutomation_Pokemon_ScreenEventTypes_H -#define PokemonAutomation_Pokemon_ScreenEventTypes_H +#ifndef PokemonAutomation_CommonTools_ScreenEvents_ScreenEventTypes_H +#define PokemonAutomation_CommonTools_ScreenEvents_ScreenEventTypes_H -#include +#include #include -#include #include -#include #include -#include "Common/Cpp/Concurrency/Mutex.h" -#include "Common/Cpp/Time.h" -#include "CommonFramework/ImageTools/ImageStats.h" -#include "CommonFramework/ImageTypes/ImageViewRGB32.h" -#include "CommonFramework/VideoPipeline/VideoFeed.h" -#include "CommonTools/InferenceCallbacks/VisualInferenceCallback.h" -#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" -#include "NintendoSwitch/Inference/NintendoSwitch_HomeMenuDetector.h" +#include namespace PokemonAutomation{ -namespace Pokemon{ // One thing a frame shows. A game reports the ones its detectors can see. @@ -47,7 +38,7 @@ enum class ScreenEvent : uint8_t{ }; // How many ScreenEvent values there are. constexpr size_t SCREEN_EVENT_COUNT = 12; - +static_assert((size_t)ScreenEvent::PLAYER_STILL + 1 == SCREEN_EVENT_COUNT, "PLAYER_STILL is the last ScreenEvent"); // "dialog box", for the log. std::string screen_event_name(ScreenEvent event); @@ -95,9 +86,11 @@ class ScreenEvents{ uint32_t m_bits = 0; }; +// "dialog box, player can move" (joined by `separator`); "nothing" when +// empty. +std::string describe(const ScreenEvents& events, std::string_view separator = ", "); -} // namespace Pokemon -} // namespace PokemonAutomation +} // namespace PokemonAutomation #endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEvents.cpp b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents.cpp similarity index 85% rename from SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEvents.cpp rename to SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents.cpp index 367a23ec8d..8b7dcb6c8a 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEvents.cpp +++ b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents.cpp @@ -4,13 +4,16 @@ * */ +#include #include #include "CommonFramework/ImageTools/ImageBoxes.h" -#include "CommonFramework/ImageTypes/ImageRGB32.h" -#include "Pokemon_ScreenEvents.h" +#include "CommonFramework/ImageTools/ImageStats.h" +#include "ScreenEventLog.h" +#include "ScreenEventTypes.h" +#include "ScreenFades.h" +#include "ScreenEvents.h" namespace PokemonAutomation{ -namespace Pokemon{ std::string screen_event_name(ScreenEvent event){ @@ -80,7 +83,7 @@ WarpFade fade_in(const ScreenEvents& events){ } return WarpFade::NONE; } -std::string describe(const ScreenEvents& events){ +std::string describe(const ScreenEvents& events, std::string_view separator){ std::string out; for (size_t c = 0; c < SCREEN_EVENT_COUNT; c++){ const ScreenEvent event = (ScreenEvent)c; @@ -88,7 +91,7 @@ std::string describe(const ScreenEvents& events){ continue; } if (!out.empty()){ - out += ", "; + out += separator; } out += screen_event_name(event); } @@ -150,23 +153,6 @@ std::string describe(const ScreenEventLog& log, WallClock origin){ } -SwitchHomeObserver::SwitchHomeObserver(NintendoSwitch::ConsoleHandle& console, ScreenEventObserver& game) - : m_game(game) - , m_home(console, COLOR_RED) -{} - -ScreenEvents SwitchHomeObserver::observe(const VideoSnapshot& frame, WallClock now){ - if (frame && m_home.detect_only(*frame.frame)){ - return {ScreenEvent::SWITCH_HOME_MENU}; - } - return m_game.observe(frame, now); -} - -void SwitchHomeObserver::make_overlays(VideoOverlaySet& items) const{ - m_game.make_overlays(items); -} - - ScreenEventWatcher::ScreenEventWatcher(ScreenEventObserver& observer, ScreenEvents stop_on) : VisualInferenceCallback("ScreenEventWatcher") , m_observer(observer) @@ -201,5 +187,4 @@ void ScreenEventWatcher::reset(){ -} // namespace Pokemon -} // namespace PokemonAutomation +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEvents.h b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents.h similarity index 59% rename from SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEvents.h rename to SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents.h index ee3080235d..3e90b8d85d 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEvents.h +++ b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents.h @@ -2,38 +2,30 @@ * * From: https://github.com/PokemonAutomation/ * - * What a story step can see on the screen, in words every game shares: a - * fade, a location banner, a dialogue box, a battle starting. Each game - * implements ScreenEventObserver with its own detectors; the judgements - * built on these events (Pokemon_SceneJudges.h) are then the same for all - * of them. + * What an autonomous program can see on the screen, in words every game + * shares: a fade, a location banner, a dialogue box, a battle starting. + * Each game implements ScreenEventObserver with its own detectors; the + * judgements built on these events are then the same for all of them. * * An observer runs during a controller action as a ScreenEventWatcher * (a VisualInferenceCallback for run_until), or is asked frame by frame. * */ -#ifndef PokemonAutomation_Pokemon_ScreenEvents_H -#define PokemonAutomation_Pokemon_ScreenEvents_H +#ifndef PokemonAutomation_CommonTools_ScreenEvents_ScreenEvents_H +#define PokemonAutomation_CommonTools_ScreenEvents_ScreenEvents_H #include #include -#include -#include -#include -#include #include "Common/Cpp/Concurrency/Mutex.h" #include "Common/Cpp/Time.h" -#include "CommonFramework/ImageTools/ImageStats.h" -#include "CommonFramework/ImageTypes/ImageViewRGB32.h" #include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" #include "CommonTools/InferenceCallbacks/VisualInferenceCallback.h" -#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" -#include "NintendoSwitch/Inference/NintendoSwitch_HomeMenuDetector.h" -#include "Pokemon_ScreenEventLog.h" +#include "ScreenEventLog.h" +#include "ScreenEventTypes.h" namespace PokemonAutomation{ -namespace Pokemon{ // A game's detectors, as events. Observers may keep state between frames @@ -52,23 +44,6 @@ class ScreenEventObserver{ }; -// A game's observer behind a look at the Switch itself: a frame showing -// the HOME menu shows SWITCH_HOME_MENU and nothing else, as a game's -// detectors may read HOME's plain screen as a quiet overworld (BDSP, -// lab-B 2026-11-09). Every other frame is the game's to read. -class SwitchHomeObserver : public ScreenEventObserver{ -public: - SwitchHomeObserver(NintendoSwitch::ConsoleHandle& console, ScreenEventObserver& game); - - virtual ScreenEvents observe(const VideoSnapshot& frame, WallClock now) override; - virtual void make_overlays(VideoOverlaySet& items) const override; - -private: - ScreenEventObserver& m_game; - NintendoSwitch::HomeMenuDetector m_home; -}; - - // An observer as a run_until callback: it records every event it sees, in // order in a log, and stops the action on any of `stop_on` (none: it only // records). @@ -104,6 +79,5 @@ class ScreenEventWatcher : public VisualInferenceCallback{ -} // namespace Pokemon -} // namespace PokemonAutomation +} // namespace PokemonAutomation #endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEvents_Tests.cpp b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents_Tests.cpp similarity index 54% rename from SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEvents_Tests.cpp rename to SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents_Tests.cpp index 82aae9bf03..7b61ab9227 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEvents_Tests.cpp +++ b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents_Tests.cpp @@ -11,13 +11,16 @@ #include #include #include +#include "Common/Cpp/Time.h" #include "Common/Cpp/TestRunners/UnitTest.h" #include "CommonFramework/ImageTypes/ImageRGB32.h" -#include "Pokemon_ScreenEvents.h" -#include "Pokemon_ScreenEvents_Tests.h" +#include "ScreenEventLog.h" +#include "ScreenEventTypes.h" +#include "ScreenFades.h" +#include "ScreenEvents.h" +#include "ScreenEvents_Tests.h" namespace PokemonAutomation{ -namespace Pokemon{ namespace{ @@ -131,23 +134,22 @@ UnitTestResult test_watcher(){ } -struct ScreenEventsInput{ - std::string name; - std::function body; -}; - -struct ScreenEventsExpectation{ -}; - struct ScreenEventsCase{ - ScreenEventsInput input; - ScreenEventsExpectation expectation; + struct Input{ + std::function body; + }; + struct Expectation{ + }; + + std::string name; + Input input; + Expectation expectation; }; class Test_ScreenEvents : public UnitTest{ public: explicit Test_ScreenEvents(ScreenEventsCase test_case) - : UnitTest(std::format("Pokemon::ScreenEvents - {}", test_case.input.name)) + : UnitTest(std::format("CommonTools::ScreenEvents - {}", test_case.name)) , m_case(std::move(test_case)) {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ @@ -163,26 +165,117 @@ class Test_ScreenEvents : public UnitTest{ const ScreenEventsCase SCREEN_EVENTS_CASES[] = { { - .input = {"a set of events, its fade and its log text", test_set_and_describe}, + .name = "a set of events, its fade and its log text", + .input = {.body = test_set_and_describe}, .expectation = {}, }, { - .input = {"a fade is uniform, dark or white; a textured floor is none", test_fade_of_screen}, + .name = "a fade is uniform, dark or white; a textured floor is none", + .input = {.body = test_fade_of_screen}, .expectation = {}, }, { - .input = {"an observer as a watcher records and stops", test_watcher}, + .name = "an observer as a watcher records and stops", + .input = {.body = test_watcher}, .expectation = {}, }, }; +namespace{ + +// A stream of frames into a ScreenEventLog, and its describe() text. +struct ScreenEventLogCase{ + struct Frame{ + int ms = 0; + std::vector events; + }; + struct Input{ + size_t capacity = 0; + std::vector frames; + }; + struct Expectation{ + // describe() of the log, times from the first frame. + std::string text; + size_t dropped = 0; + }; + + std::string name; + Input input; + Expectation expectation; +}; + +class Test_ScreenEventLog : public UnitTest{ +public: + explicit Test_ScreenEventLog(ScreenEventLogCase test_case) + : UnitTest(std::format("CommonTools::ScreenEventLog - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + ScreenEventLog log(m_case.input.capacity); + const WallClock origin = current_time(); + for (const ScreenEventLogCase::Frame& frame : m_case.input.frames){ + ScreenEvents events; + for (ScreenEvent event : frame.events){ + events.set(event, true); + } + log.record(events, origin + Milliseconds(frame.ms)); + } + if (log.dropped() != m_case.expectation.dropped){ + return std::format("dropped(): expected {}, got {}", m_case.expectation.dropped, log.dropped()); + } + return expect_equal("describe()", describe(log, origin), m_case.expectation.text); + } + +private: + ScreenEventLogCase m_case; +}; + +} // namespace + +const ScreenEventLogCase SCREEN_EVENT_LOG_CASES[] = { + { + .name = "no frames", + .input = {.capacity = 8, .frames = {}}, + .expectation = {.text = "no changes", .dropped = 0}, + }, + { + .name = "the first frame's events all rise", + .input = {.capacity = 8, .frames = {{.ms = 0, .events = {ScreenEvent::DIALOG_BOX, ScreenEvent::SCREEN_STILL}}}}, + .expectation = {.text = "+0 ms dialog box rose\n+0 ms screen still rose\n", .dropped = 0}, + }, + { + .name = "only changes are kept, rising and falling", + .input = {.capacity = 8, .frames = { + {.ms = 0, .events = {ScreenEvent::PLAYER_CAN_MOVE}}, + {.ms = 100, .events = {ScreenEvent::PLAYER_CAN_MOVE}}, + {.ms = 200, .events = {ScreenEvent::FADE_BLACK}}, + {.ms = 900, .events = {}}, + }}, + .expectation = { + .text = "+0 ms player can move rose\n+200 ms black fade rose\n+200 ms player can move fell\n+900 ms black fade fell\n", + .dropped = 0, + }, + }, + { + .name = "the oldest changes drop past the capacity", + .input = {.capacity = 2, .frames = { + {.ms = 0, .events = {ScreenEvent::DIALOG_BOX}}, + {.ms = 100, .events = {}}, + {.ms = 200, .events = {ScreenEvent::CHOICE_BOX}}, + }}, + .expectation = {.text = "(1 older changes dropped)\n+100 ms dialog box fell\n+200 ms choice box rose\n", .dropped = 1}, + }, +}; + void add_tests_ScreenEvents(UnitTestDatabase& database){ for (const auto& item : SCREEN_EVENTS_CASES){ database.add(item); } + for (const auto& item : SCREEN_EVENT_LOG_CASES){ + database.add(item); + } } -} // namespace Pokemon -} // namespace PokemonAutomation +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEvents_Tests.h b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents_Tests.h similarity index 58% rename from SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEvents_Tests.h rename to SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents_Tests.h index 7fe664f59e..3d1c84b44f 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEvents_Tests.h +++ b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents_Tests.h @@ -4,13 +4,12 @@ * */ -#ifndef PokemonAutomation_Pokemon_ScreenEvents_Tests_H -#define PokemonAutomation_Pokemon_ScreenEvents_Tests_H +#ifndef PokemonAutomation_CommonTools_ScreenEvents_ScreenEvents_Tests_H +#define PokemonAutomation_CommonTools_ScreenEvents_ScreenEvents_Tests_H #include "Common/Cpp/TestRunners/UnitTestDatabase.h" namespace PokemonAutomation{ -namespace Pokemon{ // Registers unit tests for the shared screen events. @@ -18,6 +17,5 @@ void add_tests_ScreenEvents(UnitTestDatabase& database); -} // namespace Pokemon -} // namespace PokemonAutomation +} // namespace PokemonAutomation #endif diff --git a/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenFades.h b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenFades.h new file mode 100644 index 0000000000..a153f49246 --- /dev/null +++ b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenFades.h @@ -0,0 +1,43 @@ +/* Screen Fades + * + * From: https://github.com/PokemonAutomation/ + * + * A fade to black or white, read from a screen and as screen events. + * + */ + +#ifndef PokemonAutomation_CommonTools_ScreenEvents_ScreenFades_H +#define PokemonAutomation_CommonTools_ScreenEvents_ScreenFades_H + +#include "CommonFramework/ImageTools/ImageStats.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "ScreenEventTypes.h" + +namespace PokemonAutomation{ + + +// The event of a fade: FADE_BLACK, FADE_WHITE, or none for WarpFade::NONE. +ScreenEvents fade_events(WarpFade fade); +// The fade `events` show: BLACK before WHITE if they have both. +WarpFade fade_in(const ScreenEvents& events); + + +// A fade is uniform over the middle 80% of the screen, the same stddev +// test as the shared Black/WhiteScreenWatcher: a dim cave floor (mean +// about 46 a channel) is textured, so it is no fade. A tighter bound +// needs checking against such dim floors first. +constexpr double FADE_MAX_STDDEV_SUM = 10; +// Dark goes to an rgb sum of 225, as a cave mouth's fade is dim, 45 to 64 +// a channel, not black. +constexpr double FADE_MAX_DARK_SUM = 225; +// White from an rgb sum of 500, as the shared WhiteScreenWatcher's. +constexpr double FADE_MIN_WHITE_SUM = 500; +// The fade `middle` (the stats of the screen's middle 80%) shows, if any. +WarpFade fade_of(const ImageStats& middle); +// fade_of() for a screen. +WarpFade fade_of_screen(const ImageViewRGB32& screen); + + + +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/CommonTools/ScreenMachine/ScreenMachine.cpp b/SerialPrograms/Source/CommonTools/ScreenMachine/ScreenMachine.cpp index 587d740ec8..4be721658c 100644 --- a/SerialPrograms/Source/CommonTools/ScreenMachine/ScreenMachine.cpp +++ b/SerialPrograms/Source/CommonTools/ScreenMachine/ScreenMachine.cpp @@ -30,36 +30,6 @@ const ScreenState* classify(const std::vector& states, const ImageV } // namespace -std::string_view screen_event_name(ScreenEvent event){ - switch (event){ - case ScreenEvent::FADE_BLACK: return "fade_black"; - case ScreenEvent::FADE_WHITE: return "fade_white"; - case ScreenEvent::TEXT_BOX: return "text_box"; - case ScreenEvent::BATTLE: return "battle"; - case ScreenEvent::CAN_MOVE: return "can_move"; - case ScreenEvent::STILL: return "still"; - case ScreenEvent::CHOICE_BOX: return "choice_box"; - case ScreenEvent::INTERACT_PROMPT: return "interact_prompt"; - } - return "unknown"; -} - -std::string describe(const ScreenEvents& events, std::string_view separator){ - constexpr ScreenEvent ALL[] = { - ScreenEvent::FADE_BLACK, ScreenEvent::FADE_WHITE, ScreenEvent::TEXT_BOX, - ScreenEvent::BATTLE, ScreenEvent::CAN_MOVE, ScreenEvent::STILL, - ScreenEvent::CHOICE_BOX, ScreenEvent::INTERACT_PROMPT, - }; - std::string text; - for (ScreenEvent event : ALL){ - if (events.has(event)){ - text += text.empty() ? "" : separator; - text += screen_event_name(event); - } - } - return text.empty() ? std::string("nothing") : text; -} - bool same_screen(const VideoSnapshot& a, const VideoSnapshot& b){ if (!a || !b || a->width() != b->width() || a->height() != b->height()){ return false; diff --git a/SerialPrograms/Source/CommonTools/ScreenMachine/ScreenObservation.h b/SerialPrograms/Source/CommonTools/ScreenMachine/ScreenObservation.h index 636e8eec7d..ba1e6e84b3 100644 --- a/SerialPrograms/Source/CommonTools/ScreenMachine/ScreenObservation.h +++ b/SerialPrograms/Source/CommonTools/ScreenMachine/ScreenObservation.h @@ -3,7 +3,7 @@ * From: https://github.com/PokemonAutomation/ * * What one look at the game showed, as facts rather than pixels: the - * events on screen (a fade, a text box, a battle) and named readings. A + * events on screen (a fade, a dialog box, a battle) and named readings. A * console fills it from a frame with the game's detectors; a simulation * fills it from its model and has no frame. A ScreenState that matches on * an observation (ScreenState::matches_observation) therefore runs in @@ -14,58 +14,17 @@ #ifndef PokemonAutomation_CommonTools_ScreenObservation_H #define PokemonAutomation_CommonTools_ScreenObservation_H -#include #include -#include #include #include #include -#include #include "Common/Cpp/Time.h" #include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/ScreenEvents/ScreenEventTypes.h" namespace PokemonAutomation{ -// Something a look at the screen can show. Several can hold at once. -enum class ScreenEvent : uint8_t{ - FADE_BLACK, // the screen is (fading to) black: a warp, a battle start - FADE_WHITE, // the screen is (fading to) white - TEXT_BOX, // a dialogue or message box is open - BATTLE, // a battle is showing - CAN_MOVE, // the player can move: no box, menu or scene holds them - STILL, // nothing moved since the previous look - CHOICE_BOX, // a list of answers to pick from is open - INTERACT_PROMPT, // a prompt to interact with what the player faces ("A Talk") -}; - -// The set of events one look showed. -class ScreenEvents{ -public: - ScreenEvents() = default; - explicit ScreenEvents(std::initializer_list events){ - for (ScreenEvent event : events){ - add(event); - } - } - - bool has(ScreenEvent event) const{ - return (m_bits & bit(event)) != 0; - } - void add(ScreenEvent event){ - m_bits |= bit(event); - } - - bool operator==(const ScreenEvents& other) const = default; - -private: - static uint32_t bit(ScreenEvent event){ - return (uint32_t)1 << (uint32_t)event; - } - - uint32_t m_bits = 0; -}; - // One look at the game. struct ScreenObservation{ WallClock when = WallClock::min(); @@ -78,13 +37,6 @@ struct ScreenObservation{ std::optional frame = std::nullopt; }; -// "fade_black", "fade_white", "text_box", "battle", "can_move", "still", -// "choice_box", "interact_prompt". -std::string_view screen_event_name(ScreenEvent event); - -// The events in `events` joined by `separator`, or "nothing" when empty. -std::string describe(const ScreenEvents& events, std::string_view separator = ", "); - // Whether `a` and `b` both have a frame of the same size within the // still-screen RMSD threshold (2.0). bool same_screen(const VideoSnapshot& a, const VideoSnapshot& b); diff --git a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp index 560b148d8f..cdeb54de03 100644 --- a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp +++ b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp @@ -4,6 +4,7 @@ * */ +#include "Common/Cpp/HttpServer/HttpServer_Tests.h" #include "Common/Cpp/Json/JsonTools_Tests.h" #include "Common/Cpp/ListenerSet.h" #include "Common/Cpp/MoveOnlyFunction_Tests.h" @@ -26,9 +27,12 @@ #include "CommonFramework/Windows/LabControl.h" #include "CommonTools/VisualDetectors/BlackBorderDetector.h" #include "CommonTools/OCR/OCR_Tests.h" +#include "CommonTools/ScreenEvents/ScreenEvents_Tests.h" #include "NintendoSwitch/Inference/NintendoSwitch_CheckOnlineDetector.h" #include "NintendoSwitch/Inference/NintendoSwitch_FailedToConnectDetector.h" +#include "NintendoSwitch/Inference/NintendoSwitch_HomeTileReader.h" +#include "NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h" #include "NintendoSwitch/Inference/NintendoSwitch_UpdatePopupDetector.h" #include "NintendoSwitch/Programs/Typewriter/NintendoSwitch_Typewriter_Tests.h" #include "NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.h" @@ -42,6 +46,7 @@ #include "Pokemon/Navigation/Pokemon_RouteScorer_Tests.h" #include "Pokemon/Navigation/Pokemon_RuntimeMap_Tests.h" #include "Pokemon/Navigation/Pokemon_WallRecovery_Tests.h" +#include "Pokemon/Navigation/Simulation/Pokemon_SimFaults_Tests.h" #include "Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.h" #include "PokemonBDSP/PokemonBDSP_Tests.h" #include "VideoGames/PokemonHome/PokemonHome_Tests.h" @@ -66,6 +71,7 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ UnitTestDatabase ret; add_tests_QtEventThreadPool(ret, GlobalThreadPools::computation_normal()); add_tests_Sockets(ret, GlobalThreadPools::unlimited_normal()); + add_tests_HttpServer(ret, GlobalThreadPools::unlimited_normal()); add_tests_BlackBorderDetector(ret); add_tests_ListenerSet(ret); add_tests_JsonTools(ret); @@ -80,6 +86,7 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ add_tests_AudioFileLoader(ret); add_tests_LabControl(ret); OCR::add_tests(ret); + add_tests_ScreenEvents(ret); Kernels::add_tests(ret); Pokemon::add_tests_LegPlanner(ret); Pokemon::add_tests_LegWalker(ret); @@ -90,10 +97,13 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ Pokemon::add_tests_RuntimeMap(ret); Pokemon::add_tests_RouteLog(ret); Pokemon::add_tests_RouteScorer(ret); + Pokemon::add_tests_SimFaults(ret); Pokemon::add_tests_SimGolden(ret); Pokemon::add_tests_AutoStory(ret); NintendoSwitch::add_tests_CheckOnlineDetector(ret); NintendoSwitch::add_tests_FailedToConnectDetector(ret); + NintendoSwitch::add_tests_HomeTileReader(ret); + NintendoSwitch::add_tests_SystemKeyboardDetector(ret); NintendoSwitch::add_tests_UpdatePopupDetector(ret); NintendoSwitch::Typewriter::add_tests_TypewriterTextMapping(ret); NintendoSwitch::PokemonBDSP::add_tests(ret); diff --git a/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_HomeScreenObserver.cpp b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_HomeScreenObserver.cpp new file mode 100644 index 0000000000..195d33cdd3 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_HomeScreenObserver.cpp @@ -0,0 +1,34 @@ +/* Home Screen Observer + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Common/Cpp/Color.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "NintendoSwitch_HomeScreenObserver.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + + +SwitchHomeObserver::SwitchHomeObserver(ConsoleHandle& console, ScreenEventObserver& game) + : m_game(game) + , m_home(console, COLOR_RED) +{} + +ScreenEvents SwitchHomeObserver::observe(const VideoSnapshot& frame, WallClock now){ + if (frame && m_home.detect_only(*frame.frame)){ + return {ScreenEvent::SWITCH_HOME_MENU}; + } + return m_game.observe(frame, now); +} + +void SwitchHomeObserver::make_overlays(VideoOverlaySet& items) const{ + m_game.make_overlays(items); +} + + + +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_HomeScreenObserver.h b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_HomeScreenObserver.h new file mode 100644 index 0000000000..c5ebbff0e2 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_HomeScreenObserver.h @@ -0,0 +1,42 @@ +/* Home Screen Observer + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_NintendoSwitch_HomeScreenObserver_H +#define PokemonAutomation_NintendoSwitch_HomeScreenObserver_H + +#include "Common/Cpp/Time.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "CommonTools/ScreenEvents/ScreenEventTypes.h" +#include "CommonTools/ScreenEvents/ScreenEvents.h" +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "NintendoSwitch_HomeMenuDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + + +// A game's observer behind a look at the Switch itself: a frame showing +// the HOME menu shows SWITCH_HOME_MENU and nothing else, as a game's +// detectors may read HOME's plain screen as a quiet overworld. Every +// other frame is the game's to read. +class SwitchHomeObserver : public ScreenEventObserver{ +public: + SwitchHomeObserver(ConsoleHandle& console, ScreenEventObserver& game); + + virtual ScreenEvents observe(const VideoSnapshot& frame, WallClock now) override; + virtual void make_overlays(VideoOverlaySet& items) const override; + +private: + ScreenEventObserver& m_game; + HomeMenuDetector m_home; +}; + + + +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_HomeTileReader.cpp b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_HomeTileReader.cpp new file mode 100644 index 0000000000..b8913f5001 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_HomeTileReader.cpp @@ -0,0 +1,249 @@ +/* Home Tile Reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonTools/Images/ImageFilter.h" +#include "CommonTools/OCR/OCR_Routines.h" +#include "CommonTools/OCR/OCR_StringNormalization.h" +#include "Tests/ImageUnitTest.h" +#include "NintendoSwitch_HomeTileReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + + +namespace{ + +// The selected tile's frame lines, about 0.008 of the screen tall, just +// outside the tile art: strips across the screen that the art of the +// other tiles stays out of. +const ImageFloatBox FRAME_TOP_STRIP{0.0, 0.252, 1.0, 0.014}; +const ImageFloatBox FRAME_BOTTOM_STRIP{0.0, 0.627, 1.0, 0.014}; +constexpr double FRAME_TOP = 0.256; +constexpr double FRAME_BOTTOM = 0.637; +// A column crosses a frame line when this many of its pixels in the strip +// are the frame's cyan. +constexpr size_t MIN_LINE_PIXELS = 3; +// A tile frame is 0.214 of the screen wide. +constexpr double MIN_FRAME_WIDTH = 0.18; +constexpr double MAX_FRAME_WIDTH = 0.25; + +// The band above the row where HOME writes the selected game's name. The +// name starts 0.005 to 0.01 left of the frame, right of a game card icon +// that ends 0.017 or more left of it. +constexpr double NAME_TOP = 0.19; +constexpr double NAME_HEIGHT = 0.06; +constexpr double NAME_LEAD = 0.015; +constexpr double NAME_RIGHT = 0.98; +// The name's cyan (ARGB), dark text on HOME's light background once +// filtered: red low, green and blue high. +constexpr uint32_t NAME_MINS = 0xff000096; +constexpr uint32_t NAME_MAXS = 0xff96ffff; + +// The frame's cyan, which pulses between about (0, 187, 229) and +// (52, 249, 229), with lighter anti-aliased edges. +bool is_frame_cyan(uint32_t pixel){ + const int red = (pixel >> 16) & 0xff; + const int green = (pixel >> 8) & 0xff; + const int blue = pixel & 0xff; + return green >= 160 && blue >= 160 && green - red >= 60 && blue - red >= 60; +} + +// Per column of `strip`, whether it crosses a frame line. +std::vector frame_line_columns(const ImageViewRGB32& strip){ + std::vector columns(strip.width(), false); + for (size_t x = 0; x < strip.width(); x++){ + size_t cyan = 0; + for (size_t y = 0; y < strip.height(); y++){ + cyan += is_frame_cyan(strip.pixel(x, y)) ? 1 : 0; + } + columns[x] = cyan >= MIN_LINE_PIXELS; + } + return columns; +} + +// The longest run of columns that cross both frame lines, as [begin, end). +std::pair longest_framed_run(const std::vector& top, const std::vector& bottom){ + std::pair best{0, 0}; + size_t begin = 0; + for (size_t x = 0; x <= top.size(); x++){ + const bool framed = x < top.size() && x < bottom.size() && top[x] && bottom[x]; + if (framed){ + continue; + } + if (x - begin > best.second - best.first){ + best = {begin, x}; + } + begin = x + 1; + } + return best; +} + +} // namespace + + +std::optional find_selected_home_tile(const ImageViewRGB32& screen){ + if (screen.width() == 0 || screen.height() == 0){ + return std::nullopt; + } + const std::pair run = longest_framed_run( + frame_line_columns(extract_box_reference(screen, FRAME_TOP_STRIP)), + frame_line_columns(extract_box_reference(screen, FRAME_BOTTOM_STRIP)) + ); + const double width = (double)screen.width(); + const double frame_width = (double)(run.second - run.first) / width; + if (frame_width < MIN_FRAME_WIDTH || frame_width > MAX_FRAME_WIDTH){ + return std::nullopt; + } + return ImageFloatBox((double)run.first / width, FRAME_TOP, frame_width, FRAME_BOTTOM - FRAME_TOP); +} + +std::optional read_selected_home_tile_name(Language language, const ImageViewRGB32& screen){ + const std::optional frame = find_selected_home_tile(screen); + if (!frame){ + return std::nullopt; + } + const double left = std::max(0.0, frame->x - NAME_LEAD); + const ImageViewRGB32 band = extract_box_reference(screen, ImageFloatBox(left, NAME_TOP, NAME_RIGHT - left, NAME_HEIGHT)); + const ImageRGB32 text = to_blackwhite_rgb32_range(band, true, NAME_MINS, NAME_MAXS); + return OCR::ocr_read(language, text); +} + +bool selected_home_tile_is(Language language, const ImageViewRGB32& screen, std::string_view name){ + const std::optional read = read_selected_home_tile_name(language, screen); + if (!read){ + return false; + } + const std::u32string wanted = OCR::normalize_utf32(std::string(name)); + return !wanted.empty() && OCR::normalize_utf32(*read).find(wanted) != std::u32string::npos; +} + + + +namespace{ + +// The names HOME shows for the two games on console B's frames. +constexpr std::string_view FIRERED = "Pokémon FireRed Version"; +constexpr std::string_view LEGENDS_ZA = "Pokémon Legends: Z-A"; + +// Where the first tile's frame starts, and the step to the next tile, as +// parts of the screen's width, before the row scrolls. +constexpr double FIRST_TILE_LEFT = 0.076; +constexpr double TILE_STEP = 0.211; +constexpr double TILE_TOLERANCE = 0.02; + +struct HomeTileCase{ + struct Input{ + std::string_view image; + }; + struct Expectation{ + // The selected tile's place in the row, from 0; nothing: no tile + // is selected. + std::optional tile = std::nullopt; + // The name the selected tile shows: FIRERED or LEGENDS_ZA. + std::string_view game = {}; + }; + std::string_view name; + Input input; + Expectation expectation; +}; + +// Console B, 2026-11-14: HOME after the order changed (the game played +// last moves to the first tile), and the user select over HOME. +const std::vector HOME_TILE_CASES = { + {.name = "FireRed on the first tile 1", + .input = {.image = "NintendoSwitch/HomeTileReader/FireRedFirstTile_1.png"}, + .expectation = {.tile = 0, .game = FIRERED}}, + {.name = "FireRed on the first tile 2", + .input = {.image = "NintendoSwitch/HomeTileReader/FireRedFirstTile_2.png"}, + .expectation = {.tile = 0, .game = FIRERED}}, + {.name = "FireRed on the first tile 3", + .input = {.image = "NintendoSwitch/HomeTileReader/FireRedFirstTile_3.png"}, + .expectation = {.tile = 0, .game = FIRERED}}, + {.name = "Legends Z-A on the first tile", + .input = {.image = "NintendoSwitch/HomeTileReader/LegendsZAFirstTile.png"}, + .expectation = {.tile = 0, .game = LEGENDS_ZA}}, + {.name = "Legends Z-A on the second tile", + .input = {.image = "NintendoSwitch/HomeTileReader/LegendsZASecondTile.png"}, + .expectation = {.tile = 1, .game = LEGENDS_ZA}}, + {.name = "user select over HOME", + .input = {.image = "NintendoSwitch/HomeTileReader/UserSelectOverHome.png"}, + .expectation = {}}, +}; + +// What went wrong with the frame on `screen`; empty when it is where `test` expects. +std::string check_frame(const HomeTileCase& test, const ImageViewRGB32& screen){ + const std::optional frame = find_selected_home_tile(screen); + if (!test.expectation.tile){ + return frame ? std::format("Expected no selected tile, found a frame at x={:.3f}.", frame->x) : ""; + } + if (!frame){ + return std::format("Expected a frame on tile {}, found none.", *test.expectation.tile); + } + const double expected_left = FIRST_TILE_LEFT + TILE_STEP * (double)*test.expectation.tile; + if (std::abs(frame->x - expected_left) > TILE_TOLERANCE){ + return std::format("Expected the frame on tile {} (x={:.3f}), found it at x={:.3f}.", *test.expectation.tile, expected_left, frame->x); + } + return ""; +} + +// What went wrong with the names on `screen`: it must be the game `test` +// expects and not the other one. +std::string check_names(const HomeTileCase& test, const ImageViewRGB32& screen){ + const std::string read = read_selected_home_tile_name(Language::English, screen).value_or(""); + for (std::string_view game : {FIRERED, LEGENDS_ZA}){ + const bool expected = game == test.expectation.game; + if (selected_home_tile_is(Language::English, screen, game) != expected){ + return std::format("Expected the tile {} \"{}\", read \"{}\".", expected ? "to be" : "not to be", game, read); + } + } + return ""; +} + +class Test_HomeTileReader : public ImageUnitTest{ +public: + explicit Test_HomeTileReader(const HomeTileCase& test) + : ImageUnitTest(std::format("NintendoSwitch::HomeTileReader - {}", test.name), {std::string(test.input.image)}) + , m_case(test) + { + // Plus the PaddleOCR (ONNX) recognition session and its working set. + m_memory += PADDLE_OCR_SINGLE_LINE_MEMORY_ESTIMATE; + } + + virtual UnitTestResult run_on_images( + Logger&, CancellableScope&, + const std::vector& images + ) const override{ + std::string error = check_frame(m_case, images[0]); + if (error.empty()){ + error = check_names(m_case, images[0]); + } + return error.empty() ? UnitTestResult(true) : UnitTestResult(error); + } + +private: + HomeTileCase m_case; +}; + +} // namespace + + +void add_tests_HomeTileReader(UnitTestDatabase& database){ + for (const HomeTileCase& test : HOME_TILE_CASES){ + database.add(test); + } +} + + + +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_HomeTileReader.h b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_HomeTileReader.h new file mode 100644 index 0000000000..c0c57f1052 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_HomeTileReader.h @@ -0,0 +1,55 @@ +/* Home Tile Reader + * + * From: https://github.com/PokemonAutomation/ + * + * Which game tile the HOME menu's cursor is on. HOME draws a cyan frame + * around the selected tile of the game row and writes that game's name + * above the row, also in cyan. The frame says that a tile is selected and + * where; the name says which game it is. Neither depends on the order of + * the row, which changes as games are played (the last one played moves + * to the first tile). + * + * Measured on the Switch 1 HOME menu at 1920x1080; positions are parts of + * the screen, so other capture sizes read the same. + * + */ + +#ifndef PokemonAutomation_NintendoSwitch_HomeTileReader_H +#define PokemonAutomation_NintendoSwitch_HomeTileReader_H + +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonFramework/Language.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + + +// The selected tile's cyan frame on `screen`; nothing when no tile of the +// game row is selected (the cursor on the icons below it, a dialog or the +// user select over HOME). +std::optional find_selected_home_tile(const ImageViewRGB32& screen); + +// The name HOME writes above the selected tile, as OCR reads it in +// `language` (the console's, not the game's); nothing when no tile is +// selected. +std::optional read_selected_home_tile_name(Language language, const ImageViewRGB32& screen); + +// Whether a tile is selected and its name contains `name` ("Pokémon +// Legends: Z-A", or a distinctive part of it), compared as OCR text: +// case, accents, spaces and punctuation ignored. +bool selected_home_tile_is(Language language, const ImageViewRGB32& screen, std::string_view name); + + +// The screenshot tests in CommandLineTests/NintendoSwitch/HomeTileReader/. +void add_tests_HomeTileReader(UnitTestDatabase& database); + + + +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.cpp b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.cpp index 37951460a9..b08bee33a4 100644 --- a/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.cpp +++ b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.cpp @@ -4,168 +4,277 @@ * */ -#include #include -#include -#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include +#include +#include +#include +#include +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" #include "CommonFramework/ImageTools/ImageStats.h" -#include "CommonFramework/VideoPipeline/VideoFeed.h" #include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" #include "CommonTools/Images/SolidColorTest.h" -#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "Tests/ImageUnitTest.h" #include "NintendoSwitch_SystemKeyboardDetector.h" namespace PokemonAutomation{ namespace NintendoSwitch{ -using namespace std::chrono_literals; +namespace{ + +// Centres of the five rows of keys, and of the first column and the spacing +// between columns, as fractions of the screen. +constexpr double KEY_ROW_Y[] = {0.500, 0.593, 0.681, 0.770, 0.858}; +constexpr double KEY_COL_X0 = 0.077; +constexpr double KEY_COL_STEP = 0.075; + +// In the right column, Return spans rows 1-2 and OK rows 3-4. These are the +// gaps between those keys. +constexpr double RIGHT_COLUMN_RETURN_TOP = 0.548; +constexpr double RIGHT_COLUMN_OK_TOP = 0.725; + +// The cursor border and everything else the reader looks at, from the top of the +// digit row to the bottom of the bottom row. +const ImageFloatBox KEYS_AREA(0.0, 0.43, 1.0, 0.49); + +// The cursor border pulses between bright cyan (about 50, 240, 220) and a darker +// teal (about 0, 185, 210), measured on Legends: Z-A. The blue of the OK button +// and of the ABC underline (40, 90, 250) is not green enough to match. +bool is_cursor_pixel(uint32_t pixel){ + const int red = (pixel >> 16) & 0xff; + const int green = (pixel >> 8) & 0xff; + const int blue = pixel & 0xff; + return red < 130 && green > 160 && blue > 170 && green - red > 60; +} + +uint8_t nearest_row(double y){ + uint8_t row = 0; + for (uint8_t r = 1; r < 5; r++){ + if (std::abs(KEY_ROW_Y[r] - y) < std::abs(KEY_ROW_Y[row] - y)){ + row = r; + } + } + return row; +} +// The key whose cursor border has its centre at (x, y), or nothing if that is +// off the keyboard. +std::optional key_at(double x, double y){ + const long col = std::lround((x - KEY_COL_X0) / KEY_COL_STEP); + if (col < 0 || col > 11){ + return std::nullopt; + } + if (col < 11){ + return SystemKeyboardKey{nearest_row(y), (uint8_t)col}; + } + if (y < RIGHT_COLUMN_RETURN_TOP){ + return SystemKeyboardKey{0, 11}; + } + if (y < RIGHT_COLUMN_OK_TOP){ + return SystemKeyboardKey{1, 11}; + } + return SystemKeyboardKey{3, 11}; +} + +} // namespace -// Key grid of the QWERTY layout and where each key sits: rows "1".."z", columns "1".."-". -static const std::string KEY_ROWS[] = {"1234567890-", "qwertyuiop/", "asdfghjkl:'", "zxcvbnm,.?!"}; -static constexpr double KEY_ROW_Y[] = {0.500, 0.593, 0.681, 0.770}; -static constexpr double KEY_COL_X0 = 0.077; -static constexpr double KEY_COL_STEP = 0.075; SystemKeyboardDetector::SystemKeyboardDetector(Color color) : m_color(color) - , m_key_panel(0.15, 0.555, 0.65, 0.020) + , m_key_panel_upper(0.15, 0.532, 0.65, 0.036) + , m_key_panel_lower(0.15, 0.715, 0.65, 0.032) , m_bottom_bar(0.10, 0.935, 0.35, 0.030) , m_ok_button(0.875, 0.760, 0.045, 0.020) {} void SystemKeyboardDetector::make_overlays(VideoOverlaySet& items) const{ - items.add(m_color, m_key_panel); + items.add(m_color, m_key_panel_upper); + items.add(m_color, m_key_panel_lower); items.add(m_color, m_bottom_bar); items.add(m_color, m_ok_button); } bool SystemKeyboardDetector::detect(const ImageViewRGB32& screen){ - if (!is_white(extract_box_reference(screen, m_key_panel)) || - !is_white(extract_box_reference(screen, m_bottom_bar)) - ){ + // Each strip crosses white keys and the slightly brighter gaps between them, + // which gives a stddev sum of about 11 (measured on BDSP and Legends: Z-A). + const double max_panel_stddev = 20; + const bool panel = + is_white(extract_box_reference(screen, m_key_panel_upper), 500, max_panel_stddev) || + is_white(extract_box_reference(screen, m_key_panel_lower), 500, max_panel_stddev); + if (!panel || !is_white(extract_box_reference(screen, m_bottom_bar))){ return false; } - // OK is blue, or flat grey while the field is empty (disabled). + // OK is blue (40, 93, 255), or flat grey (221, 221, 221) while the field is + // empty (disabled). Measured on BDSP and Legends: Z-A. const ImageStats ok = image_stats(extract_box_reference(screen, m_ok_button)); - return is_solid(ok, {0.172, 0.276, 0.552}) || is_grey(ok, 600, 690); + return is_solid(ok, {0.103, 0.240, 0.657}) || is_grey(ok, 600, 690); } -bool find_system_keyboard_cursor(const ImageViewRGB32& screen, int& row, int& col){ +std::optional read_system_keyboard_cursor(const ImageViewRGB32& screen){ // The cursor is a cyan border around one key. Neighbouring keys overlap that border, // so take the centroid of all cyan pixels and map it to the nearest key. - const ImageFloatBox area(0.02, 0.44, 0.84, 0.38); - ImageViewRGB32 keys = extract_box_reference(screen, area); + const ImageViewRGB32 keys = extract_box_reference(screen, KEYS_AREA); double count = 0, sum_x = 0, sum_y = 0; for (size_t y = 0; y < keys.height(); y++){ for (size_t x = 0; x < keys.width(); x++){ - const uint32_t pixel = keys.pixel(x, y); - const int red = (pixel >> 16) & 0xff; - const int green = (pixel >> 8) & 0xff; - const int blue = pixel & 0xff; - if (red < 150 && green > 200 && blue > 190){ + if (is_cursor_pixel(keys.pixel(x, y))){ count++; sum_x += (double)x; sum_y += (double)y; } } } - // The border is about 0.15% of the screen; a few stray pixels are not a cursor. + // The border is about 0.18% of the screen; a few stray pixels are not a cursor. if (count < 0.0005 * screen.width() * screen.height()){ - return false; + return std::nullopt; } - const double cx = area.x + sum_x / count / keys.width() * area.width; - const double cy = area.y + sum_y / count / keys.height() * area.height; - col = (int)std::lround((cx - KEY_COL_X0) / KEY_COL_STEP); - row = 0; - for (int r = 1; r < 4; r++){ - if (std::abs(KEY_ROW_Y[r] - cy) < std::abs(KEY_ROW_Y[row] - cy)){ - row = r; - } - } - return 0 <= col && col < 11; + return key_at( + KEYS_AREA.x + sum_x / count / keys.width() * KEYS_AREA.width, + KEYS_AREA.y + sum_y / count / keys.height() * KEYS_AREA.height + ); } bool system_keyboard_field_has_text(const ImageViewRGB32& screen){ - // Left part of the field, after the caret's position on an empty field. - const ImageStats stats = image_stats(extract_box_reference(screen, ImageFloatBox(0.11, 0.235, 0.19, 0.030))); - return stats.stddev.sum() > 30; + // Left part of the backspace key, clear of its icon and of the cursor border. + // Light grey (221) with an empty field, dark grey (52) with text. + const ImageStats stats = image_stats(extract_box_reference(screen, ImageFloatBox(0.878, 0.470, 0.008, 0.060))); + return stats.average.sum() < 400; } +size_t system_keyboard_text_pixels(const ImageViewRGB32& screen){ + // The field, above its underline, which is white in some games. + const ImageViewRGB32 field = extract_box_reference(screen, ImageFloatBox(0.03, 0.17, 0.87, 0.105)); + size_t count = 0; + for (size_t y = 0; y < field.height(); y++){ + for (size_t x = 0; x < field.width(); x++){ + const uint32_t pixel = field.pixel(x, y); + if (((pixel >> 16) & 0xff) > 190 && ((pixel >> 8) & 0xff) > 190 && (pixel & 0xff) > 190){ + count++; + } + } + } + return count; +} + + namespace{ -// Moves the keyboard's cursor onto (target_row, target_col). A button -// pressed while the keyboard slides in is ignored, so read the cursor again -// after each move until it is on the target. Whether it got there. -bool move_keyboard_cursor(VideoStream& stream, ProControllerContext& context, int target_row, int target_col){ - for (size_t attempt = 0; attempt < 10; attempt++){ - context.wait_for_all_requests(); - int row = -1; - int col = -1; - VideoSnapshot screen = stream.video().snapshot(); - if (!screen || !find_system_keyboard_cursor(screen, row, col)){ - pbf_wait(context, 300ms); - continue; +// One screenshot and what each function should read from it. +class Test_SystemKeyboardDetector : public ImageUnitTest{ +public: + // `cursor`: the key under the cursor, or nothing for a screen without the keyboard. + Test_SystemKeyboardDetector(const std::string& image, std::optional cursor, bool has_text) + : ImageUnitTest(std::format("NintendoSwitch::SystemKeyboardDetector - {}", image), {image}) + , m_cursor(cursor) + , m_has_text(has_text) + {} + + virtual UnitTestResult run_on_images( + Logger& logger, CancellableScope& scope, + const std::vector& images + ) const override{ + const ImageRGB32& image = images[0]; + const bool keyboard = SystemKeyboardDetector().detect(image); + if (keyboard != m_cursor.has_value()){ + return std::format("detect() returned {}.", keyboard); } - if (row == target_row && col == target_col){ + if (!keyboard){ return true; } - for (; row < target_row; row++){ - pbf_press_dpad(context, DPAD_DOWN, 100ms, 200ms); + const std::optional cursor = read_system_keyboard_cursor(image); + if (cursor != m_cursor){ + return cursor + ? std::format("Read the cursor at ({}, {}).", cursor->row, cursor->col) + : std::format("Read no cursor."); } - for (; row > target_row; row--){ - pbf_press_dpad(context, DPAD_UP, 100ms, 200ms); + const bool has_text = system_keyboard_field_has_text(image); + if (has_text != m_has_text){ + return std::format("system_keyboard_field_has_text() returned {}.", has_text); } - for (; col < target_col; col++){ - pbf_press_dpad(context, DPAD_RIGHT, 100ms, 200ms); + // An empty field has no white text pixels; one typed character has many. + const size_t text_pixels = system_keyboard_text_pixels(image); + if ((text_pixels >= 20) != m_has_text){ + return std::format("system_keyboard_text_pixels() returned {}.", text_pixels); } - for (; col > target_col; col--){ - pbf_press_dpad(context, DPAD_LEFT, 100ms, 200ms); + return true; + } + +private: + std::optional m_cursor; + bool m_has_text; +}; + +std::string test_image_path(std::string_view image){ + return std::format("NintendoSwitch/SystemKeyboardDetector/{}", image); +} + +// The cursor on keys other than "1". +void add_cursor_tests(UnitTestDatabase& database){ + // Legends: Z-A, English and Spanish prompts. The + // border pulses between bright cyan and a darker teal; T, / and ! are dark. + const std::pair cursors[] = { + {"PLZA-Cursor-T.png", {1, 4}}, + {"PLZA-Cursor-Slash.png", {1, 10}}, + {"PLZA-Cursor-Exclamation.png", {3, 10}}, + {"PLZA-Cursor-0.png", {0, 9}}, + {"PLZA-Cursor-Minus.png", {0, 10}}, + {"PLZA-Cursor-Backspace.png", {0, 11}}, + {"PLZA-Cursor-Return.png", {1, 11}}, + {"PLZA-Cursor-OK.png", {3, 11}}, + {"PLZA-Cursor-Globe.png", {4, 0}}, + {"PLZA-Cursor-NTilde.png", {4, 5}}, + {"PLZA-Cursor-Space.png", {4, 8}}, + }; + for (const auto& [image, cursor] : cursors){ + database.add(test_image_path(image), cursor, false); + } +} + +// Tests whose screen is not captured yet. +void add_planned_tests(UnitTestDatabase& database){ + const SystemKeyboardKey key_1{0, 0}; + // Screens still to be captured: AZERTY needs a console whose system language + // uses it (the layout follows the system, not the game). Each test is added + // once its image is in the test repo. + struct Planned{ + std::string_view image; + std::optional cursor; + bool has_text; + }; + const Planned planned[] = { + {"AZERTY-Empty.png", key_1, false}, + {"PLZA-Korean.png", std::nullopt, false}, + }; + for (const Planned& test : planned){ + const std::string image = test_image_path(test.image); + if (Filesystem::exists(std::format("{}{}", UNIT_TEST_RESOURCE_PATH(), image))){ + database.add(image, test.cursor, test.has_text); } - pbf_wait(context, 300ms); } - return false; } -} // namespace +} // namespace -void type_on_system_keyboard(VideoStream& stream, ProControllerContext& context, const std::string& text){ - for (char ch : text){ - const char key = (char)std::tolower((unsigned char)ch); - int target_row = -1; - int target_col = -1; - for (int r = 0; r < 4 && target_row < 0; r++){ - const size_t p = KEY_ROWS[r].find(key); - if (p != std::string::npos && key != '\0'){ - target_row = r; - target_col = (int)p; - } - } - if (target_row < 0){ - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - std::string("System keyboard: no key for '") + ch + "'.", - stream - ); - } - if (!move_keyboard_cursor(stream, context, target_row, target_col)){ - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - std::string("System keyboard: the cursor did not reach '") + key + "'.", - stream - ); - } - if (std::isupper((unsigned char)ch)){ - pbf_press_button(context, BUTTON_LCLICK, 100ms, 250ms); - } - pbf_press_button(context, BUTTON_A, 80ms, 300ms); +void add_tests_SystemKeyboardDetector(UnitTestDatabase& database){ + const SystemKeyboardKey key_1{0, 0}; + // The OK button is blue with text in the field and grey without. + database.add(test_image_path("BDSP-PlayerName-Empty.png"), key_1, false); + database.add(test_image_path("BDSP-FriendName-Typed.png"), key_1, true); + for (std::string_view language : {"English", "French", "German", "Italian", "Japanese", "Spanish"}){ + database.add(test_image_path(std::format("PLZA-{}-Empty.png", language)), key_1, false); + } + for (std::string_view image : {"BDSP-Dialog.png", "HomeMenu.png", "HomeMenu-OnlineLicensePopup.png", "UserSelect.png"}){ + database.add(test_image_path(image), std::nullopt, false); } - context.wait_for_all_requests(); + add_cursor_tests(database); + add_planned_tests(database); } -} // namespace NintendoSwitch -} // namespace PokemonAutomation +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h index 6091d9a14f..3a5bbf7f03 100644 --- a/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h +++ b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h @@ -2,28 +2,37 @@ * * From: https://github.com/PokemonAutomation/ * - * The Switch's own software keyboard, which games open for names (e.g. the - * player's name in BDSP): detect it, find the key under its cursor, and type - * a string by moving the cursor and checking where it landed. + * The Switch's own on-screen keyboard, which games open for names (e.g. the + * player's name in BDSP and in Legends: Z-A): detect it, read the key under its + * cursor, and tell whether its text field is empty. Typing with it is in + * Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.h. * - * Measured on 1920x1080 captures of the QWERTY layout. + * Supported: the Latin keyboard (QWERTY; AZERTY shares the key grid but has no + * test screenshot yet). Its layout follows the console's system language, not + * the game's. Measured on 1920x1080 Switch 1 captures of BDSP's and Legends: + * Z-A's name entry, the latter with the game in English, Spanish, German, + * Italian, French and Japanese (all QWERTY on an English console). Not + * supported: the Korean keyboard, whose candidate strip + * moves the keys down (the detector returns false on it), and probably the + * Chinese ones (not tested). Switch 2 is not tested. * */ #ifndef PokemonAutomation_NintendoSwitch_SystemKeyboardDetector_H #define PokemonAutomation_NintendoSwitch_SystemKeyboardDetector_H -#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "CommonFramework/ImageTools/ImageBoxes.h" -#include "CommonFramework/Tools/VideoStream.h" #include "CommonTools/VisualDetector.h" -#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" namespace PokemonAutomation{ namespace NintendoSwitch{ -// White key panel and bottom bar, and the blue OK button. +// The white key panel, the white bar below it, and the OK button (blue, or grey +// while the field is empty). Works wherever the cursor is. class SystemKeyboardDetector : public StaticScreenDetector{ public: SystemKeyboardDetector(Color color = COLOR_CYAN); @@ -33,11 +42,16 @@ class SystemKeyboardDetector : public StaticScreenDetector{ private: Color m_color; - ImageFloatBox m_key_panel; + // Two strips across the key panel, one over the gap between rows 0 and 1 and + // one over the gap between rows 2 and 3. The cursor's border reaches into at + // most one of them, so the panel is white if either is. + ImageFloatBox m_key_panel_upper; + ImageFloatBox m_key_panel_lower; ImageFloatBox m_bottom_bar; ImageFloatBox m_ok_button; }; -// Periodic watcher for the Switch system on-screen keyboard. + +// Finds SystemKeyboardDetector on the video, checking every 250 ms. class SystemKeyboardWatcher : public DetectorToFinder{ public: SystemKeyboardWatcher(Color color = COLOR_CYAN) @@ -46,23 +60,40 @@ class SystemKeyboardWatcher : public DetectorToFinder{ }; -// The key under the cursor, as row (0: digits .. 3: "zxcv") and column (0..10). +// A key of the keyboard. +// Rows 0-3 are the character rows (0: digits .. 3: "zxcv"), row 4 is the bottom +// row (globe, Shift, ABC, #+=, the accent pages and Space). +// Columns 0-10 are the character columns. Column 11 is the right column, whose +// keys are taller: backspace is (0, 11), Return (1, 11) and OK (3, 11). +// A row-4 key reports the character column nearest its centre (Space: 8). +struct SystemKeyboardKey{ + uint8_t row; + uint8_t col; + + bool operator==(const SystemKeyboardKey&) const = default; +}; + +// The key under the cursor, or nothing if no cursor border is visible. // The keyboard remembers the cursor between uses, so read it, don't assume it. -// Returns false if no cursor border is visible. -bool find_system_keyboard_cursor(const ImageViewRGB32& screen, int& row, int& col); +std::optional read_system_keyboard_cursor(const ImageViewRGB32& screen); -// Whether the text field holds any text. Pressing B on an empty field closes the +// Whether the text field holds any text: the backspace key is dark then and light +// grey while the field is empty. Pressing B on an empty field closes the // keyboard, so delete only while this is true. bool system_keyboard_field_has_text(const ImageViewRGB32& screen); -// Types `text` (letters, digits and the symbols on the QWERTY grid). For each character, -// reads the cursor, moves it with the D-pad and checks it arrived before pressing A; an -// uppercase letter is typed with Shift (left stick click). Throws, with a screenshot, if -// the cursor can't be read or doesn't arrive. -void type_on_system_keyboard(VideoStream& stream, ProControllerContext& context, const std::string& text); +// The number of white pixels in the text field. Each typed character adds some +// (a "1" adds about 110 at 1920x1080), so comparing two counts tells whether a +// key press reached the field. +size_t system_keyboard_text_pixels(const ImageViewRGB32& screen); + + + +// Register the tests of this file. +void add_tests_SystemKeyboardDetector(UnitTestDatabase& database); -} // namespace NintendoSwitch -} // namespace PokemonAutomation +} // namespace NintendoSwitch +} // namespace PokemonAutomation #endif diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardCodeEntry.h b/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardCodeEntry.h index 7048582ea0..1aa290cdc1 100644 --- a/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardCodeEntry.h +++ b/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardCodeEntry.h @@ -8,10 +8,12 @@ #define PokemonAutomation_NintendoSwitch_KeyboardCodeEntry_H #include +#include #include "Controllers/StandardHid/StandardHid_Keyboard.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" #include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" #include "NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardEntryMappings.h" +#include "NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_CodeEntryTools.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -19,6 +21,13 @@ namespace FastCodeEntry{ +// The D-pad moves from "source" to "destination" on the on-screen keyboard, +// wrapping around the sides when that is shorter, then ENTER_CHAR. +std::vector keyboard_get_path( + KeyboardEntryPosition source, + KeyboardEntryPosition destination +); + // Automatically select between keyboard or pro controller depending on the // controller type. void keyboard_enter_code( diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.cpp b/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.cpp new file mode 100644 index 0000000000..511e36ecad --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.cpp @@ -0,0 +1,249 @@ +/* System Keyboard Typing + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "NintendoSwitch/NintendoSwitch_Settings.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h" +#include "NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_CodeEntryTools.h" +#include "NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardCodeEntry.h" +#include "NintendoSwitch_SystemKeyboardTyping.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace FastCodeEntry{ + +using namespace std::chrono_literals; + + +namespace{ + +// Waits after each press. They are longer than the code entry delays because +// the next step reads the screen: the cursor animation and the capture latency +// must be over first. +constexpr Milliseconds MOVE_RELEASE = 200ms; +constexpr Milliseconds WRAP_RELEASE = 400ms; // Wrapping around a side animates for longer. +constexpr Milliseconds SETTLE = 300ms; // Before reading the cursor after moving it. +constexpr Milliseconds SHIFT_RELEASE = 250ms; +constexpr Milliseconds KEY_RELEASE = 300ms; + +// A press made while the keyboard is still sliding in is ignored, so the +// cursor is read again after each try until it is on the target. +constexpr size_t MAX_MOVE_TRIES = 10; + +// Reads of the text field after a key press, SETTLE apart, before giving up. +constexpr size_t MAX_FIELD_READS = 3; + +// The fewest white pixels a typed character adds to the field ("." or "'"). +constexpr size_t MIN_CHARACTER_PIXELS = 10; + + +// The key of each character of `text`. Throws UserSetupError, listing every +// character that has no key, before anything is typed. +std::vector text_keys(Logger& logger, const std::string& text, KeyboardLayout layout){ + const std::map& positions = KEYBOARD_POSITIONS(layout); + std::vector keys; + std::string missing; + for (char ch : text){ + // Letters are listed under their uppercase key. + auto iter = positions.find((char)std::toupper((unsigned char)ch)); + if (iter == positions.end()){ + missing += ch; + }else{ + keys.emplace_back(iter->second); + } + } + if (!missing.empty()){ + throw UserSetupError( + logger, + std::format("System keyboard: \"{}\" has characters with no key on the keyboard: \"{}\".", text, missing) + ); + } + return keys; +} + +// How long to hold each button: the hold of the code entry timings for Switch 1. +Milliseconds button_hold(ProControllerContext& context){ + const ConsoleSettings& settings = ConsoleSettings::instance(); + const CodeEntryDelays delays = context->performance_class() == ControllerPerformanceClass::SerialPABotBase_Wired + ? CodeEntryDelays(settings.CODEBOARD_ENTRY_SWITCH1_WIRED) + : CodeEntryDelays(settings.CODEBOARD_ENTRY_SWITCH1_WIRELESS); + return delays.hold; +} + +// The D-pad direction of a move. +DpadPosition move_direction(CodeEntryAction action){ + switch (action){ + case CodeEntryAction::NORM_MOVE_UP: + case CodeEntryAction::WRAP_MOVE_UP: + return DpadPosition::DPAD_UP; + case CodeEntryAction::NORM_MOVE_RIGHT: + case CodeEntryAction::WRAP_MOVE_RIGHT: + return DpadPosition::DPAD_RIGHT; + case CodeEntryAction::NORM_MOVE_DOWN: + case CodeEntryAction::WRAP_MOVE_DOWN: + return DpadPosition::DPAD_DOWN; + case CodeEntryAction::NORM_MOVE_LEFT: + case CodeEntryAction::WRAP_MOVE_LEFT: + return DpadPosition::DPAD_LEFT; + default: + return DpadPosition::DPAD_NONE; + } +} + +// The moves toward `target`. Off the character grid (the right column or the +// bottom row), one step back onto it, after which the cursor is read again. +std::vector path_toward(SystemKeyboardKey cursor, KeyboardEntryPosition target){ + if (cursor.col == 11){ + return {CodeEntryAction::NORM_MOVE_LEFT}; + } + if (cursor.row == 4){ + return {CodeEntryAction::NORM_MOVE_UP}; + } + std::vector path = keyboard_get_path({cursor.row, cursor.col}, target); + std::erase_if(path, [](CodeEntryAction action){ return !is_move(action); }); + return path; +} + +// The moves as letters for the log, e.g. "DLL" or "R*" for a wrap to the right. +std::string path_to_string(const std::vector& path){ + std::string ret; + for (CodeEntryAction action : path){ + switch (move_direction(action)){ + case DpadPosition::DPAD_UP: + ret += 'U'; + break; + case DpadPosition::DPAD_RIGHT: + ret += 'R'; + break; + case DpadPosition::DPAD_DOWN: + ret += 'D'; + break; + case DpadPosition::DPAD_LEFT: + ret += 'L'; + break; + default: + ret += '?'; + break; + } + if (is_wrap(action)){ + ret += '*'; + } + } + return ret; +} + +std::optional read_cursor(VideoStream& stream, ProControllerContext& context){ + context.wait_for_all_requests(); + VideoSnapshot screen = stream.video().snapshot(); + if (!screen){ + return std::nullopt; + } + return read_system_keyboard_cursor(screen); +} + +[[noreturn]] void throw_cursor_failure( + VideoStream& stream, + std::optional last, KeyboardEntryPosition target, char ch +){ + std::string message = last + ? std::format("the cursor is at ({}, {}), not on '{}' at ({}, {})", last->row, last->col, ch, target.row, target.col) + : std::format("the cursor could not be read while moving to '{}' at ({}, {})", ch, target.row, target.col); + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format("System keyboard: {}, after {} tries.", message, MAX_MOVE_TRIES), + stream + ); +} + +void move_cursor( + VideoStream& stream, ProControllerContext& context, Milliseconds hold, + KeyboardEntryPosition target, char ch +){ + std::optional last; + for (size_t attempt = 0; attempt < MAX_MOVE_TRIES; attempt++){ + const std::optional cursor = read_cursor(stream, context); + if (!cursor){ + stream.logger().log(std::format("System keyboard: no cursor seen, try {}.", attempt + 1)); + pbf_wait(context, SETTLE); + continue; + } + last = cursor; + if (cursor->row == target.row && cursor->col == target.col){ + return; + } + const std::vector path = path_toward(*cursor, target); + stream.logger().log(std::format( + "System keyboard: cursor ({}, {}) -> '{}' ({}, {}): {}, try {}.", + cursor->row, cursor->col, ch, target.row, target.col, path_to_string(path), attempt + 1 + )); + for (CodeEntryAction action : path){ + pbf_press_dpad(context, move_direction(action), hold, is_wrap(action) ? WRAP_RELEASE : MOVE_RELEASE); + } + pbf_wait(context, SETTLE); + } + throw_cursor_failure(stream, last, target, ch); +} + +size_t read_text_pixels(VideoStream& stream, ProControllerContext& context){ + context.wait_for_all_requests(); + VideoSnapshot screen = stream.video().snapshot(); + return screen ? system_keyboard_text_pixels(screen) : 0; +} + +// Types `ch` with the cursor on its key, and checks that the text field grew. +void press_key(VideoStream& stream, ProControllerContext& context, Milliseconds hold, char ch){ + const size_t before = read_text_pixels(stream, context); + if (std::isupper((unsigned char)ch)){ + pbf_press_button(context, BUTTON_LCLICK, hold, SHIFT_RELEASE); + } + pbf_press_button(context, BUTTON_A, hold, KEY_RELEASE); + size_t after = 0; + for (size_t read = 0; read < MAX_FIELD_READS; read++){ + after = read_text_pixels(stream, context); + if (after >= before + MIN_CHARACTER_PIXELS){ + return; + } + pbf_wait(context, SETTLE); + } + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format("System keyboard: pressed '{}', but the text field did not change ({} -> {} white pixels).", ch, before, after), + stream + ); +} + +} // namespace + + + +void type_on_system_keyboard( + VideoStream& stream, ProControllerContext& context, + const std::string& text, + KeyboardLayout layout +){ + const std::vector keys = text_keys(stream.logger(), text, layout); + const Milliseconds hold = button_hold(context); + for (size_t c = 0; c < text.size(); c++){ + move_cursor(stream, context, hold, keys[c], text[c]); + press_key(stream, context, hold, text[c]); + } + context.wait_for_all_requests(); +} + + + +} // namespace FastCodeEntry +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.h b/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.h new file mode 100644 index 0000000000..3d2e2dcc2f --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.h @@ -0,0 +1,56 @@ +/* System Keyboard Typing + * + * From: https://github.com/PokemonAutomation/ + * + * Types text on the Switch's on-screen keyboard with a Pro Controller, reading + * the cursor from the video before each key. Unlike keyboard_enter_code(), + * which moves blind from an assumed starting key, it starts wherever the + * keyboard left its cursor and corrects moves the keyboard ignored. + * + * Supported keyboards are those of NintendoSwitch_SystemKeyboardDetector.h: the + * Latin keyboard, QWERTY or AZERTY. Console-tested on Switch 1 (2026-11-13): + * the player's name in Legends: Z-A with the game in English, Spanish, German, + * Italian, French and Japanese (QWERTY, English system language), and earlier + * the player's and rival's names in BDSP. AZERTY is not tested. Not + * supported: the Korean and Chinese keyboards. Switch 2 is not tested. + * + */ + +#ifndef PokemonAutomation_NintendoSwitch_SystemKeyboardTyping_H +#define PokemonAutomation_NintendoSwitch_SystemKeyboardTyping_H + +#include +#include "CommonFramework/Tools/VideoStream.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardEntryMappings.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace FastCodeEntry{ + + +// Types `text` on the open keyboard: letters, digits and the symbols on the +// keyboard's first page. An uppercase letter is typed with Shift (left stick +// click), which assumes the keyboard opened with Shift off, as BDSP's and +// Legends: Z-A's do. +// +// Before the first press, throws UserSetupError if `text` has a character with +// no key. While typing, throws OperationFailedException, with a screenshot, if +// the cursor can't be read or doesn't reach a key, or if a key press doesn't +// add to the text field. The check counts the field's white pixels, so it +// catches a dropped press but not a wrong case. +// +// Presses are held for the "Fast Code Entry Timings" hold of Switch 1 with the +// controller's connection type, as keyboard_enter_code() does. The waits after +// them are fixed, since each move is read back from the video. +void type_on_system_keyboard( + VideoStream& stream, ProControllerContext& context, + const std::string& text, + KeyboardLayout layout = KeyboardLayout::QWERTY +); + + +} // namespace FastCodeEntry +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_AuthoredCheckpointJson_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_AuthoredCheckpointJson_Tests.cpp index 147fa168a9..94f31cf0f0 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_AuthoredCheckpointJson_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_AuthoredCheckpointJson_Tests.cpp @@ -33,42 +33,42 @@ enum class Failure{ MISSING_RESOURCE, }; -struct AuthoredInput{ +struct AuthoredCase{ std::string name; - std::string json; - std::string game = "frlg"; - bool resource = false; - std::string file = "plan"; -}; + struct Input{ + std::string json; + std::string game = "frlg"; + bool resource = false; + std::string file = "plan"; + }; -struct AuthoredExpectation{ - std::string checkpoint = "00.05"; - std::string execution = R"({"menu":"before_pokedex"})"; - std::vector events = {"first", "second"}; - Failure failure = Failure::NONE; -}; + struct Expectation{ + std::string checkpoint = "00.05"; + std::string execution = R"({"menu":"before_pokedex"})"; + std::vector events = {"first", "second"}; + Failure failure = Failure::NONE; + }; -struct AuthoredCase{ - AuthoredInput input; - AuthoredExpectation expectation; + Input input; + Expectation expectation; }; const AuthoredCase CASES[] = { - {.input = {.name = "FRLG envelope", .json = FRLG}, .expectation = {}}, - {.input = {.name = "BDSP embedded resource", .json = BDSP, .game = "bdsp", .resource = true}, .expectation = { + {.name = "FRLG envelope", .input = {.json = FRLG}, .expectation = {}}, + {.name = "BDSP embedded resource", .input = {.json = BDSP, .game = "bdsp", .resource = true}, .expectation = { .checkpoint = "01.03", .execution = R"({"on_failure":"reload_and_retry","saves_in":"Oreburgh City"})", .events = {"roark"}}}, - {.input = {.name = "FRLG embedded resource", .json = FRLG, .resource = true}, .expectation = {}}, - {.input = {.name = "wrong game", .json = BDSP}, .expectation = {.failure = Failure::PARSE}}, - {.input = {.name = "empty expected game", .json = FRLG, .game = ""}, .expectation = {.failure = Failure::PARSE}}, - {.input = {.name = "missing resource", .json = FRLG, .resource = true, .file = "absent"}, .expectation = {.failure = Failure::MISSING_RESOURCE}}, - {.input = {.name = "invalid embedded JSON", .json = "{", .resource = true}, .expectation = {.failure = Failure::PARSE}}, - {.input = {.name = "wrong format", .json = R"({"format":"future","game":"frlg","checkpoint":{},"execution":{}})"}, .expectation = {.failure = Failure::PARSE}}, - {.input = {.name = "missing execution", .json = R"({"format":"pokesim-authored-checkpoint/1","game":"frlg","checkpoint":{}})"}, .expectation = {.failure = Failure::PARSE}}, - {.input = {.name = "same count wrong field", .json = R"({"format":"pokesim-authored-checkpoint/1","game":"frlg","checkpoint":{},"extra":{}})"}, .expectation = {.failure = Failure::PARSE}}, - {.input = {.name = "extra envelope field", .json = R"({"format":"pokesim-authored-checkpoint/1","game":"frlg","checkpoint":{},"execution":{},"extra":0})"}, .expectation = {.failure = Failure::PARSE}}, - {.input = {.name = "execution must be object", .json = R"({"format":"pokesim-authored-checkpoint/1","game":"frlg","checkpoint":{"id":"x","title":"X","events":[]},"execution":false})"}, .expectation = {.failure = Failure::PARSE}}, - {.input = {.name = "invalid common action", .json = R"({"format":"pokesim-authored-checkpoint/1","game":"frlg","checkpoint":{"id":"x","title":"X","events":[{"id":"bad","kind":"magic"}]},"execution":{}})"}, .expectation = {.failure = Failure::PARSE}}, - {.input = {.name = "execution stays game owned", .json = R"({"format":"pokesim-authored-checkpoint/1","game":"future","checkpoint":{"id":"x","title":"X","events":[]},"execution":{"zero":0,"false":false,"null":null}})", .game = "future"}, .expectation = { + {.name = "FRLG embedded resource", .input = {.json = FRLG, .resource = true}, .expectation = {}}, + {.name = "wrong game", .input = {.json = BDSP}, .expectation = {.failure = Failure::PARSE}}, + {.name = "empty expected game", .input = {.json = FRLG, .game = ""}, .expectation = {.failure = Failure::PARSE}}, + {.name = "missing resource", .input = {.json = FRLG, .resource = true, .file = "absent"}, .expectation = {.failure = Failure::MISSING_RESOURCE}}, + {.name = "invalid embedded JSON", .input = {.json = "{", .resource = true}, .expectation = {.failure = Failure::PARSE}}, + {.name = "wrong format", .input = {.json = R"({"format":"future","game":"frlg","checkpoint":{},"execution":{}})"}, .expectation = {.failure = Failure::PARSE}}, + {.name = "missing execution", .input = {.json = R"({"format":"pokesim-authored-checkpoint/1","game":"frlg","checkpoint":{}})"}, .expectation = {.failure = Failure::PARSE}}, + {.name = "same count wrong field", .input = {.json = R"({"format":"pokesim-authored-checkpoint/1","game":"frlg","checkpoint":{},"extra":{}})"}, .expectation = {.failure = Failure::PARSE}}, + {.name = "extra envelope field", .input = {.json = R"({"format":"pokesim-authored-checkpoint/1","game":"frlg","checkpoint":{},"execution":{},"extra":0})"}, .expectation = {.failure = Failure::PARSE}}, + {.name = "execution must be object", .input = {.json = R"({"format":"pokesim-authored-checkpoint/1","game":"frlg","checkpoint":{"id":"x","title":"X","events":[]},"execution":false})"}, .expectation = {.failure = Failure::PARSE}}, + {.name = "invalid common action", .input = {.json = R"({"format":"pokesim-authored-checkpoint/1","game":"frlg","checkpoint":{"id":"x","title":"X","events":[{"id":"bad","kind":"magic"}]},"execution":{}})"}, .expectation = {.failure = Failure::PARSE}}, + {.name = "execution stays game owned", .input = {.json = R"({"format":"pokesim-authored-checkpoint/1","game":"future","checkpoint":{"id":"x","title":"X","events":[]},"execution":{"zero":0,"false":false,"null":null}})", .game = "future"}, .expectation = { .checkpoint = "x", .execution = R"({"zero":0,"false":false,"null":null})", .events = {}}}, }; @@ -116,7 +116,7 @@ std::string check_case(const AuthoredCase& test){ Loaded actual; actual.checkpoint = test.input.resource ? load_authored_checkpoint(files, test.input.file, test.input.game, keep_execution(actual.execution)) - : read_authored_checkpoint(parse_json(test.input.json), test.input.game, test.input.name, keep_execution(actual.execution)); + : read_authored_checkpoint(parse_json(test.input.json), test.input.game, test.name, keep_execution(actual.execution)); return check_loaded(actual, test); }catch (const ParseException& error){ return test.expectation.failure == Failure::PARSE ? "" : std::format("Unexpected parse failure: {}", error.message()); @@ -131,7 +131,7 @@ std::string check_case(const AuthoredCase& test){ class AuthoredJsonTest : public UnitTest{ public: explicit AuthoredJsonTest(AuthoredCase test) - : UnitTest(std::format("Pokemon::AuthoredCheckpointJson - {}", test.input.name)) + : UnitTest(std::format("Pokemon::AuthoredCheckpointJson - {}", test.name)) , m_case(std::move(test)) {} @@ -172,7 +172,7 @@ int main(){ int failed = 0; for (const auto& test : PokemonAutomation::Pokemon::CASES){ const std::string error = PokemonAutomation::Pokemon::check_case(test); - std::cout << test.input.name << ": " << (error.empty() ? "PASS" : error) << '\n'; + std::cout << test.name << ": " << (error.empty() ? "PASS" : error) << '\n'; failed += !error.empty(); } return failed != 0; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryDevFiles.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryDevFiles.cpp new file mode 100644 index 0000000000..8da82c99d2 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryDevFiles.cpp @@ -0,0 +1,38 @@ +/* Story Dev Files + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Common/Cpp/Filesystem/FileIO.h" +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "Pokemon_StoryDevFiles.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +std::string story_dev_folder(std::string_view game){ + return std::format("{}AutoStoryDev/{}/", USER_FILE_PATH(), game); +} + +std::vector read_story_dev_files(std::string_view folder, std::string_view game){ + const Filesystem::Path directory(std::string{folder}); + std::vector files; + const std::string prefix = std::format("{}-", game); + for (const Filesystem::Path& path : Filesystem::list_directory(directory)){ + const std::string name = path.filename().string(); + if (!name.starts_with(prefix) || !name.ends_with(".plan.json") || !Filesystem::is_regular_file(path)){ + continue; + } + files.push_back({.name = name, .path = path.string(), .text = file_to_string(path.string())}); + } + return files; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryDevFiles.h b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryDevFiles.h new file mode 100644 index 0000000000..fd572d6567 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryDevFiles.h @@ -0,0 +1,41 @@ +/* Story Dev Files + * + * From: https://github.com/PokemonAutomation/ + * + * For lab tuning: checkpoint files on disk that a run reads in place of + * the built-in ones of the same name (load_story_data() in + * Pokemon_StoryFiles.h), so a walk can be retimed without a rebuild. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryDevFiles_H +#define PokemonAutomation_Pokemon_StoryDevFiles_H + +#include +#include +#include + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// A checkpoint file read from disk. +struct StoryDevFile{ + std::string name; // "plza-02.01.plan.json" + std::string path; + std::string text; +}; + +// The folder whose checkpoint files replace `game`'s built-in ones: +// /AutoStoryDev// (USER_FILE_PATH()). +std::string story_dev_folder(std::string_view game); + +// The -*.plan.json files in `folder`, by name; none if it does not +// exist. +std::vector read_story_dev_files(std::string_view folder, std::string_view game); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.cpp new file mode 100644 index 0000000000..3c958c9feb --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.cpp @@ -0,0 +1,127 @@ +/* Story Files + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Pokemon_StoryFiles.h" + +namespace PokemonAutomation{ +namespace Pokemon{ +namespace detail{ + + +void check_dev_file_names(std::span builtin, std::span dev_files){ + for (const StoryDevFile& dev : dev_files){ + bool known = false; + for (const auto& [name, text] : builtin){ + known |= dev.name == std::format("{}.json", name); + } + if (!known){ + throw ParseException(std::format("{}: no built-in checkpoint file is named {}", dev.path, dev.name)); + } + } +} + +const StoryDevFile* find_dev_file(std::span dev_files, std::string_view builtin_name){ + const std::string file_name = std::format("{}.json", builtin_name); + for (const StoryDevFile& file : dev_files){ + if (file.name == file_name){ + return &file; + } + } + return nullptr; +} + +ParseException naming_path(const ParseException& error, std::string_view path){ + const std::string message = error.message(); + if (message.find(path) != std::string::npos){ + return error; + } + return ParseException(std::format("{}: {}", path, message)); +} + +void check_dev_checkpoint( + std::string_view path, std::string_view id, std::string_view builtin_id, const std::vector& problems +){ + if (id != builtin_id){ + throw ParseException(std::format("{}: checkpoint id {} does not match the built-in {}", path, id, builtin_id)); + } + if (!problems.empty()){ + throw ParseException(std::format("{}: {}", path, problems.front())); + } +} + +void check_in_story_order( + const std::function& in_story_order, std::string_view id, std::string_view source +){ + if (in_story_order && !in_story_order(id)){ + throw ParseException(std::format("{}: checkpoint {} is not in the game's story order", source, id)); + } +} + +namespace{ + +// Which row sequence the reload check applies to. +enum class RunKind{ + DEVELOPER, + USER, +}; + +// Text naming `run` in a reload error. +std::string_view run_suffix(RunKind run){ + switch (run){ + case RunKind::DEVELOPER: + return ""; + case RunKind::USER: + return " in a user's run"; + } + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Unknown story run kind."); +} + +// Throws ParseException naming `source` unless `previous` ends on the +// game's autosave in `rows`, for `run`. +void check_reload_after( + std::optional previous, const StoryRows& rows, RunKind run, std::string_view source +){ + if (!previous || !rows.autosaves.contains(*previous)){ + throw ParseException(std::format( + "{}: reload_when_chained after a checkpoint that does not end on the game's autosave{}: the reload would load an older save", + source, run_suffix(run) + )); + } +} + +} // namespace + +void add_story_row( + std::string_view id, const StoryRowExecution& execution, RowSave start, + const PreviousRows& previous, std::string_view source, StoryRows& rows +){ + if (execution.reload != StoryReload::NEVER){ + if (start == RowSave::WITHOUT_SAVE){ + throw ParseException(std::format("{}: reload_when_chained on a checkpoint with no save to reload", source)); + } + check_reload_after(previous.in_dev_run, rows, RunKind::DEVELOPER, source); + if (!execution.dev_only && previous.in_user_run != previous.in_dev_run){ + check_reload_after(previous.in_user_run, rows, RunKind::USER, source); + } + rows.reload_when_chained.emplace(id); + } + if (execution.reload == StoryReload::ALWAYS){ + rows.reload_always.emplace(id); + } + if (execution.autosave){ + rows.autosaves.emplace(id); + } + if (execution.dev_only){ + rows.dev_only.emplace(id); + } +} + + + +} // namespace detail +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h new file mode 100644 index 0000000000..f6d2b1da3a --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h @@ -0,0 +1,238 @@ +/* Story Files + * + * From: https://github.com/PokemonAutomation/ + * + * A game's story read from its checkpoint files: the checked-in ones, + * built into the program (cmake/EmbedTextFiles.cmake), each replaced by + * the dev file of the same name if there is one (Pokemon_StoryDevFiles.h), + * so a walk can be retimed on the console without a rebuild. The game + * reads each file into its own checkpoint type; this reads the shared + * execution fields (Pokemon_StoryRowExecution.h) into the rows they mark, + * and checks what holds for every game: a dev file replaces a built-in + * file, keeps its checkpoint's id and passes the game's lint, and a row + * reloads only a save it can count on. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryFiles_H +#define PokemonAutomation_Pokemon_StoryFiles_H + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Pokemon_AuthoredCheckpointJson.h" +#include "Pokemon_StoryDevFiles.h" +#include "Pokemon_StoryRowExecution.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// What a game reads from one checkpoint file. +template +struct StoryFileRow{ + Checkpoint checkpoint; + StoryRowExecution execution; +}; + +// How a game reads its checkpoint files. +template +struct StoryFileReader{ + // The file `text`'s checkpoint and shared execution fields + // (read_story_row_execution()); `source` names the file in errors. + // Throws ParseException when it is malformed. + std::function(std::string_view text, std::string_view source)> read; + // The problems the game finds in a dev file's checkpoint; the first + // one refuses the file. None set: no lint. + std::function(const Checkpoint&)> lint = {}; + // Whether a row starts with no save to load (a new game): it may not + // reload. None set: every row starts from a save. + std::function starts_without_save = {}; + // Whether the game plays checkpoint `a` before `b`, when that is not + // the order of its files' names. None set: file-name order. + std::function plays_before = {}; + // Whether the game's story order lists checkpoint `id`: a file whose + // id it does not list is refused, not skipped or played out of place. + // None set: the game keeps no list, and every id plays. + std::function in_story_order = {}; +}; + + +// The story of the built-in checkpoint files `builtin`, in their order or +// the game's (StoryFileReader::plays_before), each of `dev_files` in place +// of the built-in file of its name. Throws ParseException naming the file +// when a file does not parse, a dev file matches no built-in file, changes +// its checkpoint's id or fails the game's lint, a file's checkpoint id is +// not in the game's story order (StoryFileReader::in_story_order), or a +// row reloads a save it cannot count on in the order the story plays. +template +StoryData load_story_data( + std::span builtin, std::span dev_files, + const StoryFileReader& reader +); + +// Who a run's rows are listed for. +enum class StoryRowsShown{ + USER, // every row but the dev-only ones + DEVELOPER, // every row (the program's Developer Mode) +}; + +// The rows of `story` a run may play, in order. The start and end +// dropdowns list these, so their indices name these rows. +template +std::vector story_rows_for_mode(const StoryData& story, StoryRowsShown shown); + +// The rows a run from `first` to `last` of `rows` plays, in order. Empty +// if `first` comes after `last` or `last` is past the end. +template +std::vector story_run_rows(std::span rows, size_t first, size_t last); + + + +namespace detail{ + +// Throws ParseException naming the dev file that matches no built-in file. +void check_dev_file_names(std::span builtin, std::span dev_files); +// The dev file that replaces the built-in file `builtin_name` (its name +// without ".json"); null if none. +const StoryDevFile* find_dev_file(std::span dev_files, std::string_view builtin_name); +// `error`, its message prefixed with `path` unless it already names it. +ParseException naming_path(const ParseException& error, std::string_view path); +// Throws ParseException naming `path` when the dev file's checkpoint `id` +// is not the built-in file's `builtin_id`, or `problems` is not empty. +void check_dev_checkpoint( + std::string_view path, std::string_view id, std::string_view builtin_id, const std::vector& problems +); + +// Throws ParseException naming `source` and `id` when `in_story_order` +// is set and does not list `id`. +void check_in_story_order( + const std::function& in_story_order, std::string_view id, std::string_view source +); + +// Whether a row starts from a save, which a reload can load. +enum class RowSave{ + FROM_SAVE, + WITHOUT_SAVE, // a new game +}; + +// The rows a row follows in play: in a developer's run, and in a user's, +// which skips the dev-only rows. None for the first row. +struct PreviousRows{ + std::optional in_dev_run; + std::optional in_user_run; +}; + +// Adds row `id` to the sets of `rows`. Throws ParseException naming +// `source` when it reloads and starts WITHOUT_SAVE or a row it follows +// (`previous`: in a developer's run; and in a user's, unless it is +// dev-only itself) does not end on the game's autosave: a reload loads +// the last save, which must be that autosave. +void add_story_row( + std::string_view id, const StoryRowExecution& execution, RowSave start, + const PreviousRows& previous, std::string_view source, StoryRows& rows +); + +// The dev file `dev`'s row, in place of the built-in file `builtin_name`: +// it must parse, keep the built-in checkpoint's id and pass the lint. +template +StoryFileRow read_dev_file( + const StoryDevFile& dev, std::string_view builtin_text, std::string_view builtin_name, + const StoryFileReader& reader +){ + StoryFileRow row; + try{ + row = reader.read(dev.text, dev.path); + }catch (const ParseException& error){ + // Malformed JSON says nothing of where it came from. + throw naming_path(error, dev.path); + } + const std::string builtin_id = reader.read(builtin_text, builtin_name).checkpoint.id; + const std::vector problems = reader.lint ? reader.lint(row.checkpoint) : std::vector{}; + check_dev_checkpoint(dev.path, row.checkpoint.id, builtin_id, problems); + return row; +} + +} // namespace detail + + +template +StoryData load_story_data( + std::span builtin, std::span dev_files, + const StoryFileReader& reader +){ + detail::check_dev_file_names(builtin, dev_files); + StoryData data; + struct SourcedRow{ + StoryFileRow row; + std::string_view source; + }; + std::vector rows; + for (const auto& [name, text] : builtin){ + const StoryDevFile* dev = detail::find_dev_file(dev_files, name); + if (dev == nullptr){ + rows.push_back({reader.read(text, name), name}); + continue; + } + rows.push_back({detail::read_dev_file(*dev, text, name, reader), dev->path}); + data.dev_files.emplace_back(dev->name, dev->path); + } + for (const auto& [row, source] : rows){ + detail::check_in_story_order(reader.in_story_order, row.checkpoint.id, source); + } + if (reader.plays_before){ + std::stable_sort(rows.begin(), rows.end(), [&](const SourcedRow& a, const SourcedRow& b){ + return reader.plays_before(a.row.checkpoint, b.row.checkpoint); + }); + } + // A reload loads the save the row before it in play ends on, in a + // developer's run and in a user's. + std::optional last_user_row; + for (auto& [row, source] : rows){ + const bool new_game = reader.starts_without_save && reader.starts_without_save(row.checkpoint); + const detail::PreviousRows previous{ + .in_dev_run = data.checkpoints.empty() ? std::nullopt : std::optional(data.checkpoints.back().id), + .in_user_run = last_user_row ? std::optional(data.checkpoints[*last_user_row].id) : std::nullopt, + }; + detail::add_story_row( + row.checkpoint.id, row.execution, + new_game ? detail::RowSave::WITHOUT_SAVE : detail::RowSave::FROM_SAVE, + previous, source, data + ); + if (!row.execution.dev_only){ + last_user_row = data.checkpoints.size(); + } + data.checkpoints.push_back(std::move(row.checkpoint)); + } + return data; +} + +template +std::vector story_rows_for_mode(const StoryData& story, StoryRowsShown shown){ + std::vector rows; + for (const Checkpoint& row : story.checkpoints){ + if (shown == StoryRowsShown::DEVELOPER || !story.dev_only.contains(row.id)){ + rows.push_back(row); + } + } + return rows; +} + +template +std::vector story_run_rows(std::span rows, size_t first, size_t last){ + if (first > last || last >= rows.size()){ + return {}; + } + return std::vector(rows.begin() + first, rows.begin() + last + 1); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp new file mode 100644 index 0000000000..ab6a3c5f01 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp @@ -0,0 +1,410 @@ +/* Story Files Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Json/JsonValue.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon_StoryFiles.h" +#include "Pokemon_StoryFiles_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + +// A checkpoint file of the test game: {"id", "title", "execution"}, its +// execution the shared fields and the game's own "note". Its lint refuses +// the title "unlinted"; 00.00 is its new game, which starts without a save. +std::string file_text(std::string_view id, std::string_view execution, std::string_view title = "Row"){ + return std::format(R"({{"id": "{}", "title": "{}", "execution": {}}})", id, title, execution); +} + +StoryFileRow read_test_file(std::string_view text, std::string_view source){ + const JsonValue value = parse_json(std::string(text)); + const JsonObject& object = value.to_object_throw(std::string(source)); + StoryFileRow row; + row.checkpoint.id = object.get_string_throw("id", std::string(source)); + row.checkpoint.title = object.get_string_throw("title", std::string(source)); + row.execution = read_story_row_execution(object.get_object_throw("execution", std::string(source)), source, {"note"}); + return row; +} + +const StoryFileReader TEST_READER{ + .read = read_test_file, + .lint = [](const StoryCheckpoint& checkpoint){ + return checkpoint.title == "unlinted" ? std::vector{"title is unlinted"} : std::vector{}; + }, + .starts_without_save = [](const StoryCheckpoint& checkpoint){ + return checkpoint.id == "00.00"; + }, +}; + +// "a b": the strings, space-separated. +std::string joined(const std::vector& texts){ + std::string text; + for (const std::string& part : texts){ + text += (text.empty() ? "" : " ") + part; + } + return text; +} +std::string joined(const std::set>& texts){ + return joined(std::vector(texts.begin(), texts.end())); +} + +std::vector ids(const std::vector& rows){ + std::vector result; + for (const StoryCheckpoint& row : rows){ + result.push_back(row.id); + } + return result; +} + +// "rows 01.00 01.01 | autosave 01.00 | chained 01.01 | always | dev_only | dev x.json" +std::string summary(const StoryData& data){ + std::vector dev; + for (const auto& [name, path] : data.dev_files){ + dev.push_back(std::format("{}@{}", name, path)); + } + std::vector titles; + for (const StoryCheckpoint& row : data.checkpoints){ + titles.push_back(row.title); + } + return std::format( + "rows {} | titles {} | autosave {} | chained {} | always {} | dev_only {} | dev {}", + joined(ids(data.checkpoints)), joined(titles), joined(data.autosaves), joined(data.reload_when_chained), + joined(data.reload_always), joined(data.dev_only), joined(dev) + ); +} + + +struct LoadCase{ + std::string name; + struct Input{ + std::vector builtin; + std::vector dev_files = {}; + // Whether the game plays its rows in id order, not file-name order. + bool ordered_by_id = false; + // The ids the game's story order lists; none: the game lists none. + std::optional> story_order = std::nullopt; + }; + Input input; + struct Expectation{ + // summary() of the story read; none when it fails. + std::optional summary = std::nullopt; + // What the ParseException's message contains when it fails. + std::string error = {}; + }; + Expectation expectation; +}; + +const EmbeddedJsonFile ROW_0 = {"test-01.00.plan", file_text("01.00", R"({"save": "autosave"})")}; +const EmbeddedJsonFile ROW_1 = {"test-01.01.plan", file_text("01.01", R"({"reload_when_chained": true, "note": 1})")}; +const EmbeddedJsonFile ROW_2 = {"test-01.02.plan", file_text("01.02", R"({"dev_only": true, "save": "menu"})")}; + +StoryDevFile dev_file(std::string_view name, std::string text){ + return StoryDevFile{.name = std::string(name), .path = std::format("/dev/{}", name), .text = std::move(text)}; +} + +// Named to come after 01.01 by file name, before it by id. +const EmbeddedJsonFile ROW_0B_MENU = {"test-01.10.plan", file_text("01.00b", R"({"save": "menu"})")}; + +const LoadCase LOAD_CASES[] = { + { + .name = "built-in files in order, their shared execution fields read", + .input = {.builtin = {ROW_0, ROW_1, ROW_2}}, + .expectation = {.summary = "rows 01.00 01.01 01.02 | titles Row Row Row | autosave 01.00 | chained 01.01 | always | dev_only 01.02 | dev "}, + }, + { + .name = "reload_when_chained always", + .input = {.builtin = {ROW_0, {"test-01.01.plan", file_text("01.01", R"({"reload_when_chained": "always"})")}}}, + .expectation = {.summary = "rows 01.00 01.01 | titles Row Row | autosave 01.00 | chained 01.01 | always 01.01 | dev_only | dev "}, + }, + { + .name = "a dev file replaces the built-in file of its name and is named", + .input = { + .builtin = {ROW_0, ROW_1}, + .dev_files = {dev_file("test-01.01.plan.json", file_text("01.01", R"({"reload_when_chained": true})", "Retimed"))}, + }, + .expectation = {.summary = "rows 01.00 01.01 | titles Row Retimed | autosave 01.00 | chained 01.01 | always | dev_only | dev test-01.01.plan.json@/dev/test-01.01.plan.json"}, + }, + { + .name = "a dev file that is not JSON fails naming its path", + .input = {.builtin = {ROW_0}, .dev_files = {dev_file("test-01.00.plan.json", "{ not json")}}, + .expectation = {.error = "/dev/test-01.00.plan.json"}, + }, + { + .name = "a dev file with no built-in file of its name fails", + .input = {.builtin = {ROW_0}, .dev_files = {dev_file("test-09.99.plan.json", file_text("09.99", "{}"))}}, + .expectation = {.error = "/dev/test-09.99.plan.json: no built-in checkpoint file is named test-09.99.plan.json"}, + }, + { + .name = "a dev file that changes its checkpoint's id fails", + .input = {.builtin = {ROW_0}, .dev_files = {dev_file("test-01.00.plan.json", file_text("01.09", R"({"save": "autosave"})"))}}, + .expectation = {.error = "/dev/test-01.00.plan.json: checkpoint id 01.09 does not match the built-in 01.00"}, + }, + { + .name = "a dev file the game's lint refuses fails", + .input = {.builtin = {ROW_0}, .dev_files = {dev_file("test-01.00.plan.json", file_text("01.00", "{}", "unlinted"))}}, + .expectation = {.error = "/dev/test-01.00.plan.json: title is unlinted"}, + }, + { + .name = "an unknown execution field fails", + .input = {.builtin = {{"test-01.00.plan", file_text("01.00", R"({"dev_mode": true})")}}}, + .expectation = {.error = "unknown execution field dev_mode"}, + }, + { + .name = "dev_only false fails", + .input = {.builtin = {{"test-01.00.plan", file_text("01.00", R"({"dev_only": false})")}}}, + .expectation = {.error = "dev_only is true or left out"}, + }, + { + .name = "a save other than autosave or menu fails", + .input = {.builtin = {{"test-01.00.plan", file_text("01.00", R"({"save": "card"})")}}}, + .expectation = {.error = "save is \"autosave\", \"menu\" or left out"}, + }, + { + .name = "reload_when_chained false fails", + .input = {.builtin = {ROW_0, {"test-01.01.plan", file_text("01.01", R"({"reload_when_chained": false})")}}}, + .expectation = {.error = "reload_when_chained is true, \"always\" or left out"}, + }, + { + .name = "reload_when_chained after a row that does not autosave fails", + .input = {.builtin = {{"test-01.00.plan", file_text("01.00", R"({"save": "menu"})")}, ROW_1}}, + .expectation = {.error = "test-01.01.plan: reload_when_chained after a checkpoint that does not end on the game's autosave"}, + }, + { + .name = "a dev-only reload after a dev-only autosave row, the user's row before it not autosaving, loads", + .input = {.builtin = { + {"test-01.00.plan", file_text("01.00", R"({"save": "menu"})")}, + {"test-01.005.plan", file_text("01.005", R"({"dev_only": true, "save": "autosave"})")}, + {"test-01.01.plan", file_text("01.01", R"({"dev_only": true, "reload_when_chained": true})")}, + }}, + .expectation = {.summary = "rows 01.00 01.005 01.01 | titles Row Row Row | autosave 01.005 | chained 01.01 | always | dev_only 01.005 01.01 | dev "}, + }, + { + .name = "reload_when_chained after a dev-only autosave row, the user's row before it not autosaving, fails", + .input = {.builtin = { + {"test-01.00.plan", file_text("01.00", R"({"save": "menu"})")}, + {"test-01.005.plan", file_text("01.005", R"({"dev_only": true, "save": "autosave"})")}, + ROW_1, + }}, + .expectation = {.error = "test-01.01.plan: reload_when_chained after a checkpoint that does not end on the game's autosave in a user's run"}, + }, + { + .name = "reload_when_chained after a dev-only row, both rows before it autosaving", + .input = {.builtin = { + ROW_0, + {"test-01.005.plan", file_text("01.005", R"({"dev_only": true, "save": "autosave"})")}, + ROW_1, + }}, + .expectation = {.summary = "rows 01.00 01.005 01.01 | titles Row Row Row | autosave 01.00 01.005 | chained 01.01 | always | dev_only 01.005 | dev "}, + }, + { + .name = "reload_when_chained on the first row fails", + .input = {.builtin = {ROW_1}}, + .expectation = {.error = "does not end on the game's autosave"}, + }, + { + .name = "reload_when_chained on the new game fails", + .input = {.builtin = {ROW_0, {"test-00.00.plan", file_text("00.00", R"({"reload_when_chained": true})")}}}, + .expectation = {.error = "test-00.00.plan: reload_when_chained on a checkpoint with no save to reload"}, + }, + { + .name = "the game's order, its reload after the autosave row it follows there", + .input = {.builtin = {ROW_1, ROW_0}, .ordered_by_id = true}, + .expectation = {.summary = "rows 01.00 01.01 | titles Row Row | autosave 01.00 | chained 01.01 | always | dev_only | dev "}, + }, + { + .name = "the game's order, its reload after a row that does not autosave there fails", + .input = {.builtin = {ROW_0, ROW_1, ROW_0B_MENU}, .ordered_by_id = true}, + .expectation = {.error = "test-01.01.plan: reload_when_chained after a checkpoint that does not end on the game's autosave"}, + }, + { + .name = "every id in the game's story order loads", + .input = {.builtin = {ROW_0, ROW_1}, .story_order = std::vector{"01.00", "01.01"}}, + .expectation = {.summary = "rows 01.00 01.01 | titles Row Row | autosave 01.00 | chained 01.01 | always | dev_only | dev "}, + }, + { + .name = "a built-in file whose id is not in the game's story order fails naming the file and the id", + .input = {.builtin = {ROW_0, ROW_1}, .story_order = std::vector{"01.00"}}, + .expectation = {.error = "test-01.01.plan: checkpoint 01.01 is not in the game's story order"}, + }, + { + .name = "a dev file in the game's story order replaces the built-in file", + .input = { + .builtin = {ROW_0, ROW_1}, + .dev_files = {dev_file("test-01.01.plan.json", file_text("01.01", R"({"reload_when_chained": true})", "Retimed"))}, + .story_order = std::vector{"01.00", "01.01"}, + }, + .expectation = {.summary = "rows 01.00 01.01 | titles Row Retimed | autosave 01.00 | chained 01.01 | always | dev_only | dev test-01.01.plan.json@/dev/test-01.01.plan.json"}, + }, + { + .name = "a dev file whose id is not in the game's story order fails naming its path and the id", + .input = { + .builtin = {ROW_0}, + .dev_files = {dev_file("test-01.00.plan.json", file_text("01.00", R"({"save": "autosave"})"))}, + .story_order = std::vector{"01.01"}, + }, + .expectation = {.error = "/dev/test-01.00.plan.json: checkpoint 01.00 is not in the game's story order"}, + }, +}; + +UnitTestResult check_load(const LoadCase& test){ + StoryFileReader reader = TEST_READER; + if (test.input.ordered_by_id){ + reader.plays_before = [](const StoryCheckpoint& a, const StoryCheckpoint& b){ + return a.id < b.id; + }; + } + if (test.input.story_order){ + reader.in_story_order = [&order = *test.input.story_order](std::string_view id){ + return std::find(order.begin(), order.end(), id) != order.end(); + }; + } + std::string got; + std::string error; + try{ + got = summary(load_story_data(test.input.builtin, test.input.dev_files, reader)); + }catch (const ParseException& e){ + error = e.message(); + } + if (test.expectation.summary){ + if (!error.empty()){ + return std::format("expected \"{}\", failed: {}", *test.expectation.summary, error); + } + if (got != *test.expectation.summary){ + return std::format("expected \"{}\", got \"{}\"", *test.expectation.summary, got); + } + return true; + } + if (error.find(test.expectation.error) == std::string::npos){ + return std::format("expected a failure with \"{}\", got \"{}\"", test.expectation.error, error.empty() ? got : error); + } + return true; +} + + +struct RowsCase{ + std::string name; + struct Input{ + StoryRowsShown shown = StoryRowsShown::USER; + size_t first = 0; + size_t last = 0; + }; + Input input; + struct Expectation{ + std::string rows = {}; + }; + Expectation expectation; +}; + +// 01.00, 01.01, 01.02 with 01.01 dev-only. +const RowsCase ROWS_CASES[] = { + {.name = "Developer Mode lists every row", .input = {.shown = StoryRowsShown::DEVELOPER, .first = 0, .last = 2}, .expectation = {.rows = "01.00 01.01 01.02"}}, + {.name = "a user's run skips the dev-only rows", .input = {.shown = StoryRowsShown::USER, .first = 0, .last = 1}, .expectation = {.rows = "01.00 01.02"}}, + {.name = "a run from one row to a later one", .input = {.shown = StoryRowsShown::DEVELOPER, .first = 1, .last = 2}, .expectation = {.rows = "01.01 01.02"}}, + {.name = "no rows for a start after the end", .input = {.shown = StoryRowsShown::DEVELOPER, .first = 2, .last = 1}, .expectation = {.rows = ""}}, + {.name = "no rows for an end past the table", .input = {.shown = StoryRowsShown::USER, .first = 0, .last = 2}, .expectation = {.rows = ""}}, +}; + +UnitTestResult check_rows(const RowsCase& test){ + StoryData story; + for (std::string_view id : {"01.00", "01.01", "01.02"}){ + StoryCheckpoint row; + row.id = std::string(id); + story.checkpoints.push_back(std::move(row)); + } + story.dev_only.emplace("01.01"); + const std::vector shown = story_rows_for_mode(story, test.input.shown); + const std::string got = joined(ids(story_run_rows(shown, test.input.first, test.input.last))); + if (got != test.expectation.rows){ + return std::format("expected \"{}\", got \"{}\"", test.expectation.rows, got); + } + return true; +} + + +struct ReloadCase{ + std::string name; + struct Input{ + StoryRunPolicy::BoundaryReload boundary_reload = StoryRunPolicy::BoundaryReload::AT_PLAN_BOUNDARIES; + std::string row = {}; + }; + Input input; + struct Expectation{ + bool reloads = false; + }; + Expectation expectation; +}; + +// 01.01 reloads when chained, 01.02 always. +const ReloadCase RELOAD_CASES[] = { + {.name = "at plan boundaries, a when-chained row reloads", .input = {.boundary_reload = StoryRunPolicy::BoundaryReload::AT_PLAN_BOUNDARIES, .row = "01.01"}, .expectation = {.reloads = true}}, + {.name = "straight through, a when-chained row runs on", .input = {.boundary_reload = StoryRunPolicy::BoundaryReload::STRAIGHT_THROUGH, .row = "01.01"}, .expectation = {.reloads = false}}, + {.name = "straight through, an always row reloads", .input = {.boundary_reload = StoryRunPolicy::BoundaryReload::STRAIGHT_THROUGH, .row = "01.02"}, .expectation = {.reloads = true}}, + {.name = "a row with no reload runs on", .input = {.boundary_reload = StoryRunPolicy::BoundaryReload::AT_PLAN_BOUNDARIES, .row = "01.00"}, .expectation = {.reloads = false}}, +}; + +UnitTestResult check_reload(const ReloadCase& test){ + StoryRows rows; + rows.reload_when_chained = {"01.01", "01.02"}; + rows.reload_always = {"01.02"}; + const bool got = story_reloads_before(rows, test.input.boundary_reload, test.input.row); + if (got != test.expectation.reloads){ + return std::format("expected reloads {}, got {}", test.expectation.reloads, got); + } + return true; +} + + +template +class StoryFilesTest : public UnitTest{ +public: + using Check = UnitTestResult (*)(const Case&); + + StoryFilesTest(std::string_view suite, Case test_case, Check check) + : UnitTest(std::format("Pokemon::StoryFiles - {}: {}", suite, test_case.name)) + , m_case(std::move(test_case)) + , m_check(check) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return m_check(m_case); + } + +private: + Case m_case; + Check m_check; +}; + +} // namespace + + +void add_tests_StoryFiles(UnitTestDatabase& database){ + for (const LoadCase& test : LOAD_CASES){ + database.add>("load", test, check_load); + } + for (const RowsCase& test : ROWS_CASES){ + database.add>("rows", test, check_rows); + } + for (const ReloadCase& test : RELOAD_CASES){ + database.add>("reload", test, check_reload); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.h new file mode 100644 index 0000000000..5f0804951a --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.h @@ -0,0 +1,24 @@ +/* Story Files Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryFiles_Tests_H +#define PokemonAutomation_Pokemon_StoryFiles_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers the tests of a story read from built-in and dev checkpoint +// files, and of the shared execution fields. +void add_tests_StoryFiles(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryRowExecution.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryRowExecution.cpp new file mode 100644 index 0000000000..1175684067 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryRowExecution.cpp @@ -0,0 +1,105 @@ +/* Story Row Execution + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Json/JsonValue.h" +#include "Pokemon_StoryRowExecution.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + +constexpr std::string_view SHARED_FIELDS[] = {"save", "reload_when_chained", "dev_only"}; + +bool is_known_field(std::string_view key, std::initializer_list game_fields){ + for (std::string_view field : SHARED_FIELDS){ + if (key == field){ + return true; + } + } + for (std::string_view field : game_fields){ + if (key == field){ + return true; + } + } + return false; +} + +// Whether `value` is JSON true. +bool is_true(const JsonValue& value){ + return value.is_boolean() && value.to_boolean_default(); +} + +bool read_autosave(const JsonObject& execution, std::string_view where){ + const JsonValue* save = execution.get_value("save"); + if (save == nullptr){ + return false; + } + const std::string* text = save->to_string(); + if (text == nullptr || (*text != "autosave" && *text != "menu")){ + throw ParseException(std::format("{}: save is \"autosave\", \"menu\" or left out", where)); + } + return *text == "autosave"; +} + +StoryReload read_reload(const JsonObject& execution, std::string_view where){ + const JsonValue* reload = execution.get_value("reload_when_chained"); + if (reload == nullptr){ + return StoryReload::NEVER; + } + if (is_true(*reload)){ + return StoryReload::WHEN_CHAINED; + } + const std::string* text = reload->to_string(); + if (text != nullptr && *text == "always"){ + return StoryReload::ALWAYS; + } + throw ParseException(std::format("{}: reload_when_chained is true, \"always\" or left out", where)); +} + +bool read_dev_only(const JsonObject& execution, std::string_view where){ + const JsonValue* dev_only = execution.get_value("dev_only"); + if (dev_only == nullptr){ + return false; + } + if (!is_true(*dev_only)){ + throw ParseException(std::format("{}: dev_only is true or left out", where)); + } + return true; +} + +} // namespace + + +StoryRowExecution read_story_row_execution( + const JsonObject& execution, std::string_view where, std::initializer_list game_fields +){ + for (const auto& [key, value] : execution){ + if (!is_known_field(key, game_fields)){ + throw ParseException(std::format("{}: unknown execution field {}", where, key)); + } + } + return StoryRowExecution{ + .autosave = read_autosave(execution, where), + .reload = read_reload(execution, where), + .dev_only = read_dev_only(execution, where), + }; +} + +bool story_reloads_before(const StoryRows& rows, StoryRunPolicy::BoundaryReload boundary_reload, std::string_view id){ + if (rows.reload_always.contains(id)){ + return true; + } + return boundary_reload == StoryRunPolicy::BoundaryReload::AT_PLAN_BOUNDARIES && rows.reload_when_chained.contains(id); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryRowExecution.h b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryRowExecution.h new file mode 100644 index 0000000000..d820f0b6a1 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryRowExecution.h @@ -0,0 +1,104 @@ +/* Story Row Execution + * + * From: https://github.com/PokemonAutomation/ + * + * The execution fields of a checkpoint file (its JSON "execution") that + * every game reads the same way, and the sets of rows they mark: which + * rows end on the game's own autosave, which a run reloads before, and + * which are for lab runs only, and a game's story: its checkpoints and + * those rows. The game reads its other execution fields itself + * (Pokemon_AuthoredCheckpointJson.h); Pokemon_StoryFiles.h loads a story + * from its files. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryRowExecution_H +#define PokemonAutomation_Pokemon_StoryRowExecution_H + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Json/JsonObject.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// When a run reloads the row's save before it, having played the row +// before it too ("reload_when_chained"). +enum class StoryReload{ + NEVER, // left out: the row runs on from where the row before left + WHEN_CHAINED, // true: at plan boundaries (StoryRunPolicy::BoundaryReload) + ALWAYS, // "always": even straight through; only its save starts its walk +}; + +// What a checkpoint file's shared execution fields say. +struct StoryRowExecution{ + // "save": "autosave": the game saves by itself at the row's end, so the + // program does not. "menu" or left out: the program saves, when the + // save policy lets it. + bool autosave = false; + StoryReload reload = StoryReload::NEVER; + // "dev_only": true: a lab walk the story does not need (developer + // mode lists and plays it, a user's run does not). + bool dev_only = false; +}; + +// The shared fields of `execution`: "save", "reload_when_chained" and +// "dev_only", each optional. Throws ParseException naming `where` for a +// malformed one, and for a field that is neither shared nor one of +// `game_fields`, which the game reads itself. +StoryRowExecution read_story_row_execution( + const JsonObject& execution, std::string_view where, std::initializer_list game_fields +); + + +// The rows of a story the shared execution fields mark, by id. +struct StoryRows{ + std::set> autosaves; + // WHEN_CHAINED and ALWAYS rows; each comes after an autosave row, + // whose autosave is its start save. + std::set> reload_when_chained; + // The ALWAYS rows of reload_when_chained. + std::set> reload_always; + std::set> dev_only; + // The dev files that replaced a built-in checkpoint, as (file name, + // path) (Pokemon_StoryDevFiles.h). + std::vector> dev_files; +}; + +// A game's story: its checkpoints in the order it plays them, and the rows +// their execution fields mark. +template +struct StoryData : StoryRows{ + std::vector checkpoints; +}; + +// How a run plays its checkpoints: the attempts each gets, and whether it +// reloads at the plan's boundaries. +struct StoryRunPolicy{ + // Whether a run of several checkpoints reloads the save at a + // WHEN_CHAINED row (AT_PLAN_BOUNDARIES) or runs on from the state the + // row before left (STRAIGHT_THROUGH). ALWAYS rows reload either way, and + // a failed checkpoint's retries reload as usual. + enum class BoundaryReload{ + AT_PLAN_BOUNDARIES, + STRAIGHT_THROUGH, + }; + size_t attempts_per_checkpoint = 1; + BoundaryReload boundary_reload = BoundaryReload::AT_PLAN_BOUNDARIES; +}; + +// Whether a run reloads the save before row `id` of `rows` when it reaches +// it from the row before. +bool story_reloads_before(const StoryRows& rows, StoryRunPolicy::BoundaryReload boundary_reload, std::string_view id); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.cpp index 3d39269e07..095cbd5014 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.cpp @@ -324,7 +324,9 @@ DeclaredPose read_declared_pose(const JsonValue& value, std::string_view source) } StoryStep read_story_step(const JsonValue& value, std::string_view source, std::span actions){ - const JsonObject& root = fields(value, {"id", "kind", "why", "action", "args", "skip_if", "declared_after"}, source); + const JsonObject& root = fields( + value, {"id", "kind", "why", "action", "args", "skip_if", "declared_after", "once_per_save"}, source + ); StoryStep result; result.name = root.get_string_throw("id"); if (result.name.empty()){ @@ -344,6 +346,9 @@ StoryStep read_story_step(const JsonValue& value, std::string_view source, std:: if (const JsonValue* field = root.get_value("declared_after")){ result.pose_after = read_declared_pose(*field, source); } + if (const JsonValue* field = root.get_value("once_per_save")){ + result.once_per_save = field->to_boolean_throw(std::string(source)); + } return result; } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp index 0433258cbb..296b300744 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp @@ -3,23 +3,29 @@ * From: https://github.com/PokemonAutomation/ * * Each case reads a record from JSON: accepted cases check the values - * read, rejected cases check that the read fails. The Python reader's - * shared case file is checked on the Python side. + * read, rejected cases check that the read fails. The standalone + * StoryDataTests also checks PokemonSimulation's shared case file when given + * its path (devtools/story-data-tests/StoryWireCases.h). * */ +#include #include #include +#include #include #include #include #include #include "Common/Cpp/Exceptions.h" #include "Common/Cpp/Json/JsonValue.h" +#include "Pokemon/AutoStory/Types/Pokemon_DeclaredPose.h" +#include "Pokemon/AutoStory/Types/Pokemon_GridPose.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryActionArg.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpointMetadata.h" +#include "Pokemon/Navigation/Pokemon_Direction.h" #include "Pokemon_StoryStepJson.h" -#ifndef PA_STORY_DATA_STANDALONE #include "Pokemon_StoryStepJson_Tests.h" -#endif namespace PokemonAutomation{ namespace Pokemon{ @@ -36,28 +42,28 @@ enum class Record{ CHECKPOINT, }; -// A record to read, and which kind it is. -struct WireInput{ - std::string_view name; - Record record = Record::STEP; - std::string_view json; -}; - // What a read gives. using ReadRecord = std::variant; // "" when the record read is as expected, else what differs. using ReadCheck = std::string (*)(const ReadRecord& record); -// What reading it gives: a record the check accepts, or a rejection. -struct WireExpectation{ - ReadCheck check = nullptr; - bool rejected = false; -}; - struct WireCase{ - WireInput input; - WireExpectation expectation; + // A record to read, and which kind it is. + std::string_view name; + struct Input{ + Record record = Record::STEP; + std::string_view json; + }; + + // What reading it gives: a record the check accepts, or a rejection. + struct Expectation{ + ReadCheck check = nullptr; + bool rejected = false; + }; + + Input input; + Expectation expectation; }; std::string expect(bool ok, std::string_view what){ @@ -69,78 +75,84 @@ const StoryStep& step_of(const ReadRecord& record){ } const WireCase CASES[] = { - {.input = {.name = "no args", .json = R"({"id":"talk","kind":"talk"})"}, .expectation = {.check = [](const ReadRecord& record){ + {.name = "no args", .input = {.json = R"({"id":"talk","kind":"talk"})"}, .expectation = {.check = [](const ReadRecord& record){ const StoryStep& step = step_of(record); return expect(step.name == "talk" && step.kind == StoryStepKind::TALK && step.args.empty() && !step.pose_after, "a bare talk step"); }}}, - {.input = {.name = "all shared fields", .json = R"({"id":"all","kind":"game_action","why":"source reason","action":"profile","skip_if":["flag"],"declared_after":{"map":"B","tiles":[[0,0],[1,0]],"facing":"east"}})"}, .expectation = {.check = [](const ReadRecord& record){ + {.name = "all shared fields", .input = {.json = R"({"id":"all","kind":"game_action","why":"source reason","action":"profile","skip_if":["flag"],"declared_after":{"map":"B","tiles":[[0,0],[1,0]],"facing":"east"}})"}, .expectation = {.check = [](const ReadRecord& record){ const StoryStep& step = step_of(record); const DeclaredPose after = TileDeclared{.map = "B", .tiles = {{0, 0}, {1, 0}}, .facing = Direction::EAST}; return expect(step.kind == StoryStepKind::GAME && step.why == "source reason" && step.action == "profile" && step.skip_if == std::vector{"flag"} && step.pose_after == after, "every shared field read"); }}}, - {.input = {.name = "route fills its fallback", .json = R"({"id":"r","kind":"route","args":{"target":{"map":"A","tiles":[[1,2]]}}})"}, .expectation = {.check = [](const ReadRecord& record){ + {.name = "once per save", .input = {.json = R"({"id":"heal","kind":"game_action","action":"profile","once_per_save":true})"}, .expectation = {.check = [](const ReadRecord& record){ + return expect(step_of(record).once_per_save, "a once_per_save step"); + }}}, + {.name = "every time by default", .input = {.json = R"({"id":"heal","kind":"game_action","action":"profile"})"}, .expectation = {.check = [](const ReadRecord& record){ + return expect(!step_of(record).once_per_save, "a step run every time"); + }}}, + {.name = "route fills its fallback", .input = {.json = R"({"id":"r","kind":"route","args":{"target":{"map":"A","tiles":[[1,2]]}}})"}, .expectation = {.check = [](const ReadRecord& record){ const StoryActionArgs expected = {{"fight_wild", false}, {"target", GridPose{.map = "A", .tiles = {{1, 2}}, .facing = std::nullopt}}}; return expect(step_of(record).args == expected, "the target and fight_wild false"); }}}, - {.input = {.name = "legs", .json = R"({"id":"l","kind":"legs","action":"legs","args":{"legs":[{"direction":"south","tiles":2}],"fight_wild":true}})"}, .expectation = {.check = [](const ReadRecord& record){ + {.name = "legs", .input = {.json = R"({"id":"l","kind":"legs","action":"legs","args":{"legs":[{"direction":"south","tiles":2}],"fight_wild":true}})"}, .expectation = {.check = [](const ReadRecord& record){ const StoryActionArgs expected = {{"fight_wild", true}, {"legs", std::vector{{.direction = Direction::SOUTH, .tiles = 2}}}}; return expect(step_of(record).args == expected, "one south leg of 2 tiles"); }}}, - {.input = {.name = "direction", .json = R"({"id":"s","kind":"step","args":{"direction":"west"}})"}, .expectation = {.check = [](const ReadRecord& record){ + {.name = "direction", .input = {.json = R"({"id":"s","kind":"step","args":{"direction":"west"}})"}, .expectation = {.check = [](const ReadRecord& record){ const StoryActionArgs expected = {{"direction", Direction::WEST}}; return expect(step_of(record).args == expected, "direction west"); }}}, - {.input = {.name = "items", .json = R"({"id":"s","kind":"shop","args":{"items":[{"item":"potion","quantity":5}]}})"}, .expectation = {.check = [](const ReadRecord& record){ + {.name = "items", .input = {.json = R"({"id":"s","kind":"shop","args":{"items":[{"item":"potion","quantity":5}]}})"}, .expectation = {.check = [](const ReadRecord& record){ const StoryActionArgs expected = {{"items", std::vector{{.item = "potion", .quantity = 5}}}}; return expect(step_of(record).args == expected, "5 potions"); }}}, - {.input = {.name = "game args", .json = R"({"id":"buy","kind":"game_action","action":"mart","args":{"map":"CeruleanCity_Mart","budget":3000,"if_present":true}})"}, .expectation = {.check = [](const ReadRecord& record){ + {.name = "game args", .input = {.json = R"({"id":"buy","kind":"game_action","action":"mart","args":{"map":"CeruleanCity_Mart","budget":3000,"if_present":true}})"}, .expectation = {.check = [](const ReadRecord& record){ const StoryActionArgs expected = {{"budget", int64_t(3000)}, {"if_present", true}, {"map", std::string("CeruleanCity_Mart")}}; return expect(step_of(record).args == expected, "the Mart args"); }}}, - {.input = {.name = "game words", .json = R"({"id":"buy","kind":"game_action","action":"mart","args":{"map":"M","budget":1,"skip":["a","b"]}})"}, .expectation = {.check = [](const ReadRecord& record){ + {.name = "game words", .input = {.json = R"({"id":"buy","kind":"game_action","action":"mart","args":{"map":"M","budget":1,"skip":["a","b"]}})"}, .expectation = {.check = [](const ReadRecord& record){ const StoryActionArgs expected = {{"budget", int64_t(1)}, {"if_present", false}, {"map", std::string("M")}, {"skip", std::vector{"a", "b"}}}; return expect(step_of(record).args == expected, "the words and if_present false"); }}}, - {.input = {.name = "game integer rows", .json = R"({"id":"w","kind":"game_action","action":"walk","args":{"legs":[[0,4500],[-90,0],[]]}})"}, .expectation = {.check = [](const ReadRecord& record){ + {.name = "game integer rows", .input = {.json = R"({"id":"w","kind":"game_action","action":"walk","args":{"legs":[[0,4500],[-90,0],[]]}})"}, .expectation = {.check = [](const ReadRecord& record){ const StoryActionArgs expected = {{"legs", std::vector>{{0, 4500}, {-90, 0}, {}}}}; return expect(step_of(record).args == expected, "three rows, the last empty"); }}}, - {.input = {.name = "timed legs", .json = R"({"id":"w","kind":"game_action","action":"timed_walk","args":{"legs":[[0,4500],[-90,1200]]}})"}, .expectation = {.check = [](const ReadRecord& record){ + {.name = "timed legs", .input = {.json = R"({"id":"w","kind":"game_action","action":"timed_walk","args":{"legs":[[0,4500],[-90,1200]]}})"}, .expectation = {.check = [](const ReadRecord& record){ const StoryActionArgs expected = {{"legs", std::vector{{.turn = 0, .ms = 4500}, {.turn = -90, .ms = 1200}}}}; return expect(step_of(record).args == expected, "two timed legs"); }}}, - {.input = {.name = "unknown facing stays absent", .record = Record::POSE, .json = R"({"map":"M","tiles":[[0,0]]})"}, .expectation = {.check = [](const ReadRecord& record){ + {.name = "unknown facing stays absent", .input = {.record = Record::POSE, .json = R"({"map":"M","tiles":[[0,0]]})"}, .expectation = {.check = [](const ReadRecord& record){ return expect(std::get(record) == GridPose{.map = "M", .tiles = {{0, 0}}, .facing = std::nullopt}, "one tile, no facing"); }}}, - {.input = {.name = "no tiles", .record = Record::POSE, .json = R"({"map":"M","tiles":[]})"}, .expectation = {.check = [](const ReadRecord& record){ + {.name = "no tiles", .input = {.record = Record::POSE, .json = R"({"map":"M","tiles":[]})"}, .expectation = {.check = [](const ReadRecord& record){ return expect(std::get(record).tiles.empty(), "no tiles"); }}}, - {.input = {.name = "declared tile pose", .record = Record::DECLARED, .json = R"({"map":"M","tiles":[[1,2]],"facing":"north"})"}, .expectation = {.check = [](const ReadRecord& record){ + {.name = "declared tile pose", .input = {.record = Record::DECLARED, .json = R"({"map":"M","tiles":[[1,2]],"facing":"north"})"}, .expectation = {.check = [](const ReadRecord& record){ const DeclaredPose expected = TileDeclared{.map = "M", .tiles = {{1, 2}}, .facing = Direction::NORTH}; return expect(std::get(record) == expected, "a tile pose facing north"); }}}, - {.input = {.name = "declared place", .record = Record::DECLARED, .json = R"({"place":"LumioseCity","spot":"HotelZ","heading":90})"}, .expectation = {.check = [](const ReadRecord& record){ + {.name = "declared place", .input = {.record = Record::DECLARED, .json = R"({"place":"LumioseCity","spot":"HotelZ","heading":90})"}, .expectation = {.check = [](const ReadRecord& record){ const DeclaredPose expected = ZoneDeclared{.place = "LumioseCity", .spot = "HotelZ", .heading = 90}; return expect(std::get(record) == expected, "a zone pose with spot and heading"); }}}, - {.input = {.name = "declared place alone", .record = Record::DECLARED, .json = R"({"place":"HotelZ"})"}, .expectation = {.check = [](const ReadRecord& record){ + {.name = "declared place alone", .input = {.record = Record::DECLARED, .json = R"({"place":"HotelZ"})"}, .expectation = {.check = [](const ReadRecord& record){ const DeclaredPose expected = ZoneDeclared{.place = "HotelZ", .spot = std::nullopt, .heading = std::nullopt}; return expect(std::get(record) == expected, "a zone pose alone"); }}}, - {.input = {.name = "checkpoint starting at a place", .record = Record::CHECKPOINT, .json = R"({"id":"03.01","title":"Lobby","start":{"place":"HotelZ"},"events":[{"id":"down","kind":"scene","declared_after":{"place":"HotelZLobby","heading":180}}]})"}, .expectation = {.check = [](const ReadRecord& record){ + {.name = "checkpoint starting at a place", .input = {.record = Record::CHECKPOINT, .json = R"({"id":"03.01","title":"Lobby","start":{"place":"HotelZ"},"events":[{"id":"down","kind":"scene","declared_after":{"place":"HotelZLobby","heading":180}}]})"}, .expectation = {.check = [](const ReadRecord& record){ const StoryCheckpoint& checkpoint = std::get(record); const DeclaredPose start = ZoneDeclared{.place = "HotelZ", .spot = std::nullopt, .heading = std::nullopt}; return expect(checkpoint.start == start && checkpoint.steps.size() == 1 && checkpoint.steps[0].kind == StoryStepKind::MASH, "a zone start and one scene"); }}}, - {.input = {.name = "ordered checkpoint", .record = Record::CHECKPOINT, .json = R"({"id":"00.05","title":"Parcel","events":[ {"id":"first","kind":"talk"}, {"id":"second","kind":"scene"} ]})"}, .expectation = {.check = [](const ReadRecord& record){ + {.name = "ordered checkpoint", .input = {.record = Record::CHECKPOINT, .json = R"({"id":"00.05","title":"Parcel","events":[ {"id":"first","kind":"talk"}, {"id":"second","kind":"scene"} ]})"}, .expectation = {.check = [](const ReadRecord& record){ const StoryCheckpoint& checkpoint = std::get(record); return expect(checkpoint.id == "00.05" && checkpoint.title == "Parcel" && !checkpoint.start && checkpoint.steps.size() == 2 && checkpoint.steps[0].name == "first" && checkpoint.steps[1].name == "second" && checkpoint.steps_json == R"([{"id":"first","kind":"talk"},{"id":"second","kind":"scene"}])", "two steps in order and their compact JSON"); }}}, - {.input = {.name = "checkpoint postconditions", .record = Record::CHECKPOINT, .json = R"({"id":"x","title":"X","events":[],"metadata":{"phase":"reproduction","postconditions":{"position":{"map":"M","tiles":[[3,4]]},"items":[{"item":"LIFT_KEY","quantity":1}],"flags":["f"]}}})"}, .expectation = {.check = [](const ReadRecord& record){ + {.name = "checkpoint postconditions", .input = {.record = Record::CHECKPOINT, .json = R"({"id":"x","title":"X","events":[],"metadata":{"phase":"reproduction","postconditions":{"position":{"map":"M","tiles":[[3,4]]},"items":[{"item":"LIFT_KEY","quantity":1}],"flags":["f"]}}})"}, .expectation = {.check = [](const ReadRecord& record){ const StoryCheckpoint& checkpoint = std::get(record); const StoryPostconditions& post = checkpoint.postconditions; // Named, not temporaries in the comparison: GCC's -Wdangling-pointer @@ -152,30 +164,30 @@ const WireCase CASES[] = { && post.position == position && post.items == items && post.flags == flags, "the phase, position, item and flag"); }}}, - {.input = {.name = "integer rows, not rows", .json = R"({"id":"w","kind":"game_action","action":"walk","args":{"legs":[0,4500]}})"}, .expectation = {.rejected = true}}, - {.input = {.name = "integer rows, fraction", .json = R"({"id":"w","kind":"game_action","action":"walk","args":{"legs":[[0,4.5]]}})"}, .expectation = {.rejected = true}}, - {.input = {.name = "timed leg of three numbers", .json = R"({"id":"w","kind":"game_action","action":"timed_walk","args":{"legs":[[0,4500,1]]}})"}, .expectation = {.rejected = true}}, - {.input = {.name = "unknown arg", .json = R"({"id":"a","kind":"game_action","action":"mart","args":{"map":"M","budget":1,"budgte":1}})"}, .expectation = {.rejected = true}}, - {.input = {.name = "missing required arg", .json = R"({"id":"a","kind":"game_action","action":"mart","args":{"map":"M"}})"}, .expectation = {.rejected = true}}, - {.input = {.name = "route without target", .json = R"({"id":"a","kind":"route"})"}, .expectation = {.rejected = true}}, - {.input = {.name = "args on a kind with none", .json = R"({"id":"a","kind":"talk","args":{"yes":true}})"}, .expectation = {.rejected = true}}, - {.input = {.name = "game step without action", .json = R"({"id":"a","kind":"game_action"})"}, .expectation = {.rejected = true}}, - {.input = {.name = "unknown action name", .json = R"({"id":"a","kind":"game_action","action":"dance"})"}, .expectation = {.rejected = true}}, - {.input = {.name = "fractional arg", .json = R"({"id":"a","kind":"game_action","action":"mart","args":{"map":"M","budget":1.5}})"}, .expectation = {.rejected = true}}, - {.input = {.name = "args not an object", .json = R"({"id":"a","kind":"game_action","action":"mart","args":["map"]})"}, .expectation = {.rejected = true}}, - {.input = {.name = "old field", .json = R"({"id":"a","kind":"battle","may_lose":true})"}, .expectation = {.rejected = true}}, - {.input = {.name = "old variant", .json = R"({"id":"a","kind":"game_action","variant":"mart"})"}, .expectation = {.rejected = true}}, - {.input = {.name = "unknown field", .json = R"({"id":"a","kind":"talk","mystery":0})"}, .expectation = {.rejected = true}}, - {.input = {.name = "unknown kind", .json = R"({"id":"a","kind":"magic"})"}, .expectation = {.rejected = true}}, - {.input = {.name = "boolean count", .json = R"({"id":"a","kind":"train","args":{"battles":false}})"}, .expectation = {.rejected = true}}, - {.input = {.name = "wrong bool", .json = R"({"id":"a","kind":"answer","args":{"yes":0}})"}, .expectation = {.rejected = true}}, - {.input = {.name = "old pose", .record = Record::POSE, .json = R"({"map":"M","certainty":"unknown","where":{"kind":"unknown"}})"}, .expectation = {.rejected = true}}, - {.input = {.name = "bad coordinate", .record = Record::POSE, .json = R"({"map":"M","tiles":[[0,true]]})"}, .expectation = {.rejected = true}}, - {.input = {.name = "declared map and place", .record = Record::DECLARED, .json = R"({"map":"M","place":"HotelZ"})"}, .expectation = {.rejected = true}}, - {.input = {.name = "declared place with tiles", .record = Record::DECLARED, .json = R"({"place":"HotelZ","tiles":[[1,2]]})"}, .expectation = {.rejected = true}}, - {.input = {.name = "declared map with heading", .record = Record::DECLARED, .json = R"({"map":"M","heading":90})"}, .expectation = {.rejected = true}}, - {.input = {.name = "declared neither", .record = Record::DECLARED, .json = R"({"spot":"Lobby"})"}, .expectation = {.rejected = true}}, - {.input = {.name = "duplicate events", .record = Record::CHECKPOINT, .json = R"({"id":"x","title":"X","events":[{"id":"a","kind":"talk"},{"id":"a","kind":"talk"}]})"}, .expectation = {.rejected = true}}, + {.name = "integer rows, not rows", .input = {.json = R"({"id":"w","kind":"game_action","action":"walk","args":{"legs":[0,4500]}})"}, .expectation = {.rejected = true}}, + {.name = "integer rows, fraction", .input = {.json = R"({"id":"w","kind":"game_action","action":"walk","args":{"legs":[[0,4.5]]}})"}, .expectation = {.rejected = true}}, + {.name = "timed leg of three numbers", .input = {.json = R"({"id":"w","kind":"game_action","action":"timed_walk","args":{"legs":[[0,4500,1]]}})"}, .expectation = {.rejected = true}}, + {.name = "unknown arg", .input = {.json = R"({"id":"a","kind":"game_action","action":"mart","args":{"map":"M","budget":1,"budgte":1}})"}, .expectation = {.rejected = true}}, + {.name = "missing required arg", .input = {.json = R"({"id":"a","kind":"game_action","action":"mart","args":{"map":"M"}})"}, .expectation = {.rejected = true}}, + {.name = "route without target", .input = {.json = R"({"id":"a","kind":"route"})"}, .expectation = {.rejected = true}}, + {.name = "args on a kind with none", .input = {.json = R"({"id":"a","kind":"talk","args":{"yes":true}})"}, .expectation = {.rejected = true}}, + {.name = "game step without action", .input = {.json = R"({"id":"a","kind":"game_action"})"}, .expectation = {.rejected = true}}, + {.name = "unknown action name", .input = {.json = R"({"id":"a","kind":"game_action","action":"dance"})"}, .expectation = {.rejected = true}}, + {.name = "fractional arg", .input = {.json = R"({"id":"a","kind":"game_action","action":"mart","args":{"map":"M","budget":1.5}})"}, .expectation = {.rejected = true}}, + {.name = "args not an object", .input = {.json = R"({"id":"a","kind":"game_action","action":"mart","args":["map"]})"}, .expectation = {.rejected = true}}, + {.name = "old field", .input = {.json = R"({"id":"a","kind":"battle","may_lose":true})"}, .expectation = {.rejected = true}}, + {.name = "old variant", .input = {.json = R"({"id":"a","kind":"game_action","variant":"mart"})"}, .expectation = {.rejected = true}}, + {.name = "unknown field", .input = {.json = R"({"id":"a","kind":"talk","mystery":0})"}, .expectation = {.rejected = true}}, + {.name = "unknown kind", .input = {.json = R"({"id":"a","kind":"magic"})"}, .expectation = {.rejected = true}}, + {.name = "boolean count", .input = {.json = R"({"id":"a","kind":"train","args":{"battles":false}})"}, .expectation = {.rejected = true}}, + {.name = "wrong bool", .input = {.json = R"({"id":"a","kind":"answer","args":{"yes":0}})"}, .expectation = {.rejected = true}}, + {.name = "old pose", .input = {.record = Record::POSE, .json = R"({"map":"M","certainty":"unknown","where":{"kind":"unknown"}})"}, .expectation = {.rejected = true}}, + {.name = "bad coordinate", .input = {.record = Record::POSE, .json = R"({"map":"M","tiles":[[0,true]]})"}, .expectation = {.rejected = true}}, + {.name = "declared map and place", .input = {.record = Record::DECLARED, .json = R"({"map":"M","place":"HotelZ"})"}, .expectation = {.rejected = true}}, + {.name = "declared place with tiles", .input = {.record = Record::DECLARED, .json = R"({"place":"HotelZ","tiles":[[1,2]]})"}, .expectation = {.rejected = true}}, + {.name = "declared map with heading", .input = {.record = Record::DECLARED, .json = R"({"map":"M","heading":90})"}, .expectation = {.rejected = true}}, + {.name = "declared neither", .input = {.record = Record::DECLARED, .json = R"({"spot":"Lobby"})"}, .expectation = {.rejected = true}}, + {.name = "duplicate events", .input = {.record = Record::CHECKPOINT, .json = R"({"id":"x","title":"X","events":[{"id":"a","kind":"talk"},{"id":"a","kind":"talk"}]})"}, .expectation = {.rejected = true}}, }; @@ -200,19 +212,19 @@ std::vector test_actions(){ } -// `input` read. -ReadRecord read_record(const WireInput& input){ +// `input` read, with `source` naming it in parse errors. +ReadRecord read_record(const WireCase::Input& input, std::string_view source){ static const std::vector actions = test_actions(); const JsonValue value = parse_json(std::string(input.json)); switch (input.record){ case Record::POSE: - return read_grid_pose(value, input.name); + return read_grid_pose(value, source); case Record::DECLARED: - return read_declared_pose(value, input.name); + return read_declared_pose(value, source); case Record::STEP: - return read_story_step(value, input.name, actions); + return read_story_step(value, source, actions); case Record::CHECKPOINT: - return read_story_checkpoint(value, input.name, actions); + return read_story_checkpoint(value, source, actions); } throw ParseException("Unknown test record."); } @@ -220,7 +232,7 @@ ReadRecord read_record(const WireInput& input){ // "" when `test` reads as expected, else what differs. std::string check_case(const WireCase& test){ try{ - const ReadRecord record = read_record(test.input); + const ReadRecord record = read_record(test.input, test.name); if (test.expectation.rejected){ return "Expected rejection, read accepted record."; } @@ -236,7 +248,7 @@ std::string check_case(const WireCase& test){ class StoryJsonTest : public UnitTest{ public: explicit StoryJsonTest(WireCase test) - : UnitTest(std::format("Pokemon::StoryStepJson - {}", test.input.name)) + : UnitTest(std::format("Pokemon::StoryStepJson - {}", test.name)) , m_case(test) {} @@ -263,24 +275,21 @@ void add_tests_StoryStepJson(UnitTestDatabase& database){ } } -#endif - +#else - -} // namespace Pokemon -} // namespace PokemonAutomation - - -#ifdef PA_STORY_DATA_STANDALONE - -int main(){ - int failed = 0; - for (const auto& test : PokemonAutomation::Pokemon::CASES){ - const std::string error = PokemonAutomation::Pokemon::check_case(test); - std::cout << test.input.name << ": " << (error.empty() ? "PASS" : error) << '\n'; +size_t run_story_step_json_cases(){ + size_t failed = 0; + for (const WireCase& test : CASES){ + const std::string error = check_case(test); + std::cout << test.name << ": " << (error.empty() ? "PASS" : error) << '\n'; failed += !error.empty(); } - return failed != 0; + return failed; } #endif + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.h index 9c4f9565ad..507d670981 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.h @@ -7,6 +7,7 @@ #ifndef PokemonAutomation_Pokemon_StoryStepJson_Tests_H #define PokemonAutomation_Pokemon_StoryStepJson_Tests_H +#include #include "Common/Cpp/TestRunners/UnitTestDatabase.h" namespace PokemonAutomation{ @@ -16,6 +17,12 @@ namespace Pokemon{ // Registers unit tests for the lossless shared story-data wire contract. void add_tests_StoryStepJson(UnitTestDatabase& database); +#ifdef PA_STORY_DATA_STANDALONE +// Standalone StoryDataTests only: runs the built-in cases, prints one line +// per case, returns how many failed. +size_t run_story_step_json_cases(); +#endif + } // namespace Pokemon diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/MIGRATION.md b/SerialPrograms/Source/Pokemon/AutoStory/Engine/MIGRATION.md index e392e047a1..eef09ecdc4 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/MIGRATION.md +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/MIGRATION.md @@ -9,7 +9,7 @@ Each move deletes that game's executor; the last one deletes `StoryStepExecutor` 1. **Backends.** Write a console backend: `StoryBackend` over the console handle and the controller context, `kind()` CONSOLE. `observe()` waits for pending input (`wait_for_all_requests()`), takes a frame and fills `ScreenObservation::events` from the - game's detectors (fade, text box, choice box, interact prompt, battle, can move, still). + game's detectors (the `ScreenEvent`s of `CommonTools/ScreenEvents/ScreenEventTypes.h`). `stick(degrees, duration, release, pace)` holds the move stick at an angle, then lets go for `release` (a free-moving game's walk and turn; `hold()` is the 90-degree case). A game without tiles reports the way the player @@ -69,13 +69,13 @@ Each move deletes that game's executor; the last one deletes `StoryStepExecutor` readers. - **PLZA** (ZonePose, in the engine with its instantiations and `observed_state()`): a declared pose (`"start"`, a step's `"declared_after"`) is a place, `{"place": "HotelZ"}` with an - optional `"spot"` and `"heading"` (`ZoneDeclared`, read by - `StoryPoseTraits::from_declared()`). Walks are `TIMED_LEGS` args; tile args fail + optional `"spot"` and `"heading"` (`ZoneDeclared`). The run keeps only the place, as a map + with no tiles; spot and heading wait for a position reader. Walks are `TIMED_LEGS` args; tile args fail the lint. Turns are `stick()` after the camera recenter; the minimap arrow is the `"heading"` reading. A new pose type: a struct with `pose_matches` / `pose_text`, a `StoryPoseTraits` specialization -(`TILE_MOVES`, `Declared`, `from_declared()`), and an explicit instantiation line in each engine +(`TILE_MOVES`, `Declared`), and an explicit instantiation line in each engine `.cpp`. ## Moves in shared code when the last executor goes diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.cpp new file mode 100644 index 0000000000..99fd6d1d54 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.cpp @@ -0,0 +1,55 @@ +/* Covered Watch + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Pokemon_CoveredWatch.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +CoveredWatcher::CoveredWatcher( + VisualInferenceCallback& watcher, WallClock start, Milliseconds tail, + std::optional end +) + : VisualInferenceCallback(watcher.label()) + , m_watcher(watcher) + , m_coverage(start) + , m_tail(tail) + , m_end(end) +{} + +void CoveredWatcher::make_overlays(VideoOverlaySet& items) const{ + m_watcher.make_overlays(items); +} + +bool CoveredWatcher::process_frame(const VideoSnapshot& frame){ + if (frame.timestamp < m_coverage.start()){ + return false; + } + m_coverage.record(frame.timestamp); + if (m_watcher.process_frame(frame)){ + m_fired = true; + return true; + } + return m_end && frame.timestamp >= *m_end + m_tail; +} + + +WatchResult watch_for( + VideoStream& stream, CancellableScope& scope, Milliseconds duration, + const WatchLimits& limits, VisualInferenceCallback& watcher +){ + const WallClock start = current_time(); + const WallClock end = start + duration; + CoveredWatcher covered(watcher, start, limits.tail, end); + wait_until(stream, scope, end + limits.tail + limits.cap, {covered}, limits.period); + return judge_watch(covered.fired(), covered.coverage(), end, limits.max_gap); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h new file mode 100644 index 0000000000..38aa9fad0f --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h @@ -0,0 +1,134 @@ +/* Covered Watch + * + * From: https://github.com/PokemonAutomation/ + * + * A watch for a screen that ends in frame time and reports whether its + * frames cover the window (Pokemon_FrameCoverage.h). run_until() and + * wait_until() end a watch in wall time and return -1 for "did not fire" + * whether the watcher inspected a frame every 33 ms or one a second: the + * PLZA 03.08 catch (2026-11-14) read a missed "Registered" banner as "no + * new species". These wrappers keep those routines and the watcher as + * they are, record the capture times of the frames the watcher was + * handed, keep watching past the window's end until a frame captured a + * tail after it has been inspected (bounded by a wall-clock cap), and + * return SEEN, NOT_SEEN or UNKNOWN. + * + * The tail is the watcher's hold. A watcher that must see the screen for + * a hold (DetectorToFinder) fires a hold after the screen shows, so a + * screen that shows in the window's last hold fires only after the window + * ends. A fire in the tail still means the screen showed by the end. + * + */ + +#ifndef PokemonAutomation_Pokemon_CoveredWatch_H +#define PokemonAutomation_Pokemon_CoveredWatch_H + +#include +#include +#include "Common/Cpp/Time.h" +#include "CommonFramework/Tools/VideoStream.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "CommonTools/InferenceCallbacks/VisualInferenceCallback.h" +#include "Pokemon_FrameCoverage.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// `watcher` as it is, plus the frames it was handed. Frames captured +// before `start` are not handed to `watcher`: they show the screen before +// the window opened. It stops the session when `watcher` fires, or, once +// the window's end is known, on the first frame captured at or after the +// end plus `tail`. Call end_at() and read fired() and coverage() only +// while no inference routine runs it. +class CoveredWatcher : public VisualInferenceCallback{ +public: + CoveredWatcher( + VisualInferenceCallback& watcher, WallClock start, Milliseconds tail, + std::optional end = std::nullopt + ); + + virtual void make_overlays(VideoOverlaySet& items) const override; + using VisualInferenceCallback::process_frame; + virtual bool process_frame(const VideoSnapshot& frame) override; + + // The window ends at `end`: stop on the first frame captured at or + // after `end` plus the tail. + void end_at(WallClock end){ + m_end = end; + } + bool fired() const{ + return m_fired; + } + const FrameCoverage& coverage() const{ + return m_coverage; + } + +private: + VisualInferenceCallback& m_watcher; + FrameCoverage m_coverage; + Milliseconds m_tail; + std::optional m_end; + bool m_fired = false; +}; + + +// How a covered watch judges and bounds its window. The zero defaults are +// the strictest: no gap is small enough for NOT_SEEN, no tail, no cap. +struct WatchLimits{ + // judge_watch(). + Milliseconds max_gap = Milliseconds(0); + // The watcher's hold: how long past the window's end to keep watching. + Milliseconds tail = Milliseconds(0); + // How much longer than the end plus the tail to wait in wall time for + // a frame captured that late. + Milliseconds cap = Milliseconds(0); + // How often the watcher is handed the newest frame. At most `max_gap`, + // or frames cannot be inspected often enough to cover the window. + Milliseconds period = Milliseconds(50); +}; + + +// Runs `command` with `watcher` live over the window from now to the +// command's end. When the command ends without a fire, keeps watching +// until a frame captured `limits.tail` after that end has been inspected, +// at most `limits.cap` longer in wall time, so the window is judged on +// frames up to its end and a screen that shows just as the command ends +// is still caught. +template +WatchResult run_watched( + VideoStream& stream, ControllerContext& context, + std::function&& command, + const WatchLimits& limits, VisualInferenceCallback& watcher +); + +// Watches with `watcher` for `duration` in frame time: until it fires or a +// frame captured `duration` plus `limits.tail` from now has been +// inspected, at most `limits.cap` longer in wall time. +WatchResult watch_for( + VideoStream& stream, CancellableScope& scope, Milliseconds duration, + const WatchLimits& limits, VisualInferenceCallback& watcher +); + +template +WatchResult run_watched( + VideoStream& stream, ControllerContext& context, + std::function&& command, + const WatchLimits& limits, VisualInferenceCallback& watcher +){ + CoveredWatcher covered(watcher, current_time(), limits.tail); + run_until(stream, context, std::move(command), {covered}, limits.period); + const WallClock end = current_time(); + if (!covered.fired()){ + covered.end_at(end); + wait_until(stream, context, end + limits.tail + limits.cap, {covered}, limits.period); + } + return judge_watch(covered.fired(), covered.coverage(), end, limits.max_gap); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch_Tests.cpp new file mode 100644 index 0000000000..06ec9048b8 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch_Tests.cpp @@ -0,0 +1,172 @@ +/* Covered Watch Tests + * + * From: https://github.com/PokemonAutomation/ + * + * CoveredWatcher on scripted frames: each case is the capture times of + * the frames the pivot hands over (ms after the watch opened, repeats + * included), when the wrapped watcher fires, where the window ends and + * how long a tail is watched past it, and on which frame the wrapper asks + * the session to stop. A watcher with a hold is scripted as one that fires + * a hold after the screen shows. + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "Pokemon_CoveredWatch.h" +#include "Pokemon_CoveredWatch_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + + +const WallClock WATCH_START = WallClock() + Milliseconds(1000000); + +WallClock at(int64_t ms){ + return WATCH_START + Milliseconds(ms); +} + + +// Fires on every frame captured at or after `fires_at`, and counts the +// frames handed to it. +class ScriptedWatcher : public VisualInferenceCallback{ +public: + explicit ScriptedWatcher(std::optional fires_at) + : VisualInferenceCallback("ScriptedWatcher") + , m_fires_at(fires_at) + {} + virtual void make_overlays(VideoOverlaySet&) const override{} + using VisualInferenceCallback::process_frame; + virtual bool process_frame(const VideoSnapshot& frame) override{ + m_calls++; + return m_fires_at && frame.timestamp >= *m_fires_at; + } + size_t calls() const{ + return m_calls; + } +private: + std::optional m_fires_at; + size_t m_calls = 0; +}; + + +struct WatchCase{ + struct Input{ + std::vector frames; + std::optional fires_at = std::nullopt; + std::optional end = std::nullopt; + int64_t tail = 0; + }; + struct Expectation{ + // The index of the frame the wrapper stops on; none: it never stops. + std::optional stops_on; + bool fired; + // Distinct frames recorded, up to and including the one it stops on. + size_t frames; + // Frames handed to the wrapped watcher, repeats included. + size_t watcher_calls; + }; + std::string_view name; + Input input; + Expectation expectation; +}; + +const std::vector WATCH_CASES = { + {.name = "the watcher fires", + .input = {.frames = {0, 33, 66, 99}, .fires_at = 66}, + .expectation = {.stops_on = 2, .fired = true, .frames = 3, .watcher_calls = 3}}, + {.name = "a frame at the window's end stops the watch without firing", + .input = {.frames = {0, 33, 66, 99}, .end = 50}, + .expectation = {.stops_on = 2, .fired = false, .frames = 3, .watcher_calls = 3}}, + {.name = "the watcher firing on the end frame counts as fired", + .input = {.frames = {0, 33, 66}, .fires_at = 60, .end = 50}, + .expectation = {.stops_on = 2, .fired = true, .frames = 3, .watcher_calls = 3}}, + {.name = "no end and no fire never stops", + .input = {.frames = {0, 33, 66}}, + .expectation = {.stops_on = std::nullopt, .fired = false, .frames = 3, .watcher_calls = 3}}, + {.name = "old frames handed over again do not reach the end", + .input = {.frames = {0, 10, 10, 10}, .end = 40}, + .expectation = {.stops_on = std::nullopt, .fired = false, .frames = 2, .watcher_calls = 4}}, + {.name = "a screen showing in the window's last hold fires in the tail", + .input = {.frames = {0, 33, 66, 99, 132, 165, 198}, .fires_at = 80 + 100, .end = 100, .tail = 100}, + .expectation = {.stops_on = 6, .fired = true, .frames = 7, .watcher_calls = 7}}, + {.name = "the watch stops a tail after the window's end without a fire", + .input = {.frames = {0, 33, 66, 99, 132, 165, 198, 231, 264}, .end = 100, .tail = 100}, + .expectation = {.stops_on = 7, .fired = false, .frames = 8, .watcher_calls = 8}}, + {.name = "a frame captured before the window opened is not handed to the watcher", + .input = {.frames = {-40, 10}, .fires_at = -100}, + .expectation = {.stops_on = 1, .fired = true, .frames = 1, .watcher_calls = 1}}, +}; + + +std::string check_watch(const WatchCase& test){ + ScriptedWatcher watcher(test.input.fires_at ? std::optional(at(*test.input.fires_at)) : std::nullopt); + CoveredWatcher covered( + watcher, WATCH_START, Milliseconds(test.input.tail), + test.input.end ? std::optional(at(*test.input.end)) : std::nullopt + ); + std::optional stopped; + for (size_t c = 0; c < test.input.frames.size(); c++){ + if (covered.process_frame(VideoSnapshot(ImageRGB32(), at(test.input.frames[c])))){ + stopped = c; + break; + } + } + const WatchCase::Expectation& expected = test.expectation; + if (stopped != expected.stops_on){ + return std::format( + "stops on frame: expected {}, got {}", + expected.stops_on ? std::to_string(*expected.stops_on) : "none", + stopped ? std::to_string(*stopped) : "none" + ); + } + if (covered.fired() != expected.fired){ + return std::format("fired: expected {}, got {}", expected.fired, covered.fired()); + } + if (covered.coverage().frames() != expected.frames){ + return std::format("frames: expected {}, got {}", expected.frames, covered.coverage().frames()); + } + if (watcher.calls() != expected.watcher_calls){ + return std::format("watcher calls: expected {}, got {}", expected.watcher_calls, watcher.calls()); + } + return ""; +} + + +class CoveredWatchTest : public UnitTest{ +public: + explicit CoveredWatchTest(WatchCase test) + : UnitTest(std::format("Pokemon::CoveredWatch - {}", test.name)) + , m_case(std::move(test)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::string error = check_watch(m_case); + return error.empty() ? UnitTestResult(true) : UnitTestResult(error); + } +private: + WatchCase m_case; +}; + + +} // namespace + + +void add_tests_CoveredWatch(UnitTestDatabase& database){ + for (const WatchCase& test : WATCH_CASES){ + database.add(test); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch_Tests.h new file mode 100644 index 0000000000..0501566ac3 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch_Tests.h @@ -0,0 +1,23 @@ +/* Covered Watch Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_CoveredWatch_Tests_H +#define PokemonAutomation_Pokemon_CoveredWatch_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers unit tests for the watcher wrapper that records the frames a watch inspected. +void add_tests_CoveredWatch(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage.cpp new file mode 100644 index 0000000000..b99e964d21 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage.cpp @@ -0,0 +1,91 @@ +/* Frame Coverage + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "Pokemon_FrameCoverage.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +FrameCoverage::FrameCoverage(WallClock start) + : m_start(start) +{} + +void FrameCoverage::record(WallClock frame){ + if (frame < m_start){ + return; + } + if (!m_frames.empty() && frame <= m_frames.back()){ + return; + } + m_frames.push_back(frame); +} + +std::optional FrameCoverage::last_frame() const{ + if (m_frames.empty()){ + return std::nullopt; + } + return m_frames.back(); +} + +bool FrameCoverage::reached(WallClock deadline) const{ + return !m_frames.empty() && m_frames.back() >= deadline; +} + +Milliseconds FrameCoverage::largest_gap(WallClock end) const{ + WallDuration largest = WallDuration::zero(); + WallClock previous = m_start; + for (WallClock frame : m_frames){ + const WallClock clamped = std::min(frame, end); + largest = std::max(largest, clamped - previous); + previous = clamped; + } + largest = std::max(largest, end - previous); + return std::chrono::ceil(largest); +} + + +WatchResult judge_watch(bool fired, const FrameCoverage& coverage, WallClock end, Milliseconds max_gap){ + WatchResult result; + result.frames = coverage.frames(); + result.largest_gap = coverage.largest_gap(end); + if (fired){ + result.verdict = WatchVerdict::SEEN; + }else if (result.largest_gap <= max_gap){ + result.verdict = WatchVerdict::NOT_SEEN; + }else{ + result.verdict = WatchVerdict::UNKNOWN; + } + return result; +} + +std::string describe(WatchVerdict verdict){ + switch (verdict){ + case WatchVerdict::SEEN: + return "seen"; + case WatchVerdict::NOT_SEEN: + return "not seen"; + case WatchVerdict::UNKNOWN: + return "unknown"; + } + return "unknown"; +} + +std::string describe(const WatchResult& result){ + const double gap_seconds = result.largest_gap.count() / 1000.0; + if (result.verdict == WatchVerdict::UNKNOWN){ + return std::format("unknown: largest gap between frames {:.2f} s over {} frames", gap_seconds, result.frames); + } + return std::format("{} over {} frames, largest gap {:.2f} s", describe(result.verdict), result.frames, gap_seconds); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage.h new file mode 100644 index 0000000000..4f12dcd5de --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage.h @@ -0,0 +1,96 @@ +/* Frame Coverage + * + * From: https://github.com/PokemonAutomation/ + * + * Which frames a watch over a window actually inspected, and whether they + * are enough to say a screen did not show. The inference pivot polls a + * watcher every period with the newest converted frame, and that frame + * can be the one it saw last time or one captured a second ago: under CPU + * load (ConvertFrame at 150 ms mean, PLZA 03.08 on 2026-11-14) a watcher + * that polled 160 times over 8 s may have seen a few frames with second-long + * holes between them, and a 2 s "Registered to the Pokedex" banner fell + * into one. So a window is judged on the capture times of the frames + * inspected (VideoSnapshot::timestamp), not on polls or wall time. + * + */ + +#ifndef PokemonAutomation_Pokemon_FrameCoverage_H +#define PokemonAutomation_Pokemon_FrameCoverage_H + +#include +#include +#include +#include +#include "Common/Cpp/Time.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// The distinct frames inspected since a window opened, by capture time. +class FrameCoverage{ +public: + explicit FrameCoverage(WallClock start); + + // A frame captured at `frame` was inspected. A frame captured before + // the window opened, or not after the last one recorded (the pivot + // handing over the same frame again), adds nothing. + void record(WallClock frame); + + // When the window opened. + WallClock start() const{ + return m_start; + } + // How many distinct frames were recorded. + size_t frames() const{ + return m_frames.size(); + } + // The capture time of the newest frame; none before the first. + std::optional last_frame() const; + // Whether a frame captured at or after `deadline` was inspected: the + // window up to `deadline` is over in frame time, not only in wall time. + bool reached(WallClock deadline) const; + // The longest part of [start, `end`] no inspected frame was captured + // in: before the first frame, between two, or after the last. Rounded + // up to whole ms, so a gap a fraction over a limit is over it. + Milliseconds largest_gap(WallClock end) const; + +private: + WallClock m_start; + std::vector m_frames; +}; + + +// What a watch window showed. +enum class WatchVerdict{ + SEEN, // The watcher fired. + NOT_SEEN, // It did not, and the frames cover the window. + UNKNOWN, // It did not, but a gap between frames could hide the screen. +}; + +// A watch window's verdict and the frames it rests on. +struct WatchResult{ + WatchVerdict verdict = WatchVerdict::UNKNOWN; + size_t frames = 0; + Milliseconds largest_gap = Milliseconds(0); +}; + +// SEEN when the watcher fired. Otherwise NOT_SEEN only when no gap in +// [coverage's start, `end`] is longer than `max_gap`, else UNKNOWN: the +// caller looks again or falls back instead of reading "not there". A +// watcher that must see the screen for a hold (DetectorToFinder) needs +// frames at least that often, so `max_gap` is at most the hold, and at +// most how long the screen shows minus the hold. +WatchResult judge_watch(bool fired, const FrameCoverage& coverage, WallClock end, Milliseconds max_gap); + +// "seen", "not seen", "unknown". +std::string describe(WatchVerdict verdict); +// "not seen over 240 frames, largest gap 0.03 s", +// "unknown: largest gap between frames 1.02 s over 220 frames". +std::string describe(const WatchResult& result); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage_Tests.cpp new file mode 100644 index 0000000000..18d8dc8569 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage_Tests.cpp @@ -0,0 +1,158 @@ +/* Frame Coverage Tests + * + * From: https://github.com/PokemonAutomation/ + * + * A watch window judged on scripted frame times: each case is the capture + * times of the frames a watcher inspected (ms after the window opened, + * fractions allowed, in the order inspected), whether it fired, and where + * the window ends. + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon_FrameCoverage.h" +#include "Pokemon_FrameCoverage_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + + +const WallClock WINDOW_START = WallClock() + Milliseconds(1000000); + +WallClock at(double ms){ + return WINDOW_START + std::chrono::duration_cast(std::chrono::duration(ms)); +} + +// `count` frames `period` ms apart from `first`. +std::vector frames_every(double first, double period, size_t count){ + std::vector out; + for (size_t c = 0; c < count; c++){ + out.push_back(first + period * (double)c); + } + return out; +} + +// `a` then `b`. +std::vector joined(std::vector a, const std::vector& b){ + a.insert(a.end(), b.begin(), b.end()); + return a; +} + + +struct CoverageCase{ + struct Input{ + std::vector frames; + bool fired = false; + double window_end = 8000; + Milliseconds max_gap = Milliseconds(100); + }; + struct Expectation{ + WatchVerdict verdict; + size_t frames; + Milliseconds largest_gap; + // Whether a frame captured at or after the window's end was inspected. + bool reached_end; + }; + std::string_view name; + Input input; + Expectation expectation; +}; + +const std::vector COVERAGE_CASES = { + {.name = "frames every 33 ms, watcher fired", + .input = {.frames = frames_every(10, 33, 100), .fired = true, .window_end = 3000}, + .expectation = {.verdict = WatchVerdict::SEEN, .frames = 100, .largest_gap = Milliseconds(33), .reached_end = true}}, + {.name = "frames every 33 ms through the window, not fired", + .input = {.frames = frames_every(10, 33, 250)}, + .expectation = {.verdict = WatchVerdict::NOT_SEEN, .frames = 250, .largest_gap = Milliseconds(33), .reached_end = true}}, + {.name = "a second of dropped frames mid-window", + .input = {.frames = joined(frames_every(10, 33, 100), frames_every(4300, 33, 120))}, + .expectation = {.verdict = WatchVerdict::UNKNOWN, .frames = 220, .largest_gap = Milliseconds(1023), .reached_end = true}}, + {.name = "no frame inspected", + .input = {.frames = {}}, + .expectation = {.verdict = WatchVerdict::UNKNOWN, .frames = 0, .largest_gap = Milliseconds(8000), .reached_end = false}}, + {.name = "frames stop before the window ends", + .input = {.frames = frames_every(10, 33, 200)}, + .expectation = {.verdict = WatchVerdict::UNKNOWN, .frames = 200, .largest_gap = Milliseconds(1423), .reached_end = false}}, + {.name = "first frame late after the window opened", + .input = {.frames = frames_every(500, 33, 240)}, + .expectation = {.verdict = WatchVerdict::UNKNOWN, .frames = 240, .largest_gap = Milliseconds(500), .reached_end = true}}, + {.name = "the same frame handed over again counts once", + .input = {.frames = {10, 10, 10, 43, 43, 76}, .window_end = 80}, + .expectation = {.verdict = WatchVerdict::NOT_SEEN, .frames = 3, .largest_gap = Milliseconds(33), .reached_end = false}}, + {.name = "a frame captured before the window opened is not evidence", + .input = {.frames = {-40, 60, 93}, .window_end = 90}, + .expectation = {.verdict = WatchVerdict::NOT_SEEN, .frames = 2, .largest_gap = Milliseconds(60), .reached_end = true}}, + {.name = "fired after dropped frames is still seen", + .input = {.frames = {10, 1500, 1533}, .fired = true, .window_end = 2000}, + .expectation = {.verdict = WatchVerdict::SEEN, .frames = 3, .largest_gap = Milliseconds(1490), .reached_end = false}}, + {.name = "gaps of exactly the max gap still cover", + .input = {.frames = frames_every(0, 100, 11), .window_end = 1000}, + .expectation = {.verdict = WatchVerdict::NOT_SEEN, .frames = 11, .largest_gap = Milliseconds(100), .reached_end = true}}, + {.name = "a gap a fraction of a ms over the max gap does not cover", + .input = {.frames = {0, 100.5, 200}, .window_end = 200}, + .expectation = {.verdict = WatchVerdict::UNKNOWN, .frames = 3, .largest_gap = Milliseconds(101), .reached_end = true}}, +}; + + +std::string check_coverage(const CoverageCase& test){ + FrameCoverage coverage(WINDOW_START); + for (double ms : test.input.frames){ + coverage.record(at(ms)); + } + const WallClock end = at(test.input.window_end); + const WatchResult result = judge_watch(test.input.fired, coverage, end, test.input.max_gap); + const CoverageCase::Expectation& expected = test.expectation; + if (result.verdict != expected.verdict){ + return std::format("verdict: expected \"{}\", got \"{}\"", describe(expected.verdict), describe(result)); + } + if (result.frames != expected.frames){ + return std::format("frames: expected {}, got {}", expected.frames, result.frames); + } + if (result.largest_gap != expected.largest_gap){ + return std::format("largest gap: expected {} ms, got {} ms", expected.largest_gap.count(), result.largest_gap.count()); + } + if (coverage.reached(end) != expected.reached_end){ + return std::format("reached the window's end: expected {}, got {}", expected.reached_end, !expected.reached_end); + } + return ""; +} + + +class FrameCoverageTest : public UnitTest{ +public: + explicit FrameCoverageTest(CoverageCase test) + : UnitTest(std::format("Pokemon::FrameCoverage - {}", test.name)) + , m_case(std::move(test)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::string error = check_coverage(m_case); + return error.empty() ? UnitTestResult(true) : UnitTestResult(error); + } +private: + CoverageCase m_case; +}; + + +} // namespace + + +void add_tests_FrameCoverage(UnitTestDatabase& database){ + for (const CoverageCase& test : COVERAGE_CASES){ + database.add(test); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage_Tests.h new file mode 100644 index 0000000000..96aeb0d42d --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage_Tests.h @@ -0,0 +1,23 @@ +/* Frame Coverage Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_FrameCoverage_Tests_H +#define PokemonAutomation_Pokemon_FrameCoverage_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers unit tests for judging a watch window on the frames it inspected. +void add_tests_FrameCoverage(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.cpp index 926483bfb0..212d51a1b8 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.cpp @@ -4,7 +4,6 @@ * */ -#include #include "Pokemon_ScreenObservation.h" namespace PokemonAutomation{ @@ -12,24 +11,26 @@ namespace Pokemon{ ScreenObservation screen_observation(const ScreenEvents& events){ - using EngineEvent = PokemonAutomation::ScreenEvent; - ScreenObservation look; - const std::pair same[] = { - {ScreenEvent::FADE_BLACK, EngineEvent::FADE_BLACK}, - {ScreenEvent::FADE_WHITE, EngineEvent::FADE_WHITE}, - {ScreenEvent::DIALOG_BOX, EngineEvent::TEXT_BOX}, - {ScreenEvent::CHOICE_BOX, EngineEvent::TEXT_BOX}, - {ScreenEvent::BATTLE_STARTED, EngineEvent::BATTLE}, - {ScreenEvent::PLAYER_CAN_MOVE, EngineEvent::CAN_MOVE}, - {ScreenEvent::SCREEN_STILL, EngineEvent::STILL}, - {ScreenEvent::CHOICE_BOX, EngineEvent::CHOICE_BOX}, - {ScreenEvent::INTERACT_PROMPT, EngineEvent::INTERACT_PROMPT}, + // The events the story engine reads; the rest are left out. + constexpr ScreenEvent ENGINE_EVENTS[] = { + ScreenEvent::FADE_BLACK, + ScreenEvent::FADE_WHITE, + ScreenEvent::DIALOG_BOX, + ScreenEvent::BATTLE_STARTED, + ScreenEvent::PLAYER_CAN_MOVE, + ScreenEvent::SCREEN_STILL, + ScreenEvent::CHOICE_BOX, + ScreenEvent::INTERACT_PROMPT, }; - for (const auto& [event, engine_event] : same){ + ScreenObservation look; + for (ScreenEvent event : ENGINE_EVENTS){ if (events.has(event)){ - look.events.add(engine_event); + look.events.set(event, true); } } + if (events.has(ScreenEvent::CHOICE_BOX)){ + look.events.set(ScreenEvent::DIALOG_BOX, true); + } return look; } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.h index 673d91c72c..2c1b7d8dd5 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.h @@ -2,7 +2,7 @@ * * From: https://github.com/PokemonAutomation/ * - * Bridges a ScreenEventObserver (Pokemon::ScreenEvents) to the story + * Bridges a ScreenEventObserver (ScreenEvents) to the story * engine's look (ScreenObservation). * */ @@ -11,16 +11,15 @@ #define PokemonAutomation_Pokemon_ScreenObservation_H #include "CommonTools/ScreenMachine/ScreenObservation.h" -#include "Pokemon/AutoStory/Pokemon_ScreenEventTypes.h" +#include "CommonTools/ScreenEvents/ScreenEventTypes.h" namespace PokemonAutomation{ namespace Pokemon{ -// A ScreenEventObserver's events as one look for the story engine: -// DIALOG_BOX is TEXT_BOX; CHOICE_BOX adds TEXT_BOX; BATTLE_STARTED is -// BATTLE; PLAYER_CAN_MOVE is CAN_MOVE; SCREEN_STILL is STILL; the fades -// and INTERACT_PROMPT keep their names. No time and no frame. +// A ScreenEventObserver's events as one look for the story engine: the +// events the engine reads (not LOCATION_BANNER, BATTLE_ENDED or +// SWITCH_HOME_MENU), and CHOICE_BOX adds DIALOG_BOX. No time and no frame. ScreenObservation screen_observation(const ScreenEvents& events); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.cpp new file mode 100644 index 0000000000..d50dccd657 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.cpp @@ -0,0 +1,21 @@ +/* Story Action Not Implemented + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Pokemon_StoryActionNotImplemented.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +StoryActionNotImplemented::StoryActionNotImplemented(std::string message) + : OperationFailedException(ErrorReportMode::NO_ERROR_REPORT, std::move(message)) +{} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.h new file mode 100644 index 0000000000..566ade4269 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.h @@ -0,0 +1,35 @@ +/* Story Action Not Implemented + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryActionNotImplemented_H +#define PokemonAutomation_Pokemon_StoryActionNotImplemented_H + +#include +#include "CommonFramework/Exceptions/OperationFailedException.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// A story action the game has no code for yet, thrown with what it waits +// for instead of pressing buttons blind. A game's backend lets it end the +// run instead of reloading and trying again, since every attempt would +// fail the same way. +class StoryActionNotImplemented : public OperationFailedException{ +public: + explicit StoryActionNotImplemented(std::string message); + + // code-health: allow char_pointer -- Exception::name() returns a const char pointer in the base class + virtual const char* name() const override{ + return "StoryActionNotImplemented"; + } +}; + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.cpp index 9313165a6e..3f09cfaeb6 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.cpp @@ -82,6 +82,7 @@ StoryActionRegistry::StoryActionRegistry(const StoryGame& ga : m_game(game) , m_actions(builtin_story_handlers()) , m_game_problems(tile_arg_problems(game)) + , m_pause(game.breakpoints, game.pause_point) { // A game's action replaces the built-in of its kind and name in place, // so a kind's default stays the built-in one's position. @@ -172,7 +173,7 @@ Outcome StoryActionRegistry::dispatch(StoryBackend& backend, )); } const StoryActionArgs args = with_fallbacks(action->spec, step.args); - return action->handler(backend, StoryActionCall{.step = step, .args = args}, state); + return action->handler(backend, StoryActionCall{.step = step, .args = args, .pause = m_pause}, state); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.h index f8adbc6c6f..7c2e923acf 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.h @@ -58,6 +58,12 @@ class StoryActionRegistry{ // actions fails too. std::vector lint(const StoryCheckpoint& checkpoint) const; + // The pause points of this game's run (StoryGame::breakpoints and + // pause_point). Mutable state of the run, not of the game's actions. + StoryPausePoints& pause_points() const{ + return m_pause; + } + // Runs `step`'s action with its args' fallbacks filled in; a step with // no registered action fails as unsupported_step(), a console_only // action on another backend as CONSOLE_ONLY, before any input. @@ -73,6 +79,7 @@ class StoryActionRegistry{ std::vector m_specs; // What is wrong with the game's own actions, reported with every lint. std::vector m_game_problems; + mutable StoryPausePoints m_pause; }; // Instantiated once, in Pokemon_StoryActionRegistry.cpp. diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBuiltinActions.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBuiltinActions.cpp index 24e610dca6..63ca85527d 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBuiltinActions.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBuiltinActions.cpp @@ -26,6 +26,10 @@ namespace{ inline constexpr size_t MAX_TEXT_LOOKS = 80; // Looks for a text box to open after a talk's A. inline constexpr size_t MAX_OPEN_LOOKS = 20; +// A press through text or an answer: held, then released before the next +// look, the same for every text press so they read alike in the log. +inline constexpr std::chrono::milliseconds TEXT_PRESS_HOLD{80}; +inline constexpr std::chrono::milliseconds TEXT_PRESS_RELEASE{120}; // The action of a built-in that needs the game's own code. @@ -51,10 +55,10 @@ Outcome mash_to_overworld(StoryBackend& backend){ using namespace std::chrono_literals; for (size_t c = 0; c < MAX_TEXT_LOOKS; c++){ const Observation now = backend.observe(); - if (now.events.has(ScreenEvent::CAN_MOVE) && !now.events.has(ScreenEvent::TEXT_BOX)){ + if (now.events.has(ScreenEvent::PLAYER_CAN_MOVE) && !now.events.has(ScreenEvent::DIALOG_BOX)){ return outcome_ok(); } - backend.press(NintendoSwitch::BUTTON_A, 80ms, 120ms); + backend.press(NintendoSwitch::BUTTON_A, TEXT_PRESS_HOLD, TEXT_PRESS_RELEASE); } return outcome_failed("mash_no_overworld", std::format("no overworld after {} looks", MAX_TEXT_LOOKS)); } @@ -69,9 +73,9 @@ Outcome mash(StoryBackend& backend, const StoryActionCall&, RunState&) template Outcome talk(StoryBackend& backend, const StoryActionCall&, RunState&){ using namespace std::chrono_literals; - backend.press(NintendoSwitch::BUTTON_A, 80ms, 120ms); + backend.press(NintendoSwitch::BUTTON_A, TEXT_PRESS_HOLD, TEXT_PRESS_RELEASE); for (size_t c = 0; c < MAX_OPEN_LOOKS; c++){ - if (backend.observe().events.has(ScreenEvent::TEXT_BOX)){ + if (backend.observe().events.has(ScreenEvent::DIALOG_BOX)){ return mash_to_overworld(backend); } backend.wait(100ms); @@ -97,7 +101,15 @@ inline std::optional after_legs(const Pose& start, const std::vector Outcome walk_legs(StoryBackend& backend, const StoryActionCall& call, RunState& state){ const std::vector& legs = story_arg>(call.args, "legs"); - for (const StoryLeg& leg : legs){ + const std::vector start = tiles_of(state.pose); + std::optional tile = start.size() == 1 ? std::optional(start[0]) : std::nullopt; + for (size_t index = 0; index < legs.size(); index++){ + const StoryLeg& leg = legs[index]; + for (int c = 0; tile && c < leg.tiles; c++){ + tile = tile->next(leg.direction); + } + call.pause.before_leg(story_leg_pause_label(index, legs.size(), leg.direction, leg.tiles, tile)); + backend.scope().throw_if_cancelled(); for (int c = 0; c < leg.tiles; c++){ backend.tap(leg.direction); } @@ -114,7 +126,7 @@ Outcome face(StoryBackend& backend, const StoryActionCall& call, RunSt template Outcome answer(StoryBackend& backend, const StoryActionCall& call, RunState&){ using namespace std::chrono_literals; - backend.press(story_arg(call.args, "yes") ? NintendoSwitch::BUTTON_A : NintendoSwitch::BUTTON_B, 80ms, 120ms); + backend.press(story_arg(call.args, "yes") ? NintendoSwitch::BUTTON_A : NintendoSwitch::BUTTON_B, TEXT_PRESS_HOLD, TEXT_PRESS_RELEASE); return outcome_ok(); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp index e215a54d82..ccfd146863 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp @@ -9,7 +9,9 @@ #include #include #include +#include "Pokemon/AutoStory/Pokemon_CheckpointAttempts.h" #include "Pokemon/AutoStory/Pokemon_StoryEvents.h" +#include "Pokemon/AutoStory/Pokemon_StoryOutcomeClass.h" #include "Pokemon_StoryEngine.h" namespace PokemonAutomation{ @@ -25,17 +27,20 @@ namespace{ template class RunnerExecutor : public StoryStepExecutor{ public: - RunnerExecutor(StoryStepRunner& runner, const StoryGame& game) + RunnerExecutor(StoryStepRunner& runner, const StoryGame& game, StoryPausePoints& pause) : m_runner(runner) , m_game(game) + , m_pause(pause) {} virtual void begin_attempt(const StoryCheckpoint& data) override{ + m_checkpoint = &data; if (m_game.begin_attempt){ m_game.begin_attempt(data); } } virtual Outcome run(const StoryStep& step, RunState& state) override{ + m_pause.before_step(pause_label(step)); return m_runner.act(step, state); } virtual bool holds(std::string_view condition, RunState& state) override{ @@ -43,18 +48,32 @@ class RunnerExecutor : public StoryStepExecutor{ } private: + // The step's place in its checkpoint (step names are unique, which the + // lint checks). + std::string pause_label(const StoryStep& step) const{ + size_t index = 0; + if (m_checkpoint != nullptr){ + while (index < m_checkpoint->steps.size() && m_checkpoint->steps[index].name != step.name){ + index++; + } + } + return story_step_pause_label(m_checkpoint == nullptr ? "" : m_checkpoint->id, index, step.name); + } + StoryStepRunner& m_runner; const StoryGame& m_game; + StoryPausePoints& m_pause; + const StoryCheckpoint* m_checkpoint = nullptr; }; // `game` with its SAVE step "save" saving `current` only while `mode` is // PER_CHECKPOINT: a checkpoint under test or verification must not save -// part-way. +// part-way. Sets `saved` when it saves. template StoryGame with_gated_save_step( - StoryGame game, const StorySaveMode& mode, const StoryCheckpoint* const& current + StoryGame game, const StorySaveMode& mode, const StoryCheckpoint* const& current, bool& saved ){ - StoryActionHandler gated = [&mode, ¤t, save = game.save]( + StoryActionHandler gated = [&mode, ¤t, &saved, save = game.save]( StoryBackend& backend, const StoryActionCall&, RunState& ){ if (mode != StorySaveMode::PER_CHECKPOINT){ @@ -64,7 +83,9 @@ StoryGame with_gated_save_step( if (!save || current == nullptr){ return outcome_failed("unsupported_step", "save needs StoryGame::save"); } - return save(backend, *current); + Outcome outcome = save(backend, *current); + saved = saved || outcome.succeeded(); + return outcome; }; game.actions.push_back({.spec = {.kind = StoryStepKind::SAVE, .name = "save", .args = {}}, .handler = std::move(gated)}); return game; @@ -162,11 +183,15 @@ ObservedState observed_state(const Observation& observation){ template StoryEngine::StoryEngine(const StoryGame& game, StoryBackend& backend) - : m_game(with_gated_save_step(game, m_save_mode, m_checkpoint)) + : m_game(with_gated_save_step(game, m_save_mode, m_checkpoint, m_saved_part_way)) , m_backend(backend) , m_registry(m_game) , m_runner(m_registry, backend) -{} +{ + m_registry.pause_points().set_log([&backend](std::string_view line){ + backend.log(line); + }); +} template std::string StoryEngine::steps_hash(const StoryCheckpoint& checkpoint) const{ @@ -199,7 +224,9 @@ StoryEngineReport StoryEngine::run_once( m_backend.log(report.save.reason); m_save_mode = report.save.mode; m_checkpoint = &checkpoint; + m_saved_part_way = false; report.run = run_steps(checkpoint, state); + report.saved_part_way = m_saved_part_way; m_save_mode = StorySaveMode::NONE; m_checkpoint = nullptr; if (!report.run.completed){ @@ -234,26 +261,41 @@ template StoryEngineReport StoryEngine::run_with_retries( const StoryCheckpoint& checkpoint, SavePoint point, size_t attempts, RunState& state, CheckpointRun& run ){ - for (size_t attempt = 1; ; attempt++){ - run.attempts = attempt; - StoryEngineReport report = run_checkpoint(checkpoint, point, state); - if (report.verdict == CheckpointVerdict::PASSED){ - run.passed = true; - m_backend.log(checkpoint_pass_line(checkpoint.id, attempt)); - return report; - } - if (attempt >= attempts || repeats_on_reload(report.run.failure)){ - return report; - } - m_backend.log(std::format("[Segment] {}: failed, reloading for attempt {}/{}", checkpoint.id, attempt + 1, attempts)); - const Outcome reloaded = reload(state); - if (!reloaded.succeeded()){ - report.run.completed = false; - report.run.failed_name.clear(); - report.run.failure = reloaded; - return report; - } - } + StoryEngineReport report; + const CheckpointAttempts loop{ + .max_attempts = attempts, + .run = [&](size_t attempt){ + run.attempts = attempt; + report = run_checkpoint(checkpoint, point, state); + if (report.verdict == CheckpointVerdict::PASSED){ + return AttemptResult::PASSED; + } + if (report.saved_part_way){ + m_backend.log(std::format( + "[Checkpoint] {}: a save step saved part-way, so a reload would not return to its start: no retry", + checkpoint.id + )); + return AttemptResult::FINAL; + } + return repeats_on_reload(report.run.failure) ? AttemptResult::FINAL : AttemptResult::FAILED; + }, + .reset = [&](size_t next_attempt){ + m_backend.log(checkpoint_reset_line(checkpoint.id, next_attempt, attempts)); + const Outcome reloaded = reload(state); + if (!reloaded.succeeded()){ + report.run.completed = false; + report.run.failed_name.clear(); + report.run.failure = reloaded; + return false; + } + return true; + }, + .log = [this](const std::string& line){ + m_backend.log(line); + }, + }; + run.passed = run_checkpoint_attempts(checkpoint.id, loop).passed; + return report; } template @@ -280,7 +322,7 @@ SegmentReport StoryEngine::run_segment( report.failed_checkpoint = checkpoint.id; report.failed_event = result.run.failed_name; report.failure = run_failure(result); - m_backend.log(std::format("[Segment] {}: failed at {}: {}", id, checkpoint.id, to_string(report.failure))); + m_backend.log(std::format("[Segment] {}: failed at {}: {}", id, checkpoint.id, to_string_with_class(report.failure))); return report; } start_saved = result.saved; @@ -320,7 +362,7 @@ VerifyReport StoryEngine::verify_checkpoint(const StoryCheckpoint& che report.saved = result.saved; m_backend.log(std::format( "[Verify] {}: run {}/{} {}", checkpoint.id, run, policy.runs, - passed ? std::string("passed") : std::format("failed: {}", to_string(run_failure(result))) + passed ? std::string("passed") : std::format("failed: {}", to_string_with_class(run_failure(result))) )); if (repeats_on_reload(result.run.failure)){ report.failure = result.run.failure; @@ -337,7 +379,7 @@ VerifyReport StoryEngine::verify_checkpoint(const StoryCheckpoint& che template CheckpointReport StoryEngine::run_steps(const StoryCheckpoint& checkpoint, RunState& state){ - RunnerExecutor executor(m_runner, m_game); + RunnerExecutor executor(m_runner, m_game, m_registry.pause_points()); Checkpoint events = build_story_checkpoint(checkpoint, executor, &m_prepared); if (m_game.interrupts){ events.interrupts = m_game.interrupts(m_backend); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h index e7cabbcf9a..185ab5dc52 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h @@ -70,9 +70,10 @@ class StoryEngine{ // `attempts_per_checkpoint` runs, only when a reload returns to the // save it started from: the first checkpoint's, or one the checkpoint // before it saved at its end. In none and after_verification modes - // (#329) that leaves only the first. No reload either for a failure - // every attempt would repeat: a lint, a console_only action, the game - // not running. Logs "[Segment] : ..." lines. + // that leaves only the first. No reload once a SAVE step has saved + // part-way through the attempt (the reload would land there), nor for + // a failure every attempt would repeat: a lint, a console_only action, + // the game not running. Logs "[Segment] : ..." lines. SegmentReport run_segment( std::string_view id, std::span checkpoints, size_t attempts_per_checkpoint, RunState& state @@ -109,9 +110,11 @@ class StoryEngine{ const StoryCheckpoint& checkpoint, SavePoint point, size_t attempts, RunState& state, CheckpointRun& run ); - // Declared first: m_game's save step reads them. + // Declared first: m_game's save step reads them, and sets + // m_saved_part_way when it saves. StorySaveMode m_save_mode = StorySaveMode::NONE; const StoryCheckpoint* m_checkpoint = nullptr; + bool m_saved_part_way = false; StoryGame m_game; StoryBackend& m_backend; StoryActionRegistry m_registry; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEnginePolicy.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEnginePolicy.h index 0856147d0b..197fce0023 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEnginePolicy.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEnginePolicy.h @@ -52,6 +52,9 @@ struct StoryEngineReport{ // Checked after steps that completed. std::optional postconditions = std::nullopt; bool saved = false; + // A SAVE step among its steps saved: a reload now returns there, not + // to the save the checkpoint started from. + bool saved_part_way = false; }; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEnginePolicy_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEnginePolicy_Tests.cpp index 1a208e8862..c1f7b74094 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEnginePolicy_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEnginePolicy_Tests.cpp @@ -55,7 +55,7 @@ enum class HashRecord{ // A look at B3F (12, 13) or wherever `map` is. Observation end_look(std::string map){ Observation observation; - observation.events = ScreenEvents{ScreenEvent::CAN_MOVE}; + observation.events = ScreenEvents{ScreenEvent::PLAYER_CAN_MOVE}; observation.pose = GridPose{.map = std::move(map), .tiles = {{12, 13}}, .facing = Direction::NORTH}; return observation; } @@ -83,57 +83,59 @@ StoryGame engine_game(std::optional candies){ -// One engine run of 00.31 in `phase`, the player ending on `end_map`. -struct EngineInput{ - std::string_view name; - std::string_view phase; - HashRecord hash = HashRecord::MATCHING; - std::string_view steps = TALK_STEPS; - // Where the talk leaves the player; empty: the talk opens no text. - std::string_view end_map = "RocketHideout_B3F"; - std::optional candies = 2; -}; +struct EngineCase{ + // One engine run of 00.31 in `phase`, the player ending on `end_map`. + struct Input{ + std::string_view phase; + HashRecord hash = HashRecord::MATCHING; + std::string_view steps = TALK_STEPS; + // Where the talk leaves the player; empty: the talk opens no text. + std::string_view end_map = "RocketHideout_B3F"; + std::optional candies = 2; + }; -// The save mode chosen, the verdict, whether the game saved (each save -// logs "[Test] save") and a line the run logged. -struct EngineExpectation{ - StorySaveMode mode = StorySaveMode::NONE; - CheckpointVerdict verdict = CheckpointVerdict::PASSED; - size_t saves = 0; - std::string_view line = {}; -}; + // The save mode chosen, the verdict, whether the game saved (each save + // logs "[Test] save") and a line the run logged. + struct Expectation{ + StorySaveMode mode = StorySaveMode::NONE; + CheckpointVerdict verdict = CheckpointVerdict::PASSED; + size_t saves = 0; + std::string_view line = {}; + }; -struct EngineCase{ - EngineInput input; - EngineExpectation expectation; + std::string_view name; + Input input; + Expectation expectation; }; const EngineCase ENGINE_CASES[] = { - {.input = {.name = "testing never saves", .phase = "testing"}, + {.name = "testing never saves", .input = {.phase = "testing"}, .expectation = {.mode = StorySaveMode::NONE, .saves = 0, .line = "[Save] 00.31: phase testing: save mode none"}}, - {.input = {.name = "verification saves at the end", .phase = "verification"}, + {.name = "verification saves at the end", .input = {.phase = "verification"}, .expectation = {.mode = StorySaveMode::AFTER_VERIFICATION, .saves = 1, .line = "[Save] 00.31: saved"}}, - {.input = {.name = "reproduction saves", .phase = "reproduction"}, + {.name = "reproduction saves", .input = {.phase = "reproduction"}, .expectation = {.mode = StorySaveMode::PER_CHECKPOINT, .saves = 1, .line = "[Save] 00.31: phase reproduction: save mode per_checkpoint"}}, - {.input = {.name = "stale steps_hash is testing", .phase = "verification", .hash = HashRecord::STALE}, + {.name = "stale steps_hash is testing", .input = {.phase = "verification", .hash = HashRecord::STALE}, .expectation = {.mode = StorySaveMode::NONE, .saves = 0, .line = "[Save] 00.31: phase verification but steps_hash mismatch (recorded sha256:old, now "}}, - {.input = {.name = "no steps_hash is testing", .phase = "reproduction", .hash = HashRecord::MISSING}, + {.name = "no steps_hash is testing", .input = {.phase = "reproduction", .hash = HashRecord::MISSING}, .expectation = {.mode = StorySaveMode::NONE, .saves = 0, .line = "[Save] 00.31: phase reproduction but steps_hash mismatch (recorded none, now "}}, - {.input = {.name = "B4F false pass fails postconditions", .phase = "verification", .end_map = "RocketHideout_B4F"}, + {.name = "B4F false pass fails postconditions", .input = {.phase = "verification", .end_map = "RocketHideout_B4F"}, .expectation = {.mode = StorySaveMode::AFTER_VERIFICATION, .verdict = CheckpointVerdict::FAILED, .saves = 0, .line = "[Postcondition] 00.31 map: failed: expected RocketHideout_B3F, read RocketHideout_B4F"}}, - {.input = {.name = "unread bag is not checked", .phase = "verification", .candies = std::nullopt}, + {.name = "unread bag is not checked", .input = {.phase = "verification", .candies = std::nullopt}, .expectation = {.mode = StorySaveMode::AFTER_VERIFICATION, .saves = 1, .line = "[Postcondition] 00.31: 3 passed, 0 failed, 1 not checked: incomplete"}}, - {.input = {.name = "wrong candy count fails", .phase = "reproduction", .candies = 1}, + {.name = "wrong candy count fails", .input = {.phase = "reproduction", .candies = 1}, .expectation = {.mode = StorySaveMode::PER_CHECKPOINT, .verdict = CheckpointVerdict::FAILED, .saves = 0, .line = "[Postcondition] 00.31 item RARE_CANDY: failed: expected 2, read 1"}}, - {.input = {.name = "failed run never saves", .phase = "reproduction", .end_map = ""}, + {.name = "failed run never saves", .input = {.phase = "reproduction", .end_map = ""}, .expectation = {.mode = StorySaveMode::PER_CHECKPOINT, .verdict = CheckpointVerdict::FAILED, .saves = 0, .line = "[Save] 00.31: run failed: not saved"}}, - {.input = {.name = "save step skipped while verifying", .phase = "verification", .steps = SAVE_STEPS}, + {.name = "save step skipped while testing", .input = {.phase = "testing", .steps = SAVE_STEPS}, + .expectation = {.mode = StorySaveMode::NONE, .saves = 0, .line = "[Save] save step skipped: save mode none"}}, + {.name = "save step skipped while verifying", .input = {.phase = "verification", .steps = SAVE_STEPS}, .expectation = {.mode = StorySaveMode::AFTER_VERIFICATION, .saves = 1, .line = "[Save] save step skipped: save mode after_verification"}}, - {.input = {.name = "save step saves in reproduction", .phase = "reproduction", .steps = SAVE_STEPS}, + {.name = "save step saves in reproduction", .input = {.phase = "reproduction", .steps = SAVE_STEPS}, .expectation = {.mode = StorySaveMode::PER_CHECKPOINT, .saves = 2, .line = "[Step] save_here SAVE/default: ok"}}, }; @@ -144,7 +146,7 @@ std::vector> engine_looks(std::string_view end_map){ return {end_look("RocketHideout_B3F")}; } Observation text; - text.events = ScreenEvents{ScreenEvent::TEXT_BOX}; + text.events = ScreenEvents{ScreenEvent::DIALOG_BOX}; return {std::move(text), end_look(std::string(end_map))}; } @@ -166,7 +168,7 @@ std::string check_engine(const EngineCase& test, CancellableScope& scope){ FakeStoryBackend backend({.looks = engine_looks(test.input.end_map)}, scope); StoryEngine engine(game, backend); StoryCheckpoint checkpoint = read_story_checkpoint( - parse_json(checkpoint_0031(test.input.phase, test.input.steps)), test.input.name, engine.registry().specs() + parse_json(checkpoint_0031(test.input.phase, test.input.steps)), test.name, engine.registry().specs() ); if (test.input.hash != HashRecord::MISSING){ checkpoint.metadata.verified = StoryVerification{ @@ -188,7 +190,7 @@ std::string check_engine(const EngineCase& test, CancellableScope& scope){ class StoryEngineSaveTest : public UnitTest{ public: explicit StoryEngineSaveTest(EngineCase test) - : UnitTest(std::format("Pokemon::StoryEngine save - {}", test.input.name)) + : UnitTest(std::format("Pokemon::StoryEngine save - {}", test.name)) , m_case(std::move(test)) {} virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{ @@ -201,40 +203,40 @@ class StoryEngineSaveTest : public UnitTest{ -// Two checkpoints' events, hashed with the built-in actions. -struct HashInput{ - std::string_view name; - std::string_view a; - std::string_view b; -}; +struct HashCase{ + // Two checkpoints' events, hashed with the built-in actions. + struct Input{ + std::string_view a; + std::string_view b; + }; -// Whether they hash the same, and `a`'s hash when pinned: a change to it -// means every recorded verification goes stale. -struct HashExpectation{ - bool same = true; - std::optional hash_a = std::nullopt; -}; + // Whether they hash the same, and `a`'s hash when pinned: a change to it + // means every recorded verification goes stale. + struct Expectation{ + bool same = true; + std::optional hash_a = std::nullopt; + }; -struct HashCase{ - HashInput input; - HashExpectation expectation; + std::string_view name; + Input input; + Expectation expectation; }; inline constexpr std::string_view ROUTE_B3F = R"([{"id": "go", "kind": "route", "args": {"target": {"map": "RocketHideout_B3F", "tiles": [[12, 13]]}}}])"; const HashCase HASH_CASES[] = { - {.input = {.name = "pinned", .a = ROUTE_B3F, .b = ROUTE_B3F}, + {.name = "pinned", .input = {.a = ROUTE_B3F, .b = ROUTE_B3F}, .expectation = {.same = true, .hash_a = "sha256:13bc877c960a15cd0ed2f37248fa8336308b048a865623dde7654d41ac7147b8"}}, - {.input = {.name = "whitespace is not hashed", .a = R"([{"id": "t", "kind": "talk"}])", .b = R"([ { "id":"t", "kind":"talk" } ])"}, + {.name = "whitespace is not hashed", .input = {.a = R"([{"id": "t", "kind": "talk"}])", .b = R"([ { "id":"t", "kind":"talk" } ])"}, .expectation = {.same = true}}, - {.input = {.name = "why differs", .a = R"([{"id": "t", "kind": "talk", "why": "a"}])", .b = R"([{"id": "t", "kind": "talk", "why": "b"}])"}, + {.name = "why differs", .input = {.a = R"([{"id": "t", "kind": "talk", "why": "a"}])", .b = R"([{"id": "t", "kind": "talk", "why": "b"}])"}, .expectation = {.same = false}}, - {.input = {.name = "written fallback differs", .a = R"([{"id": "f", "kind": "battle"}])", .b = R"([{"id": "f", "kind": "battle", "args": {"may_lose": false}}])"}, + {.name = "written fallback differs", .input = {.a = R"([{"id": "f", "kind": "battle"}])", .b = R"([{"id": "f", "kind": "battle", "args": {"may_lose": false}}])"}, .expectation = {.same = false}}, - {.input = {.name = "another target differs", .a = ROUTE_B3F, + {.name = "another target differs", .input = { .a = ROUTE_B3F, .b = R"([{"id": "go", "kind": "route", "args": {"target": {"map": "RocketHideout_B3F", "tiles": [[12, 14]]}}}])"}, .expectation = {.same = false}}, - {.input = {.name = "another step name differs", .a = R"([{"id": "t", "kind": "talk"}])", .b = R"([{"id": "u", "kind": "talk"}])"}, + {.name = "another step name differs", .input = {.a = R"([{"id": "t", "kind": "talk"}])", .b = R"([{"id": "u", "kind": "talk"}])"}, .expectation = {.same = false}}, }; @@ -244,8 +246,8 @@ std::string hash_of(std::string_view events, std::string_view source){ } std::string check_hash(const HashCase& test){ - const std::string a = hash_of(test.input.a, test.input.name); - const std::string b = hash_of(test.input.b, test.input.name); + const std::string a = hash_of(test.input.a, test.name); + const std::string b = hash_of(test.input.b, test.name); if ((a == b) != test.expectation.same){ return std::format("{}{}: {}, {}", "expected the hashes ", (test.expectation.same ? "equal" : "different"), a, b); } @@ -258,7 +260,7 @@ std::string check_hash(const HashCase& test){ class StoryStepsHashTest : public UnitTest{ public: explicit StoryStepsHashTest(HashCase test) - : UnitTest(std::format("Pokemon::StoryEngine steps_hash - {}", test.input.name)) + : UnitTest(std::format("Pokemon::StoryEngine steps_hash - {}", test.name)) , m_case(std::move(test)) {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ @@ -271,35 +273,35 @@ class StoryStepsHashTest : public UnitTest{ -// A checkpoint's "metadata", read. -struct MetadataInput{ - std::string_view name; - std::string_view metadata; -}; +struct MetadataCase{ + // A checkpoint's "metadata", read. + struct Input{ + std::string_view metadata; + }; -// The error the read throws (contained), or what it read. -struct MetadataExpectation{ - std::string_view error = {}; - StoryPhase phase = StoryPhase::TESTING; - std::string_view steps_hash = {}; - size_t items = 0; -}; + // The error the read throws (contained), or what it read. + struct Expectation{ + std::string_view error = {}; + StoryPhase phase = StoryPhase::TESTING; + std::string_view steps_hash = {}; + size_t items = 0; + }; -struct MetadataCase{ - MetadataInput input; - MetadataExpectation expectation; + std::string_view name; + Input input; + Expectation expectation; }; const MetadataCase METADATA_CASES[] = { - {.input = {.name = "none is testing", .metadata = ""}, + {.name = "none is testing", .input = {.metadata = ""}, .expectation = {.phase = StoryPhase::TESTING}}, - {.input = {.name = "verified", .metadata = R"(, "metadata": {"phase": "verification", "verified": {"steps_hash": "sha256:ab", "build": "1", "date": "d", "runs": "3/3"}})"}, + {.name = "verified", .input = {.metadata = R"(, "metadata": {"phase": "verification", "verified": {"steps_hash": "sha256:ab", "build": "1", "date": "d", "runs": "3/3"}})"}, .expectation = {.phase = StoryPhase::VERIFICATION, .steps_hash = "sha256:ab"}}, - {.input = {.name = "postconditions", .metadata = R"(, "metadata": {"phase": "reproduction", "postconditions": {"items": [{"item": "LIFT_KEY", "quantity": 1}]}})"}, + {.name = "postconditions", .input = {.metadata = R"(, "metadata": {"phase": "reproduction", "postconditions": {"items": [{"item": "LIFT_KEY", "quantity": 1}]}})"}, .expectation = {.phase = StoryPhase::REPRODUCTION, .items = 1}}, - {.input = {.name = "unknown phase", .metadata = R"(, "metadata": {"phase": "tested"})"}, + {.name = "unknown phase", .input = {.metadata = R"(, "metadata": {"phase": "tested"})"}, .expectation = {.error = "unknown phase tested"}}, - {.input = {.name = "unknown field", .metadata = R"(, "metadata": {"phase": "testing", "runs": 3})"}, + {.name = "unknown field", .input = {.metadata = R"(, "metadata": {"phase": "testing", "runs": 3})"}, .expectation = {.error = "unsupported field runs"}}, }; @@ -328,7 +330,7 @@ std::string check_metadata(const MetadataCase& test){ class StoryMetadataTest : public UnitTest{ public: explicit StoryMetadataTest(MetadataCase test) - : UnitTest(std::format("Pokemon::StoryEngine metadata - {}", test.input.name)) + : UnitTest(std::format("Pokemon::StoryEngine metadata - {}", test.name)) , m_case(std::move(test)) {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ @@ -341,28 +343,28 @@ class StoryMetadataTest : public UnitTest{ -// What a look showed, classified by ScreenMachine states that match on -// observations. -struct ScreenInput{ - std::string_view name; - std::vector events; -}; +struct ScreenCase{ + // What a look showed, classified by ScreenMachine states that match on + // observations. + struct Input{ + std::vector events; + }; -// The state matched; none when no state did. -struct ScreenExpectation{ - std::optional state = std::nullopt; -}; + // The state matched; none when no state did. + struct Expectation{ + std::optional state = std::nullopt; + }; -struct ScreenCase{ - ScreenInput input; - ScreenExpectation expectation; + std::string_view name; + Input input; + Expectation expectation; }; const ScreenCase SCREEN_CASES[] = { - {.input = {.name = "battle first", .events = {ScreenEvent::BATTLE, ScreenEvent::TEXT_BOX}}, .expectation = {.state = "battle"}}, - {.input = {.name = "text", .events = {ScreenEvent::TEXT_BOX}}, .expectation = {.state = "text"}}, - {.input = {.name = "overworld", .events = {ScreenEvent::CAN_MOVE}}, .expectation = {.state = "overworld"}}, - {.input = {.name = "image-only state skipped", .events = {ScreenEvent::FADE_WHITE}}, .expectation = {}}, + {.name = "battle first", .input = {.events = {ScreenEvent::BATTLE_STARTED, ScreenEvent::DIALOG_BOX}}, .expectation = {.state = "battle"}}, + {.name = "text", .input = {.events = {ScreenEvent::DIALOG_BOX}}, .expectation = {.state = "text"}}, + {.name = "overworld", .input = {.events = {ScreenEvent::PLAYER_CAN_MOVE}}, .expectation = {.state = "overworld"}}, + {.name = "image-only state skipped", .input = {.events = {ScreenEvent::FADE_WHITE}}, .expectation = {}}, }; ScreenState observation_state(std::string name, ScreenEvent event){ @@ -377,9 +379,9 @@ ScreenState observation_state(std::string name, ScreenEvent event){ std::string check_screen(const ScreenCase& test){ std::vector states{ - observation_state("battle", ScreenEvent::BATTLE), - observation_state("text", ScreenEvent::TEXT_BOX), - observation_state("overworld", ScreenEvent::CAN_MOVE), + observation_state("battle", ScreenEvent::BATTLE_STARTED), + observation_state("text", ScreenEvent::DIALOG_BOX), + observation_state("overworld", ScreenEvent::PLAYER_CAN_MOVE), }; ScreenState image_only; image_only.name = "image_only"; @@ -390,7 +392,7 @@ std::string check_screen(const ScreenCase& test){ states.push_back(std::move(image_only)); ScreenObservation observation; for (ScreenEvent event : test.input.events){ - observation.events.add(event); + observation.events.set(event, true); } const ScreenState* state = match_screen_state(states, observation); const std::string got = state ? state->name : "none"; @@ -400,7 +402,7 @@ std::string check_screen(const ScreenCase& test){ class ScreenObservationTest : public UnitTest{ public: explicit ScreenObservationTest(ScreenCase test) - : UnitTest(std::format("Pokemon::StoryEngine screen observation - {}", test.input.name)) + : UnitTest(std::format("Pokemon::StoryEngine screen observation - {}", test.name)) , m_case(std::move(test)) {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineRuns_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineRuns_Tests.cpp index d272a19648..9f4a51a3d8 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineRuns_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineRuns_Tests.cpp @@ -5,12 +5,11 @@ * What a game needs from the engine to drop its own workarounds (the PLZA * move, #335): a scripted backend for any pose type, the move stick and * the heading reading, the choice and interact events, a screen machine - * over observations, the step in an action's call, the checkpoint in the - * save, the segment and verify loops, console-only actions, declared poses - * read as the game's pose type, and tile actions only for tile games. + * over observations, the step in an action's call, declared poses of the + * game's kind only, and tile actions only for tile games. * Then what PLZA found on the engine: readers opt in to the postcondition * check, inputs logged by button name, waiting states may stay still, and - * the stick's release. + * the stick's release. It also checks the engine's breakpoints. * */ @@ -60,15 +59,6 @@ bool has_line(const FakeStoryBackend& backend, std::string_view line){ return false; } -template -size_t count_lines(const FakeStoryBackend& backend, std::string_view line){ - size_t count = 0; - for (const std::string& each : backend.lines()){ - count += each == line ? 1 : 0; - } - return count; -} - template std::string expect_inputs(const FakeStoryBackend& backend, std::string_view expected){ const std::string got = joined(backend.inputs()); @@ -98,16 +88,6 @@ StoryCheckpoint checkpoint_of(std::string id, std::vector steps){ return checkpoint; } -// `checkpoint` in `phase`, verified with its steps as they are now. -template -StoryCheckpoint in_phase(StoryCheckpoint checkpoint, StoryPhase phase, const StoryEngine& engine){ - checkpoint.metadata.phase = phase; - checkpoint.metadata.verified = StoryVerification{ - .steps_hash = engine.steps_hash(checkpoint), .build = "abc", .date = "2026-11-10", .runs = "3/3", - }; - return checkpoint; -} - // A GAME action of no args. template StoryAction game_action(std::string name, StoryActionHandler handler){ @@ -143,7 +123,7 @@ StoryGame flaky_game(size_t failures){ // only its zone, run on the scripted backend of its pose type. std::string zone_checkpoint_runs(CancellableScope& scope){ StoryGame game = flaky_game(0); - FakeStoryBackend backend({.looks = {look({ScreenEvent::CAN_MOVE})}}, scope); + FakeStoryBackend backend({.looks = {look({ScreenEvent::PLAYER_CAN_MOVE})}}, scope); StoryEngine engine(game, backend); const StoryCheckpoint checkpoint = read_story_checkpoint(parse_json(R"({ "id": "03.01", "title": "Down to the lobby", "start": {"place": "HotelZ", "spot": "Room", "heading": 180}, @@ -297,7 +277,7 @@ std::vector choice_states(std::vector& acted){ std::vector states{ state("choice", ScreenEvent::CHOICE_BOX), state("prompt", ScreenEvent::INTERACT_PROMPT), - state("text", ScreenEvent::TEXT_BOX), + state("text", ScreenEvent::DIALOG_BOX), }; states[0].terminal = true; return states; @@ -318,7 +298,7 @@ ScreenMachineResult run_on_events( const std::function observe = [&]{ ScreenObservation observation; observation.when = start + std::chrono::seconds(next); - observation.events.add(events[std::min(next, events.size() - 1)]); + observation.events.set(events[std::min(next, events.size() - 1)], true); next++; return observation; }; @@ -329,7 +309,7 @@ std::string observation_machine_ends(CancellableScope& scope){ std::vector acted; std::vector lines; const ScreenMachineResult result = run_on_events( - {ScreenEvent::INTERACT_PROMPT, ScreenEvent::TEXT_BOX, ScreenEvent::CHOICE_BOX}, scope, 60s, acted, lines + {ScreenEvent::INTERACT_PROMPT, ScreenEvent::DIALOG_BOX, ScreenEvent::CHOICE_BOX}, scope, 60s, acted, lines ); if (!result.succeeded() || result.terminal != "choice"){ return std::format("expected the run to end on choice, got \"{}\"", result.failure); @@ -346,15 +326,15 @@ std::string observation_machine_ends(CancellableScope& scope){ std::string observation_machine_fails(CancellableScope& scope){ std::vector acted; std::vector lines; - ScreenMachineResult result = run_on_events({ScreenEvent::BATTLE}, scope, 60s, acted, lines); + ScreenMachineResult result = run_on_events({ScreenEvent::BATTLE_STARTED}, scope, 60s, acted, lines); if (result.failure != "The screen matches none of the known states."){ return std::format("expected no state to match, got \"{}\"", result.failure); } - result = run_on_events({ScreenEvent::TEXT_BOX}, scope, 60s, acted, lines); + result = run_on_events({ScreenEvent::DIALOG_BOX}, scope, 60s, acted, lines); if (!result.failure.starts_with("Screen \"text\" came up more than 3 times")){ return std::format("expected text to come up too often, got \"{}\"", result.failure); } - result = run_on_events({ScreenEvent::TEXT_BOX, ScreenEvent::INTERACT_PROMPT, ScreenEvent::TEXT_BOX}, scope, 1s, acted, lines); + result = run_on_events({ScreenEvent::DIALOG_BOX, ScreenEvent::INTERACT_PROMPT, ScreenEvent::DIALOG_BOX}, scope, 1s, acted, lines); if (result.failure != "Did not finish within 1s."){ return std::format("expected the looks' time to run out, got \"{}\"", result.failure); } @@ -366,7 +346,7 @@ std::string observation_machine_fails(CancellableScope& scope){ Observation framed_look(WallClock start, int seconds, uint32_t shade, ScreenEvent event){ Observation observation; observation.when = start + std::chrono::seconds(seconds); - observation.events.add(event); + observation.events.set(event, true); ImageRGB32 image(16, 16); image.fill(shade); observation.frame = VideoSnapshot(std::move(image), observation.when); @@ -387,7 +367,7 @@ ScreenMachineResult run_still_states(const std::vector>& l ScreenState text; text.name = "text"; text.matches_observation = [](const ScreenObservation& observation){ - return observation.events.has(ScreenEvent::TEXT_BOX); + return observation.events.has(ScreenEvent::DIALOG_BOX); }; text.act = [&backend]{ backend.press(NintendoSwitch::BUTTON_A, 80ms, 120ms); @@ -396,7 +376,7 @@ ScreenMachineResult run_still_states(const std::vector>& l ScreenState done; done.name = "done"; done.matches_observation = [](const ScreenObservation& observation){ - return observation.events.has(ScreenEvent::CAN_MOVE); + return observation.events.has(ScreenEvent::PLAYER_CAN_MOVE); }; done.max_visits = SIZE_MAX; done.terminal = true; @@ -416,7 +396,7 @@ std::string still_states(CancellableScope& scope){ for (int second = 0; second < 12; second++){ waits.push_back(framed_look(start, second, 0xff000000, ScreenEvent::FADE_BLACK)); } - waits.push_back(framed_look(start, 12, 0xff808080, ScreenEvent::CAN_MOVE)); + waits.push_back(framed_look(start, 12, 0xff808080, ScreenEvent::PLAYER_CAN_MOVE)); ScreenMachineResult result = run_still_states(waits, scope); if (result.terminal != "done"){ return std::format("expected a 12 s still fade to end on done, got \"{}\"", result.failure); @@ -431,7 +411,7 @@ std::string still_states(CancellableScope& scope){ } std::vector> presses; for (int second = 0; second < 12; second++){ - presses.push_back(framed_look(start, second, 0xffffffff, ScreenEvent::TEXT_BOX)); + presses.push_back(framed_look(start, second, 0xffffffff, ScreenEvent::DIALOG_BOX)); } result = run_still_states(presses, scope); if (result.failure != "The screen stopped changing: 8 steps on \"text\" did nothing."){ @@ -517,203 +497,183 @@ std::string handler_sees_step(CancellableScope& scope){ return *seen == "opening / the long intro" ? "" : std::format("expected the step, got \"{}\"", *seen); } -// Gap 6: the save is told its checkpoint, at the end and at a SAVE step. -std::string save_gets_checkpoint(CancellableScope& scope){ - StoryGame game = flaky_game(0); - FakeStoryBackend backend({.looks = {look({ScreenEvent::CAN_MOVE})}}, scope); - StoryEngine engine(game, backend); - StoryStep save; - save.kind = StoryStepKind::SAVE; - save.name = "save_here"; - const StoryCheckpoint checkpoint = in_phase(checkpoint_of("02.01", {game_step("walk", "fine"), save}), StoryPhase::REPRODUCTION, engine); - RunState state; - const StoryEngineReport report = engine.run_checkpoint(checkpoint, SavePoint::CHECKPOINT_END, state); - if (!report.saved){ - return "expected the checkpoint to save at its end"; - } - const size_t saves = count_lines(backend, "[Test] save 02.01"); - return saves == 2 ? "" : std::format("expected 2 saves of 02.01, got {}: {}", saves, joined(backend.lines())); -} - -// Gap 8: a console_only action is refused, before any input, on a backend -// that is no console; it runs on one. -StoryGame console_game(){ - StoryGame game = flaky_game(0); - StoryAction menu = game_action("open_menu", [](StoryBackend& backend, const StoryActionCall&, RunState&){ - backend.press(NintendoSwitch::BUTTON_X, 80ms, 120ms); +// A game whose "mark" action records "run " and whose pause hook +// records "pause