Updated ascii strings compare

This commit is contained in:
Tretiner 2024-07-29 00:37:29 +05:00
parent 045e5903f2
commit d3c5584990
2 changed files with 22 additions and 18 deletions

View File

@ -26,20 +26,24 @@ public static class Extensions
return span[..i]; return span[..i];
} }
public static ExBinaryReader NextAsciiStringMustEqual(this ExBinaryReader binReader, string text) public static ExBinaryReader NextAsciiStringMustEqual(this ExBinaryReader binReader, ReadOnlySpan<byte> text)
{ {
if (!binReader.IsNextAsciiStringEquals(text)) if (!binReader.IsNextAsciiStringEquals(text))
{ {
throw new Exception(text + "Нарушение структуры файла. "); throw new Exception(Encoding.ASCII.GetString(text) + "Нарушение структуры файла. ");
} }
return binReader; return binReader;
} }
public static bool IsNextAsciiStringEquals(this ExBinaryReader binReader, string text) public static bool IsNextAsciiStringEquals(this ExBinaryReader binReader, ReadOnlySpan<byte> textSpan)
{ {
Span<byte> textSpan = Encoding.ASCII.GetBytes(text); var nextTextSpan = binReader.ReadBytes(stackalloc byte[textSpan.Length]);
var nextTextSpan = binReader.ReadBytes(stackalloc byte[text.Length]);
if (textSpan.SequenceEqual(nextTextSpan))
{
return true;
}
return textSpan.Length == nextTextSpan.Length && SpansEqualInvariant(textSpan, nextTextSpan); return textSpan.Length == nextTextSpan.Length && SpansEqualInvariant(textSpan, nextTextSpan);

View File

@ -95,14 +95,14 @@ public sealed class SsReplayReader(Stream stream) : IDisposable
_binaryReader // relic chunky _binaryReader // relic chunky
.Skip(44) .Skip(44)
.NextAsciiStringMustEqual("POSTGAMEINFO\0DATADATA") .NextAsciiStringMustEqual("POSTGAMEINFO\0DATADATA"u8)
.SkipInt32(3); .SkipInt32(3);
Replay.TotalTicks = _binaryReader.ReadInt32(); // Полное игровое время (1 сек = 8 тиков) Replay.TotalTicks = _binaryReader.ReadInt32(); // Полное игровое время (1 сек = 8 тиков)
_binaryReader // relic chunky _binaryReader // relic chunky
.Skip(24) .Skip(24)
.NextAsciiStringMustEqual("FOLDINFO") .NextAsciiStringMustEqual("FOLDINFO"u8)
.SkipInt32(); .SkipInt32();
Descriptor.FoldInfoStart = _binaryReader.Position; // 153 Descriptor.FoldInfoStart = _binaryReader.Position; // 153
@ -110,7 +110,7 @@ public sealed class SsReplayReader(Stream stream) : IDisposable
_binaryReader _binaryReader
.SkipInt32() .SkipInt32()
.NextAsciiStringMustEqual("GAMEINFO\0FOLDWMAN") .NextAsciiStringMustEqual("GAMEINFO\0FOLDWMAN"u8)
.SkipInt32(); .SkipInt32();
Descriptor.FoldWmanStart = _binaryReader.Position; // 182 Descriptor.FoldWmanStart = _binaryReader.Position; // 182
@ -118,7 +118,7 @@ public sealed class SsReplayReader(Stream stream) : IDisposable
_binaryReader _binaryReader
.SkipInt32() .SkipInt32()
.NextAsciiStringMustEqual("DATASDSC") .NextAsciiStringMustEqual("DATASDSC"u8)
.Skip(20); .Skip(20);
Replay.Map.MaxPlayersCount = _binaryReader.ReadInt32(); Replay.Map.MaxPlayersCount = _binaryReader.ReadInt32();
@ -144,7 +144,7 @@ public sealed class SsReplayReader(Stream stream) : IDisposable
_binaryReader _binaryReader
.SkipInt32(4) .SkipInt32(4)
.NextAsciiStringMustEqual("DATABASE") .NextAsciiStringMustEqual("DATABASE"u8)
.SkipInt32(); .SkipInt32();
Descriptor.BeginDataBaseChunkSize = _binaryReader.Position; Descriptor.BeginDataBaseChunkSize = _binaryReader.Position;
@ -252,7 +252,7 @@ public sealed class SsReplayReader(Stream stream) : IDisposable
private PlayerModel ReadPlayer() private PlayerModel ReadPlayer()
{ {
// В реплеях c ботами нет пустых мест // В реплеях c ботами нет пустых мест
if (!_binaryReader.IsNextAsciiStringEquals("FOLDGPLY")) if (!_binaryReader.IsNextAsciiStringEquals("FOLDGPLY"u8))
{ {
_binaryReader.Skip(-8); _binaryReader.Skip(-8);
return PlayerModel.Empty; return PlayerModel.Empty;
@ -263,7 +263,7 @@ public sealed class SsReplayReader(Stream stream) : IDisposable
_binaryReader _binaryReader
.Skip(12) // int32 version 2; int32 size; int32 strLen 0; .Skip(12) // int32 version 2; int32 size; int32 strLen 0;
.NextAsciiStringMustEqual("DATAINFO") .NextAsciiStringMustEqual("DATAINFO"u8)
.Skip(12); // int32 version 1; int32 size; int32 strLen 0; .Skip(12); // int32 version 1; int32 size; int32 strLen 0;
player.Name = _binaryReader.ReadNextUnicodeString(); player.Name = _binaryReader.ReadNextUnicodeString();
@ -279,11 +279,11 @@ public sealed class SsReplayReader(Stream stream) : IDisposable
if (!_binaryReader.HasBytes) if (!_binaryReader.HasBytes)
return player; return player;
if (_binaryReader.IsNextAsciiStringEquals("FOLDTCUC")) if (_binaryReader.IsNextAsciiStringEquals("FOLDTCUC"u8))
{ {
_binaryReader _binaryReader
.Skip(12) // int32 version 1; int32 size; int32 strLen 0; .Skip(12) // int32 version 1; int32 size; int32 strLen 0;
.NextAsciiStringMustEqual("DATALCIN") .NextAsciiStringMustEqual("DATALCIN"u8)
.Skip(4); // int32 version 2; .Skip(4); // int32 version 2;
_binaryReader.ReadNextAsciiString(); // описание раскрасок отправителя реплея? _binaryReader.ReadNextAsciiString(); // описание раскрасок отправителя реплея?
@ -292,7 +292,7 @@ public sealed class SsReplayReader(Stream stream) : IDisposable
_binaryReader _binaryReader
.Skip(4) // int32 strLen 0; .Skip(4) // int32 strLen 0;
.NextAsciiStringMustEqual("DATAUNCU") // 42192 .NextAsciiStringMustEqual("DATAUNCU"u8) // 42192
.Skip(12); // int32 version 1; int32 size; int32 strLen 0; .Skip(12); // int32 version 1; int32 size; int32 strLen 0;
player.ColorScheme = new ColorScheme player.ColorScheme = new ColorScheme
@ -338,13 +338,13 @@ public sealed class SsReplayReader(Stream stream) : IDisposable
} }
_binaryReader _binaryReader
.NextAsciiStringMustEqual("FOLDIMAG") .NextAsciiStringMustEqual("FOLDIMAG"u8)
.Skip(8); // int32 version 1; int32 size; .Skip(8); // int32 version 1; int32 size;
playerImage.Name = _binaryReader.ReadNextAsciiString(); playerImage.Name = _binaryReader.ReadNextAsciiString();
_binaryReader _binaryReader
.NextAsciiStringMustEqual("DATAATTR") .NextAsciiStringMustEqual("DATAATTR"u8)
.Skip(16); // int32 version 2; int32 size; 2 * int32 (?) 0; .Skip(16); // int32 version 2; int32 size; 2 * int32 (?) 0;
var width = _binaryReader.ReadInt32(); var width = _binaryReader.ReadInt32();
@ -352,7 +352,7 @@ public sealed class SsReplayReader(Stream stream) : IDisposable
_binaryReader _binaryReader
.Skip(4) // int32 (?) 1; .Skip(4) // int32 (?) 1;
.NextAsciiStringMustEqual("DATADATA") .NextAsciiStringMustEqual("DATADATA"u8)
.Skip(12); // int32 version 2; int32 size; int32 (?) 0; .Skip(12); // int32 version 2; int32 size; int32 (?) 0;
playerImage.Init(width, height); playerImage.Init(width, height);