1
Fork 0
mirror of https://github.com/redstrate/Astra.git synced 2025-04-20 11:47:46 +00:00

Call sync() before trying to synchronize game data

This should hopefully cut down on old room state
This commit is contained in:
Joshua Goins 2024-07-30 20:07:24 -04:00
parent ce1411de79
commit a382c07896
3 changed files with 65 additions and 34 deletions

View file

@ -11,7 +11,7 @@
/** /**
* @brief Handles setting up the connection to Matrix and all of the fun things needed to do for that. * @brief Handles setting up the connection to Matrix and all of the fun things needed to do for that.
* Does NOT handle the actual synchronization process, see @c CharacterSync. That handles determining the files to sync and whatnot. * Does NOT handle the actual synchronization process, see @c CharacterSync.
*/ */
class SyncManager : public QObject class SyncManager : public QObject
{ {
@ -24,27 +24,40 @@ public:
explicit SyncManager(QObject *parent = nullptr); explicit SyncManager(QObject *parent = nullptr);
/** /**
* Log in to a connection * @brief Log in to a connection
* @param matrixId user id in the form @user:server.tld * @param matrixId user id in the form @user:server.tld
* @param password * @param password
*/ */
Q_INVOKABLE void login(const QString &matrixId, const QString &password); Q_INVOKABLE void login(const QString &matrixId, const QString &password);
/** /**
* Log out of the connection * @brief Log out of the connection
*/ */
Q_INVOKABLE void logout(); Q_INVOKABLE void logout();
/** /**
* Run a single sync. We're not syncing constantly, since we typically don't need it and it consumes a lot of data * @brief Run a single sync. We're not syncing constantly, since we typically don't need it and it consumes a lot of data.
*/ */
Q_INVOKABLE void sync(); Q_INVOKABLE QCoro::Task<> sync();
/**
* @return Whether there is a connection to the server.
*/
bool connected() const; bool connected() const;
/**
* @return The currently logged in user.
*/
QString userId() const; QString userId() const;
/**
* @return The LibQuotient connection.
*/
Quotient::Connection *connection() const; Quotient::Connection *connection() const;
/// If we're ready to begin downloading or uploading data /**
* @return If we're ready to begin downloading or uploading data
*/
bool isReady() const; bool isReady() const;
struct PreviousCharacterData { struct PreviousCharacterData {
@ -52,22 +65,35 @@ public:
QString hostname; QString hostname;
}; };
/// Returns a content repo URI, or nullopt if there's existing character data or not respectively /**
* @return The currently uploaded character data, or nullopt if there's none for @p id.
*/
QCoro::Task<std::optional<PreviousCharacterData>> getUploadedCharacterData(const QString &id); QCoro::Task<std::optional<PreviousCharacterData>> getUploadedCharacterData(const QString &id);
/// Uploads character data for @p id from @p path (a file) /**
* @brief Uploads character data for @p id from @p path.
* @return True if uploaded successfuly, false otherwise.
*/
QCoro::Task<bool> uploadedCharacterData(const QString &id, const QString &path); QCoro::Task<bool> uploadedCharacterData(const QString &id, const QString &path);
/// Downloads character data /**
* @brief Downloads the character data archive from @p mxcUri and extracts it in @p destPath.
*/
QCoro::Task<bool> downloadCharacterData(const QString &mxcUri, const QString &destPath); QCoro::Task<bool> downloadCharacterData(const QString &mxcUri, const QString &destPath);
/// Checks the lock on the sync /**
* @brief Checks if there's a lock.
*/
QCoro::Task<std::optional<QString>> checkLock(); QCoro::Task<std::optional<QString>> checkLock();
/// Sets the sync lock to the device's hostname /**
* @brief Sets the sync to the device's hostname.
*/
QCoro::Task<> setLock(); QCoro::Task<> setLock();
/// Breaks the sync lock /**
* @brief Breaks the current sync lock.
*/
QCoro::Task<> breakLock(); QCoro::Task<> breakLock();
Q_SIGNALS: Q_SIGNALS:
@ -80,7 +106,8 @@ Q_SIGNALS:
private: private:
QString roomId() const; QString roomId() const;
void setRoomId(const QString &roomId); void setRoomId(const QString &roomId);
QCoro::Task<void> findRoom(); QCoro::Task<> findRoom();
QCoro::Task<> beginInitialSync();
Quotient::AccountRegistry m_accountRegistry; Quotient::AccountRegistry m_accountRegistry;

View file

@ -39,6 +39,9 @@ QCoro::Task<bool> CharacterSync::sync(const bool initialSync)
Q_EMIT launcher.stageChanged(i18n("Synchronizing character data...")); Q_EMIT launcher.stageChanged(i18n("Synchronizing character data..."));
// Perform a manual sync just in case
co_await syncManager->sync();
// On game boot, check if we need the lock. Otherwise break it when we clean up. // On game boot, check if we need the lock. Otherwise break it when we clean up.
if (initialSync) { if (initialSync) {
if (const auto hostname = co_await syncManager->checkLock(); hostname.has_value()) { if (const auto hostname = co_await syncManager->checkLock(); hostname.has_value()) {

View file

@ -19,9 +19,9 @@
#include <QCoro> #include <QCoro>
#include <QTemporaryFile> #include <QTemporaryFile>
const QString roomType = QStringLiteral("zone.xiv.astra-sync"); const auto roomType = QStringLiteral("zone.xiv.astra-sync");
const QString syncEventType = QStringLiteral("zone.xiv.astra.sync"); const auto syncEventType = QStringLiteral("zone.xiv.astra.sync");
const QString lockEventType = QStringLiteral("zone.xiv.astra.lock"); const auto lockEventType = QStringLiteral("zone.xiv.astra.lock");
using namespace Quotient; using namespace Quotient;
@ -32,13 +32,14 @@ SyncManager::SyncManager(QObject *parent)
connect(&m_accountRegistry, &AccountRegistry::rowsInserted, this, [this]() { connect(&m_accountRegistry, &AccountRegistry::rowsInserted, this, [this]() {
connection()->setCacheState(false); connection()->setCacheState(false);
connection()->setLazyLoading(false); connection()->setLazyLoading(false);
connection()->setDirectChatEncryptionDefault(false); Connection::setDirectChatEncryptionDefault(false);
connection()->setEncryptionDefault(false); Connection::setEncryptionDefault(false);
Q_EMIT connectedChanged(); Q_EMIT connectedChanged();
Q_EMIT userIdChanged(); Q_EMIT userIdChanged();
Q_EMIT connectionChanged(); Q_EMIT connectionChanged();
sync();
beginInitialSync();
}); });
connect(&m_accountRegistry, &AccountRegistry::rowsRemoved, this, [this]() { connect(&m_accountRegistry, &AccountRegistry::rowsRemoved, this, [this]() {
Q_EMIT connectedChanged(); Q_EMIT connectedChanged();
@ -61,7 +62,7 @@ void SyncManager::login(const QString &matrixId, const QString &password)
Qt::SingleShotConnection); Qt::SingleShotConnection);
connect(connection, &Connection::connected, this, [this, connection] { connect(connection, &Connection::connected, this, [this, connection] {
qCDebug(ASTRA_LOG) << "Connected!"; qCDebug(ASTRA_LOG) << "Connected to the sync server!";
// TODO: store somewhere else, not their QSettings // TODO: store somewhere else, not their QSettings
AccountSettings account(connection->userId()); AccountSettings account(connection->userId());
@ -101,23 +102,15 @@ Quotient::Connection *SyncManager::connection() const
return nullptr; return nullptr;
} }
void SyncManager::sync() QCoro::Task<> SyncManager::sync()
{ {
// TODO: de-duplicate sync() calls. otherwise if they happen in quick succession, they wait on each other which is useless for our use case
auto connection = m_accountRegistry.accounts().first(); auto connection = m_accountRegistry.accounts().first();
connection->sync(); connection->sync();
connect( co_await qCoro(connection, &Connection::syncDone);
connection, m_accountRegistry.accounts().first()->stopSync();
&Connection::syncDone, co_return;
this,
[this]() {
m_accountRegistry.accounts().first()->stopSync();
qCDebug(ASTRA_LOG) << "Done with sync.";
// Find the room we need to sync with
findRoom();
},
Qt::SingleShotConnection);
} }
QCoro::Task<void> SyncManager::findRoom() QCoro::Task<void> SyncManager::findRoom()
@ -282,4 +275,12 @@ QCoro::Task<> SyncManager::breakLock()
co_return; co_return;
} }
QCoro::Task<> SyncManager::beginInitialSync()
{
co_await sync();
// Find the room we need to sync with
findRoom();
}
#include "moc_syncmanager.cpp" #include "moc_syncmanager.cpp"