Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 47 additions & 7 deletions plugins/rust/RustApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public class RustApp : RustPlugin

private static (string name, string value)[] _ApiHeaders = Array.Empty<(string, string)>();

private static readonly object _false = false; // avoid boxing for hooks

#endregion

#region Web API
Expand Down Expand Up @@ -2119,6 +2121,14 @@ private void OnServerInitialized()
Unsubscribe(nameof(OnClientCommand));
}

#if OXIDE
_chatCommandPrefixes = Interface.Oxide.Config.Commands.ChatPrefix.ToArray();
#endif

#if CARBON
_chatCommandPrefixes = API.Commands.Command.Prefixes.Select(p => p.Value).ToArray();
#endif

timer.Once(1f, () =>
{
MetaInfo.Read();
Expand Down Expand Up @@ -2329,11 +2339,11 @@ private void OnTeamLeave(RelationshipManager.PlayerTeam team, BasePlayer player)

#region Chat hooks

private static string[] _chatCommandPrefixes = new[] { "/" };

private object OnClientCommand(Connection connection, string text)
{
if (!text.StartsWith("chat.say", StringComparison.OrdinalIgnoreCase)
|| text.StartsWith("chat.say \"/", StringComparison.OrdinalIgnoreCase)
|| text.StartsWith("chat.say /", StringComparison.OrdinalIgnoreCase))
if (!IsChatSayCommand(text) || HasCommandPrefix(text))
{
return null;
}
Expand All @@ -2350,25 +2360,55 @@ private object OnClientCommand(Connection connection, string text)
SendMessage(player, msg);
}

return false;
return _false;
}

return null;

static bool IsChatSayCommand(string text)
{
return text.StartsWith("chat.say", StringComparison.OrdinalIgnoreCase);
}

static bool HasCommandPrefix(string text)
{
const int ChatSayLength = 8;

if (text.Length < ChatSayLength + 2)
{
return false;
}

var textSpan = text.AsSpan().Slice(ChatSayLength); // here we have at least 2 chars in textSpan
textSpan = textSpan.TrimStart().TrimStart('"'); // remove whitespaces and quotes
if (textSpan.IsEmpty)
{
return false;
}

foreach (var prefix in _chatCommandPrefixes)
{
if (textSpan.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}

return false;
}
}

private void OnPlayerChat(BasePlayer player, string message, ConVar.Chat.ChatChannel channel)
{
if (channel != ConVar.Chat.ChatChannel.Team && channel != ConVar.Chat.ChatChannel.Global && channel != ConVar.Chat.ChatChannel.Local)
if (channel is not ConVar.Chat.ChatChannel.Team and not ConVar.Chat.ChatChannel.Global and not ConVar.Chat.ChatChannel.Local)
{
return;
}

_RustAppEngine?.ChatWorker?.SaveChatMessage(new CourtApi.PluginChatMessageDto
{
steam_id = player.UserIDString,

is_team = channel == ConVar.Chat.ChatChannel.Team,

text = message
});
}
Expand Down