Skip to content

Commit 7f5b928

Browse files
committed
C#: Apply a bunch of VS suggestions
1 parent ec82a91 commit 7f5b928

7 files changed

Lines changed: 63 additions & 59 deletions

File tree

code/C#/DBDefsLib/BDBDReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public static Dictionary<string, TableInfo> Read(Stream stream, string tableName
136136
var numCols = bin.ReadInt32();
137137
versionDefinition.definitions = new Definition[numCols];
138138

139-
for(int vc = 0; vc < numCols; vc++)
139+
for (int vc = 0; vc < numCols; vc++)
140140
{
141141
var flags = bin.ReadByte();
142142
var size = bin.ReadByte();

code/C#/DBDefsLib/DBDEnumReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public EnumDefinition Read(Stream stream, MetaType metaType)
7474
if (!long.TryParse(valueStr, out entry.value))
7575
throw new Exception($"Line {lineNumber}: Invalid enum value '{valueStr}'");
7676
}
77-
77+
7878
// This way the name can have spaces
7979
var remainingLine = line[(valueStr.Length)..].TrimStart();
8080
if (!string.IsNullOrEmpty(remainingLine))

code/C#/DBDefsLib/DBDEnumWriter.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ public void Save(EnumDefinition enumDefinition, string filePath)
4949
sb.Append($" {entry.name}");
5050

5151
if (!string.IsNullOrEmpty(entry.comment))
52-
{
5352
sb.Append($" // {entry.comment}");
54-
}
5553

5654
sb.AppendLine();
5755
}

code/C#/DBDefsLib/DBDMReader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public List<MappingDefinition> Read(Stream stream)
2828
continue;
2929

3030
var mappingDefinition = new MappingDefinition();
31-
var split = line.Split(' ', StringSplitOptions.RemoveEmptyEntries);
31+
var split = line.Split(' ', StringSplitOptions.RemoveEmptyEntries);
3232

3333
// Comment available in mapping line, we handle split differently here as well
3434
if (line.Contains("//"))
@@ -113,7 +113,7 @@ public List<MappingDefinition> Read(string file, bool validate = false)
113113
if (validate)
114114
{
115115
var metaDirectory = Path.GetDirectoryName(file);
116-
foreach(var mapping in mappingDefinition)
116+
foreach (var mapping in mappingDefinition)
117117
{
118118
if (mapping.meta == MetaType.COLOR)
119119
continue;

code/C#/DBDefsLib/DBDReader.cs

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public DBDefinition Read(Stream stream, bool validate = false)
2323
if (lines[0].StartsWith("COLUMNS"))
2424
{
2525
lineNumber++;
26-
while (lineNumber < lines.Count())
26+
while (lineNumber < lines.Count)
2727
{
2828
var line = lines[lineNumber++];
2929

@@ -38,13 +38,13 @@ public DBDefinition Read(Stream stream, bool validate = false)
3838
var validTypes = new List<string> { "uint", "int", "float", "string", "locstring" };
3939

4040
// Check if line has a space in case someone didn't assign a type to a column name
41-
if (!line.Contains(" "))
41+
if (!line.Contains(' '))
4242
{
4343
throw new Exception("Line " + line + " does not contain a space between type and column name!");
4444
}
4545

4646
// Read line up to space (end of type) or < (foreign key)
47-
var type = line.Substring(0, line.IndexOfAny(new char[] { ' ', '<' }));
47+
var type = line[..line.IndexOfAny([' ', '<'])];
4848

4949
// Check if type is valid, throw exception if not!
5050
if (!validTypes.Contains(type))
@@ -61,7 +61,7 @@ public DBDefinition Read(Stream stream, bool validate = false)
6161
if (line.StartsWith(type + "<"))
6262
{
6363
// Read foreign key info between < and > without < and > in result, then split on :: to separate table and field
64-
var foreignKey = line.Substring(line.IndexOf('<') + 1, line.IndexOf('>') - line.IndexOf('<') - 1).Split(new string[] { "::" }, StringSplitOptions.None);
64+
var foreignKey = line.Substring(line.IndexOf('<') + 1, line.IndexOf('>') - line.IndexOf('<') - 1).Split(["::"], StringSplitOptions.None);
6565

6666
// There should only be 2 values in foreignKey (table and col)
6767
if (foreignKey.Length != 2)
@@ -80,7 +80,7 @@ public DBDefinition Read(Stream stream, bool validate = false)
8080
// If there's only one space on the line at the same locaiton as the first one, assume a simple line like "uint ID", this can be better
8181
if (line.LastIndexOf(' ') == line.IndexOf(' '))
8282
{
83-
name = line.Substring(line.IndexOf(' ') + 1);
83+
name = line[(line.IndexOf(' ') + 1)..];
8484
}
8585
else
8686
{
@@ -94,10 +94,10 @@ public DBDefinition Read(Stream stream, bool validate = false)
9494
}
9595

9696
// If name ends in ? it's unverified
97-
if (name.EndsWith("?"))
97+
if (name.EndsWith('?'))
9898
{
9999
columnDefinition.verified = false;
100-
name = name.Remove(name.Length - 1);
100+
name = name[..^1];
101101
}
102102
else
103103
{
@@ -107,20 +107,16 @@ public DBDefinition Read(Stream stream, bool validate = false)
107107
/* COMMENT READING */
108108
if (line.Contains("//"))
109109
{
110-
columnDefinition.comment = line.Substring(line.IndexOf("//") + 2).Trim();
110+
columnDefinition.comment = line[(line.IndexOf("//") + 2)..].Trim();
111111
}
112112

113113
// Add to dictionary
114-
if (columnDefinitionDictionary.ContainsKey(name))
114+
if (!columnDefinitionDictionary.TryAdd(name, columnDefinition))
115115
{
116116
Console.ForegroundColor = ConsoleColor.Red;
117117
Console.WriteLine("Collision with existing column name while adding new column name! Skipping..");
118118
Console.ResetColor();
119119
}
120-
else
121-
{
122-
columnDefinitionDictionary.Add(name, columnDefinition);
123-
}
124120
}
125121
}
126122
else
@@ -144,41 +140,43 @@ public DBDefinition Read(Stream stream, bool validate = false)
144140

145141
if (string.IsNullOrWhiteSpace(line))
146142
{
147-
if (builds.Count != 0 || buildRanges.Count != 0 || layoutHashes.Count != 0) {
143+
if (builds.Count != 0 || buildRanges.Count != 0 || layoutHashes.Count != 0)
144+
{
148145
versionDefinitions.Add(
149146
new VersionDefinitions()
150147
{
151-
builds = builds.ToArray(),
152-
buildRanges = buildRanges.ToArray(),
153-
layoutHashes = layoutHashes.ToArray(),
148+
builds = [.. builds],
149+
buildRanges = [.. buildRanges],
150+
layoutHashes = [.. layoutHashes],
154151
comment = comment,
155-
definitions = definitions.ToArray()
152+
definitions = [.. definitions]
156153
}
157154
);
158155
}
159-
else if (definitions.Count != 0 || !string.IsNullOrWhiteSpace(comment)) {
156+
else if (definitions.Count != 0 || !string.IsNullOrWhiteSpace(comment))
157+
{
160158
throw new Exception("No BUILD or LAYOUT, but non-empty lines/'definitions'.");
161159
}
162160

163-
definitions = new List<Definition>();
164-
layoutHashes = new List<string>();
161+
definitions = [];
162+
layoutHashes = [];
165163
comment = "";
166-
builds = new List<Build>();
167-
buildRanges = new List<BuildRange>();
164+
builds = [];
165+
buildRanges = [];
168166
}
169167

170168
if (line.StartsWith("LAYOUT"))
171169
{
172-
var splitLayoutHashes = line.Remove(0, 7).Split(new string[] { ", " }, StringSplitOptions.None);
170+
var splitLayoutHashes = line[7..].Split([", "], StringSplitOptions.None);
173171
layoutHashes.AddRange(splitLayoutHashes);
174172
}
175173

176174
if (line.StartsWith("BUILD"))
177175
{
178-
var splitBuilds = line.Remove(0, 6).Split(new string[] { ", " }, StringSplitOptions.None);
176+
var splitBuilds = line[6..].Split([", "], StringSplitOptions.None);
179177
foreach (var splitBuild in splitBuilds)
180178
{
181-
if (splitBuild.Contains("-"))
179+
if (splitBuild.Contains('-'))
182180
{
183181
var splitRange = splitBuild.Split('-');
184182
buildRanges.Add(
@@ -195,20 +193,21 @@ public DBDefinition Read(Stream stream, bool validate = false)
195193

196194
if (line.StartsWith("COMMENT"))
197195
{
198-
comment = line.Substring(7).Trim();
196+
comment = line[7..].Trim();
199197
}
200198

201199
if (!line.StartsWith("LAYOUT") && !line.StartsWith("BUILD") && !line.StartsWith("COMMENT") && !string.IsNullOrWhiteSpace(line))
202200
{
203-
var definition = new Definition();
204-
205-
// Default to everything being inline
206-
definition.isNonInline = false;
201+
var definition = new Definition
202+
{
203+
// Default to everything being inline
204+
isNonInline = false
205+
};
207206

208-
if (line.Contains("$"))
207+
if (line.Contains('$'))
209208
{
210-
var annotationStart = line.IndexOf("$");
211-
var annotationEnd = line.IndexOf("$", 1);
209+
var annotationStart = line.IndexOf('$');
210+
var annotationEnd = line.IndexOf('$', 1);
212211

213212
var annotations = new List<string>(line.Substring(annotationStart + 1, annotationEnd - annotationStart - 1).Split(','));
214213

@@ -230,7 +229,7 @@ public DBDefinition Read(Stream stream, bool validate = false)
230229
line = line.Remove(annotationStart, annotationEnd + 1);
231230
}
232231

233-
if (line.Contains("<"))
232+
if (line.Contains('<'))
234233
{
235234
var size = line.Substring(line.IndexOf('<') + 1, line.IndexOf('>') - line.IndexOf('<') - 1);
236235

@@ -248,16 +247,20 @@ public DBDefinition Read(Stream stream, bool validate = false)
248247
line = line.Remove(line.IndexOf('<'), line.IndexOf('>') - line.IndexOf('<') + 1);
249248
}
250249

251-
if (line.Contains("["))
250+
if (line.Contains('['))
252251
{
253-
int.TryParse(line.Substring(line.IndexOf('[') + 1, line.IndexOf(']') - line.IndexOf('[') - 1), out definition.arrLength);
252+
if (!int.TryParse(line.AsSpan(line.IndexOf('[') + 1, line.IndexOf(']') - line.IndexOf('[') - 1), out definition.arrLength))
253+
{
254+
throw new Exception("Invalid array length format.");
255+
}
256+
254257
line = line.Remove(line.IndexOf('['), line.IndexOf(']') - line.IndexOf('[') + 1);
255258
}
256259

257260
if (line.Contains("//"))
258261
{
259-
definition.comment = line.Substring(line.IndexOf("//") + 2).Trim();
260-
line = line.Remove(line.IndexOf("//")).Trim();
262+
definition.comment = line[(line.IndexOf("//") + 2)..].Trim();
263+
line = line[..line.IndexOf("//")].Trim();
261264
}
262265

263266
definition.name = line;
@@ -281,19 +284,21 @@ public DBDefinition Read(Stream stream, bool validate = false)
281284

282285
if (lines.Count == (i + 1))
283286
{
284-
if (builds.Count != 0 || buildRanges.Count != 0 || layoutHashes.Count != 0) {
287+
if (builds.Count != 0 || buildRanges.Count != 0 || layoutHashes.Count != 0)
288+
{
285289
versionDefinitions.Add(
286290
new VersionDefinitions()
287291
{
288-
builds = builds.ToArray(),
289-
buildRanges = buildRanges.ToArray(),
290-
layoutHashes = layoutHashes.ToArray(),
292+
builds = [.. builds],
293+
buildRanges = [.. buildRanges],
294+
layoutHashes = [.. layoutHashes],
291295
comment = comment,
292-
definitions = definitions.ToArray()
296+
definitions = [.. definitions]
293297
}
294298
);
295299
}
296-
else if (definitions.Count != 0 || !string.IsNullOrWhiteSpace(comment)) {
300+
else if (definitions.Count != 0 || !string.IsNullOrWhiteSpace(comment))
301+
{
297302
throw new Exception("No BUILD or LAYOUT, but non-empty lines/'definitions'.");
298303
}
299304
}
@@ -438,7 +443,7 @@ public DBDefinition Read(Stream stream, bool validate = false)
438443
return new DBDefinition
439444
{
440445
columnDefinitions = columnDefinitionDictionary,
441-
versionDefinitions = versionDefinitions.ToArray()
446+
versionDefinitions = [.. versionDefinitions]
442447
};
443448
}
444449

code/C#/DBDefsLib/DBDWriter.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,13 @@ public void Save(DBDefinition definition, string target, bool sort = false)
8686
for (var b = 0; b < versionDefinition.builds.Length; b++)
8787
{
8888
var major = versionDefinition.builds[b].expansion + "." + versionDefinition.builds[b].major + "." + versionDefinition.builds[b].minor;
89-
if (!buildsByMajor.ContainsKey(major))
89+
if (!buildsByMajor.TryGetValue(major, out List<Build> builds))
9090
{
91-
buildsByMajor.Add(major, new List<Build>());
91+
builds = [];
92+
buildsByMajor.Add(major, builds);
9293
}
9394

94-
buildsByMajor[major].Add(versionDefinition.builds[b]);
95+
builds.Add(versionDefinition.builds[b]);
9596
}
9697

9798
foreach (var buildList in buildsByMajor)

code/C#/DBDefsLib/Utils.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static string NormalizeColumn(string col, bool fixFirst = true)
7777

7878
if (cleaned.StartsWith(thingToUpperCase, StringComparison.CurrentCultureIgnoreCase))
7979
{
80-
cleaned = thingToUpperCase + cleaned.Substring(thingToUpperCase.Length);
80+
cleaned = thingToUpperCase + cleaned[thingToUpperCase.Length..];
8181
}
8282

8383
if (cleaned.EndsWith(thingToUpperCase, StringComparison.CurrentCultureIgnoreCase))
@@ -86,9 +86,9 @@ public static string NormalizeColumn(string col, bool fixFirst = true)
8686
}
8787
}
8888

89-
if (cleaned.EndsWith("_"))
89+
if (cleaned.EndsWith('_'))
9090
{
91-
cleaned = cleaned.Substring(0, cleaned.Length - 1);
91+
cleaned = cleaned[..^1];
9292
}
9393

9494
if (fixFirst)
@@ -122,7 +122,7 @@ public static (List<Build> builds, List<BuildRange> buildRanges) ParseBuildQuali
122122
var buildRanges = new List<BuildRange>();
123123

124124
var buildsPart = qualifier["BUILD ".Length..];
125-
var parts = buildsPart.Split(new[] { ", " }, StringSplitOptions.RemoveEmptyEntries);
125+
var parts = buildsPart.Split([", "], StringSplitOptions.RemoveEmptyEntries);
126126

127127
foreach (var part in parts)
128128
{

0 commit comments

Comments
 (0)