Game scheme handling: Use QFile instead of QSettings for increased efficiency
authorWuzzy <Wuzzy2@mail.ru>
Wed, 14 Mar 2018 14:42:50 +0100
changeset 13201 f93658732448
parent 13200 7d9462d49d4c
child 13202 0bc5f618ca7c
Game scheme handling: Use QFile instead of QSettings for increased efficiency
QTfrontend/model/ammoSchemeModel.cpp
QTfrontend/model/ammoSchemeModel.h
QTfrontend/ui/widget/selectWeapon.cpp
--- a/QTfrontend/model/ammoSchemeModel.cpp	Wed Mar 14 12:17:11 2018 +0100
+++ b/QTfrontend/model/ammoSchemeModel.cpp	Wed Mar 14 14:42:50 2018 +0100
@@ -18,6 +18,9 @@
 
 #include <QDebug>
 #include <QModelIndex>
+#include <QFile>
+#include <QTextStream>
+#include <QHash>
 
 #include "ammoSchemeModel.h"
 #include "hwconsts.h"
@@ -92,7 +95,7 @@
     numberOfDefaultSchemes = predefSchemesNames.size();
 
     spNames = QStringList()
-              << "name"                //  0
+              << "name"                //  0 | Name should be first forever
               << "fortsmode"           //  1
               << "divteams"            //  2
               << "solidland"           //  3
@@ -747,16 +750,29 @@
             fileConfig.setArrayIndex(i);
 
             QString schemeName = fileConfig.value(spNames[0]).toString();
-            if (!predefSchemesNames.contains(schemeName))
+            if (!schemeName.isNull() && !predefSchemesNames.contains(schemeName))
             {
                 QList<QVariant> scheme;
-                QSettings file(directory + "/" + schemeName + ".hwg", QSettings::IniFormat);
+                QFile file(directory + "/" + schemeName + ".hwg");
+
+                // Add keys to scheme info and create file
+                if (file.open(QIODevice::WriteOnly)) {
+                    QTextStream stream(&file);
+
+                    for (int k = 0; k < spNames.size(); ++k) {
+                        scheme << fileConfig.value(spNames[k], defaultScheme[k]);
 
-                for (int k = 0; k < spNames.size(); ++k) {
-                    scheme << fileConfig.value(spNames[k], defaultScheme[k]);
-                    file.setValue(spNames[k], fileConfig.value(spNames[k], defaultScheme[k]));
+                        // File handling
+                        // We skip the name key (k==0), it is not stored redundantly in file.
+                        // The file name is used for that already.
+                        if(k != 0) {
+                            // The file is just a list of key=value pairs
+                            stream << spNames[k] << "=" << fileConfig.value(spNames[k], defaultScheme[k]).toString();
+                            stream << endl;
+                        }
+                    }
+                    file.close();
                 }
-                file.sync();
                 imported++;
 
                 schemes.append(scheme);
@@ -765,18 +781,49 @@
         qDebug("%d game scheme(s) imported.", imported);
         fileConfig.endArray();
     } else {
-        QStringList scheme_dir = QDir(directory).entryList();
+        QStringList scheme_dir = QDir(directory).entryList(QDir::Files);
 
         for(int i = 0; i < scheme_dir.size(); i++)
         {
-            if (scheme_dir[i] == "." || scheme_dir[i] == "..") continue;
             QList<QVariant> scheme;
-            QSettings file(directory + "/" + scheme_dir[i], QSettings::IniFormat);
+            QFile file(directory + "/" + scheme_dir[i]);
 
-            for (int k = 0; k < spNames.size(); ++k)
-                scheme << file.value(spNames[k], defaultScheme[k]);
+            // Chop off file name suffix
+            QString schemeName = scheme_dir[i];
+            if (schemeName.endsWith(".hwg", Qt::CaseInsensitive)) {
+                schemeName.chop(4);
+            }
+            // Parse game scheme file
+            if (file.open(QIODevice::ReadOnly)) {
+                QTextStream stream(&file);
+                QString line, key, value;
+                QHash<QString, QString> fileKeyValues;
+                do {
+                    // Read line and get key and value
+                    line = stream.readLine();
+                    key = line.section(QChar('='), 0, 0);
+                    value = line.section(QChar('='), 1);
+                    if(!key.isNull() && !value.isNull()) {
+                        fileKeyValues[key] = value;
+                    }
+                } while (!line.isNull());
 
-            schemes.append(scheme);
+                // Add scheme name manually
+                scheme << schemeName;
+                // Add other keys from the QHash.
+                for (int k = 1; k < spNames.size(); ++k) {
+                    key = spNames[k];
+                    if (fileKeyValues.contains(key)) {
+                        scheme << fileKeyValues.value(key);
+                    } else {
+                        // Use default value in case the key is not set
+                        scheme << defaultScheme[k];
+                    }
+                }
+                schemes.append(scheme);
+
+                file.close();
+            }
         }
     }
 }
@@ -920,12 +967,22 @@
     {
         QList<QVariant> scheme = schemes[i + numberOfDefaultSchemes];
         int j = spNames.indexOf("name");
-        QSettings file(cfgdir->absolutePath() + "/Schemes/Game/" + scheme[j].toString() + ".hwg", QSettings::IniFormat);
+
+        QString schemeName = scheme[j].toString();
+        QFile file(cfgdir->absolutePath() + "/Schemes/Game/" + schemeName + ".hwg");
 
-        for (int k = 0; k < scheme.size(); ++k)
-            file.setValue(spNames[k], scheme[k]);
-
-        file.sync();
+        if (file.open(QIODevice::WriteOnly)) {
+            QTextStream stream(&file);
+            for (int k = 0; k < spNames.size(); ++k) {
+                // We skip the name key
+                if(k != j) {
+                    // The file is just a list of key=value pairs
+                    stream << spNames[k] << "=" << scheme[k].toString();
+                    stream << endl;
+                }
+            }
+            file.close();
+        }
     }
     fileConfig.endArray();
 }
--- a/QTfrontend/model/ammoSchemeModel.h	Wed Mar 14 12:17:11 2018 +0100
+++ b/QTfrontend/model/ammoSchemeModel.h	Wed Mar 14 14:42:50 2018 +0100
@@ -19,10 +19,10 @@
 #ifndef _AMMO_SCHEME_MODEL_INCLUDED
 #define _AMMO_SCHEME_MODEL_INCLUDED
 
-#include <QSettings>
 #include <QAbstractTableModel>
 #include <QStringList>
 #include <QList>
+#include <QSettings>
 
 class AmmoSchemeModel : public QAbstractTableModel
 {
--- a/QTfrontend/ui/widget/selectWeapon.cpp	Wed Mar 14 12:17:11 2018 +0100
+++ b/QTfrontend/ui/widget/selectWeapon.cpp	Wed Mar 14 14:42:50 2018 +0100
@@ -111,6 +111,7 @@
                 if (file.open(QIODevice::WriteOnly)) {
                     QTextStream stream( &file );
                     stream << old_wconf.value(keys[i]).toString() << endl;
+                    file.close();
                 }
                 imported++;
             }
@@ -131,13 +132,18 @@
 
             QFile file(cfgdir->absolutePath() + "/Schemes/Ammo/" + schemes[i]);
             QString config;
-
             if (file.open(QIODevice::ReadOnly)) {
                 QTextStream stream( &file );
                 stream >> config;
+                file.close();
             }
 
-            wconf->setValue(schemes[i].remove(".hwa"), fixWeaponSet(config));
+            // Chop off file name suffix
+            QString schemeName = schemes[i];
+            if (schemeName.endsWith(".hwa", Qt::CaseInsensitive)) {
+                schemeName.chop(4);
+            }
+            wconf->setValue(schemeName, fixWeaponSet(config));
         }
     }
 
@@ -300,6 +306,7 @@
     if (file.open(QIODevice::WriteOnly)) {
         QTextStream stream( &file );
         stream << stateFull << endl;
+        file.close();
     }
     emit weaponsEdited(curWeaponsName, m_name->text(), stateFull);
 }