Skip to content

Commit 4daf4bf

Browse files
authored
Merge pull request #2 from MapleWheels/alpha-0.0.2.0
Alpha 0.0.2.0
2 parents 82096b2 + 25eee12 commit 4daf4bf

42 files changed

Lines changed: 1385 additions & 590 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

ModConfigManagerClient/Initializer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using ModdingToolkit.Patches;
1+
using Microsoft.Xna.Framework.Input;
2+
using ModdingToolkit.Patches;
23

34
[assembly: IgnoresAccessChecksTo("Barotrauma")]
45
[assembly: IgnoresAccessChecksTo("NetScriptAssembly")]

ModConfigManagerClient/MSettingsMenu.cs

Lines changed: 402 additions & 182 deletions
Large diffs are not rendered by default.

ModdingToolkit/CSharp/Client/Config/ConfigControl.cs

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@
44

55
namespace ModdingToolkit.Config;
66

7-
public sealed class ConfigControl : IConfigControl
7+
public sealed class ConfigControl : IConfigControl, IDisplayable
88
{
99
private event System.Func<KeyOrMouse, bool>? _validateInput;
1010
private event System.Action? _onValueChanged;
11-
public string Name { get; set; } = String.Empty;
11+
public string Name { get; private set; } = String.Empty;
1212
public Type SubTypeDef => typeof(KeyOrMouse);
13-
public string ModName { get; set; } = String.Empty;
14-
public IConfigBase.Category MenuCategory => IConfigBase.Category.Ignore;
13+
public string ModName { get; private set; } = String.Empty;
14+
public string DisplayName { get; private set; }
15+
public string DisplayModName { get; private set; }
16+
public string DisplayCategory { get; private set; }
17+
public string Tooltip { get; private set; }
18+
public string ImageIcon { get; private set; }
19+
public Category MenuCategory => Category.Controls;
1520
public NetworkSync NetSync => NetworkSync.NoSync;
1621

1722
public string GetStringValue()
@@ -48,7 +53,22 @@ public void SetValueAsDefault()
4853
this.Value = new KeyOrMouse(DefaultValue.MouseButton);
4954
}
5055

51-
public IConfigBase.DisplayType GetDisplayType() => IConfigBase.DisplayType.KeyOrMouse;
56+
public DisplayType GetDisplayType() => DisplayType.KeyOrMouse;
57+
58+
public void InitializeDisplay(string? name = "", string? modName = "", string? displayName = "", string? displayModName = "",
59+
string? displayCategory = "", string? tooltip = "", string? imageIcon = "", Category menuCategory = Category.Gameplay)
60+
{
61+
if (!displayName.IsNullOrWhiteSpace())
62+
this.DisplayName = displayName;
63+
if (!displayModName.IsNullOrWhiteSpace())
64+
this.DisplayModName = displayModName;
65+
if (!displayCategory.IsNullOrWhiteSpace())
66+
this.DisplayCategory = displayCategory;
67+
if (!tooltip.IsNullOrWhiteSpace())
68+
this.Tooltip = tooltip;
69+
if (!imageIcon.IsNullOrWhiteSpace())
70+
this.ImageIcon = imageIcon;
71+
}
5272

5373
private KeyOrMouse? _value;
5474
public KeyOrMouse? Value
@@ -87,6 +107,34 @@ public bool Validate(KeyOrMouse? newValue)
87107
return this._validateInput?.Invoke(newValue) ?? true;
88108
}
89109

110+
public bool IsHit()
111+
{
112+
if (this.Value is null)
113+
return false;
114+
switch (this.Value.MouseButton)
115+
{
116+
case MouseButton.None:
117+
return Barotrauma.PlayerInput.KeyHit(this.Value.Key);
118+
case MouseButton.PrimaryMouse:
119+
case MouseButton.LeftMouse:
120+
return Barotrauma.PlayerInput.PrimaryMouseButtonClicked();
121+
case MouseButton.SecondaryMouse:
122+
case MouseButton.RightMouse:
123+
return Barotrauma.PlayerInput.SecondaryMouseButtonClicked();
124+
case MouseButton.MiddleMouse:
125+
return Barotrauma.PlayerInput.MidButtonClicked();
126+
case MouseButton.MouseButton4:
127+
return Barotrauma.PlayerInput.Mouse4ButtonClicked();
128+
case MouseButton.MouseButton5:
129+
return Barotrauma.PlayerInput.Mouse5ButtonClicked();
130+
case MouseButton.MouseWheelUp:
131+
return Barotrauma.PlayerInput.MouseWheelUpClicked();
132+
case MouseButton.MouseWheelDown:
133+
return Barotrauma.PlayerInput.MouseWheelDownClicked();
134+
}
135+
return false;
136+
}
137+
90138
public bool ValidateString(string value)
91139
{
92140
if (Enum.IsDefined(typeof(Keys), value))

ModdingToolkit/CSharp/Client/Config/ConfigEntry.cs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
using ModdingToolkit.Networking;
1+
using Barotrauma.Networking;
2+
using ModdingToolkit.Networking;
23

34
namespace ModdingToolkit.Config;
45

5-
public partial class ConfigEntry<T> : IConfigEntry<T>, INetConfigBase where T : IConvertible
6+
public partial class ConfigEntry<T> : IConfigEntry<T>, INetConfigBase, IDisplayable where T : IConvertible
67
{
78
public bool IsNetworked => this.NetSync != NetworkSync.NoSync && GameMain.IsMultiplayer;
89
public bool NetAuthorityValidate()
910
{
1011
if (!IsNetworked)
1112
return true;
13+
if (NetSync is NetworkSync.ServerAuthority && GameMain.Client.HasPermission(ClientPermissions.ManageSettings))
14+
return true;
15+
1216
return this.NetSync switch
1317
{
1418
NetworkSync.NoSync => true,
@@ -20,9 +24,44 @@ public bool NetAuthorityValidate()
2024

2125
public void TriggerNetEvent()
2226
{
23-
if (this.NetSync is NetworkSync.TwoWaySync)
27+
if (NetSync is NetworkSync.TwoWaySync
28+
|| (NetSync is NetworkSync.ServerAuthority && GameMain.Client.HasPermission(ClientPermissions.ManageSettings)))
2429
{
2530
this._onNetworkEvent?.Invoke(this);
2631
}
2732
}
33+
34+
public string DisplayName { get; private set; }
35+
public string DisplayModName { get; private set; }
36+
public string DisplayCategory { get; private set; }
37+
public string Tooltip { get; private set; }
38+
public string ImageIcon { get; private set; }
39+
public Category MenuCategory { get; private set; }
40+
41+
public void InitializeDisplay(string? name = "", string? modName = "", string? displayName = "", string? displayModName = "",
42+
string? displayCategory = "", string? tooltip = "", string? imageIcon = "", Category menuCategory = Category.Gameplay)
43+
{
44+
if (!displayName.IsNullOrWhiteSpace())
45+
this.DisplayName = displayName;
46+
if (!displayModName.IsNullOrWhiteSpace())
47+
this.DisplayModName = displayModName;
48+
if (!displayCategory.IsNullOrWhiteSpace())
49+
this.DisplayCategory = displayCategory;
50+
if (!tooltip.IsNullOrWhiteSpace())
51+
this.Tooltip = tooltip;
52+
if (!imageIcon.IsNullOrWhiteSpace())
53+
this.ImageIcon = imageIcon;
54+
this.MenuCategory = menuCategory;
55+
if (this.MenuCategory is Category.Controls)
56+
this.MenuCategory = Category.Gameplay;
57+
}
58+
59+
public virtual DisplayType GetDisplayType() =>
60+
typeof(T) switch
61+
{
62+
{ IsEnum: true } => DisplayType.DropdownEnum,
63+
{ Name: nameof(Boolean) } => DisplayType.Tickbox,
64+
{ IsPrimitive: true } => DisplayType.Number,
65+
_ => DisplayType.Standard
66+
};
2867
}

ModdingToolkit/CSharp/Client/Config/ConfigList.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace ModdingToolkit.Config;
44

5-
public partial class ConfigList : IConfigList, INetConfigBase
5+
public partial class ConfigList : IConfigList, INetConfigBase, IDisplayable
66
{
77
public bool IsNetworked => this.NetSync != NetworkSync.NoSync && GameMain.IsMultiplayer;
88
public bool NetAuthorityValidate()
@@ -25,4 +25,31 @@ public void TriggerNetEvent()
2525
this._onNetworkEvent?.Invoke(this);
2626
}
2727
}
28+
29+
public string DisplayName { get; private set; }
30+
public string DisplayModName { get; private set; }
31+
public string DisplayCategory { get; private set; }
32+
public string Tooltip { get; private set; }
33+
public string ImageIcon { get; private set; }
34+
public Category MenuCategory { get; private set; }
35+
36+
public void InitializeDisplay(string? name = "", string? modName = "", string? displayName = "", string? displayModName = "",
37+
string? displayCategory = "", string? tooltip = "", string? imageIcon = "", Category menuCategory = Category.Gameplay)
38+
{
39+
if (!displayName.IsNullOrWhiteSpace())
40+
this.DisplayName = displayName;
41+
if (!displayModName.IsNullOrWhiteSpace())
42+
this.DisplayModName = displayModName;
43+
if (!displayCategory.IsNullOrWhiteSpace())
44+
this.DisplayCategory = displayCategory;
45+
if (!tooltip.IsNullOrWhiteSpace())
46+
this.Tooltip = tooltip;
47+
if (!imageIcon.IsNullOrWhiteSpace())
48+
this.ImageIcon = imageIcon;
49+
this.MenuCategory = menuCategory;
50+
if (this.MenuCategory is Category.Controls)
51+
this.MenuCategory = Category.Gameplay;
52+
}
53+
54+
public virtual DisplayType GetDisplayType() => DisplayType.DropdownList;
2855
}

ModdingToolkit/CSharp/Client/Config/ConfigManager.cs

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ namespace ModdingToolkit.Config;
55

66
public static partial class ConfigManager
77
{
8+
#region PUBLIC_API
9+
10+
/// <summary>
11+
/// Creates a Config var for binding key or mouse buttons to.
12+
/// </summary>
13+
/// <param name="name">Name of your config variable</param>
14+
/// <param name="modName">The name of your Mod. Acts a collection everything with the same ModName.</param>
15+
/// <param name="defaultValue">The default key or mouse binding.</param>
16+
/// <param name="onValueChanged">Called whenever the value has been successfully changed.</param>
17+
/// <param name="filePathOverride">Use if you want to load this variable from another config file on disk. Takes an absolute path.</param>
18+
/// <returns></returns>
819
public static IConfigControl AddConfigKeyOrMouseBind(
920
string name,
1021
string modName,
@@ -15,33 +26,61 @@ public static IConfigControl AddConfigKeyOrMouseBind(
1526
{
1627
return CreateIConfigControl(name, modName, defaultValue, onValueChanged, filePathOverride);
1728
}
18-
19-
public static IEnumerable<IConfigControl> GetControlConfigs()
20-
{
21-
List<IConfigControl> members = new();
22-
Indexer_KeyMouseControls.ForEach(ci =>
23-
{
24-
if (LoadedConfigEntries.ContainsKey(ci.ModName)
25-
&& LoadedConfigEntries[ci.ModName].ContainsKey(ci.Name)
26-
&& LoadedConfigEntries[ci.ModName][ci.Name] is IConfigControl icc)
27-
{
28-
members.Add(icc);
29-
}
30-
});
31-
return members;
32-
}
33-
29+
30+
public static IEnumerable<IDisplayable> GetDisplayableConfigs() => Displayables.ToImmutableList();
31+
public static IEnumerable<DisplayableControl> GetControlConfigs() => DisplayableControls.ToImmutableList();
32+
33+
#endregion
34+
35+
#region INTERNAL_OPS
36+
3437
private static IConfigControl CreateIConfigControl(
3538
string name,
3639
string modName,
3740
KeyOrMouse defaultValue,
3841
Action? onValueChanged,
39-
string? filePathOverride = null)
42+
string? filePathOverride = null,
43+
DisplayData? data = null)
4044
{
4145
ConfigControl cc = new();
4246
cc.Initialize(name, modName, null, defaultValue, onValueChanged);
43-
AddConfigToLists(cc);
44-
LoadData(cc, filePathOverride);
47+
InitializeConfigBase(cc, data, filePathOverride);
4548
return cc;
4649
}
50+
51+
private static void RegisterDisplayable(IDisplayable displayable, DisplayData data)
52+
{
53+
#warning TODO: Implement display data verification
54+
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
55+
if (displayable is null)
56+
return;
57+
if (data.MenuCategory is Category.Ignore)
58+
return;
59+
displayable.InitializeDisplay(data.Name, data.ModName, data.DisplayName, data.DisplayModName,
60+
data.DisplayCategory, data.Tooltip, data.ImageIcon, data.MenuCategory);
61+
Displayables.Add(displayable);
62+
if (displayable is IConfigControl icc)
63+
DisplayableControls.Add(new DisplayableControl(displayable, icc));
64+
}
65+
66+
private static void RemoveDisplayable(IDisplayable displayable)
67+
{
68+
Displayables.RemoveAll(d => d == displayable);
69+
DisplayableControls.RemoveAll(dc => dc.Displayable == displayable);
70+
}
71+
72+
private static void DisposeClient()
73+
{
74+
Displayables.Clear();
75+
DisplayableControls.Clear();
76+
}
77+
78+
#endregion
79+
80+
#region INTERNAL VARS
81+
82+
private static readonly List<IDisplayable> Displayables = new();
83+
private static readonly List<DisplayableControl> DisplayableControls = new();
84+
85+
#endregion
4786
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace ModdingToolkit.Config;
2+
3+
public partial class ConfigRangeFloat : IDisplayable
4+
{
5+
public override DisplayType GetDisplayType() => DisplayType.Slider;
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace ModdingToolkit.Config;
2+
3+
public partial class ConfigRangeInt : IDisplayable
4+
{
5+
public override DisplayType GetDisplayType() => DisplayType.Slider;
6+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace ModdingToolkit.Config;
2+
3+
/// <summary>
4+
/// A pointer container to reduce type assignment checking at runtime. Both vars point to the same object.
5+
/// </summary>
6+
/// <param name="Displayable">The IDisplayable interface.</param>
7+
/// <param name="Control">The IConfigControl interface.</param>
8+
public record DisplayableControl(IDisplayable Displayable, IConfigControl Control);

ModdingToolkit/CSharp/Client/Config/IConfigControl.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ public interface IConfigControl : IConfigBase
88
KeyOrMouse DefaultValue { get; }
99
void Initialize(string name, string modName, KeyOrMouse? currentValue, KeyOrMouse? defaultValue, System.Action? onValueChanged);
1010
bool Validate(KeyOrMouse newValue);
11+
bool IsHit();
1112
}

0 commit comments

Comments
 (0)