2015-08-31 01:06:01 +05:30
|
|
|
/* Copyright 2015 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-08-19 04:40:17 +05:30
|
|
|
#include "World.h"
|
|
|
|
#include <pathutils.h>
|
|
|
|
|
2015-09-07 03:05:58 +05:30
|
|
|
#include "GZip.h"
|
2015-09-10 03:23:33 +05:30
|
|
|
#include <MMCZip.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-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-07 03:05:58 +05:30
|
|
|
QDir worldDir(file.filePath());
|
|
|
|
is_valid = file.isDir() && worldDir.exists("level.dat");
|
|
|
|
if(!is_valid)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto fullFilePath = worldDir.absoluteFilePath("level.dat");
|
|
|
|
QFile f(fullFilePath);
|
|
|
|
is_valid = f.open(QIODevice::ReadOnly);
|
|
|
|
if(!is_valid)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-09-10 03:23:33 +05:30
|
|
|
QFileInfo finfo(fullFilePath);
|
|
|
|
levelDatTime = finfo.lastModified();
|
|
|
|
parseLevelDat(f.readAll());
|
|
|
|
}
|
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;
|
|
|
|
}
|
2015-09-13 07:51:26 +05:30
|
|
|
auto location = MMCZip::findFileInZip(&zip, "level.dat");
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
parseLevelDat(zippedFile.readAll());
|
|
|
|
zippedFile.close();
|
|
|
|
}
|
2015-09-07 03:05:58 +05:30
|
|
|
|
2015-09-10 03:23:33 +05:30
|
|
|
bool World::install(QString to)
|
|
|
|
{
|
|
|
|
auto finalPath = PathCombine(to, DirNameFromString(m_actualName, to));
|
|
|
|
if(!ensureFolderPathExists(finalPath))
|
2015-09-07 03:05:58 +05:30
|
|
|
{
|
2015-09-10 03:23:33 +05:30
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(m_containerFile.isFile())
|
|
|
|
{
|
2015-09-13 07:51:26 +05:30
|
|
|
QuaZip zip(m_containerFile.absoluteFilePath());
|
|
|
|
if (!zip.open(QuaZip::mdUnzip))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return !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();
|
|
|
|
return copyPath(from, finalPath);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void World::parseLevelDat(QByteArray data)
|
|
|
|
{
|
|
|
|
QByteArray output;
|
2015-09-14 06:06:03 +05:30
|
|
|
is_valid = GZip::unzip(data, output);
|
2015-09-10 03:23:33 +05:30
|
|
|
if(!is_valid)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-09-07 03:05:58 +05:30
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
std::istringstream foo(std::string(output.constData(), output.size()));
|
|
|
|
auto pair = nbt::io::read_compound(foo);
|
|
|
|
is_valid = pair.first == "";
|
|
|
|
|
|
|
|
if(!is_valid)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::ostringstream ostr;
|
|
|
|
is_valid = pair.second != nullptr;
|
|
|
|
if(!is_valid)
|
|
|
|
{
|
|
|
|
qDebug() << "FAIL~!!!";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &val = pair.second->at("Data");
|
|
|
|
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-09-10 03:23:33 +05:30
|
|
|
bool success = copyPath(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
|
|
|
}
|