Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
diogotr7 committed Oct 19, 2024
1 parent 067afed commit 7307985
Show file tree
Hide file tree
Showing 15 changed files with 211 additions and 74 deletions.
3 changes: 0 additions & 3 deletions src/StarBreaker.Common/BinaryReaderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace StarBreaker.Common;

public static class BinaryReaderExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Read<T>(this BinaryReader br) where T : unmanaged
{
var size = Unsafe.SizeOf<T>();
Expand All @@ -23,7 +22,6 @@ public static T Read<T>(this BinaryReader br) where T : unmanaged
return MemoryMarshal.Read<T>(span);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T[] ReadArray<T>(this BinaryReader reader, int count) where T : unmanaged
{
var items = new T[count];
Expand Down Expand Up @@ -66,7 +64,6 @@ public static long Locate(this BinaryReader br, ReadOnlySpan<byte> magic, long b
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ReadStringOfLength(this BinaryReader br, int length)
{
if (length >= 0xffff)
Expand Down
5 changes: 1 addition & 4 deletions src/StarBreaker.Common/CigGuid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ public CigGuid(string input)
_8 = ParseHexDigit(span, 34);

return;

[MethodImpl(MethodImplOptions.AggressiveInlining)]

static byte ParseHexDigit(ReadOnlySpan<char> input, int offset)
{
if (!byte.TryParse(input.Slice(offset, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var result))
Expand All @@ -80,7 +79,6 @@ public override string ToString()
return str.ToString();
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteInto(TextWriter writer)
{
Span<char> buffer = stackalloc char[36];
Expand Down Expand Up @@ -110,7 +108,6 @@ public void WriteInto(TextWriter writer)

return;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static void WriteHexDigit(Span<char> buffer, byte value, int offset)
{
buffer[offset] = _map[value >> 4];
Expand Down
5 changes: 1 addition & 4 deletions src/StarBreaker.Common/Crc32c.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace StarBreaker.Common;

public static class Crc32c
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint FromSpan(ReadOnlySpan<byte> data)
{
var acc = 0xFFFFFFFFu;
Expand All @@ -21,10 +20,8 @@ public static uint FromSpan(ReadOnlySpan<byte> data)
return ~acc;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint FromSpan(Span<byte> data) => FromSpan((ReadOnlySpan<byte>)data);

[MethodImpl(MethodImplOptions.AggressiveInlining)]

public static uint FromString(string data)
{
var length = Encoding.UTF8.GetByteCount(data);
Expand Down
51 changes: 18 additions & 33 deletions src/StarBreaker.Common/SpanReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public SpanReader(ReadOnlySpan<byte> span, int position = 0)
_span = span;
_position = position;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]

public ReadOnlySpan<byte> PeekBytes(int count)
{
#if DEBUG
Expand All @@ -30,11 +29,10 @@ public ReadOnlySpan<byte> PeekBytes(int count)
throw new Exception("Reading past the end of the span");
}
#endif

return _span.Slice(_position, count);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ReadOnlySpan<byte> ReadBytes(int count)
{
#if DEBUG
Expand All @@ -45,51 +43,46 @@ public ReadOnlySpan<byte> ReadBytes(int count)
}

#endif

var span = _span.Slice(_position, count);
_position += count;
return span;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Expect<T>(T value) where T : unmanaged
{
var actual = Read<T>();
if (!actual.Equals(value))
throw new Exception($"Expected {value}, got {actual}");
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]

public void ExpectAny<T>(scoped ReadOnlySpan<T> values) where T : unmanaged, IEquatable<T>
{
var actual = Read<T>();
if (!values.Contains(actual))
throw new Exception($"Expected {values.ToString()}, got {actual}");
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ExpectAny<T>(scoped Span<T> values) where T : unmanaged, IEquatable<T>
{
var actual = Read<T>();
if (!values.Contains(actual))
throw new Exception($"Expected {values.ToString()}, got {actual}");
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ExpectKey(string key)
{
var uintKey = Crc32c.FromString(key);
Expect(uintKey);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ExpectAnyKey(scoped Span<string> keys)
{
Span<uint> uintKeys = stackalloc uint[keys.Length];

for (var i = 0; i < keys.Length; i++)
uintKeys[i] = Crc32c.FromString(keys[i]);

ExpectAny(uintKeys);
}

Expand All @@ -99,7 +92,7 @@ public void ExpectAnyKey(scoped Span<string> keys)
public int Position => _position;

public int Remaining => _span.Length - _position;

public uint NextKey => Peek<uint>();

public ReadOnlySpan<byte> RemainingBytes => _span[_position..];
Expand All @@ -108,86 +101,78 @@ public void ExpectAnyKey(scoped Span<string> keys)

public void Advance(int count) => _position += count;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ReadOnlySpan<T> ReadSpan<T>(int count) where T : unmanaged => MemoryMarshal.Cast<byte, T>(ReadBytes(count * Unsafe.SizeOf<T>()));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T Read<T>() where T : unmanaged => MemoryMarshal.Read<T>(ReadBytes(Unsafe.SizeOf<T>()));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool ReadBoolean() => ReadByte() != 0;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte ReadByte() => _span[_position++];

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public sbyte ReadSByte() => (sbyte)ReadByte();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Half ReadHalf(bool littleEndian = true) => littleEndian
? BinaryPrimitives.ReadHalfLittleEndian(ReadBytes(Unsafe.SizeOf<Half>()))
: BinaryPrimitives.ReadHalfBigEndian(ReadBytes(Unsafe.SizeOf<Half>()));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float ReadSingle(bool littleEndian = true) => littleEndian
? BinaryPrimitives.ReadSingleLittleEndian(ReadBytes(sizeof(float)))
: BinaryPrimitives.ReadSingleBigEndian(ReadBytes(sizeof(float)));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double ReadDouble(bool littleEndian = true) => littleEndian
? BinaryPrimitives.ReadDoubleLittleEndian(ReadBytes(sizeof(double)))
: BinaryPrimitives.ReadDoubleBigEndian(ReadBytes(sizeof(double)));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public short ReadInt16(bool littleEndian = true) => littleEndian
? BinaryPrimitives.ReadInt16LittleEndian(ReadBytes(sizeof(short)))
: BinaryPrimitives.ReadInt16BigEndian(ReadBytes(sizeof(short)));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int ReadInt32(bool littleEndian = true) => littleEndian
? BinaryPrimitives.ReadInt32LittleEndian(ReadBytes(sizeof(int)))
: BinaryPrimitives.ReadInt32BigEndian(ReadBytes(sizeof(int)));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public long ReadInt64(bool littleEndian = true) => littleEndian
? BinaryPrimitives.ReadInt64LittleEndian(ReadBytes(sizeof(long)))
: BinaryPrimitives.ReadInt64BigEndian(ReadBytes(sizeof(long)));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ushort ReadUInt16(bool littleEndian = true) => littleEndian
? BinaryPrimitives.ReadUInt16LittleEndian(ReadBytes(sizeof(ushort)))
: BinaryPrimitives.ReadUInt16BigEndian(ReadBytes(sizeof(ushort)));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint ReadUInt32(bool littleEndian = true) => littleEndian
? BinaryPrimitives.ReadUInt32LittleEndian(ReadBytes(sizeof(uint)))
: BinaryPrimitives.ReadUInt32BigEndian(ReadBytes(sizeof(uint)));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ulong ReadUInt64(bool littleEndian = true) => littleEndian
? BinaryPrimitives.ReadUInt64LittleEndian(ReadBytes(sizeof(ulong)))
: BinaryPrimitives.ReadUInt64BigEndian(ReadBytes(sizeof(ulong)));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadStringOfLength(ushort length)
{
if (length == 0)
return string.Empty;

if (length >= 0xffff)
throw new Exception("Size is too large");

return Encoding.ASCII.GetString(ReadBytes(length));
}


public string ReadStringInsideArray(int length)
{
var bytes = ReadBytes(length);
var strLength = bytes.IndexOf((byte)0);
return Encoding.ASCII.GetString(bytes[..strLength]);
}

/// <summary>
/// Reads a value from the span without advancing the position.
/// </summary>
public T Peek<T>() where T : unmanaged
{
if (typeof(T) == typeof(bool))
throw new InvalidOperationException("Read an int and compare it to 0 instead");

return MemoryMarshal.Read<T>(PeekBytes(Unsafe.SizeOf<T>()));
}
}
2 changes: 0 additions & 2 deletions src/StarBreaker.Common/TextWriterExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System.Runtime.CompilerServices;
using System.Text;

namespace StarBreaker.Common;

public static class TextWriterExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int WriteString(this TextWriter writer, ReadOnlySpan<byte> data, int offset)
{
var relevantData = data[offset..];
Expand Down
12 changes: 8 additions & 4 deletions src/StarBreaker.CryChunkFile/ChunkFile.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Text;
using StarBreaker.Common;

namespace StarBreaker.CryChunkFile;
Expand Down Expand Up @@ -54,13 +55,14 @@ public void WriteXmlTo(TextWriter writer)
switch (signature)
{
case CrChMagic:
WriteChCfTo(writer, ref reader);
//WriteChCfTo(writer, ref reader);
break;
case IvoMagic:
WriteIvoTo(writer, ref reader);
break;
default:
throw new Exception("Invalid signature");
//throw new Exception("Invalid signature");
break;
}
}

Expand Down Expand Up @@ -146,10 +148,12 @@ private static IChunk ReadChunk(ref SpanReader reader, ChunkTypeIvo chunkTypeIvo
{
return chunkTypeIvo switch
{
//TODO: fill in
ChunkTypeIvo.CompiledBonesIvo320 => CompiledBoneChunk.Read(ref reader),
ChunkTypeIvo.MtlNameIvo320 => MaterialNameChunk.Read(ref reader),
ChunkTypeIvo.CompiledPhysicalBonesUnknown => ProbablyCustomBonesChunk.Read(ref reader),
ChunkTypeIvo.PhysicalHierarchy => PhysicalHierarchyChunk.Read(ref reader),
ChunkTypeIvo.Unknown05 => UnknownChunk5.Read(ref reader),
ChunkTypeIvo.MeshIvo320 => MeshIvo320.Read(ref reader),
ChunkTypeIvo.IvoSkin2 => IvoSkin2.Read(ref reader),
_ => UnknownChunk.Read(ref reader)
};
}
Expand Down
18 changes: 9 additions & 9 deletions src/StarBreaker.CryChunkFile/ChunkTypeIvo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ namespace StarBreaker.CryChunkFile;

public enum ChunkTypeIvo : uint
{
Unknown01 = 0x0A2485B6,
Unknown02 = 0x194FBC50,
Unknown03 = 0x1BBC4103,
RigLogic = 0x0A2485B6,
DbData = 0x194FBC50,
Skeleton = 0x1BBC4103,
Unknown04 = 0x322BA3C7,
Unknown05 = 0x370275B7,
Unknown06 = 0x3C2CC7AB,
Unknown07 = 0x4733C6ED,
Unknown08 = 0x570FCCBA,
Unknown09 = 0x57A38888,
BShapesGpu = 0x57A38888,
Unknown10 = 0x6B8DF259,
Unknown11 = 0x7E035555,
RigInfo = 0x7E035555,
Unknown12 = 0x822D7322,
MtlNameIvo320 = 0x83353333,
CompiledPhysicalBonesUnknown = 0x90C62222,
PhysicalHierarchy = 0x90C62222,
MeshIvo320 = 0x92914444,
Unknown16 = 0xA9496CB5,
IvoSkin2 = 0xB8757777,
CompiledBonesIvo320 = 0xC2011111,
Unknown19 = 0xC2387AD1,
Unknown20 = 0xF5C6EB5B,
Unknown21 = 0xF7086666,
Unknown22 = 0xF7351608,
BShapes = 0xF5C6EB5B,
ProtosInfo = 0xF7086666,
DBA = 0xF7351608,
}
Loading

0 comments on commit 7307985

Please sign in to comment.