From 2fe27fd0daca14644a2a71456cd60c9a38befbdf Mon Sep 17 00:00:00 2001 From: Sky Date: Sun, 8 Dec 2013 06:12:53 +0000 Subject: [PATCH] More work on new assets system. Works given a properly constructed assets folder, no downloading yet --- logic/OneSixInstance.cpp | 65 ++++++++++++++++++++- logic/OneSixInstance.h | 2 + logic/OneSixVersion.cpp | 16 ++++- logic/OneSixVersion.h | 2 + logic/assets/AssetsIndex.cpp | 11 ++++ logic/assets/AssetsIndex.h | 5 +- logic/assets/AssetsUtils.cpp | 109 +++++++++++++++++++++++++++++++++++ logic/assets/AssetsUtils.h | 3 + 8 files changed, 210 insertions(+), 3 deletions(-) diff --git a/logic/OneSixInstance.cpp b/logic/OneSixInstance.cpp index 08b63bf9b..a6b439a13 100644 --- a/logic/OneSixInstance.cpp +++ b/logic/OneSixInstance.cpp @@ -26,6 +26,8 @@ #include #include "gui/dialogs/OneSixModEditDialog.h" #include "logger/QsLog.h" +#include "logic/assets/AssetsIndex.h" +#include "logic/assets/AssetsUtils.h" OneSixInstance::OneSixInstance(const QString &rootDir, SettingsObject *setting_obj, QObject *parent) @@ -66,6 +68,63 @@ QString replaceTokensIn(QString text, QMap with) return result; } +QDir OneSixInstance::reconstructAssets(std::shared_ptr version) +{ + QDir assetsDir = QDir("assets/"); + QDir indexDir = QDir(PathCombine(assetsDir.path(), "indexes")); + QDir objectDir = QDir(PathCombine(assetsDir.path(), "objects")); + QDir virtualDir = QDir(PathCombine(assetsDir.path(), "virtual")); + + QString indexPath = PathCombine(indexDir.path(), version->assets + ".json"); + QFile indexFile(indexPath); + QDir virtualRoot(PathCombine(virtualDir.path(), version->assets)); + + if(!indexFile.exists()) + { + QLOG_ERROR() << "No assets index file" << indexPath << "; can't reconstruct assets"; + return virtualRoot; + } + + QLOG_DEBUG() << "reconstructAssets" << assetsDir.path() << indexDir.path() << objectDir.path() << virtualDir.path() << virtualRoot.path(); + + AssetsIndex index; + bool loadAssetsIndex = AssetsUtils::loadAssetsIndexJson(indexPath, &index); + + if(loadAssetsIndex) + { + if(index.isVirtual) + { + QLOG_INFO() << "Reconstructing virtual assets folder at" << virtualRoot.path(); + + for(QString map : index.objects->keys()) + { + AssetObject asset_object = index.objects->value(map); + QString target_path = PathCombine(virtualRoot.path(), map); + QFile target(target_path); + + QString tlk = asset_object.hash.left(2); + + QString original_path = PathCombine(PathCombine(objectDir.path(), tlk), asset_object.hash); + QFile original(original_path); + if(!target.exists()) + { + QFileInfo info(target_path); + QDir target_dir = info.dir(); + //QLOG_DEBUG() << target_dir; + if(!target_dir.exists()) QDir("").mkpath(target_dir.path()); + + bool couldCopy = original.copy(target_path); + QLOG_DEBUG() << " Copying" << original_path << "to" << target_path << QString::number(couldCopy);// << original.errorString(); + } + } + + // TODO: Write last used time to virtualRoot/.lastused + } + } + + return virtualRoot; +} + QStringList OneSixInstance::processMinecraftArgs(MojangAccountPtr account) { I_D(OneSixInstance); @@ -93,10 +152,14 @@ QStringList OneSixInstance::processMinecraftArgs(MojangAccountPtr account) QString absRootDir = QDir(minecraftRoot()).absolutePath(); token_mapping["game_directory"] = absRootDir; QString absAssetsDir = QDir("assets/").absolutePath(); - token_mapping["game_assets"] = absAssetsDir; + token_mapping["game_assets"] = reconstructAssets(d->version).absolutePath(); //TODO: this is something new and not even fully implemented in the vanilla launcher. token_mapping["user_properties"] = "{ }"; + // 1.7.3+ assets tokens + token_mapping["assets_root"] = absAssetsDir; + token_mapping["assets_index_name"] = version->assets; + QStringList parts = args_pattern.split(' ', QString::SkipEmptyParts); for (int i = 0; i < parts.length(); i++) { diff --git a/logic/OneSixInstance.h b/logic/OneSixInstance.h index 042c104b4..cc98d822f 100644 --- a/logic/OneSixInstance.h +++ b/logic/OneSixInstance.h @@ -16,6 +16,7 @@ #pragma once #include +#include #include "BaseInstance.h" @@ -73,4 +74,5 @@ public: private: QStringList processMinecraftArgs(MojangAccountPtr account); + QDir reconstructAssets(std::shared_ptr version); }; diff --git a/logic/OneSixVersion.cpp b/logic/OneSixVersion.cpp index cc5b1de12..e586402b8 100644 --- a/logic/OneSixVersion.cpp +++ b/logic/OneSixVersion.cpp @@ -17,6 +17,8 @@ #include "logic/OneSixLibrary.h" #include "logic/OneSixRule.h" +#include "logger/QsLog.h" + std::shared_ptr fromJsonV4(QJsonObject root, std::shared_ptr fullVersion) { @@ -60,6 +62,18 @@ std::shared_ptr fromJsonV4(QJsonObject root, fullVersion->releaseTime = root.value("releaseTime").toString(); fullVersion->time = root.value("time").toString(); + auto assetsID = root.value("assets"); + if (assetsID.isString()) + { + fullVersion->assets = assetsID.toString(); + } + else + { + fullVersion->assets = "legacy"; + } + + QLOG_DEBUG() << "Assets version:" << fullVersion->assets; + // Iterate through the list, if it's a list. auto librariesValue = root.value("libraries"); if (!librariesValue.isArray()) @@ -151,7 +165,7 @@ std::shared_ptr OneSixVersion::fromJson(QJsonObject root) root.value("minimumLauncherVersion").toDouble(); // ADD MORE HERE :D - if (launcher_ver > 0 && launcher_ver <= 11) + if (launcher_ver > 0 && launcher_ver <= 12) return fromJsonV4(root, readVersion); else { diff --git a/logic/OneSixVersion.h b/logic/OneSixVersion.h index 5718dafe8..036f3d53a 100644 --- a/logic/OneSixVersion.h +++ b/logic/OneSixVersion.h @@ -56,6 +56,8 @@ public: QString releaseTime; /// Release type - "release" or "snapshot" QString type; + /// Assets type - "legacy" or a version ID + QString assets; /** * DEPRECATED: Old versions of the new vanilla launcher used this * ex: "username_session_version" diff --git a/logic/assets/AssetsIndex.cpp b/logic/assets/AssetsIndex.cpp index 5b401a773..509f1add8 100644 --- a/logic/assets/AssetsIndex.cpp +++ b/logic/assets/AssetsIndex.cpp @@ -15,6 +15,17 @@ #include "AssetsIndex.h" +AssetsIndex::AssetsIndex() +{ + // TODO: leak? + this->objects = new QMap(); + this->isVirtual = false; +} + AssetObject::AssetObject(QString hash, qint64 size) : hash(hash), size(size) { } + +AssetObject::AssetObject() +{ +} diff --git a/logic/assets/AssetsIndex.h b/logic/assets/AssetsIndex.h index 019265f93..09ef0d925 100644 --- a/logic/assets/AssetsIndex.h +++ b/logic/assets/AssetsIndex.h @@ -23,14 +23,17 @@ class AssetObject; class AssetsIndex { public: - QMap objects; + QMap *objects; bool isVirtual; + + AssetsIndex(); }; class AssetObject { public: AssetObject(QString hash, qint64 size); + AssetObject(); bool equals(AssetObject* other); QString getHashCode(); diff --git a/logic/assets/AssetsUtils.cpp b/logic/assets/AssetsUtils.cpp index 3589161aa..8d7d6f468 100644 --- a/logic/assets/AssetsUtils.cpp +++ b/logic/assets/AssetsUtils.cpp @@ -16,6 +16,9 @@ #include #include #include +#include +#include +#include #include "AssetsUtils.h" #include "MultiMC.h" @@ -112,4 +115,110 @@ void migrateOldAssets() } } } + +/* + * Returns true on success, with index populated + * index is undefined otherwise + */ +bool loadAssetsIndexJson(QString path, AssetsIndex *index) +{ +/* +{ + "objects": { + "icons/icon_16x16.png": { + "hash": "bdf48ef6b5d0d23bbb02e17d04865216179f510a", + "size": 3665 + }, + ... + } + } +} +*/ + + QFile file(path); + + // Try to open the file and fail if we can't. + // TODO: We should probably report this error to the user. + if (!file.open(QIODevice::ReadOnly)) + { + QLOG_ERROR() << "Failed to read assets index file" << path; + return false; + } + + // Read the file and close it. + QByteArray jsonData = file.readAll(); + file.close(); + + QJsonParseError parseError; + QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData, &parseError); + + // Fail if the JSON is invalid. + if (parseError.error != QJsonParseError::NoError) + { + QLOG_ERROR() << "Failed to parse assets index file:" << parseError.errorString() << "at offset " << QString::number(parseError.offset); + return false; + } + + // Make sure the root is an object. + if (!jsonDoc.isObject()) + { + QLOG_ERROR() << "Invalid assets index JSON: Root should be an array."; + return false; + } + + QJsonObject root = jsonDoc.object(); + + QJsonValue isVirtual = root.value("virtual"); + if(!isVirtual.isUndefined()) + { + index->isVirtual = isVirtual.toBool(false); + } + + QJsonValue objects = root.value("objects"); + QVariantMap map = objects.toVariant().toMap(); + + for(QVariantMap::const_iterator iter = map.begin(); iter != map.end(); ++iter) { + //QLOG_DEBUG() << iter.key(); + + QVariant variant = iter.value(); + QVariantMap nested_objects = variant.toMap(); + + AssetObject object; + + for(QVariantMap::const_iterator nested_iter = nested_objects.begin(); nested_iter != nested_objects.end(); ++nested_iter) { + //QLOG_DEBUG() << nested_iter.key() << nested_iter.value().toString(); + QString key = nested_iter.key(); + QVariant value = nested_iter.value(); + + if(key == "hash") + { + object.hash = value.toString(); + } + else if(key == "size") + { + object.size = value.toDouble(); + } + } + + index->objects->insert(iter.key(), object); + } + + return true; + /*for (QJsonValue accountVal : objects) + { + QJsonObject accountObj = accountVal.toObject(); + MojangAccountPtr account = MojangAccount::loadFromJson(accountObj); + if (account.get() != nullptr) + { + connect(account.get(), SIGNAL(changed()), SLOT(accountChanged())); + m_accounts.append(account); + } + else + { + QLOG_WARN() << "Failed to load an account."; + } + }*/ + + //return false; +} } diff --git a/logic/assets/AssetsUtils.h b/logic/assets/AssetsUtils.h index 80b0d6762..88a3b0dc3 100644 --- a/logic/assets/AssetsUtils.h +++ b/logic/assets/AssetsUtils.h @@ -15,7 +15,10 @@ #pragma once +#include "AssetsIndex.h" + namespace AssetsUtils { void migrateOldAssets(); +bool loadAssetsIndexJson(QString file, AssetsIndex* index); }