Support playable Modern data worlds in the editor (by Codex)

This commit is contained in:
Codex committed 2026-10-25 08:23:02 +01:00
1 parent 3dbaea294a
commit 7de6f644f2
5 files changed
+228 -6

No files matched your search

+105 -5
View File
@@ -13,6 +13,14 @@
namespace bmh::editor {
namespace {
Json table(const std::string &name) { return {{"schema", name + "@1.1"}, {"items", Json::array()}}; }
bool modern_requirement(const Json &manifest) {
const auto requirements = manifest.value("requires", Json());
return requirements.is_array() &&
std::find(requirements.begin(), requirements.end(), Json("runtime:modern-v1")) != requirements.end();
}
bool whole(const Json &value, int64_t minimum, int64_t maximum) {
return value.is_number_integer() && value >= minimum && value <= maximum;
}
bool date_valid(const Json &value) {
if (value.is_null())
return true;
@@ -128,8 +136,10 @@ World World::open(const fs::path &source, const std::string &start_date) {
if (!version.starts_with("1."))
throw Error("unsupported_version", {{"version", version}});
const bool editor = str(w.manifest.value("x-editor", Json::object()), "profile") == "editor-world-v1";
const auto editor_fields = w.manifest.value("x-editor", Json::object());
const auto playable = editor_fields.is_object() ? editor_fields.value("playable", Json()) : Json();
if (editor && (version != "1.1" || str(w.manifest, "min_reader") != "1.1" ||
w.manifest.value("x-editor", Json::object()).value("playable", true)))
(playable != false && !(playable == true && modern_requirement(w.manifest)))))
throw Error("unsupported_version");
if (!editor) {
// Only bounded 1.0 Classic packages use the Classic validator. Its
@@ -252,7 +262,7 @@ Json World::statistics() const {
{"source", utf8(source_path)},
{"imported", imported},
{"profile", "editor-world-v1"},
{"playable", false}};
{"playable", manifest.value("x-editor", Json::object()).value("playable", false)}};
}
Json World::query(const Json &args) const {
const auto name = str(args, "table", "clubs");
@@ -347,11 +357,15 @@ Json World::validate() const {
std::map<std::string, std::string> ids;
const auto editor = manifest.value("x-editor", Json::object());
const auto requirements = manifest.value("requires", Json::array());
const bool modern = modern_requirement(manifest);
const auto playable = editor.is_object() ? editor.value("playable", Json()) : Json();
if (str(manifest, "format") != "openbmh-world" || str(manifest, "kind") != "world" ||
str(manifest, "format_version") != "1.1" || str(manifest, "min_reader") != "1.1" || !editor.is_object() ||
str(editor, "profile") != "editor-world-v1" || !editor.contains("playable") || editor["playable"] != false ||
str(editor, "profile") != "editor-world-v1" ||
(playable != false && !(playable == true && modern)) ||
!requirements.is_array() ||
std::find(requirements.begin(), requirements.end(), Json("editor:world-v1")) == requirements.end())
std::find(requirements.begin(), requirements.end(), Json("editor:world-v1")) == requirements.end() ||
(modern && std::find(requirements.begin(), requirements.end(), Json("runtime:classic-v1")) != requirements.end()))
issue("error", "invalid_manifest", "manifest", "manifest");
if (!date_valid(manifest.value("start_date", Json())))
issue("error", "invalid_date", "manifest", "manifest");
@@ -373,7 +387,8 @@ Json World::validate() const {
if (name != "assets" && name != "calendar" && name != "rules" && title(row).empty())
issue("warning", "missing_name", name, id);
if (name == "players") {
if (!date_valid(row.value("born", Json())))
if (!date_valid(row.value("born", Json())) ||
(modern && row.contains("born") && row["born"].is_null()))
issue("error", "invalid_date", name, id);
if (!row.contains("born") && !row.contains("age_at_start"))
issue("warning", "missing_birthday", name, id);
@@ -402,6 +417,91 @@ Json World::validate() const {
}
}
}
// F4/F9 checks apply only to explicitly Modern worlds. Imported editor-only
// worlds retain their original Classic camp catalogue and validation rules.
if (modern) {
for (const auto &rule : rows("rules")) {
if (str(rule, "type") != "training_camps")
continue;
const auto id = str(rule, "id");
auto camp_issue = [&](const std::string &field, const std::string &entry = "") {
issue("error", "invalid_training_camps", "rules", id, {{"field", field}, {"entry", entry}});
};
const auto map_rows = rule.value("maps", Json());
const auto resorts = rule.value("resorts", Json());
if (!map_rows.is_array() || map_rows.empty() || !resorts.is_array() || resorts.empty()) {
camp_issue("maps/resorts");
continue;
}
std::map<std::string, std::pair<Json, Json>> maps;
std::set<std::string> nested_ids;
for (const auto &map : map_rows) {
if (!map.is_object()) {
camp_issue("maps");
continue;
}
const auto key = str(map, "id");
if (key.empty() || !nested_ids.insert(key).second || ids.contains(key))
camp_issue("maps.id", key);
if (text(map.value("name", Json())).empty())
camp_issue("maps.name", key);
const auto width = map.value("width", Json()), height = map.value("height", Json());
if ((!width.is_null() || !height.is_null()) &&
(!whole(width, 1, INT32_MAX) || !whole(height, 1, INT32_MAX)))
camp_issue("maps.size", key);
maps[key] = {width, height};
}
for (const auto &camp : resorts) {
if (!camp.is_object()) {
camp_issue("resorts");
continue;
}
const auto key = str(camp, "id"), map = str(camp, "map");
if (key.empty() || !nested_ids.insert(key).second || ids.contains(key))
camp_issue("resorts.id", key);
if (text(camp.value("name", Json())).empty() || text(camp.value("place", Json())).empty())
camp_issue("resorts.name/place", key);
if (!find("nations", str(camp, "country")))
camp_issue("resorts.country", key);
if (!whole(camp.value("base_price", Json()), 0, INT32_MAX))
camp_issue("resorts.base_price", key);
const auto quality = camp.value("quality", Json());
if (!quality.is_array() || quality.size() != 4 ||
!std::all_of(quality.begin(), quality.end(), [](const auto &value) { return whole(value, 0, 6); }))
camp_issue("resorts.quality", key);
const auto x = camp.value("x", Json()), y = camp.value("y", Json());
if (!camp.contains("x") || !camp.contains("y") ||
(!x.is_null() && !whole(x, 0, INT32_MAX)) || (!y.is_null() && !whole(y, 0, INT32_MAX)))
camp_issue("resorts.coordinates", key);
const auto found = maps.find(map);
if (found == maps.end())
camp_issue("resorts.map", key);
else if (!found->second.first.is_null() && !found->second.second.is_null() &&
(!whole(x, 0, INT32_MAX) || !whole(y, 0, INT32_MAX) ||
x >= found->second.first || y >= found->second.second))
camp_issue("resorts.bounds", key);
const auto image = camp.value("image", Json());
if (!image.is_null() && (!image.is_string() || image.get_ref<const std::string &>().empty() ||
asset_file(image.get_ref<const std::string &>()).empty()))
camp_issue("resorts.image", key);
}
}
size_t referees = 0;
for (const auto &person : rows("staff")) {
if (str(person, "role") != "referee")
continue;
++referees;
const auto attrs = person.value("attributes", Json::object());
if (text(person.value("name", Json())).empty() || !attrs.is_object())
issue("error", "invalid_referee", "staff", str(person, "id"));
else
for (const auto *field : {"strictness", "perception"})
if (attrs.contains(field) && !whole(attrs[field], 0, 3))
issue("error", "invalid_referee", "staff", str(person, "id"), {{"field", field}});
}
if (referees > 255)
issue("error", "invalid_referee", "staff", "", {{"count", referees}, {"maximum", 255}});
}
for (auto &name : tables())
if (name != "review" && name != "image-edits")
for (auto &row : rows(name))