Skip to content

Commit 1f802cf

Browse files
committed
Enable nullable on the project
1 parent d9962e3 commit 1f802cf

28 files changed

+257
-272
lines changed

MultiAdmin.Tests/MultiAdmin.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<LangVersion>11</LangVersion>
55
<IsPackable>false</IsPackable>
66
<RootNamespace>MultiAdmin.Tests</RootNamespace>
7+
<Nullable>enable</Nullable>
78

89
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
910
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>

MultiAdmin.Tests/ServerIO/StringSectionsTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public StringSectionsTests(ITestOutputHelper output)
1919
[InlineData("test string", new[] { "te", "st", " s", "tr", "in", "g" }, 2)]
2020
[InlineData("test string", new[] { "tes..", ".t ..", ".st..", ".ring" }, 5, ".", "..")]
2121
public void FromStringTest(string testString, string[] expectedSections, int sectionLength,
22-
string leftIndictator = null, string rightIndictator = null)
22+
string? leftIndictator = null, string? rightIndictator = null)
2323
{
2424
StringSections sections = StringSections.FromString(testString, sectionLength,
2525
leftIndictator != null ? new ColoredMessage(leftIndictator) : null,
@@ -43,8 +43,8 @@ public void FromStringTest(string testString, string[] expectedSections, int sec
4343
[Theory]
4444
// No further characters can be output because of the prefix and suffix
4545
[InlineData("test string", 2, ".", ".")]
46-
public void FromStringThrowsTest(string testString, int sectionLength, string leftIndictator = null,
47-
string rightIndictator = null)
46+
public void FromStringThrowsTest(string testString, int sectionLength, string? leftIndictator = null,
47+
string? rightIndictator = null)
4848
{
4949
Assert.Throws<ArgumentException>(() =>
5050
{

MultiAdmin.Tests/Utility/StringExtensionsTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class StringExtensionsTests
1414
[InlineData("test", "es", 1, 2)]
1515
[InlineData(null, null, 0)]
1616
[InlineData(null, null, 0, 1)]
17-
public void EqualsTest(string main, string section, int startIndex, int count = -1)
17+
public void EqualsTest(string? main, string? section, int startIndex, int count = -1)
1818
{
1919
Assert.True(count < 0 ? main.Equals(section, startIndex) : main.Equals(section, startIndex, count));
2020
}
@@ -27,7 +27,7 @@ public void EqualsTest(string main, string section, int startIndex, int count =
2727
[InlineData(null, "test", 0)]
2828
[InlineData("test", null, 0, 1)]
2929
[InlineData(null, "test", 0, 1)]
30-
public void NotEqualsTest(string main, string section, int startIndex, int count = -1)
30+
public void NotEqualsTest(string? main, string? section, int startIndex, int count = -1)
3131
{
3232
Assert.False(count < 0 ? main.Equals(section, startIndex) : main.Equals(section, startIndex, count));
3333
}
@@ -37,7 +37,7 @@ public void NotEqualsTest(string main, string section, int startIndex, int count
3737
[InlineData(typeof(ArgumentOutOfRangeException), "test", "st", 3)]
3838
[InlineData(typeof(ArgumentOutOfRangeException), "test", "te", -1)]
3939
[InlineData(typeof(ArgumentOutOfRangeException), "test", "es", 4)]
40-
public void EqualsThrowsTest(Type expected, string main, string section, int startIndex, int count = -1)
40+
public void EqualsThrowsTest(Type expected, string? main, string? section, int startIndex, int count = -1)
4141
{
4242
Assert.Throws(expected, () =>
4343
{

MultiAdmin/Config/Config.cs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ namespace MultiAdmin.Config
1010
{
1111
public class Config
1212
{
13-
public string[] rawData = { };
13+
public string[] rawData = Array.Empty<string>();
1414

1515
public Config(string path)
1616
{
17+
internalConfigPath = path;
1718
ReadConfigFile(path);
1819
}
1920

@@ -26,7 +27,7 @@ private set
2627
{
2728
try
2829
{
29-
internalConfigPath = Utils.GetFullPathSafe(value);
30+
internalConfigPath = Utils.GetFullPathSafe(value) ?? value;
3031
}
3132
catch (Exception e)
3233
{
@@ -38,21 +39,19 @@ private set
3839

3940
public void ReadConfigFile(string configPath)
4041
{
41-
if (string.IsNullOrEmpty(configPath)) return;
42-
4342
ConfigPath = configPath;
4443

4544
try
4645
{
47-
rawData = File.Exists(ConfigPath) ? File.ReadAllLines(ConfigPath, Encoding.UTF8) : new string[] { };
46+
rawData = File.Exists(ConfigPath) ? File.ReadAllLines(ConfigPath, Encoding.UTF8) : Array.Empty<string>();
4847
}
4948
catch (Exception e)
5049
{
5150
Program.LogDebugException(nameof(ReadConfigFile), e);
5251

5352
new ColoredMessage[]
5453
{
55-
new ColoredMessage($"Error while reading config (Path = {ConfigPath ?? "Null"}):",
54+
new ColoredMessage($"Error while reading config (Path = {ConfigPath}):",
5655
ConsoleColor.Red),
5756
new ColoredMessage(e.ToString(), ConsoleColor.Red)
5857
}.WriteLines();
@@ -79,7 +78,7 @@ private static string CleanValue(string value, bool removeQuotes = true)
7978
try
8079
{
8180
if (removeQuotes && newValue.StartsWith("\"") && newValue.EndsWith("\""))
82-
return newValue.Substring(1, newValue.Length - 2);
81+
return newValue[1..^1];
8382
}
8483
catch (Exception e)
8584
{
@@ -89,7 +88,7 @@ private static string CleanValue(string value, bool removeQuotes = true)
8988
return newValue;
9089
}
9190

92-
public string GetString(string key, string def = null, bool removeQuotes = true)
91+
public string? GetString(string key, string? def = null, bool removeQuotes = true)
9392
{
9493
try
9594
{
@@ -99,7 +98,7 @@ public string GetString(string key, string def = null, bool removeQuotes = true)
9998

10099
try
101100
{
102-
return CleanValue(line.Substring(key.Length + 1), removeQuotes);
101+
return CleanValue(line[(key.Length + 1)..], removeQuotes);
103102
}
104103
catch (Exception e)
105104
{
@@ -115,11 +114,11 @@ public string GetString(string key, string def = null, bool removeQuotes = true)
115114
return def;
116115
}
117116

118-
public string[] GetStringArray(string key, string[] def = null)
117+
public string[]? GetStringArray(string key, string[]? def = null)
119118
{
120119
try
121120
{
122-
string value = GetString(key, removeQuotes: false);
121+
string? value = GetString(key, removeQuotes: false);
123122

124123
if (!string.IsNullOrEmpty(value))
125124
{
@@ -145,7 +144,7 @@ public int GetInt(string key, int def = 0)
145144
{
146145
try
147146
{
148-
string value = GetString(key);
147+
string? value = GetString(key);
149148

150149
if (!string.IsNullOrEmpty(value) && int.TryParse(value, out int parseValue))
151150
return parseValue;
@@ -162,7 +161,7 @@ public uint GetUInt(string key, uint def = 0)
162161
{
163162
try
164163
{
165-
string value = GetString(key);
164+
string? value = GetString(key);
166165

167166
if (!string.IsNullOrEmpty(value) && uint.TryParse(value, out uint parseValue))
168167
return parseValue;
@@ -179,7 +178,7 @@ public float GetFloat(string key, float def = 0)
179178
{
180179
try
181180
{
182-
string value = GetString(key);
181+
string? value = GetString(key);
183182

184183
if (!string.IsNullOrEmpty(value) && float.TryParse(value, out float parsedValue))
185184
return parsedValue;
@@ -196,7 +195,7 @@ public double GetDouble(string key, double def = 0)
196195
{
197196
try
198197
{
199-
string value = GetString(key);
198+
string? value = GetString(key);
200199

201200
if (!string.IsNullOrEmpty(value) && double.TryParse(value, out double parsedValue))
202201
return parsedValue;
@@ -213,7 +212,7 @@ public decimal GetDecimal(string key, decimal def = 0)
213212
{
214213
try
215214
{
216-
string value = GetString(key);
215+
string? value = GetString(key);
217216

218217
if (!string.IsNullOrEmpty(value) && decimal.TryParse(value, out decimal parsedValue))
219218
return parsedValue;
@@ -230,7 +229,7 @@ public bool GetBool(string key, bool def = false)
230229
{
231230
try
232231
{
233-
string value = GetString(key);
232+
string? value = GetString(key);
234233

235234
if (!string.IsNullOrEmpty(value) && bool.TryParse(value, out bool parsedValue))
236235
return parsedValue;
@@ -247,7 +246,7 @@ public InputHandler.ConsoleInputSystem GetConsoleInputSystem(string key, InputHa
247246
{
248247
try
249248
{
250-
string value = GetString(key);
249+
string? value = GetString(key);
251250

252251
if (!string.IsNullOrEmpty(value) && Enum.TryParse<InputHandler.ConsoleInputSystem>(value, out var consoleInputSystem))
253252
return consoleInputSystem;

MultiAdmin/Config/ConfigHandler/ConfigEntry.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ public abstract class ConfigEntry
2020
/// <summary>
2121
/// The value of the <see cref="ConfigEntry"/>.
2222
/// </summary>
23-
public abstract object ObjectValue { get; set; }
23+
public abstract object? ObjectValue { get; set; }
2424

2525
/// <summary>
2626
/// The default value of the <see cref="ConfigEntry"/>.
2727
/// </summary>
28-
public abstract object ObjectDefault { get; set; }
28+
public abstract object? ObjectDefault { get; set; }
2929

3030
/// <summary>
3131
/// Whether to inherit this config value from the <see cref="ConfigRegister"/>'s parent <see cref="ConfigRegister"/>s if they support value inheritance.
@@ -35,17 +35,17 @@ public abstract class ConfigEntry
3535
/// <summary>
3636
/// The name of the <see cref="ConfigEntry"/>.
3737
/// </summary>
38-
public string Name { get; }
38+
public string? Name { get; }
3939

4040
/// <summary>
4141
/// The description of the <see cref="ConfigEntry"/>.
4242
/// </summary>
43-
public string Description { get; }
43+
public string? Description { get; }
4444

4545
/// <summary>
4646
/// Creates a basic <see cref="ConfigEntry"/> with no values and indication for whether to inherit the value.
4747
/// </summary>
48-
public ConfigEntry(string key, bool inherit = true, string name = null, string description = null)
48+
public ConfigEntry(string key, bool inherit = true, string? name = null, string? description = null)
4949
{
5050
Key = key;
5151

@@ -58,7 +58,7 @@ public ConfigEntry(string key, bool inherit = true, string name = null, string d
5858
/// <summary>
5959
/// Creates a basic <see cref="ConfigEntry"/> with no values.
6060
/// </summary>
61-
public ConfigEntry(string key, string name = null, string description = null) : this(key, true, name,
61+
public ConfigEntry(string key, string? name = null, string? description = null) : this(key, true, name,
6262
description)
6363
{
6464
}
@@ -82,33 +82,34 @@ public class ConfigEntry<T> : ConfigEntry
8282
/// </summary>
8383
public T Default { get; set; }
8484

85-
public override object ObjectValue
85+
public override object? ObjectValue
8686
{
8787
get => Value;
88-
set => Value = (T)value;
88+
set => Value = (T)value!;
8989
}
9090

91-
public override object ObjectDefault
91+
public override object? ObjectDefault
9292
{
9393
get => Default;
94-
set => Default = (T)value;
94+
set => Default = (T)value!;
9595
}
9696

9797
/// <inheritdoc />
9898
/// <summary>
9999
/// Creates a <see cref="ConfigEntry{T}" /> with the provided type, default value, and indication for whether to inherit the value.
100100
/// </summary>
101-
public ConfigEntry(string key, T defaultValue = default, bool inherit = true, string name = null,
102-
string description = null) : base(key, inherit, name, description)
101+
public ConfigEntry(string key, T defaultValue, bool inherit = true, string? name = null,
102+
string? description = null) : base(key, inherit, name, description)
103103
{
104+
Value = defaultValue;
104105
Default = defaultValue;
105106
}
106107

107108
/// <inheritdoc />
108109
/// <summary>
109110
/// Creates a <see cref="ConfigEntry{T}" /> with the provided type and default value.
110111
/// </summary>
111-
public ConfigEntry(string key, T defaultValue = default, string name = null, string description = null) : this(
112+
public ConfigEntry(string key, T defaultValue, string? name = null, string? description = null) : this(
112113
key, defaultValue, true, name, description)
113114
{
114115
}

MultiAdmin/Config/ConfigHandler/ConfigRegister.cs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public ConfigEntry[] GetRegisteredConfigs()
2424
/// Returns the first <see cref="ConfigEntry"/> with a key matching <paramref name="key"/>.
2525
/// </summary>
2626
/// <param name="key">The key of the <see cref="ConfigEntry"/> to retrieve.</param>
27-
public ConfigEntry GetRegisteredConfig(string key)
27+
public ConfigEntry? GetRegisteredConfig(string key)
2828
{
2929
if (string.IsNullOrEmpty(key))
3030
return null;
@@ -47,7 +47,7 @@ public ConfigEntry GetRegisteredConfig(string key)
4747
/// <param name="updateValue">Whether to update the value of the config after registration.</param>
4848
public void RegisterConfig(ConfigEntry configEntry, bool updateValue = true)
4949
{
50-
if (configEntry == null || string.IsNullOrEmpty(configEntry.Key))
50+
if (string.IsNullOrEmpty(configEntry.Key))
5151
return;
5252

5353
registeredConfigs.Add(configEntry);
@@ -63,9 +63,6 @@ public void RegisterConfig(ConfigEntry configEntry, bool updateValue = true)
6363
/// <param name="updateValue">Whether to update the value of the config after registration.</param>
6464
public void RegisterConfigs(ConfigEntry[] configEntries, bool updateValue = true)
6565
{
66-
if (configEntries == null)
67-
return;
68-
6966
foreach (ConfigEntry configEntry in configEntries)
7067
{
7168
RegisterConfig(configEntry, updateValue);
@@ -78,7 +75,7 @@ public void RegisterConfigs(ConfigEntry[] configEntries, bool updateValue = true
7875
/// <param name="configEntry">The <see cref="ConfigEntry"/> to be un-registered.</param>
7976
public void UnRegisterConfig(ConfigEntry configEntry)
8077
{
81-
if (configEntry == null || string.IsNullOrEmpty(configEntry.Key))
78+
if (string.IsNullOrEmpty(configEntry.Key))
8279
return;
8380

8481
registeredConfigs.Remove(configEntry);
@@ -90,7 +87,9 @@ public void UnRegisterConfig(ConfigEntry configEntry)
9087
/// <param name="key">The key of the <see cref="ConfigEntry"/> to be un-registered.</param>
9188
public void UnRegisterConfig(string key)
9289
{
93-
UnRegisterConfig(GetRegisteredConfig(key));
90+
ConfigEntry? entry = GetRegisteredConfig(key);
91+
if (entry != null)
92+
UnRegisterConfig(entry);
9493
}
9594

9695
/// <summary>
@@ -99,9 +98,6 @@ public void UnRegisterConfig(string key)
9998
/// <param name="configEntries">The <see cref="ConfigEntry"/>s to be un-registered.</param>
10099
public void UnRegisterConfigs(params ConfigEntry[] configEntries)
101100
{
102-
if (configEntries == null)
103-
return;
104-
105101
foreach (ConfigEntry configEntry in configEntries)
106102
{
107103
UnRegisterConfig(configEntry);
@@ -114,9 +110,6 @@ public void UnRegisterConfigs(params ConfigEntry[] configEntries)
114110
/// <param name="keys">The keys of the <see cref="ConfigEntry"/>s to be un-registered.</param>
115111
public void UnRegisterConfigs(params string[] keys)
116112
{
117-
if (keys == null)
118-
return;
119-
120113
foreach (string key in keys)
121114
{
122115
UnRegisterConfig(key);
@@ -146,9 +139,6 @@ public void UnRegisterConfigs()
146139
/// <param name="configEntries">The <see cref="ConfigEntry"/>s to be assigned values.</param>
147140
public void UpdateConfigValues(params ConfigEntry[] configEntries)
148141
{
149-
if (configEntries == null)
150-
return;
151-
152142
foreach (ConfigEntry configEntry in configEntries)
153143
{
154144
UpdateConfigValue(configEntry);

0 commit comments

Comments
 (0)