Przeglądaj źródła

Refactored AConfig class and updated GameRes

Ivan Arkhipov 7 lat temu
rodzic
commit
91196b41d3
6 zmienionych plików z 160 dodań i 297 usunięć
  1. 4 4
      Legacy.pro
  2. 96 0
      aconfig.cpp
  3. 14 11
      aconfig.h
  4. 0 280
      config.cpp
  5. 42 2
      gameres.cpp
  6. 4 0
      gameres.h

+ 4 - 4
Legacy.pro

@@ -29,24 +29,24 @@ DEFINES += QT_DEPRECATED_WARNINGS
 SOURCES += \
     main.cpp \
     mainwindow.cpp \
-    config.cpp \
     network.cpp \
     helper.cpp \
     downloadmanager.cpp \
     gui.cpp \
     gameres.cpp \
-    filesystem.cpp
+    filesystem.cpp \
+    aconfig.cpp
 
 HEADERS += \
         mainwindow.h \
     app.h \
-    config.h \
     network.h \
     helper.h \
     downloadmanager.h \
     gui.h \
     gameres.h \
-    filesystem.h
+    filesystem.h \
+    aconfig.h
 
 FORMS += \
         mainwindow.ui

+ 96 - 0
aconfig.cpp

@@ -0,0 +1,96 @@
+#include "app.h"
+
+AConfig::AConfig() {
+    filename_ = QApplication::applicationDirPath() + "/settings.ini";
+}
+
+AConfig::AConfig(const AConfig&) {
+    throw QException(); // Выкидываем QException, в случае если
+                        // вообще попытались скопировать объект
+}
+
+AConfig& AConfig::operator=(AConfig&) {
+    throw QException(); // Выкидываем QException, в случае если
+                        // вообще попытались скопировать объект
+}
+
+void AConfig::saveConfig(){
+    QSettings settings(filename_, QSettings::IniFormat);
+    //Global
+    settings.setValue("Global/current_theme", "lotro");
+    settings.setValue("Global/default_theme", "lotro");
+
+    //Editor
+    settings.setValue("Editor/texts", true);
+    settings.setValue("Editor/fonts", true);
+    settings.setValue("Editor/images", true);
+    settings.setValue("Editor/sounds", true);
+    settings.setValue("Editor/videos", false);
+    settings.setValue("Editor/screens", true);
+    settings.setValue("Editor/textures", true);
+
+    //Network
+    settings.setValue("Network/server", "http://translate.lotros.ru");
+    settings.setValue("Network/coupon", "http://translate.lotros.ru/coupon");
+    settings.setValue("Network/servers", "http://translate.lotros.ru/servers");
+    settings.setValue("Network/foot", "http://translate.lotros.ru/profmessage");
+    settings.setValue("Network/news", "http://translate.lotros.ru/groupware/launcher_news");
+    settings.setValue("Network/launcher", "http://translate.lotros.ru/upload/launcher");
+    settings.setValue("Network/releases", "http://translate.lotros.ru/groupware/check_updates");
+    settings.setValue("Network/info", "http://translate.lotros.ru/paths");
+    settings.setValue("Network/update", "http://translate.lotros.ru/groupware/update");
+
+    // Values
+    settings.setValue("Values/coupon", "");
+    settings.setValue("Values/servers", "");
+
+    // Local
+    settings.setValue("Local/file", "client_local_English.dat");
+
+    //Datetime
+    settings.setValue("Datetime/texts", true);
+    settings.setValue("Datetime/fonts", true);
+    settings.setValue("Datetime/images", true);
+    settings.setValue("Datetime/sounds", true);
+    settings.setValue("Datetime/videos", false);
+    settings.setValue("Datetime/loadscreens", true);
+    settings.setValue("Datetime/textures", true);
+
+
+    //Updates
+    settings.setValue("Updates/update", false);
+    settings.setValue("Updates/micro", false);
+
+    settings.sync();
+}
+
+QString AConfig::getValue(QString section, QString key){
+    QSettings settings( conffile, QSettings::IniFormat );
+    settings.beginGroup(section);
+    key = settings.value(key, -1).toString();
+    settings.endGroup();
+    return key;
+}
+
+void AConfig::setValue(QString section, QString key, QString value){
+    QSettings settings( conffile, QSettings::IniFormat );
+    settings.beginGroup(section);
+    settings.setValue(key, value);
+    settings.sync();
+}
+
+void AConfig::deleteKey(QString section, QString key){
+    QSettings settings( conffile, QSettings::IniFormat );
+    QStringList keys = settings.childKeys();
+    settings.beginGroup(section);
+    if (keys.contains(key)){ settings.remove(key); }
+    settings.endGroup();
+}
+
+void AConfig::deleteSection(QString section){
+    QSettings settings( conffile, QSettings::IniFormat );
+    settings.beginGroup(section);
+    settings.remove("");
+    settings.endGroup();
+}
+

+ 14 - 11
config.h → aconfig.h

@@ -3,31 +3,34 @@
 
 #include <QSettings>
 #include <QDebug>
+#include <string>
 
 #define ORGANIZATION_NAME "LotRO: Legacy Team"
 #define ORGANIZATION_DOMAIN "www.translate.lotros.ru"
 #define APPLICATION_NAME "LotRO: Legacy"
 
 class AConfig {
-   public:
+private:
     AConfig();
-    AConfig( const AConfig&);
-    AConfig& operator=( AConfig& );
+    AConfig(const AConfig&);
+    AConfig& operator=(AConfig&);
+
+public:
+    static AConfig* getInstance() {
+        static AConfig instance;
+        return &instance;
+    }
 
     void saveConfig();
-    void loadConfig();
-    QString getSkinFile();
-    QString getSkinName();
-    void loadSkin();
+
     void setValue(QString section, QString key, QString value);
     QString getValue(QString section, QString key);
+
     void deleteKey(QString section, QString key);
     void deleteSection(QString section);
-    QStringList getLotroPath();
-    const char *getDatPath(int id);
-    void saveAllDatFiles();
 
-    QString conffile;
+private:
+    QString filename_;
 };
 
 #endif // CONFIG_H

+ 0 - 280
config.cpp

@@ -1,280 +0,0 @@
-#include "app.h"
-
-
-AConfig::AConfig() {
-    conffile = QApplication::applicationDirPath() + "/settings.ini";
-}
-
-AConfig::AConfig(const AConfig&) {
-    throw QException(); // Выкидываем QException, в случае если
-                        // вообще попытались скопировать объект
-}
-
-AConfig& AConfig::operator=(AConfig&) {
-    throw QException(); // Выкидываем QException, в случае если
-                        // вообще попытались скопировать объект
-}
-
-void AConfig::saveConfig(){
-    QSettings settings( conffile, QSettings::IniFormat );
-    //Global
-    settings.setValue("Global/current_theme", "lotro");
-    settings.setValue("Global/default_theme", "lotro");
-
-    //Editor
-    settings.setValue("Editor/texts", true);
-    settings.setValue("Editor/fonts", true);
-    settings.setValue("Editor/images", true);
-    settings.setValue("Editor/sounds", true);
-    settings.setValue("Editor/videos", false);
-    settings.setValue("Editor/screens", true);
-    settings.setValue("Editor/textures", true);
-
-    //Network
-    settings.setValue("Network/server", "http://translate.lotros.ru");
-    settings.setValue("Network/coupon", "http://translate.lotros.ru/coupon");
-    settings.setValue("Network/servers", "http://translate.lotros.ru/servers");
-    settings.setValue("Network/foot", "http://translate.lotros.ru/profmessage");
-    settings.setValue("Network/news", "http://translate.lotros.ru/groupware/launcher_news");
-    settings.setValue("Network/launcher", "http://translate.lotros.ru/upload/launcher");
-    settings.setValue("Network/releases", "http://translate.lotros.ru/groupware/check_updates");
-    settings.setValue("Network/info", "http://translate.lotros.ru/paths");
-    settings.setValue("Network/update", "http://translate.lotros.ru/groupware/update");
-
-    // Values
-    settings.setValue("Values/coupon", "");
-    settings.setValue("Values/servers", "");
-
-    // Local
-    settings.setValue("Local/file", "client_local_English.dat");
-
-    //Datetime
-    settings.setValue("Datetime/texts", true);
-    settings.setValue("Datetime/fonts", true);
-    settings.setValue("Datetime/images", true);
-    settings.setValue("Datetime/sounds", true);
-    settings.setValue("Datetime/videos", false);
-    settings.setValue("Datetime/loadscreens", true);
-    settings.setValue("Datetime/textures", true);
-
-
-    //Updates
-    settings.setValue("Updates/update", false);
-    settings.setValue("Updates/micro", false);
-
-    settings.sync();
-}
-
-void AConfig::loadConfig(){
-    App *app = &App::getInstance();
-    app->logSectionStart("Загрузка конфигурации");
-    qInfo("%s:%i: %s", __FILE__, __LINE__, "Находим каталог с игрой");
-    app->window->ui->lotropathLabel->setText(app->config->getValue("Local", "folder") != "-1" ? app->config->getValue("Local", "folder") : "Выберите папку с игрой");
-    qInfo("%s:%i: %s", __FILE__, __LINE__, "Проверяем активность патча \"Шрифты\"");
-    app->window->ui->checkFonts->setChecked(app->config->getValue("Editor", "fonts") == "true" ? true : false);
-    qInfo("%s:%i: %s", __FILE__, __LINE__, "Проверяем активность патча \"Тексты\"");
-    app->window->ui->checkTexts->setChecked(app->config->getValue("Editor", "texts")== "true" ? true : false);
-    qInfo("%s:%i: %s", __FILE__, __LINE__, "Проверяем активность патча \"Карты\"");
-    app->window->ui->checkMaps->setChecked(app->config->getValue("Editor", "images") == "true" ? true : false);
-    qInfo("%s:%i: %s", __FILE__, __LINE__, "Проверяем активность патча \"Звуки\"");
-    app->window->ui->checkSounds->setChecked(app->config->getValue("Editor", "sounds")== "true" ? true : false);
-
-    app->window->ui->checkVideos->setChecked(app->config->getValue("Editor", "videos") == "true" ? true : false);
-    qInfo("%s:%i: %s", __FILE__, __LINE__, "Проверяем активность патча \"Заставки\"");
-    app->window->ui->checkScreens->setChecked(app->config->getValue("Editor", "screens")== "true" ? true : false);
-    qInfo("%s:%i: %s", __FILE__, __LINE__, "Проверяем активность микрообновлений");
-    app->window->ui->checkMicro->setChecked(app->config->getValue("Updates", "micro")== "true" ? true : false);
-    qInfo("%s:%i: %s", __FILE__, __LINE__, "Выполнено.");
-    app->logSectionEnd();
-}
-
-QString AConfig::getSkinFile(){
-    App *app = &App::getInstance();
-    QString current_theme = QApplication::applicationDirPath() + "/styles/" + app->config->getValue("Global", "current_theme") + "/skin.ini";
-    QString default_theme = QApplication::applicationDirPath() + "/styles/" + app->config->getValue("Global", "default_theme") + "/skin.ini";
-    QString skinfile = app->helper->fileExists(current_theme) ? current_theme : default_theme;
-    return skinfile;
-}
-
-QString AConfig::getSkinName(){
-    App *app = &App::getInstance();
-    QString current_theme = app->config->getValue("Global", "current_theme");
-    QString default_theme = app->config->getValue("Global", "default_theme");
-    QString path = QApplication::applicationDirPath() + "/styles/" + current_theme + "/skin.ini";
-    QString skinname = app->helper->fileExists(path) ? current_theme : default_theme;
-    return skinname;
-}
-
-void AConfig::loadSkin(){
-    App *app = &App::getInstance();
-    app->logSectionStart("Загружаем скин лаунчера");
-    qInfo("%s:%i: %s", __FILE__, __LINE__, "Определяем активную тему оформления");
-    QSettings settings( getSkinFile(), QSettings::IniFormat );
-    QStringList items = settings.childGroups();
-    QVariant k;
-    QPushButton butt;
-    QLabel lab;
-    qInfo("%s:%i: %s", __FILE__, __LINE__, "Начинаем настройку элементов");
-
-    foreach (QString item, items){
-        qInfo("%s:%i: %s%s", __FILE__, __LINE__, "Устанавливаем настройки для ", app->helper->stringToChar(item));
-        QWidget* obj = app->window->findChild<QWidget*>(item);
-        if (obj != nullptr){
-            obj->installEventFilter(app->window);
-            settings.beginGroup(item);
-            if(settings.value("left").toString() != "-1"){
-                obj->setGeometry(QRect(
-                    settings.value("left").toInt(),
-                    settings.value("top").toInt(),
-                    settings.value("width").toInt(),
-                    settings.value("height").toInt()
-                ));
-            }
-
-            qInfo("%s:%i: %s%s", __FILE__, __LINE__, app->helper->stringToChar(item), ": настройка геометрии завершена");
-
-            if(settings.value("shadow") != k){
-                QString color = settings.value("shcolor") != k ? settings.value("shcolor").toString() : "#222";
-                int blur = settings.value("shblur") != k ? settings.value("shblur").toInt() : 2;
-                int offset_x = settings.value("shx") != k ? settings.value("shx").toInt() : 1;
-                int offset_y = settings.value("shy") != k ? settings.value("shy").toInt() : 1;
-                app->helper->applyShadow(obj, color, blur, offset_x, offset_y);
-            }
-            qInfo("%s:%i: %s%s", __FILE__, __LINE__, app->helper->stringToChar(item), ":  настройка теней завершена");
-
-            if(settings.value("text") != k){
-                QString text = settings.value("text").toString();
-                if(text == "del") text = "";
-                if(obj->metaObject()->className() == butt.metaObject()->className()){
-                   QPushButton * b = dynamic_cast<QPushButton* >(obj);
-                   b->setText(text);
-                }
-
-
-                if(obj->metaObject()->className() == lab.metaObject()->className()){
-                   QLabel * l = dynamic_cast<QLabel* >(obj);
-                   l->setText(settings.value("text").toString());
-                }
-                qInfo("%s:%i: %s%s", __FILE__, __LINE__, app->helper->stringToChar(item), ": текст установлен");
-            }
-
-            if(settings.value("align") != k){
-                QLabel * lab = dynamic_cast<QLabel* >(obj);
-                if(settings.value("align") == "left") lab->setAlignment(Qt::AlignLeft);
-                if(settings.value("align") == "center") lab->setAlignment(Qt::AlignCenter);
-                if(settings.value("align") == "right") lab->setAlignment(Qt::AlignRight);
-            }
-            qInfo("%s:%i: %s%s", __FILE__, __LINE__, app->helper->stringToChar(item), ": настройки позиционирования применены");
-
-            settings.endGroup();
-        } else  {
-            if(item == "mainWindow") {
-                settings.beginGroup(item);
-                qInfo("%s:%i: %s%s", __FILE__, __LINE__, app->helper->stringToChar(item), ": обработка параметров главного окна");
-                int w = settings.value("width").toInt();
-                int h = settings.value("height").toInt();
-
-                QDesktopWidget desktop;
-                QRect rect = desktop.availableGeometry(desktop.primaryScreen()); // прямоугольник с размерами экрана
-                QPoint center = rect.center(); //координаты центра экрана
-                int x = center.x() - (w/2);  // учитываем половину ширины окна
-                int y = center.y() - (h/2); // .. половину высоты
-                center.setX(x);
-                center.setY(y);
-                app->window->move(center);
-                settings.endGroup();
-            }
-            qInfo("%s:%i: %s%s", __FILE__, __LINE__, app->helper->stringToChar(item), ": закончен просчет геометрии");
-
-            if(item == "htmlColors") {
-                settings.beginGroup(item);
-                foreach(QString key, settings.allKeys()){
-                   app->helper->htmlColors.insert(key, settings.value(key).toString());
-                }
-                settings.endGroup();
-            }
-            qInfo("%s:%i: %s%s", __FILE__, __LINE__, app->helper->stringToChar(item), ": загружены HTML-цвета");
-        }
-    }
-    qInfo("%s:%i: %s", __FILE__, __LINE__, "Выполнено.");
-    app->logSectionEnd();
-}
-
-QString AConfig::getValue(QString section, QString key){
-    QSettings settings( conffile, QSettings::IniFormat );
-    settings.beginGroup(section);
-    key = settings.value(key, -1).toString();
-    settings.endGroup();
-    return key;
-}
-
-void AConfig::setValue(QString section, QString key, QString value){
-    QSettings settings( conffile, QSettings::IniFormat );
-    settings.beginGroup(section);
-    settings.setValue(key, value);
-    settings.sync();
-}
-
-void AConfig::deleteKey(QString section, QString key){
-    QSettings settings( conffile, QSettings::IniFormat );
-    QStringList keys = settings.childKeys();
-    settings.beginGroup(section);
-    if (keys.contains(key)){ settings.remove(key); }
-    settings.endGroup();
-}
-
-void AConfig::deleteSection(QString section){
-    QSettings settings( conffile, QSettings::IniFormat );
-    settings.beginGroup(section);
-    settings.remove("");
-    settings.endGroup();
-}
-
-QStringList AConfig::getLotroPath(){
-    App *app = &App::getInstance();
-    QStringList paths;
-
-    //#if defined(Q_WS_WIN)
-
-    // Windows 7
-    QSettings m("HKEY_CLASSES_ROOT\\Local Settings\\Software\\Microsoft\\Windows\\Shell\\MuiCache", QSettings::NativeFormat);
-    foreach (QString key, m.allKeys()) {
-      if(key.contains("TurbineLauncher.exe") && app->helper->fileExists(key)){
-          paths.append(key.replace("/TurbineLauncher.exe", ""));
-      }
-    }
-
-    // Windows 8, 10
-    QSettings n("HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\12bbe590-c890-11d9-9669-0800200c9a66_is1", QSettings::NativeFormat);
-    foreach (QString key, n.allKeys()) {
-        qDebug() << key;
-        if(key.contains("InstallLocation") || key.contains("installlocation")){
-            QString folder = n.value(key).toString().replace("\\", "/").replace("/TurbineLauncher.exe", "").replace("\"", "");
-            if(app->helper->fileExists(folder + "/TurbineLauncher.exe")) paths.append(folder);
-        }
-    }
-
-    //#else
-        // Реализация под Linux
-    //#endif
-
-
-    return paths;
-}
-
-void AConfig::saveAllDatFiles(){
-    App *app = &App::getInstance();
-    for(int i = 0; i < 5; i++){
-        //app->datfiles[i].CommitChanges();
-        app->datfiles[i].CloseDatFile();
-    }
-}
-
-const char * AConfig::getDatPath(int id){
-    App *app = &App::getInstance();
-    QStringList dats;
-    dats << app->config->getValue("Local", "file") << "client_general.dat" << "client_sound.dat" << "client_surface.dat" << "client_highres.dat";
-    //std::string file = (app->config->getValue("Local", "folder") + "/" + dats.at(id)).toStdString();
-    return app->helper->stringToChar(app->config->getValue("Local", "folder") + "/" + dats.at(id));
-}
-

+ 42 - 2
gameres.cpp

@@ -1,5 +1,5 @@
 #include "gameres.h"
-#include "config.h"
+#include "aconfig.h"
 #include "filesystem.h"
 
 #include <QString>
@@ -182,6 +182,38 @@ void GameRes::checkDatFile() {
     }
 }
 
+QStringList GameRes::getLotroPath(){
+    App *app = &App::getInstance();
+    QStringList paths;
+
+    //#if defined(Q_WS_WIN)
+
+    // Windows 7
+    QSettings m("HKEY_CLASSES_ROOT\\Local Settings\\Software\\Microsoft\\Windows\\Shell\\MuiCache", QSettings::NativeFormat);
+    foreach (QString key, m.allKeys()) {
+      if(key.contains("TurbineLauncher.exe") && app->helper->fileExists(key)){
+          paths.append(key.replace("/TurbineLauncher.exe", ""));
+      }
+    }
+
+    // Windows 8, 10
+    QSettings n("HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\12bbe590-c890-11d9-9669-0800200c9a66_is1", QSettings::NativeFormat);
+    foreach (QString key, n.allKeys()) {
+        qDebug() << key;
+        if(key.contains("InstallLocation") || key.contains("installlocation")){
+            QString folder = n.value(key).toString().replace("\\", "/").replace("/TurbineLauncher.exe", "").replace("\"", "");
+            if(app->helper->fileExists(folder + "/TurbineLauncher.exe")) paths.append(folder);
+        }
+    }
+
+    //#else
+        // Реализация под Linux
+    //#endif
+
+
+    return paths;
+}
+
 // Private functions
 
 void GameRes::processFile() {
@@ -191,7 +223,7 @@ void GameRes::processFile() {
         qInfo("%s:%i: %s", __FILE__, __LINE__, "Достигнут конец файла.");
     } else {
         int dat_id = subfile.options["did"].as<int>();
-        int dat_state = datfiles_[dat_id].InitDatFile(AConfig::getInstance()->getDatPath(dat_id), dat_id);
+        int dat_state = datfiles_[dat_id].InitDatFile(getDatPath(dat_id), dat_id);
         if(dat_state > 0)
             datfiles_[dat_id].PatchFile(subfile);
     }
@@ -300,3 +332,11 @@ bool GameRes::isDatReady() { // Checks if file is ready to write data
 
     return free;
 }
+
+std::string GameRes::getDatPath(int id){
+    App *app = &App::getInstance();
+    QStringList dats;
+    dats << app->config->getValue("Local", "file") << "client_general.dat" << "client_sound.dat" << "client_surface.dat" << "client_highres.dat";
+    //std::string file = (app->config->getValue("Local", "folder") + "/" + dats.at(id)).toStdString();
+    return app->helper->stringToChar(app->config->getValue("Local", "folder") + "/" + dats.at(id));
+}

+ 4 - 0
gameres.h

@@ -41,6 +41,8 @@ public:
 
     void checkDatFile();                    // Checks and returns information if dat file is clear/updated
 
+    QStringList getLotroPath();             // Returns path to LotRO files
+
 signals:
     // Signals for every public function finish. Gives result int value.
     void openDatFileFinished(int result);
@@ -64,6 +66,8 @@ private:
     void applyLoadscreens();
     bool isDatReady(); // Checks if file is ready to write data
 
+    std::string getDatPath(int id);
+
 private:
     QThread *thread_;
     bool busy_;