iw4x-client/src/Components/Modules/RawFiles.cpp

103 lines
2.8 KiB
C++
Raw Normal View History

2022-02-27 07:53:44 -05:00
#include <STDInclude.hpp>
2017-01-19 16:23:59 -05:00
namespace Components
{
2022-07-07 09:32:18 -04:00
char* RawFiles::ReadRawFile(const char* filename, char* buf, int size)
2017-01-19 16:23:59 -05:00
{
2022-07-07 09:32:18 -04:00
auto fileHandle = 0;
auto fileSize = Game::FS_FOpenFileRead(filename, &fileHandle);
if (fileHandle != 0)
{
if ((fileSize + 1) <= size)
{
Game::FS_Read(buf, fileSize, fileHandle);
buf[fileSize] = 0;
Game::FS_FCloseFile(fileHandle);
return buf;
}
Game::FS_FCloseFile(fileHandle);
Logger::PrintError(Game::CON_CHANNEL_ERROR, "Ignoring raw file '{}' as it exceeds buffer size {} > {}\n", filename, fileSize, size);
}
auto* rawfile = Game::DB_FindXAssetHeader(Game::ASSET_TYPE_RAWFILE, filename).rawfile;
if (Game::DB_IsXAssetDefault(Game::ASSET_TYPE_RAWFILE, filename))
{
return nullptr;
}
Game::DB_GetRawBuffer(rawfile, buf, size);
return buf;
2017-01-19 16:23:59 -05:00
}
2022-07-07 09:32:18 -04:00
char* RawFiles::GetMenuBuffer(const char* filename)
2017-01-19 16:23:59 -05:00
{
2022-07-07 09:32:18 -04:00
auto fileHandle = 0;
auto fileSize = Game::FS_FOpenFileRead(filename, &fileHandle);
2017-01-19 16:23:59 -05:00
2022-07-07 09:32:18 -04:00
if (fileHandle != 0)
{
if (fileSize < 0x8000)
{
auto* buffer = static_cast<char*>(Game::Z_VirtualAlloc(fileSize + 1));
Game::FS_Read(buffer, fileSize, fileHandle);
Game::FS_FCloseFile(fileHandle);
return buffer;
}
Game::FS_FCloseFile(fileHandle);
Logger::PrintError(Game::CON_CHANNEL_ERROR, "Menu file too large: {} is {}, max allowed is {}\n", filename, fileSize, 0x8000);
}
auto* rawfile = Game::DB_FindXAssetHeader(Game::ASSET_TYPE_RAWFILE, filename).rawfile;
if (Game::DB_IsXAssetDefault(Game::ASSET_TYPE_RAWFILE, filename))
{
Logger::PrintError(Game::CON_CHANNEL_ERROR, "Menu file not found: {}, using default\n", filename);
return nullptr;
}
auto* buffer = static_cast<char*>(Game::Z_VirtualAlloc(rawfile->len + 1));
Game::DB_GetRawBuffer(rawfile, buffer, rawfile->len + 1);
return buffer;
}
RawFiles::RawFiles()
{
2017-01-19 16:23:59 -05:00
// remove fs_game check for moddable rawfiles - allows non-fs_game to modify rawfiles
Utils::Hook::Nop(0x61AB76, 2);
2022-07-07 09:32:18 -04:00
Utils::Hook(0x4DA0D0, ReadRawFile, HOOK_JUMP).install()->quick();
Utils::Hook(0x631640, GetMenuBuffer, HOOK_JUMP).install()->quick();
Command::Add("dumpraw", [](Command::Params* params)
2017-01-19 16:23:59 -05:00
{
2022-03-17 14:50:20 -04:00
if (params->size() < 2)
2017-01-19 16:23:59 -05:00
{
Logger::Print("Specify a filename!\n");
return;
}
FileSystem::File file(params->join(1));
if (file.exists())
{
Utils::IO::WriteFile("raw/" + file.getName(), file.getBuffer());
2022-06-12 17:07:53 -04:00
Logger::Print("File '{}' written to raw!\n", file.getName());
2017-01-19 16:23:59 -05:00
return;
}
2022-07-07 09:32:18 -04:00
const char* data = Game::Scr_AddSourceBuffer(nullptr, file.getName().data(), nullptr, false);
2017-01-19 16:23:59 -05:00
2022-07-07 09:32:18 -04:00
if (data != nullptr)
2017-01-19 16:23:59 -05:00
{
Utils::IO::WriteFile("raw/" + file.getName(), data);
2022-06-12 17:07:53 -04:00
Logger::Print("File '{}' written to raw!\n", file.getName());
2017-01-19 16:23:59 -05:00
}
else
{
2022-06-12 17:07:53 -04:00
Logger::Print("File '{}' does not exist!\n", file.getName());
2017-01-19 16:23:59 -05:00
}
});
}
}