Skip to content

Commit

Permalink
Creates a separate config for TrueAmmoRemover
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottyDoesKnow committed May 17, 2024
1 parent 30e3bd7 commit 03eca58
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 28 deletions.
4 changes: 2 additions & 2 deletions include/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class Config
static InitializeResult Initialize(SInt32& ammoPercent, Logger::LogLevel& logLevel);

private:
static bool GetPath(char* path, rsize_t pathSize);
static bool GetDocumentsPath(char* path, rsize_t pathSize, const char* relativePath);
static bool Exists(const char* path);
static bool Load(const char* path, SInt32& ammoPercent, Logger::LogLevel& logLevel);
static bool GetDefaultPath(char* path, rsize_t pathSize);
static bool GetLocalPath(char* path, rsize_t pathSize, const char* relativePath);
static bool Copy(const char* sourcePath, const char* destPath);
};
2 changes: 1 addition & 1 deletion include/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#define AMMO_REMOVER_VERSION_MAJOR 1
#define AMMO_REMOVER_VERSION_MINOR 2
#define AMMO_REMOVER_VERSION_PATCH 0
#define AMMO_REMOVER_VERSION_PATCH 1
#define AMMO_REMOVER_VERSION_BETA 0

#define AMMO_REMOVER_VERSION MAKE_STR(AMMO_REMOVER_VERSION_MAJOR) "." MAKE_STR(AMMO_REMOVER_VERSION_MINOR) \
Expand Down
43 changes: 21 additions & 22 deletions src/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,43 @@

static constexpr char DEFAULT_CONFIG_PATH[] = R"(\AmmoRemoverDefaults.ini)";
static constexpr char CONFIG_PATH[] = R"(\My Games\Fallout4\F4SE\AmmoRemover.ini)";
static constexpr char CONFIG_PATH_TRUE[] = R"(\My Games\Fallout4\F4SE\TrueAmmoRemover.ini)";

static constexpr SInt32 AMMO_PERCENT_MAX = 1000;

Config::InitializeResult Config::Initialize(SInt32& ammoPercent, Logger::LogLevel& logLevel)
{
char configPath[MAX_PATH];
if (!GetPath(configPath, MAX_PATH))
char defaultConfigPath[MAX_PATH];
if (!GetLocalPath(defaultConfigPath, MAX_PATH, DEFAULT_CONFIG_PATH) || !Load(defaultConfigPath, ammoPercent, logLevel))
return Failed;

if (!Exists(configPath) || !Load(configPath, ammoPercent, logLevel))
{
char defaultConfigPath[MAX_PATH];
if (!GetDefaultPath(defaultConfigPath, MAX_PATH))
return Failed;
char configPath[MAX_PATH];
if (!GetDocumentsPath(configPath, MAX_PATH, ammoPercent == 0 ? CONFIG_PATH_TRUE : CONFIG_PATH))
return Defaults;

if (!Copy(defaultConfigPath, configPath) || !Load(configPath, ammoPercent, logLevel))
{
if (Load(defaultConfigPath, ammoPercent, logLevel))
return Defaults;
if (Exists(configPath) && Load(configPath, ammoPercent, logLevel))
return ConfigFile;

return Failed;
}
}
if (Copy(defaultConfigPath, configPath) && Load(configPath, ammoPercent, logLevel))
return ConfigFile;

if (Load(defaultConfigPath, ammoPercent, logLevel))
return Defaults;

return ConfigFile;
return Failed;
}

bool Config::GetPath(char* path, const rsize_t pathSize)
bool Config::GetDocumentsPath(char* path, const rsize_t pathSize, const char* relativePath)
{
const HRESULT error = SHGetFolderPath(nullptr, CSIDL_MYDOCUMENTS | CSIDL_FLAG_CREATE,
nullptr, SHGFP_TYPE_CURRENT, path);
if (FAILED(error))
return false;

if (strlen(path) + strlen(CONFIG_PATH) > pathSize)
if (strlen(path) + strlen(relativePath) > pathSize)
return false;

strcat_s(path, pathSize, CONFIG_PATH);
strcat_s(path, pathSize, relativePath);
return true;
}

Expand Down Expand Up @@ -79,21 +78,21 @@ bool Config::Load(const char* path, SInt32& ammoPercent, Logger::LogLevel& logLe
return true;
}

bool Config::GetDefaultPath(char* path, const rsize_t pathSize)
bool Config::GetLocalPath(char* path, const rsize_t pathSize, const char* relativePath)
{
HMODULE hModule;
if (!GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
reinterpret_cast<LPCSTR>(&GetDefaultPath), &hModule)
reinterpret_cast<LPCSTR>(&GetLocalPath), &hModule)
|| !GetModuleFileName(hModule, path, MAX_PATH)
|| !PathRemoveFileSpec(path))
{
return false;
}

if (strlen(path) + strlen(DEFAULT_CONFIG_PATH) > pathSize)
if (strlen(path) + strlen(relativePath) > pathSize)
return false;

strcat_s(path, pathSize, DEFAULT_CONFIG_PATH);
strcat_s(path, pathSize, relativePath);
return true;
}

Expand Down
6 changes: 3 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ extern "C"
TESDeathEventHandler::ammoPercent);
break;
case Config::ConfigFile:
Logger::Log(Logger::Message, "Loaded %li%% Ammo Percent.", ammoPercent);
Logger::Log(Logger::Message, "Loaded %li%% Ammo Percent.", TESDeathEventHandler::ammoPercent);
break;
case Config::Defaults:
Logger::Log(Logger::Warning, "Failed to load AmmoRemover.ini, loaded %li%% Ammo Percent from AmmoRemoverDefaults.ini",
ammoPercent);
Logger::Log(Logger::Warning, "Failed to load %sAmmoRemover.ini, loaded %li%% Ammo Percent from AmmoRemoverDefaults.ini",
TESDeathEventHandler::ammoPercent == 0 ? "True" : "", TESDeathEventHandler::ammoPercent);
break;
}

Expand Down

0 comments on commit 03eca58

Please sign in to comment.