载入中...
搜索中...
未找到
RPGSaveSession.cpp
浏览该文件的文档.
2#include "rpg/Party.h"
3
4#include "common/Assert.h"
5#include "common/Snapshot.h"
6#include "common/Value.h"
8#include "rpg/GameState.h"
9#include "rpg/Quest.h"
10#include "rpg/RPGActor.h"
11#include "rpg/Tracker.h"
12
13#include <algorithm>
14#include <optional>
15#include <cstdint>
16#include <unordered_map>
17
18namespace eve::rpg {
19
20namespace {
21
22bool validContentVersion(std::string_view contentVersion) {
23 return !contentVersion.empty() && contentVersion.size() <= 256 &&
24 std::none_of(contentVersion.begin(), contentVersion.end(), [](unsigned char value) {
25 return value < 0x20u || value == 0x7fu;
26 });
27}
28
29eve::LogicalId saveSchema() {
30 const auto schema = eve::LogicalId::parse("rpg:save-session");
31 return schema ? *schema : eve::LogicalId{};
32}
33
35eve::SnapshotHashProvider saveIntegrityProvider() {
36 return [](std::string_view input) -> eve::Result<eve::ContentId> {
37 std::uint64_t left = 14695981039346656037ull;
38 std::uint64_t right = 1099511628211ull;
39 for (const unsigned char byte : input) {
40 left ^= byte;
41 left *= 1099511628211ull;
42 right ^= static_cast<std::uint64_t>(byte) + 0x9e3779b97f4a7c15ull;
43 right *= 14029467366897019727ull;
44 }
46 for (int index = 0; index < 8; ++index) {
47 bytes[static_cast<std::size_t>(index)] = static_cast<std::uint8_t>(left >> (56 - index * 8));
48 bytes[static_cast<std::size_t>(index + 8)] = static_cast<std::uint8_t>(right >> (56 - index * 8));
49 }
51 };
52}
53
54std::size_t renameStringValue(eve::Value *value, std::string_view oldId, std::string_view newId) {
55 if (!value || !value->isString() || value->asString() != oldId) return 0;
56 *value = eve::Value(std::string(newId));
57 return 1;
58}
59
60template <typename Visitor>
61std::size_t visitArray(eve::Value *value, Visitor visitor) {
62 if (!value || !value->isArray()) return 0;
63 std::size_t changed = 0;
64 for (std::size_t index = 0; index < value->arraySize(); ++index) changed += visitor(value->at(index));
65 return changed;
66}
67
68std::size_t applyIdRename(eve::Value &payload, RPGSaveIdDomain domain, std::string_view oldId,
69 std::string_view newId) {
70 eve::Value *actor = payload.find("actor");
71 eve::Value *tracker = payload.find("questTracker");
72 eve::Value *inventory = payload.find("inventory");
73 switch (domain) {
75 std::size_t changed = 0;
76 eve::Value *bag = inventory ? inventory->find("bag") : nullptr;
77 changed += visitArray(bag ? bag->find("slots") : nullptr, [&](eve::Value &stack) {
78 return renameStringValue(stack.find("itemId"), oldId, newId);
79 });
80 eve::Value *equipment = inventory ? inventory->find("equipment") : nullptr;
81 changed += visitArray(equipment ? equipment->find("slots") : nullptr, [&](eve::Value &slot) {
82 eve::Value *stack = slot.find("stack");
83 return renameStringValue(stack ? stack->find("itemId") : nullptr, oldId, newId);
84 });
85 return changed;
86 }
88 return visitArray(tracker ? tracker->find("entries") : nullptr, [&](eve::Value &entry) {
89 return renameStringValue(entry.find("id"), oldId, newId);
90 });
92 return visitArray(tracker ? tracker->find("entries") : nullptr, [&](eve::Value &entry) {
93 return visitArray(entry.find("objectives"), [&](eve::Value &objective) {
94 return renameStringValue(objective.find("id"), oldId, newId);
95 });
96 });
98 return visitArray(actor ? actor->find("learnedSkills") : nullptr, [&](eve::Value &skill) {
99 return renameStringValue(&skill, oldId, newId);
100 });
102 eve::Value *classValue = actor ? actor->find("class") : nullptr;
103 return renameStringValue(classValue ? classValue->find("id") : nullptr, oldId, newId);
104 }
106 return visitArray(actor ? actor->find("traits") : nullptr, [&](eve::Value &trait) {
107 return renameStringValue(trait.find("traitId"), oldId, newId);
108 });
110 return visitArray(actor ? actor->find("traits") : nullptr, [&](eve::Value &trait) {
111 return renameStringValue(trait.find("source"), oldId, newId);
112 });
114 eve::Value *equipment = inventory ? inventory->find("equipment") : nullptr;
115 return visitArray(equipment ? equipment->find("slots") : nullptr, [&](eve::Value &slot) {
116 return renameStringValue(slot.find("name"), oldId, newId);
117 });
118 }
119 }
120 return 0;
121}
122
123eve::Result<void> applyQuestAdditions(eve::Value &payload, const std::set<std::string> &questIds) {
124 eve::Value *tracker = payload.find("questTracker");
125 eve::Value *entries = tracker ? tracker->find("entries") : nullptr;
126 if (!tracker || !tracker->isObject() || !entries || !entries->isArray())
128 eve::DiagnosticCode::ParseError, "quest-addition migration requires a tracker entries array",
129 "payload.questTracker.entries"));
130
131 Tracker baseline;
132 auto baselineJson = baseline.snapshotJson();
133 if (!baselineJson.ok()) return eve::Result<void>::failure(baselineJson.status());
134 auto parsedBaseline = eve::Value::fromJson(baselineJson.value());
135 if (!parsedBaseline.ok()) return eve::Result<void>::failure(parsedBaseline.status());
136 eve::Value baselineRoot = std::move(parsedBaseline).takeValue();
137 eve::Value *baselineEntries = baselineRoot.find("entries");
138 if (!baselineEntries || !baselineEntries->isArray())
140 "current tracker did not produce an entries array",
141 "questTracker.entries"));
142
143 std::unordered_map<std::string, eve::Value> oldEntries;
144 for (std::size_t index = 0; index < entries->arraySize(); ++index) {
145 const eve::Value &entry = entries->at(index);
146 const eve::Value *id = entry.isObject() ? entry.find("id") : nullptr;
147 if (!id || !id->isString())
149 eve::DiagnosticCode::ParseError, "quest-addition migration found an invalid old tracker entry",
150 "payload.questTracker.entries[" + std::to_string(index) + "].id"));
151 if (!oldEntries.emplace(id->asString(), entry).second)
153 eve::DiagnosticCode::Conflict, "quest-addition migration found a duplicate old quest id",
154 "payload.questTracker.entries[" + std::to_string(index) + "].id"));
155 }
156 for (const auto &questId : questIds) {
157 if (oldEntries.contains(questId))
159 eve::DiagnosticCode::Conflict, "quest-addition migration target already exists in the old tracker",
160 "payload.questTracker.entries." + questId));
161 }
162
163 eve::Value::Array rebuilt;
164 rebuilt.reserve(baselineEntries->arraySize());
165 std::set<std::string> appliedAdditions;
166 for (std::size_t index = 0; index < baselineEntries->arraySize(); ++index) {
167 const eve::Value &initial = baselineEntries->at(index);
168 const eve::Value *id = initial.find("id");
169 if (!id || !id->isString())
171 "current tracker produced an invalid quest id",
172 "questTracker.entries"));
173 auto old = oldEntries.find(id->asString());
174 if (old != oldEntries.end()) {
175 rebuilt.push_back(std::move(old->second));
176 oldEntries.erase(old);
177 } else if (questIds.contains(id->asString())) {
178 rebuilt.push_back(initial);
179 appliedAdditions.emplace(id->asString());
180 } else {
182 eve::DiagnosticCode::Conflict, "old tracker is missing a quest without an additive migration",
183 "payload.questTracker.entries." + id->asString()));
184 }
185 }
186 if (!oldEntries.empty())
188 eve::DiagnosticCode::Conflict, "old tracker contains a quest absent from the current registry",
189 "payload.questTracker.entries." + oldEntries.begin()->first));
190 if (appliedAdditions != questIds)
192 eve::DiagnosticCode::NotFound, "quest-addition migration target is absent from the current registry",
193 "questTracker.entries"));
194 entries->operator=(eve::Value(std::move(rebuilt)));
196}
197
198} // namespace
199
205 std::optional<RPGActor::CheckpointCandidate> actor;
206 std::optional<Party::CheckpointCandidate> party;
207
208 Impl(RPGSaveSession *session, GameState &&gameStateCandidate, Tracker &&trackerCandidate,
210 std::optional<RPGActor::CheckpointCandidate> &&actorCandidate,
211 std::optional<Party::CheckpointCandidate> &&partyCandidate)
212 : owner(session),
213 gameState(std::move(gameStateCandidate)),
214 tracker(std::move(trackerCandidate)),
215 inventory(std::move(inventoryCandidate)),
216 actor(std::move(actorCandidate)),
217 party(std::move(partyCandidate)) {}
218};
219
220RPGSaveSession::PreparedRestore::PreparedRestore(std::unique_ptr<Impl> state) : state_(std::move(state)) {}
222RPGSaveSession::PreparedRestore::PreparedRestore(PreparedRestore &&) noexcept = default;
223RPGSaveSession::PreparedRestore &RPGSaveSession::PreparedRestore::operator=(PreparedRestore &&) noexcept = default;
224
225void RPGSaveSession::bind(GameState &gameState, Tracker &tracker, RPGActor &actor, inventory::Bag &bag,
226 inventory::EquipmentSet &equipment) noexcept {
227 gameState_ = &gameState;
228 tracker_ = &tracker;
229 actor_ = &actor;
230 party_ = nullptr;
231 bag_ = &bag;
232 equipment_ = &equipment;
233}
234
235void RPGSaveSession::bindParty(GameState &gameState, Tracker &tracker, Party &party,
236 inventory::Bag &bag, inventory::EquipmentSet &equipment) noexcept {
237 gameState_ = &gameState;
238 tracker_ = &tracker;
239 actor_ = nullptr;
240 party_ = &party;
241 bag_ = &bag;
242 equipment_ = &equipment;
243}
244
245eve::Result<void> RPGSaveSession::setContentVersion(std::string_view contentVersion) {
246 if (!validContentVersion(contentVersion))
249 "RPG save content version must be a non-empty identifier without controls", "contentVersion"));
250 if (!contentVersion_.empty() && contentVersion_ != contentVersion &&
251 (!compatibleContentVersions_.empty() || !idRenameMigrations_.empty() ||
252 !questAdditionMigrations_.empty() || !singleActorPartyMigrations_.empty()))
254 eve::DiagnosticCode::Conflict, "RPG content version cannot change after migration routes are registered",
255 "contentVersion"));
256 contentVersion_ = std::string(contentVersion);
257 compatibleContentVersions_.erase(contentVersion_);
259}
260
262 if (!validContentVersion(contentVersion))
265 "compatible RPG content version must be a non-empty identifier without controls", "contentVersion"));
266 if (contentVersion == contentVersion_)
268 eve::DiagnosticCode::Conflict, "current RPG content version must not be registered as an older version",
269 "contentVersion"));
270 const bool hasMigration = std::any_of(idRenameMigrations_.begin(), idRenameMigrations_.end(),
271 [&](const auto &entry) {
272 return entry.first.fromContentVersion == contentVersion;
273 }) ||
274 questAdditionMigrations_.contains(std::string(contentVersion)) ||
275 singleActorPartyMigrations_.contains(std::string(contentVersion));
276 if (hasMigration)
278 eve::DiagnosticCode::Conflict, "RPG content version already has an ID migration route", "contentVersion"));
279 if (!compatibleContentVersions_.emplace(contentVersion).second)
281 "compatible RPG content version is already registered",
282 "contentVersion"));
284}
285
286eve::Result<void> RPGSaveSession::addIdRenameMigration(std::string_view fromContentVersion,
287 RPGSaveIdDomain domain, std::string_view oldId,
288 std::string_view newId) {
289 if (contentVersion_.empty())
292 "current RPG content version must be configured before migration rules", "contentVersion"));
293 if (!validContentVersion(fromContentVersion) || !validContentVersion(oldId) ||
294 !validContentVersion(newId) || oldId == newId)
297 "RPG ID migration requires distinct valid source and destination identifiers", "migration"));
298 if (fromContentVersion == contentVersion_)
300 eve::DiagnosticCode::Conflict, "RPG ID migration source must differ from the current content version",
301 "fromContentVersion"));
302 if (compatibleContentVersions_.contains(std::string(fromContentVersion)))
304 eve::DiagnosticCode::Conflict, "RPG content version is already admitted as directly compatible",
305 "fromContentVersion"));
306 MigrationKey key{std::string(fromContentVersion), domain, std::string(oldId)};
307 if (!idRenameMigrations_.emplace(std::move(key), std::string(newId)).second)
309 eve::DiagnosticCode::AlreadyExists, "RPG ID migration source is already registered", "migration"));
311}
312
314 std::string_view questId) {
315 if (contentVersion_.empty())
318 "current RPG content version must be configured before migration rules", "contentVersion"));
319 if (!validContentVersion(fromContentVersion) || !validContentVersion(questId))
322 "quest-addition migration requires valid source and quest identifiers", "migration"));
323 if (fromContentVersion == contentVersion_)
326 "quest-addition migration source must differ from the current content version", "fromContentVersion"));
327 if (compatibleContentVersions_.contains(std::string(fromContentVersion)))
329 eve::DiagnosticCode::Conflict, "RPG content version is already admitted as directly compatible",
330 "fromContentVersion"));
331 if (!QuestRegistry::find(std::string(questId)))
333 eve::DiagnosticCode::NotFound, "quest-addition migration target is not registered", "questId"));
334 auto &quests = questAdditionMigrations_[std::string(fromContentVersion)];
335 if (!quests.emplace(questId).second)
337 eve::DiagnosticCode::AlreadyExists, "quest-addition migration is already registered", "migration"));
339}
340
342 std::string_view fromContentVersion) {
343 if (contentVersion_.empty())
346 "current RPG content version must be configured before migration rules", "contentVersion"));
347 if (!validContentVersion(fromContentVersion) || fromContentVersion == contentVersion_)
350 "single-actor party migration requires a distinct valid source version", "fromContentVersion"));
351 if (compatibleContentVersions_.contains(std::string(fromContentVersion)))
353 eve::DiagnosticCode::Conflict, "RPG content version is already admitted as directly compatible",
354 "fromContentVersion"));
355 if (!singleActorPartyMigrations_.emplace(fromContentVersion).second)
357 "single-actor party migration is already registered",
358 "fromContentVersion"));
360}
361
363 if (!gameState_ || !tracker_ || (!actor_ && !party_) || !bag_ || !equipment_)
365 eve::DiagnosticCode::PreconditionViolation, "RPG save session requires bound participants", {}));
366 if (contentVersion_.empty())
368 eve::DiagnosticCode::PreconditionViolation, "RPG save session requires a content version", {}));
369 auto gameStateJson = gameState_->snapshotJson();
370 if (!gameStateJson.ok()) return eve::Result<std::string>::failure(gameStateJson.status());
371 auto trackerJson = tracker_->snapshotJson();
372 if (!trackerJson.ok()) return eve::Result<std::string>::failure(trackerJson.status());
373 auto actorJson = actor_ ? actor_->checkpointJson() : party_->checkpointJson();
374 if (!actorJson.ok()) return eve::Result<std::string>::failure(actorJson.status());
375 inventory::InventorySaveSession inventorySession;
376 inventorySession.bind(*bag_, *equipment_);
377 auto inventoryJson = inventorySession.snapshotJson();
378 if (!inventoryJson.ok()) return eve::Result<std::string>::failure(inventoryJson.status());
379 auto gameStateValue = eve::Value::fromJson(gameStateJson.value());
380 if (!gameStateValue.ok()) return eve::Result<std::string>::failure(gameStateValue.status());
381 auto trackerValue = eve::Value::fromJson(trackerJson.value());
382 if (!trackerValue.ok()) return eve::Result<std::string>::failure(trackerValue.status());
383 auto actorValue = eve::Value::fromJson(actorJson.value());
384 if (!actorValue.ok()) return eve::Result<std::string>::failure(actorValue.status());
385 auto inventoryValue = eve::Value::fromJson(inventoryJson.value());
386 if (!inventoryValue.ok()) return eve::Result<std::string>::failure(inventoryValue.status());
388 payload.emplace(actor_ ? "actor" : "party", std::move(actorValue).takeValue());
389 payload.emplace("contentVersion", eve::Value(contentVersion_));
390 payload.emplace("gameState", std::move(gameStateValue).takeValue());
391 payload.emplace("inventory", std::move(inventoryValue).takeValue());
392 payload.emplace("questTracker", std::move(trackerValue).takeValue());
393 const eve::SchemaVersion schemaVersion(actor_ ? 1 : 2);
394 auto envelope = eve::makeSnapshotEnvelope("rpg.save-session", saveSchema(), schemaVersion,
396 eve::Value(std::move(payload)), saveIntegrityProvider());
397 if (!envelope.ok()) return eve::Result<std::string>::failure(envelope.status());
398 return eve::serializeSnapshotEnvelope(envelope.value());
399}
400
402 auto prepared = prepareRestoreSnapshotJson(json);
403 if (!prepared.ok()) return eve::Result<void>::failure(prepared.status());
405}
406
408 auto prepared = prepareRestoreSnapshotJson(json);
409 if (!prepared.ok()) return eve::Result<void>::failure(prepared.status());
410 commitPrepared(std::move(prepared).takeValue());
412}
413
415 if (!gameState_ || !tracker_ || (!actor_ && !party_) || !bag_ || !equipment_)
417 eve::DiagnosticCode::PreconditionViolation, "RPG save session requires bound participants", {}));
418 if (contentVersion_.empty())
420 eve::DiagnosticCode::PreconditionViolation, "RPG save session requires a content version", {}));
421 auto parsed = eve::parseSnapshotEnvelope(json, saveIntegrityProvider());
422 if (!parsed.ok()) return eve::Result<PreparedRestore>::failure(parsed.status());
423 const eve::SnapshotEnvelope &envelope = parsed.value();
424 if (envelope.type != "rpg.save-session")
426 eve::DiagnosticCode::InvalidArgument, "snapshot does not belong to RPGSaveSession", "type"));
427 if (envelope.schema != saveSchema())
429 eve::DiagnosticCode::InvalidArgument, "snapshot does not belong to RPGSaveSession", "schema"));
430 eve::Value root = envelope.payload;
431 if (!root.isObject())
433 eve::DiagnosticCode::ParseError, "RPG save session payload must be an object", "payload"));
434 eve::Value *contentVersion = root.find("contentVersion");
435 if (!contentVersion || !contentVersion->isString())
438 "RPG save-session content version is missing or invalid", "payload.contentVersion"));
439 const std::string sourceContentVersion = contentVersion->asString();
440 const bool migrateSingleActor = party_ && envelope.schemaVersion == eve::SchemaVersion(1) &&
441 singleActorPartyMigrations_.contains(sourceContentVersion);
442 const eve::SchemaVersion expectedVersion(actor_ ? 1 : 2);
443 if (envelope.schemaVersion != expectedVersion && !migrateSingleActor)
445 eve::DiagnosticCode::UnknownVersion, "unsupported RPG save-session snapshot version", "schemaVersion"));
446 if (migrateSingleActor) {
447 const eve::Value *oldActor = root.find("actor");
448 if (!oldActor || !oldActor->isObject())
450 eve::DiagnosticCode::ParseError, "single-actor migration requires an actor payload", "payload.actor"));
451 auto baselineJson = party_->checkpointJson();
452 if (!baselineJson.ok()) return eve::Result<PreparedRestore>::failure(baselineJson.status());
453 auto baseline = eve::Value::fromJson(baselineJson.value());
454 if (!baseline.ok()) return eve::Result<PreparedRestore>::failure(baseline.status());
455 eve::Value migratedParty = std::move(baseline).takeValue();
456 eve::Value *members = migratedParty.find("members");
457 if (!members || !members->isArray() || members->arraySize() == 0)
460 "single-actor migration requires a non-empty live party", "party"));
461 members->at(0).set("actor", *oldActor);
462 root.set("party", std::move(migratedParty));
463 }
464 if (sourceContentVersion != contentVersion_ &&
465 !compatibleContentVersions_.contains(sourceContentVersion)) {
466 bool hasMigration = false;
467 for (const auto &[key, destination] : idRenameMigrations_) {
468 if (key.fromContentVersion != sourceContentVersion) continue;
469 hasMigration = true;
470 applyIdRename(root, key.domain, key.oldId, destination);
471 }
472 const auto questAdditions = questAdditionMigrations_.find(sourceContentVersion);
473 if (questAdditions != questAdditionMigrations_.end()) {
474 hasMigration = true;
475 auto added = applyQuestAdditions(root, questAdditions->second);
476 if (!added.ok()) return eve::Result<PreparedRestore>::failure(added.status());
477 }
478 if (migrateSingleActor) hasMigration = true;
479 if (!hasMigration)
481 eve::DiagnosticCode::Conflict, "RPG save-session content version has no compatible or migration route",
482 "payload.contentVersion"));
483 root.set("contentVersion", eve::Value(contentVersion_));
484 }
485 const eve::Value *gameStateValue = root.find("gameState");
486 const eve::Value *trackerValue = root.find("questTracker");
487 const eve::Value *actorValue = root.find(actor_ ? "actor" : "party");
488 const eve::Value *inventoryValue = root.find("inventory");
489 if (!gameStateValue || !gameStateValue->isObject() || !trackerValue || !trackerValue->isObject() ||
490 !actorValue || !actorValue->isObject() || !inventoryValue || !inventoryValue->isObject())
492 eve::DiagnosticCode::ParseError, "RPG save-session participants must be objects", "payload"));
493 auto gameStateJson = gameStateValue->toJson();
494 if (!gameStateJson.ok()) return eve::Result<PreparedRestore>::failure(gameStateJson.status());
495 auto trackerJson = trackerValue->toJson();
496 if (!trackerJson.ok()) return eve::Result<PreparedRestore>::failure(trackerJson.status());
497 auto actorJson = actorValue->toJson();
498 if (!actorJson.ok()) return eve::Result<PreparedRestore>::failure(actorJson.status());
499 auto inventoryJson = inventoryValue->toJson();
500 if (!inventoryJson.ok()) return eve::Result<PreparedRestore>::failure(inventoryJson.status());
501
502 GameState candidateGameState = *gameState_;
503 Tracker candidateTracker = *tracker_;
504 auto gameStateRestore = candidateGameState.restoreSnapshotJson(gameStateJson.value());
505 if (!gameStateRestore.ok()) return eve::Result<PreparedRestore>::failure(gameStateRestore.status());
506 auto trackerRestore = candidateTracker.restoreSnapshotJson(trackerJson.value());
507 if (!trackerRestore.ok()) return eve::Result<PreparedRestore>::failure(trackerRestore.status());
508 inventory::InventorySaveSession inventorySession;
509 inventorySession.bind(*bag_, *equipment_);
510 auto inventoryCandidate = inventorySession.prepareRestoreSnapshotJson(inventoryJson.value());
511 if (!inventoryCandidate.ok()) return eve::Result<PreparedRestore>::failure(inventoryCandidate.status());
512
513 std::optional<RPGActor::CheckpointCandidate> actorCandidate;
514 std::optional<Party::CheckpointCandidate> partyCandidate;
515 if (actor_) {
516 auto prepared = actor_->prepareCheckpointJson(actorJson.value());
517 if (!prepared.ok()) return eve::Result<PreparedRestore>::failure(prepared.status());
518 actorCandidate.emplace(std::move(prepared).takeValue());
519 } else {
520 auto prepared = party_->prepareCheckpointJson(actorJson.value());
521 if (!prepared.ok()) return eve::Result<PreparedRestore>::failure(prepared.status());
522 partyCandidate.emplace(std::move(prepared).takeValue());
523 }
524
525 auto state = std::make_unique<PreparedRestore::Impl>(
526 this, std::move(candidateGameState), std::move(candidateTracker), std::move(inventoryCandidate).takeValue(),
527 std::move(actorCandidate), std::move(partyCandidate));
529}
530
532 auto &state = *prepared.state_;
533 EV_ASSERT(state.owner == this, "RPG prepared restore belongs to another session");
534 gameState_->switches_.swap(state.gameState.switches_);
535 gameState_->variables_.swap(state.gameState.variables_);
536 gameState_->selfVariables_.swap(state.gameState.selfVariables_);
537 gameState_->selfStrings_.swap(state.gameState.selfStrings_);
538 tracker_->entries.swap(state.tracker.entries);
539 tracker_->order.swap(state.tracker.order);
540 tracker_->pending.swap(state.tracker.pending);
541 tracker_->polled.swap(state.tracker.polled);
542 if (actor_)
543 actor_->commitCheckpoint(std::move(*state.actor));
544 else
545 party_->commitCheckpoint(std::move(*state.party));
546 inventory::InventorySaveSession inventorySession;
547 inventorySession.bind(*bag_, *equipment_);
548 inventorySession.commitPrepared(std::move(state.inventory));
549}
550
551} // namespace eve::rpg
Value::Object payload
int root
Definition AnimSmr.cpp:119
EVEngine assertion entry point, backed by zeroerr.
#define EV_ASSERT(cond,...)
Assert an internal engine invariant (state that must always hold).
Definition Assert.h:37
std::unordered_map< std::string, QuestRuntime > entries
bool changed
SQObjectPtr key
Definition ECS.cpp:155
EvpackChunkInput input
Definition Evpack.cpp:170
全局游戏状态:开关(bool)、变量(数字)、独立变量(按作用域)。
HexVec3 left
HexVec3 right
Versioned atomic persistence boundary for one bag and equipment set.
std::string id
std::uint64_t bytes
Generation-safe ordered RPG party relationships.
Object::Ptr value
std::string domain
std::vector< QuestDefinition > quests
任务 / 成就 / 教程的通用定义模板与进程级注册表。
std::string slot
Transactional composition of persistent RPG state owners.
Versioned, integrity-checked persistence envelopes.
std::optional< InteractionSession > session
Definition Tactics.cpp:39
目标追踪器运行时实例(玩家日志 / 成就 / 教程可各建一份)。
std::map< std::string, std::span< const std::uint8_t > > members
uint32_t index
Owning, renderer-independent dynamic values.
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Human-readable, scoped identifier in the form namespace:name.
Definition Identity.h:414
static std::optional< LogicalId > parse(std::string_view text)
Parses a scoped logical name.
Definition Identity.cpp:37
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.cpp:19
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
Result< std::string > toJson() const
Serialize this value as deterministic compact JSON.
Definition Value.cpp:187
const std::string & asString() const
Return the string payload; the caller must have checked the kind.
Definition Value.cpp:207
static Result< Value > fromJson(std::string_view json)
Parse one strict JSON value into an owning Value.
Definition Value.cpp:177
std::map< std::string, Value > Object
Definition Value.h:34
bool isArray() const noexcept
Return true when this value is an array.
Definition Value.h:108
bool isString() const noexcept
Return true when this value is a string.
Definition Value.h:106
std::vector< Value > Array
Definition Value.h:33
std::size_t arraySize() const
Return the number of array elements.
Definition Value.cpp:220
bool isObject() const noexcept
Return true when this value is an object.
Definition Value.h:110
Value & at(std::size_t index)
Return a bounds-checked array element.
Definition Value.cpp:226
Value * find(const std::string &key) noexcept
Return an object member, or nullptr when absent.
Definition Value.cpp:244
Id128 public API.
Definition Identity.h:111
std::array< std::uint8_t, 16 > Bytes
Definition Identity.h:113
static constexpr Id128 nil() noexcept
Returns the all-zero nil value.
Definition Identity.h:141
格子型物品容器(脚本可直接操作)。
Definition Bag.h:22
EVENGINE_API_FOUNDATION public API.
Definition Equipment.h:19
Validated owning inventory state awaiting publication.
Coordinates persistent inventory owners without duplicating their live state.
void commitPrepared(PreparedRestore prepared) noexcept
Publish a previously validated state without failure or callbacks.
eve::Result< std::string > snapshotJson() const
Capture the complete bag and equipment state.
eve::Result< PreparedRestore > prepareRestoreSnapshotJson(std::string_view json) const
Parse and validate a snapshot without mutating either participant.
void bind(Bag &bag, EquipmentSet &equipment) noexcept
Bind the authoritative inventory participants.
一份游戏状态(开关/变量/独立变量)。
Definition GameState.h:26
eve::Result< void > restoreSnapshotJson(std::string_view json)
Validate and atomically restore a canonical RPG game-state JSON document.
Authoritative ordered relationship between stable party member IDs and RPG actors....
Definition Party.h:24
static const QuestDefinition * find(const std::string &id)
Find a registered quest definition by id.
Definition Quest.cpp:178
属性 / 状态 / 技能三表合一的 ECS 实体。
Definition RPGActor.h:34
Validated owning RPG state that can be published without another fallible parse.
PreparedRestore(PreparedRestore &&) noexcept
A non-owning save boundary for the persistent classic-RPG state owners.
eve::Result< void > validateSnapshotJson(std::string_view json)
Parse and fully prepare a save without publishing participant state.
void bind(GameState &gameState, Tracker &tracker, RPGActor &actor, inventory::Bag &bag, inventory::EquipmentSet &equipment) noexcept
Bind the authoritative participants used by subsequent operations.
eve::Result< std::string > snapshotJson() const
Capture every bound participant into one deterministic versioned JSON document.
eve::Result< void > allowCompatibleContentVersion(std::string_view contentVersion)
Accept one older content contract that is data-compatible with the configured version.
eve::Result< PreparedRestore > prepareRestoreSnapshotJson(std::string_view json)
Parse, migrate and validate every RPG participant without publishing live state.
void commitPrepared(PreparedRestore prepared) noexcept
Publish previously prepared RPG, quest, actor and inventory state together.
eve::Result< void > addIdRenameMigration(std::string_view fromContentVersion, RPGSaveIdDomain domain, std::string_view oldId, std::string_view newId)
Register one direct old-to-current definition ID rewrite.
eve::Result< void > allowSingleActorPartyMigration(std::string_view fromContentVersion)
Register an explicit schema-v1 single-actor to schema-v2 party migration.
eve::Result< void > setContentVersion(std::string_view contentVersion)
Configure the exact game-content contract required by this save session.
eve::Result< void > addQuestAdditionMigration(std::string_view fromContentVersion, std::string_view questId)
Register an additive migration that initializes one newly introduced quest.
void bindParty(GameState &gameState, Tracker &tracker, Party &party, inventory::Bag &bag, inventory::EquipmentSet &equipment) noexcept
Bind a complete authoritative player party instead of a single compatibility actor.
eve::Result< void > restoreSnapshotJson(std::string_view json)
Validate all participants and atomically publish a save-session snapshot.
一份任务进度 + 本实例事件队列。
Definition Tracker.h:26
eve::Result< void > restoreSnapshotJson(std::string_view json)
Validate and atomically restore quest progress against the current registry.
Definition Tracker.cpp:82
std::variant< std::monostate, std::int64_t, double, std::string, bool > Value
Definition Database.h:27
RPG 模块的基础实体:把属性 / 状态 / 技能三张表挂到一个 ECS 实体上。
RPGSaveIdDomain
Definition-reference field kinds supported by direct RPG save ID migrations.
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16
Result< SnapshotEnvelope > parseSnapshotEnvelope(std::string_view json, const SnapshotHashProvider &hashProvider)
Parse and verify an envelope from canonical or compatible JSON text.
Definition Snapshot.cpp:190
detail::StrongUint64< detail::SchemaVersionTag > SchemaVersion
Persistent data-format version; not a runtime replacement generation.
Result< std::string > serializeSnapshotEnvelope(const SnapshotEnvelope &snapshot)
Serialize an envelope as deterministic compact JSON.
Definition Snapshot.cpp:184
Result< SnapshotEnvelope > makeSnapshotEnvelope(std::string type, LogicalId schema, SchemaVersion schemaVersion, PersistentId instanceId, Revision revision, SimulationTick tick, Value payload, const SnapshotHashProvider &hashProvider)
Construct and seal a snapshot envelope.
Definition Snapshot.cpp:103
std::function< Result< ContentId >(std::string_view canonicalInput)> SnapshotHashProvider
Injected content-digest implementation used by snapshots.
Definition Snapshot.h:36
Stable outer format shared by persistence and cross-process snapshots.
Definition Snapshot.h:46
SchemaVersion schemaVersion
Definition Snapshot.h:49
std::string type
Definition Snapshot.h:47
Impl(RPGSaveSession *session, GameState &&gameStateCandidate, Tracker &&trackerCandidate, inventory::InventorySaveSession::PreparedRestore &&inventoryCandidate, std::optional< RPGActor::CheckpointCandidate > &&actorCandidate, std::optional< Party::CheckpointCandidate > &&partyCandidate)
std::optional< Party::CheckpointCandidate > party
std::optional< RPGActor::CheckpointCandidate > actor
inventory::InventorySaveSession::PreparedRestore inventory