Predict and cleanup RingerComponent (#35907)

* clean up most stuff

* move to shared

* works

* shuffle shit around

* oops! access

* fixes

* todo: everything

* SUFFERING

* curse you
This commit is contained in:
Milon
2025-04-19 15:55:05 +02:00
committed by GitHub
parent e214eb7a28
commit 6138fcdce9
18 changed files with 645 additions and 405 deletions

View File

@@ -7,40 +7,21 @@ using Robust.Shared.Timing;
namespace Content.Client.PDA.Ringer
{
[UsedImplicitly]
public sealed class RingerBoundUserInterface : BoundUserInterface
public sealed class RingerBoundUserInterface(EntityUid owner, Enum uiKey) : BoundUserInterface(owner, uiKey)
{
[ViewVariables]
private RingtoneMenu? _menu;
public RingerBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
{
}
protected override void Open()
{
base.Open();
_menu = this.CreateWindow<RingtoneMenu>();
_menu.OpenToLeft();
_menu.TestRingerButton.OnPressed += _ =>
{
SendMessage(new RingerPlayRingtoneMessage());
};
_menu.TestRingtoneButtonPressed += OnTestRingtoneButtonPressed;
_menu.SetRingtoneButtonPressed += OnSetRingtoneButtonPressed;
_menu.SetRingerButton.OnPressed += _ =>
{
if (!TryGetRingtone(out var ringtone))
return;
SendMessage(new RingerSetRingtoneMessage(ringtone));
_menu.SetRingerButton.Disabled = true;
Timer.Spawn(333, () =>
{
if (_menu is { Disposed: false, SetRingerButton: { Disposed: false } ringer})
ringer.Disabled = false;
});
};
Update();
}
private bool TryGetRingtone(out Note[] ringtone)
@@ -63,36 +44,59 @@ namespace Content.Client.PDA.Ringer
return true;
}
protected override void UpdateState(BoundUserInterfaceState state)
public override void Update()
{
base.UpdateState(state);
base.Update();
if (_menu == null || state is not RingerUpdateState msg)
if (_menu == null)
return;
for (int i = 0; i < _menu.RingerNoteInputs.Length; i++)
if (!EntMan.TryGetComponent(Owner, out RingerComponent? ringer))
return;
for (var i = 0; i < _menu.RingerNoteInputs.Length; i++)
{
var note = ringer.Ringtone[i].ToString();
var note = msg.Ringtone[i].ToString();
if (RingtoneMenu.IsNote(note))
{
_menu.PreviousNoteInputs[i] = note.Replace("sharp", "#");
_menu.RingerNoteInputs[i].Text = _menu.PreviousNoteInputs[i];
}
if (!RingtoneMenu.IsNote(note))
continue;
_menu.PreviousNoteInputs[i] = note.Replace("sharp", "#");
_menu.RingerNoteInputs[i].Text = _menu.PreviousNoteInputs[i];
}
_menu.TestRingerButton.Disabled = msg.IsPlaying;
_menu.TestRingerButton.Disabled = ringer.Active;
}
protected override void Dispose(bool disposing)
private void OnTestRingtoneButtonPressed()
{
base.Dispose(disposing);
if (!disposing)
if (_menu is null)
return;
_menu?.Dispose();
SendPredictedMessage(new RingerPlayRingtoneMessage());
// We disable it instantly to remove the delay before the client receives the next compstate
// Makes the UI feel responsive, will be re-enabled by ringer.Active once it gets an update.
_menu.TestRingerButton.Disabled = true;
}
private void OnSetRingtoneButtonPressed()
{
if (_menu is null)
return;
if (!TryGetRingtone(out var ringtone))
return;
SendPredictedMessage(new RingerSetRingtoneMessage(ringtone));
_menu.SetRingerButton.Disabled = true;
Timer.Spawn(333,
() =>
{
if (_menu is { Disposed: false, SetRingerButton: { Disposed: false } ringer} )
ringer.Disabled = false;
});
}
}
}

View File

@@ -0,0 +1,56 @@
using Content.Shared.PDA;
using Content.Shared.PDA.Ringer;
using Content.Shared.Store.Components;
namespace Content.Client.PDA.Ringer;
/// <summary>
/// Handles the client-side logic for <see cref="SharedRingerSystem"/>.
/// </summary>
public sealed class RingerSystem : SharedRingerSystem
{
/// <inheritdoc/>
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<RingerComponent, AfterAutoHandleStateEvent>(OnRingerUpdate);
}
/// <summary>
/// Updates the UI whenever we get a new component state from the server.
/// </summary>
private void OnRingerUpdate(Entity<RingerComponent> ent, ref AfterAutoHandleStateEvent args)
{
UpdateRingerUi(ent);
}
/// <inheritdoc/>
protected override void UpdateRingerUi(Entity<RingerComponent> ent)
{
if (UI.TryGetOpenUi(ent.Owner, RingerUiKey.Key, out var bui))
{
bui.Update();
}
}
/// <inheritdoc/>
public override bool TryToggleUplink(EntityUid uid, Note[] ringtone, EntityUid? user = null)
{
if (!TryComp<RingerUplinkComponent>(uid, out var uplink))
return false;
if (!HasComp<StoreComponent>(uid))
return false;
// Special case for client-side prediction:
// Since we can't expose the uplink code to clients for security reasons,
// we assume if an antagonist is trying to set a ringtone, it's to unlock the uplink.
// The server will properly verify the code and correct if needed.
if (IsAntagonist(user))
return ToggleUplinkInternal((uid, uplink));
// Non-antagonists never get to toggle the uplink on the client
return false;
}
}

View File

@@ -1,7 +1,8 @@
<DefaultWindow xmlns="https://spacestation14.io"
Title="{Loc 'comp-ringer-ui-menu-title'}"
MinSize="320 128"
SetSize="320 128">
<controls:FancyWindow xmlns="https://spacestation14.io"
xmlns:controls="clr-namespace:Content.Client.UserInterface.Controls"
Title="{Loc 'comp-ringer-ui-menu-title'}"
MinSize="320 100"
SetSize="320 100">
<BoxContainer Orientation="Vertical"
VerticalExpand="True"
HorizontalExpand="True"
@@ -90,4 +91,4 @@
</BoxContainer>
</PanelContainer>
</BoxContainer>
</DefaultWindow>
</controls:FancyWindow>

View File

@@ -1,6 +1,6 @@
using System.Numerics;
using Content.Client.UserInterface.Controls;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Content.Shared.PDA;
using Robust.Client.UserInterface.Controls;
@@ -8,15 +8,21 @@ using Robust.Client.UserInterface.Controls;
namespace Content.Client.PDA.Ringer
{
[GenerateTypedNameReferences]
public sealed partial class RingtoneMenu : DefaultWindow
public sealed partial class RingtoneMenu : FancyWindow
{
public string[] PreviousNoteInputs = new[] { "A", "A", "A", "A", "A", "A" };
public LineEdit[] RingerNoteInputs = default!;
public LineEdit[] RingerNoteInputs;
public event Action? SetRingtoneButtonPressed;
public event Action? TestRingtoneButtonPressed;
public RingtoneMenu()
{
RobustXamlLoader.Load(this);
SetRingerButton.OnPressed += _ => SetRingtoneButtonPressed?.Invoke();
TestRingerButton.OnPressed += _ => TestRingtoneButtonPressed?.Invoke();
RingerNoteInputs = new[] { RingerNoteOneInput, RingerNoteTwoInput, RingerNoteThreeInput, RingerNoteFourInput, RingerNoteFiveInput, RingerNoteSixInput };
for (var i = 0; i < RingerNoteInputs.Length; ++i)
@@ -43,14 +49,28 @@ namespace Content.Client.PDA.Ringer
foo();
input.CursorPosition = input.Text.Length; // Resets caret position to the end of the typed input
};
input.OnTextChanged += _ =>
{
input.Text = input.Text.ToUpper();
if (!IsNote(input.Text))
input.OnTextChanged += args =>
{
// Convert to uppercase
var upperText = args.Text.ToUpper();
// Filter to only valid notes
var newText = upperText;
if (!IsNote(newText))
{
newText = PreviousNoteInputs[index];
input.AddStyleClass("Caution");
}
else
{
PreviousNoteInputs[index] = newText;
input.RemoveStyleClass("Caution");
}
// Only update if there's a change
if (newText != input.Text)
input.Text = newText;
};
}
}