Skip to content

Commit

Permalink
Added experimental self-extracting iwd
Browse files Browse the repository at this point in the history
  • Loading branch information
Rex109 committed Mar 29, 2024
1 parent 39f52ae commit d9edbed
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 21 deletions.
Binary file added cod4qol/cod4qol.rc
Binary file not shown.
4 changes: 4 additions & 0 deletions cod4qol/cod4qol.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,12 @@
<ClInclude Include="defines.hpp" />
<ClInclude Include="game.hpp" />
<ClInclude Include="hooks.hpp" />
<ClInclude Include="resource.h" />
<ClInclude Include="updater.hpp" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="cod4qol.rc" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
Expand Down
8 changes: 8 additions & 0 deletions cod4qol/cod4qol.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,13 @@
<ClInclude Include="updater.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="resource.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="cod4qol.rc">
<Filter>Resource Files</Filter>
</ResourceCompile>
</ItemGroup>
</Project>
18 changes: 0 additions & 18 deletions cod4qol/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ void commands::InitializeCommands()
std::cout << "Initializing commands..." << std::endl;

game::Cmd_AddCommand("loadzone", LoadZone);
game::Cmd_AddCommand("loadiwd", LoadIWD);
game::Cmd_AddCommand("vm_anim", VmAnim);
game::Cmd_AddCommand("readprotectedconfig", ReadProtectedConfig);
game::Cmd_AddCommand("writeprotectedconfig", WriteProtectedConfig);
Expand Down Expand Up @@ -104,23 +103,6 @@ void commands::LoadZone()
game::DB_LoadXAssets(info, 2, 1);
}

void commands::LoadIWD()
{
if (game::Cmd_Argc() < 2)
{
game::Com_PrintMessage(0, "Usage: loadiwd <iwdName>\n", 0);
return;
}

std::string iwdName = game::Cmd_Argv(1);

std::string relative_dir = game::fs_homepath->current.string;
relative_dir.append("\\main\\");
relative_dir.append(iwdName);

game::FS_AddSingleIwdFileForGameDirectory(relative_dir.c_str(), iwdName.c_str(), "main");
}

void commands::VmAnim()
{
if (!sv_running->current.enabled)
Expand Down
1 change: 0 additions & 1 deletion cod4qol/commands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ namespace commands
void InitializeCommands();

void LoadZone();
void LoadIWD();
void iPrintLnBold(const char* text);
void WriteProtectedConfig();
void ReadProtectedConfig();
Expand Down
62 changes: 60 additions & 2 deletions cod4qol/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "commands.hpp"
#include "hooks.hpp"
#include "updater.hpp"
#include "resource.h"

__declspec(naked) const char* game::hookedCon_LinePrefix()
{
Expand All @@ -14,15 +15,72 @@ __declspec(naked) const char* game::hookedCon_LinePrefix()
}
}

HMODULE GetCurrentModule()
{
HMODULE hModule = NULL;
GetModuleHandleEx(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
(LPCTSTR)GetCurrentModule,
&hModule);

return hModule;
}

void game::LoadModFiles()
{
std::cout << "Loading mod files..." << std::endl;

std::string relative_dir = game::fs_homepath->current.string;
relative_dir.append("\\main\\xcommon_cod4qol.iwd");
FS_AddSingleIwdFileForGameDirectory(relative_dir.c_str(), "xcommon_cod4qol.iwd", "main");

if (startup && !std::filesystem::exists(relative_dir))
{
HRSRC hRes = FindResource(GetCurrentModule(), MAKEINTRESOURCE(COD4QOL_IWD), RT_RCDATA);

if (!hRes)
{
std::cout << "Failed to find resource: " << GetLastError() << std::endl;
return;
}

HGLOBAL hData = LoadResource(GetCurrentModule(), hRes);


if (!hData)
{
std::cout << "Failed to load resource: " << GetLastError() << std::endl;
return;
}

DWORD size = SizeofResource(GetCurrentModule(), hRes);
byte* data = (byte*)LockResource(hData);

game::WriteBytesToFile(data, size, relative_dir.c_str());

UnlockResource(data);
FreeResource(hData);

std::cout << "Resource extracted to file: " << relative_dir << std::endl;
}

FS_AddSingleIwdFileForGameDirectory(relative_dir.c_str(), "xcommon_cod4qol.iwd", "main");
game::Cbuf_AddText("loadzone qol\n", 0);
}

void game::WriteBytesToFile(const byte* data, DWORD size, const char* filename)
{
std::ofstream outfile(filename, std::ios::binary);
if (!outfile) {
std::cerr << "Error opening file for writing: " << filename << std::endl;
return;
}

outfile.write(reinterpret_cast<const char*>(data), size);
outfile.close();

std::cout << "Bytes written to file: " << filename << std::endl;
}

void game::hookedDB_LoadXZoneFromGfxConfig()
{
std::cout << "Calling DB_LoadXZoneFromGfxConfig();" << std::endl;
Expand Down Expand Up @@ -66,7 +124,7 @@ void game::hookedCL_InitCGame()
{
std::cout << "Calling CL_InitCGame();" << std::endl;

game::Cbuf_AddText("loadzone qol\n", 0);
LoadModFiles();

return game::pCL_InitCGame();
}
Expand Down
2 changes: 2 additions & 0 deletions cod4qol/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <iostream>
#include "defines.hpp"
#include <d3d9.h>
#include <filesystem>

namespace game
{
Expand Down Expand Up @@ -509,6 +510,7 @@ namespace game
int Cmd_Argc();
const char* Cmd_Argv(int arg);
void LoadModFiles();
void WriteBytesToFile(const byte* data, DWORD size, const char* filename);

dvar_s* Find(const char*);
cmd_function_s* Cmd_AddCommand(const char* cmdname, void(__cdecl* function)());
Expand Down
3 changes: 3 additions & 0 deletions cod4qol/hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ void hooks::InitializeHooks()
//Fast startup
hooks::write_addr((game::cod4x_entry + 0x3AA7C), "\xC3", 1);

//g_disablePureCheck
hooks::write_addr(0xD5EC497, "\x01", 3);

//Console name
game::pCon_LinePrefix = (game::Con_LinePrefix)(0x460613);
hooks::install(&(PVOID&)game::pCon_LinePrefix, (PBYTE)game::hookedCon_LinePrefix);
Expand Down
10 changes: 10 additions & 0 deletions cod4qol/resource.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#define COD4QOL_IWD 101

#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 102
#define _APS_NEXT_COMMAND_VALUE 40003
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif

0 comments on commit d9edbed

Please sign in to comment.