DoAfter Refactor (#13225)

Co-authored-by: DrSmugleaf <drsmugleaf@gmail.com>
This commit is contained in:
keronshb
2023-02-24 19:01:25 -05:00
committed by GitHub
parent 7a9baa79c2
commit 9ebb452a3c
129 changed files with 2624 additions and 4132 deletions

View File

@@ -1,5 +1,4 @@
using System.Threading;
using Content.Shared.Tools;
using Content.Shared.Tools;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Server.Tools.Components;
@@ -18,9 +17,4 @@ public sealed class LatticeCuttingComponent : Component
[DataField("vacuumDelay")]
public float VacuumDelay = 1.75f;
/// <summary>
/// Used for do_afters.
/// </summary>
public CancellationTokenSource? CancelTokenSource = null;
}

View File

@@ -15,10 +15,5 @@ namespace Content.Server.Tools.Components
[DataField("delay")]
public float Delay = 1f;
/// <summary>
/// Used for do_afters.
/// </summary>
public CancellationTokenSource? CancelToken = null;
}
}

View File

@@ -3,15 +3,15 @@ using Content.Server.Tools.Components;
using Content.Shared.Database;
using Content.Shared.Examine;
using Content.Shared.Interaction;
using Content.Shared.Tools;
using Content.Shared.Tools.Components;
using Robust.Server.GameObjects;
namespace Content.Server.Tools.Systems;
public sealed class WeldableSystem : EntitySystem
{
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
[Dependency] private readonly ToolSystem _toolSystem = default!;
[Dependency] private readonly SharedToolSystem _toolSystem = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
public override void Initialize()
@@ -65,9 +65,8 @@ public sealed class WeldableSystem : EntitySystem
if (!CanWeld(uid, tool, user, component))
return false;
component.BeingWelded = _toolSystem.UseTool(tool, user, uid, component.FuelConsumption,
component.WeldingTime.Seconds, component.WeldingQuality,
new WeldFinishedEvent(user, tool), new WeldCancelledEvent(), uid);
var toolEvData = new ToolEventData(new WeldFinishedEvent(user, tool), targetEntity: uid);
component.BeingWelded = _toolSystem.UseTool(tool, user, uid, component.WeldingTime.Seconds, new[] { component.WeldingQuality }, toolEvData, fuel: component.FuelConsumption);
// Log attempt
_adminLogger.Add(LogType.Action, LogImpact.Low, $"{ToPrettyString(user):user} is {(component.IsWelded ? "un" : "")}welding {ToPrettyString(uid):target} at {Transform(uid).Coordinates:targetlocation}");

View File

@@ -1,5 +1,4 @@
using System.Threading;
using Content.Server.Administration.Logs;
using Content.Server.Administration.Logs;
using Content.Server.Maps;
using Content.Server.Tools.Components;
using Content.Shared.Database;
@@ -19,17 +18,10 @@ public sealed partial class ToolSystem
{
SubscribeLocalEvent<LatticeCuttingComponent, AfterInteractEvent>(OnLatticeCuttingAfterInteract);
SubscribeLocalEvent<LatticeCuttingComponent, LatticeCuttingCompleteEvent>(OnLatticeCutComplete);
SubscribeLocalEvent<LatticeCuttingComponent, LatticeCuttingCancelledEvent>(OnLatticeCutCancelled);
}
private void OnLatticeCutCancelled(EntityUid uid, LatticeCuttingComponent component, LatticeCuttingCancelledEvent args)
{
component.CancelTokenSource = null;
}
private void OnLatticeCutComplete(EntityUid uid, LatticeCuttingComponent component, LatticeCuttingCompleteEvent args)
{
component.CancelTokenSource = null;
var gridUid = args.Coordinates.GetGridUid(EntityManager);
if (gridUid == null)
return;
@@ -52,17 +44,14 @@ public sealed partial class ToolSystem
if (args.Handled || !args.CanReach || args.Target != null)
return;
if (TryCut(args.User, component, args.ClickLocation))
if (TryCut(uid, args.User, component, args.ClickLocation))
args.Handled = true;
}
private bool TryCut(EntityUid user, LatticeCuttingComponent component, EntityCoordinates clickLocation)
private bool TryCut(EntityUid toolEntity, EntityUid user, LatticeCuttingComponent component, EntityCoordinates clickLocation)
{
if (component.CancelTokenSource != null)
return true;
ToolComponent? tool = null;
if (component.ToolComponentNeeded && !TryComp<ToolComponent?>(component.Owner, out tool))
if (component.ToolComponentNeeded && !TryComp<ToolComponent?>(toolEntity, out tool))
return false;
if (!_mapManager.TryGetGrid(clickLocation.GetGridUid(EntityManager), out var mapGrid))
@@ -82,17 +71,10 @@ public sealed partial class ToolSystem
|| tile.IsBlockedTurf(true))
return false;
var tokenSource = new CancellationTokenSource();
component.CancelTokenSource = tokenSource;
var toolEvData = new ToolEventData(new LatticeCuttingCompleteEvent(clickLocation, user), targetEntity: toolEntity);
if (!UseTool(component.Owner, user, null, 0f, component.Delay, new[] {component.QualityNeeded},
new LatticeCuttingCompleteEvent
{
Coordinates = clickLocation,
User = user
}, new LatticeCuttingCancelledEvent(), toolComponent: tool, doAfterEventTarget: component.Owner,
cancelToken: tokenSource.Token))
component.CancelTokenSource = null;
if (!UseTool(toolEntity, user, null, component.Delay, new[] {component.QualityNeeded}, toolEvData, toolComponent: tool))
return false;
return true;
}
@@ -101,10 +83,12 @@ public sealed partial class ToolSystem
{
public EntityCoordinates Coordinates;
public EntityUid User;
}
private sealed class LatticeCuttingCancelledEvent : EntityEventArgs
{
public LatticeCuttingCompleteEvent(EntityCoordinates coordinates, EntityUid user)
{
Coordinates = coordinates;
User = user;
}
}
}

View File

@@ -19,17 +19,10 @@ public sealed partial class ToolSystem
{
SubscribeLocalEvent<TilePryingComponent, AfterInteractEvent>(OnTilePryingAfterInteract);
SubscribeLocalEvent<TilePryingComponent, TilePryingCompleteEvent>(OnTilePryComplete);
SubscribeLocalEvent<TilePryingComponent, TilePryingCancelledEvent>(OnTilePryCancelled);
}
private void OnTilePryCancelled(EntityUid uid, TilePryingComponent component, TilePryingCancelledEvent args)
{
component.CancelToken = null;
}
private void OnTilePryComplete(EntityUid uid, TilePryingComponent component, TilePryingCompleteEvent args)
{
component.CancelToken = null;
var gridUid = args.Coordinates.GetGridUid(EntityManager);
if (!_mapManager.TryGetGrid(gridUid, out var grid))
{
@@ -45,18 +38,13 @@ public sealed partial class ToolSystem
{
if (args.Handled || !args.CanReach || (args.Target != null && !HasComp<PuddleComponent>(args.Target))) return;
if (TryPryTile(args.User, component, args.ClickLocation))
if (TryPryTile(uid, args.User, component, args.ClickLocation))
args.Handled = true;
}
private bool TryPryTile(EntityUid user, TilePryingComponent component, EntityCoordinates clickLocation)
private bool TryPryTile(EntityUid toolEntity, EntityUid user, TilePryingComponent component, EntityCoordinates clickLocation)
{
if (component.CancelToken != null)
{
return true;
}
if (!TryComp<ToolComponent?>(component.Owner, out var tool) && component.ToolComponentNeeded)
if (!TryComp<ToolComponent?>(toolEntity, out var tool) && component.ToolComponentNeeded)
return false;
if (!_mapManager.TryGetGrid(clickLocation.GetGridUid(EntityManager), out var mapGrid))
@@ -74,34 +62,22 @@ public sealed partial class ToolSystem
if (!tileDef.CanCrowbar)
return false;
var token = new CancellationTokenSource();
component.CancelToken = token;
var toolEvData = new ToolEventData(new TilePryingCompleteEvent(clickLocation), targetEntity:toolEntity);
bool success = UseTool(
component.Owner,
user,
null,
0f,
component.Delay,
new [] {component.QualityNeeded},
new TilePryingCompleteEvent
{
Coordinates = clickLocation,
},
new TilePryingCancelledEvent(),
toolComponent: tool,
doAfterEventTarget: component.Owner,
cancelToken: token.Token);
if (!success)
component.CancelToken = null;
if (!UseTool(toolEntity, user, null, component.Delay, new[] { component.QualityNeeded }, toolEvData, toolComponent: tool))
return false;
return true;
}
private sealed class TilePryingCompleteEvent : EntityEventArgs
{
public EntityCoordinates Coordinates { get; init; }
public readonly EntityCoordinates Coordinates;
public TilePryingCompleteEvent(EntityCoordinates coordinates)
{
Coordinates = coordinates;
}
}
private sealed class TilePryingCancelledEvent : EntityEventArgs

View File

@@ -1,17 +1,9 @@
using Content.Server.Atmos.EntitySystems;
using Content.Server.Chemistry.EntitySystems;
using Content.Server.DoAfter;
using Content.Server.Popups;
using Content.Shared.Audio;
using Content.Shared.Item;
using Content.Shared.Tools;
using Content.Shared.Tools.Components;
using Robust.Server.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.Map;
using Robust.Shared.Player;
using System.Threading;
using System.Threading.Tasks;
namespace Content.Server.Tools
{
@@ -20,12 +12,10 @@ namespace Content.Server.Tools
{
[Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = default!;
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly DoAfterSystem _doAfterSystem = default!;
[Dependency] private readonly SolutionContainerSystem _solutionContainerSystem = default!;
[Dependency] private readonly AtmosphereSystem _atmosphereSystem = default!;
[Dependency] private readonly PopupSystem _popupSystem = default!;
[Dependency] private readonly TransformSystem _transformSystem = default!;
[Dependency] private readonly SharedItemSystem _itemSystem = default!;
public override void Initialize()
{
@@ -34,220 +24,6 @@ namespace Content.Server.Tools
InitializeTilePrying();
InitializeLatticeCutting();
InitializeWelders();
SubscribeLocalEvent<ToolDoAfterComplete>(OnDoAfterComplete);
SubscribeLocalEvent<ToolDoAfterCancelled>(OnDoAfterCancelled);
}
private void OnDoAfterComplete(ToolDoAfterComplete ev)
{
// Actually finish the tool use! Depending on whether that succeeds or not, either event will be broadcast.
if(ToolFinishUse(ev.Uid, ev.UserUid, ev.Fuel))
{
if (ev.EventTarget != null)
RaiseLocalEvent(ev.EventTarget.Value, ev.CompletedEvent, false);
else
RaiseLocalEvent(ev.CompletedEvent);
}
else if(ev.CancelledEvent != null)
{
if (ev.EventTarget != null)
RaiseLocalEvent(ev.EventTarget.Value, ev.CancelledEvent, false);
else
RaiseLocalEvent(ev.CancelledEvent);
}
}
private void OnDoAfterCancelled(ToolDoAfterCancelled ev)
{
if (ev.EventTarget != null)
RaiseLocalEvent(ev.EventTarget.Value, ev.Event, false);
else
RaiseLocalEvent(ev.Event);
}
/// <summary>
/// Whether a tool entity has the specified quality or not.
/// </summary>
public bool HasQuality(EntityUid uid, string quality, ToolComponent? tool = null)
{
return Resolve(uid, ref tool, false) && tool.Qualities.Contains(quality);
}
/// <summary>
/// Whether a tool entity has all specified qualities or not.
/// </summary>
public bool HasAllQualities(EntityUid uid, IEnumerable<string> qualities, ToolComponent? tool = null)
{
return Resolve(uid, ref tool, false) && tool.Qualities.ContainsAll(qualities);
}
/// <summary>
/// Sync version of UseTool.
/// </summary>
/// <param name="tool">The tool entity.</param>
/// <param name="user">The entity using the tool.</param>
/// <param name="target">Optionally, a target to use the tool on.</param>
/// <param name="fuel">An optional amount of fuel or energy to consume-</param>
/// <param name="doAfterDelay">A doAfter delay in seconds.</param>
/// <param name="toolQualitiesNeeded">The tool qualities needed to use the tool.</param>
/// <param name="doAfterCompleteEvent">An event to raise once the doAfter is completed successfully.</param>
/// <param name="doAfterCancelledEvent">An event to raise once the doAfter is canceled.</param>
/// <param name="doAfterEventTarget">Where to direct the do-after events. If null, events are broadcast</param>
/// <param name="doAfterCheck">An optional check to perform for the doAfter.</param>
/// <param name="toolComponent">The tool component.</param>
/// <param name="cancelToken">Token to provide to do_after for cancelling</param>
/// <returns>Whether initially, using the tool succeeded. If there's a doAfter delay, you'll need to listen to
/// the <see cref="doAfterCompleteEvent"/> and <see cref="doAfterCancelledEvent"/> being broadcast
/// to see whether using the tool succeeded or not. If the <see cref="doAfterDelay"/> is zero,
/// this simply returns whether using the tool succeeded or not.</returns>
public override bool UseTool(
EntityUid tool,
EntityUid user,
EntityUid? target,
float fuel,
float doAfterDelay,
IEnumerable<string> toolQualitiesNeeded,
object? doAfterCompleteEvent = null,
object? doAfterCancelledEvent = null,
EntityUid? doAfterEventTarget = null,
Func<bool>? doAfterCheck = null,
ToolComponent? toolComponent = null,
CancellationToken? cancelToken = null)
{
// No logging here, after all that'd mean the caller would need to check if the component is there or not.
if (!Resolve(tool, ref toolComponent, false))
return false;
var ev = new ToolUserAttemptUseEvent(user, target);
RaiseLocalEvent(user, ref ev);
if (ev.Cancelled)
return false;
if (!ToolStartUse(tool, user, fuel, toolQualitiesNeeded, toolComponent))
return false;
if (doAfterDelay > 0f)
{
var doAfterArgs = new DoAfterEventArgs(user, doAfterDelay / toolComponent.SpeedModifier, cancelToken ?? default, target)
{
ExtraCheck = doAfterCheck,
BreakOnDamage = true,
BreakOnStun = true,
BreakOnTargetMove = true,
BreakOnUserMove = true,
NeedHand = true,
BroadcastFinishedEvent = doAfterCompleteEvent != null ? new ToolDoAfterComplete(doAfterCompleteEvent, doAfterCancelledEvent, tool, user, fuel, doAfterEventTarget) : null,
BroadcastCancelledEvent = doAfterCancelledEvent != null ? new ToolDoAfterCancelled(doAfterCancelledEvent, doAfterEventTarget) : null,
};
_doAfterSystem.DoAfter(doAfterArgs);
return true;
}
return ToolFinishUse(tool, user, fuel, toolComponent);
}
/// <summary>
/// Async version of UseTool.
/// </summary>
/// <param name="tool">The tool entity.</param>
/// <param name="user">The entity using the tool.</param>
/// <param name="target">Optionally, a target to use the tool on.</param>
/// <param name="fuel">An optional amount of fuel or energy to consume-</param>
/// <param name="doAfterDelay">A doAfter delay in seconds.</param>
/// <param name="toolQualitiesNeeded">The tool qualities needed to use the tool.</param>
/// <param name="doAfterCheck">An optional check to perform for the doAfter.</param>
/// <param name="toolComponent">The tool component.</param>
/// <returns>Whether using the tool succeeded or not.</returns>
public async Task<bool> UseTool(EntityUid tool, EntityUid user, EntityUid? target, float fuel,
float doAfterDelay, IEnumerable<string> toolQualitiesNeeded, Func<bool>? doAfterCheck = null,
ToolComponent? toolComponent = null)
{
// No logging here, after all that'd mean the caller would need to check if the component is there or not.
if (!Resolve(tool, ref toolComponent, false))
return false;
var ev = new ToolUserAttemptUseEvent(user, target);
RaiseLocalEvent(user, ref ev);
if (ev.Cancelled)
return false;
if (!ToolStartUse(tool, user, fuel, toolQualitiesNeeded, toolComponent))
return false;
if (doAfterDelay > 0f)
{
var doAfterArgs = new DoAfterEventArgs(user, doAfterDelay / toolComponent.SpeedModifier, default, target)
{
ExtraCheck = doAfterCheck,
BreakOnDamage = true,
BreakOnStun = true,
BreakOnTargetMove = true,
BreakOnUserMove = true,
NeedHand = true,
};
var result = await _doAfterSystem.WaitDoAfter(doAfterArgs);
if (result == DoAfterStatus.Cancelled)
return false;
}
return ToolFinishUse(tool, user, fuel, toolComponent);
}
// This is hilariously long.
/// <inheritdoc cref="UseTool(Robust.Shared.GameObjects.EntityUid,Robust.Shared.GameObjects.EntityUid,System.Nullable{Robust.Shared.GameObjects.EntityUid},float,float,System.Collections.Generic.IEnumerable{string},Robust.Shared.GameObjects.EntityUid,object,object,System.Func{bool}?,Content.Shared.Tools.Components.ToolComponent?)"/>
public Task<bool> UseTool(EntityUid tool, EntityUid user, EntityUid? target, float fuel,
float doAfterDelay, string toolQualityNeeded, Func<bool>? doAfterCheck = null,
ToolComponent? toolComponent = null)
{
return UseTool(tool, user, target, fuel, doAfterDelay, new [] {toolQualityNeeded}, doAfterCheck, toolComponent);
}
private bool ToolStartUse(EntityUid tool, EntityUid user, float fuel, IEnumerable<string> toolQualitiesNeeded, ToolComponent? toolComponent = null)
{
if (!Resolve(tool, ref toolComponent))
return false;
if (!toolComponent.Qualities.ContainsAll(toolQualitiesNeeded))
return false;
var beforeAttempt = new ToolUseAttemptEvent(fuel, user);
RaiseLocalEvent(tool, beforeAttempt, false);
return !beforeAttempt.Cancelled;
}
private bool ToolFinishUse(EntityUid tool, EntityUid user, float fuel, ToolComponent? toolComponent = null)
{
if (!Resolve(tool, ref toolComponent))
return false;
var afterAttempt = new ToolUseFinishAttemptEvent(fuel, user);
RaiseLocalEvent(tool, afterAttempt, false);
if (afterAttempt.Cancelled)
return false;
if (toolComponent.UseSound != null)
PlayToolSound(tool, toolComponent);
return true;
}
public void PlayToolSound(EntityUid uid, ToolComponent? tool = null)
{
if (!Resolve(uid, ref tool))
return;
if (tool.UseSound is not {} sound)
return;
// Pass tool.Owner to Filter.Pvs to avoid a TryGetEntity call.
SoundSystem.Play(sound.GetSound(), Filter.Pvs(tool.Owner),
uid, AudioHelpers.WithVariation(0.175f).WithVolume(-5f));
}
public override void Update(float frameTime)
@@ -256,85 +32,5 @@ namespace Content.Server.Tools
UpdateWelders(frameTime);
}
private sealed class ToolDoAfterComplete : EntityEventArgs
{
public readonly object CompletedEvent;
public readonly object? CancelledEvent;
public readonly EntityUid Uid;
public readonly EntityUid UserUid;
public readonly float Fuel;
public readonly EntityUid? EventTarget;
public ToolDoAfterComplete(object completedEvent, object? cancelledEvent, EntityUid uid, EntityUid userUid, float fuel, EntityUid? eventTarget = null)
{
CompletedEvent = completedEvent;
Uid = uid;
UserUid = userUid;
Fuel = fuel;
CancelledEvent = cancelledEvent;
EventTarget = eventTarget;
}
}
private sealed class ToolDoAfterCancelled : EntityEventArgs
{
public readonly object Event;
public readonly EntityUid? EventTarget;
public ToolDoAfterCancelled(object @event, EntityUid? eventTarget = null)
{
Event = @event;
EventTarget = eventTarget;
}
}
}
/// <summary>
/// Attempt event called *before* any do afters to see if the tool usage should succeed or not.
/// You can change the fuel consumption by changing the Fuel property.
/// </summary>
public sealed class ToolUseAttemptEvent : CancellableEntityEventArgs
{
public float Fuel { get; set; }
public EntityUid User { get; }
public ToolUseAttemptEvent(float fuel, EntityUid user)
{
Fuel = fuel;
User = user;
}
}
/// <summary>
/// Event raised on the user of a tool to see if they can actually use it.
/// </summary>
[ByRefEvent]
public struct ToolUserAttemptUseEvent
{
public EntityUid User;
public EntityUid? Target;
public bool Cancelled = false;
public ToolUserAttemptUseEvent(EntityUid user, EntityUid? target)
{
User = user;
Target = target;
}
}
/// <summary>
/// Attempt event called *after* any do afters to see if the tool usage should succeed or not.
/// You can use this event to consume any fuel needed.
/// </summary>
public sealed class ToolUseFinishAttemptEvent : CancellableEntityEventArgs
{
public float Fuel { get; }
public EntityUid User { get; }
public ToolUseFinishAttemptEvent(float fuel, EntityUid user)
{
Fuel = fuel;
}
}
}