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
15 changes: 15 additions & 0 deletions SerialPrograms/Source/CommonFramework/Panels/ProgramDescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ struct PrecheckSummary{
bool skippable = false;
};

// How one precheck ended in a checks-only run.
enum class PrecheckReportStatus{
PASSED,
FAILED,
SKIPPED,
};

// What one precheck found in a checks-only run. On FAILED, the message
// tells the user what to fix.
struct PrecheckReport{
std::string name = {};
PrecheckReportStatus status = PrecheckReportStatus::PASSED;
std::string message = {};
};



class ProgramDescriptor : public PanelDescriptor{
Expand Down
117 changes: 117 additions & 0 deletions SerialPrograms/Source/CommonFramework/ProgramPrecheckRun.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/* Program Precheck Run
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <format>
#include <string_view>
#include "Common/Cpp/Json/JsonObject.h"
#include "ProgramPrecheckRun.h"

namespace PokemonAutomation{

namespace{


std::string key_path(std::string_view prefix, std::string_view key){
return prefix.empty() ? std::string(key) : std::format("{}.{}", prefix, key);
}

// Puts the values of `candidate` into `target`, an object, recursing into
// groups. `prefix` is the key path of `target`.
std::optional<RefusedOption> merge_into(const JsonObject& candidate, std::string_view prefix, JsonObject& target){
for (const auto& item : candidate){
const std::string path = key_path(prefix, item.first);
JsonValue* slot = target.get_value(item.first);
if (slot == nullptr){
return RefusedOption{path, std::format("The program has no option {}.", path)};
}
JsonObject* group = slot->to_object();
const JsonObject* values = item.second.to_object();
if (group != nullptr && values != nullptr){
std::optional<RefusedOption> refused = merge_into(*values, path, *group);
if (refused){
return refused;
}
continue;
}
if (group != nullptr || values != nullptr){
return RefusedOption{path, std::format("Option {} is a group: its value is an object of its own options.", path)};
}
*slot = item.second.clone();
}
return std::nullopt;
}

bool same_value(const JsonValue& x, const JsonValue& y){
if (x.is_float() && y.is_float()){
return x.to_double_default() == y.to_double_default();
}
return x == y;
}

std::optional<RefusedOption> find_refused_in(const JsonObject& candidate, const JsonObject& loaded, std::string_view prefix){
for (const auto& item : candidate){
const std::string path = key_path(prefix, item.first);
const JsonValue* now = loaded.get_value(item.first);
if (now == nullptr){
return RefusedOption{path, std::format("The program has no option {}.", path)};
}
const JsonObject* values = item.second.to_object();
const JsonObject* group = now->to_object();
if (values != nullptr && group != nullptr){
std::optional<RefusedOption> refused = find_refused_in(*values, *group, path);
if (refused){
return refused;
}
continue;
}
if (!same_value(item.second, *now)){
return RefusedOption{
path,
std::format("The program holds {} for {}, not {}.", now->dump(-1), path, item.second.dump(-1))
};
}
}
return std::nullopt;
}


} // namespace



std::expected<JsonValue, RefusedOption> merge_candidate_options(
const JsonValue& current, const JsonValue& candidate
){
const JsonObject* values = candidate.to_object();
if (values == nullptr){
return std::unexpected(RefusedOption{"", "The options to check must be a JSON object."});
}
JsonValue merged = current.clone();
JsonObject* target = merged.to_object();
if (target == nullptr){
return std::unexpected(RefusedOption{"", "The program's options are not a JSON object."});
}
std::optional<RefusedOption> refused = merge_into(*values, "", *target);
if (refused){
return std::unexpected(std::move(*refused));
}
return merged;
}

std::optional<RefusedOption> find_refused_option(
const JsonValue& candidate, const JsonValue& loaded
){
const JsonObject* values = candidate.to_object();
const JsonObject* now = loaded.to_object();
if (values == nullptr || now == nullptr){
return RefusedOption{"", "The options to check must be a JSON object."};
}
return find_refused_in(*values, *now, "");
}



} // namespace PokemonAutomation
65 changes: 65 additions & 0 deletions SerialPrograms/Source/CommonFramework/ProgramPrecheckRun.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* Program Precheck Run
*
* From: https://github.com/PokemonAutomation/
*
* A checks-only run of a program (ProgramSession::run_prechecks_only()):
* candidate options are loaded over the current ones, the program's
* prechecks run without starting it, and the prior options are loaded back.
* The lab's "preflight" command uses it to find a setup mistake before a run
* resets or saves anything.
*
*/

#ifndef PokemonAutomation_CommonFramework_ProgramPrecheckRun_H
#define PokemonAutomation_CommonFramework_ProgramPrecheckRun_H

#include <expected>
#include <optional>
#include <string>
#include <vector>
#include "Common/Cpp/Json/JsonValue.h"
#include "CommonFramework/Panels/ProgramDescriptor.h"

namespace PokemonAutomation{


// A candidate option the program refused, and why. The key is a dotted key
// path ("NOTIFICATIONS.Enabled"); it is empty when the program refused the
// combination of options (check_validity()), which names no key.
struct RefusedOption{
std::string key = {};
std::string message = {};
};

// What a checks-only run found.
struct PrecheckOnlyRun{
// Set when the program refused the candidate options; no check ran.
std::optional<RefusedOption> refused = {};
// The checks in the order they ran, up to the first FAILED one. Nothing
// when the program declares no precheck list, or refused the options.
std::optional<std::vector<PrecheckReport>> checks = {};
// Whether the program holds the prior options again afterwards.
bool restored = false;
};


// `current` (a program's option JSON) with the values of `candidate` put in.
// A candidate is an object of option keys; a group's value is an object of
// the group's own keys, of which only those given change. Refuses a key that
// `current` does not have, or a candidate that is not an object.
std::expected<JsonValue, RefusedOption> merge_candidate_options(
const JsonValue& current, const JsonValue& candidate
);

// The first option of `candidate` whose value `loaded` (the options read
// back after loading the merged candidate) does not hold: a number clamped
// to its bounds, a disabled or unknown choice ignored. Numbers compare by
// value, so 2 and 2.0 are the same.
std::optional<RefusedOption> find_refused_option(
const JsonValue& candidate, const JsonValue& loaded
);



} // namespace PokemonAutomation
#endif
Loading
Loading