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
@@ -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!);
}
}
}
}