Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow executing .cfg from game dir #10

Merged
merged 1 commit into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/game/mp_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ namespace mp
CL_GamepadButtonEvent_t CL_GamepadButtonEvent = reinterpret_cast<CL_GamepadButtonEvent_t>(0x822DD1E8);

Cmd_AddCommandInternal_t Cmd_AddCommandInternal = reinterpret_cast<Cmd_AddCommandInternal_t>(0x8223ADE0);
Cmd_ExecFromFastFile_t Cmd_ExecFromFastFile = reinterpret_cast<Cmd_ExecFromFastFile_t>(0x8223AF40);
Cbuf_ExecuteBuffer_t Cbuf_ExecuteBuffer = reinterpret_cast<Cbuf_ExecuteBuffer_t>(0x8223AAE8);

Com_Printf_t Com_Printf = reinterpret_cast<Com_Printf_t>(0x82237000);
Com_PrintError_t Com_PrintError = reinterpret_cast<Com_PrintError_t>(0x82235C50);
Expand Down Expand Up @@ -1304,6 +1306,27 @@ namespace mp
Load_images();
}

Detour Cmd_ExecFromFastFile_Detour;

bool Cmd_ExecFromFastFile_Hook(int localClientNum, int controllerIndex, const char *filename)
{
std::string file_path = "game:\\raw\\";
file_path += filename;

if (filesystem::file_exists(file_path))
{
std::string contents = filesystem::read_file_to_string(file_path);
if (!contents.empty())
{
Com_Printf(CON_CHANNEL_SYSTEM, "execing %s from raw:\\\n", filename);
Cbuf_ExecuteBuffer(localClientNum, controllerIndex, contents.c_str());
return true;
}
}

return Cmd_ExecFromFastFile_Detour.GetOriginal<decltype(Cmd_ExecFromFastFile)>()(localClientNum, controllerIndex, filename);
}

void init()
{
xbox::DbgPrint("Initializing MP\n");
Expand Down Expand Up @@ -1331,5 +1354,8 @@ namespace mp

CG_RegisterGraphics_Detour = Detour(CG_RegisterGraphics, CG_RegisterGraphics_Hook);
CG_RegisterGraphics_Detour.Install();

Cmd_ExecFromFastFile_Detour = Detour(Cmd_ExecFromFastFile, Cmd_ExecFromFastFile_Hook);
Cmd_ExecFromFastFile_Detour.Install();
}
}
2 changes: 2 additions & 0 deletions src/game/mp_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ namespace mp
typedef void (*CL_GamepadButtonEvent_t)(int localClientNum, int controllerIndex, int key, int down, unsigned int time);

typedef void (*Cmd_AddCommandInternal_t)(const char *cmdName, void (*function)(), cmd_function_s *allocedCmd);
typedef bool (*Cmd_ExecFromFastFile_t)(int localClientNum, int controllerIndex, const char *filename);
typedef void (*Cbuf_ExecuteBuffer_t)(int localClientNum, int controllerIndex, const char *buffer);

typedef void (*Com_PrintError_t)(conChannel_t channel, const char *fmt, ...);
typedef void (*Com_PrintMessage_t)(conChannel_t channel, const char *msg, int error);
Expand Down