Use Null-Propagation Operator

This commit is contained in:
teinarss
2020-08-16 11:38:14 +02:00
committed by Paul Chote
parent 8d27d22100
commit 9c4fd0e3d3
113 changed files with 219 additions and 464 deletions

View File

@@ -18,8 +18,7 @@ namespace OpenRA.Mods.Common.Traits.BotModules.Squads
public void Update(Squad squad)
{
if (currentState != null)
currentState.Tick(squad);
currentState?.Tick(squad);
}
public void ChangeState(Squad squad, IState newState, bool rememberPrevious)
@@ -27,14 +26,12 @@ namespace OpenRA.Mods.Common.Traits.BotModules.Squads
if (rememberPrevious)
previousState = currentState;
if (currentState != null)
currentState.Deactivate(squad);
currentState?.Deactivate(squad);
if (newState != null)
currentState = newState;
if (currentState != null)
currentState.Activate(squad);
currentState?.Activate(squad);
}
public void RevertToPreviousState(Squad squad, bool saveCurrentState)

View File

@@ -302,8 +302,7 @@ namespace OpenRA.Mods.Common.Traits
// If this bridge repair operation connects two pathfinding domains,
// update the domain index.
var domainIndex = self.World.WorldActor.TraitOrDefault<DomainIndex>();
if (domainIndex != null)
domainIndex.UpdateCells(self.World, footprint.Keys);
domainIndex?.UpdateCells(self.World, footprint.Keys);
if (LongBridgeSegmentIsDead() && !killedUnits)
{

View File

@@ -70,9 +70,7 @@ namespace OpenRA.Mods.Common.Traits
foreach (var cell in cells)
self.World.Map.CustomTerrain[cell] = terrainIndex;
var domainIndex = self.World.WorldActor.TraitOrDefault<DomainIndex>();
if (domainIndex != null)
domainIndex.UpdateCells(self.World, cells);
self.World.WorldActor.TraitOrDefault<DomainIndex>()?.UpdateCells(self.World, cells);
}
void INotifyAddedToWorld.AddedToWorld(Actor self)

View File

@@ -91,10 +91,7 @@ namespace OpenRA.Mods.Common.Traits
void INotifyLineBuildSegmentsChanged.SegmentRemoved(Actor self, Actor segment)
{
if (segments == null)
return;
segments.Remove(segment);
segments?.Remove(segment);
}
void INotifyAddedToWorld.AddedToWorld(Actor self)

View File

@@ -172,9 +172,7 @@ namespace OpenRA.Mods.Common.Traits
if (r == self.Owner)
return;
var exp = r.PlayerActor.TraitOrDefault<PlayerExperience>();
if (exp != null)
exp.GiveExperience(Info.PlayerExperience);
r.PlayerActor.TraitOrDefault<PlayerExperience>()?.GiveExperience(Info.PlayerExperience);
});
Repairers.Clear();

View File

@@ -131,8 +131,8 @@ namespace OpenRA.Mods.Common.Traits
// Manually manage the inner activity queue
var activity = currentTransform ?? transform.GetTransformActivity(self);
if (!order.Queued && activity.NextActivity != null)
activity.NextActivity.Cancel(self);
if (!order.Queued)
activity.NextActivity?.Cancel(self);
activity.Queue(new IssueOrderAfterTransform(order.OrderString, order.Target, Color.Green));

View File

@@ -95,8 +95,8 @@ namespace OpenRA.Mods.Common.Traits
// Manually manage the inner activity queue
var activity = currentTransform ?? transform.GetTransformActivity(self);
if (!order.Queued && activity.NextActivity != null)
activity.NextActivity.Cancel(self);
if (!order.Queued)
activity.NextActivity?.Cancel(self);
activity.Queue(new IssueOrderAfterTransform(order.OrderString, order.Target, Color.Green));

View File

@@ -110,8 +110,8 @@ namespace OpenRA.Mods.Common.Traits
// Manually manage the inner activity queue
var activity = currentTransform ?? transform.GetTransformActivity(self);
if (!order.Queued && activity.NextActivity != null)
activity.NextActivity.Cancel(self);
if (!order.Queued)
activity.NextActivity?.Cancel(self);
activity.Queue(new IssueOrderAfterTransform("Move", order.Target, Color.Green));

View File

@@ -129,8 +129,8 @@ namespace OpenRA.Mods.Common.Traits
// Manually manage the inner activity queue
var activity = currentTransform ?? transform.GetTransformActivity(self);
if (!order.Queued && activity.NextActivity != null)
activity.NextActivity.Cancel(self);
if (!order.Queued)
activity.NextActivity?.Cancel(self);
activity.Queue(new IssueOrderAfterTransform(order.OrderString, order.Target, Color.Green));

View File

@@ -123,8 +123,8 @@ namespace OpenRA.Mods.Common.Traits
// Manually manage the inner activity queue
var activity = currentTransform ?? transform.GetTransformActivity(self);
if (!order.Queued && activity.NextActivity != null)
activity.NextActivity.Cancel(self);
if (!order.Queued)
activity.NextActivity?.Cancel(self);
activity.Queue(new IssueOrderAfterTransform(order.OrderString, order.Target, Color.Green));

View File

@@ -41,8 +41,8 @@ namespace OpenRA.Mods.Common.Traits
if (!order.Queued || currentTransform == null)
return;
if (!order.Queued && currentTransform.NextActivity != null)
currentTransform.NextActivity.Cancel(self);
if (!order.Queued)
currentTransform.NextActivity?.Cancel(self);
currentTransform.Queue(new IssueOrderAfterTransform("DeployTransform", order.Target, Color.Green));

View File

@@ -44,20 +44,16 @@ namespace OpenRA.Mods.Common.Traits
void INotifyCrushed.OnCrush(Actor self, Actor crusher, BitSet<CrushClass> crushClasses)
{
var external = crusher.TraitsImplementing<ExternalCondition>()
.FirstOrDefault(t => t.Info.Condition == Info.OnCrushCondition && t.CanGrantCondition(crusher, self));
if (external != null)
external.GrantCondition(crusher, self, Info.OnCrushDuration);
crusher.TraitsImplementing<ExternalCondition>()
.FirstOrDefault(t => t.Info.Condition == Info.OnCrushCondition && t.CanGrantCondition(crusher, self))
?.GrantCondition(crusher, self, Info.OnCrushDuration);
}
void INotifyCrushed.WarnCrush(Actor self, Actor crusher, BitSet<CrushClass> crushClasses)
{
var external = crusher.TraitsImplementing<ExternalCondition>()
.FirstOrDefault(t => t.Info.Condition == Info.WarnCrushCondition && t.CanGrantCondition(crusher, self));
if (external != null)
external.GrantCondition(crusher, self, Info.WarnCrushDuration);
crusher.TraitsImplementing<ExternalCondition>()
.FirstOrDefault(t => t.Info.Condition == Info.WarnCrushCondition && t.CanGrantCondition(crusher, self))
?.GrantCondition(crusher, self, Info.WarnCrushDuration);
}
}
}

View File

@@ -36,11 +36,9 @@ namespace OpenRA.Mods.Common.Traits
if (IsTraitDisabled || other.IsDead)
return;
var external = other.TraitsImplementing<ExternalCondition>()
.FirstOrDefault(t => t.Info.Condition == Info.Condition && t.CanGrantCondition(other, self));
if (external != null)
external.GrantCondition(other, self, Info.Duration);
other.TraitsImplementing<ExternalCondition>()
.FirstOrDefault(t => t.Info.Condition == Info.Condition && t.CanGrantCondition(other, self))
?.GrantCondition(other, self, Info.Duration);
}
}
}

View File

@@ -248,9 +248,7 @@ namespace OpenRA.Mods.Common.Traits
{
self.World.AddToMaps(self, this);
var cs = self.World.WorldActor.TraitOrDefault<CrateSpawner>();
if (cs != null)
cs.IncrementCrates();
self.World.WorldActor.TraitOrDefault<CrateSpawner>()?.IncrementCrates();
if (self.World.Map.DistanceAboveTerrain(CenterPosition) > WDist.Zero && self.TraitOrDefault<Parachutable>() != null)
self.QueueActivity(new Parachute(self));
@@ -260,9 +258,7 @@ namespace OpenRA.Mods.Common.Traits
{
self.World.RemoveFromMaps(self, this);
var cs = self.World.WorldActor.TraitOrDefault<CrateSpawner>();
if (cs != null)
cs.DecrementCrates();
self.World.WorldActor.TraitOrDefault<CrateSpawner>()?.DecrementCrates();
}
}
}

View File

@@ -72,9 +72,7 @@ namespace OpenRA.Mods.Common.Traits
var recipient = actor; // loop variable in closure hazard
recipient.World.AddFrameEndTask(w =>
{
var gainsExperience = recipient.TraitOrDefault<GainsExperience>();
if (gainsExperience != null)
gainsExperience.GiveLevels(info.Levels);
recipient.TraitOrDefault<GainsExperience>()?.GiveLevels(info.Levels);
});
}

View File

@@ -90,11 +90,7 @@ namespace OpenRA.Mods.Common.Traits
var pilot = self.World.CreateActor(true, Info.PilotActor.ToLowerInvariant(), td);
if (!inAir)
{
var pilotMobile = pilot.TraitOrDefault<Mobile>();
if (pilotMobile != null)
pilotMobile.Nudge(pilot);
}
pilot.TraitOrDefault<Mobile>()?.Nudge(pilot);
else
Game.Sound.Play(SoundType.World, Info.ChuteSound, cp);
});

View File

@@ -66,9 +66,8 @@ namespace OpenRA.Mods.Common.Traits
killer.GiveExperience(Util.ApplyPercentageModifiers(exp, killerExperienceModifier));
}
var attackerExp = e.Attacker.Owner.PlayerActor.TraitOrDefault<PlayerExperience>();
if (attackerExp != null)
attackerExp.GiveExperience(Util.ApplyPercentageModifiers(exp, new[] { info.PlayerExperienceModifier }));
e.Attacker.Owner.PlayerActor.TraitOrDefault<PlayerExperience>()
?.GiveExperience(Util.ApplyPercentageModifiers(exp, new[] { info.PlayerExperienceModifier }));
}
}
}

View File

@@ -26,9 +26,7 @@ namespace OpenRA.Mods.Common.Traits
if (!building.AppearsFriendlyTo(self))
return;
var rb = building.TraitOrDefault<RepairableBuilding>();
if (rb != null)
rb.RepairBuilding(building, self.Owner);
building.TraitOrDefault<RepairableBuilding>()?.RepairBuilding(building, self.Owner);
}
}
}

View File

@@ -80,8 +80,7 @@ namespace OpenRA.Mods.Common.Traits
if (p != self.Owner && p.IsAlliedWith(self.Owner) && p != e.Attacker.Owner)
Game.Sound.PlayNotification(rules, p, "Speech", info.AllyNotification, p.Faction.InternalName);
if (radarPings != null)
radarPings.Add(() => self.Owner.IsAlliedWith(self.World.RenderPlayer), self.CenterPosition, info.RadarPingColor, info.RadarPingDuration);
radarPings?.Add(() => self.Owner.IsAlliedWith(self.World.RenderPlayer), self.CenterPosition, info.RadarPingColor, info.RadarPingDuration);
}
lastAttackTime = self.World.WorldTick;

View File

@@ -244,10 +244,7 @@ namespace OpenRA.Mods.Common.Traits
case "DevPlayerExperience":
{
var playerExperience = self.Owner.PlayerActor.TraitOrDefault<PlayerExperience>();
if (playerExperience != null)
playerExperience.GiveExperience((int)order.ExtraData);
self.Owner.PlayerActor.TraitOrDefault<PlayerExperience>()?.GiveExperience((int)order.ExtraData);
break;
}

View File

@@ -61,8 +61,7 @@ namespace OpenRA.Mods.Common.Traits
{
Game.Sound.PlayNotification(self.World.Map.Rules, self.Owner, "Speech", info.Notification, self.Owner.Faction.InternalName);
if (radarPings != null)
radarPings.Add(() => self.Owner.IsAlliedWith(self.World.RenderPlayer), self.CenterPosition, info.RadarPingColor, info.RadarPingDuration);
radarPings?.Add(() => self.Owner.IsAlliedWith(self.World.RenderPlayer), self.CenterPosition, info.RadarPingColor, info.RadarPingDuration);
}
lastAttackTime = self.World.WorldTick;

View File

@@ -170,8 +170,7 @@ namespace OpenRA.Mods.Common.Traits
foreach (var t in buildingInfo.Tiles(targetLocation))
{
var host = buildingInfluence.GetBuildingAt(t);
if (host != null)
host.World.Remove(host);
host?.World.Remove(host);
}
}
@@ -193,14 +192,10 @@ namespace OpenRA.Mods.Common.Traits
queue.EndProduction(item);
// FindBaseProvider may return null if the build anywhere cheat is active
// BuildingInfo.IsCloseEnoughToBase has already verified that this is a valid build location
if (buildingInfo.RequiresBaseProvider)
{
// May be null if the build anywhere cheat is active
// BuildingInfo.IsCloseEnoughToBase has already verified that this is a valid build location
var provider = buildingInfo.FindBaseProvider(w, self.Owner, targetLocation);
if (provider != null)
provider.BeginCooldown();
}
buildingInfo.FindBaseProvider(w, self.Owner, targetLocation)?.BeginCooldown();
if (GetNumBuildables(self.Owner) > prevItems)
triggerNotification = true;

View File

@@ -61,8 +61,7 @@ namespace OpenRA.Mods.Common.Traits
{
terrainColor[uv] = GetColor(world.Map, uv);
if (CellTerrainColorChanged != null)
CellTerrainColorChanged(uv);
CellTerrainColorChanged?.Invoke(uv);
}
public void WorldLoaded(World w, WorldRenderer wr)

View File

@@ -465,9 +465,7 @@ namespace OpenRA.Mods.Common.Traits
protected void PauseProduction(string itemName, bool paused)
{
var item = Queue.FirstOrDefault(a => a.Item == itemName);
if (item != null)
item.Pause(paused);
Queue.FirstOrDefault(a => a.Item == itemName)?.Pause(paused);
}
protected void CancelProduction(string itemName, uint numberToCancel)
@@ -646,8 +644,7 @@ namespace OpenRA.Mods.Common.Traits
if (Done)
{
if (OnComplete != null)
OnComplete();
OnComplete?.Invoke();
return;
}

View File

@@ -105,14 +105,11 @@ namespace OpenRA.Mods.Common.Traits.Render
{
Reverse(self, () =>
{
var wsb = wsbs.FirstEnabledTraitOrDefault();
// HACK: The actor remains alive and active for one tick before the followup activity
// (sell/transform/etc) runs. This causes visual glitches that we attempt to minimize
// by forcing the animation to frame 0 and regranting the make condition.
// These workarounds will break the actor if the followup activity doesn't dispose it!
if (wsb != null)
wsb.DefaultAnimation.PlayFetchIndex(info.Sequence, () => 0);
wsbs.FirstEnabledTraitOrDefault()?.DefaultAnimation.PlayFetchIndex(info.Sequence, () => 0);
token = self.GrantCondition(info.Condition);

View File

@@ -159,8 +159,7 @@ namespace OpenRA.Mods.Common.Traits.Render
void ITick.Tick(Actor self)
{
if (shadow != null)
shadow.Tick();
shadow?.Tick();
}
IEnumerable<IRenderable> IRender.Render(Actor self, WorldRenderer wr)

View File

@@ -97,8 +97,7 @@ namespace OpenRA.Mods.Common.Traits.Render
DefaultAnimation.PlayThen(NormalizeSequence(self, name), () =>
{
CancelCustomAnimation(self);
if (after != null)
after();
after?.Invoke();
});
}
@@ -112,8 +111,7 @@ namespace OpenRA.Mods.Common.Traits.Render
DefaultAnimation.PlayBackwardsThen(NormalizeSequence(self, name), () =>
{
CancelCustomAnimation(self);
if (after != null)
after();
after?.Invoke();
});
}

View File

@@ -132,8 +132,7 @@ namespace OpenRA.Mods.Common.Traits.Render
DefaultAnimation.PlayThen(NormalizeSequence(self, name), () =>
{
CancelCustomAnimation(self);
if (after != null)
after();
after?.Invoke();
});
}

View File

@@ -57,8 +57,8 @@ namespace OpenRA.Mods.Common.Traits.Sound
Game.Sound.PlayNotification(self.World.Map.Rules, discoverer, "Speech", Info.Notification, discoverer.Faction.InternalName);
// Radar notification
if (Info.PingRadar && radarPings.Value != null)
radarPings.Value.Add(() => true, self.CenterPosition, Color.Red, 50);
if (Info.PingRadar)
radarPings.Value?.Add(() => true, self.CenterPosition, Color.Red, 50);
}
}
}

View File

@@ -83,13 +83,9 @@ namespace OpenRA.Mods.Common.Traits
Game.Sound.Play(SoundType.World, info.OnFireSound, order.Target.CenterPosition);
foreach (var a in UnitsInRange(self.World.Map.CellContaining(order.Target.CenterPosition)))
{
var external = a.TraitsImplementing<ExternalCondition>()
.FirstOrDefault(t => t.Info.Condition == info.Condition && t.CanGrantCondition(a, self));
if (external != null)
external.GrantCondition(a, self, info.Duration);
}
a.TraitsImplementing<ExternalCondition>()
.FirstOrDefault(t => t.Info.Condition == info.Condition && t.CanGrantCondition(a, self))
?.GrantCondition(a, self, info.Duration);
}
public IEnumerable<Actor> UnitsInRange(CPos xy)

View File

@@ -227,10 +227,8 @@ namespace OpenRA.Mods.Common.Traits
return;
var power = Instances.FirstOrDefault(i => !i.IsTraitPaused);
if (power == null)
return;
power.SelectTarget(power.Self, Key, Manager);
power?.SelectTarget(power.Self, Key, Manager);
}
public virtual void Activate(Order order)

View File

@@ -381,8 +381,7 @@ namespace OpenRA.Mods.Common.Traits
foreach (var t in triggers)
t.Dirty = true;
if (CellUpdated != null)
CellUpdated(c.Cell);
CellUpdated?.Invoke(c.Cell);
}
}
@@ -403,8 +402,7 @@ namespace OpenRA.Mods.Common.Traits
foreach (var t in triggers)
t.Dirty = true;
if (CellUpdated != null)
CellUpdated(c.Cell);
CellUpdated?.Invoke(c.Cell);
}
}

View File

@@ -49,8 +49,7 @@ namespace OpenRA.Mods.Common.Traits
ClearRedo();
undoStack.Push(actionContainer);
if (ItemAdded != null)
ItemAdded(actionContainer);
ItemAdded?.Invoke(actionContainer);
}
public void Undo()
@@ -66,8 +65,7 @@ namespace OpenRA.Mods.Common.Traits
editorAction.Status = EditorActionStatus.Future;
redoStack.Push(editorAction);
if (OnChange != null)
OnChange();
OnChange?.Invoke();
}
void ClearRedo()
@@ -76,8 +74,7 @@ namespace OpenRA.Mods.Common.Traits
{
var item = redoStack.Pop();
if (ItemRemoved != null)
ItemRemoved(item);
ItemRemoved?.Invoke(item);
}
}
@@ -95,8 +92,7 @@ namespace OpenRA.Mods.Common.Traits
undoStack.Peek().Status = EditorActionStatus.History;
undoStack.Push(editorAction);
if (OnChange != null)
OnChange();
OnChange?.Invoke();
}
public bool HasUndos()

View File

@@ -92,8 +92,7 @@ namespace OpenRA.Mods.Common.Traits
UpdateNetWorth(t.Type, t.Density, newTile.Type, newTile.Density);
Tiles[uv] = newTile;
Map.CustomTerrain[uv] = newTerrain;
if (CellChanged != null)
CellChanged(cell, type);
CellChanged?.Invoke(cell, type);
// Neighbouring cell density depends on this cell
foreach (var d in CVec.Directions)
@@ -111,8 +110,7 @@ namespace OpenRA.Mods.Common.Traits
neighbouringTile.Density = density;
Tiles[neighbouringCell] = neighbouringTile;
if (CellChanged != null)
CellChanged(neighbouringCell, type);
CellChanged?.Invoke(neighbouringCell, type);
}
}

View File

@@ -158,8 +158,7 @@ namespace OpenRA.Mods.Common.Traits
cell.Density = Math.Min(cell.Type.Info.MaxDensity, cell.Density + n);
Content[p] = cell;
if (CellChanged != null)
CellChanged(p, cell.Type);
CellChanged?.Invoke(p, cell.Type);
}
public bool IsFull(CPos cell)
@@ -183,8 +182,7 @@ namespace OpenRA.Mods.Common.Traits
else
Content[cell] = c;
if (CellChanged != null)
CellChanged(cell, c.Type);
CellChanged?.Invoke(cell, c.Type);
return c.Type;
}
@@ -202,8 +200,7 @@ namespace OpenRA.Mods.Common.Traits
Content[cell] = ResourceLayerContents.Empty;
world.Map.CustomTerrain[cell] = byte.MaxValue;
if (CellChanged != null)
CellChanged(cell, c.Type);
CellChanged?.Invoke(cell, c.Type);
}
public ResourceType GetResourceType(CPos cell) { return Content[cell].Type; }