Split OneSixVersion into parts.

This commit is contained in:
Petr Mrázek 2013-09-11 23:43:17 +02:00
parent 108a5a677c
commit 7721c57e5e
12 changed files with 287 additions and 259 deletions

View File

@ -205,7 +205,11 @@ logic/OneSixInstance.h
logic/OneSixInstance_p.h
logic/OneSixUpdate.h
logic/OneSixVersion.h
logic/OneSixLibrary.h
logic/OneSixRule.h
logic/VersionFactory.h
logic/OpSys.h
# Nostalgia
logic/NostalgiaInstance.h
@ -272,9 +276,12 @@ logic/LegacyForge.cpp
# 1.6 instances
logic/OneSixAssets.cpp
logic/OneSixInstance.cpp
logic/OneSixVersion.cpp
logic/OneSixUpdate.cpp
logic/OneSixVersion.cpp
logic/OneSixLibrary.cpp
logic/OneSixRule.cpp
logic/VersionFactory.cpp
logic/OpSys.cpp
# Nostalgia
logic/NostalgiaInstance.cpp
@ -497,4 +504,4 @@ endif (UPDATE_TRANSLATIONS)
add_custom_target (translations_target DEPENDS ${QM_FILES})
install(FILES ${QM_FILES} DESTINATION ${CMAKE_INSTALL_PREFIX}/translations)
install(FILES ${QM_FILES} DESTINATION ${CMAKE_INSTALL_PREFIX}/translations)

View File

@ -2,6 +2,7 @@
#include "BaseInstance_p.h"
#include "OneSixVersion.h"
#include "OneSixLibrary.h"
#include "ModList.h"
struct OneSixInstancePrivate: public BaseInstancePrivate

84
logic/OneSixLibrary.cpp Normal file
View File

@ -0,0 +1,84 @@
#include "OneSixLibrary.h"
#include "OneSixRule.h"
void OneSixLibrary::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
{
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 );
}
}
void OneSixLibrary::setName ( QString name )
{
m_name = name;
}
void OneSixLibrary::setBaseUrl ( QString base_url )
{
m_base_url = base_url;
}
void OneSixLibrary::setIsNative()
{
m_is_native = true;
}
void OneSixLibrary::addNative ( OpSys os, QString suffix )
{
m_is_native = true;
m_native_suffixes[os] = suffix;
}
void OneSixLibrary::setRules ( QList< QSharedPointer< Rule > > rules )
{
m_rules = rules;
}
bool OneSixLibrary::isActive()
{
return m_is_active;
}
bool OneSixLibrary::isNative()
{
return m_is_native;
}
QString OneSixLibrary::downloadPath()
{
return m_download_path;
}
QString OneSixLibrary::storagePath()
{
return m_storage_path;
}

68
logic/OneSixLibrary.h Normal file
View File

@ -0,0 +1,68 @@
#pragma once
#include <QString>
#include <QStringList>
#include <QMap>
#include <QSharedPointer>
#include "OpSys.h"
class Rule;
class OneSixLibrary
{
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 actually active on the current OS?
bool m_is_active;
/// is the library a native?
bool m_is_native;
/// native suffixes per OS
QMap<OpSys, QString> m_native_suffixes;
public:
QStringList extract_excludes;
public:
/// Constructor
OneSixLibrary(QString name)
{
m_is_native = false;
m_is_active = false;
m_name = name;
m_base_url = "https://s3.amazonaws.com/Minecraft.Download/libraries/";
}
/**
* finalize the library, processing the input values into derived values and state
*
* This SHALL be called after all the values are parsed or after any further change.
*/
void finalize();
/// Set the library composite name
void setName(QString name);
/// Set the url base for downloads
void setBaseUrl(QString base_url);
/// Call this to mark the library as 'native' (it's a zip archive with DLLs)
void setIsNative();
/// Attach a name suffix to the specified OS native
void addNative(OpSys os, QString suffix);
/// Set the load rules
void setRules(QList<QSharedPointer<Rule> > rules);
/// Returns true if the library should be loaded (or extracted, in case of natives)
bool isActive();
/// Returns true if the library is native
bool isNative();
/// Get the URL to download the library from
QString downloadPath();
/// Get the relative path where the library should be saved
QString storagePath();
};

10
logic/OneSixRule.cpp Normal file
View File

@ -0,0 +1,10 @@
#include "OneSixRule.h"
RuleAction RuleAction_fromString(QString name)
{
if(name == "allow")
return Allow;
if(name == "disallow")
return Disallow;
return Defer;
}

70
logic/OneSixRule.h Normal file
View File

@ -0,0 +1,70 @@
#pragma once
#include <QString>
#include <QSharedPointer>
class OneSixLibrary;
#include "OneSixLibrary.h"
enum RuleAction
{
Allow,
Disallow,
Defer
};
RuleAction RuleAction_fromString(QString);
class Rule
{
protected:
RuleAction m_result;
virtual bool applies(OneSixLibrary * parent) = 0;
public:
Rule(RuleAction result)
:m_result(result) {}
virtual ~Rule(){};
RuleAction apply(OneSixLibrary * parent)
{
if(applies(parent))
return m_result;
else
return Defer;
};
};
class OsRule : public Rule
{
private:
// the OS
OpSys m_system;
// the OS version regexp
QString m_version_regexp;
protected:
virtual bool applies ( OneSixLibrary* )
{
return (m_system == currentSystem);
}
OsRule(RuleAction result, OpSys system, QString 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
{
protected:
virtual bool applies ( OneSixLibrary* )
{
return true;
}
ImplicitRule(RuleAction result)
: Rule(result) {}
public:
static QSharedPointer<ImplicitRule> create(RuleAction result)
{
return QSharedPointer<ImplicitRule> (new ImplicitRule(result));
}
};

View File

@ -28,6 +28,7 @@
#include "lists/MinecraftVersionList.h"
#include "VersionFactory.h"
#include "OneSixVersion.h"
#include "OneSixLibrary.h"
#include "OneSixInstance.h"
#include "pathutils.h"

View File

@ -1,111 +1,9 @@
#include "OneSixVersion.h"
#include "OneSixLibrary.h"
RuleAction RuleAction_fromString(QString name)
QList<QSharedPointer<OneSixLibrary> > OneSixVersion::getActiveNormalLibs()
{
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;
}
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
{
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 );
}
}
void Library::setName ( QString name )
{
m_name = name;
}
void Library::setBaseUrl ( QString base_url )
{
m_base_url = base_url;
}
void Library::setIsNative()
{
m_is_native = true;
}
void Library::addNative ( OpSys os, QString suffix )
{
m_is_native = true;
m_native_suffixes[os] = suffix;
}
void Library::setRules ( QList< QSharedPointer< Rule > > rules )
{
m_rules = rules;
}
bool Library::isActive()
{
return m_is_active;
}
bool Library::isNative()
{
return m_is_native;
}
QString Library::downloadPath()
{
return m_download_path;
}
QString Library::storagePath()
{
return m_storage_path;
}
QList<QSharedPointer<Library> > OneSixVersion::getActiveNormalLibs()
{
QList<QSharedPointer<Library> > output;
QList<QSharedPointer<OneSixLibrary> > output;
for ( auto lib: libraries )
{
if (lib->isActive() && !lib->isNative())
@ -116,9 +14,9 @@ QList<QSharedPointer<Library> > OneSixVersion::getActiveNormalLibs()
return output;
}
QList<QSharedPointer<Library> > OneSixVersion::getActiveNativeLibs()
QList<QSharedPointer<OneSixLibrary> > OneSixVersion::getActiveNativeLibs()
{
QList<QSharedPointer<Library> > output;
QList<QSharedPointer<OneSixLibrary> > output;
for ( auto lib: libraries )
{
if (lib->isActive() && lib->isNative())

View File

@ -1,151 +1,6 @@
#pragma once
#include <QtCore>
class Library;
enum OpSys
{
Os_Windows,
Os_Linux,
Os_OSX,
Os_Other
};
OpSys OpSys_fromString(QString);
#ifdef Q_OS_WIN32
#define currentSystem Os_Windows
#else
#ifdef Q_OS_MAC
#define currentSystem Os_OSX
#else
#define currentSystem Os_Linux
#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
{
private:
// the OS
OpSys m_system;
// the OS version regexp
QString m_version_regexp;
protected:
virtual bool applies ( Library* )
{
return (m_system == currentSystem);
}
OsRule(RuleAction result, OpSys system, QString 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
{
protected:
virtual bool applies ( Library* )
{
return true;
}
ImplicitRule(RuleAction result)
: Rule(result) {}
public:
static QSharedPointer<ImplicitRule> create(RuleAction result)
{
return QSharedPointer<ImplicitRule> (new ImplicitRule(result));
}
};
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 actually active on the current OS?
bool m_is_active;
/// is the library a native?
bool m_is_native;
/// native suffixes per OS
QMap<OpSys, QString> m_native_suffixes;
public:
QStringList extract_excludes;
public:
/// Constructor
Library(QString name)
{
m_is_native = false;
m_is_native = false;
m_name = name;
m_base_url = "https://s3.amazonaws.com/Minecraft.Download/libraries/";
}
/**
* finalize the library, processing the input values into derived values and state
*
* This SHALL be called after all the values are parsed or after any further change.
*/
void finalize();
/// Set the library composite name
void setName(QString name);
/// Set the url base for downloads
void setBaseUrl(QString base_url);
/// Call this to mark the library as 'native' (it's a zip archive with DLLs)
void setIsNative();
/// Attach a name suffix to the specified OS native
void addNative(OpSys os, QString suffix);
/// Set the load rules
void setRules(QList<QSharedPointer<Rule> > rules);
/// Returns true if the library should be loaded (or extracted, in case of natives)
bool isActive();
/// Returns true if the library is native
bool isNative();
/// Get the URL to download the library from
QString downloadPath();
/// Get the relative path where the library should be saved
QString storagePath();
};
class OneSixLibrary;
class OneSixVersion
{
@ -180,7 +35,7 @@ public:
QString mainClass;
/// the list of libs - both active and inactive, native and java
QList<QSharedPointer<Library> > libraries;
QList<QSharedPointer<OneSixLibrary> > libraries;
/*
FIXME: add support for those rules here? Looks like a pile of quick hacks to me though.
@ -208,6 +63,6 @@ public:
minimumLauncherVersion = 0xDEADBEEF;
}
QList<QSharedPointer<Library> > getActiveNormalLibs();
QList<QSharedPointer<Library> > getActiveNativeLibs();
QList<QSharedPointer<OneSixLibrary> > getActiveNormalLibs();
QList<QSharedPointer<OneSixLibrary> > getActiveNativeLibs();
};

12
logic/OpSys.cpp Normal file
View File

@ -0,0 +1,12 @@
#include "OpSys.h"
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;
}

21
logic/OpSys.h Normal file
View File

@ -0,0 +1,21 @@
#pragma once
#include <QString>
enum OpSys
{
Os_Windows,
Os_Linux,
Os_OSX,
Os_Other
};
OpSys OpSys_fromString(QString);
#ifdef Q_OS_WIN32
#define currentSystem Os_Windows
#else
#ifdef Q_OS_MAC
#define currentSystem Os_OSX
#else
#define currentSystem Os_Linux
#endif
#endif

View File

@ -1,5 +1,6 @@
#include "VersionFactory.h"
#include "OneSixVersion.h"
#include "OneSixRule.h"
// Library rules (if any)
QList<QSharedPointer<Rule> > FullVersionFactory::parse4rules(QJsonObject & baseObj)
@ -103,7 +104,7 @@ QSharedPointer<OneSixVersion> FullVersionFactory::parse4(QJsonObject root, QShar
auto nameVal = libObj.value("name");
if(!nameVal.isString())
continue;
QSharedPointer<Library> library(new Library(nameVal.toString()));
QSharedPointer<OneSixLibrary> library(new OneSixLibrary(nameVal.toString()));
auto urlVal = libObj.value("url");
if(urlVal.isString())