Skip to content

Commit c1a8a7f

Browse files
[In-Progress Work Sync] Work on Configuration APIs.
1 parent 9c3d001 commit c1a8a7f

File tree

18 files changed

+590
-120
lines changed

18 files changed

+590
-120
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Barotrauma.LuaCs.Data;
4+
5+
namespace Barotrauma.LuaCs.Configuration;
6+
7+
8+
/// <summary>
9+
/// Base type of all menu displayable types.
10+
/// </summary>
11+
public interface IDisplayableConfigBase : IDataInfo, IConfigDisplayInfo
12+
{
13+
/// <summary>
14+
/// Whether the current config is editable.
15+
/// </summary>
16+
bool IsEditable { get; }
17+
/// <summary>
18+
/// Used to indicate the implemented interface and targeted display logic.
19+
/// </summary>
20+
static virtual DisplayType DisplayOption => DisplayType.Undefined;
21+
}
22+
23+
public interface IDisplayableConfigBase<out TDisplay, in TValue> : IDisplayableConfigBase
24+
{
25+
void SetValue(TValue value);
26+
TDisplay GetDisplayValue();
27+
}
28+
29+
public interface IDisplayableConfigBool : IDisplayableConfigBase<bool, bool>
30+
{
31+
static DisplayType IDisplayableConfigBase.DisplayOption => DisplayType.Boolean;
32+
}
33+
34+
public interface IDisplayableConfigText : IDisplayableConfigBase<string, string>
35+
{
36+
static DisplayType IDisplayableConfigBase.DisplayOption => DisplayType.Text;
37+
}
38+
39+
public interface IDisplayableConfigInt : IDisplayableConfigBase<int, int>
40+
{
41+
static DisplayType IDisplayableConfigBase.DisplayOption => DisplayType.Integer;
42+
}
43+
44+
public interface IDisplayableConfigFloat : IDisplayableConfigBase<float, float>
45+
{
46+
static DisplayType IDisplayableConfigBase.DisplayOption => DisplayType.Float;
47+
}
48+
49+
public interface IDisplayableConfigSliderInt : IDisplayableConfigBase<(int Min, int Max, int Value, int Steps), int>
50+
{
51+
static DisplayType IDisplayableConfigBase.DisplayOption => DisplayType.SliderInt;
52+
}
53+
54+
public interface IDisplayableConfigSliderFloat : IDisplayableConfigBase<(float Min, float Max, float Value, int Steps), float>
55+
{
56+
static DisplayType IDisplayableConfigBase.DisplayOption => DisplayType.SliderFloat;
57+
}
58+
59+
public interface IDisplayableConfigDropdown : IDisplayableConfigBase<List<string>, string>
60+
{
61+
static DisplayType IDisplayableConfigBase.DisplayOption => DisplayType.Dropdown;
62+
}
63+
64+
/// <summary>
65+
/// Allows completely custom-designed UI for this configuration component.
66+
/// </summary>
67+
public interface IDisplayableConfigCustom : IDisplayableConfigBase
68+
{
69+
static DisplayType IDisplayableConfigBase.DisplayOption => DisplayType.Custom;
70+
/// <summary>
71+
/// Draw your menu settings option.
72+
/// </summary>
73+
/// <param name="layoutGroup">Parent layout component.</param>
74+
void DrawComponent(GUILayoutGroup layoutGroup);
75+
/// <summary>
76+
/// Called when the config element is set to be disposed to allow for cleanup.
77+
/// </summary>
78+
void DisposeGUI();
79+
/// <summary>
80+
/// Called when the UI indicates to save the current value as permanent.
81+
/// </summary>
82+
void OnValueSaved();
83+
/// <summary>
84+
/// Called when the UI indicates to discard the currently displayed value and revert to the last saved value.
85+
/// </summary>
86+
void OnValueDiscarded();
87+
}
88+
89+
90+
91+
/// <summary>
92+
/// Indicates the intended display and feedback logic to be used by the <see cref="SettingsMenu"/>.
93+
/// <br/><b>[Important]</b>
94+
/// <br/>The type must implement the indicated interface for the selected option, or it will not be displayed.
95+
/// </summary>
96+
public enum DisplayType
97+
{
98+
/// <summary>
99+
/// Will not be displayed in menus.
100+
/// </summary>
101+
Undefined,
102+
/// <summary>
103+
/// Will be shown as a checkbox.
104+
/// <br/><b>[Requires(<see cref="IDisplayableConfigBool"/>)]</b>
105+
/// </summary>
106+
Boolean,
107+
/// <summary>
108+
/// Shown as an editable text input.
109+
/// <br/><b>[Requires(<see cref="IDisplayableConfigText"/>)]</b>
110+
/// </summary>
111+
Text,
112+
/// <summary>
113+
/// Shown as number input (no decimal input).
114+
/// <br/><b>[Requires(<see cref="IDisplayableConfigInt"/>)]</b>
115+
/// </summary>
116+
Integer,
117+
/// <summary>
118+
/// Shown as a number input.
119+
/// <br/><b>[Requires(<see cref="IDisplayableConfigFloat"/>)]</b>
120+
/// </summary>
121+
Float,
122+
/// <summary>
123+
/// Shown as a slider, values parsed as integers.
124+
/// <br/><b>[Requires(<see cref="IDisplayableConfigSliderInt"/>)]</b>
125+
/// </summary>
126+
SliderInt,
127+
/// <summary>
128+
/// Shown as a slider, values parsed as single-precision decimal numbers.
129+
/// <br/><b>[Requires(<see cref="IDisplayableConfigSliderFloat"/>)]</b>
130+
/// </summary>
131+
SliderFloat,
132+
/// <summary>
133+
/// Shown as a <see cref="GUIDropDown"/> menu, values parsed as strings.
134+
/// <br/><b>[Requires(<see cref="IDisplayableConfigDropdown"/>)]</b>
135+
/// </summary>
136+
Dropdown,
137+
/// <summary>
138+
/// UI Display is implemented by inheritor and actioned by a call to <see cref="IDisplayableConfigCustom.DrawComponent"/>.
139+
/// <br/><b>[Requires(<see cref="IDisplayableConfigCustom"/>)]</b>
140+
/// </summary>
141+
Custom
142+
}

Barotrauma/BarotraumaClient/ClientSource/LuaCs/Data/IConfigInfo.cs

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

33
namespace Barotrauma.LuaCs.Data;
44

5-
public partial interface IConfigInfo
5+
public partial interface IConfigInfo : IConfigDisplayInfo { }
6+
7+
public interface IConfigDisplayInfo
68
{
9+
/// <summary>
10+
/// User-friendly name or Localization Token.
11+
/// </summary>
12+
string DisplayName { get; }
13+
/// <summary>
14+
/// User-friendly description or Localization Token.
15+
/// </summary>
16+
string Description { get; }
17+
/// <summary>
18+
/// The menu category to display under. Used for filtering.
19+
/// </summary>
20+
string DisplayCategory { get; }
721
/// <summary>
822
/// Should this config be displayed in end-user menus.
923
/// </summary>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Immutable;
3+
using Barotrauma.LuaCs.Configuration;
4+
using Barotrauma.Networking;
5+
using FluentResults;
6+
7+
namespace Barotrauma.LuaCs.Services;
8+
9+
public partial class ConfigService
10+
{
11+
public ImmutableArray<IDisplayableConfigBase> GetDisplayableConfigs()
12+
{
13+
throw new NotImplementedException();
14+
}
15+
16+
public ImmutableArray<IDisplayableConfigBase> GetDisplayableConfigsForPackage(ContentPackage package)
17+
{
18+
throw new NotImplementedException();
19+
}
20+
21+
public Result<IConfigControl> AddConfigControl(ContentPackage package, string name, KeyOrMouse defaultValue, NetSync syncMode = NetSync.None,
22+
ClientPermissions permissions = ClientPermissions.None, Func<KeyOrMouse, bool> valueChangePredicate = null,
23+
Action<IConfigControl> onValueChanged = null)
24+
{
25+
throw new NotImplementedException();
26+
}
27+
}

Barotrauma/BarotraumaClient/ClientSource/LuaCs/Services/IConfigService.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Collections.Immutable;
34
using Barotrauma.LuaCs.Configuration;
45
using Barotrauma.LuaCs.Services;
56
using Barotrauma.Networking;
@@ -8,5 +9,12 @@ namespace Barotrauma.LuaCs.Services;
89

910
public partial interface IConfigService
1011
{
12+
ImmutableArray<IDisplayableConfigBase> GetDisplayableConfigs();
13+
ImmutableArray<IDisplayableConfigBase> GetDisplayableConfigsForPackage(ContentPackage package);
1114

15+
FluentResults.Result<IConfigControl> AddConfigControl(ContentPackage package, string name, KeyOrMouse defaultValue,
16+
NetSync syncMode = NetSync.None,
17+
ClientPermissions permissions = ClientPermissions.None,
18+
Func<KeyOrMouse, bool> valueChangePredicate = null,
19+
Action<IConfigControl> onValueChanged = null);
1220
}

Barotrauma/BarotraumaClient/ClientSource/LuaCs/Services/NetworkingService.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public void NetMessageReceived(IReadMessage netMessage, ServerPacketHeader heade
4444
}
4545
}
4646

47+
public void SendNetVar(INetworkSyncEntity netVar)
48+
{
49+
throw new NotImplementedException();
50+
}
51+
4752
public void NetMessageReceived(IReadMessage message, ServerPacketHeader header)
4853
{
4954
throw new NotImplementedException();
@@ -89,6 +94,11 @@ public void Send(IWriteMessage netMessage, DeliveryMethod deliveryMethod = Deliv
8994
GameMain.Client.ClientPeer.Send(netMessage, deliveryMethod);
9095
}
9196

97+
public void RegisterNetVar(INetworkSyncEntity netVar)
98+
{
99+
throw new NotImplementedException();
100+
}
101+
92102
private void HandleNetMessageId(IReadMessage netMessage, Client client = null)
93103
{
94104
ushort id = netMessage.ReadUInt16();
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
using System;
22
using Barotrauma.LuaCs.Data;
3+
using Barotrauma.LuaCs.Services;
34
using Barotrauma.Networking;
45

56
namespace Barotrauma.LuaCs.Configuration;
67

7-
public partial interface IConfigBase : IVarId
8+
public partial interface IConfigBase : IEquatable<IConfigBase>
89
{
9-
bool IsInitialized { get; }
10+
Type GetValueType();
1011
string GetValue();
1112
bool TrySetValue(string value);
1213
bool IsAssignable(string value);
13-
Type GetValueType();
14-
void Initialize(IVarId id, string defaultValue);
15-
}
16-
17-
public interface IVarId : IDataInfo
18-
{
19-
Guid InstanceId { get; }
2014
}

Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/IConfigEntry.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33

44
namespace Barotrauma.LuaCs.Configuration;
55

6-
public interface IConfigEntry<T> : IConfigBase, INetVar where T : IConvertible, IEquatable<T>
6+
public interface IConfigEntry<T> : IConfigBase, INetworkSyncEntity where T : IConvertible, IEquatable<T>
77
{
88
T Value { get; }
99
bool TrySetValue(T value);
1010
bool IsAssignable(T value);
11-
void Initialize(IVarId id, T defaultValue);
1211
}

Barotrauma/BarotraumaShared/SharedSource/LuaCs/Configuration/IConfigList.cs

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

33
namespace Barotrauma.LuaCs.Configuration;
44

5-
public interface IConfigList : IConfigBase, INetVar
5+
public interface IConfigList : IConfigBase, INetworkSyncEntity
66
{
77

88
}
Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,43 @@
11
using System;
2+
using System.Xml.Linq;
23
using Barotrauma.LuaCs.Services;
34
using Barotrauma.Networking;
45

56
namespace Barotrauma.LuaCs.Data;
67

7-
// TODO: Finish
8+
/// <summary>
9+
/// Parsed data from a configuration xml.
10+
/// </summary>
811
public partial interface IConfigInfo : IDataInfo
912
{
1013
/// <summary>
11-
/// Specifies the data type this should be initialized to (ie. string, int, vector, etc.)
12-
/// Custom types can be registered by mods.
14+
/// Specifies the type initializer that will be used to instantiate the config var.
1315
/// </summary>
1416
Type DataType { get; }
1517
/// <summary>
16-
/// String version of the default value.
18+
/// String version of the default value.
1719
/// </summary>
1820
string DefaultValue { get; }
1921
/// <summary>
20-
/// The value the last time this config was saved, if found in /data/.
22+
/// The value the last time this config was saved.
23+
/// <br/><b>[If(Type='<see cref="string"/>')]</b><br/>
24+
/// The value is from the 'Value' Attribute. Typically used for types with single/simple values, such as primitives.
25+
/// <br/><b>[If(Type='<see cref="XElement"/>')]</b><br/>
26+
/// The value is from the first 'Value' child element. Typically used with complex config types, such as range and list.
2127
/// </summary>
22-
string StoredValue { get; }
28+
OneOf.OneOf<string, XElement> Value { get; }
2329
/// <summary>
24-
/// Custom data storage for other type-specific information needed. IE. Used to store the min,
25-
/// max and step values for the <b>IConfigRangeEntry(T)</b>.
30+
/// In what <see cref="RunState"/>(s) is this config editable. Will be editable in the selected state, and lower value states.
31+
/// <br/><br/>
32+
/// <b>[Important]</b><br/> Setting this to value lower than 'Configuration` will render this config read-only.
33+
/// <br/><br/><b>Expected Behaviour</b>:
34+
/// <br/><b>[<see cref="RunState.Unloaded"/>|<see cref="RunState.Parsed"/>]</b>: Read-Only.
35+
/// <br/><b>[<see cref="RunState.Configuration"/>]</b>: Can only be changed at the Main Menu (not in a lobby).
36+
/// <br/><b>[<see cref="RunState.Running"/>]</b>: Can be changed at the Main Menu and while a lobby is active.
2637
/// </summary>
27-
string CustomParameters { get; }
28-
/// <summary>
29-
/// <b>[Multiplayer]</b><br/>
30-
/// What permissions do clients require to change this setting.
31-
/// </summary>
32-
ClientPermissions RequiredPermissions { get; }
33-
/// <summary>
34-
/// In what <see cref="RunState"/>s is this config editable.
35-
/// <br/>
36-
/// Note: Setting this to value lower than 'Configuration` will render this config read-only.
37-
/// </summary>
38-
RunState CanEditStates { get; }
38+
RunState EditableStates { get; }
3939
/// <summary>
4040
/// Network synchronization rules for this config.
4141
/// </summary>
4242
NetSync NetSync { get; }
43-
/// <summary>
44-
/// User friendly name or Localization Token.
45-
/// </summary>
46-
string DisplayName { get; }
47-
/// <summary>
48-
/// User friendly description or Localization Token.
49-
/// </summary>
50-
string Description { get; }
5143
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.IO;
3+
4+
namespace Barotrauma.LuaCs.Data;
5+
6+
public interface IStorageConfigData
7+
{
8+
string LocalDataPathRegex { get; }
9+
string LocalPackagePath { get; }
10+
}
11+
12+
public class StorageConfigData : IStorageConfigData
13+
{
14+
public string LocalDataPathRegex => "<PACKAGENAME>";
15+
16+
public string LocalPackagePath
17+
{
18+
get
19+
{
20+
var val = GameMain.LuaCs?.LocalDataSavePath?.Value ?? "/Data/Mods/";
21+
return ContainsIllegalPaths(val) ? $"/Data/Mods/{LocalDataPathRegex}" : Path.Combine(val, LocalDataPathRegex);
22+
}
23+
}
24+
25+
private bool ContainsIllegalPaths(string path)
26+
{
27+
throw new NotImplementedException();
28+
}
29+
}

0 commit comments

Comments
 (0)