Skip to content

Commit

Permalink
Code cleanup to remove warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
SamboyCoding committed Mar 16, 2024
1 parent 27ec7d8 commit 22e87d0
Show file tree
Hide file tree
Showing 18 changed files with 70 additions and 65 deletions.
Binary file modified 1.3/Assemblies/BetterLoading.dll
Binary file not shown.
25 changes: 14 additions & 11 deletions Source/BetterLoadingConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@ namespace BetterLoading;

public class BetterLoadingConfig
{

public static BetterLoadingConfig CreateDefault()
[TomlPrecedingComment("The TipCache caches information about loading screen tips so that they can be displayed as soon as the loading screen starts after the first run.")]
public TipCacheConfig TipCache;

public BetterLoadingConfig()
{
return new()
{
TipCache = new()
{
Version = TipCacheConfig.SupportedVersion,
}
};
TipCache = new();
}

[TomlPrecedingComment("The TipCache caches information about loading screen tips so that they can be displayed as soon as the loading screen starts after the first run.")]
public TipCacheConfig TipCache;
public static BetterLoadingConfig CreateDefault()
{
return new();
}

[TomlDoNotInlineObject]
public class TipCacheConfig
Expand All @@ -29,5 +27,10 @@ public class TipCacheConfig
public int Version;
[TomlPrecedingComment("The raw tip blob. NOT intended to be manually edited.")]
public byte[] Tips = Array.Empty<byte>();

public TipCacheConfig()
{
Version = SupportedVersion;
}
}
}
13 changes: 7 additions & 6 deletions Source/BetterLoadingMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public class DllLoadError
{
public string dllName;
public LogMessage? reasonMessage;

public DllLoadError(string dllName, LogMessage? reasonMessage)
{
this.dllName = dllName;
this.reasonMessage = reasonMessage;
}
}

public BetterLoadingMain(ModContentPack content) : base(content)
Expand Down Expand Up @@ -62,12 +68,7 @@ public BetterLoadingMain(ModContentPack content) : base(content)
var failures = didntLoad
.Select(Path.GetFileNameWithoutExtension)
.Select(
filename =>
new DllLoadError
{
dllName = filename,
reasonMessage = loadFailures.FirstOrDefault(msg => msg.text.Contains($"assembly {filename}"))
}
filename => new DllLoadError(filename, loadFailures.FirstOrDefault(msg => msg.text.Contains($"assembly {filename}")))
)
.ToList();

Expand Down
8 changes: 4 additions & 4 deletions Source/Compat/HugsLib/StageHugsLibInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public class StageHugsLibInit : LoadingStage
private static int _numChildrenDefLoaded;

private static object? _currentChildMod;
private static Harmony hInstance;
private static Harmony? hInstance;

private static PropertyInfo _modIdentifierProperty;
private static PropertyInfo _modIdentifierProperty = null!;

private static StageHugsLibInit? inst;

Expand Down Expand Up @@ -120,10 +120,10 @@ public static void PostEnumerateChildren(object ___childMods, Dictionary<Assembl
foreach (var childMod in _children)
{
if (childMod.GetType().DeclaresOwnMethod("Initialize"))
hInstance.Patch(AccessTools.Method(childMod.GetType(), "Initialize"), new(typeof(StageHugsLibInit), nameof(PreChildInit)), new(typeof(StageHugsLibInit), nameof(PostChildInit)));
hInstance!.Patch(AccessTools.Method(childMod.GetType(), "Initialize"), new(typeof(StageHugsLibInit), nameof(PreChildInit)), new(typeof(StageHugsLibInit), nameof(PostChildInit)));

if (childMod.GetType().DeclaresOwnMethod("DefsLoaded"))
hInstance.Patch(AccessTools.Method(childMod.GetType(), "DefsLoaded"), new(typeof(StageHugsLibInit), nameof(PreDefsLoaded)), new(typeof(StageHugsLibInit), nameof(PostDefsLoaded)));
hInstance!.Patch(AccessTools.Method(childMod.GetType(), "DefsLoaded"), new(typeof(StageHugsLibInit), nameof(PreDefsLoaded)), new(typeof(StageHugsLibInit), nameof(PostDefsLoaded)));
}
}

Expand Down
14 changes: 3 additions & 11 deletions Source/LoadingScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,7 @@ public LoadingScreen()
BootLoadList.ForEach(s => _loadingStagesByType[s.GetType()] = s);

_currentStage.BecomeActive();
StageTimingData.ExecutedStages.Add(new()
{
start = DateTime.Now,
stage = _currentStage
});
StageTimingData.ExecutedStages.Add(new(DateTime.Now, _currentStage));
}

internal static void RegisterStageInstance<T>(T stage) where T: LoadingStage
Expand Down Expand Up @@ -229,7 +225,7 @@ public void OnGUI()
Log.Error($"[BetterLoading] The stage {_currentStage} errored during BecomeInactive: {e}");
}

StageTimingData.ExecutedStages.Last().end = DateTime.Now;
StageTimingData.ExecutedStages.Last().End = DateTime.Now;

_currentStage = currentList[idx + 1];
try
Expand All @@ -242,11 +238,7 @@ public void OnGUI()
Log.Error($"[BetterLoading] The stage {_currentStage} errored during BecomeActive: {e}");
}

StageTimingData.ExecutedStages.Add(new()
{
start = DateTime.Now,
stage = _currentStage
});
StageTimingData.ExecutedStages.Add(new(DateTime.Now, _currentStage));
idx++;
}

Expand Down
2 changes: 0 additions & 2 deletions Source/LoadingScreenTipManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace BetterLoading
{
public static class LoadingScreenTipManager
{
private static bool _hasAttemptedToLoadTips;
private static bool _tipDatabaseHasLoadedBackingField;
private static bool _hideVanillaTipsBackingField;

Expand Down Expand Up @@ -42,7 +41,6 @@ public static bool HideVanillaTips

public static void OnAvailableTipSetChanged()
{
_hasAttemptedToLoadTips = true;

Tips.Clear();

Expand Down
2 changes: 1 addition & 1 deletion Source/Stage/InitialLoad/0StageInitMods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class StageInitMods : LoadingStage
private static int _numMods = int.MaxValue;

private static int _currentModIdx = typeof(Mod).InstantiableDescendantsAndSelf().FirstIndexOf(t => t == typeof(BetterLoadingMain)) + 1;
private static ModContentPack _currentMod = BetterLoadingMain.ourContentPack;
private static ModContentPack _currentMod = BetterLoadingMain.ourContentPack!;
private static bool _completed;

private static StageInitMods? inst;
Expand Down
2 changes: 1 addition & 1 deletion Source/Stage/InitialLoad/1StageReadXML.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static void AlternativeLoadModXml(ref List<LoadableXmlAsset> __result)
}).ToList();

Log.Message($"[BetterLoading] [Enhanced XML Load] Loaded {__result.Count} loadable assets.");
_currentPackIdx = inst._numPacks + 1;
_currentPackIdx = inst!._numPacks + 1;
}

public static void OnLoadDefsComplete(ModContentPack __instance)
Expand Down
4 changes: 2 additions & 2 deletions Source/Stage/InitialLoad/3StageApplyPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ namespace BetterLoading.Stage.InitialLoad
{
public class StageApplyPatches : LoadingStage
{
private static List<ModContentPack> _modList;
private static List<ModContentPack> _modList = null!;
private static ModContentPack? _currentMod;
private static int _currentModNum;

private static bool _loadingPatches;
private static int _numPatches = -1;
private static int _currentPatch;

private static StageApplyPatches inst;
private static StageApplyPatches? inst;
private static bool _hasFinished;

public StageApplyPatches(Harmony instance) : base(instance)
Expand Down
2 changes: 1 addition & 1 deletion Source/Stage/InitialLoad/4StageRegisterDefs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class StageRegisterDefs : LoadingStage
private static int _numDefsToRegister = 1;
private static int _currentDefNum;

private static StageRegisterDefs inst;
private static StageRegisterDefs? inst;

public StageRegisterDefs(Harmony instance) : base(instance)
{
Expand Down
15 changes: 7 additions & 8 deletions Source/Stage/InitialLoad/5StageConstructDefs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Reflection;
using System.Xml;
using HarmonyLib;
using UnityEngine;
using Verse;

namespace BetterLoading.Stage.InitialLoad
Expand All @@ -15,14 +14,14 @@ public class StageConstructDefs : LoadingStage
private static int _numDefsToResolve = 1;
private static int _currentDefNum;
private static bool _shouldCount;
private static LoadableXmlAsset _asset;
private static LoadableXmlAsset? _asset;

private static StageConstructDefs inst;
private static StageConstructDefs? inst;

private static readonly ConcurrentDictionary<Type, Func<XmlNode, bool, object>> objectFromXmlMethods = new();
private static ConcurrentDictionary<TypeCacheKey, Type> typeCache = new(EqualityComparer<TypeCacheKey>.Default);

private static MethodInfo GetTypeInternal = typeof(GenTypes).GetMethod("GetTypeInAnyAssemblyInt", BindingFlags.Static | BindingFlags.NonPublic);
private static MethodInfo GetTypeInternal = typeof(GenTypes).GetMethod("GetTypeInAnyAssemblyInt", BindingFlags.Static | BindingFlags.NonPublic) ?? throw new Exception("Failed to find GetTypeInAnyAssemblyInt");


public StageConstructDefs(Harmony instance) : base(instance)
Expand Down Expand Up @@ -53,7 +52,7 @@ public override void BecomeInactive()
GlobalTimingData.TicksFinishedConstructingDefs = DateTime.UtcNow.Ticks;
}

public override string? GetCurrentStepName()
public override string GetCurrentStepName()
{
if (_asset?.name == null)
return "<initializing...>";
Expand Down Expand Up @@ -114,9 +113,9 @@ public static void PostParseProcessXml()
_currentDefNum = _numDefsToResolve;
}

public static void ParallelParseAndProcessXML(XmlDocument xmlDoc, Dictionary<XmlNode, LoadableXmlAsset> assetlookup)
public static void ParallelParseAndProcessXML(XmlDocument xmlDoc, Dictionary<XmlNode, LoadableXmlAsset?> assetlookup)
{
var xmlNodeList = xmlDoc.DocumentElement.ChildNodes.Cast<XmlNode>().ToList();
var xmlNodeList = xmlDoc.DocumentElement!.ChildNodes.Cast<XmlNode>().ToList();

//Changed from vanilla in that it's parallel and doesn't have any DeepProfiling
xmlNodeList
Expand All @@ -132,7 +131,7 @@ public static void ParallelParseAndProcessXML(XmlDocument xmlDoc, Dictionary<Xml
try
{
var processedDefs = xmlNodeList.AsParallel()
.Select(node => (node, assetlookup.TryGetValue(node)))
.Select(node => (node, assetlookup.TryGetValue(node)!))
.Select(tuple =>
{
var (node, asset) = tuple;
Expand Down
2 changes: 1 addition & 1 deletion Source/Stage/InitialLoad/6StageResolveDefDatabases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class StageResolveDefDatabases : LoadingStage
private static int _numDatabases = 1;
private static int _currentDatabaseNum;

private static StageResolveDefDatabases inst;
private static StageResolveDefDatabases? inst;

public StageResolveDefDatabases(Harmony instance) : base(instance)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class StageRunPostLoadPreFinalizeCallbacks : LoadingStage
private static bool _finishedExecuting;
private static bool _done;

private static StageRunPostLoadPreFinalizeCallbacks inst;
private static StageRunPostLoadPreFinalizeCallbacks? inst;


public StageRunPostLoadPreFinalizeCallbacks(Harmony instance) : base(instance)
Expand Down Expand Up @@ -116,7 +116,7 @@ public static bool PreExecToExecWhenFinished(List<Action> ___toExecuteWhenFinish

LongEventHandlerMirror.ToExecuteWhenFinished = remainder;

BetterLoadingMain.LoadingScreen.StartCoroutine
BetterLoadingMain.LoadingScreen!.StartCoroutine
(
ToExecuteWhenFinishedHandler.ExecuteToExecuteWhenFinishedTheGoodVersion
(
Expand Down
8 changes: 7 additions & 1 deletion Source/Stage/InitialLoad/8StageRunStaticCctors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ public override void BecomeActive()

public static IEnumerator StaticConstructAll()
{
if(_toRun == null)
throw new InvalidOperationException("StaticConstructAll called before _toRun was set!");

if(_queue == null)
throw new InvalidOperationException("StaticConstructAll called before _queue was set!");

GlobalTimingData.TicksStartedCctors = DateTime.UtcNow.Ticks;
Log.Message("[BetterLoading] Starting Antifreeze(tm) StaticConstructorCaller. Synchronizing retransmission chronicity...");
Application.runInBackground = true;
Expand Down Expand Up @@ -164,7 +170,7 @@ public static bool PreCallAll()
// Log.Message("Static constructors? Oh, sit down, vanilla, I'll do it myself. Starting now, at " + DateTime.Now.ToLongTimeString(), true);
_toRun = GenTypes.AllTypesWithAttribute<StaticConstructorOnStartup>().ToList();

BetterLoadingMain.LoadingScreen.StartCoroutine(StaticConstructAll());
BetterLoadingMain.LoadingScreen!.StartCoroutine(StaticConstructAll());

// Log.Message("[BetterLoading] Overriding LongEventHandler's toExecuteWhenFinished", true);

Expand Down
2 changes: 1 addition & 1 deletion Source/Stage/InitialLoad/9StageRunPostFinalizeCallbacks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static bool PreExecToExecWhenFinished(List<Action> ___toExecuteWhenFinish
var initialNumTasksToRun = _numTasksToRun;
Log.Message($"[BetterLoading] Processing {initialNumTasksToRun} post-finalize tasks.");

BetterLoadingMain.LoadingScreen.StartCoroutine
BetterLoadingMain.LoadingScreen!.StartCoroutine
(
ToExecuteWhenFinishedHandler.ExecuteToExecuteWhenFinishedTheGoodVersion
(
Expand Down
6 changes: 3 additions & 3 deletions Source/Stage/SaveLoad/2LoadMaps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class LoadMaps : LoadingStage {
public static int NumMaps;

private static int _currMapNum = -1;
private static Map _currMap;
private static Map? _currMap;

private static bool _currMapInitialized;
private static bool _currMapLoadedComponents;
Expand All @@ -33,7 +33,7 @@ public static void CountMaps()
}
}

public LoadMaps([NotNull] Harmony instance) : base(instance)
public LoadMaps(Harmony instance) : base(instance)
{
}

Expand All @@ -42,7 +42,7 @@ public override string GetStageName()
return "Loading Maps";
}

public override string? GetCurrentStepName()
public override string GetCurrentStepName()
{
if (_currMapNum == -1)
return "Waiting...";
Expand Down
12 changes: 9 additions & 3 deletions Source/StageTimingData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ public class StageTimingData
{
internal static readonly List<StageTimingData> ExecutedStages = new();

public DateTime start;
public DateTime end;
public LoadingStage stage;
public readonly DateTime Start;
public DateTime End;
public readonly LoadingStage Stage;

public StageTimingData(DateTime start, LoadingStage stage)
{
Start = start;
Stage = stage;
}
}
}
14 changes: 7 additions & 7 deletions Source/TypeCacheKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ namespace BetterLoading
{
public struct TypeCacheKey : IEquatable<TypeCacheKey>
{
public string typeName;
public string namespaceIfAmbiguous;
public readonly string TypeName;
public readonly string? NamespaceIfAmbiguous;

public override int GetHashCode() => namespaceIfAmbiguous == null ? typeName.GetHashCode() : (17 * 31 + typeName.GetHashCode()) * 31 + namespaceIfAmbiguous.GetHashCode();
public override int GetHashCode() => NamespaceIfAmbiguous == null ? TypeName.GetHashCode() : (17 * 31 + TypeName.GetHashCode()) * 31 + NamespaceIfAmbiguous.GetHashCode();

public bool Equals(TypeCacheKey other) => string.Equals(typeName, other.typeName) && string.Equals(namespaceIfAmbiguous, other.namespaceIfAmbiguous);
public bool Equals(TypeCacheKey other) => string.Equals(TypeName, other.TypeName) && string.Equals(NamespaceIfAmbiguous, other.NamespaceIfAmbiguous);

public override bool Equals(object obj) => obj is TypeCacheKey other && Equals(other);

public TypeCacheKey(string typeName, string namespaceIfAmbigous = null)
public TypeCacheKey(string typeName, string? namespaceIfAmbiguous = null)
{
this.typeName = typeName;
namespaceIfAmbiguous = namespaceIfAmbigous;
TypeName = typeName;
NamespaceIfAmbiguous = namespaceIfAmbiguous;
}
}
}

0 comments on commit 22e87d0

Please sign in to comment.