Skip to content

Commit f983c2e

Browse files
committed
Add project files.
1 parent 9f81001 commit f983c2e

File tree

8 files changed

+268
-0
lines changed

8 files changed

+268
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace Source2Framework.Services.Command
2+
{
3+
using CounterStrikeSharp.API.Core;
4+
using CounterStrikeSharp.API.Core.Commands;
5+
6+
using Source2Framework.Models;
7+
8+
/// <summary>
9+
/// This service exposes the internal <see cref="CommandManager"/>.
10+
/// </summary>
11+
public interface ICommandService : ISharedService
12+
{
13+
/// <summary>
14+
/// Executes a command in the given player context. (Only registered commands can be executed)
15+
/// </summary>
16+
/// <param name="player">Target player</param>
17+
/// <param name="command">Command</param>
18+
public void ExecuteRegisteredCommand(CCSPlayerController player, string command);
19+
20+
/// <summary>
21+
/// Whether the given command is registered or not.
22+
/// </summary>
23+
/// <param name="command">Command</param>
24+
/// <returns><see langword="true"/> if registered, <see langword="false"/> otherwise.</returns>
25+
public bool IsCommandRegistered(string command);
26+
27+
/// <summary>
28+
/// Get the <see cref="CommandDefinition"/> instance for the given command.
29+
/// </summary>
30+
/// <param name="command">Command</param>
31+
/// <returns><see cref="CommandDefinition"/> instance for the given command if exists, otherwise <see langword="null"/></returns>
32+
public CommandDefinition? GetCommandDefinition(string command);
33+
34+
/// <summary>
35+
/// Gets every <see cref="CommandDefinition"/> instance for the given command.
36+
/// </summary>
37+
/// <param name="command">Command</param>
38+
/// <returns>A <see cref="IList{CommandDefinition}"/> of <see cref="CommandDefinition"/>s for the given command if exists, otherwise <see langword="null"/></returns>
39+
public IList<CommandDefinition>? GetCommandDefinitions(string command);
40+
}
41+
}
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.7,)" />
12+
</ItemGroup>
13+
14+
</Project>
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.CommandService", "Source2Framework.CommandService\Source2Framework.CommandService.csproj", "{75EA3CB9-C60B-442A-BBB6-3E7B20435EC8}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Source2Framework.CommandService.API", "Source2Framework.CommandService.API\Source2Framework.CommandService.API.csproj", "{47E23A29-47A9-412A-9B4B-5B6FA786C511}"
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+
{75EA3CB9-C60B-442A-BBB6-3E7B20435EC8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{75EA3CB9-C60B-442A-BBB6-3E7B20435EC8}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{75EA3CB9-C60B-442A-BBB6-3E7B20435EC8}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{75EA3CB9-C60B-442A-BBB6-3E7B20435EC8}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{47E23A29-47A9-412A-9B4B-5B6FA786C511}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{47E23A29-47A9-412A-9B4B-5B6FA786C511}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{47E23A29-47A9-412A-9B4B-5B6FA786C511}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{47E23A29-47A9-412A-9B4B-5B6FA786C511}.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 = {4749EC06-E729-4942-8DC8-5D131B2E3FB9}
30+
EndGlobalSection
31+
EndGlobal
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
namespace Source2Framework.Services.Command
2+
{
3+
using CounterStrikeSharp.API.Core;
4+
using CounterStrikeSharp.API.Core.Commands;
5+
using CounterStrikeSharp.API.Core.Plugin;
6+
7+
using Source2Framework.Models;
8+
9+
/// <summary>
10+
/// This service exposes the internal <see cref="CounterStrikeSharp.API.Core.Commands.CommandManager"/>.
11+
/// </summary>
12+
public sealed partial class CommandService : SharedService<CommandService>, ICommandService
13+
{
14+
public required CommandManager CommandManager { get; set; }
15+
16+
public required Dictionary<string, IList<CommandDefinition>> CommandDefinitions { get; set; }
17+
18+
public CommandService(ILogger<CommandService> logger, IPluginContext pluginContext) : base(logger, pluginContext)
19+
{ }
20+
21+
public override void Initialize(bool hotReload)
22+
{
23+
CommandManager? commandManager = Reflection.GetFieldValue<CommandManager, PluginCommandManagerDecorator>(this.Plugin.CommandManager as PluginCommandManagerDecorator, "_inner");
24+
25+
if (commandManager == null)
26+
{
27+
throw new NullReferenceException("CommandManager is null");
28+
}
29+
30+
this.CommandManager = commandManager;
31+
32+
Dictionary<string, IList<CommandDefinition>>? commandDefinitions = Reflection.GetFieldValue<Dictionary<string, IList<CommandDefinition>>, CommandManager>(commandManager, "_commandDefinitions");
33+
34+
if (commandDefinitions == null)
35+
{
36+
throw new Exception("Unable to get command definitions");
37+
}
38+
39+
this.CommandDefinitions = commandDefinitions;
40+
}
41+
42+
/// <inheritdoc/>
43+
public void ExecuteRegisteredCommand(CCSPlayerController player, string command)
44+
{
45+
if (!this.IsCommandRegistered(command))
46+
{
47+
throw new Exception($"Unknown command '{command}'");
48+
}
49+
50+
player.ExecuteClientCommand($"css_{command}");
51+
}
52+
53+
/// <inheritdoc/>
54+
public bool IsCommandRegistered(string command)
55+
{
56+
return this.CommandDefinitions.ContainsKey(command);
57+
}
58+
59+
/// <inheritdoc/>
60+
public CommandDefinition? GetCommandDefinition(string command)
61+
{
62+
return this.GetCommandDefinitions(command)?.FirstOrDefault();
63+
}
64+
65+
/// <inheritdoc/>
66+
public IList<CommandDefinition>? GetCommandDefinitions(string command)
67+
{
68+
if (this.CommandDefinitions.TryGetValue(command, out IList<CommandDefinition>? commandDefinition))
69+
{
70+
return commandDefinition;
71+
}
72+
73+
return null;
74+
}
75+
}
76+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.Command;
9+
10+
[MinimumApiVersion(213)]
11+
public sealed partial class CommandServicePlugin : BasePlugin, IS2FModule
12+
{
13+
public required IFramework Framework { get; set; }
14+
15+
public readonly CommandService CommandService;
16+
17+
public CommandServicePlugin
18+
(
19+
CommandService commandService
20+
)
21+
{
22+
this.CommandService = commandService;
23+
}
24+
25+
public override void OnAllPluginsLoaded(bool hotReload)
26+
{
27+
using (ModuleLoader loader = new ModuleLoader())
28+
{
29+
loader.Attach(this, hotReload);
30+
}
31+
}
32+
33+
public void OnCoreReady(IFramework framework, bool hotReload)
34+
{
35+
(this.Framework = framework).Services.RegisterService<CommandService>(this.CommandService, true);
36+
}
37+
38+
public override void Unload(bool hotReload)
39+
{
40+
this.Framework?.Services.RemoveService<CommandService>();
41+
}
42+
}
43+
}
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 CommandServicePlugin : BasePlugin
6+
{
7+
public override string ModuleName => "Source2Framework Command Service";
8+
9+
public override string ModuleDescription => "Exposing functionality for CSS commands";
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.Command;
6+
7+
public class PluginServices : IPluginServiceCollection<CommandServicePlugin>
8+
{
9+
public void ConfigureServices(IServiceCollection serviceCollection)
10+
{
11+
serviceCollection.AddSingleton<CommandService>();
12+
}
13+
}
14+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
<Compile Remove="Command\Models\**" />
11+
<EmbeddedResource Remove="Command\Models\**" />
12+
<None Remove="Command\Models\**" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.213" />
17+
<PackageReference Include="Source2Framework.SDK" Version="[1.0.7,)" />
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<ProjectReference Include="..\Source2Framework.CommandService.API\Source2Framework.CommandService.API.csproj" />
22+
</ItemGroup>
23+
24+
<ItemGroup>
25+
<Using Include="Microsoft.Extensions.DependencyInjection" />
26+
<Using Include="Microsoft.Extensions.Logging" />
27+
</ItemGroup>
28+
29+
</Project>

0 commit comments

Comments
 (0)