Skip to content

Commit

Permalink
- weird config issues fixed! yay! (large numbers instead of decimals …
Browse files Browse the repository at this point in the history
…no more)
  • Loading branch information
mfoltz committed Nov 26, 2024
1 parent 4746ef3 commit c8db012
Showing 1 changed file with 136 additions and 1 deletion.
137 changes: 136 additions & 1 deletion Services/ConfigService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BepInEx.Configuration;
using System.Globalization;
using System.Reflection;
using System.Text.RegularExpressions;

Expand Down Expand Up @@ -388,6 +389,37 @@ public static void InitializeConfig()
if (oldConfigValues.TryGetValue(entry.Key, out var oldValue))
{
// Convert the old value to the correct type

try
{
object convertedValue;

if (entryType == typeof(float))
{
convertedValue = float.Parse(oldValue, CultureInfo.InvariantCulture);
}
else if (entryType == typeof(double))
{
convertedValue = double.Parse(oldValue, CultureInfo.InvariantCulture);
}
else if (entryType == typeof(decimal))
{
convertedValue = decimal.Parse(oldValue, CultureInfo.InvariantCulture);
}
else
{
convertedValue = Convert.ChangeType(oldValue, entryType);
}

var configEntry = generic.Invoke(null, [entry.Section, entry.Key, convertedValue, entry.Description]);
UpdateConfigProperty(entry.Key, configEntry);
}
catch (Exception ex)
{
Plugin.LogInstance.LogError($"Failed to convert old config value for {entry.Key}: {ex.Message}");
}

/*
try
{
var convertedValue = Convert.ChangeType(oldValue, entryType);
Expand All @@ -398,12 +430,12 @@ public static void InitializeConfig()
{
Plugin.LogInstance.LogError($"Failed to convert old config value for {entry.Key}: {ex.Message}");
}
*/
}
else
{
// Use default value if key is not in the old config
var configEntry = generic.Invoke(null, [entry.Section, entry.Key, entry.DefaultValue, entry.Description]);

UpdateConfigProperty(entry.Key, configEntry);
}
}
Expand Down Expand Up @@ -436,6 +468,108 @@ static ConfigEntry<T> InitConfigEntry<T>(string section, string key, T defaultVa
// Define the path to the configuration file
var configFile = Path.Combine(BepInEx.Paths.ConfigPath, $"{MyPluginInfo.PLUGIN_GUID}.cfg");

// Ensure the configuration file is only loaded if it exists
if (File.Exists(configFile))
{
string[] configLines = File.ReadAllLines(configFile);
//Plugin.LogInstance.LogInfo(configLines);
foreach (var line in configLines)
{
if (string.IsNullOrWhiteSpace(line) || line.TrimStart().StartsWith("#"))
{
continue;
}

var keyValue = line.Split('=');
if (keyValue.Length == 2)
{
var configKey = keyValue[0].Trim();
var configValue = keyValue[1].Trim();

// Check if the key matches the provided key
if (configKey.Equals(key, StringComparison.OrdinalIgnoreCase))
{
// Try to convert the string value to the expected type
try
{
object convertedValue;

Type t = typeof(T);

if (t == typeof(float))
{
convertedValue = float.Parse(configValue, CultureInfo.InvariantCulture);
}
else if (t == typeof(double))
{
convertedValue = double.Parse(configValue, CultureInfo.InvariantCulture);
}
else if (t == typeof(decimal))
{
convertedValue = decimal.Parse(configValue, CultureInfo.InvariantCulture);
}
else if (t == typeof(int))
{
convertedValue = int.Parse(configValue, CultureInfo.InvariantCulture);
}
else if (t == typeof(uint))
{
convertedValue = uint.Parse(configValue, CultureInfo.InvariantCulture);
}
else if (t == typeof(long))
{
convertedValue = long.Parse(configValue, CultureInfo.InvariantCulture);
}
else if (t == typeof(ulong))
{
convertedValue = ulong.Parse(configValue, CultureInfo.InvariantCulture);
}
else if (t == typeof(short))
{
convertedValue = short.Parse(configValue, CultureInfo.InvariantCulture);
}
else if (t == typeof(ushort))
{
convertedValue = ushort.Parse(configValue, CultureInfo.InvariantCulture);
}
else if (t == typeof(bool))
{
convertedValue = bool.Parse(configValue);
}
else if (t == typeof(string))
{
convertedValue = configValue;
}
else
{
// Handle other types or throw an exception
throw new NotSupportedException($"Type {t} is not supported");
}

entry.Value = (T)convertedValue;
}
catch (Exception ex)
{
Plugin.LogInstance.LogError($"Failed to convert config value for {key}: {ex.Message}");
}

break; // Stop searching once the key is found
}
}
}
}

return entry;
}
/*
static ConfigEntry<T> InitConfigEntry<T>(string section, string key, T defaultValue, string description)
{
// Bind the configuration entry with the default value in the new section
var entry = Plugin.Instance.Config.Bind(section, key, defaultValue, description);
// Define the path to the configuration file
var configFile = Path.Combine(BepInEx.Paths.ConfigPath, $"{MyPluginInfo.PLUGIN_GUID}.cfg");
// Ensure the configuration file is only loaded if it exists
if (File.Exists(configFile))
{
Expand Down Expand Up @@ -484,6 +618,7 @@ static ConfigEntry<T> InitConfigEntry<T>(string section, string key, T defaultVa
return entry;
}
*/
static void CreateDirectory(string path)
{
if (!Directory.Exists(path))
Expand Down

0 comments on commit c8db012

Please sign in to comment.