Skip to content

Commit

Permalink
Treewide: .ini configs -> .json configs (#3719)
Browse files Browse the repository at this point in the history
Successor to #2717.

Converts all config files to new JSON format on write while keeping
compatibility with old .ini config files on read. Values are written as
their actual type instead of stringifying everything. Also includes a
bunch of (mostly) related cleanups and new logs plastered around in
config related code.
  • Loading branch information
pongo1231 authored Feb 20, 2025
1 parent 01fa5ea commit 9723493
Show file tree
Hide file tree
Showing 26 changed files with 500 additions and 383 deletions.
86 changes: 43 additions & 43 deletions ChaosMod/Components/CrossingChallenge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void CrossingChallenge::SetStartParams()
_SET_WEATHER_TYPE_TRANSITION(m_StartWeatherType1, m_StartWeatherType2, m_StartWeatherPercent);
SET_CLOCK_TIME(m_ClockHours, m_ClockMinutes, m_ClockSeconds);
REMOVE_ALL_PED_WEAPONS(player, false);
for (WeaponInfo weapon : m_StartWeapons)
for (const auto &weapon : m_StartWeapons)
GIVE_WEAPON_TO_PED(player, weapon.hash, weapon.ammo, false, false);

CLEAR_PLAYER_WANTED_LEVEL(PLAYER_ID());
Expand Down Expand Up @@ -196,28 +196,28 @@ static std::string GetWeaponOptionName(Hash weapon)

void CrossingChallenge::SaveConfig()
{
m_ConfigFile.SetValue<bool>("StartEnabled", m_StartEnabled);
m_ConfigFile.SetValue<float>("StartLocationX", m_StartLocation.x);
m_ConfigFile.SetValue<float>("StartLocationY", m_StartLocation.y);
m_ConfigFile.SetValue<float>("StartLocationZ", m_StartLocation.z);
m_ConfigFile.SetValue<Hash>("StartVehicle", m_StartVehicleHash);
m_ConfigFile.SetValue<float>("StartHeading", m_StartHeading);
m_ConfigFile.SetValue<float>("StartCameraHeading", m_StartCameraHeading);
m_ConfigFile.SetValue<Hash>("StartWeather1", m_StartWeatherType1);
m_ConfigFile.SetValue<Hash>("StartWeather2", m_StartWeatherType2);
m_ConfigFile.SetValue<float>("StartWeatherPercent", m_StartWeatherPercent);
m_ConfigFile.SetValue<int>("StartHours", m_ClockHours);
m_ConfigFile.SetValue<int>("StartMinutes", m_ClockMinutes);
m_ConfigFile.SetValue<int>("StartSeconds", m_ClockSeconds);

for (WeaponInfo weapon : m_StartWeapons)
m_ConfigFile.SetValue<int>(GetWeaponOptionName(weapon.hash), weapon.ammo);

m_ConfigFile.SetValue<bool>("EndEnabled", m_EndEnabled);
m_ConfigFile.SetValue<float>("EndLocationX", m_EndLocation.x);
m_ConfigFile.SetValue<float>("EndLocationY", m_EndLocation.y);
m_ConfigFile.SetValue<float>("EndLocationZ", m_EndLocation.z);
m_ConfigFile.SetValue<float>("EndRadius", m_EndRadius);
m_ConfigFile.SetValue("StartEnabled", m_StartEnabled);
m_ConfigFile.SetValue("StartLocationX", m_StartLocation.x);
m_ConfigFile.SetValue("StartLocationY", m_StartLocation.y);
m_ConfigFile.SetValue("StartLocationZ", m_StartLocation.z);
m_ConfigFile.SetValue("StartVehicle", m_StartVehicleHash);
m_ConfigFile.SetValue("StartHeading", m_StartHeading);
m_ConfigFile.SetValue("StartCameraHeading", m_StartCameraHeading);
m_ConfigFile.SetValue("StartWeather1", m_StartWeatherType1);
m_ConfigFile.SetValue("StartWeather2", m_StartWeatherType2);
m_ConfigFile.SetValue("StartWeatherPercent", m_StartWeatherPercent);
m_ConfigFile.SetValue("StartHours", m_ClockHours);
m_ConfigFile.SetValue("StartMinutes", m_ClockMinutes);
m_ConfigFile.SetValue("StartSeconds", m_ClockSeconds);

for (const auto &weapon : m_StartWeapons)
m_ConfigFile.SetValue(GetWeaponOptionName(weapon.hash), weapon.ammo);

m_ConfigFile.SetValue("EndEnabled", m_EndEnabled);
m_ConfigFile.SetValue("EndLocationX", m_EndLocation.x);
m_ConfigFile.SetValue("EndLocationY", m_EndLocation.y);
m_ConfigFile.SetValue("EndLocationZ", m_EndLocation.z);
m_ConfigFile.SetValue("EndRadius", m_EndRadius);

m_ConfigFile.WriteFile();
}
Expand Down Expand Up @@ -259,42 +259,42 @@ void CrossingChallenge::CaptureEnd()

CrossingChallenge::CrossingChallenge()
{
m_Enabled = g_OptionsManager.GetConfigValue<bool>({ "EnableCrossingChallenge" }, false);
m_Enabled = g_OptionsManager.GetConfigValue({ "EnableCrossingChallenge" }, false);

if (!m_Enabled)
return;

m_StartEnabled = m_ConfigFile.ReadValue<bool>("StartEnabled", false);
m_StartEnabled = m_ConfigFile.ReadValue({ "StartEnabled" }, false);
if (m_StartEnabled)
{
m_StartLocation = Vector3(m_ConfigFile.ReadValue<float>("StartLocationX", 0.f),
m_ConfigFile.ReadValue<float>("StartLocationY", 0.f),
m_ConfigFile.ReadValue<float>("StartLocationZ", 0.f));
m_StartVehicleHash = m_ConfigFile.ReadValue<Hash>("StartVehicle", 0);
m_StartHeading = m_ConfigFile.ReadValue<float>("StartHeading", 0.f);
m_StartCameraHeading = m_ConfigFile.ReadValue<float>("StartCameraHeading", 0.f);
m_StartWeatherType1 = m_ConfigFile.ReadValue<Hash>("StartWeather1", 0);
m_StartWeatherType2 = m_ConfigFile.ReadValue<Hash>("StartWeather2", 0);
m_StartWeatherPercent = m_ConfigFile.ReadValue<float>("StartWeatherPercent", 0.f);
m_ClockHours = m_ConfigFile.ReadValue<int>("StartHours", 0);
m_ClockMinutes = m_ConfigFile.ReadValue<int>("StartMinutes", 0);
m_ClockSeconds = m_ConfigFile.ReadValue<int>("StartSeconds", 0);
m_StartLocation = Vector3(m_ConfigFile.ReadValue({ "StartLocationX" }, 0.f),
m_ConfigFile.ReadValue({ "StartLocationY" }, 0.f),
m_ConfigFile.ReadValue({ "StartLocationZ" }, 0.f));
m_StartVehicleHash = m_ConfigFile.ReadValue({ "StartVehicle" }, 0);
m_StartHeading = m_ConfigFile.ReadValue({ "StartHeading" }, 0.f);
m_StartCameraHeading = m_ConfigFile.ReadValue({ "StartCameraHeading" }, 0.f);
m_StartWeatherType1 = m_ConfigFile.ReadValue({ "StartWeather1" }, 0);
m_StartWeatherType2 = m_ConfigFile.ReadValue({ "StartWeather2" }, 0);
m_StartWeatherPercent = m_ConfigFile.ReadValue({ "StartWeatherPercent" }, 0.f);
m_ClockHours = m_ConfigFile.ReadValue({ "StartHours" }, 0);
m_ClockMinutes = m_ConfigFile.ReadValue({ "StartMinutes" }, 0);
m_ClockSeconds = m_ConfigFile.ReadValue({ "StartSeconds" }, 0);

for (Hash hash : Memory::GetAllWeapons())
{
int ammo = m_ConfigFile.ReadValue<int>(GetWeaponOptionName(hash), -1);
int ammo = m_ConfigFile.ReadValue({ GetWeaponOptionName(hash) }, -1);
if (ammo >= 0)
m_StartWeapons.emplace_back(WeaponInfo { hash, ammo });
}
}

m_EndEnabled = m_ConfigFile.ReadValue<bool>("EndEnabled", false);
m_EndEnabled = m_ConfigFile.ReadValue({ "EndEnabled" }, false);
if (m_EndEnabled)
{
m_EndLocation = Vector3(m_ConfigFile.ReadValue<float>("EndLocationX", 0.f),
m_ConfigFile.ReadValue<float>("EndLocationY", 0.f),
m_ConfigFile.ReadValue<float>("EndLocationZ", 0.f));
m_EndRadius = m_ConfigFile.ReadValue<float>("EndRadius", 0.f);
m_EndLocation =
Vector3(m_ConfigFile.ReadValue({ "EndLocationX" }, 0.f), m_ConfigFile.ReadValue({ "EndLocationY" }, 0.f),
m_ConfigFile.ReadValue({ "EndLocationZ" }, 0.f));
m_EndRadius = m_ConfigFile.ReadValue({ "EndRadius" }, 0.f);
}

if (ComponentExists<EffectDispatcher>())
Expand Down
2 changes: 1 addition & 1 deletion ChaosMod/Components/CrossingChallenge.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CrossingChallenge : public Component
int ammo;
};

OptionsFile m_ConfigFile { "chaosmod/configs/crossing.ini", { "chaosmod/crossing.ini" } };
OptionsFile m_ConfigFile { { "chaosmod/configs/cached/crossingchallenge.json", "chaosmod/crossing.ini" } };

bool m_Enabled = false;

Expand Down
7 changes: 5 additions & 2 deletions ChaosMod/Components/EffectDispatchTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ EffectDispatchTimer::EffectDispatchTimer(const std::array<BYTE, 3> &timerColor)
m_DrawTimerBar = !g_OptionsManager.GetConfigValue({ "DisableTimerBarDraw" }, OPTION_DEFAULT_NO_EFFECT_BAR);
m_EffectSpawnTime = g_OptionsManager.GetConfigValue({ "NewEffectSpawnTime" }, OPTION_DEFAULT_EFFECT_SPAWN_TIME);

m_DistanceChaosState.EnableDistanceBasedEffectDispatch = g_OptionsManager.GetConfigValue(
{ "EnableDistanceBasedEffectDispatch" }, OPTION_DEFAULT_DISTANCE_BASED_DISPATCH_ENABLED);
m_DistanceChaosState.EnableDistanceBasedEffectDispatch =
g_OptionsManager.GetConfigValue({ "EffectDispatchMode", " EnableDistanceBasedEffectDispatch " },
OPTION_DEFAULT_DISTANCE_BASED_DISPATCH_ENABLED)
? true
: false;
m_DistanceChaosState.DistanceToActivateEffect =
g_OptionsManager.GetConfigValue<float>({ "DistanceToActivateEffect" }, OPTION_DEFAULT_EFFECT_SPAWN_DISTANCE);
m_DistanceChaosState.DistanceType = static_cast<DistanceChaosState::TravelledDistanceType>(
Expand Down
2 changes: 1 addition & 1 deletion ChaosMod/Components/Voting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Voting::Voting(const std::array<BYTE, 3> &textColor) : Component(), m_TextColor(
{
m_EnableVoting =
g_OptionsManager.GetVotingValue({ "EnableVoting", "EnableTwitchVoting" }, OPTION_DEFAULT_TWITCH_VOTING_ENABLED);
m_VoteablePrefix = g_OptionsManager.GetVotingValue<std::string>({ "VoteablePrefix" }, "");
m_VoteablePrefix = g_OptionsManager.GetVotingValue<std::string>({ "VoteablePrefix" });
}

void Voting::OnModPauseCleanup(PauseCleanupFlags cleanupFlags)
Expand Down
71 changes: 44 additions & 27 deletions ChaosMod/Effects/EffectConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ namespace EffectConfig
return -1;
}

inline void ReadConfig(const char *configPath, auto &out, std::vector<const char *> compatConfigPaths = {})
inline void ReadConfig(std::vector<std::string_view> lookupPaths, auto &out)
{
OptionsFile effectsFile(configPath, compatConfigPaths);
DEBUG_LOG("Parsing effect config");

OptionsFile effectsFile(lookupPaths);

bool isJson = effectsFile.GetFoundFileName().ends_with(".json");
for (auto &[effectId, effectMetadata] : g_RegisteredEffectsMetadata)
{
struct ConfigValues
Expand All @@ -59,35 +62,47 @@ namespace EffectConfig
// HACK: Store EffectCustomName seperately
std::string valueEffectName;

auto value = effectsFile.ReadValueString({ std::string(effectId) });
if (!value.empty())
if (isJson)
{
size_t splitIndex = GetNextDelimiterOffset(value);
for (int j = 0;; j++)
{
// Effect-Name override
if (j == 6)
{
auto split = value.substr(0, splitIndex);
// Trim surrounding quotations
if (split.length() >= 2 && split[0] == '\"' && split[split.length() - 1] == '\"')
split = split.substr(1, split.size() - 2);
// Names can't be "0" to support older configs
if (!split.empty() && split != "0")
valueEffectName = split;
}
auto jsonArray = effectsFile.ReadValue<nlohmann::json::array_t>({ std::string(effectId) });
for (size_t i = 0; i < jsonArray.size(); i++)
if (i == 6)
valueEffectName = jsonArray[i];
else
configValues.ValuesRaw[i] = jsonArray[i];
}
else
{
auto value = effectsFile.ReadValue<std::string>({ std::string(effectId) });
if (!value.empty())
{
size_t splitIndex = GetNextDelimiterOffset(value);
for (int i = 0;; i++)
{
const auto &split = value.substr(0, splitIndex);

Util::TryParse<int>(split, configValues.ValuesRaw[j]);
// Effect-Name override
if (i == 6)
{
auto split = value.substr(0, splitIndex);
// Trim surrounding quotations
if (split.length() >= 2 && split[0] == '\"' && split[split.length() - 1] == '\"')
split = split.substr(1, split.size() - 2);
// Names can't be "0" to support older configs
if (!split.empty() && split != "0")
valueEffectName = split;
}
else
{
const auto &split = value.substr(0, splitIndex);

Util::TryParse<int>(split, configValues.ValuesRaw[i]);
}

if (splitIndex == value.npos)
break;

value = value.substr(splitIndex + 1);
splitIndex = GetNextDelimiterOffset(value);
}

if (splitIndex == value.npos)
break;

value = value.substr(splitIndex + 1);
splitIndex = GetNextDelimiterOffset(value);
}
}

Expand Down Expand Up @@ -148,5 +163,7 @@ namespace EffectConfig

out.emplace(EffectIdentifier(std::string(effectId)), effectData);
}

DEBUG_LOG("Parsed effect config");
}
}
7 changes: 5 additions & 2 deletions ChaosMod/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ static void ParseEffectsFile()
{
g_EnabledEffects.clear();

EffectConfig::ReadConfig("chaosmod/configs/effects.ini", g_EnabledEffects, { "chaosmod/effects.ini" });
EffectConfig::ReadConfig(
{ "chaosmod/configs/effects.json", "chaosmod/configs/effects.ini", "chaosmod/effects.ini" }, g_EnabledEffects);
}

static void Init()
Expand Down Expand Up @@ -139,7 +140,9 @@ static void Init()
const auto &effectTimerColor = ParseConfigColorString(
g_OptionsManager.GetConfigValue<std::string>({ "EffectTimedTimerColor" }, OPTION_DEFAULT_TIMED_COLOR));

g_Random.SetSeed(g_OptionsManager.GetConfigValue({ "Seed" }, 0));
auto seed = g_OptionsManager.GetConfigValue<std::string>({ "Seed" });
if (!seed.empty())
g_Random.SetSeed(std::hash<std::string> {}(seed));
g_RandomNoDeterm.SetSeed(GetTickCount64());

std::set<std::string> blacklistedComponentNames;
Expand Down
Loading

0 comments on commit 9723493

Please sign in to comment.