Files
Performance-Fish/Source/PerformanceFish/OnAssembliesLoaded.cs
T
bbradson be3dae46bb Added stat caching, fixed all the bugs
Added RitualObligationTriggers analyzer category,
Fixed defs from other mods with broken market value breaking fish's storage patches,
Fixed storage caches not updating for quality, rotting and similar changes to stored things,
Fixed storage caches not updating capacity when reinstalling storage,
Fixed prison labor compatibility,
Improved multiplayer compatibility,
Fixed ideology style dominance and altar sharing patches throwing errors instead of defaulting to disabled when not having ideology,
Fixed compatibility with RIMMSqol's world pawn GC patch by disabling that patch,
Fixed rare threading issues throwing errors on startup
2024-03-10 20:05:24 +01:00

116 lines
2.9 KiB
C#

// Copyright (c) 2023 bradson
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
using System.Threading.Tasks;
using PerformanceFish.ModCompatibility;
using PerformanceFish.Prepatching;
using PerformanceFish.System;
namespace PerformanceFish;
/// <summary>
/// Gets called before CreateModClasses(), after rimworld finishes loading assemblies
/// </summary>
public static class OnAssembliesLoaded
{
public static bool Loaded { get; private set; }
public static void Start()
{
Loaded = true;
TryPatchGraphicsSetter();
TryPatchPerformanceOptimizer();
TryPatchRIMMSqol();
TryInitialize(EqualityComparerOptimization.Optimization.Initialize);
PerformanceFishMod.AllPatchClasses = PerformanceFishMod.InitializeAllPatchClasses<IHasFishPatch>();
PerformanceFishMod.AllPrepatchClasses ??= PerformanceFishMod.InitializeAllPatchClasses<ClassWithFishPrepatches>();
TryInitialize(AnalyzerFixes.Patch, true);
_ = FishSettings.Instance;
PerformanceFishMod.AllPrepatchClasses.BeforeHarmonyPatching();
Parallel.ForEach(PerformanceFishMod.AllPatchClasses, static patchClass =>
{
try
{
if (!patchClass.RequiresLoadedGameForPatching)
patchClass.Patches.PatchAll();
}
catch (Exception ex)
{
Log.Error($"{PerformanceFishMod.NAME} encountered an exception while trying to initialize {
patchClass.GetType().Name}:\n{ex}");
}
});
PerformanceFishMod.AllPrepatchClasses.OnPatchingCompleted();
Cache.Utility.Initialize();
}
private static void TryPatchGraphicsSetter()
{
if (ActiveMods.GraphicsSetter && GraphicsSetter.TryPatch() is null)
{
Log.Error($"Failed to apply compatibility patch for {
GraphicsSetter.Mod?.Name ?? "Graphics Settings+"}");
}
}
private static void TryPatchPerformanceOptimizer()
{
if (ActiveMods.PerformanceOptimizer && !PerformanceOptimizer.TryPatch())
{
Log.Error($"Failed to apply compatibility patch for {
PerformanceOptimizer.ModContentPack?.Name ?? "Performance Optimizer"}");
}
}
// ReSharper disable once InconsistentNaming
private static void TryPatchRIMMSqol()
{
if (!ActiveMods.RIMMSqol)
return;
var success = false;
Exception? exception = null;
try
{
success = RIMMSqol.TryPatch();
}
catch (Exception ex)
{
exception = ex;
}
if (!success)
{
Log.Error($"Failed to apply compatibility patch for {
RIMMSqol.ModContentPack?.Name ?? "RIMMSqol"}.".AppendNewLineWhenNotNull(exception));
}
}
private static async void TryInitialize(Action action, bool async = false)
{
try
{
if (async)
await Task.Run(action).ConfigureAwait(false);
else
action();
}
catch (Exception ex)
{
Log.Error($"{PerformanceFishMod.NAME} encountered an exception while trying to initialize {
action.Method.FullDescription()}:\n{ex}");
}
}
}