Skip to content

Commit eff91f8

Browse files
committed
Tweak project settings and fix warnings where possible
1 parent 4eb59b1 commit eff91f8

122 files changed

Lines changed: 2119 additions & 473 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<OutputType>Library</OutputType>
44
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
5-
<LangVersion>13.0</LangVersion>
5+
<LangVersion>14.0</LangVersion>
66
<Nullable>enable</Nullable>
77
<Deterministic>true</Deterministic>
88
<AllowUnsafeBlocks>False</AllowUnsafeBlocks>

MonkeyLoader.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1313
ProjectSection(SolutionItems) = preProject
1414
Directory.Build.props = Directory.Build.props
1515
docfx.json = docfx.json
16+
nuget.config = nuget.config
1617
README.md = README.md
1718
EndProjectSection
1819
EndProject

MonkeyLoader/AnyMap.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace MonkeyLoader
1111
/// </summary>
1212
public sealed class AnyMap
1313
{
14-
private readonly Dictionary<Type, object?> _dict = new();
14+
private readonly Dictionary<Type, object?> _dict = [];
1515

1616
/// <summary>
1717
/// Gets all <see cref="Type"/>s that have a set value in this AnyMap.

MonkeyLoader/AssemblyName.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public AssemblyName(string name, bool isPath = false)
2727
=> string.Equals(left.Name, right.Name, StringComparison.InvariantCultureIgnoreCase);
2828

2929
/// <inheritdoc/>
30-
public override readonly bool Equals(object obj)
30+
public override readonly bool Equals(object? obj)
3131
=> obj is AssemblyName assemblyName && assemblyName == this;
3232

3333
/// <inheritdoc/>

MonkeyLoader/AssemblyPool.cs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ namespace MonkeyLoader
2121
/// </summary>
2222
public sealed class AssemblyPool : IAssemblyResolver
2323
{
24-
private readonly Dictionary<AssemblyName, AssemblyEntry> _assemblies = new();
24+
private readonly Dictionary<AssemblyName, AssemblyEntry> _assemblies = [];
2525
private readonly HashSet<string> _directories = new(StringComparer.OrdinalIgnoreCase);
26-
private readonly HashSet<AssemblyPool> _fallbackPools = new();
26+
private readonly HashSet<AssemblyPool> _fallbackPools = [];
2727
private readonly Func<string?>? _getPatchedAssemblyPath;
2828
private readonly Logger _logger;
2929

@@ -39,6 +39,8 @@ public sealed class AssemblyPool : IAssemblyResolver
3939
/// <summary>
4040
/// Creates a new <see cref="AssemblyPool"/> instance, loading assemblies when asked to resolve them if desired.
4141
/// </summary>
42+
/// <param name="loader">The mod loader that this pool belongs to.</param>
43+
/// <param name="poolName">The name of this pool.</param>
4244
/// <param name="getPatchedAssemblyPath">Provides the path where to save patched assemblies. Return <c>null</c> to disable.</param>
4345
/// <param name="loadForResolve">Whether to load assemblies when asked to resolve them.</param>
4446
public AssemblyPool(MonkeyLoader loader, string poolName = "AssemblyPool", Func<string?>? getPatchedAssemblyPath = null, bool loadForResolve = true)
@@ -51,16 +53,14 @@ public AssemblyPool(MonkeyLoader loader, string poolName = "AssemblyPool", Func<
5153
AppDomain.CurrentDomain.AssemblyResolve += ResolveAssembly;
5254
}
5355

54-
public bool AddFallbackPool(AssemblyPool pool) => _fallbackPools.Add(pool);
56+
public bool AddFallbackPool(AssemblyPool pool)
57+
=> _fallbackPools.Add(pool);
5558

5659
public void AddSearchDirectory(string directory)
57-
{
58-
_directories.Add(directory);
59-
}
60+
=> _directories.Add(directory);
6061

6162
public void Dispose()
62-
{
63-
}
63+
{ }
6464

6565
public IEnumerable<ILoadedNuGetPackage> GetAllAsLoadedPackages(string optionalPrefix)
6666
{
@@ -131,18 +131,6 @@ public void LoadAll(string path)
131131
/// <returns>The loaded <see cref="Assembly"/>.</returns>
132132
/// <exception cref="KeyNotFoundException">When the <paramref name="name"/> doesn't exist in this pool.</exception>
133133
public Assembly LoadAssembly(AssemblyName name) => GetEntry(name).LoadAssembly(_logger, PatchedAssemblyPath);
134-
135-
public bool TryResolveAssembly(AssemblyName name, [NotNullWhen(true)] out Assembly? assembly)
136-
{
137-
if (TryGetEntry(name, out var entry))
138-
{
139-
assembly = entry.LoadAssembly(_logger, PatchedAssemblyPath);
140-
return true;
141-
}
142-
143-
assembly = null;
144-
return false;
145-
}
146134

147135
public AssemblyDefinition LoadDefinition(string path, ReaderParameters? readerParameters = null)
148136
{
@@ -231,6 +219,18 @@ public bool TryResolve(AssemblyName name, [NotNullWhen(true)] out AssemblyDefini
231219
return false;
232220
}
233221

222+
public bool TryResolveAssembly(AssemblyName name, [NotNullWhen(true)] out Assembly? assembly)
223+
{
224+
if (TryGetEntry(name, out var entry))
225+
{
226+
assembly = entry.LoadAssembly(_logger, PatchedAssemblyPath);
227+
return true;
228+
}
229+
230+
assembly = null;
231+
return false;
232+
}
233+
234234
/// <summary>
235235
/// Tries to wait until nothing else is modifying the <see cref="AssemblyDefinition"/> of an entry anymore,
236236
/// before making a snapshot and returning it. The definition has to be returned using
@@ -271,7 +271,7 @@ private AssemblyEntry GetEntry(AssemblyName name)
271271
return assemblyDefinition;
272272
}
273273

274-
private Assembly? ResolveAssembly(object sender, ResolveEventArgs args)
274+
private Assembly? ResolveAssembly(object? sender, ResolveEventArgs args)
275275
{
276276
var name = new AssemblyName(AssemblyNameReference.Parse(args.Name).Name);
277277

@@ -287,7 +287,7 @@ private AssemblyEntry GetEntry(AssemblyName name)
287287
private AssemblyDefinition? SearchDirectory(AssemblyNameReference name, ReaderParameters parameters)
288288
{
289289
parameters.AssemblyResolver ??= this;
290-
var extensions = name.IsWindowsRuntime ? new[] { ".winmd", ".dll" } : new[] { ".exe", ".dll" };
290+
string[] extensions = name.IsWindowsRuntime ? [".winmd", ".dll"] : [".exe", ".dll"];
291291

292292
foreach (var directory in _directories)
293293
{
@@ -310,15 +310,15 @@ private AssemblyEntry GetEntry(AssemblyName name)
310310
return null;
311311
}
312312

313-
private bool TryGetEntry(AssemblyName name, out AssemblyEntry entry)
313+
private bool TryGetEntry(AssemblyName name, [NotNullWhen(true)] out AssemblyEntry? entry)
314314
=> _assemblies.TryGetValue(name, out entry);
315315

316316
private sealed class AssemblyEntry : IDisposable
317317
{
318318
public readonly FileInfo? AssemblyFile;
319319
public readonly AssemblyName Name;
320-
private AssemblyDefinition _definition;
321320
private readonly IAssemblyLoadStrategy _assemblyLoadStrategy;
321+
private AssemblyDefinition _definition;
322322
private AutoResetEvent? _definitionLock;
323323
private MemoryStream? _definitionSnapshot;
324324
private bool _disposedValue = false;
@@ -361,7 +361,7 @@ public IEnumerable<AssemblyName> GetDependencies(HashSet<AssemblyName> alreadyLo
361361
: _definition.GetAssemblyReferences()
362362
.Select(reference => AssemblyNameReference.Parse(reference.Name).Name);
363363

364-
return fullNames.Select(name => new AssemblyName(name))
364+
return fullNames.Select(name => new AssemblyName(name!))
365365
.Where(name => !alreadyLoaded.Contains(name));
366366
}
367367

@@ -387,7 +387,7 @@ public Assembly LoadAssembly(Logger logger, string? patchedAssemblyPath)
387387
// so let's always load assemblies from files for now
388388
if (saveAssemblies)
389389
{
390-
var targetPath = Path.Combine(patchedAssemblyPath, $"{Name}.dll");
390+
var targetPath = Path.Combine(patchedAssemblyPath!, $"{Name}.dll");
391391

392392
try
393393
{
@@ -398,7 +398,7 @@ public Assembly LoadAssembly(Logger logger, string? patchedAssemblyPath)
398398
{
399399
logger.Warn(ex.LogFormat($"Exception while trying to save assembly to {targetPath}"));
400400
}
401-
401+
402402
_loadedAssembly = _assemblyLoadStrategy.LoadFile(Path.GetFullPath(targetPath));
403403
logger.Trace(() => $"Loaded changed assembly definition [{Name}]");
404404
}

MonkeyLoader/Configuration/ConfigKeyValidator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ public static class ConfigKeyValidator
1111
/// <summary>
1212
/// Gets a validator component that only accepts non-null non-whitespace strings.
1313
/// </summary>
14-
public static IConfigKeyValidator<string> NotNullOrWhitespace { get; } = new ConfigKeyValidator<string>(value => !string.IsNullOrWhiteSpace(value));
14+
public static IConfigKeyValidator<string> NotNullOrWhitespace { get; } = new ConfigKeyValidator<string>(static value => !string.IsNullOrWhiteSpace(value));
1515

1616
/// <summary>
1717
/// Creates a new validator component that only accepts strings,
1818
/// where the given <paramref name="regex"/> has a match.
1919
/// </summary>
2020
/// <param name="regex">The regular expression that must have a match.</param>
2121
/// <returns>The validator component.</returns>
22-
public static IConfigKeyValidator<string> Matching(Regex regex) => new ConfigKeyValidator<string>(regex.IsMatch);
22+
public static IConfigKeyValidator<string> Matching(Regex regex) => new ConfigKeyValidator<string>(value => value is not null && regex.IsMatch(value));
2323
}
2424

2525
/// <summary>

MonkeyLoader/Configuration/ConfigSection.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ protected ConfigSection()
154154
/// </summary>
155155
/// <param name="obj">The other object.</param>
156156
/// <returns><c>true</c> if the other object is considered equal.</returns>
157-
public override bool Equals(object obj)
157+
public override bool Equals(object? obj)
158158
=> obj is ConfigSection section && section == this;
159159

160160
/// <summary>
@@ -302,7 +302,7 @@ internal void ResetHasChanges()
302302
/// <param name="key">The key to deserialize.</param>
303303
/// <param name="source">The <see cref="JObject"/> being deserialized from.</param>
304304
/// <param name="jsonSerializer">The <see cref="JsonSerializer"/> to deserialize objects with.</param>
305-
protected void DeserializeKey(IDefiningConfigKey key, JObject source, JsonSerializer jsonSerializer)
305+
protected static void DeserializeKey(IDefiningConfigKey key, JObject source, JsonSerializer jsonSerializer)
306306
{
307307
if (source[key.Id] is not JToken token)
308308
return;
@@ -400,7 +400,7 @@ protected virtual void OnSave(JObject result, JsonSerializer jsonSerializer)
400400
/// <param name="key">The key to serialize.</param>
401401
/// <param name="result">The <see cref="JObject"/> being serialized to.</param>
402402
/// <param name="jsonSerializer">The <see cref="JsonSerializer"/> to serialize objects with.</param>
403-
protected void SerializeKey(IDefiningConfigKey key, JObject result, JsonSerializer jsonSerializer)
403+
protected static void SerializeKey(IDefiningConfigKey key, JObject result, JsonSerializer jsonSerializer)
404404
{
405405
if (!key.TryGetValue(out var value))
406406
return;
@@ -415,7 +415,7 @@ protected void SerializeKey(IDefiningConfigKey key, JObject result, JsonSerializ
415415
/// <param name="key">The key that wasn't found.</param>
416416
/// <exception cref="KeyNotFoundException">Always.</exception>
417417
[DoesNotReturn]
418-
protected void ThrowKeyNotFound(IConfigKey key)
418+
protected static void ThrowKeyNotFound(IConfigKey key)
419419
=> throw new KeyNotFoundException($"Key [{key.Id}] not found in this config section!");
420420

421421
private static bool AreVersionsCompatible(Version serializedVersion, Version currentVersion)

MonkeyLoader/Configuration/DefiningConfigKey.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,12 @@ public DefiningConfigKey(string id, string? description = null, Func<T>? compute
185185
public static implicit operator T?(DefiningConfigKey<T> key) => key.GetValue();
186186

187187
/// <inheritdoc/>
188-
public bool Equals(IConfigKey other) => ConfigKey.EqualityComparer.Equals(this, other);
188+
public bool Equals(IConfigKey? other)
189+
=> ConfigKey.EqualityComparer.Equals(this, other!);
189190

190191
/// <inheritdoc/>
191-
public override bool Equals(object obj) => obj is IConfigKey otherKey && Equals(otherKey);
192+
public override bool Equals(object? obj)
193+
=> obj is IConfigKey otherKey && Equals(otherKey);
192194

193195
/// <inheritdoc/>
194196
public IEnumerator<IComponent<IDefiningConfigKey>> GetEnumerator()
@@ -419,10 +421,10 @@ private void OnChanged(bool hadValue, T? oldValue, string? eventLabel, string? c
419421
private bool Validate(object? value)
420422
=> (value is T || (value is null && Util.CanBeNull(ValueType))) && Validate((T)value!);
421423

422-
private void ValueCollectionChanged(object sender, NotifyCollectionChangedEventArgs eventArgs)
424+
private void ValueCollectionChanged(object? sender, NotifyCollectionChangedEventArgs eventArgs)
423425
=> OnChanged(true, _value, ConfigKey.CollectionChangedEventLabel, null, eventArgs);
424426

425-
private void ValuePropertyChanged(object sender, PropertyChangedEventArgs eventArgs)
427+
private void ValuePropertyChanged(object? sender, PropertyChangedEventArgs eventArgs)
426428
=> OnChanged(true, _value, ConfigKey.PropertyChangedEventLabel, eventArgs.PropertyName);
427429

428430
/// <inheritdoc/>

MonkeyLoader/Configuration/ExpandoConfigSection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ protected override void OnLoad(JObject source, JsonSerializer jsonSerializer)
119119
base.OnLoad(source, jsonSerializer);
120120
}
121121

122-
private IDefiningConfigKey<T> AddDefiningKey<T>(string name, string? description, Func<T>? computeDefault, bool internalAccessOnly, Predicate<T?>? valueValidator)
122+
private DefiningConfigKey<T> AddDefiningKey<T>(string name, string? description, Func<T>? computeDefault, bool internalAccessOnly, Predicate<T?>? valueValidator)
123123
{
124124
var definingKey = new DefiningConfigKey<T>(name, description, computeDefault, internalAccessOnly, valueValidator);
125125
definingKey.Section = this;

MonkeyLoader/Entrypoint.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ public static void Start()
2424
{
2525
try
2626
{
27-
if (Path.GetFileName(file).StartsWith("doorstop", StringComparison.OrdinalIgnoreCase)
28-
&& Path.GetExtension(file).Equals(".log", StringComparison.OrdinalIgnoreCase))
27+
if (Path.GetFileName(file).StartsWith("doorstop", StringComparison.OrdinalIgnoreCase) && Path.GetExtension(file).Equals(".log", StringComparison.OrdinalIgnoreCase))
2928
File.Delete(file);
3029
}
3130
catch

0 commit comments

Comments
 (0)