refactor indendation, fix a bug in MinecraftProcess & fix a bug in

InstanceLauncher
This commit is contained in:
Orochimarufan 2013-02-25 22:47:03 +01:00
parent f01bf10dc5
commit f4c9cb8c1d
13 changed files with 206 additions and 210 deletions

View File

@ -7,7 +7,7 @@ namespace java
class constant
{
public:
enum type_t : uint8_t
enum type_t : uint8_t
{
j_hole = 0, // HACK: this is a hole in the array, because java is crazy
j_string_data = 1,
@ -145,7 +145,7 @@ namespace java
uint16_t descriptor_index;
} name_and_type;
};
};
};
/**
* A helper class that represents the custom container used in Java class file for storage of constants
@ -185,7 +185,7 @@ namespace java
index++;
}
}
}
}
typedef std::vector<java::constant> container_type;
/**
* Access constants based on jar file index numbers (index of the first element is 1)
@ -208,5 +208,5 @@ namespace java
}
private:
container_type constants;
};
};
}

View File

@ -26,56 +26,56 @@ namespace javautils
QString GetMinecraftJarVersion(QString jarName)
{
QString version = MCVer_Unknown;
QString version = MCVer_Unknown;
// check if minecraft.jar exists
QFile jar(jarName);
if (!jar.exists())
return version;
// check if minecraft.jar exists
QFile jar(jarName);
if (!jar.exists())
return version;
// open minecraft.jar
QuaZip zip(&jar);
if (!zip.open(QuaZip::mdUnzip))
return version;
// open minecraft.jar
QuaZip zip(&jar);
if (!zip.open(QuaZip::mdUnzip))
return version;
// open Minecraft.class
zip.setCurrentFile("net/minecraft/client/Minecraft.class", QuaZip::csSensitive);
QuaZipFile Minecraft(&zip);
if (!Minecraft.open(QuaZipFile::ReadOnly))
return version;
// open Minecraft.class
zip.setCurrentFile("net/minecraft/client/Minecraft.class", QuaZip::csSensitive);
QuaZipFile Minecraft(&zip);
if (!Minecraft.open(QuaZipFile::ReadOnly))
return version;
// read Minecraft.class
qint64 size = Minecraft.size();
char *classfile = new char[size];
Minecraft.read(classfile, size);
// read Minecraft.class
qint64 size = Minecraft.size();
char *classfile = new char[size];
Minecraft.read(classfile, size);
// parse Minecraft.class
try {
char *temp = classfile;
java::classfile MinecraftClass(temp, size);
java::constant_pool constants = MinecraftClass.constants;
for(java::constant_pool::container_type::const_iterator iter=constants.begin();
iter != constants.end(); iter++)
{
const java::constant & constant = *iter;
if (constant.type != java::constant::j_string_data)
continue;
const std::string & str = constant.str_data;
if (str.compare(0, 20, "Minecraft Minecraft ") == 0)
{
version = str.substr(20).data();
break;
}
}
} catch(java::classfile_exception &) {}
// parse Minecraft.class
try {
char *temp = classfile;
java::classfile MinecraftClass(temp, size);
java::constant_pool constants = MinecraftClass.constants;
for(java::constant_pool::container_type::const_iterator iter=constants.begin();
iter != constants.end(); iter++)
{
const java::constant & constant = *iter;
if (constant.type != java::constant::j_string_data)
continue;
const std::string & str = constant.str_data;
if (str.compare(0, 20, "Minecraft Minecraft ") == 0)
{
version = str.substr(20).data();
break;
}
}
} catch(java::classfile_exception &) {}
// clean up
delete[] classfile;
Minecraft.close();
zip.close();
jar.close();
// clean up
delete[] classfile;
Minecraft.close();
zip.close();
jar.close();
return version;
return version;
}
}

View File

@ -21,8 +21,8 @@
namespace javautils
{
/**
* @brief Get the version from a minecraft.jar by parsing its class files. Expensive!
/**
* @brief Get the version from a minecraft.jar by parsing its class files. Expensive!
*/
QString GetMinecraftJarVersion(QString jar);
}

View File

@ -85,7 +85,7 @@ protected:
QStringList m_arguments;
void genArgs();
void log(QString text);
void log(QString text, ConsoleWindow::WriteMode mode = ConsoleWindow::MULTIMC);
protected slots:
void finish(int, QProcess::ExitStatus status);

View File

@ -197,6 +197,8 @@ void MinecraftProcess::launch()
start(m_instance->settings().get("JavaPath").toString(), m_arguments);
if (!waitForStarted())
{
log("Could not launch minecraft!", ConsoleWindow::ERROR);
return;
//TODO: error handling
}

View File

@ -6,7 +6,7 @@
* 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
* 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,
@ -33,53 +33,53 @@
*/
class LIBMMCSETTINGS_EXPORT Keyring : public QObject
{
Q_OBJECT
Q_OBJECT
public:
/**
* @brief the System Keyring instance
* @return the Keyring instance
*/
static Keyring *instance();
/**
* @brief the System Keyring instance
* @return the Keyring instance
*/
static Keyring *instance();
/**
* @brief store a password in the Keyring
* @param service the service name
* @param username the account name
* @param password the password to store
* @return success
*/
virtual bool storePassword(QString service, QString username, QString password) = 0;
/**
* @brief store a password in the Keyring
* @param service the service name
* @param username the account name
* @param password the password to store
* @return success
*/
virtual bool storePassword(QString service, QString username, QString password) = 0;
/**
* @brief get a password from the Keyring
* @param service the service name
* @param username the account name
* @return the password (success=!isNull())
*/
virtual QString getPassword(QString service, QString username) = 0;
/**
* @brief get a password from the Keyring
* @param service the service name
* @param username the account name
* @return the password (success=!isNull())
*/
virtual QString getPassword(QString service, QString username) = 0;
/**
* @brief lookup a password
* @param service the service name
* @param username the account name
* @return wether the password is available
*/
virtual bool hasPassword(QString service, QString username) = 0;
/**
* @brief lookup a password
* @param service the service name
* @param username the account name
* @return wether the password is available
*/
virtual bool hasPassword(QString service, QString username) = 0;
/**
* @brief get a list of all stored accounts.
* @param service the service name
* @return
*/
virtual QStringList getStoredAccounts(QString service) = 0;
/**
* @brief get a list of all stored accounts.
* @param service the service name
* @return
*/
virtual QStringList getStoredAccounts(QString service) = 0;
protected:
/// fall back to StubKeyring if false
virtual bool isValid() { return false; }
/// fall back to StubKeyring if false
virtual bool isValid() { return false; }
private:
static Keyring *m_instance;
static void destroy();
static Keyring *m_instance;
static void destroy();
};
#endif // KEYRING_H

View File

@ -6,7 +6,7 @@
* 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
* 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,
@ -37,27 +37,27 @@ class Win32Keystore;
Keyring *Keyring::instance()
{
if (m_instance == nullptr)
{
if (m_instance == nullptr)
{
#ifdef KEYRING
m_instance = new KEYRING();
if (!m_instance->isValid())
{
qWarning("Could not create SystemKeyring! falling back to StubKeyring.");
m_instance = new StubKeyring();
}
m_instance = new KEYRING();
if (!m_instance->isValid())
{
qWarning("Could not create SystemKeyring! falling back to StubKeyring.");
m_instance = new StubKeyring();
}
#else
qWarning("Keyrings are not supported on your OS. Fallback StubKeyring is insecure!");
m_instance = new StubKeyring();
qWarning("Keyrings are not supported on your OS. Fallback StubKeyring is insecure!");
m_instance = new StubKeyring();
#endif
atexit(Keyring::destroy);
}
return m_instance;
atexit(Keyring::destroy);
}
return m_instance;
}
void Keyring::destroy()
{
delete m_instance;
delete m_instance;
}
Keyring *Keyring::m_instance;

View File

@ -6,7 +6,7 @@
* 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
* 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,
@ -25,72 +25,72 @@ int scrambler = 0x9586309;
QString scramble(QString in_)
{
QByteArray in = in_.toUtf8();
QByteArray out;
for (int i = 0; i<in.length(); i++)
out.append(in.at(i) ^ scrambler);
return QString::fromUtf8(out);
QByteArray in = in_.toUtf8();
QByteArray out;
for (int i = 0; i<in.length(); i++)
out.append(in.at(i) ^ scrambler);
return QString::fromUtf8(out);
}
inline QString base64(QString in)
{
return QString(in.toUtf8().toBase64());
return QString(in.toUtf8().toBase64());
}
inline QString unbase64(QString in)
{
return QString::fromUtf8(QByteArray::fromBase64(in.toLatin1()));
return QString::fromUtf8(QByteArray::fromBase64(in.toLatin1()));
}
inline QString scramble64(QString in)
{
return base64(scramble(in));
return base64(scramble(in));
}
inline QString unscramble64(QString in)
{
return scramble(unbase64(in));
return scramble(unbase64(in));
}
// StubKeyring implementation
inline QString generateKey(QString service, QString username)
{
return QString("%1/%2").arg(base64(service)).arg(scramble64(username));
return QString("%1/%2").arg(base64(service)).arg(scramble64(username));
}
bool StubKeyring::storePassword(QString service, QString username, QString password)
{
m_settings.setValue(generateKey(service, username), scramble64(password));
return true;
m_settings.setValue(generateKey(service, username), scramble64(password));
return true;
}
QString StubKeyring::getPassword(QString service, QString username)
{
QString key = generateKey(service, username);
if (!m_settings.contains(key))
return QString();
return unscramble64(m_settings.value(key).toString());
QString key = generateKey(service, username);
if (!m_settings.contains(key))
return QString();
return unscramble64(m_settings.value(key).toString());
}
inline bool StubKeyring::hasPassword(QString service, QString username)
bool StubKeyring::hasPassword(QString service, QString username)
{
return m_settings.contains(generateKey(service, username));
return m_settings.contains(generateKey(service, username));
}
QStringList StubKeyring::getStoredAccounts(QString service)
{
service = base64(service).append('/');
QStringList out;
QStringList in(m_settings.allKeys());
QStringListIterator it(in);
while(it.hasNext())
{
QString c = it.next();
if (c.startsWith(service))
out << unscramble64(c.mid(service.length()));
}
return out;
service = base64(service).append('/');
QStringList out;
QStringList in(m_settings.allKeys());
QStringListIterator it(in);
while(it.hasNext())
{
QString c = it.next();
if (c.startsWith(service))
out << unscramble64(c.mid(service.length()));
}
return out;
}
StubKeyring::StubKeyring() :
m_settings(QSettings::UserScope, "Orochimarufan", "Keyring")
m_settings(QSettings::UserScope, "Orochimarufan", "Keyring")
{
}

View File

@ -6,7 +6,7 @@
* 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
* 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,
@ -24,19 +24,19 @@
class StubKeyring : public Keyring
{
Q_OBJECT
Q_OBJECT
public:
virtual bool storePassword(QString service, QString username, QString password);
virtual QString getPassword(QString service, QString username);
virtual bool hasPassword(QString service, QString username);
virtual QStringList getStoredAccounts(QString service);
virtual bool storePassword(QString service, QString username, QString password);
virtual QString getPassword(QString service, QString username);
virtual bool hasPassword(QString service, QString username);
virtual QStringList getStoredAccounts(QString service);
private:
friend class Keyring;
explicit StubKeyring();
virtual bool isValid() { return true; }
friend class Keyring;
explicit StubKeyring();
virtual bool isValid() { return true; }
QSettings m_settings;
QSettings m_settings;
};
#endif // STUBKEYRING_H

View File

@ -73,15 +73,6 @@ enum LIBUTIL_EXPORT Enum
};
}
namespace OptionType
{
enum LIBUTIL_EXPORT Enum
{
Switch,
Option
};
}
/**
* @brief The ParsingError class
*/
@ -211,6 +202,12 @@ private:
FlagStyle::Enum m_flagStyle;
ArgumentStyle::Enum m_argStyle;
enum OptionType
{
otSwitch,
otOption
};
// Important: the common part MUST BE COMMON ON ALL THREE structs
struct CommonDef {
QString name;
@ -226,7 +223,7 @@ private:
QString metavar;
QVariant def;
// option
OptionType::Enum type;
OptionType type;
QChar flag;
};

View File

@ -56,7 +56,7 @@ void Parser::addSwitch(QString name, bool def)
throw "Name not unique";
OptionDef *param = new OptionDef;
param->type = OptionType::Switch;
param->type = otSwitch;
param->name = name;
param->metavar = QString("<%1>").arg(name);
param->def = def;
@ -72,7 +72,7 @@ void Parser::addOption(QString name, QVariant def)
throw "Name not unique";
OptionDef *param = new OptionDef;
param->type = OptionType::Option;
param->type = otOption;
param->name = name;
param->metavar = QString("<%1>").arg(name);
param->def = def;
@ -161,7 +161,7 @@ QString Parser::compileHelp(QString progName, int helpIndent, bool useFlags)
help << flagPrefix << option->flag << ", ";
}
help << optPrefix << option->name;
if (option->type == OptionType::Option)
if (option->type == otOption)
{
QString arg = QString("%1%2").arg(((m_argStyle == ArgumentStyle::Equals) ? "=" : " "), option->metavar);
nameLength += arg.length();
@ -193,7 +193,7 @@ QString Parser::compileUsage(QString progName, bool useFlags)
usage << flagPrefix << option->flag;
else
usage << optPrefix << option->name;
if (option->type == OptionType::Option)
if (option->type == otOption)
usage << ((m_argStyle == ArgumentStyle::Equals) ? "=" : " ") << option->metavar;
usage << "]";
}
@ -265,9 +265,9 @@ QHash<QString, QVariant> Parser::parse(QStringList argv)
throw ParsingError(QString("Option %2%1 was given multiple times").arg(name, optionPrefix));
OptionDef *option = m_options[name];
if (option->type == OptionType::Switch)
if (option->type == otSwitch)
map[name] = true;
else //if (option->type == OptionType::Option)
else //if (option->type == otOption)
{
if (m_argStyle == ArgumentStyle::Space)
expecting.append(name);
@ -312,9 +312,9 @@ QHash<QString, QVariant> Parser::parse(QStringList argv)
if (map.contains(option->name))
throw ParsingError(QString("Option %2%1 was given multiple times").arg(option->name, optionPrefix));
if (option->type == OptionType::Switch)
if (option->type == otSwitch)
map[option->name] = true;
else //if (option->type == OptionType::Option)
else //if (option->type == otOption)
{
if (m_argStyle == ArgumentStyle::Space)
expecting.append(option->name);

View File

@ -66,12 +66,9 @@ private:
{
inst = iter.next();
if (inst->id() == instId)
break;
return inst;
}
if (inst->id() != instId)
return InstancePtr();
else
return iter.peekPrevious();
return InstancePtr();
}
private slots:

View File

@ -10,51 +10,51 @@ using namespace Util::Commandline;
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
app.setApplicationName("MMC Keyring test");
app.setOrganizationName("Orochimarufan");
QCoreApplication app(argc, argv);
app.setApplicationName("MMC Keyring test");
app.setOrganizationName("Orochimarufan");
Parser p;
p.addArgument("user", false);
p.addArgument("password", false);
p.addSwitch("set");
p.addSwitch("get");
p.addSwitch("list");
p.addOption("service", "Test");
p.addShortOpt("service", 's');
Parser p;
p.addArgument("user", false);
p.addArgument("password", false);
p.addSwitch("set");
p.addSwitch("get");
p.addSwitch("list");
p.addOption("service", "Test");
p.addShortOpt("service", 's');
QHash<QString, QVariant> args;
try {
args = p.parse(app.arguments());
} catch (ParsingError) {
std::cout << "Syntax error." << std::endl;
return 1;
}
QHash<QString, QVariant> args;
try {
args = p.parse(app.arguments());
} catch (ParsingError) {
std::cout << "Syntax error." << std::endl;
return 1;
}
if (args["set"].toBool()) {
if (args["user"].isNull() || args["password"].isNull()) {
std::cout << "set operation needs bot user and password set" << std::endl;
return 1;
}
if (args["set"].toBool()) {
if (args["user"].isNull() || args["password"].isNull()) {
std::cout << "set operation needs bot user and password set" << std::endl;
return 1;
}
return Keyring::instance()->storePassword(args["service"].toString(),
args["user"].toString(), args["password"].toString());
} else if (args["get"].toBool()) {
if (args["user"].isNull()) {
std::cout << "get operation needs user set" << std::endl;
return 1;
}
return Keyring::instance()->storePassword(args["service"].toString(),
args["user"].toString(), args["password"].toString());
} else if (args["get"].toBool()) {
if (args["user"].isNull()) {
std::cout << "get operation needs user set" << std::endl;
return 1;
}
std::cout << "Password: " << qPrintable(Keyring::instance()->getPassword(args["service"].toString(),
args["user"].toString())) << std::endl;
return 0;
} else if (args["list"].toBool()) {
QStringList accounts = Keyring::instance()->getStoredAccounts(args["service"].toString());
std::cout << "stored accounts:" << std::endl << '\t' << qPrintable(accounts.join("\n\t")) << std::endl;
return 0;
} else {
std::cout << "No operation given!" << std::endl;
std::cout << qPrintable(p.compileHelp(argv[0])) << std::endl;
return 1;
}
std::cout << "Password: " << qPrintable(Keyring::instance()->getPassword(args["service"].toString(),
args["user"].toString())) << std::endl;
return 0;
} else if (args["list"].toBool()) {
QStringList accounts = Keyring::instance()->getStoredAccounts(args["service"].toString());
std::cout << "stored accounts:" << std::endl << '\t' << qPrintable(accounts.join("\n\t")) << std::endl;
return 0;
} else {
std::cout << "No operation given!" << std::endl;
std::cout << qPrintable(p.compileHelp(argv[0])) << std::endl;
return 1;
}
}