Skip to content

Commit 8f99199

Browse files
committed
v1.0.0
Configuration Manager!!!
1 parent 0986760 commit 8f99199

37 files changed

+2258
-0
lines changed

DistanceModConfigurationManager.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.33529.622
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DistanceModConfigurationManager", "DistanceModConfigurationManager\DistanceModConfigurationManager.csproj", "{9BC754F3-4BBE-4C84-B06A-79311691DD74}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{9BC754F3-4BBE-4C84-B06A-79311691DD74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{9BC754F3-4BBE-4C84-B06A-79311691DD74}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{9BC754F3-4BBE-4C84-B06A-79311691DD74}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{9BC754F3-4BBE-4C84-B06A-79311691DD74}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {F11E1C9E-CC5B-4554-B615-C716DB59457A}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using BepInEx;
2+
using BepInEx.Configuration;
3+
using System;
4+
using System.Collections;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Reflection;
8+
9+
namespace DistanceModConfigurationManager
10+
{
11+
internal sealed class ConfigSettingEntry : SettingEntryBase
12+
{
13+
public ConfigEntryBase Entry { get; }
14+
15+
public ConfigSettingEntry(ConfigEntryBase entry, BaseUnityPlugin owner)
16+
{
17+
Entry = entry;
18+
19+
DispName = entry.Definition.Key;
20+
Category = entry.Definition.Section;
21+
Description = entry.Description?.Description;
22+
23+
var converter = TomlTypeConverter.GetConverter(entry.SettingType);
24+
if (converter != null)
25+
{
26+
ObjToStr = o => converter.ConvertToString(o, entry.SettingType);
27+
StrToObj = s => converter.ConvertToObject(s, entry.SettingType);
28+
}
29+
30+
var values = entry.Description?.AcceptableValues;
31+
if (values != null)
32+
GetAcceptableValues(values);
33+
34+
DefaultValue = entry.DefaultValue;
35+
36+
SetFromAttributes(entry.Description?.Tags, owner);
37+
}
38+
39+
private void GetAcceptableValues(AcceptableValueBase values)
40+
{
41+
var t = values.GetType();
42+
var listProp = t.GetProperty(nameof(AcceptableValueList<bool>.AcceptableValues), BindingFlags.Instance | BindingFlags.Public);
43+
if (listProp != null)
44+
{
45+
AcceptableValues = ((IEnumerable)listProp.GetValue(values, null)).Cast<object>().ToArray();
46+
}
47+
else
48+
{
49+
var minProp = t.GetProperty(nameof(AcceptableValueRange<bool>.MinValue), BindingFlags.Instance | BindingFlags.Public);
50+
var maxProp = t.GetProperty(nameof(AcceptableValueRange<bool>.MaxValue), BindingFlags.Instance | BindingFlags.Public);
51+
if (minProp != null && maxProp != null)
52+
{
53+
AcceptableValueRange = new KeyValuePair<object, object>(minProp.GetValue(values, null), maxProp.GetValue(values, null));
54+
ShowRangeAsPercent = (AcceptableValueRange.Key.Equals(0) || AcceptableValueRange.Key.Equals(1)) && AcceptableValueRange.Value.Equals(100) || AcceptableValueRange.Key.Equals(0f) && AcceptableValueRange.Value.Equals(1f);
55+
}
56+
}
57+
}
58+
59+
public override Type SettingType => Entry.SettingType;
60+
61+
public override object Get()
62+
{
63+
return Entry.BoxedValue;
64+
}
65+
66+
protected override void SetValue(object newVal)
67+
{
68+
Entry.BoxedValue = newVal;
69+
}
70+
}
71+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using DistanceModConfigurationManager.DistanceGUI.Data;
2+
using DistanceModConfigurationManager.DistanceGUI.Menu;
3+
using System;
4+
5+
namespace DistanceModConfigurationManager.DistanceGUI.Controls
6+
{
7+
public class ActionButton : MenuItemBase
8+
{
9+
public Action OnClick { get; set; }
10+
public ActionButton(MenuDisplayMode mode, string id, string name) : base(mode, id, name) { }
11+
public ActionButton WhenClicked(Action onClick)
12+
{
13+
OnClick = onClick;
14+
return this;
15+
}
16+
public override void Tweak(ModdingMenu menu)
17+
{
18+
if (OnClick == null)
19+
{
20+
throw new InvalidOperationException("OnClick action not initialized. Use WhenClicked() to configure the action.");
21+
}
22+
else
23+
{
24+
menu.TweakAction(Name, OnClick, Description);
25+
base.Tweak(menu);
26+
}
27+
}
28+
}
29+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using DistanceModConfigurationManager.DistanceGUI.Data;
2+
using DistanceModConfigurationManager.DistanceGUI.Menu;
3+
using System;
4+
5+
namespace DistanceModConfigurationManager.DistanceGUI.Controls
6+
{
7+
public class CheckBox : MenuItemBase
8+
{
9+
public Func<bool> Get { get; set; }
10+
public Action<bool> Set { get; set; }
11+
public bool DefaultValue { get; private set; }
12+
public CheckBox(MenuDisplayMode mode, string id, string name) : base(mode, id, name)
13+
{
14+
Get = () => DefaultValue;
15+
Set = new Action<bool>((_) => { });
16+
}
17+
public CheckBox WithDefaultValue(bool defaultValue)
18+
{
19+
DefaultValue = defaultValue;
20+
return this;
21+
}
22+
public CheckBox WithGetter(Func<bool> getter)
23+
{
24+
Get = getter ?? throw new ArgumentNullException("Getter cannot be null");
25+
return this;
26+
}
27+
public CheckBox WithSetter(Action<bool> setter)
28+
{
29+
Set = setter ?? throw new ArgumentNullException("Setter cannot be null.");
30+
return this;
31+
}
32+
public override void Tweak(ModdingMenu menu)
33+
{
34+
if (Get == null || Set == null)
35+
{
36+
throw new InvalidOperationException("Cannot call Tweak with Get or Set being null.");
37+
}
38+
39+
menu.TweakBool(Name, Get(), Set, Description);
40+
base.Tweak(menu);
41+
}
42+
}
43+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace DistanceModConfigurationManager.DistanceGUI.Controls
2+
{
3+
internal class EmptyElement : MenuItemBase
4+
{
5+
public EmptyElement() : base(Data.MenuDisplayMode.Both, "empty", string.Empty) { }
6+
}
7+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using DistanceModConfigurationManager.DistanceGUI.Data;
2+
using DistanceModConfigurationManager.DistanceGUI.Menu;
3+
using System;
4+
5+
namespace DistanceModConfigurationManager.DistanceGUI.Controls
6+
{
7+
public class FloatSlider : MenuItemBase
8+
{
9+
public float Minimum { get; private set; } = 0.0f;
10+
public float Maximum { get; private set; } = 1.0f;
11+
public Func<float> Get { get; set; }
12+
public Action<float> Set { get; set; }
13+
public float DefaultValue { get; set; }
14+
public FloatSlider(MenuDisplayMode mode, string id, string name) : base(mode, id, name)
15+
{
16+
Get = () => DefaultValue;
17+
Set = (_) => { };
18+
}
19+
public FloatSlider WithDefaultValue(float defaultValue)
20+
{
21+
if (defaultValue > Maximum || defaultValue < Minimum)
22+
{
23+
throw new ArgumentOutOfRangeException("Default value must be between minimum and maximum values.");
24+
}
25+
26+
DefaultValue = defaultValue;
27+
return this;
28+
}
29+
public FloatSlider LimitedByRange(float minimum, float maximum)
30+
{
31+
if (Minimum > Maximum)
32+
{
33+
throw new ArgumentException("Minimum must be lower than maximum.");
34+
}
35+
36+
Minimum = minimum;
37+
Maximum = maximum;
38+
39+
return this;
40+
}
41+
public FloatSlider WithGetter(Func<float> getter)
42+
{
43+
Get = getter ?? throw new ArgumentNullException("Getter cannot be null.");
44+
return this;
45+
}
46+
public FloatSlider WithSetter(Action<float> setter)
47+
{
48+
Set = setter ?? throw new ArgumentNullException("Setter cannot be null.");
49+
return this;
50+
}
51+
public override void Tweak(ModdingMenu menu)
52+
{
53+
if (Get == null || Set == null)
54+
{
55+
throw new InvalidOperationException("Cannot call Tweak with Get or Set method being null.");
56+
}
57+
58+
menu.TweakFloat(Name, Get(), Minimum, Maximum, DefaultValue, Set, Description);
59+
base.Tweak(menu);
60+
}
61+
}
62+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using DistanceModConfigurationManager.DistanceGUI.Data;
2+
using DistanceModConfigurationManager.DistanceGUI.Menu;
3+
using System;
4+
5+
namespace DistanceModConfigurationManager.DistanceGUI.Controls
6+
{
7+
public class InputPrompt : MenuItemBase
8+
{
9+
public string Title { get; set; }
10+
public Func<string> DefaultValue { get; set; }
11+
public Func<string, string> Validator { get; set; }
12+
public Action<string> SubmitAction { get; set; }
13+
public Action<InputPrompt> CloseAction { get; set; }
14+
public InputPrompt(MenuDisplayMode mode, string id, string name) : base(mode, id, name) { }
15+
public InputPrompt WithTitle(string title)
16+
{
17+
Title = title ?? string.Empty;
18+
return this;
19+
}
20+
public InputPrompt WithDefaultValue(string defaultValue)
21+
{
22+
DefaultValue = () => defaultValue;
23+
return this;
24+
}
25+
public InputPrompt WithDefaultValue(Func<string> defaultValue)
26+
{
27+
DefaultValue = defaultValue ?? (() => string.Empty);
28+
return this;
29+
}
30+
public InputPrompt WithSubmitAction(Action<string> submitAction)
31+
{
32+
SubmitAction = submitAction ?? throw new ArgumentNullException("Submit action cannot be null.");
33+
return this;
34+
}
35+
public InputPrompt WithCloseAction(Action<InputPrompt> closeAction)
36+
{
37+
CloseAction = closeAction;
38+
return this;
39+
}
40+
public InputPrompt ValidatedBy(Func<string, string> validator)
41+
{
42+
Validator = validator;
43+
return this;
44+
}
45+
protected virtual bool OnSubmit(out string error, string input)
46+
{
47+
error = Validator?.Invoke(input);
48+
49+
if (error == null)
50+
{
51+
SubmitAction?.Invoke(input);
52+
return true;
53+
}
54+
55+
return false;
56+
}
57+
protected virtual void OnCancel()
58+
{
59+
CloseAction?.Invoke(this);
60+
}
61+
protected virtual void OnTweak()
62+
{
63+
InputPromptPanel.Create(
64+
new InputPromptPanel.OnSubmit(OnSubmit),
65+
new InputPromptPanel.OnPop(OnCancel),
66+
Title,
67+
DefaultValue()
68+
);
69+
}
70+
public override void Tweak(ModdingMenu menu)
71+
{
72+
menu.TweakAction(Name, () =>
73+
{
74+
OnTweak();
75+
base.Tweak(menu);
76+
}, Description);
77+
}
78+
}
79+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using DistanceModConfigurationManager.DistanceGUI.Data;
2+
using DistanceModConfigurationManager.DistanceGUI.Menu;
3+
using System;
4+
5+
namespace DistanceModConfigurationManager.DistanceGUI.Controls
6+
{
7+
public class IntegerSlider : MenuItemBase
8+
{
9+
public int Minimum { get; set; }
10+
public int Maximum { get; set; }
11+
public Func<int> Get { get; set; }
12+
public Action<int> Set { get; set; }
13+
public int DefaultValue { get; set; }
14+
public IntegerSlider(MenuDisplayMode mode, string id, string name) : base(mode, id, name)
15+
{
16+
Get = () => DefaultValue;
17+
Set = (_) => { };
18+
}
19+
public IntegerSlider WithGetter(Func<int> getter)
20+
{
21+
Get = getter ?? throw new ArgumentNullException("Getter cannot be null.");
22+
return this;
23+
}
24+
public IntegerSlider WithSetter(Action<int> setter)
25+
{
26+
Set = setter ?? throw new ArgumentNullException("Setter cannot be null.");
27+
return this;
28+
}
29+
public IntegerSlider WithDefaultValue(int defaultValue)
30+
{
31+
DefaultValue = defaultValue;
32+
return this;
33+
}
34+
public IntegerSlider LimitedByRange(int minimum, int maximum)
35+
{
36+
if (Minimum > Maximum)
37+
{
38+
throw new ArgumentOutOfRangeException("Minimum must be lower than maximum.");
39+
}
40+
41+
Minimum = minimum;
42+
Maximum = maximum;
43+
44+
return this;
45+
}
46+
public override void Tweak(ModdingMenu menu)
47+
{
48+
if (Get == null || Set == null)
49+
{
50+
throw new InvalidOperationException("Cannot invoke tweak with Get or Set being null.");
51+
}
52+
53+
menu.TweakInt(Name, Get(), Minimum, Maximum, DefaultValue, Set, Description);
54+
base.Tweak(menu);
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)