summaryrefslogtreecommitdiffstats
path: root/logic/OneSixVersion.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'logic/OneSixVersion.cpp')
-rw-r--r--logic/OneSixVersion.cpp347
1 files changed, 263 insertions, 84 deletions
diff --git a/logic/OneSixVersion.cpp b/logic/OneSixVersion.cpp
index 56e272e2..51958389 100644
--- a/logic/OneSixVersion.cpp
+++ b/logic/OneSixVersion.cpp
@@ -1,132 +1,311 @@
#include "OneSixVersion.h"
+#include "OneSixLibrary.h"
+#include "OneSixRule.h"
-RuleAction RuleAction_fromString(QString name)
+std::shared_ptr<OneSixVersion> fromJsonV4(QJsonObject root,
+ std::shared_ptr<OneSixVersion> fullVersion)
{
- if(name == "allow")
- return Allow;
- if(name == "disallow")
- return Disallow;
- return Defer;
-}
-
-OpSys OpSys_fromString(QString name)
-{
- if(name == "linux")
- return Os_Linux;
- if(name == "windows")
- return Os_Windows;
- if(name == "osx")
- return Os_OSX;
- return Os_Other;
-}
+ fullVersion->id = root.value("id").toString();
-void Library::finalize()
-{
- QStringList parts = m_name.split ( ':' );
- QString relative = parts[0];
- relative.replace ( '.','/' );
- relative += '/' + parts[1] + '/' + parts[2] + '/' + parts[1] + '-' + parts[2];
- if ( !m_is_native )
- relative += ".jar";
- else
+ fullVersion->mainClass = root.value("mainClass").toString();
+ auto procArgsValue = root.value("processArguments");
+ if (procArgsValue.isString())
{
- if ( m_native_suffixes.contains ( currentSystem ) )
+ fullVersion->processArguments = procArgsValue.toString();
+ QString toCompare = fullVersion->processArguments.toLower();
+ if (toCompare == "legacy")
{
- relative += "-" + m_native_suffixes[currentSystem] + ".jar";
+ fullVersion->minecraftArguments = " ${auth_player_name} ${auth_session}";
}
- else
+ else if (toCompare == "username_session")
{
- // really, bad.
- relative += ".jar";
+ 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}";
}
}
- m_storage_path = relative;
- m_download_path = m_base_url + relative;
- if ( m_rules.empty() )
+ auto minecraftArgsValue = root.value("minecraftArguments");
+ if (minecraftArgsValue.isString())
{
- m_is_active = true;
+ fullVersion->minecraftArguments = minecraftArgsValue.toString();
}
- else
+
+ auto minecraftTypeValue = root.value("type");
+ if (minecraftTypeValue.isString())
{
- RuleAction result = Disallow;
- for ( auto rule: m_rules )
- {
- RuleAction temp = rule->apply ( this );
- if ( temp != Defer )
- result = temp;
- }
- m_is_active = ( result == Allow );
+ fullVersion->type = minecraftTypeValue.toString();
}
- if ( m_is_native )
+
+ 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)
{
- m_is_active = m_is_active && m_native_suffixes.contains ( currentSystem );
+ if (!libVal.isObject())
+ {
+ continue;
+ }
+
+ QJsonObject libObj = libVal.toObject();
+
+ // Library name
+ auto nameVal = libObj.value("name");
+ if (!nameVal.isString())
+ continue;
+ std::shared_ptr<OneSixLibrary> library(new OneSixLibrary(nameVal.toString()));
+
+ auto urlVal = libObj.value("url");
+ if (urlVal.isString())
+ {
+ library->setBaseUrl(urlVal.toString());
+ }
+ 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
+ if (urlAbsVal.isString())
+ {
+ library->setAbsoluteUrl(urlAbsVal.toString());
+ }
+ else if(urlAbsuVal.isString())
+ {
+ library->setAbsoluteUrl(urlAbsuVal.toString());
+ }
+ // 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;
}
-void Library::setName ( QString name )
+std::shared_ptr<OneSixVersion> OneSixVersion::fromJson(QJsonObject root)
{
- m_name = name;
-}
-void Library::setBaseUrl ( QString base_url )
-{
- m_base_url = base_url;
+ std::shared_ptr<OneSixVersion> readVersion(new OneSixVersion());
+ int launcher_ver = readVersion->minimumLauncherVersion =
+ root.value("minimumLauncherVersion").toDouble();
+
+ // ADD MORE HERE :D
+ if (launcher_ver > 0 && launcher_ver <= 9)
+ return fromJsonV4(root, readVersion);
+ else
+ {
+ return std::shared_ptr<OneSixVersion>();
+ }
}
-void Library::setIsNative()
+
+std::shared_ptr<OneSixVersion> OneSixVersion::fromFile(QString filepath)
{
- m_is_native = true;
+ QFile file(filepath);
+ if (!file.open(QIODevice::ReadOnly))
+ {
+ return std::shared_ptr<OneSixVersion>();
+ }
+
+ auto data = file.readAll();
+ QJsonParseError jsonError;
+ QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError);
+
+ if (jsonError.error != QJsonParseError::NoError)
+ {
+ return std::shared_ptr<OneSixVersion>();
+ }
+
+ if (!jsonDoc.isObject())
+ {
+ return std::shared_ptr<OneSixVersion>();
+ }
+ QJsonObject root = jsonDoc.object();
+ auto version = fromJson(root);
+ if(version)
+ version->original_file = filepath;
+ return version;
}
-void Library::addNative ( OpSys os, QString suffix )
+
+bool OneSixVersion::toOriginalFile()
{
- m_is_native = true;
- m_native_suffixes[os] = suffix;
+ 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;
+ for(const auto & lib: libraries)
+ {
+ libarray.append(lib->toJson());
+ }
+ if(libarray.count())
+ root.insert("libraries", libarray);
+ QJsonDocument doc(root);
+ file.write(doc.toJson());
+ return file.commit();
}
-void Library::setRules ( QList< QSharedPointer< Rule > > rules )
+
+QList<std::shared_ptr<OneSixLibrary>> OneSixVersion::getActiveNormalLibs()
{
- m_rules = rules;
+ QList<std::shared_ptr<OneSixLibrary>> output;
+ for (auto lib : libraries)
+ {
+ if (lib->isActive() && !lib->isNative())
+ {
+ output.append(lib);
+ }
+ }
+ return output;
}
-bool Library::isActive()
+
+QList<std::shared_ptr<OneSixLibrary>> OneSixVersion::getActiveNativeLibs()
{
- return m_is_active;
+ QList<std::shared_ptr<OneSixLibrary>> output;
+ for (auto lib : libraries)
+ {
+ if (lib->isActive() && lib->isNative())
+ {
+ output.append(lib);
+ }
+ }
+ return output;
}
-bool Library::isNative()
+
+void OneSixVersion::externalUpdateStart()
{
- return m_is_native;
+ beginResetModel();
}
-QString Library::downloadPath()
+
+void OneSixVersion::externalUpdateFinish()
{
- return m_download_path;
+ endResetModel();
}
-QString Library::storagePath()
+
+QVariant OneSixVersion::data(const QModelIndex &index, int role) const
{
- return m_storage_path;
-}
+ if (!index.isValid())
+ return QVariant();
+ int row = index.row();
+ int column = index.column();
-QList<QSharedPointer<Library> > OneSixVersion::getActiveNormalLibs()
-{
- QList<QSharedPointer<Library> > output;
- for ( auto lib: libraries )
+ if (row < 0 || row >= libraries.size())
+ return QVariant();
+
+ if (role == Qt::DisplayRole)
{
- if (lib->isActive() && !lib->isNative())
+ switch (column)
{
- output.append(lib);
+ case 0:
+ return libraries[row]->name();
+ case 1:
+ return libraries[row]->type();
+ case 2:
+ return libraries[row]->version();
+ default:
+ return QVariant();
}
}
- return output;
+ return QVariant();
}
-QList<QSharedPointer<Library> > OneSixVersion::getActiveNativeLibs()
+Qt::ItemFlags OneSixVersion::flags(const QModelIndex &index) const
{
- QList<QSharedPointer<Library> > output;
- for ( auto lib: libraries )
+ if (!index.isValid())
+ return Qt::NoItemFlags;
+ int row = index.row();
+ if (libraries[row]->isActive())
{
- if (lib->isActive() && lib->isNative())
- {
- output.append(lib);
- }
+ return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemNeverHasChildren;
+ }
+ else
+ {
+ return Qt::ItemNeverHasChildren;
+ }
+ // return QAbstractListModel::flags(index);
+}
+
+QVariant OneSixVersion::headerData(int section, Qt::Orientation orientation, int role) const
+{
+ 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();
}
- return output;
}
+int OneSixVersion::rowCount(const QModelIndex &parent) const
+{
+ return libraries.size();
+}
+int OneSixVersion::columnCount(const QModelIndex &parent) const
+{
+ return 3;
+}