feat(util): update SFile to work with StormLib

This commit is contained in:
VDm 2023-04-08 16:08:07 +04:00
parent 1d92f91aee
commit bbafb2adb5
2 changed files with 56 additions and 56 deletions

View File

@ -1,24 +1,26 @@
#include "util/SFile.hpp" #include "util/SFile.hpp"
#include <cstring> #include <cstring>
#include <limits> #include <limits>
#include <StormLib.h>
#include <storm/Memory.hpp> #include <storm/Memory.hpp>
#include <storm/String.hpp> #include <storm/String.hpp>
#include "util/Filesystem.hpp"
// TODO Proper implementation // TODO Proper implementation
int32_t SFile::Close(SFile* file) { int32_t SFile::Close(SFile* file) {
delete file->m_filename; SFileCloseFile(file->m_file);
file->m_stream->close();
delete file->m_stream;
delete file; delete file;
return 1; return 1;
} }
// TODO Proper implementation // TODO Proper implementation
size_t SFile::GetFileSize(SFile* file, size_t* filesizeHigh) { size_t SFile::GetFileSize(SFile* file, size_t* filesizeHigh) {
return file->m_size; DWORD highPart = 0;
DWORD lowPart = SFileGetFileSize(file->m_file, &highPart);
if (filesizeHigh)
*filesizeHigh = highPart;
return lowPart;
} }
int32_t SFile::IsStreamingMode() { int32_t SFile::IsStreamingMode() {
@ -32,39 +34,42 @@ 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) { for (int32_t i = 0; i < pathLen; ++i) {
if (path[i] == '\\') { if (path[i] == '\\') {
path[i] = '/'; path[i] = '/';
} }
} }
std::ifstream file (path, std::ios::in | std::ios::binary | std::ios::ate); */
size_t size;
char* data;
if (file.is_open()) { HANDLE file;
size = static_cast<size_t>(file.tellg()); if (!SFileOpenFileEx(nullptr, path, SFILE_OPEN_LOCAL_FILE, &file)) {
if (!SFileOpenFileEx(g_mpqHandle, path, SFILE_OPEN_FROM_MPQ, &file))
return 0;
}
if (bytes) { DWORD highPart = 0;
*bytes = size; size_t size = SFileGetFileSize(file, &highPart);
} size |= (highPart << 32);
data = new char[size + extraBytes]; if (bytes)
*bytes = size;
file.seekg(0, std::ios::beg); char* data = (char*) SMemAlloc(size + extraBytes, __FILE__, __LINE__, 0);
file.read(data, size);
file.close();
if (extraBytes) { SFileReadFile(file, data, size, &highPart, nullptr);
memset(data + size, 0, extraBytes);
}
if (extraBytes)
memset(data + size, 0, extraBytes);
if (buffer)
*buffer = data; *buffer = data;
return 1; SFileCloseFile(file);
} else {
return 0; return 1;
}
} }
int32_t SFile::Open(const char* filename, SFile** file) { int32_t SFile::Open(const char* filename, SFile** file) {
@ -77,34 +82,29 @@ 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) { for (int32_t i = 0; i < pathLen; ++i) {
if (path[i] == '\\') { if (path[i] == '\\') {
path[i] = '/'; path[i] = '/';
} }
} }
SFile* fileptr = new SFile; */
fileptr->m_filename = strdup(filename); HANDLE handle;
bool local = true;
std::ifstream* stream = new std::ifstream(path, std::ios::in | std::ios::binary | std::ios::ate); if (!SFileOpenFileEx(nullptr, path, SFILE_OPEN_LOCAL_FILE, &handle)) {
local = false;
if (!stream->is_open()) { if (!SFileOpenFileEx(g_mpqHandle, path, SFILE_OPEN_FROM_MPQ, &handle)) {
*file = nullptr; *file = nullptr;
return 0; return 0;
}
} }
stream->seekg(0, std::ios::beg); SFile* fileptr = new SFile;
fileptr->m_mpq = local ? nullptr : g_mpqHandle;
stream->ignore(std::numeric_limits<std::streamsize>::max()); fileptr->m_file = handle;
std::streamsize size = stream->gcount();
stream->clear();
stream->seekg(0, std::ios::beg);
fileptr->m_stream = stream;
fileptr->m_size = size;
*file = fileptr; *file = fileptr;
return 1; return 1;
@ -112,13 +112,16 @@ 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); DWORD read = 0;
if (SFileReadFile(file->m_file, buffer, bytestoread, &read, nullptr)) {
if (bytesread) { if (bytesread)
*bytesread = file->m_stream->gcount(); *bytesread = read;
return 1;
} else {
if (bytesread)
*bytesread = 0;
return 0;
} }
return 1;
} }
int32_t SFile::Unload(void* ptr) { int32_t SFile::Unload(void* ptr) {

View File

@ -3,8 +3,6 @@
#include <cstdint> #include <cstdint>
#include <cstdlib> #include <cstdlib>
#include <fstream>
#include <iostream>
class SArchive; class SArchive;
struct SOVERLAPPED; struct SOVERLAPPED;
@ -23,9 +21,8 @@ class SFile {
static int32_t Unload(void*); static int32_t Unload(void*);
// Member variables // Member variables
const char* m_filename; void* m_mpq;
std::ifstream* m_stream; // TODO Proper implementation void* m_file;
std::streamsize m_size; // TODO Proper implementation
}; };
#endif #endif