Skip to content

Commit

Permalink
make config and console paths work in asi, sfse, and self-inject at t…
Browse files Browse the repository at this point in the history
…he same time. Gamepass finally supported
  • Loading branch information
SomeCrazyGuy committed May 18, 2024
1 parent 0d52008 commit 52b7586
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 14 deletions.
6 changes: 4 additions & 2 deletions Starfield Console Replacer.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@
<MapExports>true</MapExports>
</Link>
<PostBuildEvent>
<Command>copy /Y "$(OutDir)$(TargetName).dll" "$(OutDir)vcruntime140_1.dll"</Command>
<Command>copy /Y "$(OutDir)$(TargetName).dll" "$(OutDir)vcruntime140_1.dll"
copy /Y "$(OutDir)$(TargetName).dll" "$(OutDir)BetterConsole.asi"</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
Expand Down Expand Up @@ -194,7 +195,8 @@
<MapExports>true</MapExports>
</Link>
<PostBuildEvent>
<Command>copy /Y "$(OutDir)$(TargetName).dll" "$(OutDir)vcruntime140_1.dll"</Command>
<Command>copy /Y "$(OutDir)$(TargetName).dll" "$(OutDir)vcruntime140_1.dll"
copy /Y "$(OutDir)$(TargetName).dll" "$(OutDir)BetterConsole.asi"</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down
Binary file modified VersionInfo.rc
Binary file not shown.
4 changes: 2 additions & 2 deletions src/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions src/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -66,13 +67,22 @@ extern void draw_gui() {
ImGui::Text(message);
ImGui::EndPopup();
}
ImGui::SeparatorText("Common Fixes");
if (ImGui::Button("Reset Font Scale")) {
GetSettingsMutable()->FontScaleOverride = 100;
}
if (ImGui::Button("Reset Hotkey to F1")) {
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);
Expand Down
18 changes: 13 additions & 5 deletions src/log_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct LogBuffer {
static std::vector<LogBuffer> 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
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)) {
Expand Down
38 changes: 36 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <mutex>
std::mutex logging_mutex;
Expand All @@ -48,14 +71,16 @@ 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);
FlushFileBuffers(debugfile);
}
else {
MessageBoxA(NULL, "Could not write to 'BetterConsoleLog.txt'", "ASSERTION FAILURE", 0);
abort();
}
logging_mutex.unlock();
}
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@ struct ModMenuSettings {

extern const ModMenuSettings* GetSettings();
extern ModMenuSettings* GetSettingsMutable();

extern char* GetPathInDllDir(char* path_max_buffer, const char* filename);
5 changes: 3 additions & 2 deletions src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 52b7586

Please sign in to comment.