#include "evo/server/admin/region_switch.hpp" #include #include #include #include namespace evo::server::admin { namespace { bool any_value(std::string_view /*value*/) { return true; } bool hex32(std::string_view value) { return value.size() == 32 && std::ranges::all_of(value, [](char c) { return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'); }); } // Every value following `prefix`, up to (not including) the first character in `stops`, that // `wanted` accepts (a comment can mention a flag without a value). std::vector> values_after(std::string_view text, std::string_view prefix, std::string_view stops, bool (*wanted)(std::string_view) = any_value) { std::vector> found; for (auto at = text.find(prefix); at != std::string_view::npos; at = text.find(prefix, at + 1)) { const auto first = at + prefix.size(); const auto end = std::min(text.find_first_of(stops, first), text.size()); if (wanted(text.substr(first, end - first))) found.emplace_back(first, text.substr(first, end - first)); } return found; } std::optional number(std::string_view text) { std::uint64_t value{}; if (text.empty() || std::from_chars(text.data(), text.data() + text.size(), value).ec != std::errc{}) return std::nullopt; return value; } // The one value after `prefix` (all of `count` occurrences equal), or nullopt. std::optional single(std::string_view text, std::string_view prefix, std::string_view stops, std::size_t count = 1, bool (*wanted)(std::string_view) = any_value) { const auto found = values_after(text, prefix, stops, wanted); if (found.size() != count || !std::ranges::all_of(found, [&](const auto& v) { return v.second == found[0].second; })) return std::nullopt; return found[0].second; } // `text` with every value after `prefix` replaced by `value` (back to front, so positions hold). std::string replace_after(std::string text, std::string_view prefix, std::string_view stops, std::string_view value, bool (*wanted)(std::string_view) = any_value) { auto found = values_after(text, prefix, stops, wanted); std::ranges::reverse(found); for (const auto& [at, old] : found) text.replace(at, old.size(), value); return text; } constexpr std::string_view world_key = "{name: REGION_WORLD, value: \""; constexpr std::string_view region_key = "{name: REGION_ID, value: \""; constexpr std::string_view routes_key = "{name: REGION_LIFE_ROUTES, value: \""; constexpr std::string_view commit_key = "--commit "; constexpr std::string_view wildlife_key = "--playtest-wildlife-first-id "; constexpr std::string_view header_key = "# Region host for evolution-dev: world "; constexpr std::string_view broker_region_key = "{name: BROKER_DEFAULT_REGION, value: \""; constexpr std::string_view broker_routes_key = "{name: BROKER_ROUTES, value: \""; constexpr std::string_view portal_regions_key = "{name: ADMIN_REGIONS, value: \""; constexpr std::string_view opening_key = " - {name: REGION_OPENING, value: "; constexpr std::string_view note_prefix = " # Admin portal (ADR-0182 section 15): "; } // namespace std::expected read_region_layout(const RegionSwitchFiles& files) { const auto& [values, kustomization, broker, portal] = files; RegionLayout layout; // The Deployment and the provision Job each name the world and region. const auto world = single(values, world_key, "\"", 2); const auto region = single(values, region_key, "\"", 2); const auto world_id = world ? number(*world) : std::nullopt; const auto region_id = region ? number(*region) : std::nullopt; if (!world_id) return std::unexpected{"REGION_WORLD"}; if (!region_id) return std::unexpected{"REGION_ID"}; layout.world = *world_id; layout.region = *region_id; // Canonical numbers only, so every other file can be matched by the same text. const auto id = std::to_string(layout.region); if (*region != id || *world != std::to_string(layout.world)) return std::unexpected{"REGION_ID"}; const auto routes = single(values, routes_key, "="); if (!routes || *routes != id) return std::unexpected{"REGION_LIFE_ROUTES"}; const auto commit = single(values, commit_key, " \n", 1, hex32); if (!commit) return std::unexpected{"--commit"}; layout.commit = std::string(*commit); const auto wildlife = single(values, wildlife_key, " \n"); const auto wildlife_first = wildlife ? number(*wildlife) : std::nullopt; if (!wildlife_first) return std::unexpected{"--playtest-wildlife-first-id"}; layout.wildlife_first = *wildlife_first; if (values.find(opening_key) == std::string::npos || values.find(opening_key) != values.rfind(opening_key)) return std::unexpected{"REGION_OPENING"}; const auto header = single(kustomization, header_key, "\n"); if (!header || !header->starts_with(std::to_string(layout.world) + ", region " + id + ",")) return std::unexpected{"kustomization header"}; const auto broker_region = single(broker, broker_region_key, "\""); if (!broker_region || *broker_region != id) return std::unexpected{"BROKER_DEFAULT_REGION"}; const auto broker_routes = single(broker, broker_routes_key, "="); if (!broker_routes || *broker_routes != id) return std::unexpected{"BROKER_ROUTES"}; const auto portal_regions = single(portal, portal_regions_key, "="); if (!portal_regions || *portal_regions != id) return std::unexpected{"ADMIN_REGIONS"}; return layout; } std::expected switch_region(const RegionSwitchFiles& files, const RegionLayout& to, std::string_view note) { const auto from = read_region_layout(files); if (!from) return std::unexpected{from.error()}; if (to.world != from->world || to.region == 0 || to.region == from->region || to.wildlife_first == 0 || !hex32(to.commit)) return std::unexpected{"target"}; if (note.find_first_of("\r\n") != std::string_view::npos) return std::unexpected{"note"}; auto [values, kustomization, broker, portal] = files; const auto region = std::to_string(to.region); values = replace_after(std::move(values), region_key, "\"", region); values = replace_after(std::move(values), routes_key, "=", region); values = replace_after(std::move(values), commit_key, " \n", to.commit, hex32); values = replace_after(std::move(values), wildlife_key, " \n", std::to_string(to.wildlife_first)); // One note above the opening, replacing the previous portal note. const auto line = std::string(note_prefix) + std::string(note) + "\n"; if (const auto old = values.find(note_prefix); old != std::string::npos) values.erase(old, values.find('\n', old) - old + 1); values.insert(values.find(opening_key), line); const auto header_at = kustomization.find(header_key) + header_key.size(); const auto old_region = ", region " + std::to_string(from->region) + ","; kustomization.replace(kustomization.find(old_region, header_at), old_region.size(), ", region " + region + ","); broker = replace_after(std::move(broker), broker_region_key, "\"", region); broker = replace_after(std::move(broker), broker_routes_key, "=", region); portal = replace_after(std::move(portal), portal_regions_key, "=", region); RegionSwitchFiles result{std::move(values), std::move(kustomization), std::move(broker), std::move(portal)}; const auto check = read_region_layout(result); if (!check || *check != to) return std::unexpected{"result"}; return result; } } // namespace evo::server::admin