-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'uncreated-networking-cleanup' of https://github.com/Unc…
…reatedStaff/UncreatedWarfare into uncreated-networking-cleanup
- Loading branch information
Showing
5 changed files
with
113 additions
and
4 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
UncreatedWarfare/Events/Models/Players/PlayerLeaveGroupRequested.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
UncreatedWarfare/Events/Patches/PlayerLeaveGroupRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters