Parsing the version files, part III

This commit is contained in:
Petr Mrázek 2013-07-27 11:41:45 +02:00
parent 97cf08f964
commit a7a84d4dbb
8 changed files with 214 additions and 94 deletions

View File

@ -2,6 +2,7 @@
#include <QtCore> #include <QtCore>
struct FullVersion; struct FullVersion;
class Rule;
class FullVersionFactory class FullVersionFactory
{ {
@ -19,5 +20,6 @@ public:
QSharedPointer<FullVersion> parse(QByteArray data); QSharedPointer<FullVersion> parse(QByteArray data);
private: private:
QSharedPointer<FullVersion> parse4(QJsonObject root, QSharedPointer<FullVersion> product); QSharedPointer<FullVersion> parse4(QJsonObject root, QSharedPointer<FullVersion> product);
QList<QSharedPointer<Rule> > parse4rules(QJsonObject & baseObj);
QStringList legacyWhitelist; QStringList legacyWhitelist;
}; };

View File

@ -116,8 +116,11 @@ class LIBMULTIMC_EXPORT Instance : public QObject
*/ */
Q_PROPERTY(qint64 lastCurrentVersionUpdate READ lastCurrentVersionUpdate WRITE setLastCurrentVersionUpdate) Q_PROPERTY(qint64 lastCurrentVersionUpdate READ lastCurrentVersionUpdate WRITE setLastCurrentVersionUpdate)
/*!
* Is the instance a new launcher instance? Get/Set
*/
Q_PROPERTY(bool isForNewLauncher READ isForNewLauncher WRITE setIsForNewLauncher)
// Dirs // Dirs
//! Path to the instance's .minecraft folder. //! Path to the instance's .minecraft folder.
Q_PROPERTY(QString minecraftDir READ minecraftDir STORED false) Q_PROPERTY(QString minecraftDir READ minecraftDir STORED false)
@ -250,6 +253,15 @@ public:
virtual qint64 lastCurrentVersionUpdate() const { return settings().get("lastVersionUpdate").value<qint64>(); } virtual qint64 lastCurrentVersionUpdate() const { return settings().get("lastVersionUpdate").value<qint64>(); }
virtual void setLastCurrentVersionUpdate(qint64 val) { settings().set("lastVersionUpdate", val); } virtual void setLastCurrentVersionUpdate(qint64 val) { settings().set("lastVersionUpdate", val); }
virtual bool isForNewLauncher()
{
return settings().get("IsForNewLauncher").value<bool>();
}
virtual void setIsForNewLauncher(bool value = true)
{
settings().set("IsForNewLauncher", value);
}
////// Directories ////// ////// Directories //////
QString minecraftDir() const; QString minecraftDir() const;

View File

@ -19,33 +19,6 @@
class Library; 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 enum OpSys
{ {
Os_Windows, Os_Windows,
@ -73,6 +46,33 @@ OpSys OpSys_fromString(QString);
#endif #endif
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;
};
};
class OsRule : public Rule class OsRule : public Rule
{ {
private: private:
@ -85,9 +85,13 @@ protected:
{ {
return (m_system == currentSystem); return (m_system == currentSystem);
} }
public:
OsRule(RuleAction result, OpSys system, QString version_regexp) OsRule(RuleAction result, OpSys system, QString version_regexp)
: Rule(result), m_system(system), m_version_regexp(version_regexp) {} : Rule(result), m_system(system), m_version_regexp(version_regexp) {}
public:
static QSharedPointer<OsRule> create(RuleAction result, OpSys system, QString version_regexp)
{
return QSharedPointer<OsRule> (new OsRule(result, system, version_regexp));
}
}; };
class ImplicitRule : public Rule class ImplicitRule : public Rule
@ -97,39 +101,119 @@ protected:
{ {
return true; return true;
} }
public:
ImplicitRule(RuleAction result) ImplicitRule(RuleAction result)
: Rule(result) {} : Rule(result) {}
public:
static QSharedPointer<ImplicitRule> create(RuleAction result)
{
return QSharedPointer<ImplicitRule> (new ImplicitRule(result));
}
}; };
class Library class Library
{ {
private:
// basic values used internally (so far)
QString m_name;
QString m_base_url;
QList<QSharedPointer<Rule> > m_rules;
// derived values used for real things
/// where to store the lib locally
QString m_storage_path;
/// where to download the lib from
QString m_download_path;
/// is this lib actuall active on the current OS?
bool m_is_active;
// native lib?
bool m_is_native;
QMap<OpSys, QString> m_native_suffixes;
public: public:
QString base_url;
QString name;
QList<QSharedPointer<Rule> > rules;
QMap<OpSys, QString> natives;
QStringList extract_excludes; QStringList extract_excludes;
void AddRule(RuleAction result) public:
/// finalize the library, processing the input values into derived values and state
void finalize()
{ {
rules.append(QSharedPointer<Rule>(new ImplicitRule(result))); QStringList parts = m_name.split(':');
} QString relative = parts[0];
void AddRule(RuleAction result, OpSys system, QString version_regexp) relative.replace('.','/');
relative += '/' + parts[1] + '/' + parts[2] + '/' + parts[1] + '-' + parts[2];
if(!m_is_native)
relative += ".jar";
else
{
if(m_native_suffixes.contains(currentSystem))
{
relative += "-" + m_native_suffixes[currentSystem] + ".jar";
}
else
{
// really, bad.
relative += ".jar";
}
}
m_storage_path = relative;
m_download_path = m_base_url + relative;
if(m_rules.empty())
{
m_is_active = true;
}
else
{
RuleAction result = Disallow;
for(auto rule: m_rules)
{
RuleAction temp = rule->apply( this );
if(temp != Defer)
result = temp;
}
m_is_active = (result == Allow);
}
if(m_is_native)
{
m_is_active = m_is_active && m_native_suffixes.contains(currentSystem);
}
};
Library(QString name)
{ {
rules.append(QSharedPointer<Rule>(new OsRule(result, system, version_regexp))); m_is_native = false;
m_is_native = false;
m_name = name;
m_base_url = "https://s3.amazonaws.com/Minecraft.Download/libraries/";
} }
void setName(QString name)
{
m_name = name;
}
void setBaseUrl(QString base_url)
{
m_base_url = base_url;
}
void setIsNative()
{
m_is_native = true;
}
void addNative(OpSys os, QString suffix)
{
m_is_native = true;
m_native_suffixes[os] = suffix;
}
void setRules(QList<QSharedPointer<Rule> > rules)
{
m_rules = rules;
}
bool applies() bool applies()
{ {
if(rules.empty()) return m_is_active;
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,5 @@
#include <QtCore> #include <QtCore>
#include "fullversion.h" #include "fullversion.h"
#include <library.h>
// ECHO, echo, echo, .... // ECHO, echo, echo, ....

View File

@ -2,6 +2,61 @@
#include "fullversion.h" #include "fullversion.h"
#include <library.h> #include <library.h>
class LibraryFinalizer
{
public:
LibraryFinalizer(QSharedPointer<Library> library)
{
m_library = library;
}
QSharedPointer<Library> m_library;
};
// Library rules (if any)
QList<QSharedPointer<Rule> > FullVersionFactory::parse4rules(QJsonObject & baseObj)
{
QList<QSharedPointer<Rule> > rules;
auto rulesVal = baseObj.value("rules");
if(rulesVal.isArray())
{
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())
{
// add a new implicit action rule
rules.append(ImplicitRule::create(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();
// add a new OS rule
rules.append(OsRule::create(action, requiredOs, versionRegex));
}
}
}
return rules;
}
QSharedPointer<FullVersion> FullVersionFactory::parse4(QJsonObject root, QSharedPointer<FullVersion> fullVersion) QSharedPointer<FullVersion> FullVersionFactory::parse4(QJsonObject root, QSharedPointer<FullVersion> fullVersion)
{ {
fullVersion->id = root.value("id").toString(); fullVersion->id = root.value("id").toString();
@ -48,7 +103,6 @@ QSharedPointer<FullVersion> FullVersionFactory::parse4(QJsonObject root, QShared
QJsonArray libList = root.value("libraries").toArray(); QJsonArray libList = root.value("libraries").toArray();
for (auto libVal : libList) for (auto libVal : libList)
{ {
QSharedPointer<Library> library(new Library());
if (!libVal.isObject()) if (!libVal.isObject())
{ {
continue; continue;
@ -60,7 +114,7 @@ QSharedPointer<FullVersion> FullVersionFactory::parse4(QJsonObject root, QShared
auto nameVal = libObj.value("name"); auto nameVal = libObj.value("name");
if(!nameVal.isString()) if(!nameVal.isString())
continue; continue;
library->name = nameVal.toString(); QSharedPointer<Library> library(new Library(nameVal.toString()));
// Extract excludes (if any) // Extract excludes (if any)
auto extractVal = libObj.value("extract"); auto extractVal = libObj.value("extract");
@ -84,6 +138,7 @@ QSharedPointer<FullVersion> FullVersionFactory::parse4(QJsonObject root, QShared
auto nativesVal = libObj.value("natives"); auto nativesVal = libObj.value("natives");
if(nativesVal.isObject()) if(nativesVal.isObject())
{ {
library->setIsNative();
auto nativesObj = nativesVal.toObject(); auto nativesObj = nativesVal.toObject();
auto iter = nativesObj.begin(); auto iter = nativesObj.begin();
while(iter != nativesObj.end()) while(iter != nativesObj.end())
@ -93,50 +148,13 @@ QSharedPointer<FullVersion> FullVersionFactory::parse4(QJsonObject root, QShared
continue; continue;
if(!iter.value().isString()) if(!iter.value().isString())
continue; continue;
library->natives[osType] = iter.value().toString(); library->addNative(osType, iter.value().toString());
iter++; iter++;
} }
} }
library->setRules(parse4rules(libObj));
// Library rules (if any) library->finalize();
auto rulesVal = libObj.value("rules"); fullVersion->libraries.append(library);
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; return fullVersion;
} }

View File

@ -141,6 +141,7 @@ void GameUpdateTask::jarlibFinished()
{ {
m_inst->setCurrentVersion(targetVersion->descriptor()); m_inst->setCurrentVersion(targetVersion->descriptor());
m_inst->setShouldUpdate(false); m_inst->setShouldUpdate(false);
m_inst->setIsForNewLauncher(true);
exit(1); exit(1);
} }
@ -195,6 +196,7 @@ void GameUpdateTask::legacyJarFinished()
{ {
setState(StateFinished); setState(StateFinished);
emit gameUpdateComplete(m_response); emit gameUpdateComplete(m_response);
m_inst->setIsForNewLauncher(true);
exit(1); exit(1);
} }

View File

@ -34,6 +34,7 @@ Instance::Instance(const QString &rootDir, QObject *parent) :
settings().registerSetting(new Setting("iconKey", "default")); settings().registerSetting(new Setting("iconKey", "default"));
settings().registerSetting(new Setting("notes", "")); settings().registerSetting(new Setting("notes", ""));
settings().registerSetting(new Setting("NeedsRebuild", true)); settings().registerSetting(new Setting("NeedsRebuild", true));
settings().registerSetting(new Setting("IsForNewLauncher", false));
settings().registerSetting(new Setting("ShouldUpdate", false)); settings().registerSetting(new Setting("ShouldUpdate", false));
settings().registerSetting(new Setting("JarVersion", "Unknown")); settings().registerSetting(new Setting("JarVersion", "Unknown"));
settings().registerSetting(new Setting("LwjglVersion", "2.9.0")); settings().registerSetting(new Setting("LwjglVersion", "2.9.0"));

View File

@ -34,4 +34,4 @@ OpSys OpSys_fromString(QString name)
return Os_OSX; return Os_OSX;
return Os_Other; return Os_Other;
} }
// default url for lib: https://s3.amazonaws.com/Minecraft.Download/libraries/ // default url for lib: