Keep game/weapon schemes in separate files
authorKoBeWi
Thu, 08 Feb 2018 08:50:45 +0100
changeset 12927 c0320d66310d
parent 12926 684621f7eab3
child 12928 305f3667af20
Keep game/weapon schemes in separate files
QTfrontend/hwform.cpp
QTfrontend/model/ammoSchemeModel.cpp
QTfrontend/ui/widget/selectWeapon.cpp
--- a/QTfrontend/hwform.cpp	Thu Feb 08 07:30:19 2018 +0100
+++ b/QTfrontend/hwform.cpp	Thu Feb 08 08:50:45 2018 +0100
@@ -347,7 +347,7 @@
 
     connect(ui.pageVideos, SIGNAL(goBack()), config, SLOT(SaveVideosOptions()));
 
-    ammoSchemeModel = new AmmoSchemeModel(this, cfgdir->absolutePath() + "/schemes.ini");
+    ammoSchemeModel = new AmmoSchemeModel(this, cfgdir->absolutePath() + "/Game Settings");
     ui.pageScheme->setModel(ammoSchemeModel);
     ui.pageMultiplayer->gameCFG->GameSchemes->setModel(ammoSchemeModel);
     ui.pageOptions->SchemesName->setModel(ammoSchemeModel);
--- a/QTfrontend/model/ammoSchemeModel.cpp	Thu Feb 08 07:30:19 2018 +0100
+++ b/QTfrontend/model/ammoSchemeModel.cpp	Thu Feb 08 08:50:45 2018 +0100
@@ -69,9 +69,9 @@
                                 << QVariant()              // scriptparam    43
                                 ;
 
-AmmoSchemeModel::AmmoSchemeModel(QObject* parent, const QString & fileName) :
+AmmoSchemeModel::AmmoSchemeModel(QObject* parent, const QString & directory) :
     QAbstractTableModel(parent),
-    fileConfig(fileName, QSettings::IniFormat)
+    fileConfig(cfgdir->absolutePath() + "/schemes.ini", QSettings::IniFormat)
 {
     predefSchemesNames = QStringList()
                          << "Default"
@@ -569,7 +569,7 @@
             << QVariant()              // scriptparam    43
             ;
 
-	QList<QVariant> construction;
+    QList<QVariant> construction;
     construction
             << predefSchemesNames[10]  // name           0
             << QVariant(false)         // fortsmode      1
@@ -617,7 +617,7 @@
             << QVariant()              // scriptparam    43
             ;
 
-	QList<QVariant> hedgeeditor;
+    QList<QVariant> hedgeeditor;
     hedgeeditor
             << predefSchemesNames[11]  // name           0
             << QVariant(false)         // fortsmode      1
@@ -664,9 +664,9 @@
             << QVariant(0)             // world edge     42
             << QVariant()              // scriptparam    43
             ;
-			
- 
-			
+
+
+
     schemes.append(defaultScheme);
     schemes.append(proMode);
     schemes.append(shoppa);
@@ -681,22 +681,44 @@
     schemes.append(hedgeeditor);
 
 
-    int size = fileConfig.beginReadArray("schemes");
-    for (int i = 0; i < size; ++i)
-    {
-        fileConfig.setArrayIndex(i);
+    if (!QDir(directory).exists()) {
+        QDir().mkdir(directory);
+
+        int size = fileConfig.beginReadArray("schemes");
+        for (int i = 0; i < size; ++i)
+        {
+            fileConfig.setArrayIndex(i);
+
+            if (!predefSchemesNames.contains(fileConfig.value(spNames[0]).toString()))
+            {
+                QList<QVariant> scheme;
+                QSettings file(directory + "/" + fileConfig.value(spNames[0]).toString() + ".ini", QSettings::IniFormat);
 
-        if (!predefSchemesNames.contains(fileConfig.value(spNames[0]).toString()))
+                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.sync();
+
+                schemes.append(scheme);
+            }
+        }
+        fileConfig.endArray();
+    } else {
+        QStringList scheme_dir = QDir(directory).entryList();
+
+        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);
 
             for (int k = 0; k < spNames.size(); ++k)
-                scheme << fileConfig.value(spNames[k], defaultScheme[k]);
+                scheme << file.value(spNames[k], defaultScheme[k]);
 
             schemes.append(scheme);
         }
     }
-    fileConfig.endArray();
 }
 
 QVariant AmmoSchemeModel::headerData(int section, Qt::Orientation orientation, int role) const
@@ -809,6 +831,9 @@
 
     beginRemoveRows(parent, row, row);
 
+    QList<QVariant> scheme = schemes[row];
+    int j = spNames.indexOf("name");
+    QFile(cfgdir->absolutePath() + "/Game Settings/" + scheme[j].toString() + ".ini").remove();
     schemes.removeAt(row);
 
     endRemoveRows();
@@ -830,16 +855,16 @@
 
 void AmmoSchemeModel::Save()
 {
-    fileConfig.beginWriteArray("schemes", schemes.size() - numberOfDefaultSchemes);
-
     for (int i = 0; i < schemes.size() - numberOfDefaultSchemes; ++i)
     {
-        fileConfig.setArrayIndex(i);
-
         QList<QVariant> scheme = schemes[i + numberOfDefaultSchemes];
+        int j = spNames.indexOf("name");
+        QSettings file(cfgdir->absolutePath() + "/Game Settings/" + scheme[j].toString() + ".ini", QSettings::IniFormat);
 
         for (int k = 0; k < scheme.size(); ++k)
-            fileConfig.setValue(spNames[k], scheme[k]);
+            file.setValue(spNames[k], scheme[k]);
+
+        file.sync();
     }
     fileConfig.endArray();
 }
--- a/QTfrontend/ui/widget/selectWeapon.cpp	Thu Feb 08 07:30:19 2018 +0100
+++ b/QTfrontend/ui/widget/selectWeapon.cpp	Thu Feb 08 08:50:45 2018 +0100
@@ -21,6 +21,7 @@
 #include "weaponItem.h"
 #include "hwconsts.h"
 
+#include <QDebug>
 #include <QPushButton>
 #include <QGridLayout>
 #include <QHBoxLayout>
@@ -84,16 +85,45 @@
     QFrame(parent),
     m_numItems(numItems)
 {
-    wconf = new QSettings(cfgdir->absolutePath() + "/weapons.ini", QSettings::IniFormat, this);
+    if (!QDir(cfgdir->absolutePath() + "/Weapon Settings").exists()) {
+        QDir().mkdir(cfgdir->absolutePath() + "/Weapon Settings");
+        wconf = new QSettings(cfgdir->absolutePath() + "/weapons.ini", QSettings::IniFormat, this);
 
-    for(int i = 0; i < cDefaultAmmos.size(); ++i)
-        wconf->setValue(cDefaultAmmos[i].first, cDefaultAmmos[i].second);
+        for(int i = 0; i < cDefaultAmmos.size(); ++i)
+            wconf->setValue(cDefaultAmmos[i].first, cDefaultAmmos[i].second);
+
+        QStringList keys = wconf->allKeys();
+        for(int i = 0; i < keys.size(); i++)
+        {
+            if (wconf->value(keys[i]).toString().size() != cDefaultAmmoStore->size())
+                wconf->setValue(keys[i], fixWeaponSet(wconf->value(keys[i]).toString()));
 
-    QStringList keys = wconf->allKeys();
-    for(int i = 0; i < keys.size(); i++)
-    {
-        if (wconf->value(keys[i]).toString().size() != cDefaultAmmoStore->size())
-            wconf->setValue(keys[i], fixWeaponSet(wconf->value(keys[i]).toString()));
+            QFile file(cfgdir->absolutePath() + "/Weapon Settings/" + keys[i] + ".hww");
+            if (file.open(QIODevice::WriteOnly)) {
+                QTextStream stream( &file );
+                stream << wconf->value(keys[i]).toString() << endl;
+            }
+        }
+    } else {
+        wconf = new QSettings("Hedgewars", "Hedgewars");
+        wconf->clear();
+
+        QStringList schemes = QDir(cfgdir->absolutePath() + "/Weapon Settings").entryList();
+
+        for(int i = 0; i < schemes.size(); i++)
+        {
+            if (schemes[i] == "." || schemes[i] == "..") continue;
+
+            QFile file(cfgdir->absolutePath() + "/Weapon Settings/" + schemes[i]);
+            QString config;
+
+            if (file.open(QIODevice::ReadOnly)) {
+                QTextStream stream( &file );
+                stream >> config;
+            }
+
+            wconf->setValue(schemes[i].remove(".hww"), fixWeaponSet(config));
+        }
     }
 
     QString currentState = *cDefaultAmmoStore;
@@ -251,6 +281,11 @@
         wconf->remove(curWeaponsName);
     }
     wconf->setValue(m_name->text(), stateFull);
+    QFile file(cfgdir->absolutePath() + "/Weapon Settings/" + m_name->text()+ ".hww");
+    if (file.open(QIODevice::WriteOnly)) {
+        QTextStream stream( &file );
+        stream << stateFull << endl;
+    }
     emit weaponsEdited(curWeaponsName, m_name->text(), stateFull);
 }
 
@@ -293,6 +328,7 @@
     {
         isDeleting = true;
         wconf->remove(delWeaponsName);
+        QFile(cfgdir->absolutePath() + "/Weapon Settings/" + curWeaponsName + ".hww").remove();
         emit weaponsDeleted(delWeaponsName);
     }
 }