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
@@ -11,9 +11,9 @@ using RimWorld.Planet;
namespace PerformanceFish.Planet;
public class WorldObjectsOptimization : ClassWithFishPrepatches
public sealed class WorldObjectsOptimization : ClassWithFishPrepatches
{
public class WorldObjectsHolderTickPatch : FishPrepatch
public sealed class WorldObjectsHolderTickPatch : FishPrepatch
{
public override string? Description { get; }
= "The world objects holder is responsible for ticking every world object. This includes settlements, "
@@ -87,24 +87,46 @@ public class WorldObjectsOptimization : ClassWithFishPrepatches
CachedWorldObjectsVersion = -2,
CachedMapsVersion = -2;
private static Type[]
_whitelistedTickingCompTypes
=
{
typeof(WorldObjectComp),
typeof(FormCaravanComp),
typeof(TimedDetectionRaids),
typeof(EnterCooldownComp)
},
_whitelistedWorldObjectTypes = { typeof(WorldObject), typeof(MapParent), typeof(Settlement) };
private static Type?[]
_whitelistedTickingCompTypes =
[
typeof(WorldObjectComp),
typeof(FormCaravanComp),
typeof(TimedDetectionRaids),
typeof(EnterCooldownComp)
],
_whitelistedWorldObjectTypes =
[
typeof(WorldObject),
typeof(MapParent),
typeof(Settlement),
ModCompatibility.Types.RealRuins.POIWorldObject
];
public static void AddCompToWhiteList(Type compType)
{
_whitelistedTickingCompTypes = _whitelistedTickingCompTypes.Add(compType);
SkippableComps = InitializeSkippableComps();
}
public static void AddWorldObjectToWhiteList(Type worldObjectType)
{
_whitelistedWorldObjectTypes = _whitelistedWorldObjectTypes.Add(worldObjectType);
SkippableWorldObjects = InitializeSkippableWorldObjects();
}
public static HashSet<Type>
SkippableComps = MakeSubclassHashSet(typeof(WorldObjectComp), nameof(WorldObjectComp.CompTick),
_whitelistedTickingCompTypes),
SkippableWorldObjects = MakeSubclassHashSet(typeof(WorldObject), nameof(WorldObject.Tick),
_whitelistedWorldObjectTypes);
SkippableComps = InitializeSkippableComps(),
SkippableWorldObjects = InitializeSkippableWorldObjects();
private static HashSet<Type> MakeSubclassHashSet(Type type, string name, Type[] allowedDeclaringTypes)
private static HashSet<Type> InitializeSkippableComps()
=> MakeSubclassHashSet(typeof(WorldObjectComp), nameof(WorldObjectComp.CompTick),
_whitelistedTickingCompTypes);
private static HashSet<Type> InitializeSkippableWorldObjects()
=> MakeSubclassHashSet(typeof(WorldObject), nameof(WorldObject.Tick), _whitelistedWorldObjectTypes);
private static HashSet<Type> MakeSubclassHashSet(Type type, string name, Type?[] allowedDeclaringTypes)
=> type.SubclassesWithNoMethodOverrideAndSelf(allowedDeclaringTypes, name).ToHashSet();
public static bool CacheDirty(WorldObjectsHolder instance)
@@ -115,4 +137,112 @@ public class WorldObjectsOptimization : ClassWithFishPrepatches
static WorldObjectsHolderTickPatch() => Cache.Utility.Cleared += SetDirty;
}
public sealed class ExpandingIconCaching : FishPrepatch
{
public override string? Description { get; }
= "Caches icons that get displayed for world objects like settlements on the planet view and adds various "
+ "safety checks to better catch and log errors in case of missing icons";
public override MethodBase TargetMethodBase { get; }
= AccessTools.DeclaredPropertyGetter(typeof(WorldObject), nameof(WorldObject.ExpandingIcon));
public override void Transpiler(ILProcessor ilProcessor, ModuleDefinition module)
=> ilProcessor.ReplaceBodyWith(ReplacementBody);
public static Texture2D ReplacementBody(WorldObject __instance)
=> __instance.ExpandingIconCache() ?? InitializeExpandingIcon(__instance);
[MethodImpl(MethodImplOptions.NoInlining)]
public static Texture2D InitializeExpandingIcon(WorldObject? worldObject)
{
if (worldObject is null)
return BaseContent.BadTex;
var def = worldObject.def;
ref var cache = ref worldObject.ExpandingIconCache();
try
{
cache = def?.ExpandingIconTexture;
if (cache != null)
return cache;
var material = worldObject.Material;
if (material != null)
cache = material.mainTexture as Texture2D;
if (cache == null)
{
cache = BaseContent.BadTex;
Log.Error($"No expanding icon found for '{worldObject.ToStringSafe()}' of def '{
def?.ToStringSafe()}' from mod '{def?.modContentPack?.Name}'. Assigning default.");
}
return cache;
}
catch (Exception ex)
{
Log.Error($"Exception while initializing icon for '{worldObject.ToStringSafe()}' of def '{
def?.ToStringSafe()}' from mod '{def?.modContentPack?.Name}':\n{ex}");
return cache = BaseContent.BadTex;
}
}
}
public sealed class ExpandingIconColorCaching : FishPrepatch
{
public override string? Description { get; }
= "Caches colors of icons that get displayed for world objects like settlements on the planet view and "
+ "adds various safety checks to better catch and log errors in case of missing data";
public override MethodBase TargetMethodBase { get; }
= AccessTools.DeclaredPropertyGetter(typeof(WorldObject), nameof(WorldObject.ExpandingIconColor));
public override void Transpiler(ILProcessor ilProcessor, ModuleDefinition module)
=> ilProcessor.ReplaceBodyWith(ReplacementBody);
public static Color ReplacementBody(WorldObject __instance)
=> __instance.ExpandingIconColorCache() ?? InitializeExpandingIconColor(__instance);
[MethodImpl(MethodImplOptions.NoInlining)]
public static Color InitializeExpandingIconColor(WorldObject? worldObject)
{
if (worldObject is null)
return Color.white;
var def = worldObject.def;
ref var cache = ref worldObject.ExpandingIconColorCache();
try
{
cache = def?.expandingIconColor;
if (cache != null)
return cache.GetValueOrDefault();
var material = worldObject.Material;
if (material != null)
cache = material.color;
if (cache == null)
{
cache = Color.white;
Log.Error($"No expanding icon color found for '{worldObject.ToStringSafe()}' of def '{
def?.ToStringSafe()}' from mod '{def?.modContentPack?.Name}'. Assigning default.");
}
return cache.GetValueOrDefault();
}
catch (Exception ex)
{
Log.Error($"Exception while initializing icon color for '{worldObject.ToStringSafe()}' of def '{
def?.ToStringSafe()}' from mod '{def?.modContentPack?.Name}':\n{ex}");
return (cache = Color.white).GetValueOrDefault();
}
}
}
}