Skip to content

Commit

Permalink
Use Null-Propagation Operator
Browse files Browse the repository at this point in the history
  • Loading branch information
teinarss authored and pchote committed Aug 19, 2020
1 parent 8d27d22 commit 9c4fd0e
Show file tree
Hide file tree
Showing 113 changed files with 219 additions and 464 deletions.
12 changes: 4 additions & 8 deletions OpenRA.Game/Activities/Activity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ protected virtual void OnActorDispose(Actor self) { }
/// </summary>
internal void OnActorDisposeOuter(Actor self)
{
if (ChildActivity != null)
ChildActivity.OnActorDisposeOuter(self);
ChildActivity?.OnActorDisposeOuter(self);

OnActorDispose(self);
}
Expand All @@ -199,8 +198,7 @@ public virtual void Cancel(Actor self, bool keepQueue = false)
if (!IsInterruptible)
return;

if (ChildActivity != null)
ChildActivity.Cancel(self);
ChildActivity?.Cancel(self);

// Directly mark activities that are queued and therefore didn't run yet as done
State = State == ActivityState.Queued ? ActivityState.Done : ActivityState.Canceling;
Expand Down Expand Up @@ -243,11 +241,9 @@ protected void PrintActivityTree(Actor self, Activity origin = null, int level =

Console.WriteLine(GetType().ToString().Split('.').Last());

if (ChildActivity != null)
ChildActivity.PrintActivityTree(self, origin, level + 1);
ChildActivity?.PrintActivityTree(self, origin, level + 1);

if (NextActivity != null)
NextActivity.PrintActivityTree(self, origin, level);
NextActivity?.PrintActivityTree(self, origin, level);
}
}

Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Activities/CallFunc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public CallFunc(Action a, bool interruptible)

public override bool Tick(Actor self)
{
if (a != null) a();
a?.Invoke();
return true;
}
}
Expand Down
9 changes: 3 additions & 6 deletions OpenRA.Game/Actor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,7 @@ public void QueueActivity(Activity nextActivity)

public void CancelActivity()
{
if (CurrentActivity != null)
CurrentActivity.Cancel(this);
CurrentActivity?.Cancel(this);
}

public override int GetHashCode()
Expand Down Expand Up @@ -382,8 +381,7 @@ public void Dispose()
{
// If CurrentActivity isn't null, run OnActorDisposeOuter in case some cleanups are needed.
// This should be done before the FrameEndTask to avoid dependency issues.
if (CurrentActivity != null)
CurrentActivity.OnActorDisposeOuter(this);
CurrentActivity?.OnActorDisposeOuter(this);

// Allow traits/activities to prevent a race condition when they depend on disposing the actor (e.g. Transforms)
WillDispose = true;
Expand All @@ -402,8 +400,7 @@ public void Dispose()
World.TraitDict.RemoveActor(this);
Disposed = true;

if (luaInterface != null)
luaInterface.Value.OnActorDestroyed();
luaInterface?.Value.OnActorDestroyed();
});
}

Expand Down
3 changes: 1 addition & 2 deletions OpenRA.Game/Download.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ void DisposeWebClient()
public void CancelAsync()
{
lock (syncObject)
if (wc != null)
wc.CancelAsync();
wc?.CancelAsync();
}
}
}
3 changes: 1 addition & 2 deletions OpenRA.Game/FieldLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,7 @@ public static object GetValue(string fieldName, Type fieldType, string value, Me

public static object GetValue(string fieldName, Type fieldType, MiniYaml yaml, MemberInfo field)
{
var value = yaml.Value;
if (value != null) value = value.Trim();
var value = yaml.Value?.Trim();

if (fieldType == typeof(int))
{
Expand Down
5 changes: 1 addition & 4 deletions OpenRA.Game/FileSystem/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,7 @@ Stream GetFromCache(string filename)
var package = fileIndex[filename]
.LastOrDefault(x => x.Contains(filename));

if (package != null)
return package.GetStream(filename);

return null;
return package?.GetStream(filename);
}

public Stream Open(string filename)
Expand Down
3 changes: 1 addition & 2 deletions OpenRA.Game/FileSystem/ZipFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ public bool Contains(string filename)

public void Dispose()
{
if (pkg != null)
pkg.Close();
pkg?.Close();
}

public IReadOnlyPackage OpenPackage(string filename, FileSystem context)
Expand Down
41 changes: 14 additions & 27 deletions OpenRA.Game/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ static string TimestampedFilename(bool includemilliseconds = false)

static void JoinInner(OrderManager om)
{
if (OrderManager != null) OrderManager.Dispose();
OrderManager?.Dispose();
OrderManager = om;
lastConnectionState = ConnectionState.PreConnecting;
ConnectionStateChanged(OrderManager);
Expand Down Expand Up @@ -154,8 +154,7 @@ internal static void SyncLobbyInfo()
internal static void StartGame(string mapUID, WorldType type)
{
// Dispose of the old world before creating a new one.
if (worldRenderer != null)
worldRenderer.Dispose();
worldRenderer?.Dispose();

Cursor.SetCursor(null);
BeforeGameStart();
Expand Down Expand Up @@ -330,11 +329,9 @@ static void Initialize(Arguments args)
Log.Write("graphics", "{0}", e);
Console.WriteLine("Renderer initialization failed. Check graphics.log for details.");

if (Renderer != null)
Renderer.Dispose();
Renderer?.Dispose();

if (Sound != null)
Sound.Dispose();
Sound?.Dispose();
}
}

Expand Down Expand Up @@ -388,13 +385,10 @@ public static void InitializeMod(string mod, Arguments args)

Ui.ResetAll();

if (worldRenderer != null)
worldRenderer.Dispose();
worldRenderer?.Dispose();
worldRenderer = null;
if (server != null)
server.Shutdown();
if (OrderManager != null)
OrderManager.Dispose();
server?.Shutdown();
OrderManager?.Dispose();

if (ModData != null)
{
Expand Down Expand Up @@ -430,8 +424,7 @@ public static void InitializeMod(string mod, Arguments args)
var grid = ModData.Manifest.Contains<MapGrid>() ? ModData.Manifest.Get<MapGrid>() : null;
Renderer.InitializeDepthBuffer(grid);

if (Cursor != null)
Cursor.Dispose();
Cursor?.Dispose();

Cursor = new CursorManager(ModData.CursorProvider);

Expand All @@ -446,8 +439,7 @@ public static void InitializeMod(string mod, Arguments args)

try
{
if (discoverNat != null)
discoverNat.Wait();
discoverNat?.Wait();
}
catch (Exception e)
{
Expand Down Expand Up @@ -610,8 +602,7 @@ static void InnerLogicTick(OrderManager orderManager)
Sync.RunUnsynced(Settings.Debug.SyncCheckUnsyncedCode, world, () => world.TickRender(worldRenderer));
}

if (benchmark != null)
benchmark.Tick(LocalTick);
benchmark?.Tick(LocalTick);
}
}

Expand Down Expand Up @@ -836,12 +827,10 @@ static RunStatus Run()
finally
{
// Ensure that the active replay is properly saved
if (OrderManager != null)
OrderManager.Dispose();
OrderManager?.Dispose();
}

if (worldRenderer != null)
worldRenderer.Dispose();
worldRenderer?.Dispose();
ModData.Dispose();
ChromeProvider.Deinitialize();

Expand Down Expand Up @@ -880,8 +869,7 @@ public static void Debug(string s, params object[] args)

public static void Disconnect()
{
if (OrderManager.World != null)
OrderManager.World.TraitDict.PrintReport();
OrderManager.World?.TraitDict.PrintReport();

OrderManager.Dispose();
CloseServer();
Expand All @@ -890,8 +878,7 @@ public static void Disconnect()

public static void CloseServer()
{
if (server != null)
server.Shutdown();
server?.Shutdown();
}

public static T CreateObject<T>(string name)
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Graphics/Animation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public void PlayThen(string sequenceName, Action after)
{
frame = CurrentSequence.Length - 1;
tickFunc = () => { };
if (after != null) after();
after?.Invoke();
}
};
}
Expand Down
3 changes: 1 addition & 2 deletions OpenRA.Game/Graphics/Sheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ public void ReleaseBuffer()

public void Dispose()
{
if (texture != null)
texture.Dispose();
texture?.Dispose();
}
}
}
7 changes: 3 additions & 4 deletions OpenRA.Game/Graphics/WorldRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ public void AddPalette(string name, ImmutablePalette pal, bool allowModifiers =
var oldHeight = palette.Height;
palette.AddPalette(name, pal, allowModifiers);

if (oldHeight != palette.Height && PaletteInvalidated != null)
PaletteInvalidated();
if (oldHeight != palette.Height)
PaletteInvalidated?.Invoke();
}
}

Expand Down Expand Up @@ -258,8 +258,7 @@ public void Draw()
if (enableDepthBuffer)
Game.Renderer.Context.EnableDepthBuffer();

if (terrainRenderer != null)
terrainRenderer.RenderTerrain(this, Viewport);
terrainRenderer?.RenderTerrain(this, Viewport);

Game.Renderer.Flush();

Expand Down
3 changes: 1 addition & 2 deletions OpenRA.Game/InstalledMods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ Manifest LoadMod(string id, string path)
Log.Write("debug", "Load mod '{0}': {1}".F(path, e));
}

if (package != null)
package.Dispose();
package?.Dispose();

return null;
}
Expand Down
3 changes: 1 addition & 2 deletions OpenRA.Game/LocalPlayerProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ public void RefreshPlayerData(Action onComplete = null)
}
finally
{
if (onComplete != null)
onComplete();
onComplete?.Invoke();
}
};

Expand Down
3 changes: 1 addition & 2 deletions OpenRA.Game/Manifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,7 @@ public void Dispose()
foreach (var module in modules)
{
var disposableModule = module as IDisposable;
if (disposableModule != null)
disposableModule.Dispose();
disposableModule?.Dispose();
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions OpenRA.Game/Map/CellLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ public T this[CPos cell]
{
entries[Index(cell)] = value;

if (CellEntryChanged != null)
CellEntryChanged(cell);
CellEntryChanged?.Invoke(cell);
}
}

Expand All @@ -90,8 +89,7 @@ public T this[MPos uv]
{
entries[Index(uv)] = value;

if (CellEntryChanged != null)
CellEntryChanged(uv.ToCPos(GridType));
CellEntryChanged?.Invoke(uv.ToCPos(GridType));
}
}

Expand Down
12 changes: 4 additions & 8 deletions OpenRA.Game/Map/MapCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ public void LoadMaps()
}
catch (Exception e)
{
if (mapPackage != null)
mapPackage.Dispose();
mapPackage?.Dispose();
Console.WriteLine("Failed to load map: {0}", map);
Console.WriteLine("Details: {0}", e);
Log.Write("debug", "Failed to load map: {0}", map);
Expand Down Expand Up @@ -192,8 +191,7 @@ public void QueryRemoteMapDetails(string repositoryUrl, IEnumerable<string> uids
foreach (var p in maps.Values)
p.UpdateRemoteSearch(MapStatus.Unavailable, null);

if (queryFailed != null)
queryFailed();
queryFailed?.Invoke();

return;
}
Expand All @@ -213,8 +211,7 @@ public void QueryRemoteMapDetails(string repositoryUrl, IEnumerable<string> uids
{
Log.Write("debug", "Can't parse remote map search data:\n{0}", data);
Log.Write("debug", "Exception: {0}", e);
if (queryFailed != null)
queryFailed();
queryFailed?.Invoke();
}
};

Expand Down Expand Up @@ -298,8 +295,7 @@ public void CacheMinimap(MapPreview preview)
Game.RunAfterTick(() =>
{
// Wait for any existing thread to exit before starting a new one.
if (previewLoaderThread != null)
previewLoaderThread.Join();
previewLoaderThread?.Join();

previewLoaderThread = new Thread(LoadAsyncInternal)
{
Expand Down
7 changes: 2 additions & 5 deletions OpenRA.Game/Map/MapPreview.cs
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,7 @@ public void UpdateRemoteSearch(MapStatus status, MiniYaml yaml, Action<MapPrevie
if (innerData.Preview != null)
cache.CacheMinimap(this);

if (parseMetadata != null)
parseMetadata(this);
parseMetadata?.Invoke(this);
}

// Update the status and class unconditionally
Expand Down Expand Up @@ -522,9 +521,7 @@ public void Dispose()
public void Delete()
{
Invalidate();
var deleteFromPackage = parentPackage as IReadWritePackage;
if (deleteFromPackage != null)
deleteFromPackage.Delete(Package.Name);
(parentPackage as IReadWritePackage)?.Delete(Package.Name);
}

Stream IReadOnlyFileSystem.Open(string filename)
Expand Down
Loading

0 comments on commit 9c4fd0e

Please sign in to comment.