Skip to content

Commit 7e94529

Browse files
committed
Last fixes
1 parent 1500b64 commit 7e94529

3 files changed

Lines changed: 42 additions & 18 deletions

File tree

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using UnityEngine;
43

54
namespace Unity.Netcode
65
{
@@ -13,22 +12,33 @@ public class CommandLineOptions
1312
/// <summary>
1413
/// Command-line options singleton
1514
/// </summary>
16-
public static CommandLineOptions Instance { get; private set; }
15+
[Obsolete("Not used anymore replaced by TryGetArg")]
16+
public static CommandLineOptions Instance
17+
{
18+
get
19+
{
20+
if (s_Instance == null)
21+
{
22+
s_Instance = new CommandLineOptions();
23+
}
24+
return s_Instance;
25+
}
26+
private set
27+
{
28+
s_Instance = value;
29+
}
30+
}
31+
private static CommandLineOptions s_Instance;
1732

1833
// Contains the current application instance domain's command line arguments
1934
private static readonly List<string> k_CommandLineArguments = new List<string>(Environment.GetCommandLineArgs());
2035

21-
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
22-
private static void InitializeOnLoad()
23-
{
24-
Instance = new CommandLineOptions();
25-
}
26-
2736
/// <summary>
2837
/// Returns the value of an argument or null if the argument is not present
2938
/// </summary>
3039
/// <param name="arg">The name of the argument</param>
3140
/// <returns><see cref="string"/>Value of the command line argument passed in.</returns>
41+
[Obsolete("Not used anymore replaced by TryGetArg")]
3242
public string GetArg(string arg)
3343
{
3444
var argIndex = k_CommandLineArguments.IndexOf(arg);
@@ -38,5 +48,23 @@ public string GetArg(string arg)
3848
}
3949
return null;
4050
}
51+
52+
/// <summary>
53+
/// Returns true if the argument was found.
54+
/// </summary>
55+
/// <param name="arg">The name of the argument to look up.</param>
56+
/// <param name="argValue">The argument's value, or <see langword="null"/> if not found.</param>
57+
/// <returns><c>true</c> if the argument was found; otherwise <c>false</c>.</returns>
58+
public static bool TryGetArg(string arg, out string argValue)
59+
{
60+
var argIndex = k_CommandLineArguments.IndexOf(arg);
61+
if (argIndex >= 0 && argIndex < k_CommandLineArguments.Count - 1)
62+
{
63+
argValue = k_CommandLineArguments[argIndex + 1];
64+
return true;
65+
}
66+
argValue = null;
67+
return false;
68+
}
4169
}
4270
}

com.unity.netcode.gameobjects/Runtime/Metrics/NetworkMetrics.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ internal class NetworkMetrics : INetworkMetrics
1212
{
1313
private const ulong k_MaxMetricsPerFrame = 1000L;
1414
private static readonly Dictionary<uint, string> k_SceneEventTypeNames;
15-
private static ProfilerMarker s_FrameDispatch;
16-
#if UNITY_EDITOR
17-
[UnityEngine.RuntimeInitializeOnLoadMethod(UnityEngine.RuntimeInitializeLoadType.SubsystemRegistration)]
18-
private static void ResetStaticsOnLoad() => s_FrameDispatch = new ProfilerMarker($"{nameof(NetworkMetrics)}.DispatchFrame");
19-
#endif
15+
private static readonly ProfilerMarker k_FrameDispatch;
2016

2117
static NetworkMetrics()
2218
{
@@ -25,7 +21,7 @@ static NetworkMetrics()
2521
{
2622
k_SceneEventTypeNames[(uint)type] = type.ToString();
2723
}
28-
s_FrameDispatch = new ProfilerMarker($"{nameof(NetworkMetrics)}.DispatchFrame");
24+
k_FrameDispatch = new ProfilerMarker($"{nameof(NetworkMetrics)}.DispatchFrame");
2925
}
3026

3127
private static string GetSceneEventTypeName(uint typeCode)
@@ -511,9 +507,9 @@ public void UpdatePacketLoss(float packetLoss)
511507

512508
public void DispatchFrame()
513509
{
514-
s_FrameDispatch.Begin();
510+
k_FrameDispatch.Begin();
515511
Dispatcher.Dispatch();
516-
s_FrameDispatch.End();
512+
k_FrameDispatch.End();
517513
m_NumberOfMetricsThisFrame = 0;
518514
}
519515

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ private bool ParseCommandLineOptionsPort(out ushort port)
869869
return true;
870870
}
871871
#else
872-
if (CommandLineOptions.Instance.GetArg(k_OverridePortArg) is string argValue)
872+
if (CommandLineOptions.TryGetArg(k_OverridePortArg, out var argValue))
873873
{
874874
port = (ushort)Convert.ChangeType(argValue, typeof(ushort));
875875
return true;
@@ -881,7 +881,7 @@ private bool ParseCommandLineOptionsPort(out ushort port)
881881

882882
private bool ParseCommandLineOptionsAddress(out string ipValue)
883883
{
884-
if (CommandLineOptions.Instance.GetArg(k_OverrideIpAddressArg) is string argValue)
884+
if (CommandLineOptions.TryGetArg(k_OverrideIpAddressArg, out var argValue))
885885
{
886886
ipValue = argValue;
887887
return true;

0 commit comments

Comments
 (0)