From 6d438b2ef3ca320d89eb5830024d02610ef8f6d6 Mon Sep 17 00:00:00 2001 From: Sky Date: Sun, 8 Dec 2013 02:58:44 +0000 Subject: Work on new assets system. Working legacy assets importer, disabled assets downloading for now --- CMakeLists.txt | 8 +++ gui/MainWindow.cpp | 6 ++- logic/assets/Assets.cpp | 14 ++++++ logic/assets/Assets.h | 16 ++++++ logic/assets/AssetsIndex.cpp | 20 ++++++++ logic/assets/AssetsIndex.h | 39 +++++++++++++++ logic/assets/AssetsUtils.cpp | 115 +++++++++++++++++++++++++++++++++++++++++++ logic/assets/AssetsUtils.h | 21 ++++++++ 8 files changed, 238 insertions(+), 1 deletion(-) create mode 100644 logic/assets/Assets.cpp create mode 100644 logic/assets/Assets.h create mode 100644 logic/assets/AssetsIndex.cpp create mode 100644 logic/assets/AssetsIndex.h create mode 100644 logic/assets/AssetsUtils.cpp create mode 100644 logic/assets/AssetsUtils.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 79a69225..62876595 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -384,6 +384,14 @@ logic/NagUtils.cpp logic/SkinUtils.h logic/SkinUtils.cpp +# Assets +logic/assets/AssetsIndex.h +logic/assets/AssetsIndex.cpp +logic/assets/AssetsUtils.h +logic/assets/AssetsUtils.cpp +logic/assets/Assets.h +logic/assets/Assets.cpp + ) diff --git a/gui/MainWindow.cpp b/gui/MainWindow.cpp index 854091c6..26e7b27b 100644 --- a/gui/MainWindow.cpp +++ b/gui/MainWindow.cpp @@ -84,6 +84,8 @@ #include "logic/LegacyInstance.h" +#include "logic/assets/AssetsUtils.h" + MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { MultiMCPlatform::fixWM_CLASS(this); @@ -243,7 +245,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi SLOT(assetsFilesProgress(int, int, int))); connect(assets_downloader, SIGNAL(failed()), SLOT(assetsFailed())); connect(assets_downloader, SIGNAL(finished()), SLOT(assetsFinished())); - assets_downloader->start(); + //assets_downloader->start(); } const QString currentInstanceId = MMC->settings()->get("SelectedInstance").toString(); @@ -267,6 +269,8 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi // removing this looks stupid view->setFocus(); + + AssetsUtils::migrateOldAssets(); } MainWindow::~MainWindow() diff --git a/logic/assets/Assets.cpp b/logic/assets/Assets.cpp new file mode 100644 index 00000000..a0764674 --- /dev/null +++ b/logic/assets/Assets.cpp @@ -0,0 +1,14 @@ +/* Copyright 2013 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ diff --git a/logic/assets/Assets.h b/logic/assets/Assets.h new file mode 100644 index 00000000..09998ff8 --- /dev/null +++ b/logic/assets/Assets.h @@ -0,0 +1,16 @@ +/* Copyright 2013 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once diff --git a/logic/assets/AssetsIndex.cpp b/logic/assets/AssetsIndex.cpp new file mode 100644 index 00000000..5b401a77 --- /dev/null +++ b/logic/assets/AssetsIndex.cpp @@ -0,0 +1,20 @@ +/* Copyright 2013 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "AssetsIndex.h" + +AssetObject::AssetObject(QString hash, qint64 size) : hash(hash), size(size) +{ +} diff --git a/logic/assets/AssetsIndex.h b/logic/assets/AssetsIndex.h new file mode 100644 index 00000000..019265f9 --- /dev/null +++ b/logic/assets/AssetsIndex.h @@ -0,0 +1,39 @@ +/* Copyright 2013 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include + +class AssetObject; + +class AssetsIndex +{ +public: + QMap objects; + bool isVirtual; +}; + +class AssetObject +{ +public: + AssetObject(QString hash, qint64 size); + bool equals(AssetObject* other); + QString getHashCode(); + + QString hash; + qint64 size; +}; diff --git a/logic/assets/AssetsUtils.cpp b/logic/assets/AssetsUtils.cpp new file mode 100644 index 00000000..3589161a --- /dev/null +++ b/logic/assets/AssetsUtils.cpp @@ -0,0 +1,115 @@ +/* Copyright 2013 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include "AssetsUtils.h" +#include "MultiMC.h" + +namespace AssetsUtils +{ +void migrateOldAssets() +{ + QDir assets_dir("assets"); + if(!assets_dir.exists()) return; + assets_dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot); + int base_length = assets_dir.path().length(); + + QList blacklist = {"indexes", "objects", "virtual"}; + + if(!assets_dir.exists("objects")) assets_dir.mkdir("objects"); + QDir objects_dir("assets/objects"); + + QDirIterator iterator(assets_dir, QDirIterator::Subdirectories); + int successes = 0; + int failures = 0; + while (iterator.hasNext()) { + QString currentDir = iterator.next(); + currentDir = currentDir.remove(0, base_length+1); + + bool ignore = false; + for(QString blacklisted : blacklist) + { + if(currentDir.startsWith(blacklisted)) ignore = true; + } + + if (!iterator.fileInfo().isDir() && !ignore) { + QString filename = iterator.filePath(); + + QFile input(filename); + input.open(QIODevice::ReadOnly | QIODevice::WriteOnly); + QString sha1sum = QCryptographicHash::hash(input.readAll(), QCryptographicHash::Sha1) + .toHex() + .constData(); + + QString object_name = filename.remove(0, base_length+1); + QLOG_DEBUG() << "Processing" << object_name << ":" << sha1sum << input.size(); + + QString object_tlk = sha1sum.left(2); + QString object_tlk_dir = objects_dir.path() + "/" + object_tlk; + + QDir tlk_dir(object_tlk_dir); + if(!tlk_dir.exists()) objects_dir.mkdir(object_tlk); + + QString new_filename = tlk_dir.path() + "/" + sha1sum; + QFile new_object(new_filename); + if(!new_object.exists()) + { + bool rename_success = input.rename(new_filename); + QLOG_DEBUG() << " Doesn't exist, copying to" << new_filename << ":" << QString::number(rename_success); + if(rename_success) successes++; + else failures++; + } + else + { + input.remove(); + QLOG_DEBUG() << " Already exists, deleting original and not copying."; + } + } + } + + if(successes + failures == 0) { + QLOG_DEBUG() << "No legacy assets needed importing."; + } + else + { + QLOG_DEBUG() << "Finished copying legacy assets:" << successes << "successes and" << failures << "failures."; + + QDirIterator cleanup_iterator(assets_dir); + + while (cleanup_iterator.hasNext()) { + QString currentDir = cleanup_iterator.next(); + currentDir = currentDir.remove(0, base_length+1); + + bool ignore = false; + for(QString blacklisted : blacklist) + { + if(currentDir.startsWith(blacklisted)) ignore = true; + } + + if (cleanup_iterator.fileInfo().isDir() && !ignore) { + QString path = cleanup_iterator.filePath(); + QDir folder(path); + + QLOG_DEBUG() << "Cleaning up legacy assets folder:" << path; + + folder.removeRecursively(); + } + } + } +} +} diff --git a/logic/assets/AssetsUtils.h b/logic/assets/AssetsUtils.h new file mode 100644 index 00000000..80b0d676 --- /dev/null +++ b/logic/assets/AssetsUtils.h @@ -0,0 +1,21 @@ +/* Copyright 2013 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +namespace AssetsUtils +{ +void migrateOldAssets(); +} -- cgit v1.2.3 From 2fe27fd0daca14644a2a71456cd60c9a38befbdf Mon Sep 17 00:00:00 2001 From: Sky Date: Sun, 8 Dec 2013 06:12:53 +0000 Subject: 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 08b63bf9..a6b439a1 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 042c104b..cc98d822 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 cc5b1de1..e586402b 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 5718dafe..036f3d53 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 5b401a77..509f1add 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 019265f9..09ef0d92 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 3589161a..8d7d6f46 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 80b0d676..88a3b0dc 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); } -- cgit v1.2.3