Skip to content

Commit 363d116

Browse files
committed
Merge span extension functions in a single class and move S(RO)Span<T> to Utils.
1 parent 1137124 commit 363d116

9 files changed

Lines changed: 51 additions & 43 deletions

File tree

TACTSharp/BLTE.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.IO.Compression;
33
using System.Security.Cryptography;
44

5+
using TACTSharp.Extensions;
6+
57
namespace TACTSharp
68
{
79
public static class BLTE

TACTSharp/CASCIndexInstance.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using Microsoft.Win32.SafeHandles;
22
using System.IO.MemoryMappedFiles;
33

4+
using TACTSharp.Extensions;
5+
46
namespace TACTSharp
57
{
68
public sealed class CASCIndexInstance
@@ -82,7 +84,7 @@ unsafe public (int offset, int size, int archiveIndex) GetIndexInfo(Span<byte> e
8284

8385
var indexHigh = entrySpan[header.entryKeyBytes];
8486
var indexLow = entrySpan.Slice(header.entryKeyBytes + 1, 4).ReadInt32BE();
85-
var indexSize = System.Buffers.Binary.BinaryPrimitives.ReadInt32LittleEndian(entrySpan.Slice(header.entryKeyBytes + 5, header.entrySizeBytes)) - 30;
87+
var indexSize = entrySpan.Slice(header.entryKeyBytes + 5, header.entrySizeBytes).ReadInt32LE() - 30;
8688

8789
var archiveIndex = (indexHigh << 2 | (byte)((indexLow & 0xC0000000) >> 30));
8890
var archiveOffset = (indexLow & 0x3FFFFFFF) + 30;

TACTSharp/Extensions/BinarySearchExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.Runtime.CompilerServices;
33
using System.Runtime.InteropServices;
44

5+
using TACTSharp.Utils;
6+
57
namespace TACTSharp.Extensions
68
{
79
internal static class BinarySearchExtensions

TACTSharp/Extensions/SpanExtensions.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
using System.Runtime.CompilerServices;
1+
using System.Buffers.Binary;
2+
using System.Runtime.CompilerServices;
3+
using System.Text;
4+
5+
using TACTSharp.Utils;
26

37
namespace TACTSharp.Extensions
48
{
@@ -11,5 +15,37 @@ public static StridedReadOnlySpan<T> WithStride<T>(this ReadOnlySpan<T> span, in
1115
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1216
public static StridedSpan<T> WithStride<T>(this Span<T> span, int stride)
1317
=> new(span, stride);
18+
19+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
20+
public static ushort ReadUInt16BE(this ReadOnlySpan<byte> source)
21+
=> BinaryPrimitives.ReadUInt16BigEndian(source);
22+
23+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
24+
public static short ReadInt16BE(this ReadOnlySpan<byte> source)
25+
=> BinaryPrimitives.ReadInt16BigEndian(source);
26+
27+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
28+
public static int ReadInt24BE(this ReadOnlySpan<byte> source)
29+
=> source[2] | source[1] << 8 | source[0] << 16;
30+
31+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
32+
public static int ReadInt32BE(this ReadOnlySpan<byte> source)
33+
=> BinaryPrimitives.ReadInt32BigEndian(source);
34+
35+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
36+
public static int ReadInt32LE(this ReadOnlySpan<byte> source)
37+
=> BinaryPrimitives.ReadInt32LittleEndian(source);
38+
39+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
40+
public static uint ReadUInt32BE(this ReadOnlySpan<byte> source)
41+
=> BinaryPrimitives.ReadUInt32BigEndian(source);
42+
43+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
44+
public static long ReadInt40BE(this ReadOnlySpan<byte> source)
45+
=> source[4] | source[3] << 8 | source[2] << 16 | source[1] << 24 | source[0] << 32;
46+
47+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
48+
public static string ReadNullTermString(this ReadOnlySpan<byte> source)
49+
=> Encoding.UTF8.GetString(source[..source.IndexOf((byte)0)]);
1450
}
1551
}

TACTSharp/IndexInstance.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using Microsoft.Win32.SafeHandles;
22
using System.IO.MemoryMappedFiles;
33

4+
using TACTSharp.Extensions;
5+
46
namespace TACTSharp
57
{
68
// mostly based on schlumpf's implementation, but with some changes because i dont know how to port some c++ things to c# properly

TACTSharp/InstallInstance.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.Collections;
33
using System.IO.MemoryMappedFiles;
44

5+
using TACTSharp.Extensions;
6+
57
namespace TACTSharp
68
{
79
public class InstallInstance

TACTSharp/TVFSInstance.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.Diagnostics;
44
using System.Runtime.CompilerServices;
55
using System.Text;
6+
7+
using TACTSharp.Extensions;
68
using TACTSharp.Interfaces;
79
using static TACTSharp.RootInstance;
810

TACTSharp/Utils/Extensions.cs

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Runtime.CompilerServices;
22

3-
namespace TACTSharp
3+
namespace TACTSharp.Utils
44
{
55
internal readonly ref struct StridedSpan<T>(Span<T> data, int stride)
66
{

0 commit comments

Comments
 (0)