Skip to content

Commit 0c487f3

Browse files
committed
modernise code
1 parent b71a6f8 commit 0c487f3

File tree

9 files changed

+52
-67
lines changed

9 files changed

+52
-67
lines changed

Arrays.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,12 @@ namespace Datamodel
1010
[DebuggerDisplay("Count = {Inner.Count}")]
1111
public abstract class Array<T> : IList<T>, IList
1212
{
13-
internal class DebugView
13+
internal class DebugView(Array<T> arr)
1414
{
15-
public DebugView(Array<T> arr)
16-
{
17-
Arr = arr;
18-
}
19-
Array<T> Arr;
15+
readonly Array<T> Arr = arr;
2016

2117
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
22-
public T[] Items { get { return Arr.Inner.ToArray(); } }
18+
public T[] Items { get { return [.. Arr.Inner]; } }
2319
}
2420

2521
protected List<T> Inner;
@@ -38,15 +34,15 @@ internal set
3834

3935
internal Array()
4036
{
41-
Inner = new List<T>();
37+
Inner = [];
4238
}
4339

4440
internal Array(IEnumerable<T> enumerable)
4541
{
4642
if (enumerable != null)
47-
Inner = new List<T>(enumerable);
43+
Inner = [.. enumerable];
4844
else
49-
Inner = new List<T>();
45+
Inner = [];
5046
}
5147

5248
internal Array(int capacity)

AttributeList.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public AttributeList(Datamodel? owner)
100100
}
101101
}
102102

103-
Inner = new OrderedDictionary();
103+
Inner = [];
104104
Owner = owner;
105105
}
106106

Codecs/Binary.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace Datamodel.Codecs
1818
[CodecFormat("binary", 9)]
1919
class Binary : ICodec
2020
{
21-
static readonly Dictionary<int, Type?[]> SupportedAttributes = new();
21+
static readonly Dictionary<int, Type?[]> SupportedAttributes = [];
2222

2323
/// <summary>
2424
/// The number of Datamodel binary ticks in one second. Used to store TimeSpan values.
@@ -28,21 +28,21 @@ class Binary : ICodec
2828
static Binary()
2929
{
3030
SupportedAttributes[1] =
31-
SupportedAttributes[2] = new Type?[] {
31+
SupportedAttributes[2] = [
3232
typeof(Element), typeof(int), typeof(float), typeof(bool), typeof(string), typeof(byte[]),
3333
null /* ObjectID */, typeof(Color), typeof(Vector2), typeof(Vector3), typeof(Vector4), typeof(Vector3) /* angle*/, typeof(Quaternion), typeof(Matrix4x4)
34-
};
34+
];
3535
SupportedAttributes[3] =
3636
SupportedAttributes[4] =
37-
SupportedAttributes[5] = new Type[] {
37+
SupportedAttributes[5] = [
3838
typeof(Element), typeof(int), typeof(float), typeof(bool), typeof(string), typeof(byte[]),
3939
typeof(TimeSpan), typeof(Color), typeof(Vector2), typeof(Vector3), typeof(Vector4), typeof(Vector3) /* angle*/, typeof(Quaternion), typeof(Matrix4x4)
40-
};
41-
SupportedAttributes[9] = new Type[] {
40+
];
41+
SupportedAttributes[9] = [
4242
typeof(Element), typeof(int), typeof(float), typeof(bool), typeof(string), typeof(byte[]),
4343
typeof(TimeSpan), typeof(Color), typeof(Vector2), typeof(Vector3), typeof(Vector4), typeof(QAngle), typeof(Quaternion), typeof(Matrix4x4),
4444
typeof(ulong), typeof(byte)
45-
};
45+
];
4646
}
4747

4848
static byte TypeToId(Type type, int version)
@@ -105,7 +105,7 @@ static byte TypeToId(Type type, int version)
105105

106106
protected string ReadString_Raw(BinaryReader reader)
107107
{
108-
List<byte> raw = new();
108+
List<byte> raw = [];
109109
while (true)
110110
{
111111
byte cur = reader.ReadByte();
@@ -124,7 +124,7 @@ class StringDictionary
124124
readonly Binary? Codec;
125125
readonly int EncodingVersion;
126126

127-
readonly List<string> Strings = new();
127+
readonly List<string> Strings = [];
128128
public bool Dummy;
129129

130130
// binary 4 uses int for dictionary length, but short for dictionary indices. Whoops!
@@ -156,14 +156,14 @@ public StringDictionary(int encoding_version, BinaryWriter writer, Datamodel dm)
156156
Dummy = EncodingVersion == 1;
157157
if (!Dummy)
158158
{
159-
Scraped = new HashSet<Element>();
159+
Scraped = [];
160160

161161
ScrapeElement(dm.Root);
162162
Strings = Strings.Distinct().ToList();
163163
}
164164
}
165165

166-
private readonly HashSet<Element> Scraped = new();
166+
private readonly HashSet<Element> Scraped = [];
167167

168168
void ScrapeElement(Element? elem)
169169
{
@@ -389,7 +389,7 @@ public Datamodel Decode(string encoding, int encoding_version, string format, in
389389
ConstructorInfo? constructor = typeof(Element).GetConstructor(
390390
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
391391
null,
392-
new Type[] { typeof(Datamodel), typeof(string), typeof(Guid), typeof(string) },
392+
[typeof(Datamodel), typeof(string), typeof(Guid), typeof(string)],
393393
null
394394
);
395395

@@ -399,7 +399,7 @@ public Datamodel Decode(string encoding, int encoding_version, string format, in
399399
}
400400

401401
object uninitializedObject = RuntimeHelpers.GetUninitializedObject(derivedType);
402-
constructor.Invoke(uninitializedObject, new object[] { dm, name, id, type });
402+
constructor.Invoke(uninitializedObject, [dm, name, id, type]);
403403

404404
elem = (Element?)uninitializedObject;
405405
}
@@ -548,8 +548,8 @@ public Encoder(BinaryWriter writer, Datamodel dm, int version)
548548
Datamodel = dm;
549549

550550
StringDict = new StringDictionary(version, writer, dm);
551-
ElementIndices = new Dictionary<Element, int>();
552-
ElementOrder = new List<Element>();
551+
ElementIndices = [];
552+
ElementOrder = [];
553553

554554
}
555555

Codecs/KeyValues2.cs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ namespace Datamodel.Codecs
2222
[CodecFormat("keyvalues2_noids", 4)]
2323
class KeyValues2 : ICodec
2424
{
25-
static readonly Dictionary<Type, string> TypeNames = new();
26-
static readonly Dictionary<int, Type[]> ValidAttributes = new();
25+
static readonly Dictionary<Type, string> TypeNames = [];
26+
static readonly Dictionary<int, Type[]> ValidAttributes = [];
2727
static KeyValues2()
2828
{
2929
TypeNames[typeof(Element)] = "element";
@@ -131,7 +131,7 @@ public void Flush()
131131

132132
// Multi-referenced elements are written out as a separate block at the end of the file.
133133
// In-line only the id is written.
134-
Dictionary<Element, int> ReferenceCount = new();
134+
Dictionary<Element, int> ReferenceCount = [];
135135

136136
bool SupportsReferenceIds;
137137

@@ -294,9 +294,8 @@ void WriteAttribute(string name, int encodingVersion, Type type, object value, b
294294

295295
value = FormattableString.Invariant(FormattableStringFactory.Create(matrixString));
296296
}
297-
else if (value is QAngle qangle_value)
297+
else if (value is QAngle castValue)
298298
{
299-
var castValue = (QAngle)value;
300299
value = FormattableString.Invariant($"{castValue.Pitch} {castValue.Yaw} {castValue.Roll}");
301300
}
302301

@@ -354,7 +353,7 @@ public void Encode(Datamodel dm, string encoding, int encodingVersion, Stream st
354353
writer.Write(String.Format(CodecUtilities.HeaderPattern, encoding, encodingVersion, dm.Format, dm.FormatVersion));
355354
writer.WriteLine();
356355

357-
ReferenceCount = new Dictionary<Element, int>();
356+
ReferenceCount = [];
358357

359358
if (encodingVersion >= 4 && dm.PrefixAttributes.Count > 0)
360359
{
@@ -404,8 +403,8 @@ private class IntermediateData
404403
// these store element refs while we process the elements, once were done
405404
// we can go trough these and actually create the attributes
406405
// and add the elements to lists
407-
public Dictionary<Element, List<(string, Guid)>> PropertiesToAdd = new();
408-
public Dictionary<IList, List<Guid>> ListRefs = new();
406+
public Dictionary<Element, List<(string, Guid)>> PropertiesToAdd = [];
407+
public Dictionary<IList, List<Guid>> ListRefs = [];
409408

410409
public void HandleElementProp(Element? element, string attrName, Guid id)
411410
{
@@ -418,7 +417,7 @@ public void HandleElementProp(Element? element, string attrName, Guid id)
418417

419418
if (attrList == null)
420419
{
421-
attrList = new List<(string, Guid)>();
420+
attrList = [];
422421
PropertiesToAdd.Add(element, attrList);
423422
}
424423

@@ -432,7 +431,7 @@ public void HandleListRefs(IList list, Guid id)
432431

433432
if (guidList == null)
434433
{
435-
guidList = new List<Guid>();
434+
guidList = [];
436435
ListRefs.Add(list, guidList);
437436
}
438437

@@ -522,7 +521,7 @@ string Decode_NextToken(StreamReader reader)
522521
ConstructorInfo? constructor = typeof(Element).GetConstructor(
523522
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
524523
null,
525-
new Type[] { typeof(Datamodel), typeof(string), typeof(Guid), typeof(string) },
524+
[typeof(Datamodel), typeof(string), typeof(Guid), typeof(string)],
526525
null
527526
);
528527

@@ -532,16 +531,13 @@ string Decode_NextToken(StreamReader reader)
532531
}
533532

534533
object uninitializedObject = RuntimeHelpers.GetUninitializedObject(derivedType);
535-
constructor.Invoke(uninitializedObject, new object[] { dataModel, elem_name, new Guid(elem_id), elem_class });
534+
constructor.Invoke(uninitializedObject, [dataModel, elem_name, new Guid(elem_id), elem_class]);
536535

537536
elem = (Element?)uninitializedObject;
538537
}
539538
}
540539

541-
if (elem == null)
542-
{
543-
elem = new Element(dataModel, elem_name, new Guid(elem_id), elem_class);
544-
}
540+
elem ??= new Element(dataModel, elem_name, new Guid(elem_id), elem_class);
545541
}
546542

547543
continue;
@@ -664,7 +660,7 @@ string Decode_NextToken(StreamReader reader)
664660
else if (type == typeof(TimeSpan))
665661
return TimeSpan.FromTicks((long)(double.Parse(value, CultureInfo.InvariantCulture) * TimeSpan.TicksPerSecond));
666662

667-
var num_list = value.Split((char[]?)null, StringSplitOptions.RemoveEmptyEntries);
663+
var num_list = value.Split(Array.Empty<char>(), StringSplitOptions.RemoveEmptyEntries);
668664

669665
if (type == typeof(Color))
670666
{

Datamodel.ElementList.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public DebugView(ElementList item)
3232
public Element[] Elements => Item.store.Values.Cast<Element>().ToArray();
3333
}
3434

35-
private readonly OrderedDictionary store = new();
35+
private readonly OrderedDictionary store = [];
3636
private readonly Datamodel Owner;
3737

3838
internal ElementList(Datamodel owner)

Datamodel.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public DebugView(Datamodel dm)
3636

3737
#region Attribute types
3838

39-
private static readonly Type[] attributeTypes = {
39+
private static readonly Type[] attributeTypes = [
4040
typeof(Element),
4141
typeof(int),
4242
typeof(float),
@@ -53,7 +53,7 @@ public DebugView(Datamodel dm)
5353
typeof(byte),
5454
typeof(ulong),
5555
typeof(QAngle),
56-
};
56+
];
5757

5858
public static Type[] AttributeTypes => attributeTypes;
5959

@@ -110,7 +110,7 @@ static Datamodel()
110110
}
111111

112112
#region Codecs
113-
public static readonly Dictionary<CodecRegistration, Type> Codecs = new();
113+
public static readonly Dictionary<CodecRegistration, Type> Codecs = [];
114114

115115
public static IEnumerable<CodecRegistration> CodecsRegistered => Codecs.Keys.OrderBy(reg => string.Join(null, reg.Item1, reg.Item2)).ToArray();
116116

@@ -606,7 +606,7 @@ public ImportJob(ImportRecursionMode import_mode, ImportOverwriteMode overwrite_
606606
{
607607
ImportMode = import_mode;
608608
OverwriteMode = overwrite_mode;
609-
ImportMap = new Dictionary<Element, Element>();
609+
ImportMap = [];
610610
}
611611
}
612612

Element.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,11 +403,11 @@ public static bool IsElementDerived(Type type)
403403
}
404404
else
405405
{
406-
return type == elementType ? true : false;
406+
return type == elementType;
407407
}
408408
}
409409

410-
return type.BaseType == elementType ? true : false;
410+
return type.BaseType == elementType;
411411
}
412412
}
413413

ICodec.cs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,14 @@ public interface ICodec
3838
/// Parameters for reflection based deserialisation
3939
/// By default it will look for types in the calling assembly (the one which made this class)
4040
/// </summary>
41-
public class ReflectionParams
41+
/// <param name="attemptReflection">If to use reflection or not.</param>
42+
/// <param name="additionalTypes">Additional types to consider when matching.</param>
43+
/// <param name="assembliesToSearch">Additional assemblies to look for types in.</param>
44+
public class ReflectionParams(bool attemptReflection = true, List<Type>? additionalTypes = null, List<Assembly>? assembliesToSearch = null)
4245
{
43-
public bool AttemptReflection;
44-
public List<Type> AdditionalTypes;
45-
public List<Assembly> AssembliesToSearch;
46-
47-
/// <param name="attemptReflection">If to use reflection or not.</param>
48-
/// <param name="additionalTypes">Additional types to consider when matching.</param>
49-
/// <param name="assembliesToSearch">Additional assemblies to look for types in.</param>
50-
public ReflectionParams(bool attemptReflection = true, List<Type>? additionalTypes = null, List<Assembly>? assembliesToSearch = null)
51-
{
52-
AttemptReflection = attemptReflection;
53-
AdditionalTypes = additionalTypes ??= new();
54-
AssembliesToSearch = assembliesToSearch ??= new();
55-
}
46+
public bool AttemptReflection = attemptReflection;
47+
public List<Type> AdditionalTypes = additionalTypes ??= [];
48+
public List<Assembly> AssembliesToSearch = assembliesToSearch ??= [];
5649
}
5750

5851

@@ -197,7 +190,7 @@ public static void AddDeferredAttribute(Element elem, string key, long offset)
197190

198191
public static Dictionary<string, Type> GetReflectionTypes(ReflectionParams reflectionParams)
199192
{
200-
Dictionary<string, Type> types = new();
193+
Dictionary<string, Type> types = [];
201194

202195
if (reflectionParams.AttemptReflection)
203196
{
@@ -242,7 +235,7 @@ public CodecFormatAttribute(string name, params int[] versions)
242235
public CodecFormatAttribute(string name, int version)
243236
{
244237
Name = name;
245-
Versions = new int[] { version };
238+
Versions = [version];
246239
}
247240

248241
public string Name { get; private set; }

Types/Color.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ public void ToBytes(Span<byte> bytes)
1313
}
1414

1515
public byte[] ToBytes()
16-
=> new byte[] { R, G, B, A };
16+
=> [R, G, B, A];
1717
}

0 commit comments

Comments
 (0)