2023-01-02 13:17:18 -06:00
|
|
|
#include "util/SFile.hpp"
|
|
|
|
|
#include <cstring>
|
|
|
|
|
#include <limits>
|
2024-02-12 02:19:18 +04:00
|
|
|
#include <StormLib.h>
|
2023-01-02 13:17:18 -06:00
|
|
|
#include <storm/Memory.hpp>
|
2023-01-03 00:45:25 -06:00
|
|
|
#include <storm/String.hpp>
|
2024-02-12 02:19:18 +04:00
|
|
|
#include "util/Filesystem.hpp"
|
2023-01-02 13:17:18 -06:00
|
|
|
|
2023-08-24 20:51:30 -04: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) {
|
2024-02-12 02:19:18 +04:00
|
|
|
SFileCloseFile(file->m_handle);
|
2023-01-02 13:17:18 -06:00
|
|
|
delete file;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO Proper implementation
|
2024-02-12 02:19:18 +04:00
|
|
|
uint32_t SFile::GetFileSize(SFile* file, uint32_t* filesizeHigh) {
|
|
|
|
|
DWORD high = 0;
|
|
|
|
|
DWORD low = SFileGetFileSize(file->m_handle, &high);
|
|
|
|
|
|
|
|
|
|
if (filesizeHigh) {
|
|
|
|
|
*filesizeHigh = high;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return low;
|
2023-01-02 13:17:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) {
|
2024-02-12 02:19:18 +04:00
|
|
|
if (!buffer || !filename) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*buffer = nullptr;
|
|
|
|
|
if (bytes) {
|
|
|
|
|
*bytes = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SFile* file = nullptr;
|
|
|
|
|
if (!SFile::OpenEx(nullptr, filename, 0, &file)) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2023-01-02 13:17:18 -06:00
|
|
|
|
2024-02-12 02:19:18 +04:00
|
|
|
uint32_t high = 0;
|
|
|
|
|
uint64_t size = SFile::GetFileSize(file, &high);
|
|
|
|
|
size |= ((uint64_t) high << 32);
|
2023-01-02 13:17:18 -06:00
|
|
|
|
2024-02-12 02:19:18 +04:00
|
|
|
auto data = reinterpret_cast<char*>(SMemAlloc(size + extraBytes, __FILE__, __LINE__, 0));
|
2023-01-02 13:17:18 -06:00
|
|
|
|
2024-02-12 02:19:18 +04:00
|
|
|
if (!SFile::Read(file, data, size, nullptr, nullptr, nullptr)) {
|
|
|
|
|
SMemFree(data, __FILE__, __LINE__, 0);
|
|
|
|
|
SFile::Close(file);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2023-01-02 13:17:18 -06:00
|
|
|
|
2024-02-12 02:19:18 +04:00
|
|
|
if (extraBytes) {
|
|
|
|
|
memset(data + size, 0, extraBytes);
|
|
|
|
|
}
|
2023-01-02 13:17:18 -06:00
|
|
|
|
2024-02-12 02:19:18 +04:00
|
|
|
if (bytes) {
|
|
|
|
|
*bytes = size;
|
|
|
|
|
}
|
2023-01-02 13:17:18 -06:00
|
|
|
|
2024-02-12 02:19:18 +04:00
|
|
|
if (buffer) {
|
2023-01-02 13:17:18 -06:00
|
|
|
*buffer = data;
|
|
|
|
|
}
|
2024-02-12 02:19:18 +04:00
|
|
|
|
|
|
|
|
SFile::Close(file);
|
|
|
|
|
|
|
|
|
|
return 1;
|
2023-01-02 13:17:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) {
|
2024-02-12 02:19:18 +04:00
|
|
|
if (!file || !filename) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2023-01-02 13:17:18 -06:00
|
|
|
|
2024-02-12 02:19:18 +04:00
|
|
|
char path[STORM_MAX_PATH];
|
2023-01-02 13:17:18 -06:00
|
|
|
|
2024-02-12 02:19:18 +04:00
|
|
|
// Overflow protection
|
|
|
|
|
if (SStrLen(filename) + 1 > STORM_MAX_PATH) {
|
2023-01-02 13:17:18 -06:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-12 02:19:18 +04:00
|
|
|
SStrCopy(path, filename, STORM_MAX_PATH);
|
|
|
|
|
OsFileToNativeSlashes(path);
|
2023-01-02 13:17:18 -06:00
|
|
|
|
2024-02-12 02:19:18 +04:00
|
|
|
HANDLE handle;
|
|
|
|
|
if (!SFileOpenFileEx(nullptr, path, SFILE_OPEN_LOCAL_FILE, &handle)) {
|
|
|
|
|
OsFileToBackSlashes(path);
|
|
|
|
|
if (!SFileOpenFileEx(g_mpqHandle, path, SFILE_OPEN_FROM_MPQ, &handle)) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-02 13:17:18 -06:00
|
|
|
|
2024-02-12 02:19:18 +04:00
|
|
|
*file = new SFile;
|
|
|
|
|
(*file)->m_handle = handle;
|
2023-01-02 13:17:18 -06:00
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO Proper implementation
|
|
|
|
|
int32_t SFile::Read(SFile* file, void* buffer, size_t bytestoread, size_t* bytesread, SOVERLAPPED* overlapped, TASYNCPARAMBLOCK* asyncparam) {
|
2024-02-12 02:19:18 +04:00
|
|
|
DWORD read = 0;
|
|
|
|
|
if (SFileReadFile(file->m_handle, buffer, static_cast<DWORD>(bytestoread), &read, nullptr)) {
|
|
|
|
|
if (bytesread) {
|
|
|
|
|
*bytesread = read;
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
} else {
|
|
|
|
|
if (bytesread) {
|
|
|
|
|
*bytesread = 0;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2023-01-02 13:17:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t SFile::Unload(void* ptr) {
|
|
|
|
|
SMemFree(ptr, __FILE__, __LINE__, 0);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2023-08-24 20:51:30 -04:00
|
|
|
|
|
|
|
|
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') {
|
2023-08-24 20:51:30 -04:00
|
|
|
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') {
|
2023-08-24 20:51:30 -04:00
|
|
|
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);
|
2023-08-24 22:40:26 -04:00
|
|
|
return 1;
|
2023-08-24 20:51:30 -04:00
|
|
|
}
|