2018-02-11 05:10:01 +05:30
|
|
|
/* Copyright 2015-2018 MultiMC Contributors
|
2015-08-19 04:40:17 +05:30
|
|
|
*
|
|
|
|
* 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 <QDir>
|
|
|
|
#include <QString>
|
2015-09-07 03:05:58 +05:30
|
|
|
#include <QDebug>
|
2015-09-15 03:19:32 +05:30
|
|
|
#include <QSaveFile>
|
2015-08-19 04:40:17 +05:30
|
|
|
#include "World.h"
|
|
|
|
|
2015-09-07 03:05:58 +05:30
|
|
|
#include "GZip.h"
|
2015-09-10 03:23:33 +05:30
|
|
|
#include <MMCZip.h>
|
2015-10-05 05:17:27 +05:30
|
|
|
#include <FileSystem.h>
|
2015-09-07 03:05:58 +05:30
|
|
|
#include <sstream>
|
|
|
|
#include <io/stream_reader.h>
|
|
|
|
#include <tag_string.h>
|
|
|
|
#include <tag_primitive.h>
|
2015-09-10 03:23:33 +05:30
|
|
|
#include <quazip.h>
|
|
|
|
#include <quazipfile.h>
|
2015-09-13 07:51:26 +05:30
|
|
|
#include <quazipdir.h>
|
2015-09-07 03:05:58 +05:30
|
|
|
|
2015-09-15 03:19:32 +05:30
|
|
|
std::unique_ptr <nbt::tag_compound> parseLevelDat(QByteArray data)
|
|
|
|
{
|
|
|
|
QByteArray output;
|
|
|
|
if(!GZip::unzip(data, output))
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
std::istringstream foo(std::string(output.constData(), output.size()));
|
|
|
|
auto pair = nbt::io::read_compound(foo);
|
|
|
|
|
|
|
|
if(pair.first != "")
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if(pair.second == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return std::move(pair.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray serializeLevelDat(nbt::tag_compound * levelInfo)
|
|
|
|
{
|
|
|
|
std::ostringstream s;
|
|
|
|
nbt::io::write_tag("", *levelInfo, s);
|
|
|
|
QByteArray val( s.str().data(), (int) s.str().size() );
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString getLevelDatFromFS(const QFileInfo &file)
|
|
|
|
{
|
|
|
|
QDir worldDir(file.filePath());
|
|
|
|
if(!file.isDir() || !worldDir.exists("level.dat"))
|
|
|
|
{
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
return worldDir.absoluteFilePath("level.dat");
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray getLevelDatDataFromFS(const QFileInfo &file)
|
|
|
|
{
|
|
|
|
auto fullFilePath = getLevelDatFromFS(file);
|
|
|
|
if(fullFilePath.isNull())
|
|
|
|
{
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
QFile f(fullFilePath);
|
|
|
|
if(!f.open(QIODevice::ReadOnly))
|
|
|
|
{
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
return f.readAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool putLevelDatDataToFS(const QFileInfo &file, QByteArray & data)
|
|
|
|
{
|
|
|
|
auto fullFilePath = getLevelDatFromFS(file);
|
|
|
|
if(fullFilePath.isNull())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
QSaveFile f(fullFilePath);
|
|
|
|
if(!f.open(QIODevice::WriteOnly))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
QByteArray compressed;
|
|
|
|
if(!GZip::zip(data, compressed))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(f.write(compressed) != compressed.size())
|
|
|
|
{
|
|
|
|
f.cancelWriting();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return f.commit();
|
|
|
|
}
|
|
|
|
|
2015-08-19 04:40:17 +05:30
|
|
|
World::World(const QFileInfo &file)
|
|
|
|
{
|
|
|
|
repath(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
void World::repath(const QFileInfo &file)
|
|
|
|
{
|
2015-09-10 03:23:33 +05:30
|
|
|
m_containerFile = file;
|
2015-09-07 03:05:58 +05:30
|
|
|
m_folderName = file.fileName();
|
2015-09-10 03:23:33 +05:30
|
|
|
if(file.isFile() && file.suffix() == "zip")
|
|
|
|
{
|
|
|
|
readFromZip(file);
|
|
|
|
}
|
|
|
|
else if(file.isDir())
|
|
|
|
{
|
|
|
|
readFromFS(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void World::readFromFS(const QFileInfo &file)
|
|
|
|
{
|
2015-09-15 03:19:32 +05:30
|
|
|
auto bytes = getLevelDatDataFromFS(file);
|
|
|
|
if(bytes.isEmpty())
|
2015-09-07 03:05:58 +05:30
|
|
|
{
|
2015-09-15 03:19:32 +05:30
|
|
|
is_valid = false;
|
2015-09-07 03:05:58 +05:30
|
|
|
return;
|
|
|
|
}
|
2015-09-15 03:19:32 +05:30
|
|
|
loadFromLevelDat(bytes);
|
|
|
|
levelDatTime = file.lastModified();
|
2015-09-10 03:23:33 +05:30
|
|
|
}
|
2015-09-07 03:05:58 +05:30
|
|
|
|
2015-09-10 03:23:33 +05:30
|
|
|
void World::readFromZip(const QFileInfo &file)
|
|
|
|
{
|
|
|
|
QuaZip zip(file.absoluteFilePath());
|
|
|
|
is_valid = zip.open(QuaZip::mdUnzip);
|
|
|
|
if (!is_valid)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2017-09-04 11:47:25 +05:30
|
|
|
auto location = MMCZip::findFolderOfFileInZip(&zip, "level.dat");
|
2015-09-13 07:51:26 +05:30
|
|
|
is_valid = !location.isEmpty();
|
|
|
|
if (!is_valid)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_containerOffsetPath = location;
|
2015-09-10 03:23:33 +05:30
|
|
|
QuaZipFile zippedFile(&zip);
|
|
|
|
// read the install profile
|
2015-09-13 07:51:26 +05:30
|
|
|
is_valid = zip.setCurrentFile(location + "level.dat");
|
2015-09-10 03:23:33 +05:30
|
|
|
if (!is_valid)
|
2015-09-07 03:05:58 +05:30
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-09-10 03:23:33 +05:30
|
|
|
is_valid = zippedFile.open(QIODevice::ReadOnly);
|
|
|
|
QuaZipFileInfo64 levelDatInfo;
|
|
|
|
zippedFile.getFileInfo(&levelDatInfo);
|
|
|
|
auto modTime = levelDatInfo.getNTFSmTime();
|
|
|
|
if(!modTime.isValid())
|
|
|
|
{
|
|
|
|
modTime = levelDatInfo.dateTime;
|
|
|
|
}
|
|
|
|
levelDatTime = modTime;
|
|
|
|
if (!is_valid)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-09-15 03:19:32 +05:30
|
|
|
loadFromLevelDat(zippedFile.readAll());
|
2015-09-10 03:23:33 +05:30
|
|
|
zippedFile.close();
|
|
|
|
}
|
2015-09-07 03:05:58 +05:30
|
|
|
|
2015-09-15 03:19:32 +05:30
|
|
|
bool World::install(const QString &to, const QString &name)
|
2015-09-10 03:23:33 +05:30
|
|
|
{
|
2015-10-05 05:17:27 +05:30
|
|
|
auto finalPath = FS::PathCombine(to, FS::DirNameFromString(m_actualName, to));
|
|
|
|
if(!FS::ensureFolderPathExists(finalPath))
|
2015-09-07 03:05:58 +05:30
|
|
|
{
|
2015-09-10 03:23:33 +05:30
|
|
|
return false;
|
|
|
|
}
|
2015-09-15 03:19:32 +05:30
|
|
|
bool ok = false;
|
2015-09-10 03:23:33 +05:30
|
|
|
if(m_containerFile.isFile())
|
|
|
|
{
|
2015-09-13 07:51:26 +05:30
|
|
|
QuaZip zip(m_containerFile.absoluteFilePath());
|
|
|
|
if (!zip.open(QuaZip::mdUnzip))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-09-15 03:19:32 +05:30
|
|
|
ok = !MMCZip::extractSubDir(&zip, m_containerOffsetPath, finalPath).isEmpty();
|
2015-09-10 03:23:33 +05:30
|
|
|
}
|
|
|
|
else if(m_containerFile.isDir())
|
|
|
|
{
|
|
|
|
QString from = m_containerFile.filePath();
|
2015-10-10 09:25:55 +05:30
|
|
|
ok = FS::copy(from, finalPath)();
|
2015-09-15 03:19:32 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if(ok && !name.isEmpty() && m_actualName != name)
|
|
|
|
{
|
|
|
|
World newWorld(finalPath);
|
|
|
|
if(newWorld.isValid())
|
|
|
|
{
|
|
|
|
newWorld.rename(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool World::rename(const QString &newName)
|
|
|
|
{
|
|
|
|
if(m_containerFile.isFile())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto data = getLevelDatDataFromFS(m_containerFile);
|
|
|
|
if(data.isEmpty())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto worldData = parseLevelDat(data);
|
|
|
|
if(!worldData)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto &val = worldData->at("Data");
|
|
|
|
if(val.get_type() != nbt::tag_type::Compound)
|
|
|
|
{
|
|
|
|
return false;
|
2015-09-10 03:23:33 +05:30
|
|
|
}
|
2015-09-15 03:19:32 +05:30
|
|
|
auto &dataCompound = val.as<nbt::tag_compound>();
|
|
|
|
dataCompound.put("LevelName", nbt::value_initializer(newName.toUtf8().data()));
|
|
|
|
data = serializeLevelDat(worldData.get());
|
|
|
|
|
|
|
|
putLevelDatDataToFS(m_containerFile, data);
|
|
|
|
|
|
|
|
m_actualName = newName;
|
|
|
|
|
|
|
|
QDir parentDir(m_containerFile.absoluteFilePath());
|
|
|
|
parentDir.cdUp();
|
|
|
|
QFile container(m_containerFile.absoluteFilePath());
|
2015-10-05 05:17:27 +05:30
|
|
|
auto dirName = FS::DirNameFromString(m_actualName, parentDir.absolutePath());
|
2015-09-15 03:19:32 +05:30
|
|
|
container.rename(parentDir.absoluteFilePath(dirName));
|
|
|
|
|
|
|
|
return true;
|
2015-09-10 03:23:33 +05:30
|
|
|
}
|
2015-09-07 03:05:58 +05:30
|
|
|
|
2015-09-10 03:23:33 +05:30
|
|
|
static QString read_string (nbt::value& parent, const char * name, const QString & fallback = QString())
|
|
|
|
{
|
|
|
|
try
|
2015-09-07 03:05:58 +05:30
|
|
|
{
|
2015-09-10 03:23:33 +05:30
|
|
|
auto &namedValue = parent.at(name);
|
|
|
|
if(namedValue.get_type() != nbt::tag_type::String)
|
2015-09-07 03:05:58 +05:30
|
|
|
{
|
2015-09-08 12:58:14 +05:30
|
|
|
return fallback;
|
|
|
|
}
|
2015-09-10 03:23:33 +05:30
|
|
|
auto & tag_str = namedValue.as<nbt::tag_string>();
|
|
|
|
return QString::fromStdString(tag_str.get());
|
|
|
|
}
|
|
|
|
catch(std::out_of_range e)
|
|
|
|
{
|
|
|
|
// fallback for old world formats
|
|
|
|
qWarning() << "String NBT tag" << name << "could not be found. Defaulting to" << fallback;
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
catch(std::bad_cast e)
|
|
|
|
{
|
|
|
|
// type mismatch
|
|
|
|
qWarning() << "NBT tag" << name << "could not be converted to string. Defaulting to" << fallback;
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static int64_t read_long (nbt::value& parent, const char * name, const int64_t & fallback = 0)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto &namedValue = parent.at(name);
|
|
|
|
if(namedValue.get_type() != nbt::tag_type::Long)
|
2015-09-08 12:58:14 +05:30
|
|
|
{
|
2015-09-07 03:05:58 +05:30
|
|
|
return fallback;
|
|
|
|
}
|
2015-09-10 03:23:33 +05:30
|
|
|
auto & tag_str = namedValue.as<nbt::tag_long>();
|
|
|
|
return tag_str.get();
|
|
|
|
}
|
|
|
|
catch(std::out_of_range e)
|
|
|
|
{
|
|
|
|
// fallback for old world formats
|
|
|
|
qWarning() << "Long NBT tag" << name << "could not be found. Defaulting to" << fallback;
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
catch(std::bad_cast e)
|
|
|
|
{
|
|
|
|
// type mismatch
|
|
|
|
qWarning() << "NBT tag" << name << "could not be converted to long. Defaulting to" << fallback;
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-09-15 03:19:32 +05:30
|
|
|
void World::loadFromLevelDat(QByteArray data)
|
2015-09-10 03:23:33 +05:30
|
|
|
{
|
2015-09-07 03:05:58 +05:30
|
|
|
try
|
|
|
|
{
|
2015-09-15 03:19:32 +05:30
|
|
|
auto levelData = parseLevelDat(data);
|
|
|
|
if(!levelData)
|
2015-09-07 03:05:58 +05:30
|
|
|
{
|
2015-09-15 03:19:32 +05:30
|
|
|
is_valid = false;
|
2015-09-07 03:05:58 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-15 03:19:32 +05:30
|
|
|
auto &val = levelData->at("Data");
|
2015-09-07 03:05:58 +05:30
|
|
|
is_valid = val.get_type() == nbt::tag_type::Compound;
|
|
|
|
if(!is_valid)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_actualName = read_string(val, "LevelName", m_folderName);
|
|
|
|
|
|
|
|
|
|
|
|
int64_t temp = read_long(val, "LastPlayed", 0);
|
|
|
|
if(temp == 0)
|
|
|
|
{
|
2015-09-10 03:23:33 +05:30
|
|
|
m_lastPlayed = levelDatTime;
|
2015-09-07 03:05:58 +05:30
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_lastPlayed = QDateTime::fromMSecsSinceEpoch(temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_randomSeed = read_long(val, "RandomSeed", 0);
|
|
|
|
|
|
|
|
qDebug() << "World Name:" << m_actualName;
|
|
|
|
qDebug() << "Last Played:" << m_lastPlayed.toString();
|
|
|
|
qDebug() << "Seed:" << m_randomSeed;
|
|
|
|
}
|
|
|
|
catch (nbt::io::input_error e)
|
|
|
|
{
|
|
|
|
qWarning() << "Unable to load" << m_folderName << ":" << e.what();
|
|
|
|
is_valid = false;
|
|
|
|
return;
|
|
|
|
}
|
2015-08-19 04:40:17 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
bool World::replace(World &with)
|
|
|
|
{
|
|
|
|
if (!destroy())
|
|
|
|
return false;
|
2015-10-10 09:25:55 +05:30
|
|
|
bool success = FS::copy(with.m_containerFile.filePath(), m_containerFile.path())();
|
2015-08-19 04:40:17 +05:30
|
|
|
if (success)
|
|
|
|
{
|
2015-09-07 03:05:58 +05:30
|
|
|
m_folderName = with.m_folderName;
|
2015-09-10 03:23:33 +05:30
|
|
|
m_containerFile.refresh();
|
2015-08-19 04:40:17 +05:30
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool World::destroy()
|
|
|
|
{
|
|
|
|
if(!is_valid) return false;
|
2015-09-10 03:23:33 +05:30
|
|
|
if (m_containerFile.isDir())
|
2015-08-19 04:40:17 +05:30
|
|
|
{
|
2015-09-10 03:23:33 +05:30
|
|
|
QDir d(m_containerFile.filePath());
|
|
|
|
return d.removeRecursively();
|
2015-08-19 04:40:17 +05:30
|
|
|
}
|
2015-09-10 03:23:33 +05:30
|
|
|
else if(m_containerFile.isFile())
|
2015-08-19 04:40:17 +05:30
|
|
|
{
|
2015-09-10 03:23:33 +05:30
|
|
|
QFile file(m_containerFile.absoluteFilePath());
|
|
|
|
return file.remove();
|
2015-08-19 04:40:17 +05:30
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool World::operator==(const World &other) const
|
|
|
|
{
|
2015-09-07 03:05:58 +05:30
|
|
|
return is_valid == other.is_valid && folderName() == other.folderName();
|
2015-08-19 04:40:17 +05:30
|
|
|
}
|
|
|
|
bool World::strongCompare(const World &other) const
|
|
|
|
{
|
2015-09-07 03:05:58 +05:30
|
|
|
return is_valid == other.is_valid && folderName() == other.folderName();
|
2015-08-19 04:40:17 +05:30
|
|
|
}
|