载入中...
搜索中...
未找到
TerrainBiomePresetCodec.cpp
浏览该文件的文档.
2
3#include <limits>
4
5#include "common/Value.h"
6
7namespace eve::procgen {
8namespace {
9constexpr std::size_t kMaximumPresetJsonBytes = 256U * 1024U * 1024U;
10constexpr std::size_t kMaximumSpawnerEntries = 4096;
11} // namespace
12
14 if (entries_.size() > kMaximumSpawnerEntries)
16 DiagnosticCode::InvalidArgument, "terrain.biomePreset.snapshot: too many spawner entries"));
18 entries.reserve(entries_.size());
19 for (const auto& entry : entries_) {
20 if (entry.entryId.empty() || entry.entryId.size() > std::numeric_limits<std::uint32_t>::max())
22 Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.biomePreset.snapshot: invalid entry ID"));
23 auto plan = entry.plan.snapshotJson();
24 if (!plan.ok()) return Result<std::string>::failure(plan.status());
25 entries.emplace_back(Value::Object{{"activeInBiome", entry.activeInBiome},
26 {"activeInStamper", entry.activeInStamper},
27 {"autoAssignResources", entry.autoAssignResources},
28 {"entryId", entry.entryId},
29 {"plan", std::move(plan.value())}});
30 }
31 auto encoded = Value(Value::Object{{"entries", std::move(entries)},
32 {"schema", "eve.procgen.terrain-biome-preset"},
33 {"version", 1}})
34 .toJson();
35 if (!encoded.ok()) return encoded;
36 if (encoded.value().size() > kMaximumPresetJsonBytes)
38 "terrain.biomePreset.snapshot: JSON exceeds size limit"));
39 return encoded;
40}
41
43 if (json.size() > kMaximumPresetJsonBytes)
45 Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.biomePreset.restore: JSON exceeds size limit"));
46 auto decoded = Value::fromJson(json);
47 if (!decoded.ok()) return Result<void>::failure(decoded.status());
48 const auto* root = decoded.value().getIf<Value::Object>();
49 if (!root || root->size() != 3 || !root->contains("schema") || !root->contains("version") ||
50 !root->contains("entries"))
52 "terrain.biomePreset.restore: missing or unknown root fields"));
53 const auto* schema = root->at("schema").getIf<std::string>();
54 const auto* version = root->at("version").getIf<std::int64_t>();
55 const auto* entries = root->at("entries").getIf<Value::Array>();
56 if (!schema || *schema != "eve.procgen.terrain-biome-preset" || !version ||
57 (*version != 0 && *version != 1) || !entries || entries->size() > kMaximumSpawnerEntries)
60 "terrain.biomePreset.restore: unsupported schema, version or entry count"));
61
62 TerrainBiomePreset candidate;
63 for (const auto& encodedEntry : *entries) {
64 const auto* entry = encodedEntry.getIf<Value::Object>();
65 const std::size_t expectedFields = *version == 0 ? 4 : 5;
66 if (!entry || entry->size() != expectedFields || !entry->contains("entryId") ||
67 !entry->contains("activeInBiome") || !entry->contains("activeInStamper") ||
68 !entry->contains("plan") || (*version >= 1 && !entry->contains("autoAssignResources")))
70 DiagnosticCode::InvalidArgument, "terrain.biomePreset.restore: missing or unknown entry fields"));
71 const auto* entryId = entry->at("entryId").getIf<std::string>();
72 const auto* activeInBiome = entry->at("activeInBiome").getIf<bool>();
73 const auto* activeInStamper = entry->at("activeInStamper").getIf<bool>();
74 const auto* planJson = entry->at("plan").getIf<std::string>();
75 const bool* autoAssign = nullptr;
76 if (*version >= 1) autoAssign = entry->at("autoAssignResources").getIf<bool>();
77 if (!entryId || entryId->empty() || !activeInBiome || !activeInStamper || !planJson ||
78 (*version >= 1 && !autoAssign))
80 "terrain.biomePreset.restore: invalid entry values"));
82 auto restored = plan.restoreJson(*planJson);
83 if (!restored.ok()) return Result<void>::failure(restored.status());
84 auto added = candidate.addSpawner(*entryId, plan, *activeInBiome, *activeInStamper,
85 *version == 0 ? true : *autoAssign);
86 if (!added.ok()) return Result<void>::failure(added.status());
87 }
88 entries_.swap(candidate.entries_);
89 return Result<void>::success();
90}
91
92} // namespace eve::procgen
int root
Definition AnimSmr.cpp:119
std::unordered_map< std::string, QuestRuntime > entries
Owning, renderer-independent dynamic values.
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={}, DiagnosticDetails details={}, std::string source={})
Construct an error diagnostic with the standard error severity.
Definition Diagnostic.h:125
Move-only operation result carrying either a value or Status.
Definition Result.h:155
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:164
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:175
static Result< Value > fromJson(std::string_view json)
Parse one strict JSON value into an owning Value.
Definition Value.cpp:57
std::map< std::string, Value > Object
Definition Value.h:34
std::vector< Value > Array
Definition Value.h:33
Own an ordered Pcg-style biome preset made from independent spawner plans. Entries and plans are copi...
Result< int > addSpawner(const std::string &entryId, const TerrainSpawnPlan &plan, bool activeInBiome, bool activeInStamper, bool autoAssignResources)
Append an owned nonempty spawner entry with a stable unique entry ID.
Result< void > restoreJson(const std::string &json)
Atomically restore version 1 or migrate version 0 (auto-assign defaults true).
Result< std::string > snapshotJson() const
Serialize schema version 1 as deterministic strict JSON with owned nested spawn plans.
Own an ordered mixed-domain Pcg-style spawn recipe. Rule IDs are stable and unique....
Result< void > restoreJson(const std::string &json)
Atomically restore versions 0-3; version 0 migrates all rules to enabled.