From e0b901169aaa5ebc7b74c15cd3f01f08ee98c4f1 Mon Sep 17 00:00:00 2001 From: Trial97 Date: Thu, 15 Jun 2023 00:27:20 +0300 Subject: [PATCH] Added new migration for special characters Signed-off-by: Trial97 --- launcher/settings/INIFile.cpp | 19 ++++++++-- tests/INIFile_test.cpp | 67 +++++++++++++++++++++++++++++++++-- 2 files changed, 82 insertions(+), 4 deletions(-) diff --git a/launcher/settings/INIFile.cpp b/launcher/settings/INIFile.cpp index cb909ae75..1d2f9611f 100644 --- a/launcher/settings/INIFile.cpp +++ b/launcher/settings/INIFile.cpp @@ -50,7 +50,7 @@ INIFile::INIFile() {} bool INIFile::saveFile(QString fileName) { if (!contains("ConfigVersion")) - insert("ConfigVersion", "1.1"); + insert("ConfigVersion", "1.2"); QSettings _settings_obj{ fileName, QSettings::Format::IniFormat }; _settings_obj.setFallbacksEnabled(false); @@ -125,6 +125,10 @@ bool parseOldFileFormat(QIODevice& device, QSettings::SettingsMap& map) QString valueStr = line.right(line.length() - eqPos - 1).trimmed(); valueStr = unescape(valueStr); + if ((valueStr.contains(QChar(';')) || valueStr.contains(QChar('=')) || valueStr.contains(QChar(','))) && valueStr.endsWith("\"") && + valueStr.startsWith("\"")) { + valueStr = valueStr.removeFirst().removeLast(); + } QVariant value(valueStr); map.insert(key, value); @@ -154,7 +158,18 @@ bool INIFile::loadFile(QString fileName) file.close(); for (auto&& key : map.keys()) insert(key, map.value(key)); - insert("ConfigVersion", "1.1"); + insert("ConfigVersion", "1.2"); + } else if (_settings_obj.value("ConfigVersion").toString() == "1.1") { + for (auto&& key : _settings_obj.allKeys()) { + if (auto valueStr = _settings_obj.value(key).toString(); + (valueStr.contains(QChar(';')) || valueStr.contains(QChar('=')) || valueStr.contains(QChar(','))) && + valueStr.endsWith("\"") && valueStr.startsWith("\"")) { + valueStr = valueStr.removeFirst().removeLast(); + insert(key, valueStr); + } else + insert(key, _settings_obj.value(key)); + } + insert("ConfigVersion", "1.2"); } else for (auto&& key : _settings_obj.allKeys()) insert(key, _settings_obj.value(key)); diff --git a/tests/INIFile_test.cpp b/tests/INIFile_test.cpp index 2f49e573d..5d9c9cd8a 100644 --- a/tests/INIFile_test.cpp +++ b/tests/INIFile_test.cpp @@ -2,7 +2,9 @@ #include #include +#include #include +#include "FileSystem.h" #include @@ -80,7 +82,10 @@ iconKey=vanillia_icon name=Minecraft Vanillia OverrideCommands=true PreLaunchCommand="$INST_JAVA" -jar packwiz-installer-bootstrap.jar link -)"; +Wrapperommand=)"; + fileContent += "\""; + fileContent += +R"(\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar link =)"; + fileContent += "\"\n"; QFile file(fileName); if (file.open(QFile::WriteOnly | QFile::Text)) { @@ -93,11 +98,69 @@ PreLaunchCommand="$INST_JAVA" -jar packwiz-installer-bootstrap.jar link INIFile f1; f1.loadFile(fileName); QCOMPARE(f1.get("PreLaunchCommand", "NOT SET").toString(), "\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar link"); + QCOMPARE(f1.get("Wrapperommand", "NOT SET").toString(), "\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar link ="); f1.saveFile(fileName); INIFile f2; f2.loadFile(fileName); QCOMPARE(f2.get("PreLaunchCommand", "NOT SET").toString(), "\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar link"); - QCOMPARE(f2.get("ConfigVersion", "NOT SET").toString(), "1.1"); + QCOMPARE(f2.get("Wrapperommand", "NOT SET").toString(), "\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar link ="); + QCOMPARE(f2.get("ConfigVersion", "NOT SET").toString(), "1.2"); + } + + void test_SaveAleardyExistingFileWithSpecialChars() + { + QString fileName = "test_SaveAleardyExistingFileWithSpecialChars.ini"; + FS::deletePath(fileName); // just to clean the previous test run + QSettings settings{ fileName, QSettings::Format::IniFormat }; + settings.setFallbacksEnabled(false); + + settings.setValue("simple", "value1"); + settings.setValue("withQuotes", R"("value2" with quotes)"); + settings.setValue("withSpecialCharacters", "env mesa=true"); + settings.setValue("withSpecialCharacters2", "1,2,3,4"); + settings.setValue("withSpecialCharacters2", "1;2;3;4"); + settings.setValue("withAll", "val=\"$INST_JAVA\" -jar; ls "); + + settings.sync(); + + QCOMPARE(settings.status(), QSettings::Status::NoError); + + // load + INIFile f1; + f1.loadFile(fileName); + for (auto key : settings.allKeys()) + QCOMPARE(f1.get(key, "NOT SET").toString(), settings.value(key).toString()); + f1.saveFile(fileName); + INIFile f2; + f2.loadFile(fileName); + for (auto key : settings.allKeys()) + QCOMPARE(f2.get(key, "NOT SET").toString(), settings.value(key).toString()); + QCOMPARE(f2.get("ConfigVersion", "NOT SET").toString(), "1.2"); + } + + void test_SaveAleardyExistingFileWithSpecialCharsV1() + { + QString fileName = "test_SaveAleardyExistingFileWithSpecialCharsV1.ini"; + QString fileContent = R"(InstanceType=OneSix +ConfigVersion=1.1 +iconKey=vanillia_icon +name=Minecraft Vanillia +OverrideCommands=true +PreLaunchCommand=)"; + fileContent += "\"\\\"env mesa=true\\\"\"\n"; + QFile file(fileName); + + if (file.open(QFile::WriteOnly | QFile::Text)) { + QTextStream stream(&file); + stream << fileContent; + file.close(); + } + + // load + INIFile f1; + f1.loadFile(fileName); + QCOMPARE(f1.get("PreLaunchCommand", "NOT SET").toString(), "env mesa=true"); + QCOMPARE(f1.get("ConfigVersion", "NOT SET").toString(), "1.2"); } };