From ed79a975addc24cc904c44aa5fccd25ceb41ea2b Mon Sep 17 00:00:00 2001 From: searica <143636061+searica@users.noreply.github.com> Date: Sun, 8 Dec 2024 09:50:31 -0800 Subject: [PATCH 01/11] Update: BindConfig extension method --- JotunnLib/Extensions/ConfigFileExtensions.cs | 69 ++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 JotunnLib/Extensions/ConfigFileExtensions.cs diff --git a/JotunnLib/Extensions/ConfigFileExtensions.cs b/JotunnLib/Extensions/ConfigFileExtensions.cs new file mode 100644 index 000000000..bcdc8a9d3 --- /dev/null +++ b/JotunnLib/Extensions/ConfigFileExtensions.cs @@ -0,0 +1,69 @@ +using System; +using BepInEx.Configuration; + +namespace Jotunn.Extensions +{ + + /// + /// Extends ConfigFile with a convenience method to bind config entries with less boilerplate code + /// and explicitly expose commonly used configuration manager attributes. + /// + public static class ConfigFileExtensions + { + internal static string GetExtendedDescription(string description, bool synchronizedSetting) + { + return description + (synchronizedSetting ? " [Synced with Server]" : " [Not Synced with Server]"); + } + + /// + /// Bind a new config entry to the config file and modify description to state whether the config entry is synced or not. + /// + /// Type of the value the config entry holds. + /// Configuration file to bind the config entry to. + /// Configuration file section to list the config entry in. + /// Display name of the config entry. + /// Default value of the config entry. + /// Plain text description of the config entry to display as hover text in configuration manager. + /// Acceptable values for config entry as either an AcceptableValueRange or AcceptableValueList. + /// Whether the config entry IsAdminOnly and should be synced with server. + /// Order of the setting on the settings list relative to other settings in a category. 0 by default, higher number is higher on the list. + /// Custom setting editor (OnGUI code that replaces the default editor provided by ConfigurationManager). + /// Optional config manager attributes for additional user specified functionality. Any optional fields specified by the arguments of BindConfig will be overwritten by the parameters passed to BindConfig. + /// ConfigEntry bound to the config file. + public static ConfigEntry BindConfig( + this ConfigFile configFile, + string section, + string name, + T value, + string description, + AcceptableValueBase acceptVals = null, + bool synced = true, + int order = 0, + Action drawer = null, + ConfigurationManagerAttributes configAttributes = null + ) + { + string extendedDescription = GetExtendedDescription(description, synced); + + configAttributes ??= new ConfigurationManagerAttributes(); + configAttributes.IsAdminOnly = synced; + configAttributes.Order = order; + if (drawer != null) + { + configAttributes.CustomDrawer = drawer; + } + + ConfigEntry configEntry = configFile.Bind( + section, + name, + value, + new ConfigDescription( + extendedDescription, + acceptVals, + configAttributes + ) + ); + return configEntry; + } + } +} From be12f529a868b40e4236d943439448736e4c5f06 Mon Sep 17 00:00:00 2001 From: searica <143636061+searica@users.noreply.github.com> Date: Sun, 8 Dec 2024 09:51:41 -0800 Subject: [PATCH 02/11] Fix: correct acceptVals desc --- JotunnLib/Extensions/ConfigFileExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JotunnLib/Extensions/ConfigFileExtensions.cs b/JotunnLib/Extensions/ConfigFileExtensions.cs index bcdc8a9d3..239375030 100644 --- a/JotunnLib/Extensions/ConfigFileExtensions.cs +++ b/JotunnLib/Extensions/ConfigFileExtensions.cs @@ -24,7 +24,7 @@ internal static string GetExtendedDescription(string description, bool synchroni /// Display name of the config entry. /// Default value of the config entry. /// Plain text description of the config entry to display as hover text in configuration manager. - /// Acceptable values for config entry as either an AcceptableValueRange or AcceptableValueList. + /// Acceptable values for config entry as an AcceptableValueRange, AcceptableValueList, or custom subclass. /// Whether the config entry IsAdminOnly and should be synced with server. /// Order of the setting on the settings list relative to other settings in a category. 0 by default, higher number is higher on the list. /// Custom setting editor (OnGUI code that replaces the default editor provided by ConfigurationManager). From bc53b340d311a7d7ec7ba770fe9803ed15cb25b6 Mon Sep 17 00:00:00 2001 From: searica <143636061+searica@users.noreply.github.com> Date: Sun, 8 Dec 2024 10:06:14 -0800 Subject: [PATCH 03/11] Note: future todo --- JotunnLib/Extensions/ConfigFileExtensions.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/JotunnLib/Extensions/ConfigFileExtensions.cs b/JotunnLib/Extensions/ConfigFileExtensions.cs index 239375030..0e0078bac 100644 --- a/JotunnLib/Extensions/ConfigFileExtensions.cs +++ b/JotunnLib/Extensions/ConfigFileExtensions.cs @@ -12,6 +12,7 @@ public static class ConfigFileExtensions { internal static string GetExtendedDescription(string description, bool synchronizedSetting) { + // these two hardcoded strings should probably be localized return description + (synchronizedSetting ? " [Synced with Server]" : " [Not Synced with Server]"); } From 840bbc5f19723c134a41fbb3ac0915a952e44eae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Schm=C3=B6cker?= Date: Sat, 28 Dec 2024 21:40:02 +0100 Subject: [PATCH 04/11] chore: cleanup ConfigFileExtensions --- JotunnLib/Extensions/ConfigFileExtensions.cs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/JotunnLib/Extensions/ConfigFileExtensions.cs b/JotunnLib/Extensions/ConfigFileExtensions.cs index 0e0078bac..b5545c075 100644 --- a/JotunnLib/Extensions/ConfigFileExtensions.cs +++ b/JotunnLib/Extensions/ConfigFileExtensions.cs @@ -3,7 +3,6 @@ namespace Jotunn.Extensions { - /// /// Extends ConfigFile with a convenience method to bind config entries with less boilerplate code /// and explicitly expose commonly used configuration manager attributes. @@ -12,7 +11,6 @@ public static class ConfigFileExtensions { internal static string GetExtendedDescription(string description, bool synchronizedSetting) { - // these two hardcoded strings should probably be localized return description + (synchronizedSetting ? " [Synced with Server]" : " [Not Synced with Server]"); } @@ -23,9 +21,9 @@ internal static string GetExtendedDescription(string description, bool synchroni /// Configuration file to bind the config entry to. /// Configuration file section to list the config entry in. /// Display name of the config entry. - /// Default value of the config entry. + /// Default value of the config entry. /// Plain text description of the config entry to display as hover text in configuration manager. - /// Acceptable values for config entry as an AcceptableValueRange, AcceptableValueList, or custom subclass. + /// Acceptable values for config entry as an AcceptableValueRange, AcceptableValueList, or custom subclass. /// Whether the config entry IsAdminOnly and should be synced with server. /// Order of the setting on the settings list relative to other settings in a category. 0 by default, higher number is higher on the list. /// Custom setting editor (OnGUI code that replaces the default editor provided by ConfigurationManager). @@ -35,9 +33,9 @@ public static ConfigEntry BindConfig( this ConfigFile configFile, string section, string name, - T value, + T defaultValue, string description, - AcceptableValueBase acceptVals = null, + AcceptableValueBase acceptableValues = null, bool synced = true, int order = 0, Action drawer = null, @@ -46,9 +44,10 @@ public static ConfigEntry BindConfig( { string extendedDescription = GetExtendedDescription(description, synced); - configAttributes ??= new ConfigurationManagerAttributes(); + configAttributes ??= new ConfigurationManagerAttributes(); configAttributes.IsAdminOnly = synced; configAttributes.Order = order; + if (drawer != null) { configAttributes.CustomDrawer = drawer; @@ -57,14 +56,14 @@ public static ConfigEntry BindConfig( ConfigEntry configEntry = configFile.Bind( section, name, - value, + defaultValue, new ConfigDescription( extendedDescription, - acceptVals, + acceptableValues, configAttributes ) ); return configEntry; - } + } } } From f52848edb4b1cb75cb02721401c1d0aeefd10555 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Schm=C3=B6cker?= Date: Sat, 28 Dec 2024 21:58:14 +0100 Subject: [PATCH 05/11] chore: improve consistency of BindConfig --- JotunnLib/Extensions/ConfigFileExtensions.cs | 33 ++++++++------------ 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/JotunnLib/Extensions/ConfigFileExtensions.cs b/JotunnLib/Extensions/ConfigFileExtensions.cs index b5545c075..212fd5256 100644 --- a/JotunnLib/Extensions/ConfigFileExtensions.cs +++ b/JotunnLib/Extensions/ConfigFileExtensions.cs @@ -19,26 +19,26 @@ internal static string GetExtendedDescription(string description, bool synchroni /// /// Type of the value the config entry holds. /// Configuration file to bind the config entry to. - /// Configuration file section to list the config entry in. - /// Display name of the config entry. + /// Configuration file section to list the config entry in. Settings are grouped by this. + /// Name of the setting. /// Default value of the config entry. /// Plain text description of the config entry to display as hover text in configuration manager. - /// Acceptable values for config entry as an AcceptableValueRange, AcceptableValueList, or custom subclass. /// Whether the config entry IsAdminOnly and should be synced with server. /// Order of the setting on the settings list relative to other settings in a category. 0 by default, higher number is higher on the list. - /// Custom setting editor (OnGUI code that replaces the default editor provided by ConfigurationManager). - /// Optional config manager attributes for additional user specified functionality. Any optional fields specified by the arguments of BindConfig will be overwritten by the parameters passed to BindConfig. + /// Acceptable values for config entry as an AcceptableValueRange, AcceptableValueList, or custom subclass. + /// Custom setting editor (OnGUI code that replaces the default editor provided by ConfigurationManager). + /// Config manager attributes for additional user specified functionality. Any fields of BindConfig will overwrite properties in configAttributes. /// ConfigEntry bound to the config file. public static ConfigEntry BindConfig( this ConfigFile configFile, string section, - string name, + string key, T defaultValue, string description, - AcceptableValueBase acceptableValues = null, bool synced = true, - int order = 0, - Action drawer = null, + int? order = null, + AcceptableValueBase acceptableValues = null, + Action customDrawer = null, ConfigurationManagerAttributes configAttributes = null ) { @@ -47,22 +47,15 @@ public static ConfigEntry BindConfig( configAttributes ??= new ConfigurationManagerAttributes(); configAttributes.IsAdminOnly = synced; configAttributes.Order = order; - - if (drawer != null) - { - configAttributes.CustomDrawer = drawer; - } + configAttributes.CustomDrawer = customDrawer; ConfigEntry configEntry = configFile.Bind( section, - name, + key, defaultValue, - new ConfigDescription( - extendedDescription, - acceptableValues, - configAttributes - ) + new ConfigDescription(extendedDescription, acceptableValues, configAttributes) ); + return configEntry; } } From c50c02dffeef0e17955ca056164f8eca750d97ab Mon Sep 17 00:00:00 2001 From: Searica <143636061+searica@users.noreply.github.com> Date: Sat, 28 Dec 2024 13:22:23 -0800 Subject: [PATCH 06/11] Update: bind in order extension --- JotunnLib/Extensions/ConfigFileExtensions.cs | 69 ++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/JotunnLib/Extensions/ConfigFileExtensions.cs b/JotunnLib/Extensions/ConfigFileExtensions.cs index 212fd5256..98a2736be 100644 --- a/JotunnLib/Extensions/ConfigFileExtensions.cs +++ b/JotunnLib/Extensions/ConfigFileExtensions.cs @@ -9,11 +9,80 @@ namespace Jotunn.Extensions /// public static class ConfigFileExtensions { + private static readonly Dictionary _sectionToSectionNumber = []; + private static readonly Dictionary _sectionToSettingOrder = []; + + /// + /// Formats section name as "{sectionNumber} - {section}" based on how + /// many sections have been bound to this config. + /// + /// + /// + private static string GetOrderedSectionName(string section) + { + if (!_sectionToSectionNumber.TryGetValue(section, out int number)) + { + number = _sectionToSectionNumber.Count + 1; + _sectionToSectionNumber[section] = number; + } + return $"{number} - {section}"; + } + + /// + /// Orders settings within a section. + /// + /// + /// + private static int GetSettingOrder(string section) + { + if (!_sectionToSettingOrder.TryGetValue(section, out int order)) + { + order = 0; + } + _sectionToSettingOrder[section] = order - 1; + return order; + } + internal static string GetExtendedDescription(string description, bool synchronizedSetting) { return description + (synchronizedSetting ? " [Synced with Server]" : " [Not Synced with Server]"); } + /// + /// Bind a new config entry to the config file and modify description to state whether the config entry is synced or not. + /// + /// Type of the value the config entry holds. + /// Configuration file to bind the config entry to. + /// Configuration file section to list the config entry in. Settings are grouped by this. + /// Name of the setting. + /// Default value of the config entry. + /// Plain text description of the config entry to display as hover text in configuration manager. + /// Whether the config entry IsAdminOnly and should be synced with server. + /// Whether to number the section names using a prefix based on the order they are bound to the config file. + /// Whether to order the settings in each section based on the order they are bound to the config file. + /// Acceptable values for config entry as an AcceptableValueRange, AcceptableValueList, or custom subclass. + /// Custom setting editor (OnGUI code that replaces the default editor provided by ConfigurationManager). + /// Config manager attributes for additional user specified functionality. Any fields of BindConfig will overwrite properties in configAttributes. + /// ConfigEntry bound to the config file. + public static ConfigEntry BindConfig( + this ConfigFile configFile, + string section, + string key, + T defaultValue, + string description, + bool synced = true, + bool sectionOrder = true, + bool settingOrder = true, + AcceptableValueBase acceptableValues = null, + Action customDrawer = null, + ConfigurationManagerAttributes configAttributes = null + ) + { + section = sectionOrder ? GetOrderedSectionName(section) : section; + int order = settingOrder ? GetSettingOrder(section) : 0; + return configFile.BindConfig(section, key, defaultValue, description, synced, order, acceptableValues, customDrawer, configAttributes); + } + /// /// Bind a new config entry to the config file and modify description to state whether the config entry is synced or not. /// From 3afce135e7324a66b638ec6fecf50b69549cd7b5 Mon Sep 17 00:00:00 2001 From: Searica <143636061+searica@users.noreply.github.com> Date: Sat, 28 Dec 2024 13:26:46 -0800 Subject: [PATCH 07/11] Fix: correct name of bind in order method --- JotunnLib/Extensions/ConfigFileExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JotunnLib/Extensions/ConfigFileExtensions.cs b/JotunnLib/Extensions/ConfigFileExtensions.cs index 98a2736be..b56d3f80a 100644 --- a/JotunnLib/Extensions/ConfigFileExtensions.cs +++ b/JotunnLib/Extensions/ConfigFileExtensions.cs @@ -64,7 +64,7 @@ internal static string GetExtendedDescription(string description, bool synchroni /// Custom setting editor (OnGUI code that replaces the default editor provided by ConfigurationManager). /// Config manager attributes for additional user specified functionality. Any fields of BindConfig will overwrite properties in configAttributes. /// ConfigEntry bound to the config file. - public static ConfigEntry BindConfig( + public static ConfigEntryInOrder BindConfig( this ConfigFile configFile, string section, string key, From 3de9290a116746b121831af0f2e1cb7ed7cd4589 Mon Sep 17 00:00:00 2001 From: Searica <143636061+searica@users.noreply.github.com> Date: Sat, 28 Dec 2024 13:27:56 -0800 Subject: [PATCH 08/11] Fix: BindConfigInOrder name --- JotunnLib/Extensions/ConfigFileExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JotunnLib/Extensions/ConfigFileExtensions.cs b/JotunnLib/Extensions/ConfigFileExtensions.cs index b56d3f80a..f1b3afb06 100644 --- a/JotunnLib/Extensions/ConfigFileExtensions.cs +++ b/JotunnLib/Extensions/ConfigFileExtensions.cs @@ -64,7 +64,7 @@ internal static string GetExtendedDescription(string description, bool synchroni /// Custom setting editor (OnGUI code that replaces the default editor provided by ConfigurationManager). /// Config manager attributes for additional user specified functionality. Any fields of BindConfig will overwrite properties in configAttributes. /// ConfigEntry bound to the config file. - public static ConfigEntryInOrder BindConfig( + public static ConfigEntry BindConfigInOrder( this ConfigFile configFile, string section, string key, From 280a05f65640c1c4360085fec32f632099c6ea55 Mon Sep 17 00:00:00 2001 From: Searica <143636061+searica@users.noreply.github.com> Date: Sat, 28 Dec 2024 13:33:53 -0800 Subject: [PATCH 09/11] Fix: use new instead of [] --- JotunnLib/Extensions/ConfigFileExtensions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/JotunnLib/Extensions/ConfigFileExtensions.cs b/JotunnLib/Extensions/ConfigFileExtensions.cs index f1b3afb06..43a040c62 100644 --- a/JotunnLib/Extensions/ConfigFileExtensions.cs +++ b/JotunnLib/Extensions/ConfigFileExtensions.cs @@ -9,8 +9,8 @@ namespace Jotunn.Extensions /// public static class ConfigFileExtensions { - private static readonly Dictionary _sectionToSectionNumber = []; - private static readonly Dictionary _sectionToSettingOrder = []; + private static readonly Dictionary _sectionToSectionNumber = new Dictionary(); + private static readonly Dictionary _sectionToSettingOrder = new Dictionary(); /// /// Formats section name as "{sectionNumber} - {section}" based on how From 940e961db3a082859c1ba7b7e7877d1d8e045bc1 Mon Sep 17 00:00:00 2001 From: Searica <143636061+searica@users.noreply.github.com> Date: Sat, 28 Dec 2024 13:35:19 -0800 Subject: [PATCH 10/11] Fix: missing imports --- JotunnLib/Extensions/ConfigFileExtensions.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/JotunnLib/Extensions/ConfigFileExtensions.cs b/JotunnLib/Extensions/ConfigFileExtensions.cs index 43a040c62..c94f63fbf 100644 --- a/JotunnLib/Extensions/ConfigFileExtensions.cs +++ b/JotunnLib/Extensions/ConfigFileExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using BepInEx.Configuration; namespace Jotunn.Extensions From 5a9a7db1e17b9c0566170cf049e186d1f27ad996 Mon Sep 17 00:00:00 2001 From: Searica <143636061+searica@users.noreply.github.com> Date: Sat, 28 Dec 2024 13:53:30 -0800 Subject: [PATCH 11/11] Fix: auto-numbering works for multiple config files --- JotunnLib/Extensions/ConfigFileExtensions.cs | 84 ++++++++++++-------- 1 file changed, 51 insertions(+), 33 deletions(-) diff --git a/JotunnLib/Extensions/ConfigFileExtensions.cs b/JotunnLib/Extensions/ConfigFileExtensions.cs index c94f63fbf..885688332 100644 --- a/JotunnLib/Extensions/ConfigFileExtensions.cs +++ b/JotunnLib/Extensions/ConfigFileExtensions.cs @@ -10,40 +10,58 @@ namespace Jotunn.Extensions /// public static class ConfigFileExtensions { - private static readonly Dictionary _sectionToSectionNumber = new Dictionary(); - private static readonly Dictionary _sectionToSettingOrder = new Dictionary(); + internal class ConfigFileOrderingMaps + { + public Dictionary sectionToSectionNumber = new Dictionary(); + public Dictionary sectionToSettingOrder = new Dictionary(); + } + private static readonly Dictionary _configFileOrderingMaps = new Dictionary(); - /// - /// Formats section name as "{sectionNumber} - {section}" based on how - /// many sections have been bound to this config. - /// - /// - /// - private static string GetOrderedSectionName(string section) - { - if (!_sectionToSectionNumber.TryGetValue(section, out int number)) - { - number = _sectionToSectionNumber.Count + 1; - _sectionToSectionNumber[section] = number; - } - return $"{number} - {section}"; - } + /// + /// Formats section name as "{sectionNumber} - {section}" based on how + /// many sections have been bound to this config. + /// + /// + /// + private static string GetOrderedSectionName(this ConfigFile configFile, string section) + { + if (!_configFileOrderingMaps.TryGetValue(configFile.ConfigFilePath, out ConfigFileOrderingMaps orderingMaps)) + { + orderingMaps = new ConfigFileOrderingMaps(); + _configFileOrderingMaps.Add(configFile.ConfigFilePath, orderingMaps); + } + + if (!orderingMaps.sectionToSectionNumber.TryGetValue(section, out int number)) + { + number = orderingMaps.sectionToSectionNumber.Count + 1; + orderingMaps.sectionToSectionNumber[section] = number; + } - /// - /// Orders settings within a section. - /// - /// - /// - private static int GetSettingOrder(string section) - { - if (!_sectionToSettingOrder.TryGetValue(section, out int order)) - { - order = 0; - } - _sectionToSettingOrder[section] = order - 1; - return order; - } + return $"{number} - {section}"; + } + /// + /// Orders settings within a section. + /// + /// + /// + private static int GetSettingOrder(this ConfigFile configFile, string section) + { + if (!_configFileOrderingMaps.TryGetValue(configFile.ConfigFilePath, out ConfigFileOrderingMaps orderingMaps)) + { + orderingMaps = new ConfigFileOrderingMaps(); + _configFileOrderingMaps.Add(configFile.ConfigFilePath, orderingMaps); + } + + if (!orderingMaps.sectionToSettingOrder.TryGetValue(section, out int order)) + { + order = 0; + } + + orderingMaps.sectionToSettingOrder[section] = order - 1; + return order; + } + internal static string GetExtendedDescription(string description, bool synchronizedSetting) { return description + (synchronizedSetting ? " [Synced with Server]" : " [Not Synced with Server]"); @@ -79,8 +97,8 @@ public static ConfigEntry BindConfigInOrder( ConfigurationManagerAttributes configAttributes = null ) { - section = sectionOrder ? GetOrderedSectionName(section) : section; - int order = settingOrder ? GetSettingOrder(section) : 0; + section = sectionOrder ? configFile.GetOrderedSectionName(section) : section; + int order = settingOrder ? configFile.GetSettingOrder(section) : 0; return configFile.BindConfig(section, key, defaultValue, description, synced, order, acceptableValues, customDrawer, configAttributes); }