Replace terniary null checks with coalescing.

This commit is contained in:
Paul Chote
2021-03-07 21:22:00 +00:00
committed by teinarss
parent 2473b8763b
commit d52ba83f96
43 changed files with 72 additions and 73 deletions

View File

@@ -65,7 +65,7 @@ namespace OpenRA
public CPos Location => OccupiesSpace.TopLeft;
public WPos CenterPosition => OccupiesSpace.CenterPosition;
public WRot Orientation => facing != null ? facing.Orientation : WRot.None;
public WRot Orientation => facing?.Orientation ?? WRot.None;
/// <summary>Value used to represent an invalid token.</summary>
public static readonly int InvalidConditionToken = -1;

View File

@@ -206,7 +206,7 @@ namespace OpenRA
public static void RestartGame()
{
var replay = OrderManager.Connection as ReplayConnection;
var replayName = replay != null ? replay.Filename : null;
var replayName = replay?.Filename;
var lobbyInfo = OrderManager.LobbyInfo;
// Reseed the RNG so this isn't an exact repeat of the last game

View File

@@ -112,7 +112,7 @@ namespace OpenRA.Graphics
int CurrentSequenceTickOrDefault()
{
const int DefaultTick = 40; // 25 fps == 40 ms
return CurrentSequence != null ? CurrentSequence.Tick : DefaultTick;
return CurrentSequence?.Tick ?? DefaultTick;
}
void PlaySequence(string sequenceName)

View File

@@ -38,16 +38,16 @@ namespace OpenRA.Graphics
public IRenderable[] Render(Actor self, WorldRenderer wr, PaletteReference pal)
{
var center = self.CenterPosition;
var offset = OffsetFunc != null ? OffsetFunc() : WVec.Zero;
var offset = OffsetFunc?.Invoke() ?? WVec.Zero;
var z = (ZOffset != null) ? ZOffset(center + offset) : 0;
var z = ZOffset?.Invoke(center + offset) ?? 0;
return Animation.Render(center, offset, z, pal);
}
public Rectangle ScreenBounds(Actor self, WorldRenderer wr)
{
var center = self.CenterPosition;
var offset = OffsetFunc != null ? OffsetFunc() : WVec.Zero;
var offset = OffsetFunc?.Invoke() ?? WVec.Zero;
return Animation.ScreenBounds(wr, center, offset);
}

View File

@@ -86,7 +86,7 @@ namespace OpenRA
public MapVisibility Visibility;
Lazy<Ruleset> rules;
public Ruleset Rules => rules != null ? rules.Value : null;
public Ruleset Rules => rules?.Value;
public bool InvalidCustomRules { get; private set; }
public bool DefinesUnsafeCustomRules { get; private set; }
public bool RulesLoaded { get; private set; }

View File

@@ -452,7 +452,7 @@ namespace OpenRA
existingDict.TryGetValue(key, out var existingNode);
overrideDict.TryGetValue(key, out var overrideNode);
var loc = overrideNode == null ? default(MiniYamlNode.SourceLocation) : overrideNode.Location;
var loc = overrideNode?.Location ?? default;
var comment = (overrideNode ?? existingNode).Comment;
var merged = (existingNode == null || overrideNode == null) ? overrideNode ?? existingNode :
new MiniYamlNode(key, MergePartial(existingNode.Value, overrideNode.Value), comment, loc);

View File

@@ -103,9 +103,9 @@ namespace OpenRA
return new PlayerBadge(
labelNode.Value.Value,
icon24Node != null ? icon24Node.Value.Value : null,
icon48Node != null ? icon48Node.Value.Value : null,
icon72Node != null ? icon72Node.Value.Value : null);
icon24Node?.Value.Value,
icon48Node?.Value.Value,
icon72Node?.Value.Value);
}
public Sprite GetIcon(PlayerBadge badge)

View File

@@ -323,9 +323,9 @@ namespace OpenRA
}
}
public float MusicSeekPosition => music != null ? music.SeekPosition : 0;
public float MusicSeekPosition => music?.SeekPosition ?? 0;
public float VideoSeekPosition => video != null ? video.SeekPosition : 0;
public float VideoSeekPosition => video?.SeekPosition ?? 0;
// Returns true if played successfully
public bool PlayPredefined(SoundType soundType, Ruleset ruleset, Player p, Actor voicedActor, string type, string definition, string variant,
@@ -340,11 +340,11 @@ namespace OpenRA
if (ruleset.Voices == null || ruleset.Notifications == null)
return false;
var rules = (voicedActor != null) ? ruleset.Voices[type] : ruleset.Notifications[type];
var rules = voicedActor != null ? ruleset.Voices[type] : ruleset.Notifications[type];
if (rules == null)
return false;
var id = voicedActor != null ? voicedActor.ActorID : 0;
var id = voicedActor?.ActorID ?? 0;
SoundPool pool;
var suffix = rules.DefaultVariant;

View File

@@ -592,7 +592,7 @@ namespace OpenRA.Support
Token lastToken = null;
for (var i = 0; ;)
{
var token = Token.GetNext(Expression, ref i, lastToken != null ? lastToken.Type : TokenType.Invalid);
var token = Token.GetNext(Expression, ref i, lastToken?.Type ?? TokenType.Invalid);
if (token == null)
{
// Sanity check parsed tree

View File

@@ -67,7 +67,7 @@ namespace OpenRA
LoadWidget(args, widget, c);
var logicNode = node.Value.Nodes.FirstOrDefault(n => n.Key == "Logic");
var logic = logicNode == null ? null : logicNode.Value.ToDictionary();
var logic = logicNode?.Value.ToDictionary();
args.Add("logicArgs", logic);
widget.PostInit(args);