Files
OpenRCT2-Web/src/openrct2/command_line/RootCommands.cpp
T

498 lines
17 KiB
C++
Raw Normal View History

/*****************************************************************************
2024-12-31 20:30:04 +01:00
* Copyright (c) 2014-2025 OpenRCT2 developers
*
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
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
2018-04-19 13:26:19 +01:00
#include "../Context.h"
2018-06-22 22:57:42 +02:00
#include "../OpenRCT2.h"
#include "../PlatformEnvironment.h"
#include "../Version.h"
#include "../config/Config.h"
#include "../core/Console.hpp"
#include "../core/File.h"
2017-12-03 23:45:43 +01:00
#include "../core/Guard.hpp"
#include "../core/Memory.hpp"
#include "../core/Path.hpp"
#include "../core/String.hpp"
2018-06-22 22:57:42 +02:00
#include "../localisation/Language.h"
2025-02-24 04:43:06 -05:00
#include "../network/Network.h"
#include "../object/ObjectRepository.h"
#include "../park/ParkFile.h"
2018-06-22 22:57:42 +02:00
#include "../platform/Crash.h"
2022-02-18 21:57:00 +01:00
#include "../platform/Platform.h"
#include "../scripting/ScriptEngine.h"
#include "CommandLine.hpp"
2018-06-22 22:57:42 +02:00
#include <ctime>
2018-11-21 23:16:04 +01:00
#include <iterator>
2019-02-23 15:54:17 +01:00
#include <string>
2018-06-22 22:57:42 +02:00
2024-05-10 12:10:21 +02:00
using namespace OpenRCT2;
2016-10-23 16:16:20 +01:00
#ifdef USE_BREAKPAD
#define IMPLIES_SILENT_BREAKPAD ", implies --silent-breakpad"
2016-10-23 16:16:20 +01:00
#else
#define IMPLIES_SILENT_BREAKPAD
2016-10-23 16:16:20 +01:00
#endif // USE_BREAKPAD
#ifndef DISABLE_NETWORK
Network::Mode gNetworkStart = Network::Mode::none;
2019-02-23 15:54:17 +01:00
std::string gNetworkStartHost;
int32_t gNetworkStartPort = Network::kNetworkDefaultPort;
2019-02-23 15:54:17 +01:00
std::string gNetworkStartAddress;
2016-10-23 16:16:20 +01:00
2018-06-22 22:57:42 +02:00
static uint32_t _port = 0;
static char* _address = nullptr;
2016-10-23 16:16:20 +01:00
#endif
2018-06-22 22:57:42 +02:00
static bool _help = false;
static bool _version = false;
static bool _noInstall = false;
static bool _all = false;
static bool _about = false;
static bool _verbose = false;
static bool _headless = false;
static bool _silentReplays = false;
2022-01-27 14:21:46 +01:00
static u8string _password = {};
static u8string _userDataPath = {};
static u8string _openrct2DataPath = {};
static u8string _rct1DataPath = {};
static u8string _rct2DataPath = {};
2018-06-22 22:57:42 +02:00
static bool _silentBreakpad = false;
2018-01-10 16:14:36 +01:00
// clang-format off
2025-01-19 16:59:45 +01:00
static constexpr CommandLineOptionDefinition kStandardOptions[]
{
{ CMDLINE_TYPE_SWITCH, &_help, 'h', "help", "show this help message and exit" },
{ CMDLINE_TYPE_SWITCH, &_version, 'v', "version", "show version information and exit" },
{ CMDLINE_TYPE_SWITCH, &_noInstall, 'n', "no-install", "do not install scenario if passed" },
{ CMDLINE_TYPE_SWITCH, &_all, 'a', "all", "show help for all commands" },
2025-01-19 16:59:45 +01:00
{ CMDLINE_TYPE_SWITCH, &_about, kNAC, "about", "show information about " OPENRCT2_NAME },
{ CMDLINE_TYPE_SWITCH, &_verbose, kNAC, "verbose", "log verbose messages" },
{ CMDLINE_TYPE_SWITCH, &_headless, kNAC, "headless", "run " OPENRCT2_NAME " headless" IMPLIES_SILENT_BREAKPAD },
{ CMDLINE_TYPE_SWITCH, &_silentReplays, kNAC, "silent-replays", "use unobtrusive replays" },
2024-03-04 18:15:23 +00:00
#ifndef DISABLE_NETWORK
2025-01-19 16:59:45 +01:00
{ CMDLINE_TYPE_INTEGER, &_port, kNAC, "port", "port to use for hosting or joining a server" },
{ CMDLINE_TYPE_STRING, &_address, kNAC, "address", "address to listen on when hosting a server" },
2024-03-04 18:15:23 +00:00
#endif
2025-01-19 16:59:45 +01:00
{ CMDLINE_TYPE_STRING, &_password, kNAC, "password", "password needed to join the server" },
{ CMDLINE_TYPE_STRING, &_userDataPath, kNAC, "user-data-path", "path to the user data directory (containing config.ini)" },
{ CMDLINE_TYPE_STRING, &_openrct2DataPath, kNAC, "openrct2-data-path", "path to the OpenRCT2 data directory (containing languages)" },
{ CMDLINE_TYPE_STRING, &_rct1DataPath, kNAC, "rct1-data-path", "path to the RollerCoaster Tycoon 1 data directory (containing data/csg1.dat)" },
{ CMDLINE_TYPE_STRING, &_rct2DataPath, kNAC, "rct2-data-path", "path to the RollerCoaster Tycoon 2 data directory (containing data/g1.dat)" },
#ifdef USE_BREAKPAD
2025-01-19 16:59:45 +01:00
{ CMDLINE_TYPE_SWITCH, &_silentBreakpad, kNAC, "silent-breakpad", "make breakpad crash reporting silent" },
#endif // USE_BREAKPAD
kOptionTableEnd
};
static exitcode_t HandleNoCommand(CommandLineArgEnumerator * enumerator);
static exitcode_t HandleCommandEdit(CommandLineArgEnumerator * enumerator);
static exitcode_t HandleCommandIntro(CommandLineArgEnumerator * enumerator);
2018-06-03 17:43:37 +02:00
#ifndef DISABLE_NETWORK
static exitcode_t HandleCommandHost(CommandLineArgEnumerator * enumerator);
static exitcode_t HandleCommandJoin(CommandLineArgEnumerator * enumerator);
2018-06-03 17:43:37 +02:00
#endif
static exitcode_t HandleCommandSetRCT2(CommandLineArgEnumerator * enumerator);
static exitcode_t HandleCommandScanObjects(CommandLineArgEnumerator * enumerator);
2022-06-20 21:26:40 +02:00
#if defined(_WIN32)
2016-04-21 21:41:53 +01:00
static bool _removeShell = false;
static constexpr CommandLineOptionDefinition RegisterShellOptions[]
2016-04-21 21:41:53 +01:00
{
{ CMDLINE_TYPE_SWITCH, &_removeShell, 'd', "remove", "remove shell integration" },
};
static exitcode_t HandleCommandRegisterShell(CommandLineArgEnumerator * enumerator);
#endif
static void PrintAbout();
static void PrintVersion();
static void PrintLaunchInformation();
const CommandLineCommand CommandLine::kRootCommands[]
{
// Main commands
#ifndef DISABLE_HTTP
2025-01-19 16:59:45 +01:00
DefineCommand("", "<uri>", kStandardOptions, HandleNoCommand ),
DefineCommand("edit", "<uri>", kStandardOptions, HandleCommandEdit ),
#else
2025-01-19 16:59:45 +01:00
DefineCommand("", "<path>", kStandardOptions, HandleNoCommand ),
DefineCommand("edit", "<path>", kStandardOptions, HandleCommandEdit ),
#endif
2025-01-19 16:59:45 +01:00
DefineCommand("intro", "", kStandardOptions, HandleCommandIntro ),
#ifndef DISABLE_NETWORK
2025-01-19 16:59:45 +01:00
DefineCommand("host", "<uri>", kStandardOptions, HandleCommandHost ),
DefineCommand("join", "<hostname>", kStandardOptions, HandleCommandJoin ),
#endif
2025-01-19 16:59:45 +01:00
DefineCommand("set-rct2", "<path>", kStandardOptions, HandleCommandSetRCT2),
DefineCommand("scan-objects", "<path>", kStandardOptions, HandleCommandScanObjects),
DefineCommand("handle-uri", "openrct2://.../", kStandardOptions, CommandLine::HandleCommandUri),
2022-06-20 21:26:40 +02:00
#if defined(_WIN32)
2016-04-21 21:41:53 +01:00
DefineCommand("register-shell", "", RegisterShellOptions, HandleCommandRegisterShell),
#endif
// Sub-commands
DefineSubCommand("convert", CommandLine::kConvertCommands ),
DefineSubCommand("screenshot", CommandLine::kScreenshotCommands ),
DefineSubCommand("sprite", CommandLine::kSpriteCommands ),
DefineSubCommand("simulate", CommandLine::kSimulateCommands ),
DefineSubCommand("parkinfo", CommandLine::kParkInfoCommands ),
kCommandTableEnd
};
const CommandLineExample CommandLine::kRootExamples[]
{
2016-01-10 23:59:51 +00:00
{ "./my_park.sv6", "open a saved park" },
{ "./SnowyPark.sc6", "install and open a scenario" },
{ "./ShuttleLoop.td6", "install a track" },
#ifndef DISABLE_HTTP
{ "https://example.org/files/ExamplePark.sv6", "download and open a saved park" },
#endif
#ifndef DISABLE_NETWORK
2016-01-10 23:59:51 +00:00
{ "host ./my_park.sv6 --port 11753 --headless", "run a headless server for a saved park" },
#endif
kExampleTableEnd
};
2018-01-10 16:14:36 +01:00
// clang-format on
exitcode_t CommandLine::HandleCommandDefault()
{
exitcode_t result = EXITCODE_CONTINUE;
if (_about)
{
PrintAbout();
result = EXITCODE_OK;
2018-06-22 22:57:42 +02:00
}
else
{
if (_verbose)
{
2024-03-04 18:15:23 +00:00
_log_levels[EnumValue(DiagnosticLevel::Verbose)] = true;
PrintLaunchInformation();
}
if (_version)
{
if (!_verbose)
{
PrintVersion();
}
result = EXITCODE_OK;
}
}
if (_help || _all)
{
CommandLine::PrintHelp(_all);
result = EXITCODE_OK;
}
gOpenRCT2Headless = _headless;
2017-06-07 23:17:19 +01:00
gOpenRCT2NoGraphics = _headless;
gOpenRCT2SilentBreakpad = _silentBreakpad || _headless;
2022-01-27 14:21:46 +01:00
if (!_userDataPath.empty())
{
2022-01-27 14:21:46 +01:00
gCustomUserDataPath = Path::GetAbsolute(_userDataPath);
}
2022-01-27 14:21:46 +01:00
if (!_openrct2DataPath.empty())
{
2022-01-27 14:21:46 +01:00
gCustomOpenRCT2DataPath = Path::GetAbsolute(_openrct2DataPath);
}
2022-01-27 14:21:46 +01:00
if (!_rct1DataPath.empty())
{
gCustomRCT1DataPath = Path::GetAbsolute(_rct1DataPath);
}
2022-01-27 14:21:46 +01:00
if (!_rct2DataPath.empty())
{
gCustomRCT2DataPath = Path::GetAbsolute(_rct2DataPath);
}
2022-01-27 14:21:46 +01:00
if (!_password.empty())
{
2022-01-27 14:21:46 +01:00
gCustomPassword = _password;
}
if (_silentReplays)
{
gSilentReplays = _silentReplays;
}
return result;
}
2018-06-22 22:57:42 +02:00
exitcode_t HandleNoCommand(CommandLineArgEnumerator* enumerator)
{
exitcode_t result = CommandLine::HandleCommandDefault();
if (result != EXITCODE_CONTINUE)
{
return result;
}
2018-06-22 22:57:42 +02:00
const char* parkUri;
2016-01-10 22:05:48 +00:00
if (enumerator->TryPopString(&parkUri) && parkUri[0] != '-')
{
2024-12-11 22:28:31 +01:00
String::set(gOpenRCT2StartupActionPath, sizeof(gOpenRCT2StartupActionPath), parkUri);
gOpenRCT2StartupAction = StartupAction::Open;
}
2016-11-13 20:17:49 +01:00
return EXITCODE_CONTINUE;
}
2018-06-22 22:57:42 +02:00
exitcode_t HandleCommandEdit(CommandLineArgEnumerator* enumerator)
{
exitcode_t result = CommandLine::HandleCommandDefault();
if (result != EXITCODE_CONTINUE)
{
return result;
}
2018-06-22 22:57:42 +02:00
const char* parkUri;
if (!enumerator->TryPopString(&parkUri))
{
2016-01-11 21:49:13 +00:00
Console::Error::WriteLine("Expected path or URL to a saved park.");
return EXITCODE_FAIL;
}
2024-12-11 22:28:31 +01:00
String::set(gOpenRCT2StartupActionPath, sizeof(gOpenRCT2StartupActionPath), parkUri);
gOpenRCT2StartupAction = StartupAction::Edit;
return EXITCODE_CONTINUE;
}
2018-06-22 22:57:42 +02:00
exitcode_t HandleCommandIntro([[maybe_unused]] CommandLineArgEnumerator* enumerator)
{
exitcode_t result = CommandLine::HandleCommandDefault();
if (result != EXITCODE_CONTINUE)
{
return result;
}
gOpenRCT2StartupAction = StartupAction::Intro;
return EXITCODE_CONTINUE;
}
#ifndef DISABLE_NETWORK
2018-06-22 22:57:42 +02:00
exitcode_t HandleCommandHost(CommandLineArgEnumerator* enumerator)
{
exitcode_t result = CommandLine::HandleCommandDefault();
if (result != EXITCODE_CONTINUE)
{
return result;
}
2018-06-22 22:57:42 +02:00
const char* parkUri;
if (!enumerator->TryPopString(&parkUri))
{
2016-03-05 12:20:02 +00:00
Console::Error::WriteLine("Expected path or URL to a scenario or saved park.");
return EXITCODE_FAIL;
}
gOpenRCT2StartupAction = StartupAction::Open;
2024-12-11 22:28:31 +01:00
String::set(gOpenRCT2StartupActionPath, sizeof(gOpenRCT2StartupActionPath), parkUri);
gNetworkStart = Network::Mode::server;
gNetworkStartPort = _port;
2024-12-11 22:28:31 +01:00
gNetworkStartAddress = String::toStd(_address);
2017-05-04 12:48:18 +02:00
return EXITCODE_CONTINUE;
}
2018-06-22 22:57:42 +02:00
exitcode_t HandleCommandJoin(CommandLineArgEnumerator* enumerator)
{
exitcode_t result = CommandLine::HandleCommandDefault();
if (result != EXITCODE_CONTINUE)
{
return result;
}
2018-06-22 22:57:42 +02:00
const char* hostname;
if (!enumerator->TryPopString(&hostname))
{
2016-01-11 21:49:13 +00:00
Console::Error::WriteLine("Expected a hostname or IP address to the server to connect to.");
return EXITCODE_FAIL;
}
gNetworkStart = Network::Mode::client;
gNetworkStartPort = _port;
2019-02-23 15:54:17 +01:00
gNetworkStartHost = hostname;
return EXITCODE_CONTINUE;
}
#endif // DISABLE_NETWORK
2018-06-22 22:57:42 +02:00
static exitcode_t HandleCommandSetRCT2(CommandLineArgEnumerator* enumerator)
{
exitcode_t result = CommandLine::HandleCommandDefault();
if (result != EXITCODE_CONTINUE)
{
return result;
}
// Get the path that was passed
2018-06-22 22:57:42 +02:00
const utf8* rawPath;
if (!enumerator->TryPopString(&rawPath))
{
Console::Error::WriteLine("Expected a path.");
return EXITCODE_FAIL;
}
const auto path = Path::GetAbsolute(rawPath);
// Check if path exists
Console::WriteLine("Checking path...");
if (!Path::DirectoryExists(path))
{
Console::Error::WriteLine("The path '%s' does not exist", path.c_str());
return EXITCODE_FAIL;
}
// Check if g1.dat exists (naive but good check)
Console::WriteLine("Checking g1.dat...");
2022-02-27 22:23:35 +01:00
auto pathG1Check = Path::Combine(path, u8"Data", u8"g1.dat");
if (!File::Exists(pathG1Check))
{
Console::Error::WriteLine("RCT2 path not valid.");
Console::Error::WriteLine("Unable to find %s.", pathG1Check.c_str());
return EXITCODE_FAIL;
}
// Update RCT2 path in config
auto env = OpenRCT2::CreatePlatformEnvironment();
2025-03-25 23:57:36 +01:00
auto configPath = env->GetFilePath(OpenRCT2::PathId::config);
2024-05-10 12:10:21 +02:00
Config::SetDefaults();
Config::OpenFromPath(configPath);
Config::Get().general.RCT2Path = path;
if (Config::SaveToPath(configPath))
{
Console::WriteFormat("Updating RCT2 path to '%s'.", path.c_str());
Console::WriteLine();
Console::WriteLine("Updated config.ini");
return EXITCODE_OK;
}
2021-09-15 22:22:15 +02:00
Console::Error::WriteLine("Unable to update config.ini");
return EXITCODE_FAIL;
}
2018-06-22 22:57:42 +02:00
static exitcode_t HandleCommandScanObjects([[maybe_unused]] CommandLineArgEnumerator* enumerator)
{
exitcode_t result = CommandLine::HandleCommandDefault();
if (result != EXITCODE_CONTINUE)
{
return result;
}
2018-04-19 13:26:19 +01:00
gOpenRCT2Headless = true;
gOpenRCT2NoGraphics = true;
2017-08-31 18:34:21 +01:00
2021-08-20 23:38:15 +02:00
auto context = OpenRCT2::CreateContext();
auto& env = context->GetPlatformEnvironment();
2018-04-20 19:58:59 +01:00
auto objectRepository = CreateObjectRepository(env);
2024-05-10 12:10:21 +02:00
objectRepository->Construct(Config::Get().general.Language);
return EXITCODE_OK;
}
2022-06-20 21:26:40 +02:00
#if defined(_WIN32)
2018-06-22 22:57:42 +02:00
static exitcode_t HandleCommandRegisterShell([[maybe_unused]] CommandLineArgEnumerator* enumerator)
2016-04-21 21:41:53 +01:00
{
exitcode_t result = CommandLine::HandleCommandDefault();
if (result != EXITCODE_CONTINUE)
{
return result;
}
if (!_removeShell)
{
Platform::SetUpFileAssociations();
2016-04-21 21:41:53 +01:00
}
else
{
Platform::RemoveFileAssociations();
2016-04-21 21:41:53 +01:00
}
return EXITCODE_OK;
}
2022-06-20 21:26:40 +02:00
#endif // defined(_WIN32)
2016-04-21 21:41:53 +01:00
static void PrintAbout()
{
PrintVersion();
Console::WriteLine();
Console::WriteLine("OpenRCT2 is an amusement park simulation game based upon the popular game");
Console::WriteLine("RollerCoaster Tycoon 2, written by Chris Sawyer. It attempts to mimic the ");
Console::WriteLine("original game as closely as possible while extending it with new features.");
Console::WriteLine("OpenRCT2 is licensed under the GNU General Public License version 3.0, but");
Console::WriteLine("includes some 3rd party software under different licenses. See the file");
Console::WriteLine("\"licence.txt\" shipped with the game for details.");
Console::WriteLine();
Console::WriteLine("Website: https://openrct2.io");
Console::WriteLine("GitHub: https://github.com/OpenRCT2/OpenRCT2");
Console::WriteLine("Contributors: https://github.com/OpenRCT2/OpenRCT2/blob/develop/contributors.md");
Console::WriteLine("Privacy Policy: https://github.com/OpenRCT2/OpenRCT2/blob/develop/PRIVACY.md");
Console::WriteLine();
}
static void PrintVersion()
{
2024-10-30 12:17:27 +01:00
u8string versionInfo = gVersionInfoFull;
Console::WriteLine(versionInfo.c_str());
Console::WriteFormat("%s (%s)", OPENRCT2_PLATFORM, OPENRCT2_ARCHITECTURE);
Console::WriteLine();
Console::WriteFormat("Network version: %s", Network::GetVersion().c_str());
Console::WriteLine();
#ifdef ENABLE_SCRIPTING
2025-01-19 16:59:45 +01:00
Console::WriteFormat("Plugin API version: %d", OpenRCT2::Scripting::kPluginApiVersion);
Console::WriteLine();
#else
Console::WriteFormat("Plugin API not enabled in this build");
Console::WriteLine();
#endif
2025-01-19 16:59:45 +01:00
Console::WriteFormat("Current park file version: %d", OpenRCT2::kParkFileCurrentVersion);
Console::WriteLine();
2025-01-19 16:59:45 +01:00
Console::WriteFormat("Minimum park file version: %d", OpenRCT2::kParkFileMinVersion);
Console::WriteLine();
2022-07-30 22:59:23 +02:00
#ifdef USE_BREAKPAD
Console::WriteFormat("With breakpad support enabled");
Console::WriteLine();
#else
Console::WriteFormat("Breakpad support disabled");
Console::WriteLine();
#endif
}
static void PrintLaunchInformation()
{
2018-06-22 22:57:42 +02:00
char buffer[256];
time_t timer;
struct tm* tmInfo;
// Print name and version information
2024-10-30 12:17:27 +01:00
u8string versionInfo = gVersionInfoFull;
Console::WriteLine(versionInfo.c_str());
Console::WriteFormat("%s (%s)", OPENRCT2_PLATFORM, OPENRCT2_ARCHITECTURE);
Console::WriteLine();
Console::WriteLine();
// Print current time
time(&timer);
tmInfo = localtime(&timer);
strftime(buffer, sizeof(buffer), "%Y/%m/%d %H:%M:%S", tmInfo);
Console::WriteFormat("VERBOSE: time is %s", buffer);
Console::WriteLine();
// TODO Print other potential information (e.g. user, hardware)
}