diff --git a/dDatabase/GameDatabase/ITables/IBbbAutosave.h b/dDatabase/GameDatabase/ITables/IBbbAutosave.h index 27d6082cd..090f5b334 100644 --- a/dDatabase/GameDatabase/ITables/IBbbAutosave.h +++ b/dDatabase/GameDatabase/ITables/IBbbAutosave.h @@ -17,6 +17,9 @@ public: std::string lxfml; // sd0 compressed LXFML as the client sent it std::vector sourceItems; // the models that were in the BBB inventory when it was sent int64_t updatedAt{}; // Unix seconds + // When the server couldn't rebuild it (Unix seconds), 0 otherwise. A held autosave is kept for staff and not + // rebuilt again; the client's next autosave replaces it. + int64_t heldAt{}; }; virtual std::optional GetBbbAutosave(const LWOOBJID characterId) = 0; @@ -25,6 +28,9 @@ public: virtual void SetBbbAutosave(const LWOOBJID characterId, const IBbbAutosave::Info& info) = 0; virtual void DeleteBbbAutosave(const LWOOBJID characterId) = 0; + + // Marks the character's autosave as one the server couldn't rebuild, keeping its data + virtual void HoldBbbAutosave(const LWOOBJID characterId, const int64_t heldAt) = 0; }; #endif //!__IBBBAUTOSAVE__H__ diff --git a/dDatabase/GameDatabase/MySQL/MySQLDatabase.h b/dDatabase/GameDatabase/MySQL/MySQLDatabase.h index 16346f2f9..b3ffe3591 100644 --- a/dDatabase/GameDatabase/MySQL/MySQLDatabase.h +++ b/dDatabase/GameDatabase/MySQL/MySQLDatabase.h @@ -271,6 +271,7 @@ public: std::optional GetBbbAutosave(const LWOOBJID characterId) override; void SetBbbAutosave(const LWOOBJID characterId, const IBbbAutosave::Info& info) override; void DeleteBbbAutosave(const LWOOBJID characterId) override; + void HoldBbbAutosave(const LWOOBJID characterId, const int64_t heldAt) override; // IPropertyReputation VisitorHistory GetPropertyVisitorHistory(LWOOBJID propertyId, uint32_t accountId, uint32_t day, uint32_t days) override; diff --git a/dDatabase/GameDatabase/MySQL/Tables/BbbAutosave.cpp b/dDatabase/GameDatabase/MySQL/Tables/BbbAutosave.cpp index 91aea1f0e..48f8716bd 100644 --- a/dDatabase/GameDatabase/MySQL/Tables/BbbAutosave.cpp +++ b/dDatabase/GameDatabase/MySQL/Tables/BbbAutosave.cpp @@ -3,7 +3,7 @@ #include "BbbAutosaveItems.h" std::optional MySQLDatabase::GetBbbAutosave(const LWOOBJID characterId) { - auto result = ExecuteSelect("SELECT lxfml, source_items, updated_at FROM bbb_autosave WHERE character_id = ?;", characterId); + auto result = ExecuteSelect("SELECT lxfml, source_items, updated_at, held_at FROM bbb_autosave WHERE character_id = ?;", characterId); if (!result->next()) return std::nullopt; IBbbAutosave::Info info; @@ -13,6 +13,7 @@ std::optional MySQLDatabase::GetBbbAutosave(const LWOOBJID c info.lxfml = data.str(); info.sourceItems = BbbAutosaveItems::Parse(result->getString("source_items").c_str()); info.updatedAt = result->getInt64("updated_at"); + info.heldAt = result->getInt64("held_at"); return info; } @@ -20,11 +21,15 @@ void MySQLDatabase::SetBbbAutosave(const LWOOBJID characterId, const IBbbAutosav std::istringstream lxfml(info.lxfml); const auto items = BbbAutosaveItems::Join(info.sourceItems); ExecuteInsert( - "INSERT INTO bbb_autosave (character_id, lxfml, source_items, updated_at) VALUES (?, ?, ?, ?) " - "ON DUPLICATE KEY UPDATE lxfml = VALUES(lxfml), source_items = VALUES(source_items), updated_at = VALUES(updated_at);", - characterId, static_cast(&lxfml), items, info.updatedAt); + "INSERT INTO bbb_autosave (character_id, lxfml, source_items, updated_at, held_at) VALUES (?, ?, ?, ?, ?) " + "ON DUPLICATE KEY UPDATE lxfml = VALUES(lxfml), source_items = VALUES(source_items), updated_at = VALUES(updated_at), held_at = VALUES(held_at);", + characterId, static_cast(&lxfml), items, info.updatedAt, info.heldAt); } void MySQLDatabase::DeleteBbbAutosave(const LWOOBJID characterId) { ExecuteDelete("DELETE FROM bbb_autosave WHERE character_id = ?;", characterId); } + +void MySQLDatabase::HoldBbbAutosave(const LWOOBJID characterId, const int64_t heldAt) { + ExecuteUpdate("UPDATE bbb_autosave SET held_at = ? WHERE character_id = ?;", heldAt, characterId); +} diff --git a/dDatabase/GameDatabase/SQLite/SQLiteDatabase.h b/dDatabase/GameDatabase/SQLite/SQLiteDatabase.h index 6f320675a..f1d9379d7 100644 --- a/dDatabase/GameDatabase/SQLite/SQLiteDatabase.h +++ b/dDatabase/GameDatabase/SQLite/SQLiteDatabase.h @@ -255,6 +255,7 @@ public: std::optional GetBbbAutosave(const LWOOBJID characterId) override; void SetBbbAutosave(const LWOOBJID characterId, const IBbbAutosave::Info& info) override; void DeleteBbbAutosave(const LWOOBJID characterId) override; + void HoldBbbAutosave(const LWOOBJID characterId, const int64_t heldAt) override; // IPropertyReputation VisitorHistory GetPropertyVisitorHistory(LWOOBJID propertyId, uint32_t accountId, uint32_t day, uint32_t days) override; diff --git a/dDatabase/GameDatabase/SQLite/Tables/BbbAutosave.cpp b/dDatabase/GameDatabase/SQLite/Tables/BbbAutosave.cpp index 4b847cfb2..c9a48f7bc 100644 --- a/dDatabase/GameDatabase/SQLite/Tables/BbbAutosave.cpp +++ b/dDatabase/GameDatabase/SQLite/Tables/BbbAutosave.cpp @@ -3,7 +3,7 @@ #include "BbbAutosaveItems.h" std::optional SQLiteDatabase::GetBbbAutosave(const LWOOBJID characterId) { - auto [_, result] = ExecuteSelect("SELECT lxfml, source_items, updated_at FROM bbb_autosave WHERE character_id = ?;", characterId); + auto [_, result] = ExecuteSelect("SELECT lxfml, source_items, updated_at, held_at FROM bbb_autosave WHERE character_id = ?;", characterId); if (result.eof()) return std::nullopt; IBbbAutosave::Info info; @@ -12,6 +12,7 @@ std::optional SQLiteDatabase::GetBbbAutosave(const LWOOBJID if (blob && length > 0) info.lxfml.assign(reinterpret_cast(blob), length); info.sourceItems = BbbAutosaveItems::Parse(result.getStringField("source_items", "")); info.updatedAt = result.getInt64Field("updated_at"); + info.heldAt = result.getInt64Field("held_at"); return info; } @@ -19,11 +20,15 @@ void SQLiteDatabase::SetBbbAutosave(const LWOOBJID characterId, const IBbbAutosa std::istringstream lxfml(info.lxfml); const auto items = BbbAutosaveItems::Join(info.sourceItems); ExecuteInsert( - "INSERT INTO bbb_autosave (character_id, lxfml, source_items, updated_at) VALUES (?, ?, ?, ?) " - "ON CONFLICT(character_id) DO UPDATE SET lxfml = excluded.lxfml, source_items = excluded.source_items, updated_at = excluded.updated_at;", - characterId, static_cast(&lxfml), items, info.updatedAt); + "INSERT INTO bbb_autosave (character_id, lxfml, source_items, updated_at, held_at) VALUES (?, ?, ?, ?, ?) " + "ON CONFLICT(character_id) DO UPDATE SET lxfml = excluded.lxfml, source_items = excluded.source_items, updated_at = excluded.updated_at, held_at = excluded.held_at;", + characterId, static_cast(&lxfml), items, info.updatedAt, info.heldAt); } void SQLiteDatabase::DeleteBbbAutosave(const LWOOBJID characterId) { ExecuteDelete("DELETE FROM bbb_autosave WHERE character_id = ?;", characterId); } + +void SQLiteDatabase::HoldBbbAutosave(const LWOOBJID characterId, const int64_t heldAt) { + ExecuteUpdate("UPDATE bbb_autosave SET held_at = ? WHERE character_id = ?;", heldAt, characterId); +} diff --git a/dDatabase/GameDatabase/TestSQL/TestSQLDatabase.h b/dDatabase/GameDatabase/TestSQL/TestSQLDatabase.h index 3ef8c8ca6..b65a44c13 100644 --- a/dDatabase/GameDatabase/TestSQL/TestSQLDatabase.h +++ b/dDatabase/GameDatabase/TestSQL/TestSQLDatabase.h @@ -297,6 +297,10 @@ class TestSQLDatabase : public GameDatabase { } void SetBbbAutosave(const LWOOBJID characterId, const IBbbAutosave::Info& info) override { m_BbbAutosaves[characterId] = info; } void DeleteBbbAutosave(const LWOOBJID characterId) override { m_BbbAutosaves.erase(characterId); } + void HoldBbbAutosave(const LWOOBJID characterId, const int64_t heldAt) override { + const auto it = m_BbbAutosaves.find(characterId); + if (it != m_BbbAutosaves.end()) it->second.heldAt = heldAt; + } std::map m_BbbAutosaves; // IUgcLookup diff --git a/dGame/dUtilities/BrickByBrick.cpp b/dGame/dUtilities/BrickByBrick.cpp index 1716be15d..2039ad21f 100644 --- a/dGame/dUtilities/BrickByBrick.cpp +++ b/dGame/dUtilities/BrickByBrick.cpp @@ -98,8 +98,12 @@ namespace { std::string sd0{}; // the model's own sd0 compressed LXFML }; + // More separate models than one build is ever split into; a save that splits into more is not stored + constexpr size_t MAX_MODELS_PER_BUILD = 1000; + // Splits a build into its models and stores each as a new ugc row (is_optimized 0: the UGC server makes its mesh - // and icon). Every save makes new blueprint ids: the client caches blueprints by id. + // and icon). Every save makes new blueprint ids: the client caches blueprints by id. Nothing is stored when the + // build can't be read or split; a database error is passed on. std::vector StoreModels(Entity& player, const std::string& sd0Data, const std::string& debugName) { std::vector saved; auto* character = player.GetCharacter(); @@ -108,7 +112,17 @@ namespace { std::istringstream sd0Stream(sd0Data); Sd0 sd0(sd0Stream); const auto lxfml = sd0.GetAsStringUncompressed(); - if (lxfml.empty()) return saved; + if (lxfml.empty()) { + LOG("The brick by brick build %s of %llu has no LXFML that can be read", debugName.c_str(), player.GetObjectID()); + return saved; + } + + // Each model is placed by its bricks' boxes, from the client's brickdb (read on the first save, then kept) + const auto parts = Lxfml::Split(lxfml, NiPoint3Constant::ZERO, BrickBounds::Get(Game::assetManager)); + if (parts.empty() || parts.size() > MAX_MODELS_PER_BUILD) { + LOG("The brick by brick build %s of %llu splits into %zu models, not storing it", debugName.c_str(), player.GetObjectID(), parts.size()); + return saved; + } if (Game::config && Game::config->GetValue("save_lxfmls") == "1") { std::ofstream outFile("debug_lxfml_uncompressed_" + debugName + ".lxfml"); @@ -125,8 +139,7 @@ namespace { // the owner keeps editing isn't made for every save (docs/UgcServer.md) const auto debounce = Game::config ? GeneralUtils::TryParse(Game::config->GetValue("ugc_debounce_seconds")).value_or(120) : 120; const auto processAfter = UgcDebounce::ProcessAfter(std::time(nullptr), debounce); - // Each model is placed by its bricks' boxes, from the client's brickdb (read on the first save, then kept) - for (const auto& part : Lxfml::Split(lxfml, NiPoint3Constant::ZERO, BrickBounds::Get(Game::assetManager))) { + for (const auto& part : parts) { const auto [modelId, blueprintId] = ObjectIDManager::GetNewModelIDs(); Sd0 model = sd0; model.FromData(reinterpret_cast(part.lxfml.data()), part.lxfml.size()); @@ -197,7 +210,8 @@ BrickByBrick::ModelRemoval BrickByBrick::PlanModelRemoval(const int32_t reason) BrickByBrick::Recovery BrickByBrick::PlanRecovery(const std::vector& modelsInBbb, const std::optional& autosave) { Recovery recovery; - recovery.rebuild = autosave.has_value() && !IsEmptyModel(autosave->lxfml); + // A held autosave (one the server couldn't rebuild before) is kept for staff and never rebuilt again + recovery.rebuild = autosave.has_value() && autosave->heldAt == 0 && !IsEmptyModel(autosave->lxfml); if (recovery.rebuild) recovery.consume = autosave->sourceItems; for (const auto id : modelsInBbb) { if (std::ranges::find(recovery.consume, id) == recovery.consume.end()) recovery.giveBack.push_back(id); @@ -267,7 +281,13 @@ void BrickByBrick::Save(Entity& player, const LWOOBJID localId, const std::strin return; } - const auto models = StoreModels(player, sd0Data, std::to_string(localId)); + std::vector models; + try { + models = StoreModels(player, sd0Data, std::to_string(localId)); + } catch (const std::exception& ex) { + LOG("Couldn't store the brick by brick model %llu of %llu: %s", localId, player.GetObjectID(), ex.what()); + models.clear(); + } if (models.empty()) { // BBB_COULD_NOT_GENERATE_MODEL; the client keeps the build response.reasonCode = eBlueprintSaveResponseType::ModelGenerationFailed; @@ -406,11 +426,22 @@ uint32_t BrickByBrick::RecoverUnfinishedBuild(Entity& player) { const auto autosave = Database::Get()->GetBbbAutosave(player.GetObjectID()); auto recovery = PlanRecovery(IdsIn(*inventory, eInventoryType::MODELS_IN_BBB), autosave); const bool hadBricks = !ItemsIn(*inventory, eInventoryType::BRICKS_IN_BBB).empty(); - if (!autosave && recovery.giveBack.empty() && !hadBricks) return 0; + const bool held = autosave && autosave->heldAt != 0; + if ((!autosave || held) && recovery.giveBack.empty() && !hadBricks) return 0; uint32_t rebuilt = 0; + bool hold = false; if (recovery.rebuild) { - for (const auto& model : StoreModels(player, autosave->lxfml, "autosave_" + std::to_string(player.GetObjectID()))) { + std::vector models; + try { + models = StoreModels(player, autosave->lxfml, "autosave_" + std::to_string(player.GetObjectID())); + } catch (const std::exception& ex) { + LOG("Couldn't store the brick by brick autosave of %llu: %s", player.GetObjectID(), ex.what()); + models.clear(); + } + // An autosave that makes no model is kept as it is (held) and not tried again: the build's models go back + hold = models.empty(); + for (const auto& model : models) { inventory->ReceiveItem(LWOOBJID_EMPTY, MODEL_ITEM_LOT, 1, eLootSourceType::INVENTORY, ModelItemConfig(model.blueprintId, model.modelId), model.modelId, false, { .inventory = eInventoryType::MODELS, .showFlyingLoot = false }); rebuilt++; @@ -436,7 +467,12 @@ uint32_t BrickByBrick::RecoverUnfinishedBuild(Entity& player) { } SettleBricks(*inventory, rebuilt > 0); - if (autosave) Database::Get()->DeleteBbbAutosave(player.GetObjectID()); + if (hold) { + LOG("Holding the brick by brick autosave of %llu: it made no model, so it is kept and not rebuilt again", player.GetObjectID()); + Database::Get()->HoldBbbAutosave(player.GetObjectID(), static_cast(std::time(nullptr))); + } else if (autosave && !held) { + Database::Get()->DeleteBbbAutosave(player.GetObjectID()); + } LOG("Recovered the brick by brick build of %llu: %u models rebuilt, %zu models given back", player.GetObjectID(), rebuilt, recovery.giveBack.size()); if (rebuilt > 0) { diff --git a/docs/BuildWorkflow.md b/docs/BuildWorkflow.md index 01aa28e6b..8c6b7f203 100644 --- a/docs/BuildWorkflow.md +++ b/docs/BuildWorkflow.md @@ -163,7 +163,10 @@ could be made from the LXFML and 11 (`PlacementFailed`, no message, the build st With nothing to keep (after a save, or an emptied model) it sends the bare sd0 header (5 bytes) to clear it. (The client also has a `BBB_SAVE` key action, 0x55; its handler was not traced.) DLU keeps the latest one per character in `bbb_autosave`, with the -ids of the models in `MODELS_IN_BBB` at the time. +ids of the models in `MODELS_IN_BBB` at the time. An autosave that makes no model when it is rebuilt (it can't be +inflated or read, splits into more than 1000 models, or storing it fails) is kept with `held_at` set and is not +rebuilt again: the models that were open go back to `MODELS` and the bricks to the backpack. The client's next autosave +replaces it. `S->C RebuildBBBAutosaveMsg(count)` tells the client the server rebuilt `count` unfinished models from an autosave; it shows `BBB_AUTOSAVE_REBUILDING_SINGLE` / `_MULTIPLE` ("... tried to rebuild it for you. Any leftover bricks have been diff --git a/migrations/dlu/mysql/108_bbb_autosave_held.sql b/migrations/dlu/mysql/108_bbb_autosave_held.sql new file mode 100644 index 000000000..85ed78ecb --- /dev/null +++ b/migrations/dlu/mysql/108_bbb_autosave_held.sql @@ -0,0 +1,3 @@ +/* held_at: when the server couldn't rebuild this autosave into models (Unix seconds), 0 otherwise. A held autosave is + kept as it is and not rebuilt again; the client's next autosave replaces it. */ +ALTER TABLE bbb_autosave ADD COLUMN held_at BIGINT NOT NULL DEFAULT 0; diff --git a/migrations/dlu/sqlite/91_bbb_autosave_held.sql b/migrations/dlu/sqlite/91_bbb_autosave_held.sql new file mode 100644 index 000000000..161f60714 --- /dev/null +++ b/migrations/dlu/sqlite/91_bbb_autosave_held.sql @@ -0,0 +1,2 @@ +/* bbb_autosave.held_at: see the MySQL migration. */ +ALTER TABLE bbb_autosave ADD COLUMN held_at BIGINT NOT NULL DEFAULT 0; diff --git a/tests/dDatabaseTests/DatabaseParityTests.cpp b/tests/dDatabaseTests/DatabaseParityTests.cpp index ea5c43994..6c092c7a0 100644 --- a/tests/dDatabaseTests/DatabaseParityTests.cpp +++ b/tests/dDatabaseTests/DatabaseParityTests.cpp @@ -1922,7 +1922,7 @@ TEST_F(ParitySeeded, BbbAutosave) { for (int i = 0; i < 256; i++) binary += static_cast(i); const auto describe = [](const std::optional& info) { if (!info) return json(nullptr); - return json{ info->lxfml, info->sourceItems, info->updatedAt }; + return json{ info->lxfml, info->sourceItems, info->updatedAt, info->heldAt }; }; EXPECT_EQ(Both("GetBbbAutosave empty", [&](GameDatabase& db) { return describe(db.GetBbbAutosave(CHAR_BOB)); }), json(nullptr)); Both("SetBbbAutosave", [&](GameDatabase& db) { @@ -1930,6 +1930,17 @@ TEST_F(ParitySeeded, BbbAutosave) { db.SetBbbAutosave(CHAR_BOB, { binary, { 1152921510000000002LL, 1152921510000000003LL }, 1700000001 }); return describe(db.GetBbbAutosave(CHAR_BOB)); }); + // Holding keeps the data; the next autosave replaces it and the hold + const auto heldAt = Both("HoldBbbAutosave", [&](GameDatabase& db) { + db.HoldBbbAutosave(CHAR_BOB, 1700000002); + return describe(db.GetBbbAutosave(CHAR_BOB)); + }); + EXPECT_EQ(heldAt[0], json(binary)); + EXPECT_EQ(heldAt[3], 1700000002); + EXPECT_EQ(Both("SetBbbAutosave after a hold", [&](GameDatabase& db) { + db.SetBbbAutosave(CHAR_BOB, { "second", {}, 1700000003 }); + return describe(db.GetBbbAutosave(CHAR_BOB))[3]; + }), json(0)); EXPECT_EQ(Both("DeleteBbbAutosave", [&](GameDatabase& db) { db.DeleteBbbAutosave(CHAR_BOB); return describe(db.GetBbbAutosave(CHAR_BOB)); diff --git a/tests/dGameTests/BrickByBrickTests.cpp b/tests/dGameTests/BrickByBrickTests.cpp index 829d170c9..a2e1c4917 100644 --- a/tests/dGameTests/BrickByBrickTests.cpp +++ b/tests/dGameTests/BrickByBrickTests.cpp @@ -69,6 +69,14 @@ TEST_F(BrickByBrickTests, AnEmptyAutosaveRebuildsNothing) { EXPECT_EQ(recovery.giveBack, (std::vector{ 1 })); } +TEST_F(BrickByBrickTests, AHeldAutosaveIsNeverRebuilt) { + IBbbAutosave::Info autosave{ ONE_CHUNK, { 2 }, 0, 1700000000 }; + const auto recovery = BrickByBrick::PlanRecovery({ 1, 2 }, autosave); + EXPECT_FALSE(recovery.rebuild); + EXPECT_TRUE(recovery.consume.empty()); + EXPECT_EQ(recovery.giveBack, (std::vector{ 1, 2 })); +} + TEST_F(BrickByBrickTests, BuildInventoriesLoadIntoTheNormalOnes) { EXPECT_EQ(BrickByBrick::InventoryToLoadInto(eInventoryType::MODELS_IN_BBB), eInventoryType::MODELS); EXPECT_EQ(BrickByBrick::InventoryToLoadInto(eInventoryType::BRICKS_IN_BBB), eInventoryType::BRICKS); @@ -109,6 +117,11 @@ TEST_F(BrickByBrickTests, QuickSaveIsKeptUntilClearedWithTheBareHeader) { BrickByBrick::Autosave(player, HEADER_ONLY); EXPECT_FALSE(Database::Get()->GetBbbAutosave(player.GetObjectID()).has_value()); + + // A new autosave replaces a held one, and the hold with it + Database::Get()->SetBbbAutosave(player.GetObjectID(), { ONE_CHUNK, {}, 1, 1700000000 }); + BrickByBrick::Autosave(player, ONE_CHUNK); + EXPECT_EQ(Database::Get()->GetBbbAutosave(player.GetObjectID())->heldAt, 0); } TEST_F(BrickByBrickTests, AReturnedModelIsNeverUsedUpByTheAutosave) {