2013-11-04 07:23:05 +05:30
|
|
|
/* 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 "logic/OneSixVersion.h"
|
|
|
|
#include "logic/OneSixLibrary.h"
|
|
|
|
#include "logic/OneSixRule.h"
|
2013-07-22 05:31:56 +05:30
|
|
|
|
2013-10-06 04:43:40 +05:30
|
|
|
std::shared_ptr<OneSixVersion> fromJsonV4(QJsonObject root,
|
2013-11-04 07:23:05 +05:30
|
|
|
std::shared_ptr<OneSixVersion> fullVersion)
|
2013-07-25 03:14:00 +05:30
|
|
|
{
|
2013-09-22 07:51:36 +05:30
|
|
|
fullVersion->id = root.value("id").toString();
|
|
|
|
|
|
|
|
fullVersion->mainClass = root.value("mainClass").toString();
|
|
|
|
auto procArgsValue = root.value("processArguments");
|
|
|
|
if (procArgsValue.isString())
|
|
|
|
{
|
|
|
|
fullVersion->processArguments = procArgsValue.toString();
|
|
|
|
QString toCompare = fullVersion->processArguments.toLower();
|
|
|
|
if (toCompare == "legacy")
|
|
|
|
{
|
|
|
|
fullVersion->minecraftArguments = " ${auth_player_name} ${auth_session}";
|
|
|
|
}
|
|
|
|
else if (toCompare == "username_session")
|
|
|
|
{
|
|
|
|
fullVersion->minecraftArguments =
|
|
|
|
"--username ${auth_player_name} --session ${auth_session}";
|
|
|
|
}
|
|
|
|
else if (toCompare == "username_session_version")
|
|
|
|
{
|
|
|
|
fullVersion->minecraftArguments = "--username ${auth_player_name} "
|
|
|
|
"--session ${auth_session} "
|
|
|
|
"--version ${profile_name}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto minecraftArgsValue = root.value("minecraftArguments");
|
|
|
|
if (minecraftArgsValue.isString())
|
|
|
|
{
|
|
|
|
fullVersion->minecraftArguments = minecraftArgsValue.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto minecraftTypeValue = root.value("type");
|
|
|
|
if (minecraftTypeValue.isString())
|
|
|
|
{
|
|
|
|
fullVersion->type = minecraftTypeValue.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
fullVersion->releaseTime = root.value("releaseTime").toString();
|
|
|
|
fullVersion->time = root.value("time").toString();
|
|
|
|
|
|
|
|
// Iterate through the list, if it's a list.
|
|
|
|
auto librariesValue = root.value("libraries");
|
|
|
|
if (!librariesValue.isArray())
|
|
|
|
return fullVersion;
|
|
|
|
|
|
|
|
QJsonArray libList = root.value("libraries").toArray();
|
|
|
|
for (auto libVal : libList)
|
|
|
|
{
|
|
|
|
if (!libVal.isObject())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject libObj = libVal.toObject();
|
|
|
|
|
|
|
|
// Library name
|
|
|
|
auto nameVal = libObj.value("name");
|
|
|
|
if (!nameVal.isString())
|
|
|
|
continue;
|
2013-10-06 04:43:40 +05:30
|
|
|
std::shared_ptr<OneSixLibrary> library(new OneSixLibrary(nameVal.toString()));
|
2013-09-22 07:51:36 +05:30
|
|
|
|
|
|
|
auto urlVal = libObj.value("url");
|
|
|
|
if (urlVal.isString())
|
|
|
|
{
|
|
|
|
library->setBaseUrl(urlVal.toString());
|
|
|
|
}
|
2013-09-30 06:04:46 +05:30
|
|
|
auto hintVal = libObj.value("MMC-hint");
|
|
|
|
if (hintVal.isString())
|
|
|
|
{
|
|
|
|
library->setHint(hintVal.toString());
|
|
|
|
}
|
|
|
|
auto urlAbsVal = libObj.value("MMC-absoluteUrl");
|
|
|
|
auto urlAbsuVal = libObj.value("MMC-absulute_url"); // compatibility
|
2013-09-22 07:51:36 +05:30
|
|
|
if (urlAbsVal.isString())
|
|
|
|
{
|
|
|
|
library->setAbsoluteUrl(urlAbsVal.toString());
|
|
|
|
}
|
2013-11-04 07:23:05 +05:30
|
|
|
else if (urlAbsuVal.isString())
|
2013-09-30 06:04:46 +05:30
|
|
|
{
|
|
|
|
library->setAbsoluteUrl(urlAbsuVal.toString());
|
|
|
|
}
|
2013-09-22 07:51:36 +05:30
|
|
|
// Extract excludes (if any)
|
|
|
|
auto extractVal = libObj.value("extract");
|
|
|
|
if (extractVal.isObject())
|
|
|
|
{
|
|
|
|
QStringList excludes;
|
|
|
|
auto extractObj = extractVal.toObject();
|
|
|
|
auto excludesVal = extractObj.value("exclude");
|
|
|
|
if (excludesVal.isArray())
|
|
|
|
{
|
|
|
|
auto excludesList = excludesVal.toArray();
|
|
|
|
for (auto excludeVal : excludesList)
|
|
|
|
{
|
|
|
|
if (excludeVal.isString())
|
|
|
|
excludes.append(excludeVal.toString());
|
|
|
|
}
|
|
|
|
library->extract_excludes = excludes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto nativesVal = libObj.value("natives");
|
|
|
|
if (nativesVal.isObject())
|
|
|
|
{
|
|
|
|
library->setIsNative();
|
|
|
|
auto nativesObj = nativesVal.toObject();
|
|
|
|
auto iter = nativesObj.begin();
|
|
|
|
while (iter != nativesObj.end())
|
|
|
|
{
|
|
|
|
auto osType = OpSys_fromString(iter.key());
|
|
|
|
if (osType == Os_Other)
|
|
|
|
continue;
|
|
|
|
if (!iter.value().isString())
|
|
|
|
continue;
|
|
|
|
library->addNative(osType, iter.value().toString());
|
|
|
|
iter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
library->setRules(rulesFromJsonV4(libObj));
|
|
|
|
library->finalize();
|
|
|
|
fullVersion->libraries.append(library);
|
|
|
|
}
|
|
|
|
return fullVersion;
|
|
|
|
}
|
|
|
|
|
2013-10-06 04:43:40 +05:30
|
|
|
std::shared_ptr<OneSixVersion> OneSixVersion::fromJson(QJsonObject root)
|
2013-09-22 07:51:36 +05:30
|
|
|
{
|
2013-10-06 04:43:40 +05:30
|
|
|
std::shared_ptr<OneSixVersion> readVersion(new OneSixVersion());
|
2013-09-22 07:51:36 +05:30
|
|
|
int launcher_ver = readVersion->minimumLauncherVersion =
|
|
|
|
root.value("minimumLauncherVersion").toDouble();
|
|
|
|
|
|
|
|
// ADD MORE HERE :D
|
2013-11-22 05:34:14 +05:30
|
|
|
if (launcher_ver > 0 && launcher_ver <= 10)
|
2013-09-22 07:51:36 +05:30
|
|
|
return fromJsonV4(root, readVersion);
|
|
|
|
else
|
|
|
|
{
|
2013-10-06 04:43:40 +05:30
|
|
|
return std::shared_ptr<OneSixVersion>();
|
2013-09-22 07:51:36 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-06 04:43:40 +05:30
|
|
|
std::shared_ptr<OneSixVersion> OneSixVersion::fromFile(QString filepath)
|
2013-09-22 07:51:36 +05:30
|
|
|
{
|
|
|
|
QFile file(filepath);
|
|
|
|
if (!file.open(QIODevice::ReadOnly))
|
|
|
|
{
|
2013-10-06 04:43:40 +05:30
|
|
|
return std::shared_ptr<OneSixVersion>();
|
2013-09-22 07:51:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
auto data = file.readAll();
|
|
|
|
QJsonParseError jsonError;
|
|
|
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError);
|
|
|
|
|
|
|
|
if (jsonError.error != QJsonParseError::NoError)
|
|
|
|
{
|
2013-10-06 04:43:40 +05:30
|
|
|
return std::shared_ptr<OneSixVersion>();
|
2013-09-22 07:51:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (!jsonDoc.isObject())
|
|
|
|
{
|
2013-10-06 04:43:40 +05:30
|
|
|
return std::shared_ptr<OneSixVersion>();
|
2013-09-22 07:51:36 +05:30
|
|
|
}
|
|
|
|
QJsonObject root = jsonDoc.object();
|
|
|
|
auto version = fromJson(root);
|
2013-11-04 07:23:05 +05:30
|
|
|
if (version)
|
2013-10-18 04:30:46 +05:30
|
|
|
version->original_file = filepath;
|
2013-09-22 07:51:36 +05:30
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OneSixVersion::toOriginalFile()
|
|
|
|
{
|
|
|
|
if (original_file.isEmpty())
|
|
|
|
return false;
|
|
|
|
QSaveFile file(original_file);
|
|
|
|
if (!file.open(QIODevice::WriteOnly))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// serialize base attributes (those we care about anyway)
|
|
|
|
QJsonObject root;
|
|
|
|
root.insert("minecraftArguments", minecraftArguments);
|
|
|
|
root.insert("mainClass", mainClass);
|
|
|
|
root.insert("minimumLauncherVersion", minimumLauncherVersion);
|
|
|
|
root.insert("time", time);
|
|
|
|
root.insert("id", id);
|
|
|
|
root.insert("type", type);
|
|
|
|
// screw processArguments
|
|
|
|
root.insert("releaseTime", releaseTime);
|
|
|
|
QJsonArray libarray;
|
2013-11-04 07:23:05 +05:30
|
|
|
for (const auto &lib : libraries)
|
2013-09-22 07:51:36 +05:30
|
|
|
{
|
|
|
|
libarray.append(lib->toJson());
|
|
|
|
}
|
2013-11-04 07:23:05 +05:30
|
|
|
if (libarray.count())
|
2013-09-22 07:51:36 +05:30
|
|
|
root.insert("libraries", libarray);
|
|
|
|
QJsonDocument doc(root);
|
|
|
|
file.write(doc.toJson());
|
|
|
|
return file.commit();
|
|
|
|
}
|
|
|
|
|
2013-10-06 04:43:40 +05:30
|
|
|
QList<std::shared_ptr<OneSixLibrary>> OneSixVersion::getActiveNormalLibs()
|
2013-09-22 07:51:36 +05:30
|
|
|
{
|
2013-10-06 04:43:40 +05:30
|
|
|
QList<std::shared_ptr<OneSixLibrary>> output;
|
2013-09-22 07:51:36 +05:30
|
|
|
for (auto lib : libraries)
|
2013-07-29 04:29:35 +05:30
|
|
|
{
|
2013-08-05 06:59:50 +05:30
|
|
|
if (lib->isActive() && !lib->isNative())
|
2013-07-29 04:29:35 +05:30
|
|
|
{
|
|
|
|
output.append(lib);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2013-10-06 04:43:40 +05:30
|
|
|
QList<std::shared_ptr<OneSixLibrary>> OneSixVersion::getActiveNativeLibs()
|
2013-07-29 04:29:35 +05:30
|
|
|
{
|
2013-10-06 04:43:40 +05:30
|
|
|
QList<std::shared_ptr<OneSixLibrary>> output;
|
2013-09-22 07:51:36 +05:30
|
|
|
for (auto lib : libraries)
|
2013-07-29 04:29:35 +05:30
|
|
|
{
|
2013-08-05 06:59:50 +05:30
|
|
|
if (lib->isActive() && lib->isNative())
|
2013-07-29 04:29:35 +05:30
|
|
|
{
|
|
|
|
output.append(lib);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
2013-08-05 06:59:50 +05:30
|
|
|
|
2013-09-22 07:51:36 +05:30
|
|
|
void OneSixVersion::externalUpdateStart()
|
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OneSixVersion::externalUpdateFinish()
|
|
|
|
{
|
|
|
|
endResetModel();
|
|
|
|
}
|
2013-08-05 06:59:50 +05:30
|
|
|
|
2013-09-22 07:51:36 +05:30
|
|
|
QVariant OneSixVersion::data(const QModelIndex &index, int role) const
|
2013-09-16 04:24:39 +05:30
|
|
|
{
|
2013-09-22 07:51:36 +05:30
|
|
|
if (!index.isValid())
|
2013-09-16 04:24:39 +05:30
|
|
|
return QVariant();
|
2013-09-22 07:51:36 +05:30
|
|
|
|
2013-09-16 04:24:39 +05:30
|
|
|
int row = index.row();
|
|
|
|
int column = index.column();
|
2013-09-22 07:51:36 +05:30
|
|
|
|
|
|
|
if (row < 0 || row >= libraries.size())
|
2013-09-16 04:24:39 +05:30
|
|
|
return QVariant();
|
2013-09-22 07:51:36 +05:30
|
|
|
|
|
|
|
if (role == Qt::DisplayRole)
|
2013-09-16 04:24:39 +05:30
|
|
|
{
|
2013-09-22 07:51:36 +05:30
|
|
|
switch (column)
|
2013-09-16 04:24:39 +05:30
|
|
|
{
|
2013-09-22 07:51:36 +05:30
|
|
|
case 0:
|
|
|
|
return libraries[row]->name();
|
|
|
|
case 1:
|
|
|
|
return libraries[row]->type();
|
|
|
|
case 2:
|
|
|
|
return libraries[row]->version();
|
|
|
|
default:
|
|
|
|
return QVariant();
|
2013-09-16 04:24:39 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2013-09-22 07:51:36 +05:30
|
|
|
Qt::ItemFlags OneSixVersion::flags(const QModelIndex &index) const
|
2013-09-16 04:24:39 +05:30
|
|
|
{
|
2013-09-22 07:51:36 +05:30
|
|
|
if (!index.isValid())
|
2013-09-16 04:24:39 +05:30
|
|
|
return Qt::NoItemFlags;
|
|
|
|
int row = index.row();
|
2013-09-22 07:51:36 +05:30
|
|
|
if (libraries[row]->isActive())
|
2013-09-16 04:24:39 +05:30
|
|
|
{
|
|
|
|
return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemNeverHasChildren;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return Qt::ItemNeverHasChildren;
|
|
|
|
}
|
2013-09-22 07:51:36 +05:30
|
|
|
// return QAbstractListModel::flags(index);
|
2013-09-16 04:24:39 +05:30
|
|
|
}
|
|
|
|
|
2013-09-22 07:51:36 +05:30
|
|
|
QVariant OneSixVersion::headerData(int section, Qt::Orientation orientation, int role) const
|
2013-09-16 04:24:39 +05:30
|
|
|
{
|
|
|
|
if (role != Qt::DisplayRole || orientation != Qt::Horizontal)
|
|
|
|
return QVariant();
|
|
|
|
switch (section)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return QString("Name");
|
|
|
|
case 1:
|
|
|
|
return QString("Type");
|
|
|
|
case 2:
|
|
|
|
return QString("Version");
|
|
|
|
default:
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-22 07:51:36 +05:30
|
|
|
int OneSixVersion::rowCount(const QModelIndex &parent) const
|
2013-09-16 04:24:39 +05:30
|
|
|
{
|
|
|
|
return libraries.size();
|
|
|
|
}
|
|
|
|
|
2013-09-22 07:51:36 +05:30
|
|
|
int OneSixVersion::columnCount(const QModelIndex &parent) const
|
2013-09-16 04:24:39 +05:30
|
|
|
{
|
2013-09-22 07:51:36 +05:30
|
|
|
return 3;
|
2013-09-16 04:24:39 +05:30
|
|
|
}
|