diff options
author | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2024-02-11 21:39:51 +0100 |
---|---|---|
committer | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2024-03-02 17:05:37 +0100 |
commit | 78d6d9352f66f41963207b04a999d94c17d67cf2 (patch) | |
tree | c286d4a6ebd46bc89808d3e18b2d7d920ea10351 | |
parent | 8546361abaa6123acd9dc1429575d1fd1cf6a4ec (diff) | |
download | mana-78d6d9352f66f41963207b04a999d94c17d67cf2.tar.gz mana-78d6d9352f66f41963207b04a999d94c17d67cf2.tar.bz2 mana-78d6d9352f66f41963207b04a999d94c17d67cf2.tar.xz mana-78d6d9352f66f41963207b04a999d94c17d67cf2.zip |
Added support for -y / --server-type parameter
Usually this would be guessed correctly by the port, but now it is also
possible to just specify the server type and the port will be derived
from there, unless a default port is given in the branding file.
Closes #56
-rw-r--r-- | src/client.cpp | 32 | ||||
-rw-r--r-- | src/client.h | 3 | ||||
-rw-r--r-- | src/gui/customserverdialog.cpp | 2 | ||||
-rw-r--r-- | src/gui/logindialog.cpp | 12 | ||||
-rw-r--r-- | src/gui/logindialog.h | 3 | ||||
-rw-r--r-- | src/gui/register.cpp | 4 | ||||
-rw-r--r-- | src/gui/serverdialog.cpp | 25 | ||||
-rw-r--r-- | src/main.cpp | 9 | ||||
-rw-r--r-- | src/net/logindata.h | 2 | ||||
-rw-r--r-- | src/net/manaserv/connection.cpp | 2 | ||||
-rw-r--r-- | src/net/manaserv/connection.h | 4 | ||||
-rw-r--r-- | src/net/serverinfo.h | 24 |
12 files changed, 68 insertions, 54 deletions
diff --git a/src/client.cpp b/src/client.cpp index c337420d..f90ca05a 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -378,20 +378,34 @@ Client::Client(const Options &options): // Initialize default server mCurrentServer.hostname = options.serverName; mCurrentServer.port = options.serverPort; + mCurrentServer.type = ServerInfo::parseType(options.serverType); loginData.username = options.username; loginData.password = options.password; loginData.remember = config.getBoolValue("remember"); loginData.registerLogin = false; - if (mCurrentServer.hostname.empty()) - mCurrentServer.hostname = branding.getValue("defaultServer", std::string()); + if (mCurrentServer.type == ServerInfo::UNKNOWN && mCurrentServer.port != 0) + { + mCurrentServer.type = ServerInfo::defaultServerTypeForPort(mCurrentServer.port); + } - if (mCurrentServer.port == 0) + if (mCurrentServer.type == ServerInfo::UNKNOWN) { - mCurrentServer.port = (unsigned short) branding.getValue("defaultPort", - DEFAULT_PORT); mCurrentServer.type = ServerInfo::parseType( - branding.getValue("defaultServerType", "tmwathena")); + branding.getValue("defaultServerType", "tmwathena")); + } + + if (mCurrentServer.port == 0) + { + const uint16_t defaultPort = ServerInfo::defaultPortForServerType(mCurrentServer.type); + mCurrentServer.port = static_cast<uint16_t>( + branding.getValue("defaultPort", defaultPort)); + } + + const bool noServerList = branding.getValue("onlineServerList", std::string()).empty(); + if (mCurrentServer.hostname.empty() && noServerList) + { + mCurrentServer.hostname = branding.getValue("defaultServer", std::string()); } if (chatLogger) @@ -607,10 +621,7 @@ int Client::exec() // If a server was passed on the command line, or branding // provides a server and a blank server list, we skip the // server selection dialog. - if ((!mOptions.serverName.empty() && mOptions.serverPort) - || (!branding.getValue("defaultServer","").empty() && - branding.getValue("defaultPort",0) && - branding.getValue("onlineServerList", "").empty())) + if (!mCurrentServer.hostname.empty() && mCurrentServer.port) { mState = STATE_CONNECT_SERVER; @@ -933,6 +944,7 @@ int Client::exec() Net::getLoginHandler()->disconnect(); Net::getGameHandler()->disconnect(); + mCurrentServer.hostname.clear(); mState = STATE_CHOOSE_SERVER; break; diff --git a/src/client.h b/src/client.h index fe202ccc..ccee43ba 100644 --- a/src/client.h +++ b/src/client.h @@ -145,9 +145,10 @@ public: std::string configDir; std::string localDataDir; std::string screenshotDir; + std::string serverType; std::string serverName; - short serverPort = 0; + uint16_t serverPort = 0; }; Client(const Options &options); diff --git a/src/gui/customserverdialog.cpp b/src/gui/customserverdialog.cpp index 733a6b5c..455951ec 100644 --- a/src/gui/customserverdialog.cpp +++ b/src/gui/customserverdialog.cpp @@ -194,7 +194,7 @@ void CustomServerDialog::action(const gcn::ActionEvent &event) if (mPortField->getText().empty()) serverInfo.port = ServerInfo::defaultPortForServerType(serverInfo.type); else - serverInfo.port = (short) atoi(mPortField->getText().c_str()); + serverInfo.port = static_cast<uint16_t>(atoi(mPortField->getText().c_str())); // Tell the server has to be saved serverInfo.save = true; diff --git a/src/gui/logindialog.cpp b/src/gui/logindialog.cpp index 4d065d65..1f96e02d 100644 --- a/src/gui/logindialog.cpp +++ b/src/gui/logindialog.cpp @@ -22,14 +22,12 @@ #include "gui/logindialog.h" #include "client.h" -#include "configuration.h" #include "gui/okdialog.h" #include "gui/sdlinput.h" #include "gui/widgets/button.h" #include "gui/widgets/checkbox.h" -#include "gui/widgets/dropdown.h" #include "gui/widgets/label.h" #include "gui/widgets/layout.h" #include "gui/widgets/passwordfield.h" @@ -40,12 +38,6 @@ #include "net/net.h" #include "utils/gettext.h" -#include "utils/stringutils.h" - -static const int MAX_SERVER_LIST_SIZE = 5; -static const int LOGIN_DIALOG_WIDTH = 220; -static const int LOGIN_DIALOG_HEIGHT = 140; -static const int FIELD_WIDTH = LOGIN_DIALOG_WIDTH - 70; LoginDialog::LoginDialog(LoginData *loginData): Window(_("Login")), @@ -93,9 +85,7 @@ LoginDialog::LoginDialog(LoginData *loginData): mLoginButton->setEnabled(canSubmit()); } -LoginDialog::~LoginDialog() -{ -} +LoginDialog::~LoginDialog() = default; void LoginDialog::action(const gcn::ActionEvent &event) { diff --git a/src/gui/logindialog.h b/src/gui/logindialog.h index 5761f4a6..67814cd4 100644 --- a/src/gui/logindialog.h +++ b/src/gui/logindialog.h @@ -28,9 +28,6 @@ #include <guichan/keylistener.hpp> #include <guichan/listmodel.hpp> -#include <string> -#include <vector> - class LoginData; /** diff --git a/src/gui/register.cpp b/src/gui/register.cpp index b57ee975..0e23777c 100644 --- a/src/gui/register.cpp +++ b/src/gui/register.cpp @@ -167,7 +167,7 @@ void RegisterDialog::action(const gcn::ActionEvent &event) minUser); error = 1; } - else if (user.length() > maxUser - 1 ) + else if (user.length() > maxUser - 1) { // Name too long errorMessage = strprintf @@ -183,7 +183,7 @@ void RegisterDialog::action(const gcn::ActionEvent &event) minPass); error = 2; } - else if (mPasswordField->getText().length() > maxPass - 1 ) + else if (mPasswordField->getText().length() > maxPass - 1) { // Pass too long errorMessage = strprintf diff --git a/src/gui/serverdialog.cpp b/src/gui/serverdialog.cpp index cc1fa26a..0bff4c9e 100644 --- a/src/gui/serverdialog.cpp +++ b/src/gui/serverdialog.cpp @@ -51,7 +51,6 @@ #include <guichan/font.hpp> #include <cstdlib> -#include <iostream> #include <string> static const int MAX_SERVERLIST = 6; @@ -65,13 +64,13 @@ static std::string serverTypeToString(ServerInfo::Type type) case ServerInfo::MANASERV: return "ManaServ"; default: - return ""; + return std::string(); } } ServersListModel::ServersListModel(ServerInfos *servers, ServerDialog *parent): mServers(servers), - mVersionStrings(servers->size(), VersionString(0, "")), + mVersionStrings(servers->size(), VersionString(0, std::string())), mParent(parent) { } @@ -96,7 +95,7 @@ std::string ServersListModel::getElementAt(int elementIndex) void ServersListModel::setVersionString(int index, const std::string &version) { if (version.empty()) - mVersionStrings[index] = VersionString(0, ""); + mVersionStrings[index] = VersionString(0, std::string()); else { int width = gui->getFont()->getWidth(version); @@ -421,7 +420,7 @@ void ServerDialog::downloadServerList() // Fall back to manasource.org when neither branding nor config set it if (listFile.empty()) - listFile = "http://www.manasource.org/serverlist.xml"; + listFile = "https://www.manasource.org/serverlist.xml"; mDownload = new Net::Download(this, listFile, &downloadUpdate); mDownload->setFile(mDir + "/serverlist.xml"); @@ -490,7 +489,7 @@ void ServerDialog::loadServers() { if (xmlStrEqual(subNode->name, BAD_CAST "connection")) { - server.hostname = XML::getProperty(subNode, "hostname", ""); + server.hostname = XML::getProperty(subNode, "hostname", std::string()); server.port = XML::getProperty(subNode, "port", 0); if (server.port == 0) { @@ -552,13 +551,13 @@ void ServerDialog::loadCustomServers() const std::string descriptionKey = "MostUsedServerDescription" + index; ServerInfo server; - server.name = config.getValue(nameKey, ""); - server.hostname = config.getValue(hostNameKey, ""); - server.type = ServerInfo::parseType(config.getValue(typeKey, "")); + server.name = config.getValue(nameKey, std::string()); + server.hostname = config.getValue(hostNameKey, std::string()); + server.type = ServerInfo::parseType(config.getValue(typeKey, std::string())); - const int defaultPort = ServerInfo::defaultPortForServerType(server.type); - server.port = (unsigned short) config.getValue(portKey, defaultPort); - server.description = config.getValue(descriptionKey, ""); + const uint16_t defaultPort = ServerInfo::defaultPortForServerType(server.type); + server.port = static_cast<uint16_t>(config.getValue(portKey, defaultPort)); + server.description = config.getValue(descriptionKey, std::string()); // Stop on the first invalid server if (!server.isValid()) @@ -623,7 +622,7 @@ void ServerDialog::saveCustomServers(const ServerInfo ¤tServer, int index) // Insert an invalid entry at the end to make the loading stop there if (savedServerCount < MAX_SERVERLIST) - config.setValue("MostUsedServerName" + toString(savedServerCount), ""); + config.setValue("MostUsedServerName" + toString(savedServerCount), std::string()); // Restore the correct description if (index < 0) diff --git a/src/main.cpp b/src/main.cpp index b3fd699c..6244b946 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -53,6 +53,7 @@ static void printHelp() << _(" -c --character : Login with this character") << endl << _(" -s --server : Login server name or IP") << endl << _(" -p --port : Login server port") << endl + << _(" -y --server-type : Login server type") << endl << _(" --update-host : Use this update host") << endl << _(" -D --default : Choose default character server and " "character") << endl @@ -74,7 +75,7 @@ static void printVersion() static void parseOptions(int argc, char *argv[], Client::Options &options) { - const char *optstring = "hvud:U:P:Dc:s:p:C:"; + const char *optstring = "hvud:U:P:Dc:s:p:C:y:"; const struct option long_options[] = { { "config-dir", required_argument, nullptr, 'C' }, @@ -93,6 +94,7 @@ static void parseOptions(int argc, char *argv[], Client::Options &options) { "chat-log-dir", required_argument, nullptr, 'T' }, { "version", no_argument, nullptr, 'v' }, { "screenshot-dir", required_argument, nullptr, 'i' }, + { "server-type", required_argument, nullptr, 'y' }, { nullptr } }; @@ -132,7 +134,7 @@ static void parseOptions(int argc, char *argv[], Client::Options &options) options.serverName = optarg; break; case 'p': - options.serverPort = (short) atoi(optarg); + options.serverPort = static_cast<uint16_t>(atoi(optarg)); break; case 'u': options.skipUpdate = true; @@ -155,6 +157,9 @@ static void parseOptions(int argc, char *argv[], Client::Options &options) case 'i': options.screenshotDir = optarg; break; + case 'y': + options.serverType = optarg; + break; } } diff --git a/src/net/logindata.h b/src/net/logindata.h index 380f9061..1e19b541 100644 --- a/src/net/logindata.h +++ b/src/net/logindata.h @@ -40,7 +40,7 @@ public: std::string email; std::string captchaResponse; - Gender gender; + Gender gender = GENDER_UNSPECIFIED; bool remember; /**< Whether to store the username. */ bool registerLogin; /**< Whether an account is being registered. */ diff --git a/src/net/manaserv/connection.cpp b/src/net/manaserv/connection.cpp index d439f964..896d86ad 100644 --- a/src/net/manaserv/connection.cpp +++ b/src/net/manaserv/connection.cpp @@ -42,7 +42,7 @@ Connection::~Connection() connections--; } -bool Connection::connect(const std::string &address, short port) +bool Connection::connect(const std::string &address, enet_uint16 port) { logger->log("Net::Connection::connect(%s, %i)", address.c_str(), port); diff --git a/src/net/manaserv/connection.h b/src/net/manaserv/connection.h index e6646e0e..dfd45e31 100644 --- a/src/net/manaserv/connection.h +++ b/src/net/manaserv/connection.h @@ -25,8 +25,6 @@ #include <enet/enet.h> #include "net/manaserv/network.h" -#include <iosfwd> - namespace ManaServ { class MessageOut; @@ -49,7 +47,7 @@ namespace ManaServ * This method is non-blocking, use isConnected to check whether the * server is connected. */ - bool connect(const std::string &address, short port); + bool connect(const std::string &address, enet_uint16 port); /** * Disconnects from the given server. diff --git a/src/net/serverinfo.h b/src/net/serverinfo.h index f9119d19..481ef7cd 100644 --- a/src/net/serverinfo.h +++ b/src/net/serverinfo.h @@ -24,8 +24,9 @@ #include "utils/stringutils.h" -#include <string> +#include <cstdint> #include <deque> +#include <string> class ServerInfo { @@ -41,7 +42,7 @@ public: Type type = UNKNOWN; std::string name; std::string hostname; - unsigned short port = 0; + uint16_t port = 0; std::string description; VersionString version = std::make_pair(0, std::string()); @@ -51,7 +52,7 @@ public: bool isValid() const { - return !(hostname.empty() || port == 0 || type == UNKNOWN); + return !hostname.empty() && port != 0 && type != UNKNOWN; } void clear() @@ -76,24 +77,35 @@ public: if (compareStrI(type, "tmwathena") == 0) return TMWATHENA; // Used for backward compatibility - else if (compareStrI(type, "eathena") == 0) + if (compareStrI(type, "eathena") == 0) return TMWATHENA; - else if (compareStrI(type, "manaserv") == 0) + if (compareStrI(type, "manaserv") == 0) return MANASERV; return UNKNOWN; } - static unsigned short defaultPortForServerType(Type type) + static uint16_t defaultPortForServerType(Type type) { switch (type) { default: + case ServerInfo::UNKNOWN: + return 0; case ServerInfo::TMWATHENA: return 6901; case ServerInfo::MANASERV: return 9601; } } + + static Type defaultServerTypeForPort(uint16_t port) + { + if (port == 6901) + return TMWATHENA; + if (port == 9601) + return MANASERV; + return UNKNOWN; + } }; using ServerInfos = std::deque<ServerInfo>; |