Files
OpenRCT2-Web/src/openrct2/cmdline/UriHandler.cpp
T

102 lines
2.9 KiB
C++
Raw Normal View History

2016-11-12 02:21:42 +00:00
/*****************************************************************************
2020-07-21 15:04:34 +02:00
* Copyright (c) 2014-2020 OpenRCT2 developers
2016-11-12 02:21:42 +00:00
*
2018-06-15 14:07:34 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
2016-11-12 02:21:42 +00:00
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
2016-11-12 02:21:42 +00:00
*****************************************************************************/
2018-06-22 22:57:42 +02:00
#include "../OpenRCT2.h"
2016-11-12 02:21:42 +00:00
#include "../core/Console.hpp"
#include "../core/String.hpp"
#include "../network/network.h"
#include "CommandLine.hpp"
2018-06-22 22:57:42 +02:00
static exitcode_t HandleUri(const std::string& uri);
2017-03-08 20:45:49 +00:00
#ifndef DISABLE_NETWORK
2018-06-22 22:57:42 +02:00
static exitcode_t HandleUriJoin(const std::vector<std::string>& args);
static bool TryParseHostnamePort(
const std::string& hostnamePort, std::string* outHostname, int32_t* outPort, int32_t defaultPort);
2017-03-08 20:45:49 +00:00
#endif
2016-11-12 02:21:42 +00:00
2018-06-22 22:57:42 +02:00
exitcode_t CommandLine::HandleCommandUri(CommandLineArgEnumerator* enumerator)
2016-11-12 02:21:42 +00:00
{
2018-06-22 22:57:42 +02:00
const utf8* uri;
2016-11-12 02:21:42 +00:00
if (enumerator->TryPopString(&uri))
{
if (String::StartsWith(uri, "openrct2://"))
{
2018-06-22 22:57:42 +02:00
const utf8* uriCommand = uri + 11;
2016-11-12 02:21:42 +00:00
return HandleUri(uriCommand);
}
}
Console::Error::WriteLine("Invalid URI");
return EXITCODE_FAIL;
}
2018-06-22 22:57:42 +02:00
static exitcode_t HandleUri(const std::string& uri)
2016-11-12 02:21:42 +00:00
{
2017-03-08 20:45:49 +00:00
exitcode_t result = EXITCODE_CONTINUE;
auto args = String::Split(uri, "/");
2017-12-03 23:45:43 +01:00
if (!args.empty())
2016-11-12 02:21:42 +00:00
{
2017-03-08 20:45:49 +00:00
#ifndef DISABLE_NETWORK
std::string arg = args[0];
if (arg == "join")
2016-11-12 02:21:42 +00:00
{
2017-03-08 20:45:49 +00:00
result = HandleUriJoin(args);
2016-11-12 02:21:42 +00:00
}
2017-03-08 20:45:49 +00:00
#endif
}
return result;
}
#ifndef DISABLE_NETWORK
2018-06-22 22:57:42 +02:00
static exitcode_t HandleUriJoin(const std::vector<std::string>& args)
2017-03-08 20:45:49 +00:00
{
std::string hostname;
int32_t port;
2017-03-08 20:45:49 +00:00
if (args.size() > 1 && TryParseHostnamePort(args[1], &hostname, &port, NETWORK_DEFAULT_PORT))
{
// Set the network start configuration
gNetworkStart = NETWORK_MODE_CLIENT;
2019-02-23 15:54:17 +01:00
gNetworkStartHost = hostname;
2017-03-08 20:45:49 +00:00
gNetworkStartPort = port;
return EXITCODE_CONTINUE;
}
else
{
Console::Error::WriteLine("Expected hostname:port after join");
return EXITCODE_FAIL;
2016-11-12 02:21:42 +00:00
}
}
static bool TryParseHostnamePort(
const std::string& hostnamePort, std::string* outHostname, int32_t* outPort, int32_t defaultPort)
{
try
{
// Argument is in hostname:port format, so we need to split
std::string hostname = hostnamePort;
int32_t port = defaultPort;
size_t colonIndex = hostnamePort.find_first_of(':');
if (colonIndex != std::string::npos)
{
hostname = hostnamePort.substr(0, colonIndex);
port = std::stoi(hostnamePort.substr(colonIndex + 1));
}
*outPort = port;
*outHostname = hostname;
return true;
}
2018-06-22 22:57:42 +02:00
catch (const std::exception&)
{
return false;
}
}
2017-03-08 20:45:49 +00:00
#endif // DISABLE_NETWORK