2021-10-22 15:50:51 +02:00
|
|
|
#include "Logger.h"
|
|
|
|
|
#include "CustompacketChunk.h"
|
2021-10-23 12:05:48 +02:00
|
|
|
#include "ClientMacros.h"
|
2021-10-22 15:50:51 +02:00
|
|
|
|
|
|
|
|
#include "windows.h"
|
|
|
|
|
|
2021-10-24 12:23:43 +02:00
|
|
|
CLIENT_FUNCTION(
|
2021-10-24 11:56:58 +02:00
|
|
|
SFileOpenFile
|
2021-10-23 12:09:38 +02:00
|
|
|
, 0x00424F80
|
2021-10-24 11:56:58 +02:00
|
|
|
, __stdcall
|
2021-10-23 12:09:38 +02:00
|
|
|
, int
|
|
|
|
|
, (
|
|
|
|
|
char const* filename
|
|
|
|
|
, HANDLE* a2 /*file handle out*/
|
|
|
|
|
)
|
2021-10-22 15:50:51 +02:00
|
|
|
)
|
|
|
|
|
|
2021-10-24 12:23:43 +02:00
|
|
|
CLIENT_FUNCTION(
|
2021-10-24 11:56:58 +02:00
|
|
|
SFileGetFileSize
|
2021-10-23 12:09:38 +02:00
|
|
|
, 0x004218C0
|
2021-10-24 11:56:58 +02:00
|
|
|
, __stdcall
|
2021-10-23 12:09:38 +02:00
|
|
|
, DWORD /*lowest 32 bits in size*/
|
|
|
|
|
, (
|
|
|
|
|
HANDLE handle
|
|
|
|
|
, DWORD* highSize /*highest 32 bits in size*/
|
|
|
|
|
)
|
2021-10-22 15:50:51 +02:00
|
|
|
)
|
|
|
|
|
|
2021-10-24 12:23:43 +02:00
|
|
|
CLIENT_FUNCTION(
|
2021-10-23 12:09:38 +02:00
|
|
|
SFileReadFile
|
|
|
|
|
, 0x00422530
|
2021-10-24 11:56:58 +02:00
|
|
|
, __stdcall
|
2021-10-23 12:09:38 +02:00
|
|
|
, int
|
|
|
|
|
, (
|
|
|
|
|
HANDLE handle // likely a handle
|
|
|
|
|
, char* data
|
|
|
|
|
, DWORD bytesToRead
|
|
|
|
|
, DWORD* bytesRead
|
|
|
|
|
, int overlap // just set to 0
|
|
|
|
|
, int unk // just set to 0
|
|
|
|
|
)
|
2021-10-22 15:50:51 +02:00
|
|
|
)
|
|
|
|
|
|
2021-10-24 12:23:43 +02:00
|
|
|
CLIENT_FUNCTION(
|
2021-10-23 12:09:38 +02:00
|
|
|
SFileCloseFile
|
|
|
|
|
, 0x00422910
|
2021-10-24 11:56:58 +02:00
|
|
|
, __stdcall
|
2021-10-23 12:09:38 +02:00
|
|
|
, int
|
|
|
|
|
, (
|
|
|
|
|
HANDLE a1
|
|
|
|
|
)
|
2021-10-22 15:50:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
namespace ClientMPQ {
|
2021-10-23 12:09:38 +02:00
|
|
|
// todo: it looks like normal GetLastError isn't working here,
|
|
|
|
|
// so we need to find the address the client uses. Until then,
|
2021-10-24 11:47:49 +02:00
|
|
|
// we cannot specify exactly what read errors have occurred if we get them.
|
2021-10-22 15:50:51 +02:00
|
|
|
|
2021-10-23 12:09:38 +02:00
|
|
|
size_t readFile(std::string const& filename, char** buf)
|
|
|
|
|
{
|
|
|
|
|
HANDLE handle = nullptr;
|
|
|
|
|
bool res = SFileOpenFile(filename.c_str(), &handle);
|
|
|
|
|
if (!res)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
DWORD high = 0;
|
|
|
|
|
DWORD low = SFileGetFileSize(handle, &high);
|
|
|
|
|
if (high > 0)
|
|
|
|
|
{
|
|
|
|
|
// such large files cannot be read into memory in one go
|
|
|
|
|
LOG_ERROR << "File is too large to be read:" << filename;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
char* data = new char[low];
|
|
|
|
|
DWORD read = 0;
|
|
|
|
|
int readRes = SFileReadFile(handle, data, low, &read, 0, 0);
|
|
|
|
|
if (!readRes)
|
|
|
|
|
{
|
|
|
|
|
LOG_ERROR << "Unknown read error: " << filename;
|
|
|
|
|
delete[] data;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
SFileCloseFile(handle);
|
|
|
|
|
*buf = data;
|
|
|
|
|
return read;
|
|
|
|
|
}
|
2021-10-22 15:50:51 +02:00
|
|
|
|
2021-10-23 12:09:38 +02:00
|
|
|
std::string readFile(std::string const& filename)
|
|
|
|
|
{
|
|
|
|
|
char* buf;
|
|
|
|
|
size_t size = readFile(filename, &buf);
|
|
|
|
|
if (size == 0)
|
|
|
|
|
{
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
std::string s(buf, size);
|
|
|
|
|
delete[] buf;
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-22 15:50:51 +02:00
|
|
|
}
|