Skip to content

Commit 7047231

Browse files
committed
Add project files.
1 parent 8946720 commit 7047231

File tree

11 files changed

+406
-0
lines changed

11 files changed

+406
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
namespace Source2Framework.Services.Chat
2+
{
3+
using CounterStrikeSharp.API.Core;
4+
using CounterStrikeSharp.API.Modules.Commands;
5+
using CounterStrikeSharp.API.Modules.Utils;
6+
7+
using Source2Framework.Models;
8+
9+
public interface IChatService : ISharedService
10+
{
11+
public delegate HookResult OnPlayerSayEvent(CCSPlayerController player, CommandInfo info, ref string message);
12+
13+
public event OnPlayerSayEvent? OnPlayerSayPre;
14+
15+
public event OnPlayerSayEvent? OnPlayerSay;
16+
17+
public event OnPlayerSayEvent? OnPlayerSayPost;
18+
19+
public delegate HookResult OnPlayerSayTeamEvent(CCSPlayerController player, CommandInfo info, ref string message);
20+
21+
public event OnPlayerSayEvent? OnPlayerSayTeamPre;
22+
23+
public event OnPlayerSayEvent? OnPlayerSayTeam;
24+
25+
public event OnPlayerSayEvent? OnPlayerSayTeamPost;
26+
27+
public string GetPrefix();
28+
29+
public void PrintToChat(CCSPlayerController player, string message);
30+
31+
public void PrintToChatAll(string message, Func<CCSPlayerController, bool>? predicate = null);
32+
33+
public void PrintToTeam(string message, CsTeam team);
34+
}
35+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.213" />
11+
<PackageReference Include="Source2Framework.SDK" Version="1.0.1" />
12+
</ItemGroup>
13+
14+
</Project>

Source2Framework.ChatService.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.9.34526.213
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Source2Framework.ChatService", "Source2Framework.ChatService\Source2Framework.ChatService.csproj", "{1E816D72-D37E-4CD3-A96F-E40D50CD72F5}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Source2Framework.ChatService.API", "Source2Framework.ChatService.API\Source2Framework.ChatService.API.csproj", "{64EE4E37-254C-4A44-B2AE-808610102692}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{1E816D72-D37E-4CD3-A96F-E40D50CD72F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{1E816D72-D37E-4CD3-A96F-E40D50CD72F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{1E816D72-D37E-4CD3-A96F-E40D50CD72F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{1E816D72-D37E-4CD3-A96F-E40D50CD72F5}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{64EE4E37-254C-4A44-B2AE-808610102692}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{64EE4E37-254C-4A44-B2AE-808610102692}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{64EE4E37-254C-4A44-B2AE-808610102692}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{64EE4E37-254C-4A44-B2AE-808610102692}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {37BF4B10-BFDA-4392-8BA6-8BAF1C1A2143}
30+
EndGlobalSection
31+
EndGlobal
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
namespace Source2Framework.Services.Chat
2+
{
3+
using CounterStrikeSharp.API.Core;
4+
using CounterStrikeSharp.API.Core.Plugin;
5+
using CounterStrikeSharp.API.Modules.Utils;
6+
using Microsoft.Extensions.Logging;
7+
8+
using Source2Framework.Extensions;
9+
using Source2Framework.Models;
10+
using Source2Framework.Services.Command;
11+
12+
public sealed partial class ChatService : SharedService<ChatService>, IChatService
13+
{
14+
public required ICommandService CommandService;
15+
16+
public static string ChatPrefix { get; set; } = string.Empty;
17+
18+
public ChatService(ILogger<ChatService> logger, IPluginContext pluginContext) : base(logger, pluginContext)
19+
{ }
20+
21+
public override void Initialize(bool hotReload)
22+
{
23+
this.Plugin.AddCommandListener("say", this.OnSay);
24+
this.Plugin.AddCommandListener("say_team", this.OnSayTeam);
25+
26+
ChatPrefix = ((this.Plugin as ChatServicePlugin)!.Config.ChatPrefix);
27+
}
28+
29+
public override void OnServiceInitialized(IService service)
30+
{
31+
if (service is ICommandService commandService)
32+
{
33+
this.CommandService = commandService;
34+
}
35+
}
36+
37+
public override void Shutdown(bool hotReload)
38+
{
39+
this.Plugin.RemoveCommandListener("say", this.OnSay, HookMode.Pre);
40+
this.Plugin.RemoveCommandListener("say_team", this.OnSayTeam, HookMode.Pre);
41+
}
42+
43+
public void PrintToChat(CCSPlayerController player, string message)
44+
{
45+
player.CPrintToChat(message);
46+
}
47+
48+
public void PrintToChatAll(string message, Func<CCSPlayerController, bool>? predicate = null)
49+
{
50+
SDK.CPrintToChatAll(message, predicate);
51+
}
52+
53+
public void PrintToTeam(string message, CsTeam team)
54+
{
55+
PrintToChatAll(message, (player) => player.Team == team);
56+
}
57+
58+
public string GetPrefix()
59+
{
60+
return ChatPrefix;
61+
}
62+
63+
private bool ShouldIgnore(IEnumerable<string> triggers, string command)
64+
{
65+
if (command.Contains(' '))
66+
{
67+
command = command.Split(' ').First();
68+
}
69+
70+
string? prefix = triggers.FirstOrDefault((t) => command.StartsWith(t));
71+
72+
if (prefix != null)
73+
{
74+
command = command.ReplaceFirst(prefix, command.StartsWith($"{prefix}css") ? string.Empty : "css_");
75+
76+
if (this.CommandService.IsCommandRegistered(command))
77+
{
78+
return true;
79+
}
80+
}
81+
82+
return false;
83+
}
84+
}
85+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
namespace Source2Framework.Services.Chat
2+
{
3+
using CounterStrikeSharp.API.Core;
4+
using CounterStrikeSharp.API.Modules.Commands;
5+
6+
using Source2Framework.Extensions;
7+
8+
public sealed partial class ChatService : IChatService
9+
{
10+
public event IChatService.OnPlayerSayEvent? OnPlayerSayPre;
11+
12+
public event IChatService.OnPlayerSayEvent? OnPlayerSay;
13+
14+
public event IChatService.OnPlayerSayEvent? OnPlayerSayPost;
15+
16+
private HookResult OnSay(CCSPlayerController? player, CommandInfo info)
17+
{
18+
if (player == null || !player.IsValid() || info.GetArg(1).Length == 0)
19+
return HookResult.Continue;
20+
21+
string? message = info.GetArg(1);
22+
23+
if (message == null)
24+
{
25+
return HookResult.Continue;
26+
}
27+
28+
HookResult preResult = this.OnPlayerSayPre?.Invoke(player, info, ref message) ?? HookResult.Continue;
29+
30+
if (preResult != HookResult.Continue)
31+
{
32+
return preResult;
33+
}
34+
35+
if (this.ShouldIgnore(CoreConfig.PublicChatTrigger, message))
36+
{
37+
return HookResult.Continue;
38+
}
39+
40+
if (this.ShouldIgnore(CoreConfig.SilentChatTrigger, message))
41+
{
42+
return HookResult.Continue;
43+
}
44+
45+
HookResult result = this.OnPlayerSay?.Invoke(player, info, ref message) ?? HookResult.Continue;
46+
47+
if (result == HookResult.Handled)
48+
{
49+
return HookResult.Handled;
50+
}
51+
52+
HookResult postResult = this.OnPlayerSayPost?.Invoke(player, info, ref message) ?? HookResult.Continue;
53+
54+
if (postResult == HookResult.Changed)
55+
{
56+
this.PrintToChatAll(message);
57+
}
58+
59+
return HookResult.Handled;
60+
}
61+
}
62+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
namespace Source2Framework.Services.Chat
2+
{
3+
using CounterStrikeSharp.API.Core;
4+
using CounterStrikeSharp.API.Modules.Commands;
5+
6+
using Source2Framework.Extensions;
7+
8+
public sealed partial class ChatService : IChatService
9+
{
10+
public event IChatService.OnPlayerSayEvent? OnPlayerSayTeamPre;
11+
12+
public event IChatService.OnPlayerSayEvent? OnPlayerSayTeam;
13+
14+
public event IChatService.OnPlayerSayEvent? OnPlayerSayTeamPost;
15+
16+
private HookResult OnSayTeam(CCSPlayerController? player, CommandInfo info)
17+
{
18+
if (player == null || !player.IsValid() || info.GetArg(1).Length == 0)
19+
return HookResult.Continue;
20+
21+
string? message = info.GetArg(1);
22+
23+
if (message == null)
24+
{
25+
return HookResult.Continue;
26+
}
27+
28+
HookResult preResult = this.OnPlayerSayTeamPre?.Invoke(player, info, ref message) ?? HookResult.Continue;
29+
30+
if (preResult != HookResult.Continue)
31+
{
32+
return preResult;
33+
}
34+
35+
if (this.ShouldIgnore(CoreConfig.PublicChatTrigger, message))
36+
{
37+
return HookResult.Continue;
38+
}
39+
40+
if (this.ShouldIgnore(CoreConfig.SilentChatTrigger, message))
41+
{
42+
return HookResult.Continue;
43+
}
44+
45+
HookResult result = this.OnPlayerSayTeam?.Invoke(player, info, ref message) ?? HookResult.Continue;
46+
47+
if (result == HookResult.Handled)
48+
{
49+
return HookResult.Handled;
50+
}
51+
52+
HookResult postResult = this.OnPlayerSayTeamPost?.Invoke(player, info, ref message) ?? HookResult.Continue;
53+
54+
if (postResult < HookResult.Handled)
55+
{
56+
this.PrintToTeam(message, player.Team);
57+
}
58+
59+
return HookResult.Handled;
60+
}
61+
}
62+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
namespace Source2Framework
2+
{
3+
using CounterStrikeSharp.API.Core;
4+
using CounterStrikeSharp.API.Core.Attributes;
5+
6+
using Source2Framework.Models;
7+
8+
using Source2Framework.Services.Chat;
9+
10+
[MinimumApiVersion(213)]
11+
public sealed partial class ChatServicePlugin : BasePlugin, IPluginConfig<PluginConfig>, IS2FModule
12+
{
13+
public required IFramework Framework { get; set; }
14+
15+
public required PluginConfig Config { get; set; } = new PluginConfig();
16+
17+
public readonly ChatService ChatService;
18+
19+
public ChatServicePlugin
20+
(
21+
ChatService chatService
22+
)
23+
{
24+
this.ChatService = chatService;
25+
}
26+
27+
public void OnConfigParsed(PluginConfig config)
28+
{
29+
this.Config = config;
30+
}
31+
32+
public override void OnAllPluginsLoaded(bool hotReload)
33+
{
34+
using (ModuleLoader loader = new ModuleLoader())
35+
{
36+
loader.Attach(this, hotReload);
37+
}
38+
}
39+
40+
public void OnCoreReady(IFramework framework, bool hotReload)
41+
{
42+
(this.Framework = framework).Services.RegisterService<ChatService>(this.ChatService, true);
43+
}
44+
45+
public override void Unload(bool hotReload)
46+
{
47+
this.Framework?.Services.RemoveService<ChatService>();
48+
}
49+
}
50+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Source2Framework
2+
{
3+
using CounterStrikeSharp.API.Core;
4+
5+
public sealed partial class PluginConfig : BasePluginConfig
6+
{
7+
public string ChatPrefix { get; set; } = "{default}「{lightblue}S2F{default}」{lime} »{default}";
8+
}
9+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace Source2Framework
2+
{
3+
using CounterStrikeSharp.API.Core;
4+
5+
public sealed partial class ChatServicePlugin : BasePlugin
6+
{
7+
public override string ModuleName => "Source2Framework Chat Service";
8+
9+
public override string ModuleDescription => "Exposing functionality for chat";
10+
11+
public override string ModuleAuthor => "Nexd @ Eternar";
12+
13+
public override string ModuleVersion => "1.0.0 " +
14+
#if RELEASE
15+
"(release)";
16+
#else
17+
"(debug)";
18+
#endif
19+
}
20+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Source2Framework
2+
{
3+
using CounterStrikeSharp.API.Core;
4+
5+
using Source2Framework.Services.Chat;
6+
7+
public class PluginServices : IPluginServiceCollection<ChatServicePlugin>
8+
{
9+
public void ConfigureServices(IServiceCollection serviceCollection)
10+
{
11+
serviceCollection.AddSingleton<ChatService>();
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)