From 363d116d6ee91d0e490ea0377b75b6cdf6281e6e Mon Sep 17 00:00:00 2001 From: Warpten Date: Mon, 29 Jun 2026 01:45:13 +0200 Subject: [PATCH] Merge span extension functions in a single class and move S(RO)Span to Utils. --- TACTSharp/BLTE.cs | 2 + TACTSharp/CASCIndexInstance.cs | 4 +- .../Extensions/BinarySearchExtensions.cs | 2 + TACTSharp/Extensions/SpanExtensions.cs | 38 +++++++++++++++++- TACTSharp/IndexInstance.cs | 2 + TACTSharp/InstallInstance.cs | 2 + TACTSharp/TVFSInstance.cs | 2 + TACTSharp/Utils/Extensions.cs | 40 ------------------- TACTSharp/{ => Utils}/StridedSpan.cs | 2 +- 9 files changed, 51 insertions(+), 43 deletions(-) delete mode 100644 TACTSharp/Utils/Extensions.cs rename TACTSharp/{ => Utils}/StridedSpan.cs (97%) diff --git a/TACTSharp/BLTE.cs b/TACTSharp/BLTE.cs index e7df45f..ae9fef9 100644 --- a/TACTSharp/BLTE.cs +++ b/TACTSharp/BLTE.cs @@ -2,6 +2,8 @@ using System.IO.Compression; using System.Security.Cryptography; +using TACTSharp.Extensions; + namespace TACTSharp { public static class BLTE diff --git a/TACTSharp/CASCIndexInstance.cs b/TACTSharp/CASCIndexInstance.cs index d8c8bbe..bb0cff9 100644 --- a/TACTSharp/CASCIndexInstance.cs +++ b/TACTSharp/CASCIndexInstance.cs @@ -1,6 +1,8 @@ using Microsoft.Win32.SafeHandles; using System.IO.MemoryMappedFiles; +using TACTSharp.Extensions; + namespace TACTSharp { public sealed class CASCIndexInstance @@ -82,7 +84,7 @@ unsafe public (int offset, int size, int archiveIndex) GetIndexInfo(Span e var indexHigh = entrySpan[header.entryKeyBytes]; var indexLow = entrySpan.Slice(header.entryKeyBytes + 1, 4).ReadInt32BE(); - var indexSize = System.Buffers.Binary.BinaryPrimitives.ReadInt32LittleEndian(entrySpan.Slice(header.entryKeyBytes + 5, header.entrySizeBytes)) - 30; + var indexSize = entrySpan.Slice(header.entryKeyBytes + 5, header.entrySizeBytes).ReadInt32LE() - 30; var archiveIndex = (indexHigh << 2 | (byte)((indexLow & 0xC0000000) >> 30)); var archiveOffset = (indexLow & 0x3FFFFFFF) + 30; diff --git a/TACTSharp/Extensions/BinarySearchExtensions.cs b/TACTSharp/Extensions/BinarySearchExtensions.cs index aec9c0f..d041821 100644 --- a/TACTSharp/Extensions/BinarySearchExtensions.cs +++ b/TACTSharp/Extensions/BinarySearchExtensions.cs @@ -2,6 +2,8 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using TACTSharp.Utils; + namespace TACTSharp.Extensions { internal static class BinarySearchExtensions diff --git a/TACTSharp/Extensions/SpanExtensions.cs b/TACTSharp/Extensions/SpanExtensions.cs index 3c1289c..1483ac5 100644 --- a/TACTSharp/Extensions/SpanExtensions.cs +++ b/TACTSharp/Extensions/SpanExtensions.cs @@ -1,4 +1,8 @@ -using System.Runtime.CompilerServices; +using System.Buffers.Binary; +using System.Runtime.CompilerServices; +using System.Text; + +using TACTSharp.Utils; namespace TACTSharp.Extensions { @@ -11,5 +15,37 @@ public static StridedReadOnlySpan WithStride(this ReadOnlySpan span, in [MethodImpl(MethodImplOptions.AggressiveInlining)] public static StridedSpan WithStride(this Span span, int stride) => new(span, stride); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ushort ReadUInt16BE(this ReadOnlySpan source) + => BinaryPrimitives.ReadUInt16BigEndian(source); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static short ReadInt16BE(this ReadOnlySpan source) + => BinaryPrimitives.ReadInt16BigEndian(source); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int ReadInt24BE(this ReadOnlySpan source) + => source[2] | source[1] << 8 | source[0] << 16; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int ReadInt32BE(this ReadOnlySpan source) + => BinaryPrimitives.ReadInt32BigEndian(source); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int ReadInt32LE(this ReadOnlySpan source) + => BinaryPrimitives.ReadInt32LittleEndian(source); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static uint ReadUInt32BE(this ReadOnlySpan source) + => BinaryPrimitives.ReadUInt32BigEndian(source); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static long ReadInt40BE(this ReadOnlySpan source) + => source[4] | source[3] << 8 | source[2] << 16 | source[1] << 24 | source[0] << 32; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static string ReadNullTermString(this ReadOnlySpan source) + => Encoding.UTF8.GetString(source[..source.IndexOf((byte)0)]); } } diff --git a/TACTSharp/IndexInstance.cs b/TACTSharp/IndexInstance.cs index c46245f..1a1f2c7 100644 --- a/TACTSharp/IndexInstance.cs +++ b/TACTSharp/IndexInstance.cs @@ -1,6 +1,8 @@ using Microsoft.Win32.SafeHandles; using System.IO.MemoryMappedFiles; +using TACTSharp.Extensions; + namespace TACTSharp { // mostly based on schlumpf's implementation, but with some changes because i dont know how to port some c++ things to c# properly diff --git a/TACTSharp/InstallInstance.cs b/TACTSharp/InstallInstance.cs index f1f2578..89ffc3f 100644 --- a/TACTSharp/InstallInstance.cs +++ b/TACTSharp/InstallInstance.cs @@ -2,6 +2,8 @@ using System.Collections; using System.IO.MemoryMappedFiles; +using TACTSharp.Extensions; + namespace TACTSharp { public class InstallInstance diff --git a/TACTSharp/TVFSInstance.cs b/TACTSharp/TVFSInstance.cs index 57b7cc4..bbfea6a 100644 --- a/TACTSharp/TVFSInstance.cs +++ b/TACTSharp/TVFSInstance.cs @@ -3,6 +3,8 @@ using System.Diagnostics; using System.Runtime.CompilerServices; using System.Text; + +using TACTSharp.Extensions; using TACTSharp.Interfaces; using static TACTSharp.RootInstance; diff --git a/TACTSharp/Utils/Extensions.cs b/TACTSharp/Utils/Extensions.cs deleted file mode 100644 index cce31ae..0000000 --- a/TACTSharp/Utils/Extensions.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System.Buffers.Binary; -using System.Text; - -static class Extensions -{ - public static ushort ReadUInt16BE(this ReadOnlySpan source) - { - return BinaryPrimitives.ReadUInt16BigEndian(source); - } - - public static short ReadInt16BE(this ReadOnlySpan source) - { - return BinaryPrimitives.ReadInt16BigEndian(source); - } - - public static int ReadInt24BE(this ReadOnlySpan source) - { - return source[2] | source[1] << 8 | source[0] << 16; - } - - public static int ReadInt32BE(this ReadOnlySpan source) - { - return BinaryPrimitives.ReadInt32BigEndian(source); - } - - public static uint ReadUInt32BE(this ReadOnlySpan source) - { - return BinaryPrimitives.ReadUInt32BigEndian(source); - } - - public static long ReadInt40BE(this ReadOnlySpan source) - { - return source[4] | source[3] << 8 | source[2] << 16 | source[1] << 24 | source[0] << 32; - } - - public static string ReadNullTermString(this ReadOnlySpan source) - { - return Encoding.UTF8.GetString(source[..source.IndexOf((byte)0)]); - } -} diff --git a/TACTSharp/StridedSpan.cs b/TACTSharp/Utils/StridedSpan.cs similarity index 97% rename from TACTSharp/StridedSpan.cs rename to TACTSharp/Utils/StridedSpan.cs index cdb6d97..b2d7040 100644 --- a/TACTSharp/StridedSpan.cs +++ b/TACTSharp/Utils/StridedSpan.cs @@ -1,6 +1,6 @@ using System.Runtime.CompilerServices; -namespace TACTSharp +namespace TACTSharp.Utils { internal readonly ref struct StridedSpan(Span data, int stride) {