Resolve and fingerprint versioned game rule packages

This commit is contained in:
faumaray committed 2026-12-02 21:20:21 +03:00
1 parent 8d2870a6f8
commit de38aff2ff
6 files changed
+783 -4

No files matched your search

@@ -0,0 +1,103 @@
#pragma once
#include <array>
#include <cstddef>
#include <expected>
#include <memory>
#include <span>
#include <string>
#include <string_view>
#include <vector>
namespace settlement {
using RulesDigest = std::array<std::byte, 32>;
struct RulesDiagnostic {
std::string package;
std::string source;
std::string field;
std::string message;
std::size_t byte_offset = 0;
};
struct RulesLimits {
std::size_t package_bytes = 256 * 1024;
std::size_t catalog_bytes = 4 * 1024 * 1024;
std::size_t packages = 128;
std::size_t definitions = 1024;
std::size_t dependencies = 64;
std::size_t references = 64;
std::size_t json_depth = 24;
std::size_t json_nodes = 16384;
};
struct RuleDefinition {
std::string id;
std::string kind;
std::string canonical_data;
std::vector<std::string> references;
};
struct FrozenRulePackage {
std::string id;
std::string exact_version;
RulesDigest content_sha256{};
bool operator==(const FrozenRulePackage&) const = default;
};
class RulesManifest;
struct RulePackageData;
// Schema 1 requires schema/id/version/dependencies/exclusive_capabilities/definitions.
// Package IDs use lower-case dot-separated segments. Definition IDs and capabilities
// use package:local-name. Each definition has id/kind/data/references, where data is
// integer-valued strict JSON. Unknown metadata fields are errors. Set-like arrays are
// sorted; arrays inside data retain order. Sources are diagnostic labels, never paths
// to execute or open. Only one installed version of each package ID is allowed.
class RulePackage {
public:
[[nodiscard]] const FrozenRulePackage& identity() const noexcept;
[[nodiscard]] std::span<const RuleDefinition> definitions() const noexcept;
[[nodiscard]] std::string_view canonical_json() const noexcept;
private:
explicit RulePackage(std::shared_ptr<const RulePackageData> data);
std::shared_ptr<const RulePackageData> data_;
friend std::expected<RulePackage, RulesDiagnostic> load_rule_package(
std::string_view, std::string_view, const RulesLimits&);
friend std::expected<RulesManifest, RulesDiagnostic> resolve_rules(
std::span<const RulePackage>, std::span<const std::string>, const RulesLimits&);
};
class RulesManifest {
public:
[[nodiscard]] std::span<const FrozenRulePackage> packages() const noexcept { return packages_; }
[[nodiscard]] const RulesDigest& fingerprint() const noexcept { return fingerprint_; }
// Schema-tagged canonical UTF-8 bytes shared by persistence and handshake callers.
[[nodiscard]] std::string_view canonical_json() const noexcept { return canonical_json_; }
bool operator==(const RulesManifest&) const = default;
private:
RulesManifest(std::vector<FrozenRulePackage>, RulesDigest, std::string);
std::vector<FrozenRulePackage> packages_;
RulesDigest fingerprint_{};
std::string canonical_json_;
friend std::expected<RulesManifest, RulesDiagnostic> resolve_rules(
std::span<const RulePackage>, std::span<const std::string>, const RulesLimits&);
};
[[nodiscard]] std::expected<RulePackage, RulesDiagnostic> load_rule_package(
std::string_view source, std::string_view json, const RulesLimits& limits = {});
// Resolves selected IDs and their transitive dependencies. Prerelease dependencies
// require an exact constraint equal to that full version, optionally prefixed by '='.
// Content digests are computed from the parsed content and never trusted from input.
[[nodiscard]] std::expected<RulesManifest, RulesDiagnostic> resolve_rules(
std::span<const RulePackage> available, std::span<const std::string> selected,
const RulesLimits& limits = {});
[[nodiscard]] std::expected<RulesManifest, RulesDiagnostic> default_rules_manifest();
[[nodiscard]] std::string rules_digest_hex(const RulesDigest& digest);
} // namespace settlement