Use .cfg over unordered map

This commit is contained in:
FutureRave 2021-12-04 18:34:19 +00:00
parent e2de20c2aa
commit 69592a5656
No known key found for this signature in database
GPG Key ID: E883E2BC9657D955
6 changed files with 46 additions and 22 deletions

View File

@ -3,7 +3,7 @@
namespace Components
{
Utils::Signal<Scheduler::Callback> Dvar::RegistrationSignal;
std::unordered_set<std::string> Dvar::ChangedDvars;
const char* Dvar::ArchiveDvarPath = "userraw/archivedvars.cfg";
Dvar::Var::Var(const std::string& dvarName) : Var()
{
@ -190,22 +190,11 @@ namespace Components
Dvar::RegistrationSignal.connect(callback);
}
void Dvar::DvarReset(Game::dvar_t* var, Game::DvarSetSource source)
{
assert(var != nullptr);
Game::Dvar_SetVariant(var, var->reset, source);
}
void Dvar::ResetDvarsValue()
{
auto it = Dvar::ChangedDvars.begin();
while (it != Dvar::ChangedDvars.end())
{
auto var = Dvar::Var(*it).get<Game::dvar_t*>();
Dvar::DvarReset(var, Game::DVAR_SOURCE_INTERNAL);
it = Dvar::ChangedDvars.erase(it);
}
Command::Execute("exec archivedvars.cfg");
// Cleanup
Utils::IO::RemoveFile(Dvar::ArchiveDvarPath);
}
Game::dvar_t* Dvar::RegisterName(const char* name, const char* /*default*/, Game::dvar_flag flag, const char* description)
@ -280,10 +269,28 @@ namespace Components
return Game::Dvar_SetFromStringByNameFromSource(dvar, value, Game::DvarSetSource::DVAR_SOURCE_EXTERNAL);
}
void Dvar::DvarSetFromStringByNameStub(const char* var, const char* value)
void Dvar::SaveArchiveDvar(const Game::dvar_t* var)
{
Dvar::ChangedDvars.emplace(var);
Utils::Hook::Call<void(const char*, const char*)>(0x4F52E0)(var, value);
if (!Utils::IO::FileExists(Dvar::ArchiveDvarPath))
{
Utils::IO::WriteFile(Dvar::ArchiveDvarPath,
"// generated by IW4x, do not modify\n");
}
Utils::IO::WriteFile(Dvar::ArchiveDvarPath,
Utils::String::VA("seta %s \"%s\"\n", var->name, Game::Dvar_DisplayableValue(var)), true);
}
void Dvar::DvarSetFromStringByNameStub(const char* dvarName, const char* value)
{
// Save the dvar original value if it has the archive flag
const auto* dvar = Game::Dvar_FindVar(dvarName);
if (dvar != nullptr && dvar->flags & Game::dvar_flag::DVAR_FLAG_SAVED)
{
Dvar::SaveArchiveDvar(dvar);
}
Utils::Hook::Call<void(const char*, const char*)>(0x4F52E0)(dvarName, value);
}
Dvar::Dvar()
@ -356,11 +363,14 @@ namespace Components
// Hook Dvar_SetFromStringByName inside CG_SetClientDvarFromServer so we can reset dvars when the player leaves the server
Utils::Hook(0x59386A, Dvar::DvarSetFromStringByNameStub, HOOK_CALL).install()->quick();
// If the game closed abruptly the file would have not been deleted
Utils::IO::RemoveFile(Dvar::ArchiveDvarPath);
}
Dvar::~Dvar()
{
Dvar::RegistrationSignal.clear();
Dvar::ChangedDvars.clear();
Utils::IO::RemoveFile(Dvar::ArchiveDvarPath);
}
}

View File

@ -50,18 +50,18 @@ namespace Components
template<typename T> static Var Register(const char* name, T value, Flag flag, const char* description);
template<typename T> static Var Register(const char* name, T value, T min, T max, Flag flag, const char* description);
static void DvarReset(Game::dvar_t* var, Game::DvarSetSource source);
static void ResetDvarsValue();
private:
static Utils::Signal<Scheduler::Callback> RegistrationSignal;
static std::unordered_set<std::string> ChangedDvars;
static const char* ArchiveDvarPath;
static Game::dvar_t* RegisterName(const char* name, const char* defaultVal, Game::dvar_flag flag, const char* description);
static Game::dvar_t* SetFromStringByNameExternal(const char* dvar, const char* value);
static Game::dvar_t* SetFromStringByNameSafeExternal(const char* dvar, const char* value);
static void DvarSetFromStringByNameStub(const char* var, const char* value);
static void SaveArchiveDvar(const Game::dvar_t* var);
static void DvarSetFromStringByNameStub(const char* dvarName, const char* value);
};
}

View File

@ -116,6 +116,7 @@ namespace Game
Dvar_FindVar_t Dvar_FindVar = Dvar_FindVar_t(0x4D5390);
Dvar_InfoString_Big_t Dvar_InfoString_Big = Dvar_InfoString_Big_t(0x4D98A0);
Dvar_SetCommand_t Dvar_SetCommand = Dvar_SetCommand_t(0x4EE430);
Dvar_DisplayableValue_t Dvar_DisplayableValue = Dvar_DisplayableValue_t(0x4B5530);
Encode_Init_t Encode_Init = Encode_Init_t(0x462AB0);
@ -145,6 +146,7 @@ namespace Game
FS_Restart_t FS_Restart = FS_Restart_t(0x461A50);
FS_BuildPathToFile_t FS_BuildPathToFile = FS_BuildPathToFile_t(0x4702C0);
FS_IsShippedIWD_t FS_IsShippedIWD = FS_IsShippedIWD_t(0x642440);
FS_Delete_t FS_Delete = FS_Delete_t(0x48A5B0);
G_GetWeaponIndexForName_t G_GetWeaponIndexForName = G_GetWeaponIndexForName_t(0x49E540);
G_SpawnEntitiesFromString_t G_SpawnEntitiesFromString = G_SpawnEntitiesFromString_t(0x4D8840);

View File

@ -289,6 +289,9 @@ namespace Game
typedef dvar_t* (__cdecl * Dvar_SetCommand_t)(const char* name, const char* value);
extern Dvar_SetCommand_t Dvar_SetCommand;
typedef const char* (__cdecl * Dvar_DisplayableValue_t)(const dvar_t* cvar);
extern Dvar_DisplayableValue_t Dvar_DisplayableValue;
typedef bool(__cdecl * Encode_Init_t)(const char* );
extern Encode_Init_t Encode_Init;
@ -359,6 +362,9 @@ namespace Game
typedef iwd_t*(__cdecl * FS_IsShippedIWD_t)(const char* fullpath, const char* iwd);
extern FS_IsShippedIWD_t FS_IsShippedIWD;
typedef int(__cdecl* FS_Delete_t)(const char* fileName);
extern FS_Delete_t FS_Delete;
typedef int(__cdecl* G_GetWeaponIndexForName_t)(char*);
extern G_GetWeaponIndexForName_t G_GetWeaponIndexForName;

View File

@ -63,6 +63,11 @@ namespace Utils
return false;
}
bool RemoveFile(const std::string& file)
{
return DeleteFileA(file.data()) == TRUE;
}
size_t FileSize(const std::string& file)
{
if (FileExists(file))

View File

@ -8,6 +8,7 @@ namespace Utils
bool WriteFile(const std::string& file, const std::string& data, bool append = false);
bool ReadFile(const std::string& file, std::string* data);
std::string ReadFile(const std::string& file);
bool RemoveFile(const std::string& file);
size_t FileSize(const std::string& file);
bool CreateDir(const std::string& dir);
bool DirectoryExists(const std::string& file);