Added gas grid optimization, various despawn optimizations, new hauling optimizations utilizing events and regions, bugfixes for old patches and for vanilla functions too, as well as smaller additions. 1.3 files removed.

This commit is contained in:
bbradson
2024-01-27 10:24:24 +01:00
parent 0247ad59a1
commit c8b764de68
151 changed files with 7318 additions and 2145 deletions
@@ -15,5 +15,19 @@ public abstract class ClassWithFishPatches : SingletonFactory<ClassWithFishPatch
public virtual FishPatchHolder Patches { get; }
public virtual bool RequiresLoadedGameForPatching => false;
public virtual bool ShowSettings
{
get
{
foreach (var patch in Patches)
{
if (patch.ShowSettings)
return true;
}
return false;
}
}
protected ClassWithFishPatches() => Patches = new(GetType());
}
+58 -10
View File
@@ -8,6 +8,8 @@ using System.Diagnostics;
using System.Linq;
using nuget::JetBrains.Annotations;
using PerformanceFish.Cache;
using PerformanceFish.ModCompatibility;
using PerformanceFish.Prepatching;
namespace PerformanceFish.Patching;
@@ -15,11 +17,36 @@ namespace PerformanceFish.Patching;
[UsedImplicitly(ImplicitUseTargetFlags.WithInheritors | ImplicitUseTargetFlags.WithMembers)]
public abstract class FishPatch : SingletonFactory<FishPatch>, IExposable, IHasDescription
{
private bool _enabled;
private string? _descriptionWithNotes;
private FishPrepatchBase[]? _resolvedLinkedPrepatches;
private FishPatch[]? _resolvedLinkedHarmonyPatches;
public virtual List<string> IncompatibleModIDs { get; } = [];
public virtual List<Type> LinkedPatches { get; } = [];
private FishPrepatchBase[] ResolvedLinkedPrepatches
=> _resolvedLinkedPrepatches ??= LinkedPatches
.Where(static type => type.IsAssignableTo(typeof(FishPrepatchBase)))
.Select(static type => FishPrepatchBase.Get(type))
.ToArray();
private FishPatch[] ResolvedLinkedHarmonyPatches
=> _resolvedLinkedHarmonyPatches ??= LinkedPatches
.Where(static type => type.IsAssignableTo(typeof(FishPatch))).Select(static type => Get(type))
.ToArray();
public bool DisabledForModCompatibility => ActiveMods.ContainsAnyOf(IncompatibleModIDs);
public virtual bool ShouldBenchmark => false;
public virtual bool Enabled
{
get => _enabled;
get => _enabled && !DisabledForModCompatibility;
set
{
if (value == _enabled)
@@ -37,30 +64,45 @@ public abstract class FishPatch : SingletonFactory<FishPatch>, IExposable, IHasD
TryUnpatch();
ShouldBePatched = shouldBePatched;
}
for (var i = 0; i < ResolvedLinkedPrepatches.Length; i++)
ResolvedLinkedPrepatches[i].Enabled = value;
for (var i = 0; i < ResolvedLinkedHarmonyPatches.Length; i++)
ResolvedLinkedHarmonyPatches[i].Enabled = value;
}
}
private bool _enabled;
public virtual bool DefaultState
{
[Pure] get => true;
}
public virtual bool ShowSettings => true;
public virtual int PrefixMethodPriority => TryGetPriority(PrefixMethodInfo);
public virtual int PostfixMethodPriority => TryGetPriority(PostfixMethodInfo);
public virtual int TranspilerMethodPriority => TryGetPriority(TranspilerMethodInfo);
public virtual int FinalizerMethodPriority => TryGetPriority(FinalizerMethodInfo);
public MethodInfo? HarmonyMethodInfo => HarmonyMethodInfos.FirstOrDefault();
public List<MethodInfo> HarmonyMethodInfos { get; } = new();
public List<MethodInfo> HarmonyMethodInfos { get; } = [];
public MethodInfo? BenchmarkHarmonyMethodInfo => BenchmarkHarmonyMethodInfos.FirstOrDefault();
public List<MethodInfo> BenchmarkHarmonyMethodInfos { get; } = new();
public List<MethodInfo> BenchmarkHarmonyMethodInfos { get; } = [];
private bool Patched { get; set; }
private bool ShouldBePatched { get; set; }
public virtual string? Name => null;
public virtual string? Description => null;
public string? DescriptionWithNotes
=> _descriptionWithNotes ??= !DisabledForModCompatibility
? Description
: Description
+ $"{(Description?.EndsWith(".") ?? true ? "" : ".")} Disabled for compatibility with {
IncompatibleModIDs.Select(static id => ActiveMods.TryGetModMetaData(id)?.Name)
.Where(static id => id != null).ToCommaList(true)}";
public virtual Delegate? TargetMethodGroup => null;
public virtual IEnumerable<Delegate>? TargetMethodGroups => null;
public virtual Expression<Action>? TargetMethod => null;
@@ -76,8 +118,8 @@ public abstract class FishPatch : SingletonFactory<FishPatch>, IExposable, IHasD
??= (TargetMethods?.Select(SymbolExtensions.GetMethodInfo)
?? TargetMethodGroups?.Select(static m => m.Method))?.Cast<MethodBase>().ToArray()
?? (TargetMethodInfo != null!
? new[] { TargetMethodInfo }
: Array.Empty<MethodBase>());
? [TargetMethodInfo]
: Array.Empty<MethodBase>());
private MethodBase[]? _targetMethodInfos;
@@ -102,7 +144,9 @@ public abstract class FishPatch : SingletonFactory<FishPatch>, IExposable, IHasD
public virtual MethodInfo? FinalizerMethodInfo
=> TryGetMethod("FINALIZER", static m => m.HasAttribute<HarmonyFinalizer>());
internal Type ParentType => GetType().DeclaringType;
internal Type ParentType
=> GetType().DeclaringType
?? ThrowHelper.ThrowArgumentNullException<Type>($"{this} is missing parent class. This is not supported");
internal IHasFishPatch? ParentClass
=> PerformanceFishMod.AllPatchClasses?.FirstOrDefault(patchClass => patchClass.GetType() == ParentType);
@@ -249,10 +293,14 @@ public abstract class FishPatch : SingletonFactory<FishPatch>, IExposable, IHasD
private static int TryGetPriority(MethodInfo? info)
=> info?.TryGetAttribute<HarmonyPriority>()?.info.priority ?? Priority.Normal;
protected internal virtual void OnPatchingCompleted()
{
}
public virtual void ExposeData() => Scribe_Values.Look(ref _enabled, "enabled", DefaultState);
public override string ToString() => GetType().FullName;
public override string ToString() => GetType().FullName ?? GetType().Name;
}
internal static class Benchmarking
@@ -317,7 +365,7 @@ internal static class Benchmarking
}
}
public class Results
public sealed class Results
{
public int Count;
public Stopwatch? Stopwatch;
@@ -1,21 +0,0 @@
// 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/.
namespace PerformanceFish.Patching;
public static class FishPatchExtensions
{
public static void PatchAll(this IEnumerable<FishPatch> patches)
{
foreach (var patch in patches)
patch.TryPatch();
}
public static void UnpatchAll(this IEnumerable<FishPatch> patches)
{
foreach (var patch in patches)
patch.TryUnpatch();
}
}
@@ -13,12 +13,33 @@ namespace PerformanceFish.Patching;
[UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)]
public class FishPatchHolder : IExposable, IEnumerable<FishPatch>
{
private volatile int _completedPatchingOnce;
public ConcurrentDictionary<Type, FishPatch> All { get; } = new();
public T Get<T>() where T : FishPatch => (T)All[typeof(T)];
public void Add(FishPatch patch) => All[patch.GetType()] = patch;
public void PatchAll() => All.Values.PatchAll();
public void UnpatchAll() => All.Values.UnpatchAll();
public void PatchAll()
{
using var patches = new PooledArray<FishPatch>(All.Values);
for (var i = 0; i < patches.Length; i++)
patches[i].TryPatch();
if (Interlocked.Exchange(ref _completedPatchingOnce, 1) == 0)
{
for (var i = 0; i < patches.Length; i++)
patches[i].OnPatchingCompleted();
}
}
public void UnpatchAll()
{
foreach (var patch in All.Values)
patch.TryUnpatch();
}
public FishPatch this[Type type] => All[type];
public FishPatchHolder(Type type)
@@ -62,8 +83,8 @@ public class FishPatchHolder : IExposable, IEnumerable<FishPatch>
patchClass.Patches[type].Enabled = false;
patchClass.Patches.All.TryRemove(type, out _);
Log.Warning($"Performance Fish removed a duplicate patch from {patchClass.GetType().FullName
}. This is likely caused by no longer valid mod configs");
Log.Warning($"Performance Fish removed a duplicate patch from {
patchClass.GetType().FullName}. This is likely caused by no longer valid mod configs");
}
}
@@ -13,4 +13,5 @@ public interface IHasFishPatch
{
public FishPatchHolder Patches { get; }
public bool RequiresLoadedGameForPatching { get; }
public bool ShowSettings { get; }
}