Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* 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;
}

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,73 @@
/* Shared Authored Checkpoint JSON
*
* From: https://github.com/PokemonAutomation/
*
* Reading 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:
* nothing here binds or runs a step, and nothing read keeps JSON.
*
* The envelope's "format" is "pokesim-authored-checkpoint/1": the
* files are written by PokemonSimulation's story tools, which plan
* and check a checkpoint's steps offline; a file written by hand uses the
* same name.
*
*/

#ifndef PokemonAutomation_Pokemon_AuthoredCheckpointJson_H
#define PokemonAutomation_Pokemon_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()
);

// 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,155 @@
/* Shared Authored Checkpoint JSON Tests
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <format>
#include <string>
#include <utility>
#include <vector>
#include "Common/Cpp/Exceptions.h"
#include "Pokemon_AuthoredCheckpointJson.h"
#include "Pokemon_AuthoredCheckpointJson_Tests.h"

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 AuthoredCase{
std::string name;
struct Input{
std::string json;
std::string game = "frlg";
bool resource = false;
std::string file = "plan";
};

struct Expectation{
std::string checkpoint = "00.05";
std::string execution = R"({"menu":"before_pokedex"})";
std::vector<std::string> events = {"first", "second"};
Failure failure = Failure::NONE;
};

Input input;
Expectation expectation;
};

const AuthoredCase CASES[] = {
{.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"}}},
{.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 = {}}},
};


// 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, 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.";
}
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.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());
}
}


class AuthoredJsonTest : public UnitTest{
public:
explicit AuthoredJsonTest(AuthoredCase test)
: UnitTest(std::format("Pokemon::AuthoredCheckpointJson - {}", test.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;
};


} // namespace


void add_tests_AuthoredCheckpointJson(UnitTestDatabase& database){
for (const AuthoredCase& test : CASES){
database.add<AuthoredJsonTest>(test);
}
}



} // namespace Pokemon
} // namespace PokemonAutomation
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* Story Dev Files
*
* From: https://github.com/PokemonAutomation/
*
* Checkpoint files that a run reads in place of the built-in ones of the
* same name (load_story_data() in Pokemon_StoryFiles.h). The program
* passes none; the tests pass their own.
*
*/

#ifndef PokemonAutomation_Pokemon_StoryDevFiles_H
#define PokemonAutomation_Pokemon_StoryDevFiles_H

#include <string>

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;
};



} // namespace Pokemon
} // namespace PokemonAutomation
#endif
Loading
Loading