diff --git a/1.4/Assemblies/PerformanceFish.dll b/1.4/Assemblies/PerformanceFish.dll index 5d28c31..173c3f4 100644 Binary files a/1.4/Assemblies/PerformanceFish.dll and b/1.4/Assemblies/PerformanceFish.dll differ diff --git a/1.4/Assemblies/PerformanceFish.pdb b/1.4/Assemblies/PerformanceFish.pdb index 0bab96e..28d623c 100644 Binary files a/1.4/Assemblies/PerformanceFish.pdb and b/1.4/Assemblies/PerformanceFish.pdb differ diff --git a/1.5/Assemblies/PerformanceFish.dll b/1.5/Assemblies/PerformanceFish.dll index e537328..894c895 100644 Binary files a/1.5/Assemblies/PerformanceFish.dll and b/1.5/Assemblies/PerformanceFish.dll differ diff --git a/1.5/Assemblies/PerformanceFish.pdb b/1.5/Assemblies/PerformanceFish.pdb index 68a6ca9..79c9c61 100644 Binary files a/1.5/Assemblies/PerformanceFish.pdb and b/1.5/Assemblies/PerformanceFish.pdb differ diff --git a/About/About.xml b/About/About.xml index 331c3af..4817d54 100644 --- a/About/About.xml +++ b/About/About.xml @@ -15,6 +15,7 @@
  • Ludeon.RimWorld.Royalty
  • Ludeon.RimWorld.Ideology
  • Ludeon.RimWorld.Biotech
  • +
  • Ludeon.RimWorld.Anomaly
  • UnlimitedHugs.HugsLib
  • OskarPotocki.VanillaFactionsExpanded.Core
  • Taranchuk.PerformanceOptimizer
  • diff --git a/Source/PerformanceFish/CellIndex.cs b/Source/PerformanceFish/CellIndex.cs index 8083c68..731b6c4 100644 --- a/Source/PerformanceFish/CellIndex.cs +++ b/Source/PerformanceFish/CellIndex.cs @@ -24,4 +24,6 @@ public record struct CellIndex(int Value) public bool IsFogged(Map map) => map.fogGrid.fogGrid[Value]; public float GetSnowDepth(Map map) => map.snowGrid.depthGrid[Value]; + + public Building? GetEdifice(Map map) => map.edificeGrid[Value]; } \ No newline at end of file diff --git a/Source/PerformanceFish/Directory.Build.Props b/Source/PerformanceFish/Directory.Build.Props index 133edb3..57d0265 100644 --- a/Source/PerformanceFish/Directory.Build.Props +++ b/Source/PerformanceFish/Directory.Build.Props @@ -4,7 +4,7 @@ PerformanceFish PerformanceFish bradson - 0.6.1.1 + 0.6.2.0 LICENSE net48 preview diff --git a/Source/PerformanceFish/Hauling/HaulDestinationManagerCache.cs b/Source/PerformanceFish/Hauling/HaulDestinationManagerCache.cs index 5f32c30..193ab1d 100644 --- a/Source/PerformanceFish/Hauling/HaulDestinationManagerCache.cs +++ b/Source/PerformanceFish/Hauling/HaulDestinationManagerCache.cs @@ -11,16 +11,15 @@ public record struct HaulDestinationManagerCache() public event Action? PriorityChanged; - public int[] GroupCountByPriority = new int[StoragePriorityCount]; + public readonly int[] GroupCountByPriority = new int[StoragePriorityCount]; [MethodImpl(MethodImplOptions.NoInlining)] public void OnPriorityChanged(HaulDestinationManager manager) { - var slotGroupsByPriority = manager.AllGroupsListInPriorityOrder; - - Array.Clear(GroupCountByPriority, 0, GroupCountByPriority.Length); - for (var i = slotGroupsByPriority.Count; i-- > 0;) - GroupCountByPriority[(int)slotGroupsByPriority[i].Settings.Priority]++; + GroupCountByPriority.Clear(); + manager.AllGroupsListInPriorityOrder.UnwrapArray(out var slotGroupsByPriority, out var slotGroupCount); + for (var i = slotGroupCount; --i >= 0;) + GroupCountByPriority[(int)slotGroupsByPriority.UnsafeLoad(i).Settings.Priority]++; PriorityChanged?.Invoke(manager); } diff --git a/Source/PerformanceFish/Hauling/HaulDestinationManagerPatches.cs b/Source/PerformanceFish/Hauling/HaulDestinationManagerPatches.cs index 2ec951e..c508f17 100644 --- a/Source/PerformanceFish/Hauling/HaulDestinationManagerPatches.cs +++ b/Source/PerformanceFish/Hauling/HaulDestinationManagerPatches.cs @@ -17,6 +17,8 @@ public sealed class HaulDestinationManagerPatches : ClassWithFishPrepatches = AccessTools.DeclaredMethod(typeof(HaulDestinationManager), nameof(HaulDestinationManager.Notify_HaulDestinationChangedPriority)); + public static void Prefix(HaulDestinationManager __instance) => __instance.RecalculateStorageGroupMemberCount(); + public static void Postfix(HaulDestinationManager __instance) => __instance.Cache().OnPriorityChanged(__instance); } @@ -29,6 +31,12 @@ public sealed class HaulDestinationManagerPatches : ClassWithFishPrepatches = AccessTools.DeclaredMethod(typeof(HaulDestinationManager), nameof(HaulDestinationManager.AddHaulDestination)); + public static void Prefix(HaulDestinationManager __instance, IHaulDestination haulDestination) + { + if (haulDestination is IStorageGroupMember { Group: { } group }) + group.SpawnedMemberCount()++; + } + public static void Postfix(HaulDestinationManager __instance, IHaulDestination haulDestination) { if (haulDestination is not ISlotGroupParent slotGroupParent) @@ -47,6 +55,12 @@ public sealed class HaulDestinationManagerPatches : ClassWithFishPrepatches = AccessTools.DeclaredMethod(typeof(HaulDestinationManager), nameof(HaulDestinationManager.RemoveHaulDestination)); + public static void Prefix(HaulDestinationManager __instance, IHaulDestination haulDestination) + { + if (haulDestination is IStorageGroupMember { Group: { } group }) + group.SpawnedMemberCount()--; + } + public static void Postfix(HaulDestinationManager __instance, IHaulDestination haulDestination) { if (haulDestination is not ISlotGroupParent slotGroupParent) @@ -76,8 +90,8 @@ public sealed class HaulDestinationManagerPatches : ClassWithFishPrepatches if (__result != 0) return __result; - var storageSettingsGroupA = a.TryGetStorageGroup(); - var storageSettingsGroupB = b.TryGetStorageGroup(); + var storageSettingsGroupA = a.StorageGroup; + var storageSettingsGroupB = b.StorageGroup; return storageSettingsGroupA == null ? storageSettingsGroupB == null @@ -91,7 +105,7 @@ public sealed class HaulDestinationManagerPatches : ClassWithFishPrepatches [MethodImpl(MethodImplOptions.NoInlining)] public static int CompareStorageSettingsGroups(StorageGroup a, StorageGroup b) { - var result = b.MemberCount.CompareTo(a.MemberCount); + var result = b.SpawnedMemberCount().CompareTo(a.SpawnedMemberCount()); return result != 0 ? result : a.loadID.CompareTo(b.loadID); } } diff --git a/Source/PerformanceFish/Hauling/StorageExtensions.cs b/Source/PerformanceFish/Hauling/StorageExtensions.cs index 337cb37..e176ac0 100644 --- a/Source/PerformanceFish/Hauling/StorageExtensions.cs +++ b/Source/PerformanceFish/Hauling/StorageExtensions.cs @@ -36,6 +36,21 @@ public static class StorageExtensions => slotGroup.parent is Building storage ? storage.TrueItemsPerCell() * cellCount : cellCount; + + public static void RecalculateStorageGroupMemberCount(this HaulDestinationManager haulDestinationManager) + { + haulDestinationManager.map.storageGroups.groups.UnwrapArray(out var storageGroups, out var storageGroupCount); + for (var i = storageGroupCount; --i >= 0;) + storageGroups.UnsafeLoad(i).SpawnedMemberCount() = 0; + + haulDestinationManager.AllGroupsListInPriorityOrder.UnwrapArray(out var slotGroupsByPriority, + out var slotGroupCount); + for (var i = slotGroupCount; --i >= 0;) + { + if (slotGroupsByPriority.UnsafeLoad(i).StorageGroup is { } storageGroup) + storageGroup.SpawnedMemberCount()++; + } + } static StorageExtensions() { diff --git a/Source/PerformanceFish/Hauling/StoreUtilityPrepatches.cs b/Source/PerformanceFish/Hauling/StoreUtilityPrepatches.cs index 33f5a84..717966d 100644 --- a/Source/PerformanceFish/Hauling/StoreUtilityPrepatches.cs +++ b/Source/PerformanceFish/Hauling/StoreUtilityPrepatches.cs @@ -254,7 +254,7 @@ public sealed class StoreUtilityPrepatches : ClassWithFishPrepatches #if STORAGE_GROUP_DEBUG if (!Debug.LoggedOnce && !(Current.Game?.tickManager?.Paused ?? true)) - Debug.LogStuff(listInPriorityOrder); + Debug.LogStuff(haulDestinationManager); #endif var foundPriority = currentPriority; @@ -287,10 +287,10 @@ public sealed class StoreUtilityPrepatches : ClassWithFishPrepatches var slotGroup = listInPriorityOrder[i++]; int otherGroupMembers; - var storageGroup = slotGroup.TryGetStorageGroup(); + var storageGroup = slotGroup.StorageGroup; if (storageGroup != null) { - otherGroupMembers = storageGroup.MemberCount - 1; + otherGroupMembers = storageGroup.SpawnedMemberCount() - 1; Debug.VerifyStorageGroup(otherGroupMembers, listInPriorityOrder, i, storageGroup); groupsOfPriorityCount -= otherGroupMembers; @@ -334,19 +334,18 @@ public sealed class StoreUtilityPrepatches : ClassWithFishPrepatches return true; FixCache: - if (hadToFixCache) + if (!hadToFixCache) + { + hadToFixCache = true; + UpdateCache(haulDestinationManager); + goto StartOfLoop; + } + else { LogErrorForFailedTryFindBestBetterStoreCellForAttempt(t, carrier, map, currentPriority, faction, closestSlot); goto Result; } - else - { - hadToFixCache = true; - } - - UpdateCache(haulDestinationManager); - goto StartOfLoop; } [MethodImpl(MethodImplOptions.NoInlining)] @@ -357,11 +356,29 @@ public sealed class StoreUtilityPrepatches : ClassWithFishPrepatches map}', currentPriority '{currentPriority}', faction '{ faction}'. The last found cell was '{ closestSlot}'. It is most likely incompatible with something in the mod list.\n{ - Debug.GetStorageGroupInfo(map?.haulDestinationManager?.AllGroupsListInPriorityOrder)}"); + Debug.GetStorageGroupInfo(map?.haulDestinationManager)}"); [MethodImpl(MethodImplOptions.NoInlining)] public static void UpdateCache(HaulDestinationManager haulDestinationManager) { + var storageGroups = haulDestinationManager.map.storageGroups.groups; + for (var i = storageGroups.Count; i-- > 0;) + { + var group = storageGroups[i]; + var members = group.members; + for (var j = members.Count; j-- > 0;) + { + var member = members[j]; + if (member.Group != group) + members.RemoveAt(j); + } + + if (members.Count == 0) + storageGroups.RemoveAt(i); + } + + haulDestinationManager.RecalculateStorageGroupMemberCount(); + haulDestinationManager.AllGroupsListInPriorityOrder.InsertionSort(HaulDestinationManager .CompareSlotGroupPrioritiesDescending); haulDestinationManager.Cache().OnPriorityChanged(haulDestinationManager); @@ -404,36 +421,36 @@ public sealed class StoreUtilityPrepatches : ClassWithFishPrepatches for (var b = otherGroupMembers; b-- > 0;) { var otherSlotGroup = listInPriorityOrder[i + b]; - if (otherSlotGroup.TryGetStorageGroup() == storageGroup) + if (otherSlotGroup.StorageGroup == storageGroup) continue; Log.Error($"Incorrect haul destination order! Storage '{ otherSlotGroup.parent}' was expected to have Storage group '{storageGroup}', but had '{ - otherSlotGroup.TryGetStorageGroup().ToStringSafe()}' instead"); + otherSlotGroup.StorageGroup.ToStringSafe()}' instead"); } } [Conditional("STORAGE_GROUP_DEBUG")] - public static void LogStuff(List listInPriorityOrder) + public static void LogStuff(HaulDestinationManager haulDestinationManager) { LoggedOnce = true; - Log.Message(GetStorageGroupInfo(listInPriorityOrder)); + Log.Message(GetStorageGroupInfo(haulDestinationManager)); } - public static string GetStorageGroupInfo(List? slotGroupsInPriorityOrder) + public static string GetStorageGroupInfo(HaulDestinationManager? haulDestinationManager) { + var slotGroupsInPriorityOrder = haulDestinationManager?.AllGroupsListInPriorityOrder; if (slotGroupsInPriorityOrder is null) return string.Empty; - var storageGroups = slotGroupsInPriorityOrder.Select(static slotGroup => slotGroup.TryGetStorageGroup()) - .Where(Is.NotNull).Distinct().ToList(); + var storageGroups = haulDestinationManager!.map.storageGroups.groups; return StringHelper.Resolve($"SlotGroup count: { slotGroupsInPriorityOrder.Count}, StorageGroup count: { storageGroups.Count}, slotGroups in storage groups: {storageGroups - .Select(static group => group!.MemberCount) + .Select(static group => group!.SpawnedMemberCount()) .Sum()}, outside of storage groups: {slotGroupsInPriorityOrder - .Select(static slotGroup => slotGroup.TryGetStorageGroup()) + .Select(static slotGroup => slotGroup.StorageGroup) .Where(Is.Null).Count()}"); } diff --git a/Source/PerformanceFish/PrepatcherFields.cs b/Source/PerformanceFish/PrepatcherFields.cs index 721a657..b00ffd4 100644 --- a/Source/PerformanceFish/PrepatcherFields.cs +++ b/Source/PerformanceFish/PrepatcherFields.cs @@ -74,6 +74,10 @@ public static class PrepatcherFields [MethodImpl(MethodImplOptions.AggressiveInlining)] public static extern ref StorageSettingsPatches.StorageSettingsCache Cache(this StorageSettings storageSettings); + [PrepatcherField] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static extern ref int SpawnedMemberCount(this StorageGroup storageGroup); + #if V1_4 [PrepatcherField] [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/Source/PerformanceFish/Rendering/DynamicDrawManagerPatches.cs b/Source/PerformanceFish/Rendering/DynamicDrawManagerPatches.cs index 2ed98a3..934292d 100644 --- a/Source/PerformanceFish/Rendering/DynamicDrawManagerPatches.cs +++ b/Source/PerformanceFish/Rendering/DynamicDrawManagerPatches.cs @@ -55,7 +55,7 @@ public sealed class DynamicDrawManagerPatches : ClassWithFishPrepatches } catch (Exception ex) { - Log.Error("Exception drawing dynamic things: " + ex); + Log.Error($"Exception drawing dynamic things for map '{instance.map}':\n{ex}"); } finally { @@ -92,7 +92,7 @@ public sealed class DynamicDrawManagerPatches : ClassWithFishPrepatches ThingsToDraw.Add(drawThing); if (!DebugViewSettings.singleThreadedDrawing) - drawThing.DynamicDrawPhase(DrawPhase.EnsureInitialized); + EnsureInitialized(drawThing); } else if (drawThing is Pawn pawn && checkShadows && shadowViewRect.Contains(drawThing.Position)) { @@ -102,6 +102,18 @@ public sealed class DynamicDrawManagerPatches : ClassWithFishPrepatches } } + public static void EnsureInitialized(Thing drawThing) + { + try + { + drawThing.DynamicDrawPhase(DrawPhase.EnsureInitialized); + } + catch (Exception ex) + { + Logging.EnsureInitializedException(drawThing, ex); + } + } + public static void DrawShadowsNow() { PawnShadowsToDraw.UnwrapArray(out var pawnShadowsToDraw, out var pawnShadowsToDrawCount); @@ -115,7 +127,7 @@ public sealed class DynamicDrawManagerPatches : ClassWithFishPrepatches } catch (Exception ex) { - Log.Error($"Exception drawing shadow for '{pawn}': {ex}"); + Logging.DrawShadowException(pawn, ex); } } } @@ -133,7 +145,7 @@ public sealed class DynamicDrawManagerPatches : ClassWithFishPrepatches } catch (Exception ex) { - Log.Error($"Exception drawing '{drawThing}': {ex}"); + Logging.DrawException(drawThing, ex); } } } @@ -181,7 +193,38 @@ public sealed class DynamicDrawManagerPatches : ClassWithFishPrepatches { public Thing[] thingsToDraw = Array.Empty(); - public void Execute(int index) => thingsToDraw[index].DynamicDrawPhase(DrawPhase.ParallelPreDraw); + public void Execute(int index) + { + var thing = thingsToDraw[index]; + try + { + thing.DynamicDrawPhase(DrawPhase.ParallelPreDraw); + } + catch (Exception ex) + { + Logging.ParallelPreDrawException(thing, ex); + } + } + } + + public static class Logging + { + [MethodImpl(MethodImplOptions.NoInlining)] + public static void EnsureInitializedException(Thing drawThing, Exception ex) + => Log.Error($"Exception in EnsureInitialized for '{drawThing}' at cell { + drawThing.PositionHeld}:\n{ex}"); + + [MethodImpl(MethodImplOptions.NoInlining)] + public static void DrawException(Thing drawThing, Exception ex) + => Log.Error($"Exception drawing '{drawThing}' at cell {drawThing.PositionHeld}:\n{ex}"); + + [MethodImpl(MethodImplOptions.NoInlining)] + public static void DrawShadowException(Pawn pawn, Exception ex) + => Log.Error($"Exception drawing shadow for '{pawn}' at cell {pawn.PositionHeld}:\n{ex}"); + + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ParallelPreDrawException(Thing thing, Exception ex) + => Log.Error($"Exception in ParallelPreDraw for '{thing}' at cell {thing.PositionHeld}:\n{ex}"); } } } diff --git a/Source/PerformanceFish/Rendering/PrintImprovements.cs b/Source/PerformanceFish/Rendering/PrintImprovements.cs index d77cfc5..9f9a262 100644 --- a/Source/PerformanceFish/Rendering/PrintImprovements.cs +++ b/Source/PerformanceFish/Rendering/PrintImprovements.cs @@ -13,6 +13,11 @@ namespace PerformanceFish.Rendering; public sealed class PrintImprovements : ClassWithFishPrepatches { + public const float + X_ADJUSTMENT = 0.0005f, + Y_ADJUSTMENT = 0.0045f, + COMBINED_ADJUSTMENT = X_ADJUSTMENT + Y_ADJUSTMENT; + public sealed class PrintPlanePatch : FishPrepatch { public override string? Description { get; } @@ -57,19 +62,19 @@ public sealed class PrintImprovements : ClassWithFishPrepatches [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float TopLeftAdjustment(in Vector2 size) - => (size.y * 0.0048f) + (size.x * 0.0002f); + => (size.y * Y_ADJUSTMENT) + (size.x * X_ADJUSTMENT); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float TopRightAdjustment(in Vector2 size) - => (size.y * 0.0048f) - (size.x * 0.0002f); + => (size.y * Y_ADJUSTMENT) - (size.x * X_ADJUSTMENT); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float BottomLeftAdjustment(in Vector2 size) - => (size.y * -0.0048f) + (size.x * 0.0002f); + => (size.y * -Y_ADJUSTMENT) + (size.x * X_ADJUSTMENT); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float BottomRightAdjustment(in Vector2 size) - => (size.y * -0.0048f) - (size.x * 0.0002f); + => (size.y * -Y_ADJUSTMENT) - (size.x * X_ADJUSTMENT); } public sealed class PlantPatch : FishPrepatch @@ -113,22 +118,22 @@ public sealed class PrintImprovements : ClassWithFishPrepatches public static Vector3 AdjustCenter(Vector3 center, Plant plant) { - if (Math.Abs(center.y - LowPlantAltitude) > 0.001f) + var plantAltitude = plant.def.altitudeLayer; + if (Math.Abs(center.y - plantAltitude.AltitudeFor()) >= COMBINED_ADJUSTMENT) return center; - var plantPosition = plant.PositionHeld; var map = plant.MapHeld; - if (map.edificeGrid[plantPosition.CellToIndex(map)] is + if (map.edificeGrid[plant.PositionHeld.CellToIndex(map)] is { } building + && building.def.altitudeLayer == plantAltitude) { - def.altitudeLayer: AltitudeLayer.LowPlant - } building) - { - center.y += ((plantPosition.z - building.OccupiedRect().minZ) * 0.01f) + 0.005f; + var buildingDrawPos = building.DrawPos; + var buildingSize = building.GetRotatedDrawSize(); + + center.y += ((buildingSize.y - (buildingDrawPos.z - center.z)) * Y_ADJUSTMENT) + + ((buildingSize.x - (buildingDrawPos.x - center.x)) * X_ADJUSTMENT); } return center; } - - public static readonly float LowPlantAltitude = AltitudeLayer.LowPlant.AltitudeFor(); } } \ No newline at end of file diff --git a/Source/PerformanceFish/System/ReflectionCaching.cs b/Source/PerformanceFish/System/ReflectionCaching.cs index d41989b..fe3665d 100644 --- a/Source/PerformanceFish/System/ReflectionCaching.cs +++ b/Source/PerformanceFish/System/ReflectionCaching.cs @@ -99,9 +99,9 @@ public sealed class ReflectionCaching : ClassWithFishPatches { public override string Description { get; } = "Caches GetField lookups"; - public override MethodBase TargetMethodInfo { get; } - = AccessTools.Method(typeof(RuntimeType), nameof(RuntimeType.GetField), - [typeof(string), typeof(BindingFlags)])!; + public override MethodBase? TargetMethodInfo { get; } + = AccessTools.DeclaredMethod(typeof(RuntimeType), nameof(RuntimeType.GetField), + [typeof(string), typeof(BindingFlags)]); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool Prefix(Type __instance, string name, BindingFlags bindingAttr, out FieldInfo? __result, @@ -145,8 +145,9 @@ public sealed class ReflectionCaching : ClassWithFishPatches { public override string Description { get; } = "Caches GetFields lookups"; - public override MethodBase TargetMethodInfo { get; } - = AccessTools.Method(typeof(RuntimeType), nameof(RuntimeType.GetFields), [typeof(BindingFlags)])!; + public override MethodBase? TargetMethodInfo { get; } + = AccessTools.DeclaredMethod(typeof(RuntimeType), nameof(RuntimeType.GetFields), + [typeof(BindingFlags)]); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool Prefix(Type __instance, BindingFlags bindingAttr, out FieldInfo[]? __result, @@ -186,9 +187,9 @@ public sealed class ReflectionCaching : ClassWithFishPatches { public override string Description { get; } = "Caches GetMethod lookups"; - public override Expression TargetMethod { get; } - // ReSharper disable once ReturnValueOfPureMethodIsNotUsed - = static () => default(Type)!.GetMethod(null!, default(BindingFlags)); + public override MethodBase? TargetMethodInfo { get; } + = AccessTools.DeclaredMethod(typeof(Type), nameof(Type.GetMethod), + [typeof(string), typeof(BindingFlags)]); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool Prefix(Type __instance, string name, BindingFlags bindingAttr, out MethodInfo? __result, @@ -223,9 +224,9 @@ public sealed class ReflectionCaching : ClassWithFishPatches { public override string Description { get; } = "Caches GetMethod lookups"; - public override Expression TargetMethod { get; } - // ReSharper disable once ReturnValueOfPureMethodIsNotUsed - = static () => default(Type)!.GetMethod(null!, null!); + public override MethodBase? TargetMethodInfo { get; } + = AccessTools.DeclaredMethod(typeof(Type), nameof(Type.GetMethod), + [typeof(string), typeof(Type[])]); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool Prefix(Type __instance, string name, Type[] types, out MethodInfo? __result, @@ -257,8 +258,11 @@ public sealed class ReflectionCaching : ClassWithFishPatches { public override string Description { get; } = "Caches GetMethod lookups"; - public override Expression TargetMethod { get; } - = static () => default(Type)!.GetMethod(null!, default, null, null!, null); + public override MethodBase? TargetMethodInfo { get; } + = AccessTools.DeclaredMethod(typeof(Type), nameof(Type.GetMethod), + [ + typeof(string), typeof(BindingFlags), typeof(Binder), typeof(Type[]), typeof(ParameterModifier[]) + ]); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool Prefix(Type __instance, string name, BindingFlags bindingAttr, Binder? binder, @@ -300,8 +304,9 @@ public sealed class ReflectionCaching : ClassWithFishPatches { public override string Description { get; } = "Caches GetConstructor lookups"; - public override Expression TargetMethod { get; } - = static () => default(Type)!.GetConstructor(default, null, null!, null); + public override MethodBase? TargetMethodInfo { get; } + = AccessTools.DeclaredMethod(typeof(Type), nameof(Type.GetConstructor), + [typeof(BindingFlags), typeof(Binder), typeof(Type[]), typeof(ParameterModifier[])]); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool Prefix(Type __instance, BindingFlags bindingAttr, Binder? binder, Type[] types, @@ -341,9 +346,9 @@ public sealed class ReflectionCaching : ClassWithFishPatches { public override string Description { get; } = "Caches GetProperty lookups"; - public override Expression TargetMethod { get; } - // ReSharper disable once ReturnValueOfPureMethodIsNotUsed - = static () => default(Type)!.GetProperty(null!, default(BindingFlags)); + public override MethodBase? TargetMethodInfo { get; } + = AccessTools.DeclaredMethod(typeof(Type), nameof(Type.GetProperty), + [typeof(string), typeof(BindingFlags)]); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool Prefix(Type __instance, string name, BindingFlags bindingAttr, @@ -384,7 +389,7 @@ public sealed class ReflectionCaching : ClassWithFishPatches // https://github.com/Unity-Technologies/mono/blob/unity-2019.4-mbe/mono/metadata/icall.c#L2737-L2754 public override MethodBase TargetMethodInfo { get; } - = AccessTools.PropertyGetter(typeof(RuntimeType), nameof(RuntimeType.FullName)); + = AccessTools.DeclaredPropertyGetter(typeof(RuntimeType), nameof(RuntimeType.FullName)); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool Prefix(RuntimeType __instance, ref string? __result, out bool __state) @@ -747,7 +752,7 @@ public sealed class ReflectionCaching : ClassWithFishPatches //} public override MethodBase TargetMethodInfo { get; } - = AccessTools.Method(typeof(MonoField), nameof(MonoField.GetValue))!; + = AccessTools.DeclaredMethod(typeof(MonoField), nameof(MonoField.GetValue))!; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool Prefix(FieldInfo __instance, object? obj, ref object? __result) @@ -771,8 +776,9 @@ public sealed class ReflectionCaching : ClassWithFishPatches public override string Description { get; } = "Optimizes the SetValue method by invoking it through specialized cached delegates"; - public override Expression TargetMethod { get; } - = static () => default(FieldInfo)!.SetValue(null, null); + public override MethodBase? TargetMethodInfo { get; } + = AccessTools.DeclaredMethod(typeof(FieldInfo), nameof(FieldInfo.SetValue), + [typeof(object), typeof(object)]); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool Prefix(FieldInfo __instance, object? obj, object? value) @@ -924,8 +930,9 @@ public sealed class ReflectionCaching : ClassWithFishPatches = "Optimizes the Invoke method by compiling specialized cached delegates. Disabled by default due to " + "issues with various mods, like RIMMSqol and Performance Optimizer"; - public override Expression TargetMethod { get; } - = static () => default(MethodBase)!.Invoke(null, null); + public override MethodBase? TargetMethodInfo { get; } + = AccessTools.DeclaredMethod(typeof(MethodBase), nameof(MethodBase.Invoke), + [typeof(object), typeof(object[])]); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool Prefix(MethodBase __instance, object? obj, object[]? parameters, ref object? __result) diff --git a/Source/PerformanceFish/Utility/MiscExtensions.cs b/Source/PerformanceFish/Utility/MiscExtensions.cs index 40c756d..1a8add6 100644 --- a/Source/PerformanceFish/Utility/MiscExtensions.cs +++ b/Source/PerformanceFish/Utility/MiscExtensions.cs @@ -32,8 +32,6 @@ public static class MiscExtensions public static StorageSettings? TryGetGroupStoreSettings(this ISlotGroupParent slotGroupParent) => slotGroupParent.TryGetStorageGroup()?.GetStoreSettings(); - public static StorageGroup? TryGetStorageGroup(this SlotGroup slotGroup) => slotGroup.parent.TryGetStorageGroup(); - public static StorageGroup? TryGetStorageGroup(this ISlotGroupParent slotGroupParent) => slotGroupParent is IStorageGroupMember groupMember ? groupMember.Group : null; @@ -82,6 +80,9 @@ public static class MiscExtensions return grid.regionGrid[c.CellToIndex(map)] is { valid: true } region ? region : null; } + public static Vector2 GetRotatedDrawSize(this Thing thing) + => !thing.Graphic.ShouldDrawRotated && thing.Rotation.IsHorizontal ? thing.DrawSize.Rotated() : thing.DrawSize; + public static bool ContainsThingStackableWith(this List thingsOfSingleDef, Thing t) { var def = t.def;