feat(util): implement SFile::{FileExistsEx, FileExists, SetFilePointer}

This commit is contained in:
superp00t 2024-09-06 12:28:57 -04:00
parent 4c6f78eda4
commit e954521204
2 changed files with 52 additions and 0 deletions

View File

@ -59,6 +59,22 @@ uint32_t SFile::GetFileSize(SFile* file, uint32_t* filesizeHigh) {
return low;
}
// TODO: Proper implementation
int32_t SFile::FileExists(const char* filename) {
return SFile::FileExistsEx(filename, 0);
}
// TODO: Proper implementation
int32_t SFile::FileExistsEx(const char* filename, uint32_t a2) {
SFile* test_file;
if (!SFile::Open(filename, &test_file)) {
return 0;
}
SFile::Close(test_file);
return 1;
}
int32_t SFile::IsStreamingMode() {
// TODO
return 0;
@ -157,6 +173,39 @@ int32_t SFile::OpenEx(SArchive* archive, const char* filename, uint32_t flags, S
return 1;
}
uint32_t SFile::SetFilePointer(SFile* file, int32_t distancetomove, int32_t* distancetomovehigh, uint32_t movemethod) {
switch (file->m_type) {
case SFILE_PLAIN: {
auto stream = reinterpret_cast<Blizzard::File::StreamRecord*>(file->m_handle);
int32_t whence = movemethod;
if (whence != 0 && whence != 1) {
if (whence >= 2) {
whence = movemethod != 2 ? -1 : 2;
}
}
int64_t offset = distancetomove;
if (distancetomovehigh) {
offset |= *distancetomovehigh << 32;
}
int64_t pos;
Blizzard::File::SetPos(stream, offset, whence);
if (Blizzard::File::GetPos(stream, pos)) {
if (distancetomovehigh) {
*distancetomovehigh = (pos >> 32) & 0xFFFFFFFF;
}
return pos & 0xFFFFFFFF;
}
return 0xFFFFFFFF;
}
case SFILE_PAQ: {
return static_cast<uint32_t>(SFileSetFilePointer(file->m_handle, static_cast<LONG>(distancetomove), reinterpret_cast<LONG*>(distancetomovehigh), static_cast<DWORD>(movemethod)));
}
default:
STORM_ASSERT(0);
}
}
// TODO Proper implementation
int32_t SFile::Read(SFile* file, void* buffer, size_t bytestoread, size_t* bytesread, SOVERLAPPED* overlapped, TASYNCPARAMBLOCK* asyncparam) {
switch (file->m_type) {

View File

@ -26,10 +26,13 @@ class SFile {
// Static functions
static int32_t Close(SFile*);
static uint32_t GetFileSize(SFile*, uint32_t*);
static int32_t FileExists(const char* filename);
static int32_t FileExistsEx(const char* filename, uint32_t a2);
static int32_t IsStreamingMode(void);
static int32_t Load(SArchive*, const char*, void**, size_t*, size_t, uint32_t, SOVERLAPPED*);
static int32_t Open(const char*, SFile**);
static int32_t OpenEx(SArchive*, const char*, uint32_t, SFile**);
static uint32_t SetFilePointer(SFile* file, int32_t distancetomove, int32_t* distancetomovehigh, uint32_t movemethod);
static int32_t Read(SFile*, void*, size_t, size_t*, SOVERLAPPED*, TASYNCPARAMBLOCK*);
static int32_t Unload(void*);
static int32_t SetBasePath(const char* path);