2021-06-09 22:19:39 +02:00
using Content.Shared.Verbs ;
2020-10-26 12:11:32 +01:00
using Robust.Server.Player ;
2020-03-25 11:16:57 +01:00
using Robust.Shared.GameObjects ;
2019-04-15 21:11:38 -06:00
using Robust.Shared.IoC ;
2020-08-13 14:40:27 +02:00
using Robust.Shared.Log ;
2018-11-21 20:58:11 +01:00
2021-06-09 22:19:39 +02:00
namespace Content.Server.Verbs
2018-11-21 20:58:11 +01:00
{
2021-10-05 14:29:03 +11:00
public sealed class VerbSystem : SharedVerbSystem
2018-11-21 20:58:11 +01:00
{
public override void Initialize ( )
{
base . Initialize ( ) ;
2021-10-05 14:29:03 +11:00
SubscribeNetworkEvent < RequestServerVerbsEvent > ( HandleVerbRequest ) ;
2021-10-28 18:21:19 +13:00
SubscribeNetworkEvent < ExecuteVerbEvent > ( HandleTryExecuteVerb ) ;
2020-10-26 12:11:32 +01:00
}
2021-10-05 14:29:03 +11:00
/// <summary>
/// Called when asked over the network to run a given verb.
/// </summary>
2021-10-28 18:21:19 +13:00
public void HandleTryExecuteVerb ( ExecuteVerbEvent args , EntitySessionEventArgs eventArgs )
2020-10-26 12:11:32 +01:00
{
2020-03-25 11:16:57 +01:00
var session = eventArgs . SenderSession ;
2020-02-18 19:43:54 -08:00
var userEntity = session . AttachedEntity ;
2018-11-21 20:58:11 +01:00
2020-08-15 20:38:37 +02:00
if ( userEntity = = null )
{
2021-10-05 14:29:03 +11:00
Logger . Warning ( $"{nameof(HandleTryExecuteVerb)} called by player {session} with no attached entity." ) ;
2020-08-15 20:38:37 +02:00
return ;
}
2021-10-05 14:29:03 +11:00
if ( ! EntityManager . TryGetEntity ( args . Target , out var targetEntity ) )
2018-11-21 20:58:11 +01:00
{
2021-10-05 14:29:03 +11:00
return ;
2020-02-18 19:43:54 -08:00
}
2018-11-21 20:58:11 +01:00
2021-10-05 14:29:03 +11:00
// Get the list of verbs. This effectively also checks that the requested verb is in fact a valid verb that
2021-10-28 18:21:19 +13:00
// the user can perform.
var verbs = GetLocalVerbs ( targetEntity , userEntity , args . Type ) [ args . Type ] ;
// Note that GetLocalVerbs might waste time checking & preparing unrelated verbs even though we know
// precisely which one we want to run. However, MOST entities will only have 1 or 2 verbs of a given type.
// The one exception here is the "other" verb type, which has 3-4 verbs + all the debug verbs.
2021-10-05 14:29:03 +11:00
// Find the requested verb.
if ( verbs . TryGetValue ( args . RequestedVerb , out var verb ) )
2021-11-23 23:00:16 +13:00
ExecuteVerb ( verb , userEntity . Uid , args . Target ) ;
2021-10-05 14:29:03 +11:00
else
2021-10-28 18:21:19 +13:00
// 404 Verb not found. Note that this could happen due to something as simple as opening the verb menu, walking away, then trying
// to run the pickup-item verb. So maybe this shouldn't even be logged?
Logger . Info ( $"{nameof(HandleTryExecuteVerb)} called by player {session} with an invalid verb: {args.RequestedVerb.Category?.Text} {args.RequestedVerb.Text}" ) ;
2020-02-18 19:43:54 -08:00
}
2018-11-21 20:58:11 +01:00
2021-10-05 14:29:03 +11:00
private void HandleVerbRequest ( RequestServerVerbsEvent args , EntitySessionEventArgs eventArgs )
2020-02-18 19:43:54 -08:00
{
2020-03-25 11:16:57 +01:00
var player = ( IPlayerSession ) eventArgs . SenderSession ;
2018-11-21 20:58:11 +01:00
2021-10-05 14:29:03 +11:00
if ( ! EntityManager . TryGetEntity ( args . EntityUid , out var target ) )
2020-02-18 19:43:54 -08:00
{
2021-10-05 14:29:03 +11:00
Logger . Warning ( $"{nameof(HandleVerbRequest)} called on a non-existent entity with id {args.EntityUid} by player {player}." ) ;
2020-02-18 19:43:54 -08:00
return ;
}
2019-11-23 19:11:50 +00:00
2021-10-28 18:21:19 +13:00
if ( player . AttachedEntity = = null )
2020-08-15 20:38:37 +02:00
{
2021-10-05 14:29:03 +11:00
Logger . Warning ( $"{nameof(HandleVerbRequest)} called by player {player} with no attached entity." ) ;
2020-08-15 20:38:37 +02:00
return ;
}
2021-10-28 18:21:19 +13:00
// We do not verify that the user has access to the requested entity. The individual verbs should check
// this, and some verbs (e.g. view variables) won't even care about whether an entity is accessible through
// the entity menu or not.
2020-02-18 19:43:54 -08:00
2021-10-28 18:21:19 +13:00
var response = new VerbsResponseEvent ( args . EntityUid , GetLocalVerbs ( target , player . AttachedEntity , args . Type ) ) ;
2020-03-25 11:16:57 +01:00
RaiseNetworkEvent ( response , player . ConnectedClient ) ;
2018-11-21 20:58:11 +01:00
}
}
}