From b2cabc75672f9d818ac7db7537d302916238adad Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Wed, 2 Aug 2023 22:25:20 +1000 Subject: [PATCH] Game can now load players with non-uuid filenames --- source/game/StarPlayerStorage.cpp | 31 +++++++++++++++++++++---------- source/game/StarPlayerStorage.hpp | 2 ++ 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/source/game/StarPlayerStorage.cpp b/source/game/StarPlayerStorage.cpp index f128244..bdff700 100644 --- a/source/game/StarPlayerStorage.cpp +++ b/source/game/StarPlayerStorage.cpp @@ -39,9 +39,11 @@ PlayerStorage::PlayerStorage(String const& storageDir) { String filename = File::relativeTo(m_storageDirectory, file.first); if (filename.endsWith(".player")) { try { - Uuid uuid(file.first.rsplit('.').at(0)); + auto json = VersionedJson::readFile(filename); + Uuid uuid(json.content.getString("uuid")); auto& playerCacheData = m_savedPlayersCache[uuid]; - playerCacheData = entityFactory->loadVersionedJson(VersionedJson::readFile(filename), EntityType::Player); + playerCacheData = entityFactory->loadVersionedJson(json, EntityType::Player); + m_playerFileNames.insert(uuid, file.first.rsplit('.').at(0)); } catch (std::exception const& e) { Logger::error("Error loading player file, ignoring! {} : {}", filename, outputException(e, false)); } @@ -131,11 +133,13 @@ Json PlayerStorage::savePlayer(PlayerPtr const& player) { auto uuid = player->uuid(); auto& playerCacheData = m_savedPlayersCache[uuid]; + if (!m_playerFileNames.hasLeftValue(uuid)) + m_playerFileNames.insert(uuid, uuid.hex()); auto newPlayerData = player->diskStore(); if (playerCacheData != newPlayerData) { playerCacheData = newPlayerData; VersionedJson versionedJson = entityFactory->storeVersionedJson(EntityType::Player, playerCacheData); - VersionedJson::writeFile(versionedJson, File::relativeTo(m_storageDirectory, strf("{}.player", uuid.hex()))); + VersionedJson::writeFile(versionedJson, File::relativeTo(m_storageDirectory, strf("{}.player", uuidFileName(uuid)))); } return newPlayerData; } @@ -204,7 +208,7 @@ WorldChunks PlayerStorage::loadShipData(Uuid const& uuid) { if (!m_savedPlayersCache.contains(uuid)) throw PlayerException(strf("No such stored player with uuid '{}'", uuid.hex())); - String filename = File::relativeTo(m_storageDirectory, strf("{}.shipworld", uuid.hex())); + String filename = File::relativeTo(m_storageDirectory, strf("{}.shipworld", uuidFileName(uuid))); try { if (File::exists(filename)) return WorldStorage::getWorldChunksFromFile(filename); @@ -223,9 +227,8 @@ void PlayerStorage::applyShipUpdates(Uuid const& uuid, WorldChunks const& update if (updates.empty()) return; - - String filename = File::relativeTo(m_storageDirectory, strf("{}.shipworld", uuid.hex())); - WorldStorage::applyWorldChunksUpdateToFile(filename, updates); + String filePath = File::relativeTo(m_storageDirectory, strf("{}.shipworld", uuidFileName(uuid))); + WorldStorage::applyWorldChunksUpdateToFile(filePath, updates); } void PlayerStorage::moveToFront(Uuid const& uuid) { @@ -238,10 +241,11 @@ void PlayerStorage::backupCycle(Uuid const& uuid) { auto configuration = Root::singleton().configuration(); unsigned playerBackupFileCount = configuration->get("playerBackupFileCount").toUInt(); + auto& fileName = uuidFileName(uuid); - File::backupFileInSequence(File::relativeTo(m_storageDirectory, strf("{}.player", uuid.hex())), playerBackupFileCount, ".bak"); - File::backupFileInSequence(File::relativeTo(m_storageDirectory, strf("{}.shipworld", uuid.hex())), playerBackupFileCount, ".bak"); - File::backupFileInSequence(File::relativeTo(m_storageDirectory, strf("{}.metadata", uuid.hex())), playerBackupFileCount, ".bak"); + File::backupFileInSequence(File::relativeTo(m_storageDirectory, strf("{}.player", fileName)), playerBackupFileCount, ".bak"); + File::backupFileInSequence(File::relativeTo(m_storageDirectory, strf("{}.shipworld", fileName)), playerBackupFileCount, ".bak"); + File::backupFileInSequence(File::relativeTo(m_storageDirectory, strf("{}.metadata", fileName)), playerBackupFileCount, ".bak"); } void PlayerStorage::setMetadata(String key, Json value) { @@ -256,6 +260,13 @@ Json PlayerStorage::getMetadata(String const& key) { return m_metadata.value(key); } +String const& PlayerStorage::uuidFileName(Uuid const& uuid) const { + if (auto fileName = m_playerFileNames.rightPtr(uuid)) + return *fileName; + else + throw PlayerException::format("No matching filename for uuid '{}'", uuid.hex()); +} + void PlayerStorage::writeMetadata() { JsonArray order; for (auto const& p : m_savedPlayersCache) diff --git a/source/game/StarPlayerStorage.hpp b/source/game/StarPlayerStorage.hpp index 9f7fd4a..519ef86 100644 --- a/source/game/StarPlayerStorage.hpp +++ b/source/game/StarPlayerStorage.hpp @@ -44,11 +44,13 @@ public: Json getMetadata(String const& key); private: + String const& uuidFileName(Uuid const& uuid) const; void writeMetadata(); mutable RecursiveMutex m_mutex; String m_storageDirectory; OrderedHashMap m_savedPlayersCache; + BiMap m_playerFileNames; JsonObject m_metadata; };