Skip to content

Commit d832d96

Browse files
committed
fix SlicedReader.Align
1 parent d4c1e11 commit d832d96

4 files changed

Lines changed: 23 additions & 34 deletions

File tree

UnityAsset.NET.Variant.SourceGenerator/UnityAsset.NET.Variant.SourceGenerator.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<IsRoslynAnalyzer>true</IsRoslynAnalyzer>
77
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
88
<IncludeBuildOutput>false</IncludeBuildOutput>
9+
<IsPackable>false</IsPackable>
910
</PropertyGroup>
1011

1112
<ItemGroup>

UnityAsset.NET/IO/Reader/BlockReader.cs

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ public class BlockReader : IReader
5656
public readonly IVirtualFile File;
5757
private byte[] _buffer = [];
5858
private ulong _posOffset;
59-
private ulong _bufferPos;
6059
private ulong _bufferSize;
6160
private int _currentBlockIndex = -1;
62-
public readonly UnityCN? _unityCnInfo;
61+
private readonly UnityCN? _unityCnInfo;
6362

64-
private ulong BufferRemaining => _bufferSize - _bufferPos;
63+
private ulong BufferPos => (ulong)Position - _posOffset;
64+
private ulong BufferRemaining => _bufferSize - BufferPos;
6565

6666
#region Cache
6767

@@ -253,7 +253,6 @@ private void EnsureBlockLoaded(long position)
253253
_bufferSize = block.UncompressedSize;
254254
_posOffset = block.UncompressedOffset;
255255
}
256-
_bufferPos = (ulong)position - _posOffset;
257256
}
258257

259258
# region IReader
@@ -262,8 +261,10 @@ private void EnsureBlockLoaded(long position)
262261

263262
public byte ReadByte()
264263
{
265-
EnsureBlockLoaded(Position++);
266-
return _buffer[_bufferPos++];
264+
EnsureBlockLoaded(Position);
265+
var ret = _buffer[BufferPos];
266+
Position++;
267+
return ret;
267268
}
268269

269270
public byte[] ReadBytes(int count)
@@ -288,10 +289,9 @@ public void ReadExactly(Span<byte> buffer)
288289
var available = (int)BufferRemaining;
289290
var toCopy = Math.Min(buffer.Length - written, available);
290291

291-
_buffer.AsSpan((int)_bufferPos, toCopy)
292+
_buffer.AsSpan((int)BufferPos, toCopy)
292293
.CopyTo(buffer.Slice(written, toCopy));
293294

294-
_bufferPos += (uint)toCopy;
295295
Position += (uint)toCopy;
296296
written += toCopy;
297297
}
@@ -301,8 +301,7 @@ public short ReadInt16()
301301
EnsureBlockLoaded(Position);
302302
if (BufferRemaining >= 2)
303303
{
304-
var span = _buffer.AsSpan((int)_bufferPos, 2);
305-
_bufferPos += 2;
304+
var span = _buffer.AsSpan((int)BufferPos, 2);
306305
Position += 2;
307306

308307
return Endian == Endianness.BigEndian
@@ -321,8 +320,7 @@ public ushort ReadUInt16()
321320
EnsureBlockLoaded(Position);
322321
if (BufferRemaining >= 2)
323322
{
324-
var span = _buffer.AsSpan((int)_bufferPos, 2);
325-
_bufferPos += 2;
323+
var span = _buffer.AsSpan((int)BufferPos, 2);
326324
Position += 2;
327325

328326
return Endian == Endianness.BigEndian
@@ -341,8 +339,7 @@ public int ReadInt32()
341339
EnsureBlockLoaded(Position);
342340
if (BufferRemaining >= 4)
343341
{
344-
var span = _buffer.AsSpan((int)_bufferPos, 4);
345-
_bufferPos += 4;
342+
var span = _buffer.AsSpan((int)BufferPos, 4);
346343
Position += 4;
347344

348345
return Endian == Endianness.BigEndian
@@ -361,8 +358,7 @@ public uint ReadUInt32()
361358
EnsureBlockLoaded(Position);
362359
if (BufferRemaining >= 4)
363360
{
364-
var span = _buffer.AsSpan((int)_bufferPos, 4);
365-
_bufferPos += 4;
361+
var span = _buffer.AsSpan((int)BufferPos, 4);
366362
Position += 4;
367363

368364
return Endian == Endianness.BigEndian
@@ -381,8 +377,7 @@ public long ReadInt64()
381377
EnsureBlockLoaded(Position);
382378
if (BufferRemaining >= 8)
383379
{
384-
var span = _buffer.AsSpan((int)_bufferPos, 8);
385-
_bufferPos += 8;
380+
var span = _buffer.AsSpan((int)BufferPos, 8);
386381
Position += 8;
387382

388383
return Endian == Endianness.BigEndian
@@ -401,8 +396,7 @@ public ulong ReadUInt64()
401396
EnsureBlockLoaded(Position);
402397
if (BufferRemaining >= 8)
403398
{
404-
var span = _buffer.AsSpan((int)_bufferPos, 8);
405-
_bufferPos += 8;
399+
var span = _buffer.AsSpan((int)BufferPos, 8);
406400
Position += 8;
407401

408402
return Endian == Endianness.BigEndian
@@ -421,8 +415,7 @@ public float ReadSingle()
421415
EnsureBlockLoaded(Position);
422416
if (BufferRemaining >= 4)
423417
{
424-
var span = _buffer.AsSpan((int)_bufferPos, 4);
425-
_bufferPos += 4;
418+
var span = _buffer.AsSpan((int)BufferPos, 4);
426419
Position += 4;
427420

428421
return Endian == Endianness.BigEndian
@@ -441,8 +434,7 @@ public double ReadDouble()
441434
EnsureBlockLoaded(Position);
442435
if (BufferRemaining >= 8)
443436
{
444-
var span = _buffer.AsSpan((int)_bufferPos, 8);
445-
_bufferPos += 8;
437+
var span = _buffer.AsSpan((int)BufferPos, 8);
446438
Position += 8;
447439

448440
return Endian == Endianness.BigEndian
@@ -460,28 +452,27 @@ public string ReadNullTerminatedString()
460452
{
461453
EnsureBlockLoaded(Position);
462454

463-
var currentBlockRemaining = (int)(_bufferSize - _bufferPos);
464-
var span = _buffer.AsSpan((int)_bufferPos, currentBlockRemaining);
455+
var currentBlockRemaining = (int)BufferRemaining;
456+
var span = _buffer.AsSpan((int)BufferPos, currentBlockRemaining);
465457

466458
int nullIndex = span.IndexOf((byte)0);
467459

468460
if (nullIndex >= 0)
469461
{
470462
string result = Encoding.UTF8.GetString(span.Slice(0, nullIndex));
471463
var advance = nullIndex + 1;
472-
_bufferPos += (uint)advance;
473464
Position += advance;
474465
return result;
475466
}
476467

477468
using var ms = new MemoryStream();
478469
while (true)
479470
{
480-
if (_bufferPos >= _bufferSize)
471+
if (BufferPos >= _bufferSize)
481472
{
482473
EnsureBlockLoaded(Position);
483474
}
484-
byte b = _buffer[_bufferPos++];
475+
byte b = _buffer[BufferPos];
485476
Position++;
486477
if (b == 0)
487478
break;

UnityAsset.NET/IO/Reader/SlicedReader.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ public Endianness Endian
2222
set => BaseReader.Endian = value;
2323
}
2424

25-
void IReader.Align(uint alignment)
26-
{
27-
BaseReader.Align(alignment);
28-
}
2925
# endregion
3026

3127

UnityAsset.NET/UnityAsset.NET.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@
5858
<ItemGroup>
5959
<ProjectReference Include="..\UnityAsset.NET.Variant.SourceGenerator\UnityAsset.NET.Variant.SourceGenerator.csproj"
6060
OutputItemType="Analyzer"
61-
ReferenceOutputAssembly="false" />
61+
ReferenceOutputAssembly="false"
62+
PrivateAssets="all"/>
6263
</ItemGroup>
6364

6465
</Project>

0 commit comments

Comments
 (0)