mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-14 19:29:36 +01:00
Custom command line args & CVar override on Client.
This commit is contained in:
@@ -1,26 +1,140 @@
|
||||
using CommandLine;
|
||||
using System.Collections.Generic;
|
||||
using Robust.Shared.Utility;
|
||||
using C = System.Console;
|
||||
|
||||
namespace Robust.Client
|
||||
{
|
||||
internal sealed class CommandLineArgs
|
||||
{
|
||||
[Option("headless", Required = false, HelpText = "Run without graphics/audio.")]
|
||||
public bool Headless { get; set; }
|
||||
public bool Headless { get; }
|
||||
public bool SelfContained { get; }
|
||||
public bool Connect { get; }
|
||||
public string ConnectAddress { get; }
|
||||
public bool Launcher { get; }
|
||||
public string Username { get; }
|
||||
public IReadOnlyCollection<(string key, string value)> CVars { get; }
|
||||
|
||||
[Option("self-contained", Required = false,
|
||||
HelpText = "Store data relative to executable instead of user-global locations.")]
|
||||
public bool SelfContained { get; set; }
|
||||
// Manual parser because C# has no good command line parsing libraries. Also dependencies bad.
|
||||
// Also I don't like spending 100ms parsing command line args. Do you?
|
||||
public static bool TryParse(IReadOnlyList<string> args, out CommandLineArgs parsed)
|
||||
{
|
||||
parsed = null;
|
||||
var headless = false;
|
||||
var selfContained = false;
|
||||
var connect = false;
|
||||
var connectAddress = "localhost";
|
||||
var launcher = false;
|
||||
string username = null;
|
||||
var cvars = new List<(string, string)>();
|
||||
|
||||
[Option("connect", Required = false, HelpText = "Automatically connect to connect-address.")]
|
||||
public bool Connect { get; set; }
|
||||
using var enumerator = args.GetEnumerator();
|
||||
|
||||
[Option("connect-address", Required = false, HelpText = "Address to automatically connect to.")]
|
||||
public string ConnectAddress { get; set; } = "localhost";
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
var arg = enumerator.Current;
|
||||
if (arg == "--connect")
|
||||
{
|
||||
connect = true;
|
||||
}
|
||||
else if (arg == "--connect-address")
|
||||
{
|
||||
if (!enumerator.MoveNext())
|
||||
{
|
||||
C.WriteLine("Missing connection address.");
|
||||
return false;
|
||||
}
|
||||
|
||||
[Option("launcher", Required = false, HelpText = "Run in launcher mode (no main menu, auto connect).")]
|
||||
public bool Launcher { get; set; }
|
||||
connectAddress = enumerator.Current;
|
||||
}
|
||||
else if (arg == "--self-contained")
|
||||
{
|
||||
selfContained = true;
|
||||
}
|
||||
else if (arg == "--launcher")
|
||||
{
|
||||
launcher = true;
|
||||
}
|
||||
else if (arg == "--headless")
|
||||
{
|
||||
headless = true;
|
||||
}
|
||||
else if (arg == "--username")
|
||||
{
|
||||
if (!enumerator.MoveNext())
|
||||
{
|
||||
C.WriteLine("Missing username.");
|
||||
return false;
|
||||
}
|
||||
|
||||
[Option("username", Required = false, HelpText = "Override username.")]
|
||||
public string Username { get; set; }
|
||||
username = enumerator.Current;
|
||||
}
|
||||
else if (arg == "--cvar")
|
||||
{
|
||||
if (!enumerator.MoveNext())
|
||||
{
|
||||
C.WriteLine("Missing cvar value.");
|
||||
return false;
|
||||
}
|
||||
|
||||
var cvar = enumerator.Current;
|
||||
DebugTools.AssertNotNull(cvar);
|
||||
var split = cvar.Split("=");
|
||||
|
||||
if (split.Length < 2)
|
||||
{
|
||||
C.WriteLine("Expected = in cvar.");
|
||||
return false;
|
||||
}
|
||||
|
||||
cvars.Add((split[0], split[1]));
|
||||
}
|
||||
else if (arg == "--help")
|
||||
{
|
||||
PrintHelp();
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
C.WriteLine("Unknown argument: {0}", arg);
|
||||
}
|
||||
}
|
||||
|
||||
parsed = new CommandLineArgs(headless, selfContained, connect, launcher, username, cvars, connectAddress);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void PrintHelp()
|
||||
{
|
||||
C.WriteLine(@"
|
||||
Options:
|
||||
--headless Run without graphics/audio/input.
|
||||
--self-contained Store data relative to executable instead of user-global locations.
|
||||
--connect Automatically connect to connect-address.
|
||||
--connect-address Address to automatically connect to.
|
||||
Default: localhost
|
||||
--launcher Run in launcher mode (no main menu, auto connect).
|
||||
--username Override username.
|
||||
--cvar Specifies an additional cvar overriding the config file. Syntax is <key>=<value>
|
||||
--help Display this help text and exit.
|
||||
");
|
||||
}
|
||||
|
||||
private CommandLineArgs(
|
||||
bool headless,
|
||||
bool selfContained,
|
||||
bool connect,
|
||||
bool launcher,
|
||||
string username,
|
||||
IReadOnlyCollection<(string key, string value)> cVars,
|
||||
string connectAddress)
|
||||
{
|
||||
Headless = headless;
|
||||
SelfContained = selfContained;
|
||||
Connect = connect;
|
||||
Launcher = launcher;
|
||||
Username = username;
|
||||
CVars = cVars;
|
||||
ConnectAddress = connectAddress;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,6 +105,11 @@ namespace Robust.Client
|
||||
}
|
||||
}
|
||||
|
||||
if (_commandLineArgs != null)
|
||||
{
|
||||
_configurationManager.OverrideConVars(_commandLineArgs.CVars);
|
||||
}
|
||||
|
||||
_signalHandler.MaybeStart();
|
||||
|
||||
_resourceCache.Initialize(LoadConfigAndUserData ? userDataDir : null);
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using CommandLine;
|
||||
using Robust.Client.Interfaces;
|
||||
using Robust.Client.Interfaces;
|
||||
using Robust.Shared.Interfaces.Timing;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Log;
|
||||
@@ -19,8 +16,10 @@ namespace Robust.Client
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
Parser.Default.ParseArguments<CommandLineArgs>(args)
|
||||
.WithParsed(ParsedMain);
|
||||
if (CommandLineArgs.TryParse(args, out var parsed))
|
||||
{
|
||||
ParsedMain(parsed);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ParsedMain(CommandLineArgs args)
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
</PropertyGroup>
|
||||
<Import Project="..\MSBuild\Robust.DefineConstants.targets" />
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CommandLineParser" Version="2.6.0" />
|
||||
<PackageReference Include="DiscordRichPresence" Version="1.0.121" />
|
||||
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3" />
|
||||
<PackageReference Include="nfluidsynth" Version="0.2.4" />
|
||||
|
||||
Reference in New Issue
Block a user