diff options
author | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-08-02 22:25:20 +1000 |
---|---|---|
committer | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-08-02 22:25:20 +1000 |
commit | b2cabc75672f9d818ac7db7537d302916238adad (patch) | |
tree | 0fb451fa5895d74ac75e57fa3b510d8ca82618c6 /source/game/StarPlayerStorage.cpp | |
parent | 98949574a8cbcd0ba3e0d910fbf1abcbb1bcb1d2 (diff) |
Game can now load players with non-uuid filenames
Diffstat (limited to 'source/game/StarPlayerStorage.cpp')
-rw-r--r-- | source/game/StarPlayerStorage.cpp | 31 |
1 files changed, 21 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) |