2016-02-28 02:32:56 +05:30
|
|
|
#include "OneSixVersionFormat.h"
|
|
|
|
#include <Json.h>
|
|
|
|
#include "minecraft/ParseUtils.h"
|
2016-02-28 23:31:54 +05:30
|
|
|
#include <minecraft/MinecraftVersion.h>
|
|
|
|
#include <minecraft/VersionBuildError.h>
|
2016-03-07 06:31:28 +05:30
|
|
|
#include <minecraft/MojangVersionFormat.h>
|
2016-02-28 02:32:56 +05:30
|
|
|
|
|
|
|
using namespace Json;
|
|
|
|
|
|
|
|
static void readString(const QJsonObject &root, const QString &key, QString &variable)
|
|
|
|
{
|
|
|
|
if (root.contains(key))
|
|
|
|
{
|
|
|
|
variable = requireString(root.value(key));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static QString readStringRet(const QJsonObject &root, const QString &key)
|
|
|
|
{
|
|
|
|
if (root.contains(key))
|
|
|
|
{
|
|
|
|
return requireString(root.value(key));
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2016-03-07 06:31:28 +05:30
|
|
|
LibraryPtr OneSixVersionFormat::libraryFromJson(const QJsonObject &libObj, const QString &filename)
|
2016-02-28 23:31:54 +05:30
|
|
|
{
|
2016-03-07 06:31:28 +05:30
|
|
|
LibraryPtr out = MojangVersionFormat::libraryFromJson(libObj, filename);
|
2016-02-28 23:31:54 +05:30
|
|
|
readString(libObj, "MMC-hint", out->m_hint);
|
|
|
|
readString(libObj, "MMC-absulute_url", out->m_absolute_url);
|
|
|
|
readString(libObj, "MMC-absoluteUrl", out->m_absolute_url);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2016-03-07 06:31:28 +05:30
|
|
|
QJsonObject OneSixVersionFormat::libraryToJson(Library *library)
|
2016-02-28 23:31:54 +05:30
|
|
|
{
|
2016-03-07 06:31:28 +05:30
|
|
|
QJsonObject libRoot = MojangVersionFormat::libraryToJson(library);
|
2016-02-28 23:31:54 +05:30
|
|
|
if (library->m_absolute_url.size())
|
|
|
|
libRoot.insert("MMC-absoluteUrl", library->m_absolute_url);
|
|
|
|
if (library->m_hint.size())
|
|
|
|
libRoot.insert("MMC-hint", library->m_hint);
|
|
|
|
return libRoot;
|
|
|
|
}
|
|
|
|
|
|
|
|
VersionFilePtr OneSixVersionFormat::versionFileFromJson(const QJsonDocument &doc, const QString &filename, const bool requireOrder)
|
2016-02-28 02:32:56 +05:30
|
|
|
{
|
|
|
|
VersionFilePtr out(new VersionFile());
|
|
|
|
if (doc.isEmpty() || doc.isNull())
|
|
|
|
{
|
|
|
|
throw JSONValidationError(filename + " is empty or null");
|
|
|
|
}
|
|
|
|
if (!doc.isObject())
|
|
|
|
{
|
|
|
|
throw JSONValidationError(filename + " is not an object");
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject root = doc.object();
|
|
|
|
|
|
|
|
if (requireOrder)
|
|
|
|
{
|
|
|
|
if (root.contains("order"))
|
|
|
|
{
|
|
|
|
out->order = requireInteger(root.value("order"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// FIXME: evaluate if we don't want to throw exceptions here instead
|
|
|
|
qCritical() << filename << "doesn't contain an order field";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out->name = root.value("name").toString();
|
|
|
|
out->fileId = root.value("fileId").toString();
|
|
|
|
out->version = root.value("version").toString();
|
|
|
|
out->mcVersion = root.value("mcVersion").toString();
|
|
|
|
out->filename = filename;
|
|
|
|
|
|
|
|
readString(root, "id", out->id);
|
|
|
|
|
|
|
|
readString(root, "mainClass", out->mainClass);
|
|
|
|
readString(root, "appletClass", out->appletClass);
|
2016-03-13 04:53:45 +05:30
|
|
|
readString(root, "minecraftArguments", out->minecraftArguments);
|
|
|
|
if(out->minecraftArguments.isEmpty())
|
|
|
|
{
|
|
|
|
QString processArguments;
|
|
|
|
readString(root, "processArguments", processArguments);
|
|
|
|
QString toCompare = processArguments.toLower();
|
|
|
|
if (toCompare == "legacy")
|
|
|
|
{
|
|
|
|
out->minecraftArguments = " ${auth_player_name} ${auth_session}";
|
|
|
|
}
|
|
|
|
else if (toCompare == "username_session")
|
|
|
|
{
|
|
|
|
out->minecraftArguments = "--username ${auth_player_name} --session ${auth_session}";
|
|
|
|
}
|
|
|
|
else if (toCompare == "username_session_version")
|
|
|
|
{
|
|
|
|
out->minecraftArguments = "--username ${auth_player_name} --session ${auth_session} --version ${profile_name}";
|
|
|
|
}
|
|
|
|
else if (!toCompare.isEmpty())
|
|
|
|
{
|
|
|
|
out->addProblem(PROBLEM_ERROR, QObject::tr("processArguments is set to unknown value '%1'").arg(processArguments));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-28 02:32:56 +05:30
|
|
|
readString(root, "type", out->type);
|
|
|
|
|
2016-03-02 07:33:44 +05:30
|
|
|
out->m_releaseTime = timeFromS3Time(readStringRet(root, "releaseTime"));
|
|
|
|
out->m_updateTime = timeFromS3Time(readStringRet(root, "time"));
|
2016-02-28 02:32:56 +05:30
|
|
|
|
|
|
|
readString(root, "assets", out->assets);
|
|
|
|
|
|
|
|
if (root.contains("+tweakers"))
|
|
|
|
{
|
|
|
|
for (auto tweakerVal : requireArray(root.value("+tweakers")))
|
|
|
|
{
|
|
|
|
out->addTweakers.append(requireString(tweakerVal));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (root.contains("+traits"))
|
|
|
|
{
|
|
|
|
for (auto tweakerVal : requireArray(root.value("+traits")))
|
|
|
|
{
|
|
|
|
out->traits.insert(requireString(tweakerVal));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (root.contains("+jarMods"))
|
|
|
|
{
|
|
|
|
for (auto libVal : requireArray(root.value("+jarMods")))
|
|
|
|
{
|
|
|
|
QJsonObject libObj = requireObject(libVal);
|
|
|
|
// parse the jarmod
|
2016-02-28 23:31:54 +05:30
|
|
|
auto lib = OneSixVersionFormat::jarModFromJson(libObj, filename, out->name);
|
2016-02-28 02:32:56 +05:30
|
|
|
if(lib->originalName.isEmpty())
|
|
|
|
{
|
|
|
|
auto fixed = out->name;
|
|
|
|
fixed.remove(" (jar mod)");
|
|
|
|
lib->originalName = out->name;
|
|
|
|
}
|
|
|
|
// and add to jar mods
|
|
|
|
out->jarMods.append(lib);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-13 04:53:45 +05:30
|
|
|
auto readLibs = [&](const char * which)
|
2016-02-28 02:32:56 +05:30
|
|
|
{
|
2016-03-13 04:53:45 +05:30
|
|
|
for (auto libVal : requireArray(root.value(which)))
|
2016-02-28 02:32:56 +05:30
|
|
|
{
|
|
|
|
QJsonObject libObj = requireObject(libVal);
|
|
|
|
// parse the library
|
2016-02-28 23:31:54 +05:30
|
|
|
auto lib = libraryFromJson(libObj, filename);
|
2016-03-13 06:58:55 +05:30
|
|
|
out->libraries.append(lib);
|
2016-02-28 02:32:56 +05:30
|
|
|
}
|
2016-03-13 04:53:45 +05:30
|
|
|
};
|
|
|
|
bool hasPlusLibs = root.contains("+libraries");
|
|
|
|
bool hasLibs = root.contains("libraries");
|
|
|
|
if (hasPlusLibs && hasLibs)
|
|
|
|
{
|
|
|
|
out->addProblem(PROBLEM_WARNING, QObject::tr("Version file has both '+libraries' and 'libraries'. This is no longer supported."));
|
|
|
|
readLibs("libraries");
|
|
|
|
readLibs("+libraries");
|
|
|
|
}
|
|
|
|
else if (hasLibs)
|
|
|
|
{
|
|
|
|
readLibs("libraries");
|
|
|
|
}
|
|
|
|
else if(hasPlusLibs)
|
|
|
|
{
|
|
|
|
readLibs("+libraries");
|
2016-02-28 02:32:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* removed features that shouldn't be used */
|
2016-03-13 04:53:45 +05:30
|
|
|
if (root.contains("tweakers"))
|
|
|
|
{
|
|
|
|
out->addProblem(PROBLEM_ERROR, QObject::tr("Version file contains unsupported element 'tweakers'"));
|
|
|
|
}
|
2016-02-28 02:32:56 +05:30
|
|
|
if (root.contains("-libraries"))
|
|
|
|
{
|
|
|
|
out->addProblem(PROBLEM_ERROR, QObject::tr("Version file contains unsupported element '-libraries'"));
|
|
|
|
}
|
|
|
|
if (root.contains("-tweakers"))
|
|
|
|
{
|
|
|
|
out->addProblem(PROBLEM_ERROR, QObject::tr("Version file contains unsupported element '-tweakers'"));
|
|
|
|
}
|
|
|
|
if (root.contains("-minecraftArguments"))
|
|
|
|
{
|
|
|
|
out->addProblem(PROBLEM_ERROR, QObject::tr("Version file contains unsupported element '-minecraftArguments'"));
|
|
|
|
}
|
2016-03-13 04:53:45 +05:30
|
|
|
if (root.contains("+minecraftArguments"))
|
|
|
|
{
|
|
|
|
out->addProblem(PROBLEM_ERROR, QObject::tr("Version file contains unsupported element '+minecraftArguments'"));
|
|
|
|
}
|
2016-02-28 02:32:56 +05:30
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2016-02-28 23:31:54 +05:30
|
|
|
template <typename T>
|
|
|
|
struct libraryConversion
|
|
|
|
{
|
2016-03-07 06:31:28 +05:30
|
|
|
static QJsonObject convert(std::shared_ptr<Library> &value)
|
2016-02-28 23:31:54 +05:30
|
|
|
{
|
|
|
|
return OneSixVersionFormat::libraryToJson(value.get());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-28 02:32:56 +05:30
|
|
|
static QJsonDocument versionFileToJson(VersionFilePtr patch, bool saveOrder)
|
|
|
|
{
|
|
|
|
QJsonObject root;
|
|
|
|
if (saveOrder)
|
|
|
|
{
|
|
|
|
root.insert("order", patch->order);
|
|
|
|
}
|
|
|
|
writeString(root, "name", patch->name);
|
|
|
|
writeString(root, "fileId", patch->fileId);
|
|
|
|
writeString(root, "version", patch->version);
|
|
|
|
writeString(root, "mcVersion", patch->mcVersion);
|
|
|
|
writeString(root, "id", patch->id);
|
|
|
|
writeString(root, "mainClass", patch->mainClass);
|
|
|
|
writeString(root, "appletClass", patch->appletClass);
|
2016-03-13 04:53:45 +05:30
|
|
|
writeString(root, "minecraftArguments", patch->minecraftArguments);
|
2016-02-28 02:32:56 +05:30
|
|
|
writeString(root, "type", patch->type);
|
|
|
|
writeString(root, "assets", patch->assets);
|
|
|
|
if (patch->isMinecraftVersion())
|
|
|
|
{
|
2016-03-02 07:33:44 +05:30
|
|
|
writeString(root, "releaseTime", timeToS3Time(patch->m_releaseTime));
|
|
|
|
writeString(root, "time", timeToS3Time(patch->m_updateTime));
|
2016-02-28 02:32:56 +05:30
|
|
|
}
|
|
|
|
writeStringList(root, "+tweakers", patch->addTweakers);
|
|
|
|
writeStringList(root, "+traits", patch->traits.toList());
|
2016-03-13 06:58:55 +05:30
|
|
|
if (!patch->libraries.isEmpty())
|
2016-02-28 23:31:54 +05:30
|
|
|
{
|
|
|
|
QJsonArray array;
|
2016-03-13 06:58:55 +05:30
|
|
|
for (auto value: patch->libraries)
|
2016-02-28 23:31:54 +05:30
|
|
|
{
|
|
|
|
array.append(OneSixVersionFormat::libraryToJson(value.get()));
|
|
|
|
}
|
2016-03-13 04:53:45 +05:30
|
|
|
root.insert("libraries", array);
|
2016-02-28 23:31:54 +05:30
|
|
|
}
|
|
|
|
if (!patch->jarMods.isEmpty())
|
|
|
|
{
|
|
|
|
QJsonArray array;
|
|
|
|
for (auto value: patch->jarMods)
|
|
|
|
{
|
|
|
|
array.append(OneSixVersionFormat::jarModtoJson(value.get()));
|
|
|
|
}
|
|
|
|
root.insert("+jarMods", array);
|
|
|
|
}
|
2016-02-28 02:32:56 +05:30
|
|
|
// write the contents to a json document.
|
|
|
|
{
|
|
|
|
QJsonDocument out;
|
|
|
|
out.setObject(root);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-28 23:31:54 +05:30
|
|
|
static QJsonDocument minecraftVersionToJson(MinecraftVersionPtr patch, bool saveOrder)
|
|
|
|
{
|
2016-03-13 04:53:45 +05:30
|
|
|
if(patch->getVersionSource() == Local && patch->getVersionFile())
|
2016-02-28 23:31:54 +05:30
|
|
|
{
|
|
|
|
return OneSixVersionFormat::profilePatchToJson(patch->getVersionFile(), saveOrder);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw VersionIncomplete(QObject::tr("Can't write incomplete/builtin Minecraft version %1").arg(patch->name()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonDocument OneSixVersionFormat::profilePatchToJson(const ProfilePatchPtr &patch, bool saveOrder)
|
2016-02-28 02:32:56 +05:30
|
|
|
{
|
|
|
|
auto vfile = std::dynamic_pointer_cast<VersionFile>(patch);
|
|
|
|
if(vfile)
|
|
|
|
{
|
|
|
|
return versionFileToJson(vfile, saveOrder);
|
|
|
|
}
|
2016-02-28 23:31:54 +05:30
|
|
|
auto mversion = std::dynamic_pointer_cast<MinecraftVersion>(patch);
|
|
|
|
if(mversion)
|
|
|
|
{
|
|
|
|
return minecraftVersionToJson(mversion, saveOrder);
|
|
|
|
}
|
2016-03-13 04:53:45 +05:30
|
|
|
throw VersionIncomplete(QObject::tr("Unhandled object type while processing %1").arg(patch->getName()));
|
2016-02-28 02:32:56 +05:30
|
|
|
}
|
|
|
|
|
2016-02-28 23:31:54 +05:30
|
|
|
JarmodPtr OneSixVersionFormat::jarModFromJson(const QJsonObject &libObj, const QString &filename, const QString &originalName)
|
|
|
|
{
|
|
|
|
JarmodPtr out(new Jarmod());
|
|
|
|
if (!libObj.contains("name"))
|
|
|
|
{
|
|
|
|
throw JSONValidationError(filename +
|
|
|
|
"contains a jarmod that doesn't have a 'name' field");
|
|
|
|
}
|
|
|
|
out->name = libObj.value("name").toString();
|
|
|
|
out->originalName = libObj.value("originalName").toString();
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject OneSixVersionFormat::jarModtoJson(Jarmod *jarmod)
|
|
|
|
{
|
|
|
|
QJsonObject out;
|
|
|
|
writeString(out, "name", jarmod->name);
|
|
|
|
if(!jarmod->originalName.isEmpty())
|
|
|
|
{
|
|
|
|
writeString(out, "originalName", jarmod->originalName);
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|