Skip to content

Commit

Permalink
Merge branch 'main' into feat/overhaul-mod-loading-locations
Browse files Browse the repository at this point in the history
  • Loading branch information
Alystrasz authored Nov 24, 2024
2 parents 6509283 + fad89b3 commit 26a3661
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 158 deletions.
13 changes: 5 additions & 8 deletions primedev/engine/runframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@
#include "hoststate.h"
#include "server/serverpresence.h"

AUTOHOOK_INIT()

// clang-format off
AUTOHOOK(CEngine__Frame, engine.dll + 0x1C8650,
void, __fastcall, (CEngine* self))
// clang-format on
static void(__fastcall* o_pCEngine__Frame)(CEngine* self) = nullptr;
static void __fastcall h_CEngine__Frame(CEngine* self)
{
CEngine__Frame(self);
o_pCEngine__Frame(self);
}

ON_DLL_LOAD("engine.dll", RunFrame, (CModule module))
{
AUTOHOOK_DISPATCH()
o_pCEngine__Frame = module.Offset(0x1C8650).RCast<decltype(o_pCEngine__Frame)>();
HookAttach(&(PVOID&)o_pCEngine__Frame, (PVOID)h_CEngine__Frame);
}
56 changes: 28 additions & 28 deletions primedev/logging/loghooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#include <iomanip>
#include <sstream>

AUTOHOOK_INIT()

ConVar* Cvar_spewlog_enable;
ConVar* Cvar_cl_showtextmsg;

Expand Down Expand Up @@ -70,10 +68,8 @@ const std::unordered_map<SpewType_t, const char> PrintSpewTypes_Short = {

ICenterPrint* pInternalCenterPrint = NULL;

// clang-format off
AUTOHOOK(TextMsg, client.dll + 0x198710,
void,, (BFRead* msg))
// clang-format on
static void (*o_pTextMsg)(BFRead* msg) = nullptr;
static void h_TextMsg(BFRead* msg)
{
TextMsgPrintType_t msg_dest = (TextMsgPrintType_t)msg->ReadByte();

Expand Down Expand Up @@ -103,10 +99,8 @@ void,, (BFRead* msg))
}
}

// clang-format off
AUTOHOOK(Hook_fprintf, engine.dll + 0x51B1F0,
int,, (void* const stream, const char* const format, ...))
// clang-format on
static int (*o_pfprintf)(void* const stream, const char* const format, ...) = nullptr;
static int h_fprintf(void* const stream, const char* const format, ...)
{
NOTE_UNUSED(stream);

Expand All @@ -127,19 +121,15 @@ int,, (void* const stream, const char* const format, ...))
return 0;
}

// clang-format off
AUTOHOOK(ConCommand_echo, engine.dll + 0x123680,
void,, (const CCommand& arg))
// clang-format on
static void (*o_pConCommand_echo)(const CCommand& arg) = nullptr;
static void h_ConCommand_echo(const CCommand& arg)
{
if (arg.ArgC() >= 2)
NS::log::echo->info("{}", arg.ArgS());
}

// clang-format off
AUTOHOOK(EngineSpewFunc, engine.dll + 0x11CA80,
void, __fastcall, (void* pEngineServer, SpewType_t type, const char* format, va_list args))
// clang-format on
static void(__fastcall* o_pEngineSpewFunc)(void* pEngineServer, SpewType_t type, const char* format, va_list args) = nullptr;
static void __fastcall h_EngineSpewFunc(void* pEngineServer, SpewType_t type, const char* format, va_list args)
{
NOTE_UNUSED(pEngineServer);
if (!Cvar_spewlog_enable->GetBool())
Expand Down Expand Up @@ -214,10 +204,8 @@ void, __fastcall, (void* pEngineServer, SpewType_t type, const char* format, va_
}

// used for printing the output of status
// clang-format off
AUTOHOOK(Status_ConMsg, engine.dll + 0x15ABD0,
void,, (const char* text, ...))
// clang-format on
static void (*o_pStatus_ConMsg)(const char* text, ...) = nullptr;
static void h_Status_ConMsg(const char* text, ...)
{
char formatted[2048];
va_list list;
Expand All @@ -233,10 +221,8 @@ void,, (const char* text, ...))
spdlog::info(formatted);
}

// clang-format off
AUTOHOOK(CClientState_ProcessPrint, engine.dll + 0x1A1530,
bool,, (void* thisptr, uintptr_t msg))
// clang-format on
static bool (*o_pCClientState_ProcessPrint)(void* thisptr, uintptr_t msg) = nullptr;
static bool h_CClientState_ProcessPrint(void* thisptr, uintptr_t msg)
{
NOTE_UNUSED(thisptr);

Expand All @@ -252,14 +238,28 @@ bool,, (void* thisptr, uintptr_t msg))

ON_DLL_LOAD_RELIESON("engine.dll", EngineSpewFuncHooks, ConVar, (CModule module))
{
AUTOHOOK_DISPATCH_MODULE(engine.dll)
o_pfprintf = module.Offset(0x51B1F0).RCast<decltype(o_pfprintf)>();
HookAttach(&(PVOID&)o_pfprintf, (PVOID)h_fprintf);

o_pConCommand_echo = module.Offset(0x123680).RCast<decltype(o_pConCommand_echo)>();
HookAttach(&(PVOID&)o_pConCommand_echo, (PVOID)h_ConCommand_echo);

o_pEngineSpewFunc = module.Offset(0x11CA80).RCast<decltype(o_pEngineSpewFunc)>();
HookAttach(&(PVOID&)o_pEngineSpewFunc, (PVOID)h_EngineSpewFunc);

o_pStatus_ConMsg = module.Offset(0x15ABD0).RCast<decltype(o_pStatus_ConMsg)>();
HookAttach(&(PVOID&)o_pStatus_ConMsg, (PVOID)h_Status_ConMsg);

o_pCClientState_ProcessPrint = module.Offset(0x1A1530).RCast<decltype(o_pCClientState_ProcessPrint)>();
HookAttach(&(PVOID&)o_pCClientState_ProcessPrint, (PVOID)h_CClientState_ProcessPrint);

Cvar_spewlog_enable = new ConVar("spewlog_enable", "0", FCVAR_NONE, "Enables/disables whether the engine spewfunc should be logged");
}

ON_DLL_LOAD_CLIENT_RELIESON("client.dll", ClientPrintHooks, ConVar, (CModule module))
{
AUTOHOOK_DISPATCH_MODULE(client.dll)
o_pTextMsg = module.Offset(0x198710).RCast<decltype(o_pTextMsg)>();
HookAttach(&(PVOID&)o_pTextMsg, (PVOID)h_TextMsg);

Cvar_cl_showtextmsg = new ConVar("cl_showtextmsg", "1", FCVAR_NONE, "Enable/disable text messages printing on the screen.");
pInternalCenterPrint = module.Offset(0x216E940).RCast<ICenterPrint*>();
Expand Down
35 changes: 32 additions & 3 deletions primedev/mods/autodownload/moddownloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ int ModDownloader::ModFetchingProgressCallback(
{
NOTE_UNUSED(totalToUpload);
NOTE_UNUSED(nowUploaded);

// Abort download
ModDownloader* instance = static_cast<ModDownloader*>(ptr);
if (instance->modState.state == ABORTED)
{
return 1;
}

if (totalDownloadSize != 0 && finishedDownloadSize != 0)
{
ModDownloader* instance = static_cast<ModDownloader*>(ptr);
Expand Down Expand Up @@ -563,6 +571,13 @@ void ModDownloader::ExtractMod(fs::path modPath, VerifiedModPlatform platform)
}
}

// Abort mod extraction if needed
if (modState.state == ABORTED)
{
spdlog::info("User cancelled mod installation, aborting mod extraction.");
return;
}

// Go to next file
if ((i + 1) < gi.number_entry)
{
Expand Down Expand Up @@ -602,8 +617,7 @@ void ModDownloader::DownloadMod(std::string modName, std::string modVersion)
spdlog::error("Error while removing downloaded archive: {}", a.what());
}

modState.state = DONE;
spdlog::info("Done downloading {}.", modName);
spdlog::info("Done cleaning after downloading {}.", modName);
});

// Download mod archive
Expand All @@ -613,7 +627,10 @@ void ModDownloader::DownloadMod(std::string modName, std::string modVersion)
if (!fetchingResult.has_value())
{
spdlog::error("Something went wrong while fetching archive, aborting.");
modState.state = MOD_FETCHING_FAILED;
if (modState.state != ABORTED)
{
modState.state = MOD_FETCHING_FAILED;
}
return;
}
archiveLocation = fetchingResult.value();
Expand All @@ -626,11 +643,17 @@ void ModDownloader::DownloadMod(std::string modName, std::string modVersion)

// Extract downloaded mod archive
ExtractMod(archiveLocation, fullVersion.platform);
modState.state = DONE;
});

requestThread.detach();
}

void ModDownloader::CancelDownload()
{
modState.state = ABORTED;
}

ON_DLL_LOAD_RELIESON("engine.dll", ModDownloader, (ConCommand), (CModule module))
{
g_pModDownloader = new ModDownloader();
Expand Down Expand Up @@ -687,3 +710,9 @@ ADD_SQFUNC("ModInstallState", NSGetModInstallState, "", "", ScriptContext::SERVE

return SQRESULT_NOTNULL;
}

ADD_SQFUNC("void", NSCancelModDownload, "", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI)
{
g_pModDownloader->CancelDownload();
return SQRESULT_NULL;
}
1 change: 1 addition & 0 deletions primedev/mods/autodownload/moddownloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class ModDownloader
CHECKSUMING,
EXTRACTING,
DONE, // Everything went great, mod can be used in-game
ABORTED, // User cancelled mod install process

// Errors
FAILED, // Generic error message, should be avoided as much as possible
Expand Down
2 changes: 1 addition & 1 deletion primedev/mods/modmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ void ModManager::LoadMods()
int currentModIndex = 0;
for (Mod& mod : m_LoadedMods)
{
if (!mod.m_bEnabled || (!mod.RequiredOnClient && !mod.Pdiff.size()))
if (!mod.m_bEnabled)
continue;

modinfoDoc["Mods"].PushBack(rapidjson::kObjectType, modinfoDoc.GetAllocator());
Expand Down
20 changes: 20 additions & 0 deletions primedev/mods/modsavefiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,26 @@ template <ScriptContext context> void SaveFileManager::SaveFileAsync(fs::path fi
std::thread writeThread(
[mutex, file, contents]()
{
// Check if has extension and return early if not
if (!file.has_extension())
{
spdlog::error("A mod failed to save a file via Safe I/O due to the following error:");
spdlog::error("No file extension specified");
return;
}

// If there's a file extension missing here that you need, feel free to make a PR adding it
static const std::set<std::string> whitelist = {".txt", ".json"};

// Check if file extension is whitelisted
std::string extension = file.extension().string();
if (whitelist.find(extension) == whitelist.end())
{
spdlog::error("A mod failed to save a file via Safe I/O due to the following error:");
spdlog::error("Disallowed file extension: {}", extension);
return;
}

try
{
mutex.get().lock();
Expand Down
Loading

0 comments on commit 26a3661

Please sign in to comment.