Skip to content

Commit

Permalink
Refactor - Use GetValueOrDefault
Browse files Browse the repository at this point in the history
  • Loading branch information
csinkers committed Nov 14, 2024
1 parent b03479a commit 4430003
Show file tree
Hide file tree
Showing 8 changed files with 8 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Config/PathResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public string ResolvePathAbsolute(string relative)
: path;
}

public string GetPath(string pathName) => Paths.TryGetValue(pathName, out var result) ? result : null;
public string GetPath(string pathName) => Paths.GetValueOrDefault(pathName);

public string ResolvePath(string relative)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Textures/CompositedTexture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public int GetSubImageAtTime(int logicalId, int tick, bool backAndForth)
}
else frame = tick % logicalImage.Frames;

return _layerLookup.TryGetValue(new LayerKey(logicalId, frame), out var result) ? result : 0;
return _layerLookup.GetValueOrDefault(new LayerKey(logicalId, frame), 0);
}

public void AddTexture(
Expand Down
2 changes: 1 addition & 1 deletion src/Editor/EditorAssetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,5 @@ public int GetIdForAsset(object asset)
return id;
}

public EditorAsset GetAssetForId(int id) => _assetsById.TryGetValue(id, out var asset) ? asset : null;
public EditorAsset GetAssetForId(int id) => _assetsById.GetValueOrDefault(id);
}
2 changes: 1 addition & 1 deletion src/Formats/Assets/WaveLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public ISample this[int instrument]
.ToLookup(x => x.Instrument)
.ToDictionary(x => x.Key, x => x.First());

return _instrumentIndex.TryGetValue(instrument, out var sample) ? sample : null;
return _instrumentIndex.GetValueOrDefault(instrument);
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/Formats/Containers/SpellListContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ public void Write(string path, IList<(AssetLoadContext, byte[])> assets, ModCont

for(int i = 0; i <= maxId; i++)
{
if (!dict.TryGetValue(i, out var bytes))
bytes = Blank;
var bytes = dict.GetValueOrDefault(i, Blank);

ApiUtil.Assert(bytes.Length == SpellData.SizeOnDisk,
$"Expected spell data for entry {i} to be {SpellData.SizeOnDisk} bytes, but was {bytes.Length}");
Expand Down
4 changes: 1 addition & 3 deletions src/Formats/Parsers/ItemNameMetaLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ public ListStringSet Serdes(ListStringSet existing, ISerializer s, AssetLoadCont
if (target is not Dictionary<string, ListStringSet> dict)
throw new FormatException($"Expected target \"{targetId}\" to be a {nameof(Dictionary<string, ListStringSet>)} but it was {target?.GetType()}");

return dict.TryGetValue(context.Language, out var list)
? list
: null;
return dict.GetValueOrDefault(context.Language);
}

public object Serdes(object existing, ISerializer s, AssetLoadContext context)
Expand Down
2 changes: 1 addition & 1 deletion src/Scripting/ControlFlowGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ from end in kvp.Value
public ImmutableArray<int> Parents(int i) => _edgesByEnd.TryGetValue(i, out var nodes) ? nodes : ImmutableArray<int>.Empty;
IList<int> IGraph.Children(int i) => Children(i);
IList<int> IGraph.Parents(int i) => Parents(i);
public CfgEdge GetEdgeLabel(int startNode, int endNode) => _labels.TryGetValue((startNode, endNode), out var label) ? label : CfgEdge.True;
public CfgEdge GetEdgeLabel(int startNode, int endNode) => CollectionExtensions.GetValueOrDefault(_labels, (startNode, endNode), CfgEdge.True);
public DominatorTree GetDominatorTree() => this.GetDominatorTree(EntryIndex);
public DominatorTree GetPostDominatorTree() => Reverse().GetDominatorTree();
public bool IsCyclic() => GetBackEdges().Any();
Expand Down
2 changes: 1 addition & 1 deletion src/Tests/UAlbion.TestCommon/MockSpellManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public SpellId GetSpellId(SpellClass school, byte number)
=> _lookup.TryGetValue((school, number), out var id) ? id : SpellId.None;

public SpellData GetSpellOrDefault(SpellId id)
=> _spells.TryGetValue(id, out var spell) ? spell : null;
=> _spells.GetValueOrDefault(id);

public MockSpellManager Add(SpellData spell)
{
Expand Down

0 comments on commit 4430003

Please sign in to comment.