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
@@ -0,0 +1,59 @@
// 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 PerformanceFish.Prepatching;
namespace PerformanceFish.Events;
public sealed class MapEvents : ClassWithFishPrepatches
{
public sealed class Instanced
{
public event Action<Map>?
ComponentsConstructed,
ComponentsLoaded;
internal void OnComponentsConstructed(Map map) => ComponentsConstructed?.Invoke(map);
internal void OnComponentsLoaded(Map map) => ComponentsLoaded?.Invoke(map);
}
public sealed class MapConstructComponentsPatch : FishPrepatch
{
public override string? Description { get; }
= "Hook to raise an event when map components finish constructing. Does nothing by itself, but certain "
+ "functions require this.";
public override bool Enabled => true;
public override bool ShowSettings => false;
public override MethodBase TargetMethodBase { get; }
= AccessTools.DeclaredMethod(typeof(Map), nameof(Map.ConstructComponents));
public static void Postfix(Map __instance) => __instance.Events().OnComponentsConstructed(__instance);
}
public sealed class MapExposeComponentsPatch : FishPrepatch
{
public override string? Description { get; }
= "Maps apparently construct all components twice when loading a save. Once in ConstructComponents and "
+ "afterwards another time here. This is the companion patch to MapConstructCompanions, raising the "
+ "ComponentsExposed event.";
public override bool Enabled => true;
public override bool ShowSettings => false;
public override MethodBase TargetMethodBase { get; }
= AccessTools.DeclaredMethod(typeof(Map), nameof(Map.ExposeComponents));
public static void Postfix(Map __instance)
{
if (Scribe.mode == LoadSaveMode.LoadingVars)
__instance.Events().OnComponentsLoaded(__instance);
}
}
}
@@ -0,0 +1,15 @@
// 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.Events;
public static class StaticEvents
{
public static event Action CacheCleared
{
add => Cache.Utility.Cleared += value;
remove => Cache.Utility.Cleared -= value;
}
}
@@ -0,0 +1,167 @@
// 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 PerformanceFish.Prepatching;
namespace PerformanceFish.Events;
public sealed class ThingEvents : ClassWithFishPrepatches
{
public static event Action<Thing>?
Initialized,
Destroyed;
public sealed class Instanced
{
public event ThingGridEvents.EventHandler?
RegisteredAtThingGrid,
DeregisteredAtThingGrid;
public event Action<Thing, Map>? Spawned;
public event Action<Thing>?
DeSpawning,
DeSpawned,
Destroying,
Destroyed;
internal void OnSpawned(Thing thing, Map map) => Spawned?.Invoke(thing, map);
internal void OnDeSpawning(Thing thing) => DeSpawning?.Invoke(thing);
internal void OnDeSpawned(Thing thing) => DeSpawned?.Invoke(thing);
internal void OnDestroying(Thing thing) => Destroying?.Invoke(thing);
internal void OnDestroyed(Thing thing) => Destroyed?.Invoke(thing);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void OnRegisteredAtThingGrid(Thing thing, Map map, in IntVec3 cell)
=> RegisteredAtThingGrid?.Invoke(thing, map, cell);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void OnDeregisteredAtThingGrid(Thing thing, Map map, in IntVec3 cell)
=> DeregisteredAtThingGrid?.Invoke(thing, map, cell);
}
public sealed class SpawnSetupPatch : FishPrepatch
{
public override string? Description { get; }
= "Hook to invoke an event whenever things spawn. Does nothing by itself, but certain functions require "
+ "this.";
public override bool Enabled => true;
public override bool ShowSettings => false;
public override MethodBase TargetMethodBase { get; }
= AccessTools.DeclaredMethod(typeof(Thing), nameof(Thing.SpawnSetup));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Postfix(Thing __instance, Map map) => __instance.Events().OnSpawned(__instance, map);
}
public sealed class DeSpawnPatch : FishPrepatch
{
public override string? Description { get; }
= "Hook to invoke an event whenever things despawn. Does nothing by itself, but certain functions require "
+ "this.";
public override bool Enabled => true;
public override bool ShowSettings => false;
public override MethodBase TargetMethodBase { get; }
= AccessTools.DeclaredMethod(typeof(Thing), nameof(Thing.DeSpawn));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Prefix(Thing __instance) => __instance.Events().OnDeSpawning(__instance);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Postfix(Thing __instance) => __instance.Events().OnDeSpawned(__instance);
}
public sealed class DestroyPatch : FishPrepatch
{
public override string? Description { get; }
= "Hook to invoke an event whenever things get destroyed. Does nothing by itself, but certain functions "
+ "require this.";
public override bool Enabled => true;
public override bool ShowSettings => false;
public override MethodBase TargetMethodBase { get; }
= AccessTools.DeclaredMethod(typeof(Thing), nameof(Thing.Destroy));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Prefix(Thing __instance) => __instance.Events().OnDestroying(__instance);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Postfix(Thing __instance)
{
__instance.Events().OnDestroyed(__instance);
Destroyed?.Invoke(__instance);
}
}
public sealed class ExposeDataInitializePatch : FishPrepatch
{
public override string? Description { get; }
= "Hook to invoke an event when things finish loading from a save file. Does nothing by itself, but "
+ "certain functions require this.";
public override bool Enabled => true;
public override bool ShowSettings => false;
public override MethodBase TargetMethodBase { get; }
= AccessTools.DeclaredMethod(typeof(Thing), nameof(Thing.ExposeData));
public static void Postfix(Thing __instance)
{
if (Scribe.mode == LoadSaveMode.LoadingVars && __instance is not ThingWithComps)
OnThingInitialized(__instance);
}
}
public sealed class PostMakePatch : FishPrepatch
{
public override string? Description { get; }
= "Hook to invoke an event when things get created. Does nothing by itself, but certain functions require "
+ "this.";
public override bool Enabled => true;
public override bool ShowSettings => false;
public override MethodBase TargetMethodBase { get; }
= AccessTools.DeclaredMethod(typeof(Thing), nameof(Thing.PostMake));
public static void Postfix(Thing __instance)
{
if (__instance is not ThingWithComps)
OnThingInitialized(__instance);
}
}
public sealed class InitializeCompsPatch : FishPrepatch
{
public override string? Description { get; }
= "Hook to invoke an event when things with comps get initialized. Does nothing by itself, but certain "
+ "functions require this.";
public override bool Enabled => true;
public override bool ShowSettings => false;
public override MethodBase TargetMethodBase { get; }
= AccessTools.DeclaredMethod(typeof(ThingWithComps), nameof(ThingWithComps.InitializeComps));
public static void Postfix(ThingWithComps __instance) => OnThingInitialized(__instance);
}
private static void OnThingInitialized(Thing thing) => Initialized?.Invoke(thing);
}
@@ -0,0 +1,74 @@
// 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 Mono.Cecil;
using Mono.Cecil.Cil;
using PerformanceFish.Prepatching;
namespace PerformanceFish.Events;
public sealed class ThingGridEvents : ClassWithFishPrepatches
{
/// <summary>
/// Invoked for every thing.Position change, including for motes and pawns
/// </summary>
public sealed class RegisterInCellPatch : FishPrepatch
{
public override string? Description { get; }
= "Hook to invoke an event whenever things register in a cell. Does nothing by itself, but certain "
+ "functions require this.";
public override bool Enabled => true;
public override bool ShowSettings => false;
public override MethodBase TargetMethodBase { get; }
= AccessTools.DeclaredMethod(typeof(ThingGrid), nameof(ThingGrid.RegisterInCell));
public static void OnRegistered(ThingGrid __instance, Thing t, in IntVec3 c)
=> t.Events().OnRegisteredAtThingGrid(t, __instance.map, c);
public override void Transpiler(ILProcessor ilProcessor, ModuleDefinition module)
=> ilProcessor.InsertRange(ilProcessor.instructions.FirstIndexOf(static code
=> code.OpCode == OpCodes.Callvirt
&& code.Operand is MethodReference { Name: "Add" } method
&& method.DeclaringType.Name.Contains("List"))
+ 1,
OpCodes.Ldarg_0,
OpCodes.Ldarg_1,
(OpCodes.Ldarga_S, ilProcessor.Body.GetParameter(2)),
(OpCodes.Call, methodof(OnRegistered)));
}
public sealed class DeregisterInCellPatch : FishPrepatch
{
public override string? Description { get; }
= "Hook to invoke an event whenever things deregister from a cell. Does nothing by itself, but certain "
+ "functions require this.";
public override bool Enabled => true;
public override bool ShowSettings => false;
public override MethodBase TargetMethodBase { get; }
= AccessTools.DeclaredMethod(typeof(ThingGrid), nameof(ThingGrid.DeregisterInCell));
public static void OnDeregistered(ThingGrid __instance, Thing t, in IntVec3 c)
=> t.Events().OnDeregisteredAtThingGrid(t, __instance.map, c);
public override void Transpiler(ILProcessor ilProcessor, ModuleDefinition module)
=> ilProcessor.InsertRange(ilProcessor.instructions.FirstIndexOf(static code
=> code.OpCode == OpCodes.Callvirt
&& code.Operand is MethodReference { Name: "Remove" } method
&& method.DeclaringType.Name.Contains("List"))
+ 1,
OpCodes.Ldarg_0,
OpCodes.Ldarg_1,
(OpCodes.Ldarga, ilProcessor.Body.GetParameter(2)),
(OpCodes.Call, methodof(OnDeregistered)));
}
public delegate void EventHandler(Thing thing, Map map, in IntVec3 cell);
}