From 52b7586406d7233488086849f687252ca582ca20 Mon Sep 17 00:00:00 2001 From: linuxversion <1660477+SomeCrazyGuy@users.noreply.github.com> Date: Fri, 17 May 2024 20:22:03 -0400 Subject: [PATCH] make config and console paths work in asi, sfse, and self-inject at the same time. Gamepass finally supported --- Starfield Console Replacer.vcxproj | 6 +++-- VersionInfo.rc | Bin 4738 -> 4738 bytes src/console.cpp | 4 +-- src/gui.cpp | 10 ++++++++ src/log_buffer.cpp | 18 ++++++++++---- src/main.cpp | 38 +++++++++++++++++++++++++++-- src/main.h | 2 +- src/settings.cpp | 5 ++-- 8 files changed, 69 insertions(+), 14 deletions(-) diff --git a/Starfield Console Replacer.vcxproj b/Starfield Console Replacer.vcxproj index 5ead3a9..eb572a1 100644 --- a/Starfield Console Replacer.vcxproj +++ b/Starfield Console Replacer.vcxproj @@ -137,7 +137,8 @@ true - copy /Y "$(OutDir)$(TargetName).dll" "$(OutDir)vcruntime140_1.dll" + copy /Y "$(OutDir)$(TargetName).dll" "$(OutDir)vcruntime140_1.dll" +copy /Y "$(OutDir)$(TargetName).dll" "$(OutDir)BetterConsole.asi" @@ -194,7 +195,8 @@ true - copy /Y "$(OutDir)$(TargetName).dll" "$(OutDir)vcruntime140_1.dll" + copy /Y "$(OutDir)$(TargetName).dll" "$(OutDir)vcruntime140_1.dll" +copy /Y "$(OutDir)$(TargetName).dll" "$(OutDir)BetterConsole.asi" diff --git a/VersionInfo.rc b/VersionInfo.rc index 2fc19164d11a3a3a26b37a4a15c2ce7dab981aaa..75f87972015f33de9e9f10aaae1579e1e386a3d1 100644 GIT binary patch delta 56 zcmZotZBpGZiG$gQ!C>-K4nq*V`6Nd!GdqyMpa&HE$fdhEi{}L+D_CH1K9}U?0zM%Y E0I5L^O#lD@ delta 56 zcmZotZBpGZiG$gY!E*9c4nq*V`6Nd!GdqyMpvPb^`6HL^<}98UjI3aR$@yH8n+y1a FSOBdd4s8Gc diff --git a/src/console.cpp b/src/console.cpp index 4066547..957d805 100644 --- a/src/console.cpp +++ b/src/console.cpp @@ -47,8 +47,8 @@ constexpr uint64_t OFFSET_console_run = 0x2911d84; //void ConsoleRun(NULL, char* //TODO: get the location of betterconsole and spawn the files there? -#define OUTPUT_FILE_PATH ".\\Data\\SFSE\\Plugins\\BetterConsoleOutput.txt" -#define HISTORY_FILE_PATH ".\\Data\\SFSE\\Plugins\\BetterConsoleHistory.txt" +#define OUTPUT_FILE_PATH "BetterConsoleOutput.txt" +#define HISTORY_FILE_PATH "BetterConsoleHistory.txt" enum class InputMode : uint32_t { diff --git a/src/gui.cpp b/src/gui.cpp index 26b154d..0b1ab8e 100644 --- a/src/gui.cpp +++ b/src/gui.cpp @@ -45,6 +45,7 @@ extern void draw_gui() { ImGui::OpenPopup("HelpLinks"); } if (ImGui::BeginPopup("HelpLinks")) { + ImGui::SeparatorText("Get Help"); if (ImGui::Button("NexusMods")) { ShellExecuteA(NULL, "open", "https://www.nexusmods.com/starfield/mods/3683", NULL, NULL, 1); } @@ -66,6 +67,7 @@ extern void draw_gui() { ImGui::Text(message); ImGui::EndPopup(); } + ImGui::SeparatorText("Common Fixes"); if (ImGui::Button("Reset Font Scale")) { GetSettingsMutable()->FontScaleOverride = 100; } @@ -73,6 +75,14 @@ extern void draw_gui() { GetSettingsMutable()->ConsoleHotkey = 112; GetSettingsMutable()->HotkeyModifier = 0; } + if (ImGui::Button("Open Log File")) { + char path[260]; + ShellExecuteA(NULL, "open", GetPathInDllDir(path, "BetterConsoleLog.txt"), NULL, NULL, 1); + } + if (ImGui::Button("Open Config File")) { + char path[260]; + ShellExecuteA(NULL, "open", GetPathInDllDir(path, "MiniModMenuRegistry.txt"), NULL, NULL, 1); + } ImGui::EndPopup(); } ImGui::SetWindowFontScale((float) GetSettings()->FontScaleOverride / 100.f); diff --git a/src/log_buffer.cpp b/src/log_buffer.cpp index 3fb9834..7b7d93a 100644 --- a/src/log_buffer.cpp +++ b/src/log_buffer.cpp @@ -16,7 +16,7 @@ struct LogBuffer { static std::vector Logs{}; -static LogBufferHandle LogBufferCreate(const char* name, const char* path) { +static LogBufferHandle LogBufferCreate(const char* name, const char* logfile_name) { // create a dummy logfile for log 0 // this allows you to perform if(logbufferhandle) because a valid handle is never 0 @@ -29,8 +29,9 @@ static LogBufferHandle LogBufferCreate(const char* name, const char* path) { LogBuffer log; log.name = name; log.logfile = NULL; - if (path) { - fopen_s(&log.logfile, path, "ab"); + if (logfile_name) { + char path[260]; + fopen_s(&log.logfile, GetPathInDllDir(path, logfile_name), "ab"); } auto ret = Logs.size(); Logs.push_back(log); @@ -97,7 +98,11 @@ static void LogBufferSave(LogBufferHandle handle, const char* filename) { ASSERT(handle != 0); ASSERT(handle < Logs.size()); FILE* f = nullptr; - fopen_s(&f, filename, "wb"); + { + char path[260]; + fopen_s(&f, GetPathInDllDir(path, filename), "wb"); + } + if (f == nullptr) return; for (uint32_t line = 0; line < LogBufferGetLineCount(handle); ++line) { @@ -125,7 +130,10 @@ static LogBufferHandle LogBufferRestore(const char* name, const char* filename) char max_line[4096]; FILE* f = nullptr; - fopen_s(&f, filename, "r+b"); + { + char path[260]; + fopen_s(&f, GetPathInDllDir(path, filename), "r+b"); + } if (f == nullptr) return LogBufferCreate(name, filename); while(fgets(max_line, sizeof(max_line), f)) { diff --git a/src/main.cpp b/src/main.cpp index 484152d..a4ca411 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -39,6 +39,29 @@ static bool should_show_ui = false; #define EveryNFrames(N) []()->bool{static unsigned count=0;if(++count==(N)){count=0;}return !count;}() +static char DLL_DIR[MAX_PATH]; + +extern char* GetPathInDllDir(char* path_max_buffer, const char* filename) { + + char* p = path_max_buffer; + + unsigned i, j; + + for (i = 0; DLL_DIR[i]; ++i) { + *p++ = DLL_DIR[i]; + } + + for (j = 0; filename[j]; ++j) { + *p++ = filename[j]; + } + + *p = 0; + + return path_max_buffer; +} + + + #ifdef MODMENU_DEBUG #include std::mutex logging_mutex; @@ -48,7 +71,8 @@ static void write_log(const char* const str) noexcept { static HANDLE debugfile = INVALID_HANDLE_VALUE; logging_mutex.lock(); if (debugfile == INVALID_HANDLE_VALUE) { - debugfile = CreateFileW(L"BetterConsoleLog.txt", GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + char path[MAX_PATH]; + debugfile = CreateFileA(GetPathInDllDir(path, "BetterConsoleLog.txt"), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); } if (debugfile != INVALID_HANDLE_VALUE) { WriteFile(debugfile, str, (DWORD)strnlen(str, 4096), NULL, NULL); @@ -56,6 +80,7 @@ static void write_log(const char* const str) noexcept { } else { MessageBoxA(NULL, "Could not write to 'BetterConsoleLog.txt'", "ASSERTION FAILURE", 0); + abort(); } logging_mutex.unlock(); } @@ -358,11 +383,20 @@ extern "C" __declspec(dllexport) void SFSEPlugin_Load(const SFSEInterface * sfse // could check if we are named vcruntime // could check if path is sfse plugin dir // could fallback to asi loader called us -extern "C" BOOL WINAPI DllMain(HINSTANCE, DWORD fdwReason, LPVOID) { +extern "C" BOOL WINAPI DllMain(HINSTANCE self, DWORD fdwReason, LPVOID) { if (fdwReason == DLL_PROCESS_ATTACH) { /* lock the linker/dll loader until hooks are installed, TODO: make sure this code path is fast */ static bool RunHooksOnlyOnce = true; ASSERT(RunHooksOnlyOnce == true); //i want to know if this assert ever gets triggered + + //use the directory of the betterconsole dll as the place to put other files + GetModuleFileNameA(self, DLL_DIR, MAX_PATH); + char* n = DLL_DIR; + while (*n) ++n; + while ((n != DLL_DIR) && (*n != '\\')) --n; + ++n; + *n = 0; + SetupModMenu(); RunHooksOnlyOnce = false; } diff --git a/src/main.h b/src/main.h index 5939469..2bacb0b 100644 --- a/src/main.h +++ b/src/main.h @@ -69,4 +69,4 @@ struct ModMenuSettings { extern const ModMenuSettings* GetSettings(); extern ModMenuSettings* GetSettingsMutable(); - +extern char* GetPathInDllDir(char* path_max_buffer, const char* filename); diff --git a/src/settings.cpp b/src/settings.cpp index b2bfd7c..c1ccc18 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -8,7 +8,7 @@ #include "simpledraw.h" #include "internal_plugin.h" -#define SETTINGS_REGISTRY_PATH ".\\Data\\SFSE\\Plugins\\MiniModMenuRegistry.txt" +#define SETTINGS_REGISTRY_PATH "MiniModMenuRegistry.txt" union SettingValue { void* as_data; @@ -298,7 +298,8 @@ static bool TurboSettingsParser() { bool ret = false; //TODO: should use the 'W' variant to handle unsual paths? - auto hFile = CreateFileA(SETTINGS_REGISTRY_PATH, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + char path[MAX_PATH]; + auto hFile = CreateFileA(GetPathInDllDir(path, SETTINGS_REGISTRY_PATH), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { DEBUG("could not open settings file: %s", SETTINGS_REGISTRY_PATH); return false; //file cannot be opened