diff options
author | Orochimarufan <orochimarufan.x3@gmail.com> | 2013-02-25 22:47:03 +0100 |
---|---|---|
committer | Orochimarufan <orochimarufan.x3@gmail.com> | 2013-03-22 13:56:57 +0100 |
commit | f4c9cb8c1d395422b7e4f1c27ac92b6df08a39bb (patch) | |
tree | 595a93410af4ff4e238d27d78b030c595915d4cc | |
parent | f01bf10dc511268ead551a191a6a3211c006ba44 (diff) | |
download | MultiMC-f4c9cb8c1d395422b7e4f1c27ac92b6df08a39bb.tar MultiMC-f4c9cb8c1d395422b7e4f1c27ac92b6df08a39bb.tar.gz MultiMC-f4c9cb8c1d395422b7e4f1c27ac92b6df08a39bb.tar.lz MultiMC-f4c9cb8c1d395422b7e4f1c27ac92b6df08a39bb.tar.xz MultiMC-f4c9cb8c1d395422b7e4f1c27ac92b6df08a39bb.zip |
refactor indendation, fix a bug in MinecraftProcess & fix a bug in
InstanceLauncher
-rw-r--r-- | java/constants.h | 8 | ||||
-rw-r--r-- | java/javautils.cpp | 86 | ||||
-rw-r--r-- | java/javautils.h | 4 | ||||
-rw-r--r-- | libmultimc/include/minecraftprocess.h | 2 | ||||
-rw-r--r-- | libmultimc/src/minecraftprocess.cpp | 2 | ||||
-rw-r--r-- | libsettings/include/keyring.h | 78 | ||||
-rw-r--r-- | libsettings/src/keyring.cpp | 30 | ||||
-rw-r--r-- | libsettings/src/stubkeyring.cpp | 62 | ||||
-rw-r--r-- | libsettings/src/stubkeyring.h | 20 | ||||
-rw-r--r-- | libutil/include/cmdutils.h | 17 | ||||
-rw-r--r-- | libutil/src/cmdutils.cpp | 16 | ||||
-rw-r--r-- | main.cpp | 7 | ||||
-rw-r--r-- | test.cpp | 94 |
13 files changed, 211 insertions, 215 deletions
diff --git a/java/constants.h b/java/constants.h index 80697ebe..61aa5687 100644 --- a/java/constants.h +++ b/java/constants.h @@ -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; - }; + }; } diff --git a/java/javautils.cpp b/java/javautils.cpp index bf4b5cdf..4a359031 100644 --- a/java/javautils.cpp +++ b/java/javautils.cpp @@ -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; } } diff --git a/java/javautils.h b/java/javautils.h index 7f9b6d89..883eff1d 100644 --- a/java/javautils.h +++ b/java/javautils.h @@ -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); } diff --git a/libmultimc/include/minecraftprocess.h b/libmultimc/include/minecraftprocess.h index 8986f7ad..d6b9f612 100644 --- a/libmultimc/include/minecraftprocess.h +++ b/libmultimc/include/minecraftprocess.h @@ -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); diff --git a/libmultimc/src/minecraftprocess.cpp b/libmultimc/src/minecraftprocess.cpp index 943d76b1..3aecb1ee 100644 --- a/libmultimc/src/minecraftprocess.cpp +++ b/libmultimc/src/minecraftprocess.cpp @@ -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 } diff --git a/libsettings/include/keyring.h b/libsettings/include/keyring.h index 4563a268..afdc3bba 100644 --- a/libsettings/include/keyring.h +++ b/libsettings/include/keyring.h @@ -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 diff --git a/libsettings/src/keyring.cpp b/libsettings/src/keyring.cpp index 1b13e35c..9eaba684 100644 --- a/libsettings/src/keyring.cpp +++ b/libsettings/src/keyring.cpp @@ -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; diff --git a/libsettings/src/stubkeyring.cpp b/libsettings/src/stubkeyring.cpp index 0e29d2f2..963f3dd9 100644 --- a/libsettings/src/stubkeyring.cpp +++ b/libsettings/src/stubkeyring.cpp @@ -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") { } diff --git a/libsettings/src/stubkeyring.h b/libsettings/src/stubkeyring.h index f8e0eaaa..0566d5ab 100644 --- a/libsettings/src/stubkeyring.h +++ b/libsettings/src/stubkeyring.h @@ -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 diff --git a/libutil/include/cmdutils.h b/libutil/include/cmdutils.h index 32261dd7..83397e73 100644 --- a/libutil/include/cmdutils.h +++ b/libutil/include/cmdutils.h @@ -73,15 +73,6 @@ enum LIBUTIL_EXPORT Enum }; } -namespace OptionType -{ -enum LIBUTIL_EXPORT Enum -{ - Switch, - Option -}; -} - /** * @brief The ParsingError class */ @@ -210,6 +201,12 @@ public: 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 { @@ -226,7 +223,7 @@ private: QString metavar; QVariant def; // option - OptionType::Enum type; + OptionType type; QChar flag; }; diff --git a/libutil/src/cmdutils.cpp b/libutil/src/cmdutils.cpp index 3bf4e872..13db503d 100644 --- a/libutil/src/cmdutils.cpp +++ b/libutil/src/cmdutils.cpp @@ -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); @@ -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: @@ -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"); - - 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; - } - - 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; - } - - 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; - } + 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'); + + 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; + } + + 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; + } } |