From e0b901169aaa5ebc7b74c15cd3f01f08ee98c4f1 Mon Sep 17 00:00:00 2001 From: Trial97 Date: Thu, 15 Jun 2023 00:27:20 +0300 Subject: [PATCH 1/5] 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"); } }; From cf4c1605ebe2f1db302928f847398e23236aed16 Mon Sep 17 00:00:00 2001 From: Trial97 Date: Thu, 15 Jun 2023 00:37:32 +0300 Subject: [PATCH 2/5] Fixed qt5 build Signed-off-by: Trial97 --- launcher/settings/INIFile.cpp | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/launcher/settings/INIFile.cpp b/launcher/settings/INIFile.cpp index 1d2f9611f..37846d112 100644 --- a/launcher/settings/INIFile.cpp +++ b/launcher/settings/INIFile.cpp @@ -97,6 +97,20 @@ QString unescape(QString orig) } return out; } + +QString unquete(QString str) +{ + if ((str.contains(QChar(';')) || str.contains(QChar('=')) || str.contains(QChar(','))) && str.endsWith("\"") && str.startsWith("\"")) { +#if QT_VERSION <= QT_VERSION_CHECK(6, 0, 0) + str = str.remove(0, 1); + str = str.remove(str.size() - 1, 1); +#else + str = str.removeFirst().removeLast(); +#endif + } + return str; +} + bool parseOldFileFormat(QIODevice& device, QSettings::SettingsMap& map) { QTextStream in(device.readAll()); @@ -124,11 +138,7 @@ bool parseOldFileFormat(QIODevice& device, QSettings::SettingsMap& map) QString key = line.left(eqPos).trimmed(); 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(); - } + valueStr = unquete(unescape(valueStr)); QVariant value(valueStr); map.insert(key, value); @@ -164,8 +174,7 @@ bool INIFile::loadFile(QString fileName) 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); + insert(key, unquete(valueStr)); } else insert(key, _settings_obj.value(key)); } From 811c79423fa048c5f4dd567b1121630d16aa3b0c Mon Sep 17 00:00:00 2001 From: Trial97 Date: Thu, 15 Jun 2023 00:43:05 +0300 Subject: [PATCH 3/5] Fixed version comparation Signed-off-by: Trial97 --- launcher/settings/INIFile.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/launcher/settings/INIFile.cpp b/launcher/settings/INIFile.cpp index 37846d112..ad600ab91 100644 --- a/launcher/settings/INIFile.cpp +++ b/launcher/settings/INIFile.cpp @@ -101,7 +101,7 @@ QString unescape(QString orig) QString unquete(QString str) { if ((str.contains(QChar(';')) || str.contains(QChar('=')) || str.contains(QChar(','))) && str.endsWith("\"") && str.startsWith("\"")) { -#if QT_VERSION <= QT_VERSION_CHECK(6, 0, 0) +#if QT_VERSION < QT_VERSION_CHECK(6, 5, 0) str = str.remove(0, 1); str = str.remove(str.size() - 1, 1); #else From 1b42b9a08e5777fcf0d2578f3ffa05e354e47f9a Mon Sep 17 00:00:00 2001 From: Trial97 Date: Thu, 15 Jun 2023 14:25:58 +0300 Subject: [PATCH 4/5] Fixed tests Signed-off-by: Trial97 --- launcher/settings/INIFile.cpp | 6 +++--- tests/INIFile_test.cpp | 39 ++++++++++++++++++----------------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/launcher/settings/INIFile.cpp b/launcher/settings/INIFile.cpp index ad600ab91..d16256b96 100644 --- a/launcher/settings/INIFile.cpp +++ b/launcher/settings/INIFile.cpp @@ -98,7 +98,7 @@ QString unescape(QString orig) return out; } -QString unquete(QString str) +QString unquote(QString str) { if ((str.contains(QChar(';')) || str.contains(QChar('=')) || str.contains(QChar(','))) && str.endsWith("\"") && str.startsWith("\"")) { #if QT_VERSION < QT_VERSION_CHECK(6, 5, 0) @@ -138,7 +138,7 @@ bool parseOldFileFormat(QIODevice& device, QSettings::SettingsMap& map) QString key = line.left(eqPos).trimmed(); QString valueStr = line.right(line.length() - eqPos - 1).trimmed(); - valueStr = unquete(unescape(valueStr)); + valueStr = unquote(unescape(valueStr)); QVariant value(valueStr); map.insert(key, value); @@ -174,7 +174,7 @@ bool INIFile::loadFile(QString fileName) if (auto valueStr = _settings_obj.value(key).toString(); (valueStr.contains(QChar(';')) || valueStr.contains(QChar('=')) || valueStr.contains(QChar(','))) && valueStr.endsWith("\"") && valueStr.startsWith("\"")) { - insert(key, unquete(valueStr)); + insert(key, unquote(valueStr)); } else insert(key, _settings_obj.value(key)); } diff --git a/tests/INIFile_test.cpp b/tests/INIFile_test.cpp index 5d9c9cd8a..eaf077d43 100644 --- a/tests/INIFile_test.cpp +++ b/tests/INIFile_test.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include "FileSystem.h" @@ -74,9 +75,8 @@ class IniFileTest : public QObject { QCOMPARE(out_list_numbers, list_numbers); } - void test_SaveAleardyExistingFile() + void test_SaveAlreadyExistingFile() { - QString fileName = "test_SaveAleardyExistingFile.ini"; QString fileContent = R"(InstanceType=OneSix iconKey=vanillia_icon name=Minecraft Vanillia @@ -86,9 +86,10 @@ Wrapperommand=)"; fileContent += "\""; fileContent += +R"(\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar link =)"; fileContent += "\"\n"; - QFile file(fileName); + QTemporaryFile file; - if (file.open(QFile::WriteOnly | QFile::Text)) { + // QFile file(fileName); + if (file.open()) { QTextStream stream(&file); stream << fileContent; file.close(); @@ -96,22 +97,23 @@ Wrapperommand=)"; // load INIFile f1; - f1.loadFile(fileName); + f1.loadFile(file.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); + f1.saveFile(file.fileName()); INIFile f2; - f2.loadFile(fileName); + f2.loadFile(file.fileName()); QCOMPARE(f2.get("PreLaunchCommand", "NOT SET").toString(), "\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar link"); 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() + void test_SaveAlreadyExistingFileWithSpecialChars() { - QString fileName = "test_SaveAleardyExistingFileWithSpecialChars.ini"; - FS::deletePath(fileName); // just to clean the previous test run - QSettings settings{ fileName, QSettings::Format::IniFormat }; + QTemporaryFile file; + file.open(); + file.close(); + QSettings settings{ file.fileName(), QSettings::Format::IniFormat }; settings.setFallbacksEnabled(false); settings.setValue("simple", "value1"); @@ -127,20 +129,20 @@ Wrapperommand=)"; // load INIFile f1; - f1.loadFile(fileName); + f1.loadFile(file.fileName()); for (auto key : settings.allKeys()) QCOMPARE(f1.get(key, "NOT SET").toString(), settings.value(key).toString()); - f1.saveFile(fileName); + f1.saveFile(file.fileName()); INIFile f2; - f2.loadFile(fileName); + f2.loadFile(file.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() + void test_SaveAlreadyExistingFileWithSpecialCharsV1() { - QString fileName = "test_SaveAleardyExistingFileWithSpecialCharsV1.ini"; + QTemporaryFile file; QString fileContent = R"(InstanceType=OneSix ConfigVersion=1.1 iconKey=vanillia_icon @@ -148,9 +150,8 @@ name=Minecraft Vanillia OverrideCommands=true PreLaunchCommand=)"; fileContent += "\"\\\"env mesa=true\\\"\"\n"; - QFile file(fileName); - if (file.open(QFile::WriteOnly | QFile::Text)) { + if (file.open()) { QTextStream stream(&file); stream << fileContent; file.close(); @@ -158,7 +159,7 @@ PreLaunchCommand=)"; // load INIFile f1; - f1.loadFile(fileName); + f1.loadFile(file.fileName()); QCOMPARE(f1.get("PreLaunchCommand", "NOT SET").toString(), "env mesa=true"); QCOMPARE(f1.get("ConfigVersion", "NOT SET").toString(), "1.2"); } From 1d354df1f81e3c7f7f82d17bf3fd261eb2d98ee4 Mon Sep 17 00:00:00 2001 From: Trial97 Date: Thu, 15 Jun 2023 16:51:58 +0300 Subject: [PATCH 5/5] Fix tests for window Signed-off-by: Trial97 --- tests/INIFile_test.cpp | 71 +++++++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 22 deletions(-) diff --git a/tests/INIFile_test.cpp b/tests/INIFile_test.cpp index eaf077d43..95730e244 100644 --- a/tests/INIFile_test.cpp +++ b/tests/INIFile_test.cpp @@ -86,34 +86,48 @@ Wrapperommand=)"; fileContent += "\""; fileContent += +R"(\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar link =)"; fileContent += "\"\n"; +#if defined(Q_OS_WIN) + QString fileName = "test_SaveAlreadyExistingFile.ini"; + QFile file(fileName); + QCOMPARE(file.open(QFile::WriteOnly | QFile::Text), true); +#else QTemporaryFile file; - - // QFile file(fileName); - if (file.open()) { - QTextStream stream(&file); - stream << fileContent; - file.close(); - } + QCOMPARE(file.open(), true); + QCOMPARE(file.fileName().isEmpty(), false); + QString fileName = file.fileName(); +#endif + QTextStream stream(&file); + stream << fileContent; + file.close(); // load INIFile f1; - f1.loadFile(file.fileName()); + 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(file.fileName()); + f1.saveFile(fileName); INIFile f2; - f2.loadFile(file.fileName()); + f2.loadFile(fileName); QCOMPARE(f2.get("PreLaunchCommand", "NOT SET").toString(), "\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar link"); QCOMPARE(f2.get("Wrapperommand", "NOT SET").toString(), "\"$INST_JAVA\" -jar packwiz-installer-bootstrap.jar link ="); QCOMPARE(f2.get("ConfigVersion", "NOT SET").toString(), "1.2"); +#if defined(Q_OS_WIN) + FS::deletePath(fileName); +#endif } void test_SaveAlreadyExistingFileWithSpecialChars() { +#if defined(Q_OS_WIN) + QString fileName = "test_SaveAlreadyExistingFileWithSpecialChars.ini"; +#else QTemporaryFile file; - file.open(); + QCOMPARE(file.open(), true); + QCOMPARE(file.fileName().isEmpty(), false); + QString fileName = file.fileName(); file.close(); - QSettings settings{ file.fileName(), QSettings::Format::IniFormat }; +#endif + QSettings settings{ fileName, QSettings::Format::IniFormat }; settings.setFallbacksEnabled(false); settings.setValue("simple", "value1"); @@ -129,20 +143,22 @@ Wrapperommand=)"; // load INIFile f1; - f1.loadFile(file.fileName()); + f1.loadFile(fileName); for (auto key : settings.allKeys()) QCOMPARE(f1.get(key, "NOT SET").toString(), settings.value(key).toString()); - f1.saveFile(file.fileName()); + f1.saveFile(fileName); INIFile f2; - f2.loadFile(file.fileName()); + 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"); +#if defined(Q_OS_WIN) + FS::deletePath(fileName); +#endif } void test_SaveAlreadyExistingFileWithSpecialCharsV1() { - QTemporaryFile file; QString fileContent = R"(InstanceType=OneSix ConfigVersion=1.1 iconKey=vanillia_icon @@ -151,17 +167,28 @@ OverrideCommands=true PreLaunchCommand=)"; fileContent += "\"\\\"env mesa=true\\\"\"\n"; - if (file.open()) { - QTextStream stream(&file); - stream << fileContent; - file.close(); - } +#if defined(Q_OS_WIN) + QString fileName = "test_SaveAlreadyExistingFileWithSpecialCharsV1.ini"; + QFile file(fileName); + QCOMPARE(file.open(QFile::WriteOnly | QFile::Text), true); +#else + QTemporaryFile file; + QCOMPARE(file.open(), true); + QCOMPARE(file.fileName().isEmpty(), false); + QString fileName = file.fileName(); +#endif + QTextStream stream(&file); + stream << fileContent; + file.close(); // load INIFile f1; - f1.loadFile(file.fileName()); + f1.loadFile(fileName); QCOMPARE(f1.get("PreLaunchCommand", "NOT SET").toString(), "env mesa=true"); QCOMPARE(f1.get("ConfigVersion", "NOT SET").toString(), "1.2"); +#if defined(Q_OS_WIN) + FS::deletePath(fileName); +#endif } };