Skip to content

Commit

Permalink
feat: allow redirecting of save files to be relative to exe
Browse files Browse the repository at this point in the history
  • Loading branch information
Lazrius committed Jan 6, 2025
1 parent c4d7709 commit ec7df07
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
4 changes: 3 additions & 1 deletion FLUF.CrashWalker/Include/FLUF.CrashWalker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ struct ErrorPayload
class CrashCatcher;
class FlufCrashWalker final : public FlufModule
{
void LoadErrorPayloadFromCache(std::string_view path);
void OnGameLoad() override;
void OnDllLoaded(std::string_view dllName, HMODULE dllPtr) override;
void OnDllUnloaded(std::string_view dllName, HMODULE dllPtr) override;

void LoadErrorPayloadFromCache(std::string_view path);
static int __stdcall GlobalExceptionHandler(EXCEPTION_POINTERS* exceptionPointers);

inline static std::unique_ptr<CrashCatcher> crashCatcher;
inline static std::unique_ptr<Config> config;
std::vector<ErrorPayload> possibleErrors;
Expand Down
10 changes: 7 additions & 3 deletions FLUF/Include/Fluf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ struct CShip;
struct SStartupInfo;
class Fluf
{
using GetUserDataPathSig = bool (*)(char*);

friend ClientSend;
friend ClientReceive;
friend FlufModule;
Expand All @@ -45,6 +47,8 @@ class Fluf
// FLUF can run on the client or server. This becomes true when the process is 'freelancer.exe'
bool runningOnClient = false;
inline static FARPROC oldServerStartupFunc;
inline static auto getUserDataPathDetour =
FunctionDetour(reinterpret_cast<GetUserDataPathSig>(GetProcAddress(GetModuleHandleA("common.dll"), "?GetUserDataPath@@YA_NQAD@Z")));

// The serverClient receives data from the server
IClientImpl* serverClient = nullptr;
Expand All @@ -58,12 +62,12 @@ class Fluf

static void OnUpdateHook(double delta);
static void* OnScriptLoadHook(const char* file);
void OnGameLoad() const;
static bool __thiscall OnServerStart(IServerImpl* server, SStartupInfo& info);

static HINSTANCE __stdcall LoadLibraryDetour(LPCSTR libName);
static BOOL __stdcall FreeLibraryDetour(HMODULE module);

void OnGameLoad() const;
static bool __thiscall OnServerStart(IServerImpl* server, SStartupInfo& info);
static bool GetUserDataPathDetour(char* path);

template <typename R, typename... Args>
struct ReturnType;
Expand Down
1 change: 1 addition & 0 deletions FLUF/Include/Internal/FlufConfiguration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ class FlufConfiguration
LogLevel logLevel = LogLevel::Info;
std::unordered_set<LogSink> logSinks;
std::unordered_set<std::string> modules;
bool setSaveDirectoryRelativeToExecutable = false;
};
24 changes: 21 additions & 3 deletions FLUF/Source/Fluf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,12 @@ std::string GetLastErrorAsString()
// Ask Win32 to give us the string version of that message ID.
// The parameters we pass in, tell Win32 to create the buffer that holds the message for us (because we don't yet know how long the message string will be).
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
nullptr,
errorMessageID,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&messageBuffer,
reinterpret_cast<LPSTR>(&messageBuffer),
0,
NULL);
nullptr);

// Copy the error message into a std::string.
std::string message(messageBuffer, size);
Expand All @@ -432,6 +432,11 @@ Fluf::Fluf()
config = std::make_shared<FlufConfiguration>();
config->Load();

if (config->setSaveDirectoryRelativeToExecutable)
{
getUserDataPathDetour.Detour(GetUserDataPathDetour);
}

std::array<char, MAX_PATH> fileNameBuffer{};
GetModuleFileNameA(nullptr, fileNameBuffer.data(), MAX_PATH);
std::string_view fileName = fileNameBuffer.data();
Expand Down Expand Up @@ -529,6 +534,19 @@ Fluf::~Fluf()
{
loadedModules.pop_back();
}

if (config->setSaveDirectoryRelativeToExecutable)
{
getUserDataPathDetour.UnDetour();
}

loadLibraryDetour.UnDetour();
freeLibraryDetour.UnDetour();
}

bool Fluf::GetUserDataPathDetour(char* path)
{
constexpr std::string_view newSavePath = R"(..\SAVES)";
strcpy_s(path, newSavePath.size(), newSavePath.data());
return true;
}

0 comments on commit ec7df07

Please sign in to comment.