2022-12-25 04:28:24 +05:30
|
|
|
|
|
|
|
// SPDX-FileCopyrightText: 2022 Rachel Powers <508861+Ryex@users.noreply.github.com>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prism Launcher - Minecraft Launcher
|
|
|
|
* Copyright (C) 2022 Rachel Powers <508861+Ryex@users.noreply.github.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "LocalWorldSaveParseTask.h"
|
|
|
|
|
|
|
|
#include "FileSystem.h"
|
|
|
|
|
|
|
|
#include <quazip/quazip.h>
|
|
|
|
#include <quazip/quazipdir.h>
|
2022-12-27 02:59:13 +05:30
|
|
|
#include <quazip/quazipfile.h>
|
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
#include <QFileInfo>
|
2022-12-25 04:28:24 +05:30
|
|
|
|
|
|
|
namespace WorldSaveUtils {
|
|
|
|
|
|
|
|
bool process(WorldSave& pack, ProcessingLevel level)
|
|
|
|
{
|
|
|
|
switch (pack.type()) {
|
|
|
|
case ResourceType::FOLDER:
|
|
|
|
return WorldSaveUtils::processFolder(pack, level);
|
|
|
|
case ResourceType::ZIPFILE:
|
|
|
|
return WorldSaveUtils::processZIP(pack, level);
|
|
|
|
default:
|
2022-12-27 02:59:13 +05:30
|
|
|
qWarning() << "Invalid type for world save parse task!";
|
2022-12-25 04:28:24 +05:30
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-27 02:59:13 +05:30
|
|
|
/// @brief checks a folder structure to see if it contains a level.dat
|
|
|
|
/// @param dir the path to check
|
|
|
|
/// @param saves used in recursive call if a "saves" dir was found
|
|
|
|
/// @return std::tuple of (
|
|
|
|
/// bool <found level.dat>,
|
|
|
|
/// QString <name of folder containing level.dat>,
|
|
|
|
/// bool <saves folder found>
|
|
|
|
/// )
|
2022-12-25 04:28:24 +05:30
|
|
|
static std::tuple<bool, QString, bool> contains_level_dat(QDir dir, bool saves = false)
|
|
|
|
{
|
2022-12-27 02:59:13 +05:30
|
|
|
for (auto const& entry : dir.entryInfoList()) {
|
2022-12-25 04:28:24 +05:30
|
|
|
if (!entry.isDir()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!saves && entry.fileName() == "saves") {
|
|
|
|
return contains_level_dat(QDir(entry.filePath()), true);
|
|
|
|
}
|
|
|
|
QFileInfo level_dat(FS::PathCombine(entry.filePath(), "level.dat"));
|
|
|
|
if (level_dat.exists() && level_dat.isFile()) {
|
|
|
|
return std::make_tuple(true, entry.fileName(), saves);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::make_tuple(false, "", saves);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool processFolder(WorldSave& save, ProcessingLevel level)
|
|
|
|
{
|
|
|
|
Q_ASSERT(save.type() == ResourceType::FOLDER);
|
|
|
|
|
2022-12-27 02:59:13 +05:30
|
|
|
auto [found, save_dir_name, found_saves_dir] = contains_level_dat(QDir(save.fileinfo().filePath()));
|
2022-12-25 04:28:24 +05:30
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
save.setSaveDirName(save_dir_name);
|
|
|
|
|
|
|
|
if (found_saves_dir) {
|
|
|
|
save.setSaveFormat(WorldSaveFormat::MULTI);
|
|
|
|
} else {
|
|
|
|
save.setSaveFormat(WorldSaveFormat::SINGLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (level == ProcessingLevel::BasicInfoOnly) {
|
2022-12-27 02:59:13 +05:30
|
|
|
return true; // only need basic info already checked
|
2022-12-25 04:28:24 +05:30
|
|
|
}
|
|
|
|
|
2022-12-27 02:59:13 +05:30
|
|
|
// reserved for more intensive processing
|
|
|
|
|
|
|
|
return true; // all tests passed
|
2022-12-25 04:28:24 +05:30
|
|
|
}
|
|
|
|
|
2022-12-27 02:59:13 +05:30
|
|
|
/// @brief checks a folder structure to see if it contains a level.dat
|
|
|
|
/// @param zip the zip file to check
|
|
|
|
/// @return std::tuple of (
|
|
|
|
/// bool <found level.dat>,
|
|
|
|
/// QString <name of folder containing level.dat>,
|
|
|
|
/// bool <saves folder found>
|
|
|
|
/// )
|
2022-12-25 04:28:24 +05:30
|
|
|
static std::tuple<bool, QString, bool> contains_level_dat(QuaZip& zip)
|
|
|
|
{
|
|
|
|
bool saves = false;
|
|
|
|
QuaZipDir zipDir(&zip);
|
|
|
|
if (zipDir.exists("/saves")) {
|
|
|
|
saves = true;
|
|
|
|
zipDir.cd("/saves");
|
|
|
|
}
|
2022-12-27 02:59:13 +05:30
|
|
|
|
2022-12-25 04:28:24 +05:30
|
|
|
for (auto const& entry : zipDir.entryList()) {
|
|
|
|
zipDir.cd(entry);
|
|
|
|
if (zipDir.exists("level.dat")) {
|
|
|
|
return std::make_tuple(true, entry, saves);
|
|
|
|
}
|
|
|
|
zipDir.cd("..");
|
|
|
|
}
|
|
|
|
return std::make_tuple(false, "", saves);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool processZIP(WorldSave& save, ProcessingLevel level)
|
|
|
|
{
|
|
|
|
Q_ASSERT(save.type() == ResourceType::ZIPFILE);
|
|
|
|
|
|
|
|
QuaZip zip(save.fileinfo().filePath());
|
|
|
|
if (!zip.open(QuaZip::mdUnzip))
|
2022-12-27 02:59:13 +05:30
|
|
|
return false; // can't open zip file
|
2022-12-25 04:28:24 +05:30
|
|
|
|
2022-12-27 02:59:13 +05:30
|
|
|
auto [found, save_dir_name, found_saves_dir] = contains_level_dat(zip);
|
2022-12-25 04:28:24 +05:30
|
|
|
|
2022-12-25 06:13:43 +05:30
|
|
|
if (save_dir_name.endsWith("/")) {
|
|
|
|
save_dir_name.chop(1);
|
|
|
|
}
|
2022-12-27 02:59:13 +05:30
|
|
|
|
2022-12-25 04:28:24 +05:30
|
|
|
if (!found) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
save.setSaveDirName(save_dir_name);
|
|
|
|
|
|
|
|
if (found_saves_dir) {
|
|
|
|
save.setSaveFormat(WorldSaveFormat::MULTI);
|
|
|
|
} else {
|
|
|
|
save.setSaveFormat(WorldSaveFormat::SINGLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (level == ProcessingLevel::BasicInfoOnly) {
|
|
|
|
zip.close();
|
2022-12-27 02:59:13 +05:30
|
|
|
return true; // only need basic info already checked
|
2022-12-25 04:28:24 +05:30
|
|
|
}
|
|
|
|
|
2022-12-27 02:59:13 +05:30
|
|
|
// reserved for more intensive processing
|
2022-12-25 04:28:24 +05:30
|
|
|
|
|
|
|
zip.close();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool validate(QFileInfo file)
|
|
|
|
{
|
|
|
|
WorldSave sp{ file };
|
|
|
|
return WorldSaveUtils::process(sp, ProcessingLevel::BasicInfoOnly) && sp.valid();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace WorldSaveUtils
|
|
|
|
|
2022-12-27 02:59:13 +05:30
|
|
|
LocalWorldSaveParseTask::LocalWorldSaveParseTask(int token, WorldSave& save) : Task(nullptr, false), m_token(token), m_save(save) {}
|
2022-12-25 04:28:24 +05:30
|
|
|
|
|
|
|
bool LocalWorldSaveParseTask::abort()
|
|
|
|
{
|
|
|
|
m_aborted = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalWorldSaveParseTask::executeTask()
|
|
|
|
{
|
|
|
|
if (!WorldSaveUtils::process(m_save))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (m_aborted)
|
|
|
|
emitAborted();
|
|
|
|
else
|
|
|
|
emitSucceeded();
|
|
|
|
}
|