Parsing the version files, part II

This commit is contained in:
Petr Mrázek 2013-07-24 23:44:00 +02:00
parent 18853ca3fa
commit 97cf08f964
5 changed files with 246 additions and 34 deletions

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <QString> #include <QString>
#include <QStringList>
class Library;
class FullVersion class FullVersion
{ {
@ -40,7 +41,7 @@ public:
QString mainClass; QString mainClass;
// the list of libs. just the names for now. expand to full-blown strutures! // the list of libs. just the names for now. expand to full-blown strutures!
QStringList libraries; QList<QSharedPointer<Library> > libraries;
// is this actually a legacy version? if so, none of the other stuff here will be ever used. // is this actually a legacy version? if so, none of the other stuff here will be ever used.
// added by FullVersionFactory // added by FullVersionFactory

View File

@ -15,4 +15,121 @@
#pragma once #pragma once
#include <QtCore>
class Library;
enum RuleAction
{
Allow,
Disallow,
Defer
};
RuleAction RuleAction_fromString(QString);
class Rule
{
protected:
RuleAction m_result;
virtual bool applies(Library * parent) = 0;
public:
Rule(RuleAction result)
:m_result(result) {}
virtual ~Rule(){};
RuleAction apply(Library * parent)
{
if(applies(parent))
return m_result;
else
return Defer;
};
};
enum OpSys
{
Os_Windows,
Os_Linux,
Os_OSX,
Os_Other
};
OpSys OpSys_fromString(QString);
#ifdef Q_OS_MAC
#define currentSystem Os_OSX
#endif
#ifdef Q_OS_LINUX
#define currentSystem Os_Linux
#endif
#ifdef Q_OS_WIN32
#define currentSystem Os_Windows
#endif
#ifndef currentSystem
#define currentSystem Os_Other
#endif
class OsRule : public Rule
{
private:
// the OS
OpSys m_system;
// the OS version regexp
QString m_version_regexp;
protected:
virtual bool applies ( Library* )
{
return (m_system == currentSystem);
}
public:
OsRule(RuleAction result, OpSys system, QString version_regexp)
: Rule(result), m_system(system), m_version_regexp(version_regexp) {}
};
class ImplicitRule : public Rule
{
protected:
virtual bool applies ( Library* )
{
return true;
}
public:
ImplicitRule(RuleAction result)
: Rule(result) {}
};
class Library
{
public:
QString base_url;
QString name;
QList<QSharedPointer<Rule> > rules;
QMap<OpSys, QString> natives;
QStringList extract_excludes;
void AddRule(RuleAction result)
{
rules.append(QSharedPointer<Rule>(new ImplicitRule(result)));
}
void AddRule(RuleAction result, OpSys system, QString version_regexp)
{
rules.append(QSharedPointer<Rule>(new OsRule(result, system, version_regexp)));
}
bool applies()
{
if(rules.empty())
return true;
RuleAction result = Disallow;
for(auto rule: rules)
{
RuleAction temp = rule->apply( this );
if(temp != Defer)
result = temp;
}
return result == Allow;
}
};

View File

@ -1,4 +1,4 @@
#include <QtCore>
#include "fullversion.h" #include "fullversion.h"
// ECHO, echo, echo, .... // ECHO, echo, echo, ....

View File

@ -1,69 +1,144 @@
#include "fullversionfactory.h" #include "fullversionfactory.h"
#include "fullversion.h" #include "fullversion.h"
#include <library.h>
QSharedPointer<FullVersion> FullVersionFactory::parse4(QJsonObject root, QSharedPointer<FullVersion> product) QSharedPointer<FullVersion> FullVersionFactory::parse4(QJsonObject root, QSharedPointer<FullVersion> fullVersion)
{ {
product->id = root.value("id").toString(); fullVersion->id = root.value("id").toString();
// if it's on our legacy list, it's legacy // if it's on our legacy list, it's legacy
if(legacyWhitelist.contains(product->id)) if(legacyWhitelist.contains(fullVersion->id))
product->isLegacy = true; fullVersion->isLegacy = true;
product->mainClass = root.value("mainClass").toString(); fullVersion->mainClass = root.value("mainClass").toString();
auto procArgsValue = root.value("processArguments"); auto procArgsValue = root.value("processArguments");
if(procArgsValue.isString()) if(procArgsValue.isString())
{ {
product->processArguments = procArgsValue.toString(); fullVersion->processArguments = procArgsValue.toString();
QString toCompare = product->processArguments.toLower(); QString toCompare = fullVersion->processArguments.toLower();
if(toCompare == "legacy") if(toCompare == "legacy")
{ {
product->minecraftArguments = " ${auth_player_name} ${auth_session}"; fullVersion->minecraftArguments = " ${auth_player_name} ${auth_session}";
product->isLegacy = true; fullVersion->isLegacy = true;
} }
else if(toCompare == "username_session") else if(toCompare == "username_session")
{ {
product->minecraftArguments = "--username ${auth_player_name} --session ${auth_session}"; fullVersion->minecraftArguments = "--username ${auth_player_name} --session ${auth_session}";
} }
else if(toCompare == "username_session_version") else if(toCompare == "username_session_version")
{ {
product->minecraftArguments = "--username ${auth_player_name} --session ${auth_session} --version ${profile_name}"; fullVersion->minecraftArguments = "--username ${auth_player_name} --session ${auth_session} --version ${profile_name}";
} }
} }
auto minecraftArgsValue = root.value("minecraftArguments"); auto minecraftArgsValue = root.value("minecraftArguments");
if(minecraftArgsValue.isString()) if(minecraftArgsValue.isString())
{ {
product->minecraftArguments = minecraftArgsValue.toString(); fullVersion->minecraftArguments = minecraftArgsValue.toString();
} }
product->releaseTime = root.value("releaseTime").toString(); fullVersion->releaseTime = root.value("releaseTime").toString();
product->time = root.value("time").toString(); fullVersion->time = root.value("time").toString();
// Iterate through the list. // Iterate through the list, if it's a list.
auto librariesValue = root.value("libraries"); auto librariesValue = root.value("libraries");
if(librariesValue.isArray()) if(!librariesValue.isArray())
{ return fullVersion;
QJsonArray libList = root.value("libraries").toArray(); QJsonArray libList = root.value("libraries").toArray();
for (auto lib : libList) for (auto libVal : libList)
{ {
if (!lib.isObject()) QSharedPointer<Library> library(new Library());
if (!libVal.isObject())
{ {
continue; continue;
} }
QJsonObject libObj = lib.toObject(); QJsonObject libObj = libVal.toObject();
QString crud = libObj.value("name").toString(); // Library name
product->libraries.append(crud); auto nameVal = libObj.value("name");
if(!nameVal.isString())
continue;
library->name = nameVal.toString();
// TODO: improve! // Extract excludes (if any)
/* auto extractVal = libObj.value("extract");
auto parts = crud.split(':'); if(extractVal.isObject())
int zz = parts.size(); {
*/ QStringList excludes;
auto extractObj = extractVal.toObject();
auto excludesVal = extractObj.value("exclude");
if(!excludesVal.isArray())
goto SKIP_EXTRACTS;
auto excludesList = excludesVal.toArray();
for(auto excludeVal : excludesList)
{
if(excludeVal.isString())
excludes.append(excludeVal.toString());
}
library->extract_excludes = excludes;
}
SKIP_EXTRACTS:
auto nativesVal = libObj.value("natives");
if(nativesVal.isObject())
{
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->natives[osType] = iter.value().toString();
iter++;
} }
} }
return product;
// Library rules (if any)
auto rulesVal = libObj.value("rules");
if(rulesVal.isArray())
{
QList<QSharedPointer<Rule> > rules;
QJsonArray ruleList = rulesVal.toArray();
for(auto ruleVal : ruleList)
{
QSharedPointer<Rule> rule;
if(!ruleVal.isObject())
continue;
auto ruleObj = ruleVal.toObject();
auto actionVal = ruleObj.value("action");
if(!actionVal.isString())
continue;
auto action = RuleAction_fromString(actionVal.toString());
if(action == Defer)
continue;
auto osVal = ruleObj.value("os");
if(!osVal.isObject())
{
rule.reset(new ImplicitRule(action));
}
else
{
auto osObj = osVal.toObject();
auto osNameVal = osObj.value("name");
if(!osNameVal.isString())
continue;
OpSys requiredOs = OpSys_fromString(osNameVal.toString());
QString versionRegex = osObj.value("version").toString();
rule.reset(new OsRule(action, requiredOs, versionRegex));
}
rules.append(rule);
}
library->rules = rules;
}
}
return fullVersion;
} }
QSharedPointer<FullVersion> FullVersionFactory::parse(QByteArray data) QSharedPointer<FullVersion> FullVersionFactory::parse(QByteArray data)

View File

@ -15,4 +15,23 @@
#include "include/library.h" #include "include/library.h"
RuleAction RuleAction_fromString(QString name)
{
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;
}
// default url for lib: https://s3.amazonaws.com/Minecraft.Download/libraries/ // default url for lib: https://s3.amazonaws.com/Minecraft.Download/libraries/