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
+85 -15
View File
@@ -3,19 +3,28 @@
// 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 System.Runtime.InteropServices;
namespace PerformanceFish.Prepatching;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct FishStash
public unsafe struct FishStash : IEquatable<FishStash>
{
private const string ANCHOR_STRING_VALUE = "F1shSt4sh69";
private const string ANCHOR_STRING_VALUE = "F1shSt4sh6912345";
private const int ANCHOR_STRING_LENGTH = 16;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 11)]
private string? _anchorString;
public UnhandledMessageQueue? UnhandledMessages;
public GCHandle MessageQueueGCHandle;
private fixed char _anchorString[ANCHOR_STRING_LENGTH];
internal GCHandle MessageQueueGCHandle;
private int _activePrepatchCount;
private IntPtr _activePrepatches;
public UnhandledMessageQueue? UnhandledMessages
=> MessageQueueGCHandle != default && MessageQueueGCHandle.IsAllocated
? Unsafe.As<UnhandledMessageQueue>(MessageQueueGCHandle.Target)
: null;
public static ref FishStash Get
=> ref StaticHandle != default
@@ -29,8 +38,12 @@ public struct FishStash
?? ThrowHelper.ThrowInvalidOperationException<nint>("Failed to fetch internal FishStashHandle.");
ref var stash = ref InternalFishStashHandle.Stash;
if (stash._anchorString != ANCHOR_STRING_VALUE)
ThrowHelper.ThrowInvalidDataException($"FishStash started with invalid anchor: {stash._anchorString}");
fixed (char* anchor = stash._anchorString)
{
var anchorString = new string(anchor, 0, ANCHOR_STRING_LENGTH);
if (anchorString != ANCHOR_STRING_VALUE)
ThrowHelper.ThrowInvalidDataException($"FishStash started with invalid anchor: {anchorString}");
}
return ref stash;
}
@@ -41,18 +54,53 @@ public struct FishStash
internal static nint StaticHandle { get; private set; }
internal static unsafe ref FishStash FromHandle(IntPtr handle) => ref Unsafe.AsRef<FishStash>(handle.ToPointer());
internal static ref FishStash FromHandle(IntPtr handle) => ref Unsafe.AsRef<FishStash>(handle.ToPointer());
private static unsafe void Create()
private static void Create()
{
Guard.IsEqualTo(ANCHOR_STRING_VALUE.Length, ANCHOR_STRING_LENGTH);
if (StaticHandle != default)
ThrowHelper.ThrowInvalidOperationException("Tried creating duplicate FishStash.");
var handle = (FishStash*)Marshal.AllocHGlobal(sizeof(FishStash));
Unsafe.InitBlockUnaligned(handle, 0, (uint)sizeof(FishStash));
var handle = Marshal.AllocHGlobal(sizeof(FishStash));
Unsafe.InitBlockUnaligned(handle.ToPointer(), 0, (uint)sizeof(FishStash));
((FishStash*)handle)->_anchorString = ANCHOR_STRING_VALUE;
StaticHandle = handle;
for (var i = 0; i < ANCHOR_STRING_VALUE.Length; i++)
handle->_anchorString[i] = ANCHOR_STRING_VALUE[i];
StaticHandle = (nint)handle;
}
internal void InitializeActivePrepatchIDs()
{
var activePrepatches = PerformanceFishMod.AllPrepatchClasses!.SelectMany(static patchClass
=> patchClass.Patches.Where(static patch
=> patch.Enabled).Select(static patch => patch.IDNumber)).ToArray();
var activePrepatchCount = activePrepatches.Length;
_activePrepatchCount = activePrepatchCount;
_activePrepatches = Marshal.AllocHGlobal(sizeof(int) * activePrepatchCount);
Marshal.Copy(activePrepatches, 0, _activePrepatches, activePrepatchCount);
}
internal void Release() => Marshal.FreeHGlobal(_activePrepatches);
internal bool IsPatchActive(FishPrepatchBase patch)
{
if (_activePrepatchCount <= 0)
return false;
var patchID = patch.IDNumber;
var activePrepatches = (int*)_activePrepatches;
for (var i = _activePrepatchCount; i-- > 0;)
{
if (activePrepatches[i] == patchID)
return true;
}
return false;
}
static FishStash()
@@ -60,4 +108,26 @@ public struct FishStash
if (!Log.Ready && InternalFishStashHandle is null)
Create();
}
public override bool Equals(object? obj) => obj is FishStash stash && Equals(stash);
public override int GetHashCode()
=> HashCode.Combine(MessageQueueGCHandle.GetHashCode(), _activePrepatchCount, _activePrepatches.GetHashCode());
public static bool operator ==(FishStash left, FishStash right) => left.Equals(right);
public static bool operator !=(FishStash left, FishStash right) => !(left == right);
public bool Equals(FishStash other)
{
fixed (char* anchor = _anchorString)
{
if (anchor != other._anchorString)
return false;
}
return MessageQueueGCHandle == other.MessageQueueGCHandle
&& _activePrepatchCount == other._activePrepatchCount
&& _activePrepatches == other._activePrepatches;
}
}