2021-06-21 02:13:54 +02:00
|
|
|
using System.Collections.Generic;
|
2021-03-05 01:08:38 +01:00
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Server.Tools.Components;
|
|
|
|
|
using Content.Server.UserInterface;
|
|
|
|
|
using Content.Shared.Configurable;
|
|
|
|
|
using Content.Shared.Interaction;
|
2021-10-07 13:01:27 +02:00
|
|
|
using Content.Shared.Tools;
|
|
|
|
|
using Content.Shared.Tools.Components;
|
|
|
|
|
using Content.Shared.Verbs;
|
|
|
|
|
using Robust.Server.Console;
|
2021-03-05 01:08:38 +01:00
|
|
|
using Robust.Server.GameObjects;
|
2020-10-30 01:16:26 +01:00
|
|
|
using Robust.Shared.GameObjects;
|
2021-12-03 14:17:01 +01:00
|
|
|
using Robust.Shared.IoC;
|
2020-10-30 01:16:26 +01:00
|
|
|
using Robust.Shared.Serialization;
|
2021-03-05 01:08:38 +01:00
|
|
|
using Robust.Shared.Serialization.Manager.Attributes;
|
2021-10-07 13:01:27 +02:00
|
|
|
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
2020-10-30 01:16:26 +01:00
|
|
|
using Robust.Shared.ViewVariables;
|
|
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Server.Configurable
|
2020-10-30 01:16:26 +01:00
|
|
|
{
|
|
|
|
|
[RegisterComponent]
|
|
|
|
|
[ComponentReference(typeof(SharedConfigurationComponent))]
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed class ConfigurationComponent : SharedConfigurationComponent, IInteractUsing, ISerializationHooks
|
2020-10-30 01:16:26 +01:00
|
|
|
{
|
2021-12-08 17:04:21 +01:00
|
|
|
[Dependency] private readonly IEntityManager _entMan = default!;
|
|
|
|
|
|
2021-03-16 15:50:20 +01:00
|
|
|
[ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(ConfigurationUiKey.Key);
|
2020-10-30 01:16:26 +01:00
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("keys")] private List<string> _keys = new();
|
|
|
|
|
|
2020-10-30 01:16:26 +01:00
|
|
|
[ViewVariables]
|
2020-11-27 11:00:49 +01:00
|
|
|
private readonly Dictionary<string, string> _config = new();
|
2020-10-30 01:16:26 +01:00
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("validation")]
|
|
|
|
|
private readonly Regex _validation = new ("^[a-zA-Z0-9 ]*$", RegexOptions.Compiled);
|
|
|
|
|
|
2021-10-07 13:01:27 +02:00
|
|
|
[DataField("qualityNeeded", customTypeSerializer:typeof(PrototypeIdSerializer<ToolQualityPrototype>))]
|
|
|
|
|
private string _qualityNeeded = "Pulsing";
|
|
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
void ISerializationHooks.BeforeSerialization()
|
|
|
|
|
{
|
|
|
|
|
_keys = _config.Keys.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ISerializationHooks.AfterDeserialization()
|
|
|
|
|
{
|
|
|
|
|
foreach (var key in _keys)
|
|
|
|
|
{
|
2021-06-21 02:13:54 +02:00
|
|
|
_config.Add(key, string.Empty);
|
2021-03-05 01:08:38 +01:00
|
|
|
}
|
|
|
|
|
}
|
2020-10-30 01:16:26 +01:00
|
|
|
|
2021-06-19 19:41:26 -07:00
|
|
|
protected override void OnAdd()
|
2020-10-30 01:16:26 +01:00
|
|
|
{
|
2020-11-06 04:04:21 +11:00
|
|
|
base.OnAdd();
|
2020-10-30 01:16:26 +01:00
|
|
|
if (UserInterface != null)
|
|
|
|
|
{
|
|
|
|
|
UserInterface.OnReceiveMessage += UserInterfaceOnReceiveMessage;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-19 19:41:26 -07:00
|
|
|
protected override void OnRemove()
|
2020-11-06 04:04:21 +11:00
|
|
|
{
|
|
|
|
|
base.OnRemove();
|
|
|
|
|
if (UserInterface != null)
|
|
|
|
|
{
|
|
|
|
|
UserInterface.OnReceiveMessage -= UserInterfaceOnReceiveMessage;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-16 15:50:20 +01:00
|
|
|
public string? GetConfig(string name)
|
2020-10-30 01:16:26 +01:00
|
|
|
{
|
|
|
|
|
return _config.GetValueOrDefault(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Startup()
|
|
|
|
|
{
|
|
|
|
|
base.Startup();
|
|
|
|
|
UpdateUserInterface();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
|
|
|
|
|
{
|
2021-12-08 17:04:21 +01:00
|
|
|
if (UserInterface == null || !_entMan.TryGetComponent(eventArgs.User, out ActorComponent? actor))
|
2020-10-30 01:16:26 +01:00
|
|
|
return false;
|
|
|
|
|
|
2021-12-08 17:04:21 +01:00
|
|
|
if (!_entMan.TryGetComponent<ToolComponent?>(eventArgs.Using, out var tool) || !tool.Qualities.Contains(_qualityNeeded))
|
2020-10-30 01:16:26 +01:00
|
|
|
return false;
|
|
|
|
|
|
2020-11-27 08:45:16 +01:00
|
|
|
OpenUserInterface(actor);
|
2020-10-30 01:16:26 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UserInterfaceOnReceiveMessage(ServerBoundUserInterfaceMessage serverMsg)
|
|
|
|
|
{
|
|
|
|
|
var message = serverMsg.Message;
|
|
|
|
|
var config = new Dictionary<string, string>(_config);
|
|
|
|
|
|
|
|
|
|
if (message is ConfigurationUpdatedMessage msg)
|
|
|
|
|
{
|
|
|
|
|
foreach (var key in config.Keys)
|
|
|
|
|
{
|
|
|
|
|
var value = msg.Config.GetValueOrDefault(key);
|
|
|
|
|
|
2021-06-21 02:13:54 +02:00
|
|
|
if (value == null || _validation != null && !_validation.IsMatch(value) && value != string.Empty)
|
2020-10-30 01:16:26 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
_config[key] = value;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-15 20:04:33 -07:00
|
|
|
// TODO raise event.
|
2020-10-30 01:16:26 +01:00
|
|
|
}
|
2021-03-16 15:50:20 +01:00
|
|
|
}
|
2020-10-30 01:16:26 +01:00
|
|
|
|
|
|
|
|
private void UpdateUserInterface()
|
|
|
|
|
{
|
2020-11-06 04:04:21 +11:00
|
|
|
UserInterface?.SetState(new ConfigurationBoundUserInterfaceState(_config));
|
2020-10-30 01:16:26 +01:00
|
|
|
}
|
|
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
public void OpenUserInterface(ActorComponent actor)
|
2020-11-27 08:45:16 +01:00
|
|
|
{
|
|
|
|
|
UpdateUserInterface();
|
2021-05-12 13:42:18 +02:00
|
|
|
UserInterface?.Open(actor.PlayerSession);
|
|
|
|
|
UserInterface?.SendMessage(new ValidationUpdateMessage(_validation.ToString()), actor.PlayerSession);
|
2020-11-27 08:45:16 +01:00
|
|
|
}
|
|
|
|
|
|
2020-10-30 01:16:26 +01:00
|
|
|
private static void FillConfiguration<T>(List<string> list, Dictionary<string, T> configuration, T value){
|
|
|
|
|
for (var index = 0; index < list.Count; index++)
|
|
|
|
|
{
|
|
|
|
|
configuration.Add(list[index], value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|