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:
@@ -0,0 +1,38 @@
|
||||
// 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.Utility;
|
||||
|
||||
public static class CellIndexExtensions
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int CellToIndex(this in IntVec3 cell, Map map) => cell.CellToIndex(map.cellIndices);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int CellToIndex(this in IntVec3 cell, CellIndices cellIndices)
|
||||
=> cell.CellToIndex(cellIndices.mapSizeX);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int CellToIndex(this in IntVec3 cell, int mapSizeX) => (cell.z * mapSizeX) + cell.x;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int CellToIndex(this IntVec2 cell, Map map) => cell.CellToIndex(map.cellIndices);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int CellToIndex(this IntVec2 cell, CellIndices cellIndices)
|
||||
=> cell.CellToIndex(cellIndices.mapSizeX);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int CellToIndex(this IntVec2 cell, int mapSizeX) => (cell.z * mapSizeX) + cell.x;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int CellToIndex(int x, int z, Map map) => CellToIndex(x, z, map.cellIndices);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int CellToIndex(int x, int z, CellIndices cellIndices) => CellToIndex(x, z, cellIndices.mapSizeX);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int CellToIndex(int x, int z, int mapSizeX) => (z * mapSizeX) + x;
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using Mono.Collections.Generic;
|
||||
|
||||
namespace PerformanceFish.Utility;
|
||||
@@ -295,6 +296,8 @@ public static class CollectionExtensions
|
||||
|
||||
return ThrowForNoElements<T>();
|
||||
}
|
||||
|
||||
public static List<T> AsOrToList<T>(this IEnumerable<T> enumerable) => enumerable as List<T> ?? enumerable.ToList();
|
||||
|
||||
[DoesNotReturn]
|
||||
private static T ThrowForNoElements<T>()
|
||||
|
||||
@@ -9,10 +9,9 @@ namespace PerformanceFish.Utility;
|
||||
/// Scribe_Deep normally creates a new instance when loading data. Scribe on this class prevents that. Also includes a
|
||||
/// try-catch block
|
||||
/// </summary>
|
||||
public class Exposable : IExposable
|
||||
public sealed class Exposable(IExposable @this) : IExposable
|
||||
{
|
||||
public readonly IExposable Parent;
|
||||
public Exposable(IExposable @this) => Parent = @this;
|
||||
public readonly IExposable Parent = @this;
|
||||
|
||||
public void ExposeData()
|
||||
{
|
||||
@@ -27,7 +26,7 @@ public class Exposable : IExposable
|
||||
}
|
||||
|
||||
public override string ToString() => TrimName(Parent.ToString());
|
||||
public override bool Equals(object obj) => obj.Equals(Parent);
|
||||
public override bool Equals(object? obj) => Parent.Equals(obj);
|
||||
public override int GetHashCode() => Parent.GetHashCode();
|
||||
|
||||
public static void Scribe(IExposable iExposable, string label, bool trimLabel = true)
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
// 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.Utility;
|
||||
|
||||
public static class GUIHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Unity calls OnGUI with EventType.Layout at the beginning and EventType.Repaint at the end of every frame.
|
||||
/// Anything drawn within the Layout call in turn gets covered every time, making it a waste of CPU cycles.
|
||||
/// </summary>
|
||||
public static bool CanSkip
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => Event.current.type == EventType.Layout;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
// 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 System.Security;
|
||||
using Verse.Sound;
|
||||
|
||||
namespace PerformanceFish.Utility;
|
||||
|
||||
public static class MiscExtensions
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[SecuritySafeCritical]
|
||||
public static bool AsBool(this int value) // ref on parameter instead of var would enforce stack allocation
|
||||
{
|
||||
var temp = (byte)value; // bool is 32 bits in .NET, but the jit compiler only eliminates when casting to byte first
|
||||
return Unsafe.As<byte, bool>(ref temp); // result is a direct return with no instructions whatsoever
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[SecuritySafeCritical]
|
||||
public static int AsInt(this bool value)
|
||||
{
|
||||
var temp = value;
|
||||
return Unsafe.As<bool, byte>(ref temp);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool IsItem(this Thing thing) => thing.def.category == ThingCategory.Item;
|
||||
|
||||
public static ISlotGroupParent? GetSlotGroupParent(this in IntVec3 position, Map map)
|
||||
=> map.haulDestinationManager.SlotGroupAt(position)?.parent;
|
||||
|
||||
public static ISlotGroupParent? StoringSlotGroupParent(this Thing thing)
|
||||
=> thing.Position.GetSlotGroupParent(thing.GetMap());
|
||||
|
||||
public static int SlotGroupCellCount(this Thing thing) => ((ISlotGroupParent)thing).AllSlotCellsList().Count;
|
||||
|
||||
public static List<Thing> GetThingListUnchecked(this in IntVec3 cell, Map map)
|
||||
=> map.thingGrid.ThingsListAtFast(cell.CellToIndex(map));
|
||||
|
||||
public static List<Thing> GetThingListUnchecked(this IntVec2 cell, Map map)
|
||||
=> map.thingGrid.ThingsListAtFast(cell.CellToIndex(map));
|
||||
|
||||
public static Region? RegionAtUnchecked(this IntVec3 c, Map map,
|
||||
RegionType allowedRegionTypes = RegionType.Set_Passable)
|
||||
{
|
||||
map.regionAndRoomUpdater.TryRebuildDirtyRegionsAndRooms();
|
||||
|
||||
return map.regionGrid.regionGrid[c.CellToIndex(map)] is { valid: true } region
|
||||
&& (region.type & allowedRegionTypes) != RegionType.None
|
||||
? region
|
||||
: null;
|
||||
}
|
||||
|
||||
public static Region? GetValidRegionAtUnchecked(this RegionGrid grid, in IntVec3 c)
|
||||
{
|
||||
var map = grid.map;
|
||||
|
||||
map.regionAndRoomUpdater.TryRebuildDirtyRegionsAndRooms();
|
||||
return grid.regionGrid[c.CellToIndex(map)] is { valid: true } region ? region : null;
|
||||
}
|
||||
|
||||
public static bool ContainsThingStackableWith(this List<Thing> thingsOfSingleDef, Thing t)
|
||||
{
|
||||
var def = t.def;
|
||||
|
||||
for (var i = thingsOfSingleDef.Count; i-- > 0;)
|
||||
{
|
||||
if (thingsOfSingleDef[i].stackCount < def.stackLimit && thingsOfSingleDef[i].CanStackWith(t))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static Map? TryGetMapHeld(this Thing thing)
|
||||
{
|
||||
var maps = Current.Game.Maps;
|
||||
var mapIndex = (uint)thing.mapIndexOrState;
|
||||
|
||||
return mapIndex < (uint)maps.Count
|
||||
? maps[(int)mapIndex]
|
||||
: thing.ParentHolder is { } parentHolder
|
||||
? ThingOwnerUtility.GetRootMap(parentHolder)
|
||||
: null;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Map? TryGetMap(this Thing thing)
|
||||
{
|
||||
var maps = Current.Game.Maps;
|
||||
var mapIndex = (uint)thing.mapIndexOrState;
|
||||
|
||||
return mapIndex < (uint)maps.Count ? maps[(int)mapIndex] : null;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Map GetMap(this Thing thing) => Current.Game.Maps[thing.mapIndexOrState];
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Map GetMap(this Region region) => Current.Game.Maps[region.mapIndex];
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool IsSpawned(this Thing thing) => (uint)thing.mapIndexOrState < (uint)Current.Game.Maps.Count;
|
||||
|
||||
public static void InvokeWhenCellIndicesReady(this Map map, Action<Map> action)
|
||||
{
|
||||
if (map.cellIndices != null)
|
||||
action(map);
|
||||
else
|
||||
map.Events().ComponentsConstructed += action;
|
||||
}
|
||||
|
||||
public static void AddLetterSilently(this LetterStack letterStack, Letter letter, Color color, bool playSound = false,
|
||||
string? debugInfo = null)
|
||||
{
|
||||
var letterDef = letter.def.MemberwiseClone();
|
||||
letterDef.index = 0;
|
||||
letterDef.shortHash = 0;
|
||||
letterDef.color = color;
|
||||
letterDef.flashColor = color;
|
||||
letter.def = letterDef;
|
||||
letterStack.AddLetterSilently(letter, playSound, debugInfo);
|
||||
}
|
||||
|
||||
public static void AddLetterSilently(this LetterStack letterStack, Letter letter, bool playSound = false,
|
||||
string? debugInfo = null)
|
||||
{
|
||||
if (playSound)
|
||||
letter.def.arriveSound.PlayOneShotOnCamera();
|
||||
|
||||
letter.arrivalTime = Time.time;
|
||||
letter.arrivalTick = Find.TickManager.TicksGame;
|
||||
letter.debugInfo = debugInfo;
|
||||
letterStack.letters.Add(letter);
|
||||
letter.Received();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
// 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 System.Linq;
|
||||
using JetBrains.Annotations;
|
||||
using UnityEngine.Rendering;
|
||||
|
||||
namespace PerformanceFish.Utility;
|
||||
|
||||
public static class PipetteTool
|
||||
{
|
||||
[UsedImplicitly]
|
||||
[DebugAction("Misc", "Pipette tool", actionType = DebugActionType.ToolMap,
|
||||
allowedGameStates = AllowedGameStates.PlayingOnMap)]
|
||||
public static void Action()
|
||||
{
|
||||
var things = UI.MouseCell().GetThingList(Find.CurrentMap).ToList();
|
||||
things.Sort(static (x, y) => ((int)x.def.altitudeLayer).CompareTo((int)y.def.altitudeLayer));
|
||||
|
||||
for (var i = things.Count; i-- > 0; )
|
||||
{
|
||||
var thing = things[i];
|
||||
var material = thing.Graphic?.MatAt(thing.Rotation, thing);
|
||||
if (material == null)
|
||||
continue;
|
||||
|
||||
var texture = material.mainTexture;
|
||||
if (texture == null)
|
||||
continue;
|
||||
|
||||
var destinationTexture = RenderTexture.GetTemporary(texture.width, texture.height);
|
||||
Graphics.Blit(texture, destinationTexture);
|
||||
|
||||
AsyncGPUReadback.Request(destinationTexture, callback: result
|
||||
=>
|
||||
{
|
||||
try
|
||||
{
|
||||
using var pixelDataNative = result.GetData<Color32>();
|
||||
|
||||
var pixelDataFiltered = pixelDataNative
|
||||
.Where(static color => (color.r > 0 || color.g > 0 || color.b > 0) && color.a > 0)
|
||||
.ToArray();
|
||||
|
||||
var averageColor = pixelDataFiltered.Length < 1
|
||||
? (Color32)Color.black
|
||||
: GetAverage(pixelDataFiltered);
|
||||
|
||||
Log.Message($"Texture of thing '{thing}' has average color of {averageColor} {
|
||||
"(■)".Colorize(averageColor)}.");
|
||||
|
||||
Find.LetterStack.AddLetterSilently(LetterMaker.MakeLetter("Pipette result",
|
||||
$"Texture of thing '{thing}' has average color of {averageColor} {
|
||||
"(■)".Colorize(averageColor)}",
|
||||
LetterDefOf.NeutralEvent), averageColor);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error($"Exception in AsyncGPUReadback:\n{e}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
RenderTexture.ReleaseTemporary(destinationTexture);
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static Color32 GetAverage(Color32[] colors)
|
||||
=> new(GetAverage(colors, static color => color.r),
|
||||
GetAverage(colors, static color => color.g),
|
||||
GetAverage(colors, static color => color.b),
|
||||
GetAverage(colors, static color => color.a));
|
||||
|
||||
private static byte GetAverage(IEnumerable<Color32> colors, Func<Color32, int> selector)
|
||||
=> (byte)Mathf.RoundToInt((float)colors.Average(selector));
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
// 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 System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace PerformanceFish.Utility;
|
||||
|
||||
public record struct PooledArray<T> : IDisposable, IEnumerable<T>
|
||||
{
|
||||
private static T[]?[] _buffers = new T[]?[4];
|
||||
private static int _bufferCount;
|
||||
|
||||
private T[] _array;
|
||||
private int _length;
|
||||
|
||||
public T[] BackingArray => _array;
|
||||
public int Length => _length;
|
||||
public int Capacity => _array.Length;
|
||||
|
||||
public T this[int index]
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
Guard.IsLessThan(index, Length);
|
||||
return _array[index];
|
||||
}
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
set
|
||||
{
|
||||
Guard.IsLessThan(index, Length);
|
||||
_array[index] = value;
|
||||
}
|
||||
}
|
||||
|
||||
public PooledArray() : this(0)
|
||||
{
|
||||
}
|
||||
|
||||
public PooledArray(int length)
|
||||
{
|
||||
if (_bufferCount < 1)
|
||||
{
|
||||
NewBuffer(length);
|
||||
}
|
||||
else
|
||||
{
|
||||
ref var bucket = ref _buffers[--_bufferCount];
|
||||
_array = bucket!;
|
||||
bucket = null;
|
||||
if (_array.Length < length)
|
||||
ResizeBuffer(length);
|
||||
}
|
||||
|
||||
_length = length;
|
||||
}
|
||||
|
||||
public PooledArray(ICollection<T> collection) : this(collection.Count) => collection.CopyTo(_array, 0);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ref T GetReference(int index)
|
||||
{
|
||||
Guard.IsLessThan(index, Length);
|
||||
return ref _array[index];
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private void ResizeBuffer(int length) => Array.Resize(ref _array, Mathf.NextPowerOfTwo(length));
|
||||
|
||||
[MemberNotNull(nameof(_array))]
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private void NewBuffer(int length) => _array = new T[Mathf.NextPowerOfTwo(length)];
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (++_bufferCount > _buffers.Length)
|
||||
ExpandBuffers(_bufferCount);
|
||||
|
||||
if (_length > 0)
|
||||
Array.Clear(_array, 0, _length);
|
||||
|
||||
_buffers[_bufferCount - 1] = _array;
|
||||
}
|
||||
|
||||
public void Sort(IComparer<T> comparer) => _array.AsSpan()[.._length].Sort(comparer);
|
||||
|
||||
private static void ExpandBuffers(int minLength)
|
||||
{
|
||||
var newBuffers = new T[]?[Mathf.NextPowerOfTwo(minLength)];
|
||||
|
||||
Array.Copy(_buffers, newBuffers, _buffers.Length);
|
||||
|
||||
_buffers = newBuffers;
|
||||
}
|
||||
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
for (var i = 0; i < _length; i++)
|
||||
yield return _array[i];
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
}
|
||||
@@ -3,6 +3,8 @@
|
||||
// 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 System.Diagnostics;
|
||||
using System.Reflection.Emit;
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace PerformanceFish.Utility;
|
||||
@@ -27,6 +29,8 @@ public static class ReflectionExtensions
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static bool IsConst(this FieldInfo info) => info is { IsLiteral: true, IsInitOnly: false };
|
||||
|
||||
public static IEnumerable<Type> SubclassesWithNoMethodOverride(this Type type, params string[] names)
|
||||
{
|
||||
@@ -60,7 +64,7 @@ public static class ReflectionExtensions
|
||||
yield return subclass;
|
||||
}
|
||||
|
||||
public static IEnumerable<Type> SubclassesWithNoMethodOverride(this Type type, Type[] allowedDeclaringTypes,
|
||||
public static IEnumerable<Type> SubclassesWithNoMethodOverride(this Type type, Type?[] allowedDeclaringTypes,
|
||||
params string[] names)
|
||||
{
|
||||
Guard.IsNotNull(type);
|
||||
@@ -75,8 +79,11 @@ public static class ReflectionExtensions
|
||||
var hasOverrides = false;
|
||||
for (var j = names.Length; j-- > 0;)
|
||||
{
|
||||
if (allowedDeclaringTypes.Contains(AccessTools.Method(allSubclasses[i], names[j])!.DeclaringType!))
|
||||
if (AccessTools.Method(allSubclasses[i], names[j])!.DeclaringType is { } declaringType
|
||||
&& allowedDeclaringTypes.Contains(declaringType))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
hasOverrides = true;
|
||||
break;
|
||||
@@ -87,7 +94,7 @@ public static class ReflectionExtensions
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<Type> SubclassesWithNoMethodOverrideAndSelf(this Type type, Type[] allowedDeclaringTypes,
|
||||
public static IEnumerable<Type> SubclassesWithNoMethodOverrideAndSelf(this Type type, Type?[] allowedDeclaringTypes,
|
||||
params string[] names)
|
||||
{
|
||||
yield return type;
|
||||
@@ -95,4 +102,94 @@ public static class ReflectionExtensions
|
||||
foreach (var subclass in type.SubclassesWithNoMethodOverride(allowedDeclaringTypes, names))
|
||||
yield return subclass;
|
||||
}
|
||||
|
||||
public static Version GetReferencedAssemblyVersion(this Assembly assembly, Assembly referencedAssembly)
|
||||
=> assembly.GetReferencedAssemblyVersion(referencedAssembly.GetName().Name);
|
||||
|
||||
public static Version GetReferencedAssemblyVersion(this Assembly assembly, string referencedAssemblyName)
|
||||
=> assembly.GetReferencedAssemblies().First(reference
|
||||
=> reference.FullName.StartsWith($"{referencedAssemblyName}, Version", StringComparison.Ordinal)).Version;
|
||||
|
||||
public static Version GetLoadedVersion(this Assembly assembly) => assembly.GetName().Version;
|
||||
|
||||
public static T MemberwiseClone<T>(this T obj) => MemberwiseCloneCache<T>.InstanceFunc(obj);
|
||||
|
||||
private static class MemberwiseCloneCache
|
||||
{
|
||||
[ThreadStatic]
|
||||
private static FishTable<Type, Delegate>? _staticFuncs;
|
||||
|
||||
public static FishTable<Type, Delegate> StaticFuncs => _staticFuncs ??= Initialize();
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private static FishTable<Type, Delegate> Initialize()
|
||||
=> new()
|
||||
{
|
||||
ValueInitializer = static type
|
||||
=> (Delegate)AccessTools.DeclaredField(typeof(MemberwiseCloneCache<>).MakeGenericType(type),
|
||||
nameof(MemberwiseCloneCache<object>.StaticFunc)).GetValue(null)
|
||||
};
|
||||
}
|
||||
|
||||
private static class MemberwiseCloneCache<T>
|
||||
{
|
||||
internal static readonly Func<T, T> StaticFunc, InstanceFunc;
|
||||
|
||||
static MemberwiseCloneCache()
|
||||
{
|
||||
var type = typeof(T);
|
||||
try
|
||||
{
|
||||
var dm = new DynamicMethod($"FisheryMemberwiseCloneFunc_{type.Name}",
|
||||
type, [type], type, true);
|
||||
|
||||
var il = dm.GetILGenerator();
|
||||
|
||||
if (type.IsValueType)
|
||||
{
|
||||
il.Emit(FishTranspiler.Argument(0));
|
||||
}
|
||||
else
|
||||
{
|
||||
var copyVariable = FishTranspiler.NewLocalVariable(type, il);
|
||||
|
||||
il.Emit(FishTranspiler.New(type, Type.EmptyTypes));
|
||||
il.Emit(copyVariable.Store());
|
||||
|
||||
foreach (var field in typeof(T).GetFields(BindingFlags.Instance
|
||||
| BindingFlags.Public
|
||||
| BindingFlags.NonPublic))
|
||||
{
|
||||
var fieldInstruction = FishTranspiler.Field(field);
|
||||
|
||||
il.Emit(copyVariable.Load());
|
||||
il.Emit(FishTranspiler.Argument(0));
|
||||
il.Emit(fieldInstruction.Load());
|
||||
il.Emit(fieldInstruction.Store());
|
||||
}
|
||||
|
||||
il.Emit(copyVariable.Load());
|
||||
}
|
||||
|
||||
il.Emit(FishTranspiler.Return);
|
||||
|
||||
StaticFunc = (Func<T, T>)dm.CreateDelegate(typeof(Func<T, T>));
|
||||
|
||||
InstanceFunc = static obj => obj!.GetType() == typeof(T)
|
||||
? StaticFunc(obj)
|
||||
: ((Func<T, T>)MemberwiseCloneCache.StaticFuncs.GetOrAdd(obj.GetType()))(obj);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error($"Failed creating a MemberwiseClone delegate for {type.FullDescription()}\n{
|
||||
e}\n{new StackTrace()}");
|
||||
|
||||
var defaultCloneFunc = (Func<object, object>)AccessTools
|
||||
.DeclaredMethod(typeof(object), nameof(MemberwiseClone))
|
||||
.CreateDelegate(typeof(Func<object, object>));
|
||||
|
||||
InstanceFunc = StaticFunc = obj => (T)defaultCloneFunc(obj!);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,5 +8,8 @@ namespace PerformanceFish.Utility;
|
||||
public static class StringExtensions
|
||||
{
|
||||
public static bool NameEqualsCaseInsensitive(this MethodInfo methodInfo, string name)
|
||||
=> methodInfo.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase);
|
||||
=> methodInfo.Name.Equals(name, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
public static string AppendWhen(this string instance, bool predicate, string text)
|
||||
=> predicate ? instance + text : instance;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// 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.Utility;
|
||||
|
||||
public static class ThingHelper
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int GetKey(this Thing thing)
|
||||
=> thing.thingIDNumber != -1 ? thing.thingIDNumber : RuntimeHelpers.GetHashCode(thing);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
// 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.Utility;
|
||||
|
||||
public static class TickHelper
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool Past(int tick) => tick < TicksGame;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool MatchesModulo(int powerOfTwo) => MatchesModulo(powerOfTwo, 0);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool MatchesModulo(int powerOfTwo, int offset)
|
||||
=> ((TicksGame + offset) & (powerOfTwo - 1)) == 0;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int Add(int ticks) => ticks + TicksGame;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int Add(int ticks, int randomOffset)
|
||||
=> ticks + (randomOffset & 63) + TicksGame;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int Add(int ticks, int randomOffset, int powerOfTwo)
|
||||
=> ticks + (randomOffset & (powerOfTwo - 1)) + TicksGame;
|
||||
|
||||
public static int TicksGame
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => Current.gameInt.tickManager.TicksGame;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user