@@ -496,8 +496,9 @@ FFileArchive::FFileArchive(const char *Filename, unsigned InOptions)
496
496
: Options(InOptions)
497
497
, f(NULL )
498
498
, Buffer(NULL )
499
- , BufferPos(0 )
500
499
, BufferSize(0 )
500
+ , BufferPos(0 )
501
+ , FilePos(0 )
501
502
{
502
503
// process the filename
503
504
FullName = appStrdup (Filename);
@@ -636,9 +637,15 @@ void FFileReader::Serialize(void *data, int size)
636
637
}
637
638
SeekPos = -1 ;
638
639
}
640
+ #if MAX_DEBUG
641
+ int tell = ftell (f);
642
+ if (tell != FilePos)
643
+ appError (" Bad FilePos!" );
644
+ #endif
639
645
if (size >= FILE_BUFFER_SIZE / 2 )
640
646
{
641
647
// Large block, read directly to destination skipping buffer
648
+ // appPrintf("read2: %d+%d -> %d\n", (int)FilePos, size, (int)FilePos + size);
642
649
int res = fread (data, size, 1 , f);
643
650
if (res != 1 )
644
651
appError (" Unable to read %d bytes at pos=0x%llX" , size, FilePos);
@@ -647,10 +654,16 @@ void FFileReader::Serialize(void *data, int size)
647
654
GSerializeBytes += size;
648
655
#endif
649
656
FilePos += size;
657
+ BufferPos = FilePos;
658
+ // Invalidate buffer
659
+ BufferSize = 0 ;
660
+ BufferBytesLeft = 0 ;
661
+ LocalReadPos = 0 ;
650
662
return ;
651
663
}
652
664
// Fill buffer
653
665
int ReadBytes = fread (Buffer, 1 , FILE_BUFFER_SIZE, f);
666
+ // appPrintf("read: %d+%d -> %d\n", (int)FilePos, ReadBytes, (int)FilePos + ReadBytes);
654
667
if (ReadBytes == 0 )
655
668
appError (" Unable to read %d bytes at pos=0x%llX" , 1 , FilePos);
656
669
#if PROFILE
@@ -680,14 +693,17 @@ void FFileReader::Seek(int Pos)
680
693
681
694
void FFileReader::Seek64 (int64 Pos)
682
695
{
696
+ // appPrintf("seek: %d\n", (int)Pos);
683
697
// Check for buffer validity
684
698
int64 LocalPos64 = Pos - BufferPos;
685
699
if (LocalPos64 < 0 || LocalPos64 >= BufferSize)
686
700
{
687
- // Outside of the buffer
701
+ // Outside of the current buffer, invalidate it
702
+ BufferSize = 0 ;
688
703
BufferBytesLeft = 0 ;
704
+ LocalReadPos = 0 ;
689
705
// SeekPos will be reset to -1 after actual seek
690
- SeekPos = Pos;
706
+ BufferPos = SeekPos = Pos;
691
707
}
692
708
else
693
709
{
@@ -717,6 +733,7 @@ int64 FFileReader::GetFileSize64() const
717
733
FFileReader* _this = const_cast <FFileReader*>(this );
718
734
// don't rewind file back
719
735
_this->FilePos = _this->FileSize = _filelengthi64 (fileno (f));
736
+ _this->SeekPos = 0 ;
720
737
#else
721
738
fseeko64 (f, 0 , SEEK_END);
722
739
FFileReader* _this = const_cast <FFileReader*>(this );
0 commit comments