Skip to content

Commit

Permalink
Some changes with rocket integration to catch exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
DiFFoZ committed Jul 25, 2023
1 parent d78af8a commit 3248df7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
29 changes: 18 additions & 11 deletions PermissionExtensions/Events/UnturnedPlayerChatting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ public async Task HandleEventAsync(object? sender, UnturnedPlayerChattingEvent @
{
var isAdmin = @event.Player.SteamPlayer.isAdmin;
var isGold = @event.Player.SteamPlayer.isPro;
if (!m_Configuration.GetSection("override:color:admin").Get<bool>() && isAdmin || Provider.hideAdmins)

if (isAdmin && !Provider.hideAdmins && !m_Configuration.GetSection("override:color:admin").Get<bool>())
{
return;
}

if (!m_Configuration.GetSection("override:color:gold").Get<bool>() && isGold)
if (isGold && !m_Configuration.GetSection("override:color:gold").Get<bool>())
{
return;
}

var id = @event.Player.SteamId.ToString();
var id = @event.Player.EntityInstanceId;
var displayName = @event.Player.SteamPlayer.playerID.characterName;

var role = await m_PermissionExtensions.GetOrderedPermissionRoleData(id, KnownActorTypes.Player);
Expand Down Expand Up @@ -73,10 +73,10 @@ public async Task HandleEventAsync(object? sender, UnturnedPlayerChattingEvent @

var eventIsCancelled = @event.IsCancelled;
var eventColor = @event.Color.ToSystemColor();

CallRocketPlayerChatted(@event.Player, @event.Mode, @event.Message, ref eventColor,
ref eventIsCancelled);

@event.IsCancelled = eventIsCancelled;
@event.Color = eventColor.ToUnityColor();
}
Expand All @@ -89,28 +89,35 @@ private void CallRocketPlayerChatted(UnturnedPlayer player, EChatMode mode, stri
return;
}

var canOverrideColor = m_Configuration.GetSection("rocketmodIntegration:canOverrideColor").Get<bool>();
var canCancelMessage = m_Configuration.GetSection("rocketmodIntegration:canCancelMessage").Get<bool>();

if (!canOverrideColor && !canCancelMessage)
{
return;
}

m_Logger.LogDebug("Calling the event UnturnedChat.OnPlayerChatted");

var colorEx = color;
var cancelEx = cancel;

m_PermissionExtensions.CallRocketEvent(player, mode, message, ref colorEx, ref cancelEx);
if (m_Configuration.GetSection("rocketmodIntegration:canOverrideColor").Get<bool>())
if (canOverrideColor)
{
color = colorEx;
}

if (cancelEx)
{
m_Logger.LogDebug("RocketMod cancel the message!");
m_Logger.LogDebug("RocketMod cancelled the message!");
}
if (color != colorEx)
{
m_Logger.LogDebug("RocketMod override the color! {FromColor} -> {ToColor}", color, colorEx);
m_Logger.LogDebug("RocketMod overrides the color! {FromColor} -> {ToColor}", color, colorEx);
}

cancel = cancelEx && m_Configuration.GetSection("rocketmodIntegration:canCancelMessage").Get<bool>();

cancel = cancelEx && canCancelMessage;
}
}
}
17 changes: 15 additions & 2 deletions PermissionExtensions/Events/UnturnedUserConnecting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,21 @@ public async Task HandleEventAsync(object? sender, UnturnedUserConnectingEvent @
m_Logger.LogDebug("Found role {RoleDisplayName}({RoleId}) for player {FullName}",
role.DisplayName, role.Id, @event.User.FullActorName);

var prefix = role.Data?.ContainsKey("prefix") ?? false ? role.Data["prefix"] : string.Empty;
var suffix = role.Data?.ContainsKey("suffix") ?? false ? role.Data["suffix"] : string.Empty;
var prefix = string.Empty;
var suffix = string.Empty;

if (role.Data is not null)
{
if (role.Data.TryGetValue("prefix", out var obj))
{
prefix = (obj as string) ?? string.Empty;
}

if (role.Data.TryGetValue("suffix", out obj))
{
suffix = (obj as string) ?? string.Empty;
}
}

string pendingName = prefix + @event.User.DisplayName + suffix;

Expand Down
28 changes: 19 additions & 9 deletions PermissionExtensions/PermissionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public class PermissionExtensions : OpenModUnturnedPlugin
private readonly IUserDataStore m_UserDataStore;
private readonly ILogger<PermissionExtensions> m_Logger;
private readonly ILifetimeScope m_LifetimeScope;

private RocketModHandleChatPatch m_HandleChatPatch = null!;

public PermissionExtensions(IServiceProvider serviceProvider, IPermissionRolesDataStore permissionRolesDataStore,
Expand All @@ -37,25 +36,37 @@ public PermissionExtensions(IServiceProvider serviceProvider, IPermissionRolesDa
m_LifetimeScope = lifetimeScope;
}

protected override UniTask OnLoadAsync()
protected override async UniTask OnLoadAsync()
{
m_Logger.LogInformation("Made with <3 by EvolutionPlugins");
m_Logger.LogInformation("https://github.com/evolutionplugins \\ https://github.com/diffoz");
m_Logger.LogInformation("Support discord: https://discord.gg/6KymqGv");

if (RocketModIntegration.IsRocketModUnturnedLoaded(out var asm) && RocketModIntegration.IsRocketModInstalled())
try
{
m_HandleChatPatch =
ActivatorUtilitiesEx.CreateInstance<RocketModHandleChatPatch>(m_LifetimeScope,
m_Logger, Harmony, asm!);
if (RocketModIntegration.IsRocketModUnturnedLoaded(out var asm) && RocketModIntegration.IsRocketModInstalled())
{
m_HandleChatPatch =
ActivatorUtilitiesEx.CreateInstance<RocketModHandleChatPatch>(m_LifetimeScope,
m_Logger, Harmony, asm!);
}
}
catch
{
// rocketmod is not installed, ignoring the issue
}

return AddExample().AsUniTask(false);
await AddExample();
}

public void CallRocketEvent(UnturnedPlayer player, EChatMode chatMode, string message, ref Color color, ref bool cancel)
{
m_HandleChatPatch?.CallRocketEventInternal(player.Player, chatMode, ref color, message, ref cancel);
try
{
m_HandleChatPatch?.CallRocketEventInternal(player.Player, chatMode, ref color, message, ref cancel);
}
catch
{ }
}

public async Task<PermissionRoleData?> GetOrderedPermissionRoleData(string id, string type)
Expand Down Expand Up @@ -85,7 +96,6 @@ public void CallRocketEvent(UnturnedPlayer player, EChatMode chatMode, string me

private Task AddExample()
{
// ReSharper disable once ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator < bad rider
foreach (var role in m_PermissionRolesDataStore.Roles)
{
if (role?.Data == null)
Expand Down

0 comments on commit 3248df7

Please sign in to comment.