feat(util): SFile functions now use the BlizzardCore filesystem utilities

This commit is contained in:
superp00t 2023-12-04 18:54:37 -05:00
parent 5eff48097f
commit 0302864f8d
2 changed files with 21 additions and 37 deletions

View File

@ -3,6 +3,7 @@
#include <limits> #include <limits>
#include <storm/Memory.hpp> #include <storm/Memory.hpp>
#include <storm/String.hpp> #include <storm/String.hpp>
#include <bc/file/File.hpp>
static char s_basepath[STORM_MAX_PATH] = {0}; static char s_basepath[STORM_MAX_PATH] = {0};
static char s_datapath[STORM_MAX_PATH] = {0}; static char s_datapath[STORM_MAX_PATH] = {0};
@ -11,8 +12,7 @@ static char s_datapath[STORM_MAX_PATH] = {0};
int32_t SFile::Close(SFile* file) { int32_t SFile::Close(SFile* file) {
delete file->m_filename; delete file->m_filename;
file->m_stream->close(); Blizzard::File::Close(file->m_stream);
delete file->m_stream;
delete file; delete file;
@ -35,18 +35,14 @@ int32_t SFile::Load(SArchive* archive, const char* filename, void** buffer, size
char path[STORM_MAX_PATH]; char path[STORM_MAX_PATH];
SStrCopy(path, filename, sizeof(path)); SStrCopy(path, filename, sizeof(path));
for (int32_t i = 0; i < pathLen; ++i) { uint32_t openflags = BC_FILE_OPEN_MUST_EXIST | BC_FILE_OPEN_SHARE_READ | BC_FILE_OPEN_READ;
if (path[i] == '\\') { Blizzard::File::StreamRecord* stream;
path[i] = '/'; bool opened = Blizzard::File::Open(path, openflags, stream);
}
}
std::ifstream file (path, std::ios::in | std::ios::binary | std::ios::ate);
size_t size; size_t size;
char* data; char* data;
if (file.is_open()) { if (opened) {
size = static_cast<size_t>(file.tellg()); size = static_cast<size_t>(Blizzard::File::GetFileInfo(stream)->size);
if (bytes) { if (bytes) {
*bytes = size; *bytes = size;
@ -54,9 +50,9 @@ int32_t SFile::Load(SArchive* archive, const char* filename, void** buffer, size
data = new char[size + extraBytes]; data = new char[size + extraBytes];
file.seekg(0, std::ios::beg); Blizzard::File::SetPos(stream, 0, BC_FILE_SEEK_START);
file.read(data, size); Blizzard::File::Read(stream, data, size, nullptr);
file.close(); Blizzard::File::Close(stream);
if (extraBytes) { if (extraBytes) {
memset(data + size, 0, extraBytes); memset(data + size, 0, extraBytes);
@ -80,33 +76,23 @@ int32_t SFile::OpenEx(SArchive* archive, const char* filename, uint32_t flags, S
char path[STORM_MAX_PATH]; char path[STORM_MAX_PATH];
SStrCopy(path, filename, sizeof(path)); SStrCopy(path, filename, sizeof(path));
for (int32_t i = 0; i < pathLen; ++i) {
if (path[i] == '\\') {
path[i] = '/';
}
}
SFile* fileptr = new SFile; SFile* fileptr = new SFile;
fileptr->m_filename = strdup(filename); fileptr->m_filename = strdup(filename);
std::ifstream* stream = new std::ifstream(path, std::ios::in | std::ios::binary | std::ios::ate); uint32_t openflags = BC_FILE_OPEN_MUST_EXIST | BC_FILE_OPEN_SHARE_READ | BC_FILE_OPEN_READ;
if (!stream->is_open()) { Blizzard::File::StreamRecord* stream;
auto opened = Blizzard::File::Open(fileptr->m_filename, openflags, stream);
if (!opened) {
*file = nullptr; *file = nullptr;
return 0; return 0;
} }
stream->seekg(0, std::ios::beg); auto fileinfo = Blizzard::File::GetFileInfo(stream);
stream->ignore(std::numeric_limits<std::streamsize>::max());
std::streamsize size = stream->gcount();
stream->clear();
stream->seekg(0, std::ios::beg);
fileptr->m_stream = stream; fileptr->m_stream = stream;
fileptr->m_size = size; fileptr->m_size = fileinfo->size;
*file = fileptr; *file = fileptr;
@ -115,11 +101,7 @@ int32_t SFile::OpenEx(SArchive* archive, const char* filename, uint32_t flags, S
// TODO Proper implementation // TODO Proper implementation
int32_t SFile::Read(SFile* file, void* buffer, size_t bytestoread, size_t* bytesread, SOVERLAPPED* overlapped, TASYNCPARAMBLOCK* asyncparam) { int32_t SFile::Read(SFile* file, void* buffer, size_t bytestoread, size_t* bytesread, SOVERLAPPED* overlapped, TASYNCPARAMBLOCK* asyncparam) {
file->m_stream->read((char*)buffer, bytestoread); Blizzard::File::Read(file->m_stream, buffer, bytestoread, bytesRead);
if (bytesread) {
*bytesread = file->m_stream->gcount();
}
return 1; return 1;
} }

View File

@ -6,6 +6,8 @@
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <bc/file/File.hpp>
class SArchive; class SArchive;
struct SOVERLAPPED; struct SOVERLAPPED;
struct TASYNCPARAMBLOCK; struct TASYNCPARAMBLOCK;
@ -28,8 +30,8 @@ class SFile {
// Member variables // Member variables
const char* m_filename; const char* m_filename;
std::ifstream* m_stream; // TODO Proper implementation Blizzard::File::StreamRecord* m_stream; // TODO Proper implementation
std::streamsize m_size; // TODO Proper implementation uint64_t m_size; // TODO Proper implementation
}; };
#endif #endif