Skip to content

Commit

Permalink
Merge branch 'uncreated-networking-cleanup' of https://github.com/Unc…
Browse files Browse the repository at this point in the history
…reatedStaff/UncreatedWarfare into uncreated-networking-cleanup
  • Loading branch information
DanielWillett committed Dec 31, 2024
2 parents 0c8485a + 7d02c13 commit 4318f6d
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Uncreated.Warfare.Events.Models.Players;

/// <summary>
/// Event listener args which fires after a player attempts to leave their current group from the vanilla Group menu.
/// </summary>
public class PlayerLeaveGroupRequested : CancellablePlayerEvent
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ internal sealed class InteractableBedReceiveClaimRequest : IHarmonyPatch

void IHarmonyPatch.Patch(ILogger logger, Harmony patcher)
{
_target = typeof(InteractableBed).GetMethod(nameof(InteractableBed.ReceiveClaimRequest), BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
_target = typeof(InteractableBed).GetMethod(nameof(InteractableBed.ReceiveClaimRequest), BindingFlags.Instance | BindingFlags.Public);

if (_target != null)
{
patcher.Patch(_target, prefix: Accessor.GetMethod(Prefix));
logger.LogDebug("Patched {0} for bedroll claim requested.", _target);
logger.LogDebug("Patched {0} for bedroll claim requested event.", _target);
return;
}

Expand All @@ -39,13 +39,13 @@ void IHarmonyPatch.Unpatch(ILogger logger, Harmony patcher)
return;

patcher.Unpatch(_target, Accessor.GetMethod(Prefix));
logger.LogDebug("Unpatched {0} for sign text updated event.", _target);
logger.LogDebug("Unpatched {0} for bedroll claim requested event.", _target);
_target = null;
}

// SDG.Unturned.InteractableSign
/// <summary>
/// Postfix of <see cref="InteractableBed.ReceiveClaimRequest"/> to invoke <see cref="SignTextChanged"/>.
/// Postfix of <see cref="InteractableBed.ReceiveClaimRequest"/> to invoke <see cref="ClaimBedRequested"/>.
/// </summary>
private static bool Prefix(InteractableBed __instance, in ServerInvocationContext context)
{
Expand Down
66 changes: 66 additions & 0 deletions UncreatedWarfare/Events/Patches/PlayerLeaveGroupRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using DanielWillett.ReflectionTools;
using DanielWillett.ReflectionTools.Formatting;
using HarmonyLib;
using System.Reflection;
using Uncreated.Warfare.Events.Models.Barricades;
using Uncreated.Warfare.Events.Models.Players;
using Uncreated.Warfare.Interaction;
using Uncreated.Warfare.Patches;
using Uncreated.Warfare.Players;
using Uncreated.Warfare.Players.Management;
using Uncreated.Warfare.Players.Permissions;

namespace Uncreated.Warfare.Events.Patches;

[UsedImplicitly]
internal sealed class PlayerLeaveGroupRequest : IHarmonyPatch
{
private static MethodInfo? _target;

void IHarmonyPatch.Patch(ILogger logger, Harmony patcher)
{
_target = typeof(GroupManager).GetMethod(nameof(GroupManager.requestGroupExit), BindingFlags.Static | BindingFlags.Public);

if (_target != null)
{
patcher.Patch(_target, prefix: Accessor.GetMethod(Prefix));
logger.LogDebug("Patched {0} for request leave group event.", _target);
return;
}

logger.LogError("Failed to find method: {0}.",
new MethodDefinition(nameof(GroupManager.requestGroupExit))
.DeclaredIn<GroupManager>(isStatic: true)
.WithParameter<Player>("player")
.ReturningVoid()
);
}

void IHarmonyPatch.Unpatch(ILogger logger, Harmony patcher)
{
if (_target == null)
return;

patcher.Unpatch(_target, Accessor.GetMethod(Prefix));
logger.LogDebug("Unpatched {0} for request leave group event.", _target);
_target = null;
}

// SDG.Unturned.InteractableSign
/// <summary>
/// Postfix of <see cref="GroupManager.requestGroupExit"/> to invoke <see cref="PlayerLeaveGroupRequested"/>.
/// </summary>
private static bool Prefix(Player player)
{
IContainer serviceProvider = WarfareModule.Singleton.ServiceProvider;

WarfarePlayer warfarePlayer = serviceProvider.Resolve<IPlayerService>().GetOnlinePlayer(player);

PlayerLeaveGroupRequested args = new()
{
Player = warfarePlayer,
};

return WarfareModule.EventDispatcher.DispatchEventAsync(args).GetAwaiter().GetResult();
}
}
32 changes: 32 additions & 0 deletions UncreatedWarfare/Tweaks/PreventLeaveGroupTweak.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using Uncreated.Warfare.Events.Models;
using Uncreated.Warfare.Events.Models.Players;
using Uncreated.Warfare.Interaction;
using Uncreated.Warfare.Players;
using Uncreated.Warfare.Players.Permissions;
using Uncreated.Warfare.Translations;

namespace Uncreated.Warfare.Tweaks;

public class PreventLeaveGroupTweak : IAsyncEventListener<PlayerLeaveGroupRequested>
{
public const string LeaveGroupPermissionName = "warfare::features.leavegroup";
public static readonly PermissionLeaf LeaveGroupPermission = new(LeaveGroupPermissionName);
public async UniTask HandleEventAsync(PlayerLeaveGroupRequested e, IServiceProvider serviceProvider,
CancellationToken token = default)
{
UserPermissionStore? permissionStore = serviceProvider.GetService<UserPermissionStore>();
if (permissionStore == null)
return;

if (await permissionStore.HasPermissionAsync(e.Player, LeaveGroupPermission, token))
return;

ChatService chatService = serviceProvider.GetRequiredService<ChatService>();
PlayersTranslations translations = serviceProvider.GetRequiredService<TranslationInjection<PlayersTranslations>>().Value;

chatService.Send(e.Player, translations.NoLeavingGroup);
e.Cancel();
}
}
2 changes: 2 additions & 0 deletions UncreatedWarfare/WarfareModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,8 @@ private void ConfigureServices(ContainerBuilder bldr)
.InstancePerMatchingLifetimeScope(LifetimeScopeTags.Session);
bldr.RegisterType<LandmineExplosionRestrictions>().AsImplementedInterfaces()
.InstancePerMatchingLifetimeScope(LifetimeScopeTags.Session);
bldr.RegisterType<PreventLeaveGroupTweak>().AsImplementedInterfaces()
.InstancePerMatchingLifetimeScope(LifetimeScopeTags.Session);

// Localization
bldr.RegisterType<LanguageService>()
Expand Down

0 comments on commit 4318f6d

Please sign in to comment.