From ec6204e447fd9e88b4cf2a628ed220f93f271fd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sat, 19 Mar 2016 03:06:32 +0100 Subject: [PATCH] NOISSUE share logic for new attributes between mojang and onesix format --- logic/minecraft/MinecraftVersion.h | 2 +- logic/minecraft/MinecraftVersionList.cpp | 2 +- logic/minecraft/MojangVersionFormat.cpp | 187 +++++++++--------- logic/minecraft/MojangVersionFormat.h | 8 +- logic/minecraft/ProfilePatch.h | 5 +- logic/minecraft/VersionFile.h | 4 + logic/minecraft/forge/ForgeInstaller.cpp | 2 +- .../onesix/OneSixProfileStrategy.cpp | 11 +- .../minecraft/onesix/OneSixVersionFormat.cpp | 85 +------- logic/minecraft/onesix/OneSixVersionFormat.h | 2 +- tests/tst_MojangVersionFormat.cpp | 4 +- 11 files changed, 131 insertions(+), 181 deletions(-) diff --git a/logic/minecraft/MinecraftVersion.h b/logic/minecraft/MinecraftVersion.h index 2c1eb289..1539ffd8 100644 --- a/logic/minecraft/MinecraftVersion.h +++ b/logic/minecraft/MinecraftVersion.h @@ -76,7 +76,7 @@ public: /* methods */ return true; } - VersionFilePtr getVersionFile(); + virtual VersionFilePtr getVersionFile() override; // virtual QJsonDocument toJson(bool saveOrder) override; diff --git a/logic/minecraft/MinecraftVersionList.cpp b/logic/minecraft/MinecraftVersionList.cpp index f219c782..62c588d1 100644 --- a/logic/minecraft/MinecraftVersionList.cpp +++ b/logic/minecraft/MinecraftVersionList.cpp @@ -508,7 +508,7 @@ void MCVListVersionUpdateTask::json_downloaded() file->fileId = "net.minecraft"; // now dump the file to disk - auto doc = OneSixVersionFormat::profilePatchToJson(file, false); + auto doc = OneSixVersionFormat::versionFileToJson(file, false); auto newdata = doc.toBinaryData(); auto id = updatedVersion->descriptor(); QString targetPath = "versions/" + id + "/" + id + ".dat"; diff --git a/logic/minecraft/MojangVersionFormat.cpp b/logic/minecraft/MojangVersionFormat.cpp index a832e8c7..78447aa8 100644 --- a/logic/minecraft/MojangVersionFormat.cpp +++ b/logic/minecraft/MojangVersionFormat.cpp @@ -128,6 +128,68 @@ QJsonObject assetIndexToJson(MojangAssetIndexInfo::Ptr info) return out; } +void MojangVersionFormat::readVersionProperties(const QJsonObject &in, VersionFile *out) +{ + Bits::readString(in, "id", out->id); + Bits::readString(in, "mainClass", out->mainClass); + Bits::readString(in, "minecraftArguments", out->minecraftArguments); + if(out->minecraftArguments.isEmpty()) + { + QString processArguments; + Bits::readString(in, "processArguments", processArguments); + QString toCompare = processArguments.toLower(); + if (toCompare == "legacy") + { + out->minecraftArguments = " ${auth_player_name} ${auth_session}"; + } + else if (toCompare == "username_session") + { + out->minecraftArguments = "--username ${auth_player_name} --session ${auth_session}"; + } + else if (toCompare == "username_session_version") + { + out->minecraftArguments = "--username ${auth_player_name} --session ${auth_session} --version ${profile_name}"; + } + else if (!toCompare.isEmpty()) + { + out->addProblem(PROBLEM_ERROR, QObject::tr("processArguments is set to unknown value '%1'").arg(processArguments)); + } + } + Bits::readString(in, "type", out->type); + + if(in.contains("assetIndex")) + { + out->mojangAssetIndex = assetIndexFromJson(requireObject(in, "assetIndex")); + } + Bits::readString(in, "assets", out->assets); + + out->m_releaseTime = timeFromS3Time(in.value("releaseTime").toString("")); + out->m_updateTime = timeFromS3Time(in.value("time").toString("")); + + if (in.contains("minimumLauncherVersion")) + { + out->minimumLauncherVersion = requireInteger(in.value("minimumLauncherVersion")); + if (out->minimumLauncherVersion > CURRENT_MINIMUM_LAUNCHER_VERSION) + { + out->addProblem( + PROBLEM_WARNING, + QObject::tr("The 'minimumLauncherVersion' value of this version (%1) is higher than supported by MultiMC (%2). It might not work properly!") + .arg(out->minimumLauncherVersion) + .arg(CURRENT_MINIMUM_LAUNCHER_VERSION)); + } + } + if(in.contains("downloads")) + { + auto downloadsObj = requireObject(in, "downloads"); + for(auto iter = downloadsObj.begin(); iter != downloadsObj.end(); iter++) + { + auto classifier = iter.key(); + auto classifierObj = requireObject(iter.value()); + out->mojangDownloads[classifier] = downloadInfoFromJson(classifierObj); + } + } +} + VersionFilePtr MojangVersionFormat::versionFileFromJson(const QJsonDocument &doc, const QString &filename) { VersionFilePtr out(new VersionFile()); @@ -142,38 +204,13 @@ VersionFilePtr MojangVersionFormat::versionFileFromJson(const QJsonDocument &doc QJsonObject root = doc.object(); + readVersionProperties(root, out.get()); + out->name = "Minecraft"; out->fileId = "net.minecraft"; - out->version = root.value("version").toString(); + out->version = out->id; out->filename = filename; - Bits::readString(root, "id", out->id); - - Bits::readString(root, "mainClass", out->mainClass); - Bits::readString(root, "minecraftArguments", out->minecraftArguments); - Bits::readString(root, "type", out->type); - - if(root.contains("assetIndex")) - { - out->mojangAssetIndex = assetIndexFromJson(requireObject(root, "assetIndex")); - } - Bits::readString(root, "assets", out->assets); - - out->m_releaseTime = timeFromS3Time(root.value("releaseTime").toString("")); - out->m_updateTime = timeFromS3Time(root.value("time").toString("")); - - if (root.contains("minimumLauncherVersion")) - { - out->minimumLauncherVersion = requireInteger(root.value("minimumLauncherVersion")); - if (out->minimumLauncherVersion > CURRENT_MINIMUM_LAUNCHER_VERSION) - { - out->addProblem( - PROBLEM_WARNING, - QObject::tr("The 'minimumLauncherVersion' value of this version (%1) is higher than supported by MultiMC (%2). It might not work properly!") - .arg(out->minimumLauncherVersion) - .arg(CURRENT_MINIMUM_LAUNCHER_VERSION)); - } - } if (root.contains("libraries")) { @@ -185,34 +222,41 @@ VersionFilePtr MojangVersionFormat::versionFileFromJson(const QJsonDocument &doc out->libraries.append(lib); } } - if(root.contains("downloads")) - { - auto downloadsObj = requireObject(root, "downloads"); - for(auto iter = downloadsObj.begin(); iter != downloadsObj.end(); iter++) - { - auto classifier = iter.key(); - auto classifierObj = requireObject(iter.value()); - out->mojangDownloads[classifier] = downloadInfoFromJson(classifierObj); - } - } return out; } -QJsonDocument versionFileToJson(VersionFilePtr patch) +void MojangVersionFormat::writeVersionProperties(const VersionFile* in, QJsonObject& out) +{ + writeString(out, "id", in->id); + writeString(out, "mainClass", in->mainClass); + writeString(out, "minecraftArguments", in->minecraftArguments); + writeString(out, "type", in->type); + writeString(out, "assets", in->assets); + writeString(out, "releaseTime", timeToS3Time(in->m_releaseTime)); + writeString(out, "time", timeToS3Time(in->m_updateTime)); + if(in->minimumLauncherVersion != -1) + { + out.insert("minimumLauncherVersion", in->minimumLauncherVersion); + } + if(in->mojangAssetIndex && in->mojangAssetIndex->known) + { + out.insert("assetIndex", assetIndexToJson(in->mojangAssetIndex)); + } + if(in->mojangDownloads.size()) + { + QJsonObject downloadsOut; + for(auto iter = in->mojangDownloads.begin(); iter != in->mojangDownloads.end(); iter++) + { + downloadsOut.insert(iter.key(), downloadInfoToJson(iter.value())); + } + out.insert("downloads", downloadsOut); + } +} + +QJsonDocument MojangVersionFormat::versionFileToJson(const VersionFilePtr &patch) { QJsonObject root; - writeString(root, "id", patch->id); - writeString(root, "mainClass", patch->mainClass); - writeString(root, "minecraftArguments", patch->minecraftArguments); - writeString(root, "type", patch->type); - writeString(root, "assets", patch->assets); - writeString(root, "releaseTime", timeToS3Time(patch->m_releaseTime)); - writeString(root, "time", timeToS3Time(patch->m_updateTime)); - if(patch->minimumLauncherVersion != -1) - { - root.insert("minimumLauncherVersion", patch->minimumLauncherVersion); - } - + writeVersionProperties(patch.get(), root); if (!patch->libraries.isEmpty()) { QJsonArray array; @@ -222,19 +266,7 @@ QJsonDocument versionFileToJson(VersionFilePtr patch) } root.insert("libraries", array); } - if(patch->mojangAssetIndex && patch->mojangAssetIndex->known) - { - root.insert("assetIndex", assetIndexToJson(patch->mojangAssetIndex)); - } - if(patch->mojangDownloads.size()) - { - QJsonObject downloadsOut; - for(auto iter = patch->mojangDownloads.begin(); iter != patch->mojangDownloads.end(); iter++) - { - downloadsOut.insert(iter.key(), downloadInfoToJson(iter.value())); - } - root.insert("downloads", downloadsOut); - } + // write the contents to a json document. { QJsonDocument out; @@ -243,33 +275,6 @@ QJsonDocument versionFileToJson(VersionFilePtr patch) } } -static QJsonDocument minecraftVersionToJson(MinecraftVersionPtr patch) -{ - if(patch->getVersionSource() == Local && patch->getVersionFile()) - { - return MojangVersionFormat::profilePatchToJson(patch->getVersionFile()); - } - else - { - throw VersionIncomplete(QObject::tr("Can't write incomplete/builtin Minecraft version %1").arg(patch->name())); - } -} - -QJsonDocument MojangVersionFormat::profilePatchToJson(const ProfilePatchPtr &patch) -{ - auto vfile = std::dynamic_pointer_cast(patch); - if(vfile) - { - return versionFileToJson(vfile); - } - auto mversion = std::dynamic_pointer_cast(patch); - if(mversion) - { - return minecraftVersionToJson(mversion); - } - throw VersionIncomplete(QObject::tr("Unhandled object type while processing %1").arg(patch->getName())); -} - LibraryPtr MojangVersionFormat::libraryFromJson(const QJsonObject &libObj, const QString &filename) { LibraryPtr out(new Library()); diff --git a/logic/minecraft/MojangVersionFormat.h b/logic/minecraft/MojangVersionFormat.h index a5a94095..4e141088 100644 --- a/logic/minecraft/MojangVersionFormat.h +++ b/logic/minecraft/MojangVersionFormat.h @@ -8,10 +8,16 @@ class MULTIMC_LOGIC_EXPORT MojangVersionFormat { +friend class OneSixVersionFormat; +protected: + // does not include libraries + static void readVersionProperties(const QJsonObject& in, VersionFile* out); + // does not include libraries + static void writeVersionProperties(const VersionFile* in, QJsonObject& out); public: // version files / profile patches static VersionFilePtr versionFileFromJson(const QJsonDocument &doc, const QString &filename); - static QJsonDocument profilePatchToJson(const ProfilePatchPtr &patch); + static QJsonDocument versionFileToJson(const VersionFilePtr &patch); // libraries static LibraryPtr libraryFromJson(const QJsonObject &libObj, const QString &filename); diff --git a/logic/minecraft/ProfilePatch.h b/logic/minecraft/ProfilePatch.h index 56bc58a7..fa2f0a0f 100644 --- a/logic/minecraft/ProfilePatch.h +++ b/logic/minecraft/ProfilePatch.h @@ -3,6 +3,7 @@ #include #include #include +#include #include "JarMod.h" class MinecraftProfile; @@ -43,7 +44,7 @@ private: QString m_description; }; -class ProfilePatch +class ProfilePatch : public std::enable_shared_from_this { public: virtual ~ProfilePatch(){}; @@ -73,6 +74,8 @@ public: virtual VersionSource getVersionSource() = 0; + virtual std::shared_ptr getVersionFile() = 0; + virtual const QList& getProblems() { return m_problems; diff --git a/logic/minecraft/VersionFile.h b/logic/minecraft/VersionFile.h index 49c55646..7627cc96 100644 --- a/logic/minecraft/VersionFile.h +++ b/logic/minecraft/VersionFile.h @@ -63,6 +63,10 @@ public: /* methods */ return Local; } + std::shared_ptr getVersionFile() override + { + return std::dynamic_pointer_cast(shared_from_this()); + } virtual bool isCustom() override { diff --git a/logic/minecraft/forge/ForgeInstaller.cpp b/logic/minecraft/forge/ForgeInstaller.cpp index 86ba6176..7957de0e 100644 --- a/logic/minecraft/forge/ForgeInstaller.cpp +++ b/logic/minecraft/forge/ForgeInstaller.cpp @@ -275,7 +275,7 @@ bool ForgeInstaller::add(OneSixInstance *to) << "for reading:" << file.errorString(); return false; } - file.write(OneSixVersionFormat::profilePatchToJson(m_forge_json, true).toJson()); + file.write(OneSixVersionFormat::versionFileToJson(m_forge_json, true).toJson()); file.commit(); return true; diff --git a/logic/minecraft/onesix/OneSixProfileStrategy.cpp b/logic/minecraft/onesix/OneSixProfileStrategy.cpp index 3ae055c0..aaaa9d97 100644 --- a/logic/minecraft/onesix/OneSixProfileStrategy.cpp +++ b/logic/minecraft/onesix/OneSixProfileStrategy.cpp @@ -56,7 +56,7 @@ void OneSixProfileStrategy::upgradeDeprecatedFiles() file->fileId = "net.minecraft"; file->version = file->id; file->name = "Minecraft"; - auto data = OneSixVersionFormat::profilePatchToJson(file, false).toJson(); + auto data = OneSixVersionFormat::versionFileToJson(file, false).toJson(); QSaveFile newPatchFile(mcJson); if(!newPatchFile.open(QIODevice::WriteOnly)) { @@ -299,7 +299,12 @@ bool OneSixProfileStrategy::customizePatch(ProfilePatchPtr patch) { return false; } - auto document = OneSixVersionFormat::profilePatchToJson(patch, true); + auto vfile = patch->getVersionFile(); + if(!vfile) + { + return false; + } + auto document = OneSixVersionFormat::versionFileToJson(vfile, true); jsonFile.write(document.toJson()); if(!jsonFile.commit()) { @@ -402,7 +407,7 @@ bool OneSixProfileStrategy::installJarMods(QStringList filepaths) << "for reading:" << file.errorString(); return false; } - file.write(OneSixVersionFormat::profilePatchToJson(f, true).toJson()); + file.write(OneSixVersionFormat::versionFileToJson(f, true).toJson()); file.close(); profile->appendPatch(f); } diff --git a/logic/minecraft/onesix/OneSixVersionFormat.cpp b/logic/minecraft/onesix/OneSixVersionFormat.cpp index e262cecd..d4b372c3 100644 --- a/logic/minecraft/onesix/OneSixVersionFormat.cpp +++ b/logic/minecraft/onesix/OneSixVersionFormat.cpp @@ -76,40 +76,10 @@ VersionFilePtr OneSixVersionFormat::versionFileFromJson(const QJsonDocument &doc out->mcVersion = root.value("mcVersion").toString(); out->filename = filename; - readString(root, "id", out->id); + MojangVersionFormat::readVersionProperties(root, out.get()); - readString(root, "mainClass", out->mainClass); + // added for legacy Minecraft window embedding, TODO: remove readString(root, "appletClass", out->appletClass); - readString(root, "minecraftArguments", out->minecraftArguments); - if(out->minecraftArguments.isEmpty()) - { - QString processArguments; - readString(root, "processArguments", processArguments); - QString toCompare = processArguments.toLower(); - if (toCompare == "legacy") - { - out->minecraftArguments = " ${auth_player_name} ${auth_session}"; - } - else if (toCompare == "username_session") - { - out->minecraftArguments = "--username ${auth_player_name} --session ${auth_session}"; - } - else if (toCompare == "username_session_version") - { - out->minecraftArguments = "--username ${auth_player_name} --session ${auth_session} --version ${profile_name}"; - } - else if (!toCompare.isEmpty()) - { - out->addProblem(PROBLEM_ERROR, QObject::tr("processArguments is set to unknown value '%1'").arg(processArguments)); - } - } - - readString(root, "type", out->type); - - out->m_releaseTime = timeFromS3Time(readStringRet(root, "releaseTime")); - out->m_updateTime = timeFromS3Time(readStringRet(root, "time")); - - readString(root, "assets", out->assets); if (root.contains("+tweakers")) { @@ -196,16 +166,7 @@ VersionFilePtr OneSixVersionFormat::versionFileFromJson(const QJsonDocument &doc return out; } -template -struct libraryConversion -{ - static QJsonObject convert(std::shared_ptr &value) - { - return OneSixVersionFormat::libraryToJson(value.get()); - } -}; - -static QJsonDocument versionFileToJson(VersionFilePtr patch, bool saveOrder) +QJsonDocument OneSixVersionFormat::versionFileToJson(const VersionFilePtr &patch, bool saveOrder) { QJsonObject root; if (saveOrder) @@ -216,17 +177,10 @@ static QJsonDocument versionFileToJson(VersionFilePtr patch, bool saveOrder) writeString(root, "fileId", patch->fileId); writeString(root, "version", patch->version); writeString(root, "mcVersion", patch->mcVersion); - writeString(root, "id", patch->id); - writeString(root, "mainClass", patch->mainClass); + + MojangVersionFormat::writeVersionProperties(patch.get(), root); + writeString(root, "appletClass", patch->appletClass); - writeString(root, "minecraftArguments", patch->minecraftArguments); - writeString(root, "type", patch->type); - writeString(root, "assets", patch->assets); - if (patch->isMinecraftVersion()) - { - writeString(root, "releaseTime", timeToS3Time(patch->m_releaseTime)); - writeString(root, "time", timeToS3Time(patch->m_updateTime)); - } writeStringList(root, "+tweakers", patch->addTweakers); writeStringList(root, "+traits", patch->traits.toList()); if (!patch->libraries.isEmpty()) @@ -255,33 +209,6 @@ static QJsonDocument versionFileToJson(VersionFilePtr patch, bool saveOrder) } } -static QJsonDocument minecraftVersionToJson(MinecraftVersionPtr patch, bool saveOrder) -{ - if(patch->getVersionSource() == Local && patch->getVersionFile()) - { - return OneSixVersionFormat::profilePatchToJson(patch->getVersionFile(), saveOrder); - } - else - { - throw VersionIncomplete(QObject::tr("Can't write incomplete/builtin Minecraft version %1").arg(patch->name())); - } -} - -QJsonDocument OneSixVersionFormat::profilePatchToJson(const ProfilePatchPtr &patch, bool saveOrder) -{ - auto vfile = std::dynamic_pointer_cast(patch); - if(vfile) - { - return versionFileToJson(vfile, saveOrder); - } - auto mversion = std::dynamic_pointer_cast(patch); - if(mversion) - { - return minecraftVersionToJson(mversion, saveOrder); - } - throw VersionIncomplete(QObject::tr("Unhandled object type while processing %1").arg(patch->getName())); -} - JarmodPtr OneSixVersionFormat::jarModFromJson(const QJsonObject &libObj, const QString &filename, const QString &originalName) { JarmodPtr out(new Jarmod()); diff --git a/logic/minecraft/onesix/OneSixVersionFormat.h b/logic/minecraft/onesix/OneSixVersionFormat.h index 3011688c..5696e79e 100644 --- a/logic/minecraft/onesix/OneSixVersionFormat.h +++ b/logic/minecraft/onesix/OneSixVersionFormat.h @@ -10,7 +10,7 @@ class OneSixVersionFormat public: // version files / profile patches static VersionFilePtr versionFileFromJson(const QJsonDocument &doc, const QString &filename, const bool requireOrder); - static QJsonDocument profilePatchToJson(const ProfilePatchPtr &patch, bool saveOrder); + static QJsonDocument versionFileToJson(const VersionFilePtr &patch, bool saveOrder); // libraries static LibraryPtr libraryFromJson(const QJsonObject &libObj, const QString &filename); diff --git a/tests/tst_MojangVersionFormat.cpp b/tests/tst_MojangVersionFormat.cpp index a9a3f977..2e7b1cb8 100644 --- a/tests/tst_MojangVersionFormat.cpp +++ b/tests/tst_MojangVersionFormat.cpp @@ -33,7 +33,7 @@ slots: QJsonDocument doc = readJson("tests/data/1.9-simple.json"); auto vfile = MojangVersionFormat::versionFileFromJson(doc, "1.9-simple.json"); - auto doc2 = MojangVersionFormat::profilePatchToJson(vfile); + auto doc2 = MojangVersionFormat::versionFileToJson(vfile); writeJson("1.9-simple-passthorugh.json", doc2); QCOMPARE(doc, doc2); } @@ -43,7 +43,7 @@ slots: QJsonDocument doc = readJson("tests/data/1.9.json"); auto vfile = MojangVersionFormat::versionFileFromJson(doc, "1.9.json"); - auto doc2 = MojangVersionFormat::profilePatchToJson(vfile); + auto doc2 = MojangVersionFormat::versionFileToJson(vfile); writeJson("1.9-passthorugh.json", doc2); QCOMPARE(doc, doc2); }