Lots of new optimizations utilizing prepatcher

To start, this mod now requires Prepatcher and Fishery.
New optimizations include: gene ticking improvements, world object improvements, hediff optimizations, world pawn gc optimizations, better mothballing, dds support, ContentFinder caching, additions and changes to reflection caching, as well as various other additions. Multiplayer support has been improved too, but should be considered highly experimental. The WorkGiver_DoBill bug is fixed in this build. Multiple patches aiming to improve RimWorld's load time have been added too.
This commit is contained in:
bbradson
2023-06-03 20:24:38 +02:00
parent cb0a407425
commit ca9321add8
125 changed files with 11272 additions and 3681 deletions
@@ -0,0 +1,108 @@
// 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.ModCompatibility;
public static class ActiveMods
{
public static readonly bool
Fishery = Contains(PackageIDs.FISHERY),
// GradientHair = Contains(PackageIDs.GRADIENT_HAIR),
GraphicsSetter = Contains(PackageIDs.GRAPHICS_SETTINGS),
Harmony = Contains(PackageIDs.HARMONY),
Multiplayer = Contains(PackageIDs.MULTIPLAYER),
PerformanceOptimizer = Contains(PackageIDs.PERFORMANCE_OPTIMIZER),
Prepatcher = Contains(PackageIDs.PREPATCHER),
VanillaExpandedFramework = Contains(PackageIDs.VANILLA_EXPANDED_FRAMEWORK);
// ModsConfig.IsActive does not ignore _steam postfixes, causing it to fail when local copies were made
public static bool Contains(string packageID) => TryGetModMetaData(packageID) != null;
public static ModMetaData? TryGetModMetaData(string packageID)
=> ModLister.GetActiveModWithIdentifier(packageID, true);
public static ModContentPack? TryGetModContentPack(string packageID)
{
var allMods = LoadedModManager.RunningModsListForReading;
var count = allMods.Count;
for (var i = 0; i < count; i++)
{
if (allMods[i].PackageIdPlayerFacing.Equals(packageID, StringComparison.OrdinalIgnoreCase))
return allMods[i];
}
return null;
}
public static Assembly? TryGetAssembly(this ModContentPack modContentPack, string name)
{
var assemblies = modContentPack.assemblies.loadedAssemblies;
for (var i = 0; i < assemblies.Count; i++)
{
if (assemblies[i].GetName().Name.Equals(name, StringComparison.OrdinalIgnoreCase))
return assemblies[i];
}
return null;
}
public static void CheckRequirements()
{
if (!Fishery)
ThrowForMissingModDependency(nameof(Fishery));
if (!Harmony)
ThrowForMissingModDependency(nameof(Harmony));
VerifyLoadOrder(nameof(Fishery), PackageIDs.FISHERY, PackageIDs.PERFORMANCE_FISH, PackageIDs.BETTER_LOG);
VerifyLoadOrder(nameof(Harmony), PackageIDs.HARMONY, PackageIDs.PERFORMANCE_FISH);
CheckHarmonyVersion();
CheckFisheryVersion();
}
private static void VerifyLoadOrder(string dependencyName, string dependencyID, params string[] dependingModIDs)
{
var activeModsInLoadOrder = (List<ModMetaData>)ModsConfig.ActiveModsInLoadOrder;
if (activeModsInLoadOrder.FindIndex(mod => mod.PackageId == dependencyID)
> activeModsInLoadOrder.FindIndex(mod => dependingModIDs.Contains(mod.PackageId)))
{
ThrowForIncorrectLoadOrder(dependencyName);
}
}
private static void CheckFisheryVersion()
{
if (FisheryLib.FisheryLib.VERSION > FisheryLib.FisheryLib.CurrentlyLoadedVersion)
ThrowForOutdatedModDependency(nameof(Fishery));
}
private static void CheckHarmonyVersion()
=> CheckDependencyVersion(nameof(Harmony), typeof(Harmony), "0Harmony");
private static void CheckDependencyVersion(string dependencyName, Type dependencyType,
string dependencyAssemblyName)
{
if (typeof(PerformanceFishMod).Assembly.GetReferencedAssemblies().First(assembly
=> assembly.FullName.StartsWith($"{dependencyAssemblyName}, Version", StringComparison.Ordinal)).Version
> dependencyType.Assembly.GetName().Version)
{
ThrowForOutdatedModDependency(dependencyName);
}
}
private static void ThrowForIncorrectLoadOrder(string dependencyName)
=> ThrowHelper.ThrowOperationCanceledException($"Incorrect load order detected. {
dependencyName} must be loaded before any mod depending on it.");
private static void ThrowForOutdatedModDependency(string modName)
=> ThrowHelper.ThrowOperationCanceledException($"Outdated {modName} mod detected. {
modName} must be updated for Performance Fish to work correctly");
private static void ThrowForMissingModDependency(string modName)
=> ThrowHelper.ThrowOperationCanceledException(
$"{modName} mod is missing. Performance Fish requires this mod in order to function.");
}
@@ -0,0 +1,26 @@
// 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.ModCompatibility;
public static class GraphicsSetter
{
public static MethodBase? TargetMethod { get; }
= AccessTools.Method("GraphicSetter.Patches.TextureLoadingPatch+LoadPNGPatch:Prefix")
?? AccessTools.Method("GraphicSetter.GraphicsPatches+LoadPNGPatch:Prefix");
public static ModMetaData? Mod { get; } = ActiveMods.TryGetModMetaData(PackageIDs.GRAPHICS_SETTINGS);
public static MethodBase? TryPatch()
=> TargetMethod != null
? PerformanceFishMod.Harmony.Patch(TargetMethod, transpiler: new(methodof(Transpiler)))
: null;
public static CodeInstructions Transpiler(CodeInstructions codes)
{
yield return FishTranspiler.Constant(1);
yield return FishTranspiler.Return;
}
}
@@ -0,0 +1,21 @@
// 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.ModCompatibility;
public static class PackageIDs
{
public const string
BETTER_LOG = "bs.betterlog",
FISHERY = "bs.fishery",
// GRADIENT_HAIR = "automatic.gradienthair",
GRAPHICS_SETTINGS = "Telefonmast.GraphicsSettings",
HARMONY = "brrainz.harmony",
MULTIPLAYER = "rwmt.Multiplayer",
PERFORMANCE_FISH = "bs.performance",
PERFORMANCE_OPTIMIZER = "Taranchuk.PerformanceOptimizer",
PREPATCHER = "zetrith.prepatcher",
VANILLA_EXPANDED_FRAMEWORK = "OskarPotocki.VanillaFactionsExpanded.Core";
}
@@ -0,0 +1,64 @@
// 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.ModCompatibility;
public static class PerformanceOptimizer
{
private const string ASSEMBLY_NAME = "PerformanceOptimizer";
public static bool Active => ActiveMods.PerformanceOptimizer;
public static ModContentPack? ModContentPack { get; }
= ActiveMods.TryGetModContentPack(PackageIDs.PERFORMANCE_OPTIMIZER);
public static Type? OptimizationBaseType { get; }
public static Type[]? BlockedPatches { get; }
public static string[] BlockedPatchNames { get; }
=
{
"PerformanceOptimizer.Optimization_FasterGetCompReplacement",
"PerformanceOptimizer.Optimization_IdeoUtility_GetStyleDominance"
};
static PerformanceOptimizer()
{
if (ModContentPack is null)
return;
var assembly = ModContentPack.TryGetAssembly(ASSEMBLY_NAME);
if (assembly is null)
{
Log.Error("Performance Fish failed to find Performance Optimizer's assembly. "
+ "This is likely to cause issues.");
return;
}
OptimizationBaseType = assembly.GetType("PerformanceOptimizer.Optimization");
BlockedPatches = Array.ConvertAll(BlockedPatchNames, name => assembly.GetType(name));
}
public static bool TryPatch()
{
if (BlockedPatches.NullOrEmpty())
return false;
foreach (var patch in BlockedPatches.Concat(OptimizationBaseType))
{
if (AccessTools.DeclaredPropertyGetter(patch, "IsEnabled") is { } isEnabledGetter)
PerformanceFishMod.Harmony.Patch(isEnabledGetter, new(methodof(IsEnabledPatch)));
}
return true;
}
public static bool IsEnabledPatch(object __instance, ref bool __result)
=> !BlockedPatches!.Contains(__instance.GetType())
|| (__result = false);
}
@@ -0,0 +1,46 @@
// 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.ModCompatibility;
public class VanillaExpandedFixes : ClassWithFishPatches
{
public class ExpandableGraphicData : FishPatch
{
public override string? Description { get; }
= "Fixes the LoadAllFiles method in Vanilla Expanded Framework's ExpandableGraphicData to not scan through "
+ "all texture folders of all mods for every graphic and instead just use cached files the game already "
+ "loaded. Improves load times and prevents dds related errors.";
public override bool Enabled => ActiveMods.VanillaExpandedFramework;
public override MethodBase TargetMethodInfo { get; }
= AccessTools.Method("VFECore.ExpandableGraphicData:LoadAllFiles");
public static CodeInstructions Transpiler(CodeInstructions codes)
=> Reflection.MakeReplacementCall(LoadAllFiles);
public static List<string> LoadAllFiles(object __instance, string? folderPath)
{
var paths = new List<string>();
var mods = LoadedModManager.RunningModsListForReading;
var count = mods.Count;
for (var i = 0; i < count; i++)
{
var contentListTrie = mods[i].GetContentHolder<Texture2D>().contentListTrie;
var prefix = !folderPath.NullOrEmpty() && folderPath![^1] == '/'
? folderPath
: folderPath + '/';
foreach (var path in contentListTrie.GetByPrefix(prefix))
paths.Add(path);
}
return paths;
}
}
}