Skip to content

Commit

Permalink
Fix warnings in Content.Tools module (#17862)
Browse files Browse the repository at this point in the history
  • Loading branch information
TemporalOroboros authored Jul 10, 2023
1 parent 4cc771f commit 7ec8bea
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Content.Tools/MappingMergeDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static void Main(string[] args)
var based = new Map(args[1]); // On what?
var other = new Map(args[2]);

if ((ours.GridsNode.Children.Count != 1) || (based.GridsNode.Children.Count != 1) || (other.GridsNode.Children.Count != 1))
if (ours.GridsNode.Children.Count != 1 || based.GridsNode.Children.Count != 1 || other.GridsNode.Children.Count != 1)
{
Console.WriteLine("one or more files had an amount of grids not equal to 1");
Environment.Exit(1);
Expand Down
28 changes: 16 additions & 12 deletions Content.Tools/Merger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void MergeTileChunks(YamlSequenceNode aChunks, YamlSequenceNode bChunks,
var cI = MapTileId(cR.ReadUInt32(), TileMapFromOtherToOurs);
// cI needs translation.

uint result = aI;
var result = aI;
if (aI == bI)
{
// If aI == bI then aI did not change anything, so cI always wins
Expand All @@ -138,20 +138,20 @@ public void MergeTileChunks(YamlSequenceNode aChunks, YamlSequenceNode bChunks,
}
}

public uint MapTileId(uint src, Dictionary<uint, uint> mapping)
public static uint MapTileId(uint src, Dictionary<uint, uint> mapping)
{
return (src & 0xFFFF0000) | mapping[src & 0xFFFF];
}

public Dictionary<string, YamlMappingNode> ConvertTileChunks(YamlSequenceNode chunks)
public static Dictionary<string, YamlMappingNode> ConvertTileChunks(YamlSequenceNode chunks)
{
var map = new Dictionary<string, YamlMappingNode>();
foreach (var chunk in chunks)
map[chunk["ind"].ToString()] = (YamlMappingNode) chunk;
return map;
}

public byte[] GetChunkBytes(Dictionary<string, YamlMappingNode> chunks, string ind)
public static byte[] GetChunkBytes(Dictionary<string, YamlMappingNode> chunks, string ind)
{
if (!chunks.ContainsKey(ind))
return new byte[ExpectedChunkSize];
Expand Down Expand Up @@ -197,7 +197,7 @@ public void PlanEntityMapping()

public bool MergeEntities()
{
bool success = true;
var success = true;
foreach (var kvp in EntityMapFromOtherToOurs)
{
// For debug use.
Expand All @@ -220,10 +220,13 @@ public bool MergeEntities()
else
{
oursEnt = (YamlMappingNode) YamlTools.CopyYamlNodes(MapOther.Entities[kvp.Key]);
if (!MapEntity(oursEnt)) {
if (!MapEntity(oursEnt))
{
Console.WriteLine("Unable to successfully import entity C/" + kvp.Key);
success = false;
} else {
}
else
{
MapOurs.Entities[kvp.Value] = oursEnt;
}
}
Expand All @@ -240,7 +243,7 @@ public bool MergeEntityNodes(YamlMappingNode ours, YamlMappingNode based, YamlMa
return false;
// Merge stuff that isn't components
var path = "Entity" + other["uid"];
YamlTools.MergeYamlMappings(ours, based, otherMapped, path, new[] {"components"});
YamlTools.MergeYamlMappings(ours, based, otherMapped, path, new[] { "components" });
// Components are special
var ourComponents = new Dictionary<string, YamlMappingNode>();
var basedComponents = new Dictionary<string, YamlMappingNode>();
Expand Down Expand Up @@ -301,10 +304,11 @@ public bool MapEntity(YamlMappingNode other)

public bool MapEntityProperty(YamlMappingNode node, string property, string path)
{
if (node.Children.ContainsKey(property)) {
if (node.Children.ContainsKey(property))
{
var prop = node[property];
if (prop is YamlScalarNode)
return MapEntityProperty((YamlScalarNode) prop, path + "/" + property);
if (prop is YamlScalarNode yamlProp)
return MapEntityProperty(yamlProp, path + "/" + property);
}
return true;
}
Expand Down Expand Up @@ -333,7 +337,7 @@ public bool MapEntityRecursiveAndBadly(YamlNode node, string path)
case YamlSequenceNode subSequence:
var idx = 0;
foreach (var val in subSequence)
if (!MapEntityRecursiveAndBadly(val, path + "/" + (idx++)))
if (!MapEntityRecursiveAndBadly(val, path + "/" + idx++))
return false;
return true;
case YamlMappingNode subMapping:
Expand Down
2 changes: 1 addition & 1 deletion Content.Tools/TypeTagPreserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public sealed class TypeTagPreserver : IEmitter
{
public TypeTagPreserver(IEmitter emitter)
{
Emitter = emitter;
Emitter = emitter;
}

private IEmitter Emitter { get; }
Expand Down
31 changes: 14 additions & 17 deletions Content.Tools/YamlTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ public static YamlNode CopyYamlNodes(YamlNode other)
switch (other)
{
case YamlSequenceNode subSequence:
YamlSequenceNode tmp1 = new YamlSequenceNode();
var tmp1 = new YamlSequenceNode();
MergeYamlSequences(tmp1, new YamlSequenceNode(), subSequence, "");
return tmp1;
case YamlMappingNode subMapping:
YamlMappingNode tmp2 = new YamlMappingNode();
MergeYamlMappings(tmp2, new YamlMappingNode(), subMapping, "", new string[] {});
var tmp2 = new YamlMappingNode();
MergeYamlMappings(tmp2, new YamlMappingNode(), subMapping, "", Array.Empty<string>());
return tmp2;
case YamlScalarNode subScalar:
YamlScalarNode tmp3 = new YamlScalarNode();
var tmp3 = new YamlScalarNode();
CopyYamlScalar(tmp3, subScalar);
return tmp3;
default:
Expand Down Expand Up @@ -47,7 +47,7 @@ public static void MergeYamlNodes(YamlNode ours, YamlNode based, YamlNode other,
MergeYamlSequences((YamlSequenceNode) ours, (YamlSequenceNode) based, subSequence, path);
break;
case YamlMappingNode subMapping:
MergeYamlMappings((YamlMappingNode) ours, (YamlMappingNode) based, subMapping, path, new string[] {});
MergeYamlMappings((YamlMappingNode) ours, (YamlMappingNode) based, subMapping, path, Array.Empty<string>());
break;
case YamlScalarNode subScalar:
// Console.WriteLine(path + " - " + ours + " || " + based + " || " + other);
Expand All @@ -67,7 +67,7 @@ public static void MergeYamlNodes(YamlNode ours, YamlNode based, YamlNode other,

public static void MergeYamlSequences(YamlSequenceNode ours, YamlSequenceNode based, YamlSequenceNode other, string path)
{
if ((ours.Children.Count == based.Children.Count) && (other.Children.Count == ours.Children.Count))
if (ours.Children.Count == based.Children.Count && other.Children.Count == ours.Children.Count)
{
// this is terrible and doesn't do proper rearrange detection
// but it looks as if vectors might be arrays
Expand Down Expand Up @@ -95,7 +95,7 @@ public static void MergeYamlMappings(YamlMappingNode ours, YamlMappingNode based
var localPath = path + "/" + kvp.Key;
var deletedByOurs = !ours.Children.ContainsKey(kvp.Key);
var deletedByOther = !other.Children.ContainsKey(kvp.Key);
if (deletedByOther && (!deletedByOurs))
if (deletedByOther && !deletedByOurs)
{
// Delete
ours.Children.Remove(kvp.Key);
Expand Down Expand Up @@ -157,17 +157,14 @@ public static float YamlNodesHeuristic(YamlNode a, YamlNode b)
{
if (a.GetType() != b.GetType())
return 0.0f;
switch (a)

return a switch
{
case YamlSequenceNode x:
return YamlSequencesHeuristic(x, (YamlSequenceNode) b);
case YamlMappingNode y:
return YamlMappingsHeuristic(y, (YamlMappingNode) b);
case YamlScalarNode z:
return (z.Value == ((YamlScalarNode) b).Value) ? 1.0f : 0.0f;
default:
throw new ArgumentException($"Unrecognized YAML node type: {a.GetType()}", nameof(a));
}
YamlSequenceNode x => YamlSequencesHeuristic(x, (YamlSequenceNode) b),
YamlMappingNode y => YamlMappingsHeuristic(y, (YamlMappingNode) b),
YamlScalarNode z => (z.Value == ((YamlScalarNode) b).Value) ? 1.0f : 0.0f,
_ => throw new ArgumentException($"Unrecognized YAML node type: {a.GetType()}", nameof(a))
};
}

public static float YamlSequencesHeuristic(YamlSequenceNode a, YamlSequenceNode b)
Expand Down

0 comments on commit 7ec8bea

Please sign in to comment.