From 794cfd78c8b5ca503b4168b3d468d0f429f664ad Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Tue, 7 Jan 2025 10:55:48 +0100 Subject: [PATCH] Process commandlets without always initializing the entire engine --- src/commandlets/commandlet.cpp | 45 +++++++++++++++++++ src/commandlets/commandlet.h | 5 +++ src/commandlets/lightmapcmd.cpp | 8 +++- src/common/audio/sound/i_sound.cpp | 4 ++ src/common/console/c_console.cpp | 3 +- src/common/platform/win32/i_input.cpp | 18 +++++--- src/common/platform/win32/i_main.cpp | 3 +- src/common/rendering/v_video.cpp | 3 +- .../rendering/vulkan/vk_renderdevice.cpp | 6 +-- src/common/utility/x86.cpp | 3 +- src/d_iwad.cpp | 2 +- src/d_main.cpp | 35 +++++++++++---- 12 files changed, 110 insertions(+), 25 deletions(-) diff --git a/src/commandlets/commandlet.cpp b/src/commandlets/commandlet.cpp index c41a4cd45..cfd236edf 100644 --- a/src/commandlets/commandlet.cpp +++ b/src/commandlets/commandlet.cpp @@ -2,6 +2,45 @@ #include "commandlet.h" #include "lightmapcmd.h" #include "version.h" +#include + +int D_DoomMain_Game(); +static std::function* ToolCallback; +extern bool DisableLogging; + +int ToolMain() +{ + RootCommandlet commands; + commands.RunCommand(); + return 0; +} + +void Commandlet::RunInGame(std::function action) +{ + std::function callback = [=]() { + DisableLogging = false; + action(); + }; + + try + { + bool verbose = Args->CheckParm("-verbose") != 0; + if (!verbose) + DisableLogging = true; + ToolCallback = &callback; + D_DoomMain_Game(); + ToolCallback = nullptr; + DisableLogging = false; + } + catch (...) + { + ToolCallback = nullptr; + DisableLogging = false; + throw; + } +} + +///////////////////////////////////////////////////////////////////////////// void CommandletGroup::AddGroup(std::unique_ptr group) { @@ -20,6 +59,12 @@ RootCommandlet::RootCommandlet() AddGroup(); } +void RootCommandlet::RunEngineCommand() +{ + if (ToolCallback) + (*ToolCallback)(); +} + void RootCommandlet::RunCommand() { FArgs args = *Args; diff --git a/src/commandlets/commandlet.h b/src/commandlets/commandlet.h index cee0cdb0f..f091cadf9 100644 --- a/src/commandlets/commandlet.h +++ b/src/commandlets/commandlet.h @@ -2,6 +2,7 @@ #pragma once #include +#include #include "templates.h" #include "zstring.h" #include "printf.h" @@ -15,6 +16,8 @@ public: virtual void OnCommand(FArgs args) = 0; virtual void OnPrintHelp() = 0; + void RunInGame(std::function action); + const FString& GetLongFormName() const { return LongFormName; } const FString& GetShortDescription() const { return ShortDescription; } @@ -60,7 +63,9 @@ class RootCommandlet : public CommandletGroup { public: RootCommandlet(); + void RunCommand(); + static void RunEngineCommand(); private: void RunCommand(CommandletGroup* group, FArgs args, const FString prefix); diff --git a/src/commandlets/lightmapcmd.cpp b/src/commandlets/lightmapcmd.cpp index 70561f89b..318f5b82c 100644 --- a/src/commandlets/lightmapcmd.cpp +++ b/src/commandlets/lightmapcmd.cpp @@ -20,7 +20,9 @@ LightmapBuildCmdlet::LightmapBuildCmdlet() void LightmapBuildCmdlet::OnCommand(FArgs args) { - Printf("Baking LIGHTMAP lump!\n"); + RunInGame([]() { + Printf("Baking LIGHTMAP lump!\n"); + }); } void LightmapBuildCmdlet::OnPrintHelp() @@ -38,7 +40,9 @@ LightmapDeleteCmdlet::LightmapDeleteCmdlet() void LightmapDeleteCmdlet::OnCommand(FArgs args) { - Printf("Deleting LIGHTMAP lump!\n"); + RunInGame([]() { + Printf("Deleting LIGHTMAP lump!\n"); + }); } void LightmapDeleteCmdlet::OnPrintHelp() diff --git a/src/common/audio/sound/i_sound.cpp b/src/common/audio/sound/i_sound.cpp index f859ae0a8..83998c2a7 100644 --- a/src/common/audio/sound/i_sound.cpp +++ b/src/common/audio/sound/i_sound.cpp @@ -38,6 +38,7 @@ #include "oalsound.h" #include "i_module.h" +#include "i_interface.h" #include "cmdlib.h" #include "c_dispatch.h" @@ -253,6 +254,9 @@ void I_InitSound () nosound = !!Args->CheckParm ("-nosound"); nosfx = !!Args->CheckParm ("-nosfx"); + if (RunningAsTool) + nosound = true; + GSnd = NULL; if (nosound) { diff --git a/src/common/console/c_console.cpp b/src/common/console/c_console.cpp index 44c8ffacf..3afe02027 100644 --- a/src/common/console/c_console.cpp +++ b/src/common/console/c_console.cpp @@ -408,11 +408,12 @@ void WriteLineToLog(FILE *LogFile, const char *outline) fflush(LogFile); } +bool DisableLogging; extern bool gameisdead; int PrintString (int iprintlevel, const char *outline) { - if (gameisdead) + if ((!RunningAsTool && gameisdead) || DisableLogging) return 0; if (!conbuffer) return 0; // when called too early diff --git a/src/common/platform/win32/i_input.cpp b/src/common/platform/win32/i_input.cpp index 45a74fe0c..c058cdfa9 100644 --- a/src/common/platform/win32/i_input.cpp +++ b/src/common/platform/win32/i_input.cpp @@ -520,7 +520,8 @@ bool I_InitInput (void *hwnd) { HRESULT hr; - Printf ("I_InitInput\n"); + if (!RunningAsTool) + Printf ("I_InitInput\n"); noidle = !!Args->CheckParm ("-noidle"); g_pdi = NULL; @@ -532,19 +533,24 @@ bool I_InitInput (void *hwnd) g_pdi = NULL; // Just to be sure DirectInput8Create didn't change it } - Printf ("I_StartupMouse\n"); + if (!RunningAsTool) + Printf ("I_StartupMouse\n"); I_StartupMouse(); - Printf ("I_StartupKeyboard\n"); + if (!RunningAsTool) + Printf ("I_StartupKeyboard\n"); I_StartupKeyboard(); - Printf ("I_StartupXInput\n"); + if (!RunningAsTool) + Printf ("I_StartupXInput\n"); I_StartupXInput(); - Printf ("I_StartupRawPS2\n"); + if (!RunningAsTool) + Printf ("I_StartupRawPS2\n"); I_StartupRawPS2(); - Printf ("I_StartupDirectInputJoystick\n"); + if (!RunningAsTool) + Printf ("I_StartupDirectInputJoystick\n"); I_StartupDirectInputJoystick(); return TRUE; diff --git a/src/common/platform/win32/i_main.cpp b/src/common/platform/win32/i_main.cpp index 2ddec159c..7e884c5ce 100644 --- a/src/common/platform/win32/i_main.cpp +++ b/src/common/platform/win32/i_main.cpp @@ -337,7 +337,8 @@ int DoMain (HINSTANCE hInstance) void I_ShowFatalError(const char *msg) { I_ShutdownGraphics (); - mainwindow.RestoreConView(); + if (!RunningAsTool) + mainwindow.RestoreConView(); S_StopMusic(true); if (CVMAbortException::stacktrace.IsNotEmpty()) diff --git a/src/common/rendering/v_video.cpp b/src/common/rendering/v_video.cpp index 35a54e107..38efbc174 100644 --- a/src/common/rendering/v_video.cpp +++ b/src/common/rendering/v_video.cpp @@ -365,7 +365,8 @@ void V_Init2() I_InitGraphics(); Video->SetResolution(); // this only fails via exceptions. - Printf ("Resolution: %d x %d\n", SCREENWIDTH, SCREENHEIGHT); + if (!RunningAsTool) + Printf("Resolution: %d x %d\n", SCREENWIDTH, SCREENHEIGHT); // init these for the scaling menu menu_resolution_custom_width = SCREENWIDTH; diff --git a/src/common/rendering/vulkan/vk_renderdevice.cpp b/src/common/rendering/vulkan/vk_renderdevice.cpp index 049072205..c749715d0 100644 --- a/src/common/rendering/vulkan/vk_renderdevice.cpp +++ b/src/common/rendering/vulkan/vk_renderdevice.cpp @@ -586,9 +586,9 @@ void VulkanRenderDevice::PrintStartupLog() Printf(PRINT_LOG, "\n"); const auto &limits = props.limits; - Printf("Max. texture size: %d\n", limits.maxImageDimension2D); - Printf("Max. uniform buffer range: %d\n", limits.maxUniformBufferRange); - Printf("Min. uniform buffer offset alignment: %" PRIu64 "\n", limits.minUniformBufferOffsetAlignment); + Printf(PRINT_LOG, "Max. texture size: %d\n", limits.maxImageDimension2D); + Printf(PRINT_LOG, "Max. uniform buffer range: %d\n", limits.maxUniformBufferRange); + Printf(PRINT_LOG, "Min. uniform buffer offset alignment: %" PRIu64 "\n", limits.minUniformBufferOffsetAlignment); } void VulkanRenderDevice::SetLevelMesh(LevelMesh* mesh) diff --git a/src/common/utility/x86.cpp b/src/common/utility/x86.cpp index 5707c6572..26da263b0 100644 --- a/src/common/utility/x86.cpp +++ b/src/common/utility/x86.cpp @@ -35,6 +35,7 @@ #include #include #include "x86.h" +#include "printf.h" CPUInfo CPU; @@ -194,7 +195,7 @@ FString DumpCPUInfo(const CPUInfo *cpu, bool brief) out.Format("CPU Vendor ID: %s\n", cpu->VendorID); if (cpustring[0]) { - out.AppendFormat(" Name: %s\n", cpustring); + out.AppendFormat(" Name: " TEXTCOLOR_ORANGE "%s" TEXTCOLOR_NORMAL "\n", cpustring); } if (cpu->bIsAMD) { diff --git a/src/d_iwad.cpp b/src/d_iwad.cpp index cde54626f..e20763975 100644 --- a/src/d_iwad.cpp +++ b/src/d_iwad.cpp @@ -759,7 +759,7 @@ int FIWadManager::IdentifyVersion (std::vector&wadfiles, const char { if (RunningAsTool) { - Printf("Please specify which iwad to use with -iwad\n"); + I_FatalError("Please specify which iwad to use with -iwad\n"); return -1; } diff --git a/src/d_main.cpp b/src/d_main.cpp index 3997cc436..38522ad31 100644 --- a/src/d_main.cpp +++ b/src/d_main.cpp @@ -141,6 +141,7 @@ void D_DoAnonStats(); void I_DetectOS(); void UpdateGenericUI(bool cvar); void Local_Job_Init(); +int ToolMain(); // MACROS ------------------------------------------------------------------ @@ -3646,11 +3647,16 @@ static int D_InitGame(const FIWADInfo* iwad_info, std::vector& allw // //========================================================================== -static int D_DoomMain_Internal (void) +namespace { - const char *wad; - FIWadManager *iwad_man; + const char* wad; + const char* batchout; +} +int D_DoomMain_Game(); + +int GameMain_Internal() +{ NetworkEntityManager::NetIDStart = MAXPLAYERS + 1; GC::AddMarkerFunc(GC_MarkGameRoots); VM_CastSpriteIDToString = Doom_CastSpriteIDToString; @@ -3700,7 +3706,7 @@ static int D_DoomMain_Internal (void) std::set_new_handler(NewFailure); - const char *batchout = Args->CheckValue("-errorlog"); + batchout = Args->CheckValue("-errorlog"); D_DoomInit(); @@ -3764,6 +3770,19 @@ static int D_DoomMain_Internal (void) } C_InitConsole(80*8, 25*8, false); + + if (RunningAsTool) + { + return ToolMain(); + } + else + { + return D_DoomMain_Game(); + } +} + +int D_DoomMain_Game() +{ I_DetectOS(); // +logfile gets checked too late to catch the full startup log in the logfile so do some extra check for it here. @@ -3794,7 +3813,7 @@ static int D_DoomMain_Internal (void) FString optionalwad = BaseFileSearch(OPTIONALWAD, NULL, true, GameConfig); - iwad_man = new FIWadManager(basewad.GetChars(), optionalwad.GetChars()); + FIWadManager* iwad_man = new FIWadManager(basewad.GetChars(), optionalwad.GetChars()); // Now that we have the IWADINFO, initialize the autoload ini sections. GameConfig->DoAutoloadSetup(iwad_man); @@ -3852,9 +3871,7 @@ static int D_DoomMain_Internal (void) if (RunningAsTool) { - Printf("\n"); - RootCommandlet commands; - commands.RunCommand(); + RootCommandlet::RunEngineCommand(); return 0; } @@ -3891,7 +3908,7 @@ int GameMain() try { - ret = D_DoomMain_Internal(); + ret = GameMain_Internal(); } catch (const CExitEvent &exit) // This is a regular exit initiated from deeply nested code. {