Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* 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;
}




} // namespace Pokemon
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* 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/Types/Pokemon_StoryActionSpec.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
);

// A JSON file built into the program: its name without ".json", and its
// text.
using EmbeddedJsonFile = std::pair<std::string, std::string>;



} // namespace Pokemon
} // namespace PokemonAutomation

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/* 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,
};

struct AuthoredCase{
std::string name;
struct Input{
std::string json;
std::string game = "frlg";
};

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 envelope", .input = {.json = BDSP, .game = "bdsp"}, .expectation = {
.checkpoint = "01.03", .execution = R"({"on_failure":"reload_and_retry","saves_in":"Oreburgh City"})", .events = {"roark"}}},
{.name = "wrong game", .input = {.json = BDSP}, .expectation = {.failure = Failure::PARSE}},
{.name = "empty expected game", .input = {.json = FRLG, .game = ""}, .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 = {}}},
};


// The actions the documents name: none, so each step is its kind's default.
std::span<const StoryActionSpec> story_actions(){
return {};
}

// 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{
Loaded actual;
actual.checkpoint = read_authored_checkpoint(
parse_json(test.input.json), test.input.game, test.name, keep_execution(actual.execution), story_actions()
);
return check_loaded(actual, test);
}catch (const ParseException& error){
return test.expectation.failure == Failure::PARSE ? "" : std::format("Unexpected parse 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,51 @@
/* Story Files
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <format>
#include "Common/Cpp/Exceptions.h"
#include "Pokemon_StoryFiles.h"

namespace PokemonAutomation{
namespace Pokemon{
namespace detail{


void add_story_row(
std::string_view id, const StoryRowExecution& execution, RowSave start,
std::optional<std::string_view> 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));
}
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
));
}
rows.reload_when_chained.emplace(id);
}
if (execution.reload == StoryReload::ALWAYS){
rows.reload_always.emplace(id);
}
switch (execution.save){
case StoryRowSave::NONE:
break;
case StoryRowSave::AUTOSAVE:
rows.autosaves.emplace(id);
break;
case StoryRowSave::UNVERIFIED:
rows.unverified_autosaves.emplace(id);
break;
}
}



} // namespace detail
} // namespace Pokemon
} // namespace PokemonAutomation
Loading
Loading