Repository navigation
AutoStory: story step types and engine (split from #122, layer 1 of 3) #312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
10f0583
5b974f0
1b74c82
dd9529b
babbf85
48c10b7
c766e87
d3e856e
093d1ab
a940ce0
e8833b3
31ddeae
8dad6da
8fd27c7
c51e57f
20c4961
6a1e81a
8e82401
f3e190d
f2962a9
af19559
50adad1
e1f068b
aa4f2bd
ee30e9f
2428141
fff3615
264b725
d535358
1420085
27c91d3
41905b9
d898f8f
b7bd1a6
f3e65fb
456f87a
62667cb
630f19b
4f2339d
a402d33
895fe4f
ba5efca
a8546e7
fb669bf
9b28861
3438d7a
dc7afd6
07eae1d
e4ce5af
0957678
68e4ecf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| name: Story data codec | ||
| on: | ||
| pull_request: | ||
| workflow_dispatch: | ||
| jobs: | ||
| native-codec: | ||
| strategy: | ||
| fail-fast: false | ||
| matrix: | ||
| os: [ubuntu-latest, macos-latest, windows-latest] | ||
| runs-on: ${{ matrix.os }} | ||
| steps: | ||
| - uses: actions/checkout@v7 | ||
| - uses: actions/checkout@v7 | ||
| with: | ||
| repository: dayfine/PokemonSimulation | ||
| # Synthetic contract cases, not an approved map/resource pin. | ||
| ref: 866613fa03e5c4a5eb7e57c91b59fafb44fc7ee3 | ||
| path: publisher | ||
| - uses: actions/setup-python@v6 | ||
| with: | ||
| python-version: '3.12' | ||
| - name: Check the Python reader against the same cases | ||
| working-directory: publisher | ||
| env: | ||
| PYTHONPATH: src | ||
| run: python -m unittest pokesim.story.test_checkpoint_wire | ||
| - 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* Shared Authored Checkpoint JSON | ||
| * | ||
| * From: https://github.com/PokemonAutomation/ | ||
| * | ||
| */ | ||
|
|
||
| #include <format> | ||
| #include <utility> | ||
| #include "Common/Cpp/Exceptions.h" | ||
| #include "Pokemon_StoryStepJson.h" | ||
| #include "Pokemon_AuthoredCheckpointJson.h" | ||
|
|
||
| namespace PokemonAutomation{ | ||
| namespace Pokemon{ | ||
|
|
||
|
|
||
| void validate_json_fields( | ||
| const JsonObject& object, std::initializer_list<std::string_view> fields, std::string_view source | ||
| ){ | ||
| if (object.size() != fields.size()){ | ||
| throw ParseException(std::format("{}: missing or extra authored checkpoint fields.", source)); | ||
| } | ||
| for (std::string_view field : fields){ | ||
| object.get_value_throw(std::string(field), std::string(source)); | ||
| } | ||
| } | ||
|
|
||
| StoryCheckpoint read_authored_checkpoint( | ||
| const JsonValue& value, std::string_view game, std::string_view source, | ||
| const ExecutionReader& read_execution, std::span<const StoryActionSpec> actions | ||
| ){ | ||
| const JsonObject& root = value.to_object_throw(std::string(source)); | ||
| validate_json_fields(root, {"format", "game", "checkpoint", "execution"}, source); | ||
| if (game.empty() || root.get_string_throw("format") != "pokesim-authored-checkpoint/1" | ||
| || root.get_string_throw("game") != game | ||
| ){ | ||
| throw ParseException(std::format("{}: unsupported authored checkpoint format/game.", source)); | ||
| } | ||
| StoryCheckpoint checkpoint = read_story_checkpoint(root.get_value_throw("checkpoint"), source, actions); | ||
| read_execution(root.get_object_throw("execution", std::string(source)), source); | ||
| return checkpoint; | ||
| } | ||
|
|
||
| JsonValue authored_checkpoint_json(const StoryCheckpoint& checkpoint, std::string_view game, JsonObject execution){ | ||
| if (game.empty()){ | ||
| throw ParseException("Authored checkpoint game must not be empty."); | ||
| } | ||
| JsonObject result; | ||
| result["format"] = "pokesim-authored-checkpoint/1"; | ||
| result["game"] = std::string(game); | ||
| result["checkpoint"] = story_checkpoint_json(checkpoint); | ||
| result["execution"] = std::move(execution); | ||
| return result; | ||
| } | ||
|
|
||
| StoryCheckpoint load_authored_checkpoint( | ||
| std::span<const EmbeddedJsonFile> files, std::string_view file, std::string_view game, | ||
| const ExecutionReader& read_execution, std::span<const StoryActionSpec> actions | ||
| ){ | ||
| for (const auto& [name, text] : files){ | ||
| if (name == file){ | ||
| return read_authored_checkpoint(parse_json(text), game, file, read_execution, actions); | ||
| } | ||
| } | ||
| throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format("Authored checkpoint is not embedded: {}", file)); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| } // namespace Pokemon | ||
| } // namespace PokemonAutomation |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* Shared Authored Checkpoint JSON | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this simply parsing? AuthoredCheckpoint - I wonder if we need to keep both at the same time
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it only parses and encodes; the header now says so (e8833b3).
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the main feedback is to keep IO and in-game logic separate so parsing should be done in one layer, then everything else no longer need JSON, is that fair / possible?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 31ddeae: read_story_step() parses args against the action's spec and fills fallbacks; the authored envelope hands execution to the game's reader and AuthoredCheckpoint (with its JsonObject) is gone. |
||
| * | ||
| * From: https://github.com/PokemonAutomation/ | ||
| * | ||
| * Reading and writing an authored checkpoint file: the shared | ||
| * StoryCheckpoint (Pokemon_StoryStepJson.h) in an envelope that names the | ||
| * game, plus the game's own execution object, which the game parses into | ||
| * its own type through a reader it passes in. Only parsing and encoding: | ||
| * nothing here binds or runs a step, and nothing read keeps JSON. | ||
| * | ||
| */ | ||
|
|
||
| #ifndef PokemonAutomation_AuthoredCheckpointJson_H | ||
| #define PokemonAutomation_AuthoredCheckpointJson_H | ||
|
|
||
| #include <functional> | ||
| #include <initializer_list> | ||
| #include <span> | ||
| #include <string> | ||
| #include <string_view> | ||
| #include <utility> | ||
| #include "Common/Cpp/Json/JsonObject.h" | ||
| #include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" | ||
| #include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" | ||
|
|
||
| namespace PokemonAutomation{ | ||
| namespace Pokemon{ | ||
|
|
||
|
|
||
| // The game's reader of an envelope's execution object: it parses the | ||
| // object into the game's own type and throws ParseException when it is | ||
| // malformed. | ||
| using ExecutionReader = std::function<void(const JsonObject& execution, std::string_view source)>; | ||
|
|
||
| // Require exactly these fields, including fields whose value is null/zero/false. | ||
| // Missing/unknown fields throw ParseException with the document source. | ||
| void validate_json_fields( | ||
| const JsonObject& object, std::initializer_list<std::string_view> fields, std::string_view source | ||
| ); | ||
|
|
||
| // The checkpoint of `game`'s strict authored envelope, its steps read | ||
| // against `actions` (read_story_checkpoint()); its execution object goes | ||
| // to `read_execution`. | ||
| StoryCheckpoint read_authored_checkpoint( | ||
| const JsonValue& value, std::string_view game, std::string_view source, | ||
| const ExecutionReader& read_execution, | ||
| std::span<const StoryActionSpec> actions = builtin_story_actions() | ||
| ); | ||
|
|
||
| // Encode the authored envelope; execution is supplied by the game adapter. | ||
| // This does not encode game bookkeeping or the legacy native-export envelope. | ||
| JsonValue authored_checkpoint_json(const StoryCheckpoint& checkpoint, std::string_view game, JsonObject execution); | ||
|
|
||
| // A JSON file built into the program: its name without ".json", and its | ||
| // text. | ||
| using EmbeddedJsonFile = std::pair<std::string, std::string>; | ||
|
|
||
| // Find embedded file `file` among `files` and parse it as | ||
| // read_authored_checkpoint() does. Missing resources throw | ||
| // InternalProgramError; malformed documents throw ParseException. | ||
| StoryCheckpoint load_authored_checkpoint( | ||
| std::span<const EmbeddedJsonFile> files, std::string_view file, std::string_view game, | ||
| const ExecutionReader& read_execution, | ||
| std::span<const StoryActionSpec> actions = builtin_story_actions() | ||
| ); | ||
|
|
||
|
|
||
|
|
||
| } // namespace Pokemon | ||
| } // namespace PokemonAutomation | ||
|
|
||
| #endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| /* Shared Authored Checkpoint JSON Tests | ||
| * | ||
| * From: https://github.com/PokemonAutomation/ | ||
| * | ||
| */ | ||
|
|
||
| #include <format> | ||
| #include <iostream> | ||
| #include <string> | ||
| #include <utility> | ||
| #include <vector> | ||
| #include "Common/Cpp/Exceptions.h" | ||
| #include "Pokemon_AuthoredCheckpointJson.h" | ||
| #include "Pokemon_StoryStepJson.h" | ||
| #ifndef PA_STORY_DATA_STANDALONE | ||
| #include "Pokemon_AuthoredCheckpointJson_Tests.h" | ||
| #endif | ||
|
|
||
| namespace PokemonAutomation{ | ||
| namespace Pokemon{ | ||
|
|
||
|
|
||
| namespace{ | ||
|
|
||
|
|
||
| // Documents intentionally contain no game headers or native bookkeeping. | ||
| const std::string FRLG = R"({"format":"pokesim-authored-checkpoint/1","game":"frlg","checkpoint":{"id":"00.05","title":"Parcel","events":[{"id":"first","kind":"talk"},{"id":"second","kind":"scene"}]},"execution":{"menu":"before_pokedex"}})"; | ||
| const std::string BDSP = R"({"format":"pokesim-authored-checkpoint/1","game":"bdsp","checkpoint":{"id":"01.03","title":"Mine","events":[{"id":"roark","kind":"talk"}]},"execution":{"on_failure":"reload_and_retry","saves_in":"Oreburgh City"}})"; | ||
|
|
||
| // How a case fails, if it does. | ||
| enum class Failure{ | ||
| NONE, | ||
| PARSE, | ||
| MISSING_RESOURCE, | ||
| }; | ||
|
|
||
| struct AuthoredInput{ | ||
| std::string name; | ||
| 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<std::string> events = {"first", "second"}; | ||
| Failure failure = Failure::NONE; | ||
| }; | ||
|
|
||
| struct AuthoredCase{ | ||
| AuthoredInput input; | ||
| AuthoredExpectation expectation; | ||
| }; | ||
|
|
||
| const AuthoredCase CASES[] = { | ||
| {.input = {.name = "FRLG envelope", .json = FRLG}, .expectation = {}}, | ||
| {.input = {.name = "BDSP embedded resource", .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 = { | ||
| .checkpoint = "x", .execution = R"({"zero":0,"false":false,"null":null})", .events = {}}}, | ||
| }; | ||
|
|
||
|
|
||
| // What a test reads: the checkpoint, and the execution object its reader | ||
| // kept. | ||
| struct Loaded{ | ||
| StoryCheckpoint checkpoint; | ||
| JsonObject execution; | ||
| }; | ||
|
|
||
| // A reader that keeps the execution object in `kept`. | ||
| ExecutionReader keep_execution(JsonObject& kept){ | ||
| return [&kept](const JsonObject& execution, std::string_view){ | ||
| kept = execution.clone(); | ||
| }; | ||
| } | ||
|
|
||
| // "" when `actual` is what `test` expects and survives a write and a | ||
| // read, else what was expected and what was read. | ||
| std::string check_loaded(const Loaded& actual, const AuthoredCase& test){ | ||
| if (test.expectation.failure != Failure::NONE){ | ||
| return "Expected rejection, read accepted authored checkpoint."; | ||
| } | ||
| if (actual.checkpoint.id != test.expectation.checkpoint){ | ||
| return std::format("Expected checkpoint {}, read {}", test.expectation.checkpoint, actual.checkpoint.id); | ||
| } | ||
| if (!(JsonValue(actual.execution.clone()) == parse_json(test.expectation.execution))){ | ||
| return std::format("Expected execution {}, read {}", test.expectation.execution, actual.execution.dump()); | ||
| } | ||
| std::vector<std::string> events; | ||
| for (const StoryStep& step : actual.checkpoint.steps){ | ||
| events.push_back(step.name); | ||
| } | ||
| if (events != test.expectation.events){ | ||
| return "Expected authored event IDs in their original order."; | ||
| } | ||
| const JsonValue encoded = authored_checkpoint_json(actual.checkpoint, test.input.game, actual.execution.clone()); | ||
| Loaded reread; | ||
| reread.checkpoint = read_authored_checkpoint(encoded, test.input.game, test.input.name, keep_execution(reread.execution)); | ||
| if (!(story_checkpoint_json(actual.checkpoint) == story_checkpoint_json(reread.checkpoint)) || actual.execution != reread.execution){ | ||
| return "Expected checkpoint and execution to survive the shared writer/reader round trip."; | ||
| } | ||
| return ""; | ||
| } | ||
|
|
||
| // "" when `test` reads or fails as expected. | ||
| std::string check_case(const AuthoredCase& test){ | ||
| try{ | ||
| const std::vector<std::pair<std::string, std::string>> files = {{"plan", test.input.json}}; | ||
| 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)); | ||
| return check_loaded(actual, test); | ||
| }catch (const ParseException& error){ | ||
| return test.expectation.failure == Failure::PARSE ? "" : std::format("Unexpected parse failure: {}", error.message()); | ||
| }catch (const InternalProgramError& error){ | ||
| return test.expectation.failure == Failure::MISSING_RESOURCE ? "" : std::format("Unexpected resource failure: {}", error.message()); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| #ifndef PA_STORY_DATA_STANDALONE | ||
|
|
||
| class AuthoredJsonTest : public UnitTest{ | ||
|
Check failure on line 138 in SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_AuthoredCheckpointJson_Tests.cpp
|
||
| public: | ||
| explicit AuthoredJsonTest(AuthoredCase test) | ||
| : UnitTest(std::format("Pokemon::AuthoredCheckpointJson - {}", test.input.name)) | ||
| , m_case(std::move(test)) | ||
| {} | ||
|
|
||
| virtual UnitTestResult run(Logger&, CancellableScope&) const override{ | ||
| const std::string error = check_case(m_case); | ||
| return error.empty() ? UnitTestResult(true) : UnitTestResult(error); | ||
| } | ||
|
|
||
| private: | ||
| AuthoredCase m_case; | ||
| }; | ||
|
|
||
| #endif | ||
|
|
||
|
|
||
| } // namespace | ||
|
|
||
|
|
||
| #ifndef PA_STORY_DATA_STANDALONE | ||
|
|
||
| void add_tests_AuthoredCheckpointJson(UnitTestDatabase& database){ | ||
| for (const AuthoredCase& test : CASES){ | ||
| database.add<AuthoredJsonTest>(test); | ||
| } | ||
| } | ||
|
|
||
| #endif | ||
|
|
||
|
|
||
|
|
||
| } // 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'; | ||
| failed += !error.empty(); | ||
| } | ||
| return failed != 0; | ||
| } | ||
|
|
||
| #endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /* Authored Checkpoint JSON Tests | ||
| * | ||
| * From: https://github.com/PokemonAutomation/ | ||
| * | ||
| */ | ||
|
|
||
| #ifndef PokemonAutomation_Pokemon_AuthoredCheckpointJson_Tests_H | ||
| #define PokemonAutomation_Pokemon_AuthoredCheckpointJson_Tests_H | ||
|
|
||
| #include "Common/Cpp/TestRunners/UnitTestDatabase.h" | ||
|
|
||
| namespace PokemonAutomation{ | ||
| namespace Pokemon{ | ||
|
|
||
|
|
||
| // Registers unit tests for the shared authored envelope, game identity and embedded-resource loading. | ||
| void add_tests_AuthoredCheckpointJson(UnitTestDatabase& database); | ||
|
|
||
|
|
||
|
|
||
| } // namespace Pokemon | ||
| } // namespace PokemonAutomation | ||
| #endif |
Uh oh!
There was an error while loading. Please reload this page.