Browse Source

Working change locale

Ivan Arkhipov 3 years ago
parent
commit
d09a2a1c88

+ 61 - 28
src/Legacy/models/datoriginalfilesdatabase.cpp

@@ -3,45 +3,48 @@
 #include <string>
 
 
-DatOriginalFilesDatabase::DatOriginalFilesDatabase(QString database_path) :
-    orig_files_db(QSqlDatabase::addDatabase("QSQLITE", database_path)),
-    _add_file_prepared_query(orig_files_db)
-{
-    orig_files_db.setDatabaseName(database_path);
-    orig_files_db.open();
-    orig_files_db.exec(_create_database_query);
-    orig_files_db.exec("PRAGMA synchronous = OFF");
-    orig_files_db.exec("PRAGMA count_changes = OFF");
-    orig_files_db.exec("PRAGMA journal_mode = MEMORY");
-    orig_files_db.exec("PRAGMA temp_store = MEMORY");
-    orig_files_db.exec("PRAGMA encoding = \"UTF-8\";");
+DatOriginalFilesDatabase::DatOriginalFilesDatabase(QString database_path) {
+    orig_files_db = new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE", database_path));
+    _add_file_prepared_query = new QSqlQuery(*orig_files_db);
+
+    orig_files_db->setDatabaseName(database_path);
+    orig_files_db->open();
+    orig_files_db->exec(_create_database_query);
+    orig_files_db->exec("PRAGMA synchronous = OFF");
+    orig_files_db->exec("PRAGMA count_changes = OFF");
+    orig_files_db->exec("PRAGMA journal_mode = MEMORY");
+    orig_files_db->exec("PRAGMA temp_store = MEMORY");
+    orig_files_db->exec("PRAGMA encoding = \"UTF-8\";");
 
-    _add_file_prepared_query.prepare(_add_file_query);
+    _add_file_prepared_query->prepare(_add_file_query);
 }
 
 DatOriginalFilesDatabase::~DatOriginalFilesDatabase() {
+    const QString connection_name = orig_files_db->connectionName();
     closeDatabase();
+    delete _add_file_prepared_query;
+    delete orig_files_db;
+    QSqlDatabase::removeDatabase(connection_name);
 }
 
 void DatOriginalFilesDatabase::addFile(int file_id, int dat_id, int version, int iteration, const LOTRO_DAT::SubfileData &data) {
     std::stringstream options_stream;
     options_stream << data.options;
     const QString options = QString::fromStdString(options_stream.str());
-    qDebug() << "AAAAAAdding file " << file_id << dat_id << version << iteration;
-    _add_file_prepared_query.bindValue(":file_id", file_id);
-    _add_file_prepared_query.bindValue(":dat_id", dat_id);
-    _add_file_prepared_query.bindValue(":version", version);
-    _add_file_prepared_query.bindValue(":iteration", iteration);
-    _add_file_prepared_query.bindValue(":options", options);
-    _add_file_prepared_query.bindValue(":binary_data", QByteArray(reinterpret_cast<char*>(data.binary_data.data()), data.binary_data.size()));
-    _add_file_prepared_query.bindValue(":text_data", QString::fromStdU16String(data.text_data));
-    if (!_add_file_prepared_query.exec()) {
-        qWarning() << _add_file_prepared_query.lastError();
+    _add_file_prepared_query->bindValue(":file_id", file_id);
+    _add_file_prepared_query->bindValue(":dat_id", dat_id);
+    _add_file_prepared_query->bindValue(":version", version);
+    _add_file_prepared_query->bindValue(":iteration", iteration);
+    _add_file_prepared_query->bindValue(":options", options);
+    _add_file_prepared_query->bindValue(":binary_data", QByteArray(reinterpret_cast<char*>(data.binary_data.data()), data.binary_data.size()));
+    _add_file_prepared_query->bindValue(":text_data", QString::fromStdU16String(data.text_data));
+    if (!_add_file_prepared_query->exec()) {
+        qWarning() << _add_file_prepared_query->lastError();
     }
 }
 
 bool DatOriginalFilesDatabase::getFile(int file_id, int &dat_id, int &version, int &iteration, LOTRO_DAT::SubfileData &data) {
-    QSqlQuery get_file_query;
+    QSqlQuery get_file_query(*orig_files_db);
     get_file_query.exec(_get_file_query.arg(file_id));
     if (!get_file_query.next()) {
         return false;
@@ -58,16 +61,46 @@ bool DatOriginalFilesDatabase::getFile(int file_id, int &dat_id, int &version, i
 }
 
 bool DatOriginalFilesDatabase::fileExists(int file_id) {
-    QSqlQuery get_file_query;
+    QSqlQuery get_file_query(*orig_files_db);
     get_file_query.exec(_get_file_query.arg(file_id));
     return get_file_query.next();
 }
 
+size_t DatOriginalFilesDatabase::getRowsCount()
+{
+    QSqlQuery count_all_rows_query(*orig_files_db);
+    count_all_rows_query.exec(_count_all_rows_query);
+    count_all_rows_query.next();
+    return count_all_rows_query.value(0).toInt();
+}
+
 void DatOriginalFilesDatabase::removeDatabase() {
-    orig_files_db.close();
-    QFile::remove(orig_files_db.databaseName());
+    orig_files_db->close();
+    QFile::remove(orig_files_db->databaseName());
 }
 
 void DatOriginalFilesDatabase::closeDatabase() {
-    orig_files_db.close();
+    orig_files_db->commit();
+    orig_files_db->close();
+}
+
+void DatOriginalFilesDatabase::performOperationOnAllFiles(const DatOriginalFilesDatabase::FileOperation &operation)
+{
+    QSqlQuery get_all_rows_query(*orig_files_db);
+    get_all_rows_query.exec(_get_all_files_query);
+    while (get_all_rows_query.next()) {
+        int file_id = get_all_rows_query.value(0).toInt();
+        int dat_id = get_all_rows_query.value(1).toInt();
+        int version = get_all_rows_query.value(2).toInt();
+        int iteration = get_all_rows_query.value(3).toInt();
+        LOTRO_DAT::SubfileData data;
+
+        const QString options = QString::fromUtf8(get_all_rows_query.value(4).toByteArray());
+        data.options = YAML::Load(options.toStdString());
+        const QByteArray binary_data = get_all_rows_query.value(5).toByteArray();
+        data.binary_data = LOTRO_DAT::BinaryData(binary_data.data(), binary_data.size());
+        data.text_data = QString::fromUtf8(get_all_rows_query.value(6).toByteArray()).toStdU16String();
+
+        operation(file_id, dat_id, version, iteration, data);
+    }
 }

+ 10 - 3
src/Legacy/models/datoriginalfilesdatabase.h

@@ -8,6 +8,8 @@
 
 class DatOriginalFilesDatabase {
 public:
+    typedef std::function<void (int file_id, int dat_id, int version, int iteration, const LOTRO_DAT::SubfileData& data)> FileOperation;
+
     explicit DatOriginalFilesDatabase(QString database_path);
 
     ~DatOriginalFilesDatabase();
@@ -18,13 +20,17 @@ public:
 
     bool fileExists(int file_id);
 
+    size_t getRowsCount();
+
     void removeDatabase();
 
     void closeDatabase();
 
+    void performOperationOnAllFiles(const FileOperation& operation);
+
 private:
-    QSqlDatabase orig_files_db;
-    QSqlQuery _add_file_prepared_query;
+    QSqlDatabase* orig_files_db = nullptr;
+    QSqlQuery* _add_file_prepared_query = nullptr;
 
     const QString _create_database_query = "CREATE TABLE IF NOT EXISTS patch_data ("
                                                 "file_id INTEGER PRIMARY KEY,"
@@ -49,7 +55,8 @@ private:
                                     ";";
 
     const QString _get_file_query = "SELECT * FROM patch_data WHERE `file_id`=%1;";
-
+    const QString _get_all_files_query = "SELECT * FROM patch_data;";
+    const QString _count_all_rows_query = "SELECT COUNT(*) FROM patch_data";
 };
 
 #endif // DATORIGINALFILESDATABASE_H

+ 79 - 18
src/Legacy/models/patchinstaller.cpp

@@ -141,12 +141,53 @@ void PatchInstaller::installPatch(QString patch_name, LOTRO_DAT::Database* datab
         _current_applied_patches_info.textures_patch_hashsum = hashsum;
     } else if (patch_name == "font") {
         _current_applied_patches_info.fonts_patch_hashsum = hashsum;
+    } else if (patch_name == "video") {
+        _current_applied_patches_info.videos_patch_hashsum = hashsum;
     }
     orig_files_db.closeDatabase();
     insertPatchesInfoInDatFile(_current_applied_patches_info);
     return;
 }
 
+void PatchInstaller::installOriginalPatch(QString patch_name)
+{
+    qDebug() << "Installing original version of patch " << patch_name;
+    DatOriginalFilesDatabase orig_db(QApplication::applicationDirPath() + "/data/" + patch_name + "_orig.db");
+
+    DatOriginalFilesDatabase::FileOperation operation = [this](int file_id, int dat_id, int version, int iteration, const LOTRO_DAT::SubfileData& data) {
+        if (dat_id == E_CLIENT_LOCAL) {
+            client_local_file_->PatchFile(data, version, iteration);
+        } else if (dat_id == E_CLIENT_GENERAL) {
+            client_general_file_->PatchFile(data, version, iteration);
+        }
+        current_status.finished_parts++;
+        if (current_status.finished_parts * 100 / current_status.total_parts !=
+                (current_status.finished_parts - 1) * 100 * 10 / current_status.total_parts) {
+            // emitting if changed at least on 0.1%
+            emit progressChanged(current_status);
+        }
+    };
+
+    orig_db.performOperationOnAllFiles(operation);
+
+    if (patch_name == "text") {
+        _current_applied_patches_info.texts_patch_hashsum = "";
+    } else if (patch_name == "image") {
+        _current_applied_patches_info.images_patch_hashsum = "";
+    } else if (patch_name == "sound") {
+        _current_applied_patches_info.sounds_patch_hashsum = "";
+    } else if (patch_name == "texture") {
+        _current_applied_patches_info.textures_patch_hashsum = "";
+    } else if (patch_name == "font") {
+        _current_applied_patches_info.fonts_patch_hashsum = "";
+    } else if (patch_name == "video") {
+        _current_applied_patches_info.videos_patch_hashsum = "";
+    } else if (patch_name == "loadscreen") {
+        _current_applied_patches_info.loadscreens_patch_hashsum = "";
+    }
+    insertPatchesInfoInDatFile(_current_applied_patches_info);
+}
+
 void PatchInstaller::installLoadscreens(LOTRO_DAT::Database* database) {
     QString locale_prefix = Settings::getValue("Lotro/original_locale").toString();
 
@@ -265,7 +306,7 @@ PatchInstaller::AppliedPatchesInfo PatchInstaller::getAppliedPatchesInfoFromDatF
             } else if (str.startsWith("FONTS:")) {
                 result.fonts_patch_hashsum = QString(str).remove(0, 6);
             } else if (str.startsWith("VIDEOS:")) {
-                result.videos_patch_hashsum = QString(str).remove(0, 6);
+                result.videos_patch_hashsum = QString(str).remove(0, 7);
             }
         }
     }
@@ -392,23 +433,6 @@ void PatchInstaller::startPatchInstallationChain() {
 
     for (const QString& patch: patches) {
         if (!Settings::getValue("DatabaseDownload/" + patch).toBool()) {
-            if (patch == "text") {
-                _current_applied_patches_info.texts_patch_hashsum = "";
-            } else if (patch == "image") {
-                _current_applied_patches_info.images_patch_hashsum = "";
-            } else if (patch == "sound") {
-                _current_applied_patches_info.sounds_patch_hashsum = "";
-            } else if (patch == "texture") {
-                _current_applied_patches_info.textures_patch_hashsum = "";
-            } else if (patch == "font") {
-                _current_applied_patches_info.fonts_patch_hashsum = "";
-            } else if (patch == "video") {
-                _current_applied_patches_info.videos_patch_hashsum = "";
-            } else if (patch == "loadscreen") {
-                _current_applied_patches_info.loadscreens_patch_hashsum = "";
-            }
-
-            qDebug() << "Skipping patch " << patch << " because its disabled";
             continue;
         }
 
@@ -452,6 +476,39 @@ void PatchInstaller::startPatchInstallationChain() {
         current_status.total_parts += db->CountRows();
     }
 
+    // Parsing info about patches which original versions should be installed
+    QStringList patches_to_be_installed_orig_versions;
+    for (const QString& patch: patches) {
+        if (Settings::getValue("DatabaseDownload/" + patch).toBool()) {
+            continue;
+        }
+
+        QString hashsum_in_dat_file = "";
+        if (patch == "text") {
+            hashsum_in_dat_file = _current_applied_patches_info.texts_patch_hashsum;
+        } else if (patch == "image") {
+            hashsum_in_dat_file = _current_applied_patches_info.images_patch_hashsum;
+        } else if (patch == "sound") {
+            hashsum_in_dat_file = _current_applied_patches_info.sounds_patch_hashsum;
+        } else if (patch == "texture") {
+            hashsum_in_dat_file = _current_applied_patches_info.textures_patch_hashsum;
+        } else if (patch == "font") {
+            hashsum_in_dat_file = _current_applied_patches_info.fonts_patch_hashsum;
+        } else if (patch == "video") {
+            hashsum_in_dat_file = _current_applied_patches_info.videos_patch_hashsum;
+        } else if (patch == "loadscreen") {
+            hashsum_in_dat_file = _current_applied_patches_info.loadscreens_patch_hashsum;
+        }
+
+        if (hashsum_in_dat_file == "") {
+            qDebug() << "Skipping installing original version of patch " << patch << " because hashsum in dat file is already empty";
+            continue;
+        }
+        patches_to_be_installed_orig_versions.append(patch);
+        DatOriginalFilesDatabase db(QApplication::applicationDirPath() + "/data/" + patch + "_orig.db");
+        current_status.total_parts += db.getRowsCount();
+    }
+
     emit progressChanged(current_status);
     for (const QString patch_name: patch_databases.keys()) {
         qInfo() << "PatchInstaller: Installing patch " << patch_name;
@@ -460,6 +517,10 @@ void PatchInstaller::startPatchInstallationChain() {
         delete patch_databases[patch_name];
     }
 
+    for (const QString& patch_name : patches_to_be_installed_orig_versions) {
+        installOriginalPatch(patch_name);
+    }
+
     insertPatchesInfoInDatFile(_current_applied_patches_info);
     qInfo() << "PatchInstaller: Finished installation chain...";
     emit finished();

+ 3 - 0
src/Legacy/models/patchinstaller.h

@@ -53,6 +53,9 @@ private:
     bool datPathIsRelevant();
 
     void installPatch(QString patch_name, LOTRO_DAT::Database* database);
+
+    void installOriginalPatch(QString patch_name);
+
     void installLoadscreens(LOTRO_DAT::Database* database);
 
     void installVideos(LOTRO_DAT::Database* database);