thunderbrew/src/util/SFile.cpp

153 lines
3.7 KiB
C++
Raw Normal View History

2023-01-02 13:17:18 -06:00
#include "util/SFile.hpp"
#include <cstring>
#include <limits>
#include <storm/Memory.hpp>
2023-01-03 00:45:25 -06:00
#include <storm/String.hpp>
#include <bc/file/File.hpp>
2023-01-02 13:17:18 -06:00
static char s_basepath[STORM_MAX_PATH] = {0};
static char s_datapath[STORM_MAX_PATH] = {0};
2023-01-02 13:17:18 -06:00
// TODO Proper implementation
int32_t SFile::Close(SFile* file) {
delete file->m_filename;
Blizzard::File::Close(file->m_stream);
2023-01-02 13:17:18 -06:00
delete file;
return 1;
}
// TODO Proper implementation
size_t SFile::GetFileSize(SFile* file, size_t* filesizeHigh) {
return file->m_size;
}
int32_t SFile::IsStreamingMode() {
// TODO
return 0;
}
// TODO Proper implementation
int32_t SFile::Load(SArchive* archive, const char* filename, void** buffer, size_t* bytes, size_t extraBytes, uint32_t flags, SOVERLAPPED* overlapped) {
2023-01-03 00:45:25 -06:00
auto pathLen = SStrLen(filename);
char path[STORM_MAX_PATH];
SStrCopy(path, filename, sizeof(path));
2023-01-02 13:17:18 -06:00
uint32_t openflags = BC_FILE_OPEN_MUST_EXIST | BC_FILE_OPEN_SHARE_READ | BC_FILE_OPEN_READ;
Blizzard::File::StreamRecord* stream;
bool opened = Blizzard::File::Open(path, openflags, stream);
2023-01-02 13:17:18 -06:00
size_t size;
char* data;
if (opened) {
size = static_cast<size_t>(Blizzard::File::GetFileInfo(stream)->size);
2023-01-02 13:17:18 -06:00
if (bytes) {
*bytes = size;
}
data = new char[size + extraBytes];
Blizzard::File::SetPos(stream, 0, BC_FILE_SEEK_START);
Blizzard::File::Read(stream, data, size, nullptr);
Blizzard::File::Close(stream);
2023-01-02 13:17:18 -06:00
if (extraBytes) {
memset(data + size, 0, extraBytes);
}
*buffer = data;
return 1;
} else {
return 0;
}
}
int32_t SFile::Open(const char* filename, SFile** file) {
return SFile::OpenEx(nullptr, filename, 0, file);
}
// TODO Proper implementation
int32_t SFile::OpenEx(SArchive* archive, const char* filename, uint32_t flags, SFile** file) {
2023-01-03 00:45:25 -06:00
auto pathLen = SStrLen(filename);
char path[STORM_MAX_PATH];
SStrCopy(path, filename, sizeof(path));
2023-01-02 13:17:18 -06:00
SFile* fileptr = new SFile;
fileptr->m_filename = strdup(filename);
uint32_t openflags = BC_FILE_OPEN_MUST_EXIST | BC_FILE_OPEN_SHARE_READ | BC_FILE_OPEN_READ;
2023-01-02 13:17:18 -06:00
Blizzard::File::StreamRecord* stream;
auto opened = Blizzard::File::Open(fileptr->m_filename, openflags, stream);
if (!opened) {
*file = nullptr;
2023-01-02 13:17:18 -06:00
return 0;
}
auto fileinfo = Blizzard::File::GetFileInfo(stream);
2023-01-02 13:17:18 -06:00
fileptr->m_stream = stream;
fileptr->m_size = fileinfo->size;
2023-01-02 13:17:18 -06:00
*file = fileptr;
return 1;
}
// TODO Proper implementation
int32_t SFile::Read(SFile* file, void* buffer, size_t bytestoread, size_t* bytesread, SOVERLAPPED* overlapped, TASYNCPARAMBLOCK* asyncparam) {
2023-12-04 19:09:40 -05:00
Blizzard::File::Read(file->m_stream, buffer, bytestoread, bytesread);
2023-01-02 13:17:18 -06:00
return 1;
}
int32_t SFile::Unload(void* ptr) {
SMemFree(ptr, __FILE__, __LINE__, 0);
return 1;
}
int32_t SFile::SetBasePath(const char* path) {
SStrCopy(s_basepath, path, STORM_MAX_PATH);
2023-08-24 21:25:02 -04:00
if (*s_basepath != '\0') {
auto len = SStrLen(s_basepath);
if (s_basepath[len-1] != '\\') {
SStrPack(s_basepath, "\\", STORM_MAX_PATH);
}
}
// TODO
// SFileSetBasePath(path);
return 1;
}
int32_t SFile::SetDataPath(const char* path) {
SStrCopy(s_datapath, path, STORM_MAX_PATH);
2023-08-24 21:25:02 -04:00
if (*s_datapath != '\0') {
auto len = SStrLen(s_datapath);
if (s_basepath[len-1] != '\\') {
SStrPack(s_datapath, "\\", STORM_MAX_PATH);
}
}
return 1;
}
int32_t SFile::GetBasePath(char* buffer, size_t bufferchars) {
SStrCopy(buffer, s_basepath, bufferchars);
return 1;
}
int32_t SFile::GetDataPath(char* buffer, size_t bufferchars) {
SStrCopy(buffer, s_datapath, bufferchars);
return 1;
}