Skip to content

Commit b5c104d

Browse files
committed
Creating a command line option class + singleton
1 parent 4f12176 commit b5c104d

File tree

3 files changed

+46
-16
lines changed

3 files changed

+46
-16
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Unity.Netcode
6+
{
7+
public class CommandLineOptions
8+
{
9+
public static CommandLineOptions Instance { get; private set; } = null!;
10+
11+
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
12+
private static void RuntimeInitializeOnLoad() => Instance = new CommandLineOptions();
13+
14+
// Contains the current application instance domain's command line arguments
15+
internal static List<string> CommandLineArguments = new List<string>();
16+
17+
// Invoked upon application start
18+
[RuntimeInitializeOnLoadMethod]
19+
private static void ParseCommandLineArguments()
20+
{
21+
// Get all the command line arguments to be parsed later and/or modified
22+
// prior to being parsed (for testing purposes).
23+
CommandLineArguments = new List<string>(Environment.GetCommandLineArgs());
24+
}
25+
26+
/// <summary>
27+
/// Returns the value of an argument or null if there the argument is not present
28+
/// </summary>
29+
/// <param name="arg">The name of the argument</param>
30+
public string GetArg(string arg)
31+
{
32+
var argIndex = CommandLineArguments.IndexOf(arg);
33+
if (argIndex >= 0 && argIndex < CommandLineArguments.Count - 1)
34+
{
35+
return CommandLineArguments[argIndex + 1];
36+
}
37+
38+
return null;
39+
}
40+
}
41+
}

com.unity.netcode.gameobjects/Runtime/Configuration/CommandLineOptions.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -808,29 +808,16 @@ public void SetClientRelayData(string ipAddress, ushort port, byte[] allocationI
808808
// Command line options
809809
private const string k_OverridePortArg = "-port";
810810

811-
private string GetArg(string[] commandLineArgs, string arg)
812-
{
813-
var argIndex = Array.IndexOf(commandLineArgs, arg);
814-
if (argIndex >= 0 && argIndex < commandLineArgs.Length - 1)
815-
{
816-
return commandLineArgs[argIndex + 1];
817-
}
818-
819-
return null;
820-
}
821-
822811
private bool ParseCommandLineOptions(out ushort port)
823812
{
824813
#if UNITY_SERVER && UNITY_DEDICATED_SERVER_ARGUMENTS_PRESENT
825-
826814
if (UnityEngine.DedicatedServer.Arguments.Port != null)
827815
{
828816
port = (ushort)UnityEngine.DedicatedServer.Arguments.Port;
829817
return true;
830818
}
831-
832819
#else
833-
if (GetArg(Environment.GetCommandLineArgs(), k_OverridePortArg) is string argValue)
820+
if (CommandLineOptions.Instance.GetArg(k_OverridePortArg) is string argValue)
834821
{
835822
port = (ushort)Convert.ChangeType(argValue, typeof(ushort));
836823
return true;
@@ -1626,14 +1613,14 @@ public override void Initialize(NetworkManager networkManager = null)
16261613
return;
16271614
}
16281615
#endif
1629-
16301616
m_NetworkManager = networkManager;
16311617

1618+
//If the port doesn't have a forced value and is set by a command line option, override it.
16321619
if (!m_HasForcedConnectionData && ParseCommandLineOptions(out var port))
16331620
{
16341621
if (m_NetworkManager?.LogLevel <= LogLevel.Developer)
16351622
{
1636-
Debug.Log($"Already has command line option set. Using connection data set to {ConnectionData.Address}:{port}");
1623+
Debug.Log($"The port is set by a command line option. Using following connection data: {ConnectionData.Address}:{port}");
16371624
}
16381625
ConnectionData.Port = (ushort)port;
16391626
}

0 commit comments

Comments
 (0)