Compare commits

...

15 Commits
v32 ... v1.0.55

Author SHA1 Message Date
Boink
0f72631eb0 Update CommitSuicide offset for 17-11-2023 update (#98) 2023-11-18 09:43:06 +10:00
Nexd
75fcf21fb7 feat: managed coreconfig implementation (#79) 2023-11-17 17:50:59 +10:00
Nexd
0ddf6bcdfa fix: new signature for CBaseModelEntity_SetModel (#84) 2023-11-15 15:18:03 +10:00
Roflmuffin
98661cd069 fix: public and silent triggers (finally) 2023-11-14 22:29:59 +10:00
Roflmuffin
86a5699b40 feat: re-add global command listener 2023-11-14 21:15:06 +10:00
Roflmuffin
414710d05c hotfix: wrap vfunc creation in try catch to prevent all vfuncs from erroring 2023-11-13 21:40:33 +10:00
Michael Wilson
b09c2b62c8 Improved Command Handling (#76) 2023-11-13 20:59:46 +10:00
Roflmuffin
31760518ed ci: fighting with the machines 2023-11-13 20:27:52 +10:00
Roflmuffin
6a160bcc3d ci: remove build number from PR checks 2023-11-13 20:25:49 +10:00
Roflmuffin
9c8e9db56e ci: set fallback build number for PRs 2023-11-13 20:23:46 +10:00
Roflmuffin
e2e0eab87d ci: run main pipeline skipping publish on PR 2023-11-13 20:19:37 +10:00
Nexd
43292bb1d2 feat: CBaseModelEntity_SetModel (#72) 2023-11-13 09:10:49 +10:00
Nexd
12c54cd4fc hotfix: deserializer couldn't call setter (#70) 2023-11-12 22:39:15 +10:00
Michael Wilson
e155a70873 Cross platform builds (#69) 2023-11-12 15:43:51 +10:00
Nexd
69d9b5d2c8 feat: Provide configuration standard for plugins (#67) 2023-11-12 14:25:06 +10:00
38 changed files with 888 additions and 720 deletions

View File

@@ -5,94 +5,193 @@ on:
paths-ignore:
- 'docs/**'
branches: [ "main" ]
pull_request:
branches: [ "main" ]
env:
BUILD_TYPE: Release
jobs:
build:
permissions:
contents: write
packages: write
runs-on: ubuntu-latest
container:
image: registry.gitlab.steamos.cloud/steamrt/sniper/sdk:latest
build_windows:
runs-on: windows-latest
steps:
- name: Prepare env
shell: bash
run: echo "GITHUB_SHA_SHORT=${GITHUB_SHA::7}" >> $GITHUB_ENV
- name: Setup protobuf
shell: bash
run: sudo apt-get update && sudo apt install -y protobuf-compiler
- name: Visual Studio environment
shell: cmd
run: |
:: See https://github.com/microsoft/vswhere/wiki/Find-VC
for /f "usebackq delims=*" %%i in (`vswhere -latest -property installationPath`) do (
call "%%i"\Common7\Tools\vsdevcmd.bat -arch=x64 -host_arch=x64
)
:: Loop over all environment variables and make them global.
for /f "delims== tokens=1,2" %%a in ('set') do (
echo>>"%GITHUB_ENV%" %%a=%%b
)
- uses: actions/checkout@v3
with:
submodules: 'recursive'
- name: Generate build number
id: buildnumber
uses: onyxmueller/build-tag-number@v1
with:
token: ${{secrets.github_token}}
- uses: actions/setup-dotnet@v3
with:
dotnet-version: '7.0.x'
- run: |
dotnet publish -c Release /p:Version=1.0.${{ env.BUILD_NUMBER }} managed/CounterStrikeSharp.API
dotnet pack -c Release /p:Version=1.0.${{ env.BUILD_NUMBER }} managed/CounterStrikeSharp.API
- name: Configure CMake
run: cmake -B build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
- name: Build
# Build your program with the given configuration
run: cmake --build build --config ${{env.BUILD_TYPE}}
run: |
mkdir -p build
cd build
cmake -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} ..
cmake --build . --config ${{env.BUILD_TYPE}} -- /m:16
- name: Clean build directory
run: |
mkdir -p build/addons/counterstrikesharp/bin/win64
mv build/${{env.BUILD_TYPE}}/*.dll build/addons/counterstrikesharp/bin/win64
mkdir build/output/
mv build/addons build/output
- uses: actions/upload-artifact@v3
with:
name: counterstrikesharp-build-windows-${{ env.GITHUB_SHA_SHORT }}
path: build/output/
build_linux:
runs-on: ubuntu-latest
# Could not figure out how to run in a container only on some matrix paths, so I've split it out into its own build.
container:
image: registry.gitlab.steamos.cloud/steamrt/sniper/sdk:latest
steps:
- name: Prepare env
shell: bash
run: echo "GITHUB_SHA_SHORT=${GITHUB_SHA::7}" >> $GITHUB_ENV
- uses: actions/checkout@v3
with:
submodules: 'recursive'
- name: Build
run: |
mkdir -p build
cd build
cmake -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} ..
cmake --build . --config ${{env.BUILD_TYPE}} -- -j16
- name: Clean build directory
run: |
mkdir build/output/
mv build/addons build/output
- name: Add API to Artifacts
run: |
mkdir -p build/output/addons/counterstrikesharp/api
cp -r managed/CounterStrikeSharp.API/bin/Release/net7.0/publish/* build/output/addons/counterstrikesharp/api
- uses: actions/upload-artifact@v3
with:
name: counterstrikesharp-build-linux-${{ env.GITHUB_SHA_SHORT }}
path: build/output/
build_managed:
permissions:
contents: write
runs-on: ubuntu-latest
outputs:
buildnumber: ${{ steps.buildnumber.outputs.build_number }}
steps:
- name: Prepare env
shell: bash
run: echo "GITHUB_SHA_SHORT=${GITHUB_SHA::7}" >> $GITHUB_ENV
- name: Fallback build number
if: github.event_name == 'pull_request'
shell: bash
run: echo "BUILD_NUMBER=0" >> $GITHUB_ENV
# We don't need expensive submodules for the managed side.
- uses: actions/checkout@v3
- name: Generate build number
if: github.event_name == 'push'
id: buildnumber
uses: onyxmueller/build-tag-number@v1
with:
token: ${{secrets.github_token}}
- name: Build runtime v${{ env.BUILD_NUMBER }}
uses: actions/setup-dotnet@v3
with:
dotnet-version: '7.0.x'
- run: |
dotnet publish -c Release /p:Version=1.0.${{ env.BUILD_NUMBER }} managed/CounterStrikeSharp.API
dotnet pack -c Release /p:Version=1.0.${{ env.BUILD_NUMBER }} managed/CounterStrikeSharp.API
- uses: actions/upload-artifact@v3
with:
name: counterstrikesharp-build-${{ env.BUILD_NUMBER }}-${{ env.GITHUB_SHA_SHORT }}
path: build/output/
name: counterstrikesharp-build-api-${{ env.GITHUB_SHA_SHORT }}
path: managed/CounterStrikeSharp.API/bin/Release
- name: Zip CounterStrikeSharp Build
run: (cd build/output && zip -qq -r ../../counterstrikesharp-build-${{ env.BUILD_NUMBER }}-${{ env.GITHUB_SHA_SHORT }}.zip *)
publish:
if: github.event_name == 'push'
permissions:
contents: write
needs: [ "build_linux", "build_windows", "build_managed" ]
runs-on: ubuntu-latest
steps:
- name: Prepare env
shell: bash
run: echo "GITHUB_SHA_SHORT=${GITHUB_SHA::7}" >> $GITHUB_ENV
- uses: actions/download-artifact@v3
with:
name: counterstrikesharp-build-windows-${{ env.GITHUB_SHA_SHORT }}
path: build/windows
- uses: actions/download-artifact@v3
with:
name: counterstrikesharp-build-linux-${{ env.GITHUB_SHA_SHORT }}
path: build/linux
- uses: actions/download-artifact@v3
with:
name: counterstrikesharp-build-api-${{ env.GITHUB_SHA_SHORT }}
path: build/api
# TODO: This stuff should really be in a matrix
- name: Add API to Artifacts
run: |
mkdir -p build/linux/addons/counterstrikesharp/api
mkdir -p build/windows/addons/counterstrikesharp/api
cp -r build/api/net7.0/publish/* build/linux/addons/counterstrikesharp/api
cp -r build/api/net7.0/publish/* build/windows/addons/counterstrikesharp/api
- name: Zip Builds
run: |
(cd build/linux && zip -qq -r ../../counterstrikesharp-build-${{ needs.build_managed.outputs.buildnumber }}-linux-${{ env.GITHUB_SHA_SHORT }}.zip *)
(cd build/windows && zip -qq -r ../../counterstrikesharp-build-${{ needs.build_managed.outputs.buildnumber }}-windows-${{ env.GITHUB_SHA_SHORT }}.zip *)
- name: Add dotnet runtime
run: |
mkdir -p build/output/addons/counterstrikesharp/dotnet
mkdir -p build/linux/addons/counterstrikesharp/dotnet
curl -s -L https://download.visualstudio.microsoft.com/download/pr/dc2c0a53-85a8-4fda-a283-fa28adb5fbe2/8ccade5bc400a5bb40cd9240f003b45c/aspnetcore-runtime-7.0.11-linux-x64.tar.gz \
| tar xvz -C build/output/addons/counterstrikesharp/dotnet
mv build/output/addons/counterstrikesharp/dotnet/shared/Microsoft.NETCore.App/7.0.11/* build/output/addons/counterstrikesharp/dotnet/shared/Microsoft.NETCore.App/
| tar xvz -C build/linux/addons/counterstrikesharp/dotnet
mv build/linux/addons/counterstrikesharp/dotnet/shared/Microsoft.NETCore.App/7.0.11/* build/linux/addons/counterstrikesharp/dotnet/shared/Microsoft.NETCore.App/
mkdir -p build/windows/addons/counterstrikesharp/dotnet
curl -s -L https://download.visualstudio.microsoft.com/download/pr/a99861c8-2e00-4587-aaef-60366ca77307/a44ceec2c5d34165ae881600f52edc43/aspnetcore-runtime-7.0.11-win-x64.zip -o dotnet.zip
unzip -qq dotnet.zip -d build/windows/addons/counterstrikesharp/dotnet
- name: Zip CounterStrikeSharp Runtime Build
run: (cd build/output && zip -qq -r ../../counterstrikesharp-with-runtime-build-${{ env.BUILD_NUMBER }}-${{ env.GITHUB_SHA_SHORT }}.zip *)
- uses: actions/upload-artifact@v3
with:
name: counterstrikesharp-with-runtime-build-${{ env.BUILD_NUMBER }}-${{ env.GITHUB_SHA_SHORT }}
path: build/output/
- name: Zip Builds
run: |
(cd build/linux && zip -qq -r ../../counterstrikesharp-with-runtime-build-${{ needs.build_managed.outputs.buildnumber }}-linux-${{ env.GITHUB_SHA_SHORT }}.zip *)
(cd build/windows && zip -qq -r ../../counterstrikesharp-with-runtime-build-${{ needs.build_managed.outputs.buildnumber }}-windows-${{ env.GITHUB_SHA_SHORT }}.zip *)
- name: Release
uses: softprops/action-gh-release@v1
with:
tag_name: v${{ env.BUILD_NUMBER }}
tag_name: v${{ needs.build_managed.outputs.buildnumber }}
files: |
counterstrikesharp-build-${{ env.BUILD_NUMBER }}-${{ env.GITHUB_SHA_SHORT }}.zip
counterstrikesharp-with-runtime-build-${{ env.BUILD_NUMBER }}-${{ env.GITHUB_SHA_SHORT }}.zip
counterstrikesharp-build-${{ needs.build_managed.outputs.buildnumber }}-windows-${{ env.GITHUB_SHA_SHORT }}.zip
counterstrikesharp-with-runtime-build-${{ needs.build_managed.outputs.buildnumber }}-windows-${{ env.GITHUB_SHA_SHORT }}.zip
counterstrikesharp-build-${{ needs.build_managed.outputs.buildnumber }}-linux-${{ env.GITHUB_SHA_SHORT }}.zip
counterstrikesharp-with-runtime-build-${{ needs.build_managed.outputs.buildnumber }}-linux-${{ env.GITHUB_SHA_SHORT }}.zip
- name: Publish NuGet package
run: |
dotnet nuget push managed/CounterStrikeSharp.API/bin/Release/CounterStrikeSharp.API.1.0.${{ env.BUILD_NUMBER }}.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json --skip-duplicate
dotnet nuget push managed/CounterStrikeSharp.API/bin/Release/CounterStrikeSharp.API.1.0.${{ env.BUILD_NUMBER }}.snupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json --skip-duplicate
dotnet nuget push build/api/CounterStrikeSharp.API.1.0.${{ needs.build_managed.outputs.buildnumber }}.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json --skip-duplicate
dotnet nuget push build/api/CounterStrikeSharp.API.1.0.${{ needs.build_managed.outputs.buildnumber }}.snupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json --skip-duplicate

View File

@@ -1,53 +0,0 @@
name: Build
on:
pull_request:
branches: [ "main" ]
env:
BUILD_TYPE: Release
jobs:
build:
runs-on: ubuntu-latest
container:
image: registry.gitlab.steamos.cloud/steamrt/sniper/sdk:latest
permissions:
pull-requests: read
steps:
- uses: actions/checkout@v3
with:
submodules: 'recursive'
- uses: dorny/paths-filter@v2
id: changes
with:
filters: |
csharp:
- managed/**/*
- src/scripting/natives/**/*
cpp:
- src/**/*
- uses: actions/setup-dotnet@v3
if: steps.changes.outputs.csharp == 'true'
with:
dotnet-version: '7.0.x'
- if: steps.changes.outputs.csharp == 'true'
run: dotnet build -c Release managed/CounterStrikeSharp.API
- name: Setup protobuf
shell: bash
if: steps.changes.outputs.cpp == 'true'
run: sudo apt-get update && sudo apt install -y protobuf-compiler
- name: Configure CMake
if: steps.changes.outputs.cpp == 'true'
run: cmake -B build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
- name: Build
# Build your program with the given configuration
if: steps.changes.outputs.cpp == 'true'
run: cmake --build build --config ${{env.BUILD_TYPE}}

View File

@@ -1,64 +0,0 @@
name: Build
on:
push:
paths-ignore:
- 'docs/**'
branches: [ "win32" ]
env:
BUILD_TYPE: Release
jobs:
matrix:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
steps:
- name: Prepare env
shell: bash
run: echo "GITHUB_SHA_SHORT=${GITHUB_SHA::7}" >> $GITHUB_ENV
- name: Visual Studio environment
if: runner.os == 'Windows'
shell: cmd
run: |
:: See https://github.com/microsoft/vswhere/wiki/Find-VC
for /f "usebackq delims=*" %%i in (`vswhere -latest -property installationPath`) do (
call "%%i"\Common7\Tools\vsdevcmd.bat -arch=x64 -host_arch=x64
)
:: Loop over all environment variables and make them global.
for /f "delims== tokens=1,2" %%a in ('set') do (
echo>>"%GITHUB_ENV%" %%a=%%b
)
- uses: actions/checkout@v3
with:
submodules: 'recursive'
- uses: actions/setup-dotnet@v3
with:
dotnet-version: '7.0.x'
- name: Install Protoc
uses: arduino/setup-protoc@v2
- name: Build Linux
if: runner.os == 'Linux'
run: |
cd ${{github.workspace}}
mkdir -p build
cd build
cmake -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} ..
cmake --build . --config ${{env.BUILD_TYPE}} -- -j16
- name: Build Windows
if: runner.os == 'Windows'
run: |
cd ${{github.workspace}}
mkdir -p build
cd build
cmake -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} ..
cmake --build . --config ${{env.BUILD_TYPE}} -- /m:16

View File

@@ -29,6 +29,8 @@ SET(SOURCE_FILES
src/core/utils.h
src/core/globals.h
src/core/globals.cpp
src/core/coreconfig.h
src/core/coreconfig.cpp
src/core/gameconfig.h
src/core/gameconfig.cpp
src/core/log.h
@@ -75,8 +77,6 @@ SET(SOURCE_FILES
src/core/managers/entity_manager.h
src/core/managers/chat_manager.cpp
src/core/managers/chat_manager.h
src/core/managers/client_command_manager.cpp
src/core/managers/client_command_manager.h
src/core/managers/server_manager.cpp
src/core/managers/server_manager.h
src/scripting/natives/natives_server.cpp
@@ -86,7 +86,7 @@ SET(SOURCE_FILES
if (LINUX)
# memoverride.cpp is not usable on CMake Windows, cuz CMake default link libraries (seems) always link ucrt.lib
set(SOURCE_FILES
set(SOURCE_FILES
${SOURCE_FILES}
libraries/hl2sdk-cs2/public/tier0/memoverride.cpp
)

View File

@@ -1,5 +1,5 @@
{
"PublicChatTrigger": "!",
"SilentChatTrigger": "/",
"PublicChatTrigger": [ "!" ],
"SilentChatTrigger": [ "/" ],
"FollowCS2ServerGuidelines": true
}

View File

@@ -0,0 +1,4 @@
Place your plugin configurations here.
TestPlugin/TestPlugin.json
AnotherPlugin/AnotherPlugin.json

View File

@@ -47,10 +47,17 @@
"linux": "\\x55\\x48\\x89\\xE5\\x41\\x57\\x49\\x89\\xFF\\x41\\x56\\x41\\x55\\x41\\x54\\x4D\\x89\\xC4"
}
},
"CBaseModelEntity_SetModel": {
"signatures": {
"library": "server",
"windows": "\\x48\\x89\\x5C\\x24\\x10\\x48\\x89\\x7C\\x24\\x20\\x55\\x48\\x8B\\xEC\\x48\\x83\\xEC\\x50\\x48\\x8B\\xF9\\x4C\\x8B\\xC2\\x48\\x8B\\x0D\\x69\\x07\\xD9\\x00",
"linux": "\\x55\\x48\\x89\\xF2\\x48\\x89\\xE5\\x41\\x54\\x49\\x89\\xFC\\x48\\x8D\\x7D\\xE0\\x48\\x83\\xEC\\x18\\x48\\x8D\\x05\\x15\\xD4\\xBF\\x00"
}
},
"CBasePlayerPawn_CommitSuicide": {
"offsets": {
"windows": 355,
"linux": 355
"windows": 356,
"linux": 356
}
},
"CBaseEntity_Teleport": {

View File

@@ -30,7 +30,7 @@ namespace CounterStrikeSharp.API.Core
}
}
public static IntPtr AddCommand(string name, string description, bool serveronly, int flags, InputArgument callback){
public static void AddCommand(string name, string description, bool serveronly, int flags, InputArgument callback){
lock (ScriptContext.GlobalScriptContext.Lock) {
ScriptContext.GlobalScriptContext.Reset();
ScriptContext.GlobalScriptContext.Push(name);
@@ -41,7 +41,6 @@ namespace CounterStrikeSharp.API.Core
ScriptContext.GlobalScriptContext.SetIdentifier(0x807C6B9C);
ScriptContext.GlobalScriptContext.Invoke();
ScriptContext.GlobalScriptContext.CheckErrors();
return (IntPtr)ScriptContext.GlobalScriptContext.GetResult(typeof(IntPtr));
}
}

View File

@@ -30,6 +30,8 @@ using CounterStrikeSharp.API.Modules.Events;
using CounterStrikeSharp.API.Modules.Entities;
using CounterStrikeSharp.API.Modules.Listeners;
using CounterStrikeSharp.API.Modules.Timers;
using McMaster.NETCore.Plugins;
using CounterStrikeSharp.API.Modules.Config;
namespace CounterStrikeSharp.API.Core
{
@@ -207,14 +209,10 @@ namespace CounterStrikeSharp.API.Core
{
var wrappedHandler = new Func<int, IntPtr, HookResult>((i, ptr) =>
{
if (i == -1)
{
return HookResult.Continue;
}
var caller = (i != -1) ? new CCSPlayerController(NativeAPI.GetEntityFromIndex(i + 1)) : null;
var entity = new CCSPlayerController(NativeAPI.GetEntityFromIndex(i + 1));
var command = new CommandInfo(ptr, entity);
return handler.Invoke(entity.IsValid ? entity : null, command);
var command = new CommandInfo(ptr, caller);
return handler.Invoke(caller, command);
});
var subscriber = new CallbackSubscriber(handler, wrappedHandler, () => { RemoveCommandListener(name, handler, mode); });
@@ -333,6 +331,31 @@ namespace CounterStrikeSharp.API.Core
this.RegisterConsoleCommandAttributeHandlers(instance);
}
public void InitializeConfig(object instance, Type pluginType)
{
Type[] interfaces = pluginType.GetInterfaces();
Func<Type, bool> predicate = (i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPluginConfig<>));
// if the plugin has set a configuration type (implements IPluginConfig<>)
if (interfaces.Any(predicate))
{
// IPluginConfig<>
Type @interface = interfaces.Where(predicate).FirstOrDefault()!;
// custom config type passed as generic
Type genericType = @interface!.GetGenericArguments().First();
var config = typeof(ConfigManager)
.GetMethod("Load")!
.MakeGenericMethod(genericType)
.Invoke(null, new object[] { Path.GetFileName(ModuleDirectory) }) as IBasePluginConfig;
// we KNOW that we can do this "safely"
pluginType.GetRuntimeMethod("OnConfigParsed", new Type[] { genericType })
.Invoke(instance, new object[] { config });
}
}
/// <summary>
/// Registers all game event handlers that are decorated with the `[GameEventHandler]` attribute.
/// </summary>

View File

@@ -0,0 +1,32 @@
/*
* This file is part of CounterStrikeSharp.
* CounterStrikeSharp is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CounterStrikeSharp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CounterStrikeSharp. If not, see <https://www.gnu.org/licenses/>. *
*/
using System.Text.Json.Serialization;
namespace CounterStrikeSharp.API.Core
{
public interface IBasePluginConfig
{
[JsonPropertyName("ConfigVersion")]
int Version { get; set; }
}
public class BasePluginConfig : IBasePluginConfig
{
[JsonPropertyName("ConfigVersion")]
public virtual int Version { get; set; } = 1;
}
}

View File

@@ -23,6 +23,7 @@ using System.Text.Json.Serialization;
using CounterStrikeSharp.API.Modules.Utils;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Commands;
using System.Collections.Generic;
namespace CounterStrikeSharp.API.Core
{
@@ -31,11 +32,11 @@ namespace CounterStrikeSharp.API.Core
/// </summary>
internal sealed partial class CoreConfigData
{
[JsonPropertyName("PublicChatTrigger")] public string PublicChatTrigger { get; internal set; } = "!";
[JsonPropertyName("SilentChatTrigger")] public string SilentChatTrigger { get; internal set; } = "/";
[JsonPropertyName("PublicChatTrigger")] public IEnumerable<string> PublicChatTrigger { get; set; } = new HashSet<string>() { "!" };
[JsonPropertyName("FollowCS2ServerGuidelines")] public bool FollowCS2ServerGuidelines { get; internal set; } = true;
[JsonPropertyName("SilentChatTrigger")] public IEnumerable<string> SilentChatTrigger { get; set; } = new HashSet<string>() { "/" };
[JsonPropertyName("FollowCS2ServerGuidelines")] public bool FollowCS2ServerGuidelines { get; set; } = true;
}
/// <summary>
@@ -46,12 +47,12 @@ namespace CounterStrikeSharp.API.Core
/// <summary>
/// List of characters to use for public chat triggers.
/// </summary>
public static string PublicChatTrigger => _coreConfig.PublicChatTrigger;
public static IEnumerable<string> PublicChatTrigger => _coreConfig.PublicChatTrigger;
/// <summary>
/// List of characters to use for silent chat triggers.
/// </summary>
public static string SilentChatTrigger => _coreConfig.SilentChatTrigger;
public static IEnumerable<string> SilentChatTrigger => _coreConfig.SilentChatTrigger;
/// <summary>
/// <para>
@@ -62,7 +63,9 @@ namespace CounterStrikeSharp.API.Core
///
/// <para>
/// Enabling this option will block plugins from using functionality that is known to cause this.
///
/// Note that this does NOT guarantee that you cannot
///
/// receive a ban.
/// </para>
///
@@ -101,12 +104,12 @@ namespace CounterStrikeSharp.API.Core
try
{
var data = JsonSerializer.Deserialize<CoreConfigData>(File.ReadAllText(coreConfigPath), new JsonSerializerOptions() { ReadCommentHandling = JsonCommentHandling.Skip });
if (data != null)
{
_coreConfig = data;
}
Console.WriteLine($"Loaded core configuration");
}
catch (Exception ex)

View File

@@ -0,0 +1,28 @@
/*
* This file is part of CounterStrikeSharp.
* CounterStrikeSharp is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CounterStrikeSharp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CounterStrikeSharp. If not, see <https://www.gnu.org/licenses/>. *
*/
namespace CounterStrikeSharp.API.Core
{
/// <summary>
/// An interface that describes a plugin configuration.
/// </summary>
public interface IPluginConfig<T> where T: IBasePluginConfig, new()
{
T Config { get; set; }
public void OnConfigParsed(T config);
}
}

View File

@@ -0,0 +1,27 @@
/*
* This file is part of CounterStrikeSharp.
* CounterStrikeSharp is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CounterStrikeSharp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CounterStrikeSharp. If not, see <https://www.gnu.org/licenses/>. *
*/
using CounterStrikeSharp.API.Modules.Memory;
namespace CounterStrikeSharp.API.Core;
public partial class CBaseModelEntity
{
public void SetModel(string model)
{
VirtualFunctions.SetModel(Handle, model);
}
}

View File

@@ -26,7 +26,7 @@ public partial class CCSPlayerController
public void PrintToConsole(string message)
{
NativeAPI.PrintToConsole((int)EntityIndex.Value.Value, message);
NativeAPI.PrintToConsole((int)EntityIndex.Value.Value, $"{message}\n\0");
}
public void PrintToChat(string message)

View File

@@ -20,6 +20,7 @@ using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using CounterStrikeSharp.API.Core.Attributes;
using CounterStrikeSharp.API.Modules.Config;
using CounterStrikeSharp.API.Modules.Events;
using McMaster.NETCore.Plugins;
@@ -103,6 +104,7 @@ namespace CounterStrikeSharp.API.Core
_plugin = (BasePlugin)Activator.CreateInstance(pluginType)!;
_plugin.ModulePath = _path;
_plugin.RegisterAllAttributes(_plugin);
_plugin.InitializeConfig(_plugin, pluginType);
_plugin.Load(hotReload);
Console.WriteLine($"Finished loading plugin: {Name}");

View File

@@ -0,0 +1,79 @@
/*
* This file is part of CounterStrikeSharp.
* CounterStrikeSharp is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CounterStrikeSharp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CounterStrikeSharp. If not, see <https://www.gnu.org/licenses/>. *
*/
using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Text.Json;
using CounterStrikeSharp.API.Core;
namespace CounterStrikeSharp.API.Modules.Config
{
public static class ConfigManager
{
private static readonly DirectoryInfo? _rootDir;
private static readonly string _pluginConfigsFolderPath;
static ConfigManager()
{
_rootDir = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.Parent;
_pluginConfigsFolderPath = Path.Combine(_rootDir.FullName, "configs", "plugins");
}
public static T Load<T>(string pluginName) where T : IBasePluginConfig, new()
{
string directoryPath = Path.Combine(_pluginConfigsFolderPath, pluginName);
string configPath = Path.Combine(directoryPath, $"{pluginName}.json");
T config = (T)Activator.CreateInstance(typeof(T))!;
if (!File.Exists(configPath))
{
try
{
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
StringBuilder builder = new StringBuilder();
builder.Append($"// This configuration was automatically generated by CounterStrikeSharp for plugin '{pluginName}', at {DateTimeOffset.Now:yyyy/MM/dd hh:mm:ss}\n");
builder.Append(JsonSerializer.Serialize<T>(config, new JsonSerializerOptions { WriteIndented = true }));
File.WriteAllText(configPath, builder.ToString());
return config;
}
catch (Exception ex)
{
Console.WriteLine($"Failed to generate configuration file for {pluginName}: {ex}");
}
}
try
{
config = JsonSerializer.Deserialize<T>(File.ReadAllText(configPath), new JsonSerializerOptions() { ReadCommentHandling = JsonCommentHandling.Skip })!;
}
catch (Exception ex)
{
Console.WriteLine($"Failed to parse configuration '{pluginName}': {ex}");
}
return config;
}
}
}

View File

@@ -31,9 +31,15 @@ public partial class VirtualFunction
{
if (!_createdFunctions.TryGetValue(signature, out var function))
{
function = NativeAPI.CreateVirtualFunctionBySignature(IntPtr.Zero, Addresses.ServerPath, signature,
argumentTypes.Count(), (int)returnType, arguments);
_createdFunctions[signature] = function;
try
{
function = NativeAPI.CreateVirtualFunctionBySignature(IntPtr.Zero, Addresses.ServerPath, signature,
argumentTypes.Count(), (int)returnType, arguments);
_createdFunctions[signature] = function;
}
catch (Exception)
{
}
}
return function;

View File

@@ -24,4 +24,7 @@ public static class VirtualFunctions
// void(*UTIL_Remove)(CEntityInstance*);
public static Action<IntPtr> UTIL_Remove = VirtualFunction.CreateVoid<IntPtr>(GameData.GetSignature("UTIL_Remove"));
// void(*CBaseModelEntity_SetModel)(CBaseModelEntity*, const char*);
public static Action<IntPtr, string> SetModel = VirtualFunction.CreateVoid<IntPtr, string>(GameData.GetSignature("CBaseModelEntity_SetModel"));
}

View File

@@ -18,6 +18,8 @@ using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text.Json.Serialization;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes;
@@ -32,8 +34,17 @@ using CounterStrikeSharp.API.Modules.Utils;
namespace TestPlugin
{
[MinimumApiVersion(1)]
public class SamplePlugin : BasePlugin
public class SampleConfig : BasePluginConfig
{
[JsonPropertyName("IsPluginEnabled")]
public bool IsPluginEnabled { get; set; } = true;
[JsonPropertyName("LogPrefix")]
public string LogPrefix { get; set; } = "CSSharp";
}
[MinimumApiVersion(33)]
public class SamplePlugin : BasePlugin, IPluginConfig<SampleConfig>
{
public override string ModuleName => "Sample Plugin";
public override string ModuleVersion => "v1.0.0";
@@ -42,8 +53,24 @@ namespace TestPlugin
public override string ModuleDescription => "A playground of features used for testing";
public SampleConfig Config { get; set; }
// This method is called right before `Load` is called
public void OnConfigParsed(SampleConfig config)
{
// Save config instance
Config = config;
}
public override void Load(bool hotReload)
{
// Basic usage of the configuration system
if (!Config.IsPluginEnabled)
{
Console.WriteLine($"{Config.LogPrefix} {ModuleName} is disabled");
return;
}
Console.WriteLine(
$"Test Plugin has been loaded, and the hot reload flag was {hotReload}, path is {ModulePath}");
@@ -327,7 +354,7 @@ namespace TestPlugin
[ConsoleCommand("cssharp_attribute", "This is a custom attribute event")]
public void OnCommand(CCSPlayerController? player, CommandInfo command)
{
Log("cssharp_attribute called!");
command.ReplyToCommand("cssharp_attribute called", true);
}
[ConsoleCommand("css_changelevel", "Changes map")]

78
src/core/coreconfig.cpp Normal file
View File

@@ -0,0 +1,78 @@
/*
* This file is part of CounterStrikeSharp.
* CounterStrikeSharp is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CounterStrikeSharp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CounterStrikeSharp. If not, see <https://www.gnu.org/licenses/>. *
*/
#include <fstream>
#include "core/log.h"
#include "core/coreconfig.h"
namespace counterstrikesharp {
CCoreConfig::CCoreConfig(const std::string& path) { m_sPath = path; }
CCoreConfig::~CCoreConfig() = default;
bool CCoreConfig::Init(char* conf_error, int conf_error_size)
{
std::ifstream ifs(m_sPath);
if (!ifs) {
V_snprintf(conf_error, conf_error_size, "CoreConfig file not found.");
return false;
}
m_json = json::parse(ifs);
try {
PublicChatTrigger = m_json["PublicChatTrigger"];
SilentChatTrigger = m_json["SilentChatTrigger"];
FollowCS2ServerGuidelines = m_json["FollowCS2ServerGuidelines"];
} catch (const std::exception& ex) {
V_snprintf(conf_error, conf_error_size, "Failed to parse CoreConfig file: %s", ex.what());
return false;
}
return true;
}
const std::string CCoreConfig::GetPath() const
{
return m_sPath;
}
bool CCoreConfig::IsTriggerInternal(std::vector<std::string> triggers, const std::string& message, std::string*& prefix) const
{
for (std::string& trigger : triggers)
{
if (message.rfind(trigger, 0) == 0)
{
prefix = &trigger;
return true;
}
}
return false;
}
bool CCoreConfig::IsSilentChatTrigger(const std::string& message, std::string*& prefix) const
{
return IsTriggerInternal(SilentChatTrigger, message, prefix);
}
bool CCoreConfig::IsPublicChatTrigger(const std::string& message, std::string*& prefix) const
{
return IsTriggerInternal(PublicChatTrigger, message, prefix);
}
} // namespace counterstrikesharp

50
src/core/coreconfig.h Normal file
View File

@@ -0,0 +1,50 @@
/*
* This file is part of CounterStrikeSharp.
* CounterStrikeSharp is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CounterStrikeSharp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CounterStrikeSharp. If not, see <https://www.gnu.org/licenses/>. *
*/
#pragma once
#include "core/globals.h"
#include <string>
#include <nlohmann/json.hpp>
namespace counterstrikesharp {
class CCoreConfig
{
public:
std::vector<std::string> PublicChatTrigger = { std::string("!") };
std::vector<std::string> SilentChatTrigger = { std::string("/") };
bool FollowCS2ServerGuidelines = true;
using json = nlohmann::json;
CCoreConfig(const std::string& path);
~CCoreConfig();
bool Init(char* conf_error, int conf_error_size);
const std::string GetPath() const;
bool IsSilentChatTrigger(const std::string& message, std::string*& prefix) const;
bool IsPublicChatTrigger(const std::string& message, std::string*& prefix) const;
private:
bool IsTriggerInternal(std::vector<std::string> triggers, const std::string& message, std::string*& prefix) const;
private:
std::string m_sPath;
json m_json;
};
} // namespace counterstrikesharp

View File

@@ -23,6 +23,8 @@
#include "const.h"
#include "utils/virtual.h"
#include <string>
#include <vector>
#include <stdint.h>
#include <type_traits>
@@ -54,6 +56,45 @@ inline uint64_t hash_64_fnv1a_const(const char *str, const uint64_t value = val_
}
namespace schema {
static std::vector<std::string> CS2BadList = {
"m_bIsValveDS",
"m_bIsQuestEligible",
"m_iItemDefinitionIndex", // in unmanaged this cannot be set.
"m_iEntityLevel",
"m_iItemIDHigh",
"m_iItemIDLow",
"m_iAccountID",
"m_iEntityQuality",
"m_bInitialized",
"m_szCustomName",
"m_iAttributeDefinitionIndex",
"m_iRawValue32",
"m_iRawInitialValue32",
"m_flValue", // MNetworkAlias "m_iRawValue32"
"m_flInitialValue", // MNetworkAlias "m_iRawInitialValue32"
"m_bSetBonus",
"m_nRefundableCurrency",
"m_OriginalOwnerXuidLow",
"m_OriginalOwnerXuidHigh",
"m_nFallbackPaintKit",
"m_nFallbackSeed",
"m_flFallbackWear",
"m_nFallbackStatTrak",
"m_iCompetitiveWins",
"m_iCompetitiveRanking",
"m_iCompetitiveRankType",
"m_iCompetitiveRankingPredicted_Win",
"m_iCompetitiveRankingPredicted_Loss",
"m_iCompetitiveRankingPredicted_Tie",
"m_nActiveCoinRank",
"m_nMusicID",
};
int16_t FindChainOffset(const char *className);
SchemaKey GetOffset(const char *className, uint32_t classKey, const char *memberName, uint32_t memberKey);
} // namespace schema

View File

@@ -19,7 +19,6 @@
#include "memory_module.h"
#include "interfaces/cs2_interfaces.h"
#include "core/managers/entity_manager.h"
#include "core/managers/client_command_manager.h"
#include "core/managers/server_manager.h"
#include <public/game/server/iplayerinfo.h>
#include <public/entity2/entitysystem.h>
@@ -66,6 +65,7 @@ SourceHook::Impl::CSourceHookImpl source_hook_impl;
SourceHook::ISourceHook *source_hook = &source_hook_impl;
ISmmAPI *ismm = nullptr;
CGameEntitySystem* entitySystem = nullptr;
CCoreConfig* coreConfig = nullptr;
CGameConfig* gameConfig = nullptr;
// Custom Managers
@@ -76,7 +76,6 @@ TimerSystem timerSystem;
ConCommandManager conCommandManager;
EntityManager entityManager;
ChatManager chatManager;
ClientCommandManager clientCommandManager;
ServerManager serverManager;
void Initialize() {

View File

@@ -48,8 +48,8 @@ class ChatCommands;
class HookManager;
class EntityManager;
class ChatManager;
class ClientCommandManager;
class ServerManager;
class CCoreConfig;
class CGameConfig;
namespace globals {
@@ -92,7 +92,6 @@ extern EntityManager entityManager;
extern TimerSystem timerSystem;
extern ChatCommands chatCommands;
extern ChatManager chatManager;
extern ClientCommandManager clientCommandManager;
extern ServerManager serverManager;
extern HookManager hookManager;
@@ -101,6 +100,7 @@ extern int source_hook_pluginid;
extern IGameEventSystem *gameEventSystem;
extern CounterStrikeSharpMMPlugin *mmPlugin;
extern ISmmAPI *ismm;
extern CCoreConfig* coreConfig;
extern CGameConfig* gameConfig;
void Initialize();

View File

@@ -24,6 +24,7 @@
#include <public/eiface.h>
#include "core/memory.h"
#include "core/log.h"
#include "core/coreconfig.h"
#include "core/gameconfig.h"
#include <funchook.h>
@@ -39,8 +40,7 @@ ChatManager::~ChatManager() {}
void ChatManager::OnAllInitialized()
{
m_pHostSay = reinterpret_cast<HostSay>(
modules::server->FindSignature(globals::gameConfig->GetSignature("Host_Say"))
);
modules::server->FindSignature(globals::gameConfig->GetSignature("Host_Say")));
if (m_pHostSay == nullptr) {
CSSHARP_CORE_ERROR("Failed to find signature for \'Host_Say\'");
@@ -57,71 +57,72 @@ void ChatManager::OnShutdown() {}
void DetourHostSay(CBaseEntity* pController, CCommand& args, bool teamonly, int unk1,
const char* unk2)
{
CCommand newArgs;
newArgs.Tokenize(args.Arg(1));
if (*args[1] == '/' || *args[1] == '!') {
globals::chatManager.OnSayCommandPost(pController, newArgs);
return;
}
m_pHostSay(pController, args, teamonly, unk1, unk2);
if (pController) {
auto pEvent = globals::gameEventManager->CreateEvent("player_chat", true);
if (pEvent) {
pEvent->SetBool("teamonly", teamonly);
pEvent->SetInt("userid", pController->GetEntityIndex().Get());
pEvent->SetInt("userid", pController->GetEntityIndex().Get() - 1);
pEvent->SetString("text", args[1]);
globals::gameEventManager->FireEvent(pEvent, true);
}
}
std::string* prefix;
bool bSilent = globals::coreConfig->IsSilentChatTrigger(args[1], prefix);
bool bCommand = bSilent || globals::coreConfig->IsPublicChatTrigger(args[1], prefix);
if (!bSilent) {
m_pHostSay(pController, args, teamonly, unk1, unk2);
}
if (bCommand)
{
char *pszMessage = (char *)(args.ArgS() + prefix->length() + 1);
// Trailing slashes are only removed if Host_Say has been called.
if (bSilent)
pszMessage[V_strlen(pszMessage) - 1] = 0;
CCommand args;
args.Tokenize(pszMessage);
auto prefixedPhrase = std::string("css_") + args.Arg(0);
auto bValidWithPrefix = globals::conCommandManager.IsValidValveCommand(prefixedPhrase.c_str());
if (bValidWithPrefix) {
// Re-tokenize with a `css_` prefix if we have found that its a valid command.
args.Tokenize(("css_" + std::string(pszMessage)).c_str());
}
globals::chatManager.OnSayCommandPost(pController, args);
}
}
bool ChatManager::OnSayCommandPre(CBaseEntity* pController, CCommand& command) {
return false;
}
bool ChatManager::OnSayCommandPre(CBaseEntity* pController, CCommand& command) { return false; }
bool ChatManager::OnSayCommandPost(CBaseEntity* pController, CCommand& command)
void ChatManager::OnSayCommandPost(CBaseEntity* pController, CCommand& command)
{
const char* args = command.ArgS();
auto commandStr = command.Arg(0);
return InternalDispatch(pController, commandStr + 1, command);
return InternalDispatch(pController, commandStr, command);
}
bool ChatManager::InternalDispatch(CBaseEntity* pPlayerController, const char* szTriggerPhase,
void ChatManager::InternalDispatch(CBaseEntity* pPlayerController, const char* szTriggerPhase,
CCommand& fullCommand)
{
auto ppArgV = new const char*[fullCommand.ArgC()];
ppArgV[0] = strdup(szTriggerPhase);
for (int i = 1; i < fullCommand.ArgC(); i++) {
ppArgV[i] = fullCommand.Arg(i);
}
auto prefixedPhrase = std::string("css_") + szTriggerPhase;
auto command = globals::conCommandManager.FindCommand(prefixedPhrase.c_str());
if (command) {
ppArgV[0] = prefixedPhrase.c_str();
}
CCommand commandCopy(fullCommand.ArgC(), ppArgV);
if (pPlayerController == nullptr) {
auto result = globals::conCommandManager.InternalDispatch(CPlayerSlot(-1), &commandCopy);
delete[] ppArgV;
return result;
globals::conCommandManager.ExecuteCommandCallbacks(
fullCommand.Arg(0), CCommandContext(CommandTarget_t::CT_NO_TARGET, CPlayerSlot(-1)),
fullCommand, HookMode::Pre);
return;
}
auto index = pPlayerController->GetEntityIndex().Get();
auto slot = CPlayerSlot(index - 1);
auto result = globals::conCommandManager.InternalDispatch(slot, &commandCopy);
delete[] ppArgV;
return result;
globals::conCommandManager.ExecuteCommandCallbacks(
fullCommand.Arg(0), CCommandContext(CommandTarget_t::CT_NO_TARGET, slot), fullCommand,
HookMode::Pre);
}
} // namespace counterstrikesharp

View File

@@ -53,10 +53,9 @@ class ChatManager : public GlobalClass
void OnShutdown() override;
bool OnSayCommandPre(CBaseEntity* pController, CCommand& args);
bool OnSayCommandPost(CBaseEntity* pController, CCommand& args);
void OnSayCommandPost(CBaseEntity* pController, CCommand& args);
private:
bool InternalDispatch(CBaseEntity* pPlayerController, const char* szTriggerPhrase,
void InternalDispatch(CBaseEntity* pPlayerController, const char* szTriggerPhrase,
CCommand& pFullCommand);
std::vector<ChatCommandInfo*> m_cmd_list;

View File

@@ -1,147 +0,0 @@
#include "core/managers/client_command_manager.h"
#include <public/eiface.h>
#include <algorithm>
#include "scripting/callback_manager.h"
#include "core/log.h"
namespace counterstrikesharp {
ClientCommandManager::ClientCommandManager() {}
ClientCommandManager::~ClientCommandManager() {}
void ClientCommandManager::OnAllInitialized()
{
m_global_cmd.callback_pre = globals::callbackManager.CreateCallback("OnClientCommandGlobalPre");
m_global_cmd.callback_post =
globals::callbackManager.CreateCallback("OnClientCommandGlobalPost");
}
void ClientCommandManager::OnShutdown() {}
bool ClientCommandManager::DispatchClientCommand(CPlayerSlot slot, const char* cmd,
const CCommand* args)
{
CSSHARP_CORE_TRACE("Dispatch client command {}", cmd);
auto* p_info = m_cmd_lookup[cmd];
bool result = false;
if (m_global_cmd.callback_pre->GetFunctionCount() > 0) {
m_global_cmd.callback_pre->ScriptContext().Reset();
m_global_cmd.callback_pre->ScriptContext().Push(slot.Get());
m_global_cmd.callback_pre->ScriptContext().Push(args);
for (auto fnMethodToCall : m_global_cmd.callback_pre->GetFunctions()) {
if (!fnMethodToCall)
continue;
fnMethodToCall(&m_global_cmd.callback_pre->ScriptContextStruct());
auto hookResult = m_global_cmd.callback_pre->ScriptContext().GetResult<HookResult>();
CSSHARP_CORE_TRACE("Received hook result from command callback {}:{}", cmd, hookResult);
if (hookResult >= HookResult::Stop) {
return true;
} else if (hookResult >= HookResult::Handled) {
result = true;
}
}
}
if (p_info && p_info->callback_pre) {
p_info->callback_pre->ScriptContext().Reset();
p_info->callback_pre->ScriptContext().Push(slot.Get());
p_info->callback_pre->ScriptContext().Push(args);
for (auto fnMethodToCall : p_info->callback_pre->GetFunctions()) {
if (!fnMethodToCall)
continue;
fnMethodToCall(&p_info->callback_pre->ScriptContextStruct());
auto hookResult = p_info->callback_pre->ScriptContext().GetResult<HookResult>();
CSSHARP_CORE_TRACE("Received hook result from command callback {}:{}", cmd, hookResult);
if (hookResult >= HookResult::Stop) {
return true;
} else if (hookResult >= HookResult::Handled) {
result = true;
}
}
}
if (m_global_cmd.callback_post->GetFunctionCount() > 0) {
m_global_cmd.callback_post->ScriptContext().Reset();
m_global_cmd.callback_post->ScriptContext().Push(slot.Get());
m_global_cmd.callback_post->ScriptContext().Push(args);
m_global_cmd.callback_post->Execute();
}
if (result && p_info && p_info->callback_post) {
p_info->callback_post->ScriptContext().Reset();
p_info->callback_post->ScriptContext().Push(slot.Get());
p_info->callback_post->ScriptContext().Push(args);
p_info->callback_post->Execute();
}
return result;
}
void ClientCommandManager::AddCommandListener(const char* cmd, CallbackT callback, bool bPost)
{
// Handle global command listeners that listen for every ClientCommand.
if (cmd == nullptr) {
if (bPost) {
m_global_cmd.callback_post->AddListener(callback);
return;
}
m_global_cmd.callback_pre->AddListener(callback);
return;
}
auto* p_info = m_cmd_lookup[std::string(cmd)];
if (!p_info) {
p_info = new ClientCommandInfo();
p_info->command = cmd;
p_info->callback_pre = globals::callbackManager.CreateCallback(cmd);
p_info->callback_post = globals::callbackManager.CreateCallback(cmd);
m_cmd_list.push_back(p_info);
m_cmd_lookup[cmd] = p_info;
}
if (bPost) {
p_info->callback_post->AddListener(callback);
} else {
p_info->callback_pre->AddListener(callback);
}
}
void ClientCommandManager::RemoveCommandListener(const char* cmd, CallbackT callback, bool bPost)
{
if (cmd == nullptr) {
if (bPost) {
m_global_cmd.callback_post->RemoveListener(callback);
return;
}
m_global_cmd.callback_pre->RemoveListener(callback);
return;
}
auto* p_info = m_cmd_lookup[std::string(cmd)];
if (!p_info) {
return;
}
if (bPost) {
p_info->callback_post->RemoveListener(callback);
} else {
p_info->callback_pre->RemoveListener(callback);
}
}
} // namespace counterstrikesharp

View File

@@ -1,45 +0,0 @@
#pragma once
#include <map>
#include <vector>
#include "core/globals.h"
#include "core/global_listener.h"
#include "scripting/script_engine.h"
#include <string>
#include "playerslot.h"
namespace counterstrikesharp {
class ScriptCallback;
class ClientCommandInfo {
friend class ClientCommandManager;
public:
ClientCommandInfo() {}
private:
std::string command;
ScriptCallback* callback_pre;
ScriptCallback* callback_post;
};
class ClientCommandManager : public GlobalClass {
public:
ClientCommandManager();
~ClientCommandManager();
void OnAllInitialized() override;
void OnShutdown() override;
bool DispatchClientCommand(CPlayerSlot slot, const char* cmd, const CCommand* args);
void AddCommandListener(const char* cmd, CallbackT callback, bool bPost);
void RemoveCommandListener(const char* cmd, CallbackT callback, bool bPost);
private:
std::vector<ClientCommandInfo*> m_cmd_list;
std::map<std::string, ClientCommandInfo*> m_cmd_lookup;
ClientCommandInfo m_global_cmd;
};
} // namespace counterstrikesharp

View File

@@ -157,307 +157,248 @@ CON_COMMAND(dump_schema, "dump schema symbols")
output << std::setw(2) << j << std::endl;
}
SH_DECL_HOOK2_void(ConCommandHandle, Dispatch, SH_NOATTRIB, false, const CCommandContext&,
const CCommand&);
SH_DECL_HOOK3_void(ICvar, DispatchConCommand, SH_NOATTRIB, 0, ConCommandHandle,
const CCommandContext&, const CCommand&);
void ConCommandInfo::HookChange(CallbackT cb, bool post)
ConCommandInfo::ConCommandInfo()
{
if (post) {
this->callback_post->AddListener(cb);
} else {
this->callback_pre->AddListener(cb);
}
callback_pre = globals::callbackManager.CreateCallback("");
callback_post = globals::callbackManager.CreateCallback("");
}
ConCommandInfo::~ConCommandInfo()
{
globals::callbackManager.ReleaseCallback(callback_pre);
globals::callbackManager.ReleaseCallback(callback_post);
}
ConCommandInfo::ConCommandInfo(bool bNoCallbacks) {
}
void ConCommandInfo::UnhookChange(CallbackT cb, bool post)
{
if (post) {
if (this->callback_post && this->callback_post->GetFunctionCount()) {
callback_post->RemoveListener(cb);
}
} else {
if (this->callback_pre && this->callback_pre->GetFunctionCount()) {
callback_pre->RemoveListener(cb);
}
}
}
ConCommandManager::ConCommandManager() : last_command_client(-1) {}
ConCommandManager::ConCommandManager() {}
ConCommandManager::~ConCommandManager() {}
void ConCommandManager::OnAllInitialized() {}
void ConCommandManager::OnAllInitialized()
{
SH_ADD_HOOK_MEMFUNC(ICvar, DispatchConCommand, globals::cvars, this,
&ConCommandManager::Hook_DispatchConCommand, false);
SH_ADD_HOOK_MEMFUNC(ICvar, DispatchConCommand, globals::cvars, this,
&ConCommandManager::Hook_DispatchConCommand_Post, true);
void ConCommandManager::OnShutdown() {}
m_global_cmd.callback_pre = globals::callbackManager.CreateCallback("OnClientCommandGlobalPre");
m_global_cmd.callback_post =
globals::callbackManager.CreateCallback("OnClientCommandGlobalPost");
}
void ConCommandManager::OnShutdown()
{
SH_REMOVE_HOOK_MEMFUNC(ICvar, DispatchConCommand, globals::cvars, this,
&ConCommandManager::Hook_DispatchConCommand, false);
SH_REMOVE_HOOK_MEMFUNC(ICvar, DispatchConCommand, globals::cvars, this,
&ConCommandManager::Hook_DispatchConCommand_Post, true);
globals::callbackManager.ReleaseCallback(m_global_cmd.callback_pre);
globals::callbackManager.ReleaseCallback(m_global_cmd.callback_post);
}
void CommandCallback(const CCommandContext& context, const CCommand& command)
{
bool rval = globals::conCommandManager.InternalDispatch(context.GetPlayerSlot(), &command);
if (rval) {
RETURN_META(MRES_SUPERCEDE);
}
// This is handled by the global hook
RETURN_META(MRES_SUPERCEDE);
}
void CommandCallback_Post(const CCommandContext& context, const CCommand& command)
void ConCommandManager::AddCommandListener(const char* name, CallbackT callback, HookMode mode)
{
bool rval = globals::conCommandManager.InternalDispatch_Post(context.GetPlayerSlot(), &command);
if (rval) {
RETURN_META(MRES_SUPERCEDE);
}
}
ConCommandInfo* ConCommandManager::AddOrFindCommand(const char* name, const char* description,
bool server_only, int flags)
{
ConCommandInfo* p_info = m_cmd_lookup[std::string(name)];
if (!p_info) {
CSSHARP_CORE_TRACE("[ConCommandManager] Could not find command in existing lookup {}",
name);
// auto found = std::find_if(m_cmd_list.begin(), m_cmd_list.end(),
// [&](ConCommandInfo* info) {
// return V_strcasecmp(info->command->GetName(), name) == 0;
// });
// if (found != m_cmd_list.end()) {
// return *found;
// }
p_info = new ConCommandInfo();
ConCommandHandle existingCommand = globals::cvars->FindCommand(name);
ConCommandRefAbstract pointerConCommand;
p_info->p_cmd = pointerConCommand;
if (!existingCommand.IsValid()) {
if (!description) {
description = "";
}
CSSHARP_CORE_TRACE("[ConCommandManager] Creating new command {}", name);
char* new_name = strdup(name);
char* new_desc = strdup(description);
CSSHARP_CORE_TRACE("[ConCommandManager] Creating new command {}, {}, {}, {}, {}",
(void*)&pointerConCommand, new_name, (void*)CommandCallback,
new_desc, flags);
auto conCommand =
new ConCommand(&pointerConCommand, new_name, CommandCallback, new_desc, flags);
CSSHARP_CORE_TRACE("[ConCommandManager] Creating callbacks for command {}", name);
p_info->command = conCommand;
p_info->callback_pre = globals::callbackManager.CreateCallback(name);
p_info->callback_post = globals::callbackManager.CreateCallback(name);
p_info->server_only = server_only;
CSSHARP_CORE_TRACE(
"[ConCommandManager] Adding hooks for command callback for command {}", name);
SH_ADD_HOOK(ConCommandHandle, Dispatch, &pointerConCommand.handle,
SH_STATIC(CommandCallback), false);
SH_ADD_HOOK(ConCommandHandle, Dispatch, &pointerConCommand.handle,
SH_STATIC(CommandCallback_Post), true);
CSSHARP_CORE_TRACE("[ConCommandManager] Adding command to internal lookup {}", name);
m_cmd_list.push_back(p_info);
m_cmd_lookup[name] = p_info;
if (name == nullptr) {
if (mode == HookMode::Pre) {
m_global_cmd.callback_pre->AddListener(callback);
} else {
p_info->callback_pre = globals::callbackManager.CreateCallback(name);
p_info->callback_post = globals::callbackManager.CreateCallback(name);
p_info->server_only = server_only;
m_global_cmd.callback_post->AddListener(callback);
}
return p_info;
return;
}
return p_info;
}
auto strName = std::string(name);
ConCommandInfo* pInfo = m_cmd_lookup[strName];
ConCommandInfo* ConCommandManager::AddCommand(const char* name, const char* description,
bool server_only, int flags, CallbackT callback)
{
ConCommandInfo* p_info = AddOrFindCommand(name, description, server_only, flags);
if (!p_info || !p_info->callback_pre) {
return nullptr;
if (!pInfo) {
pInfo = new ConCommandInfo();
m_cmd_lookup[strName] = pInfo;
ConCommandHandle hExistingCommand = globals::cvars->FindCommand(name);
if (hExistingCommand.IsValid()) {
pInfo->command = globals::cvars->GetCommand(hExistingCommand);
}
}
p_info->callback_pre->AddListener(callback);
return p_info;
if (mode == HookMode::Pre) {
pInfo->callback_pre->AddListener(callback);
} else {
pInfo->callback_post->AddListener(callback);
}
}
bool ConCommandManager::RemoveCommand(const char* name, CallbackT callback)
void ConCommandManager::RemoveCommandListener(const char* name, CallbackT callback, HookMode mode)
{
auto strName = std::string(strdup(name));
ConCommandInfo* p_info = m_cmd_lookup[strName];
if (!p_info)
if (name == nullptr) {
if (mode == HookMode::Pre) {
m_global_cmd.callback_pre->RemoveListener(callback);
} else {
m_global_cmd.callback_post->RemoveListener(callback);
}
return;
}
auto strName = std::string(name);
ConCommandInfo* pInfo = m_cmd_lookup[strName];
if (!pInfo) {
return;
}
if (mode == HookMode::Pre) {
pInfo->callback_pre->RemoveListener(callback);
} else {
pInfo->callback_post->RemoveListener(callback);
}
}
bool ConCommandManager::AddValveCommand(const char* name, const char* description, bool server_only,
int flags)
{
ConCommandHandle hExistingCommand = globals::cvars->FindCommand(name);
if (hExistingCommand.IsValid())
return false;
if (p_info->callback_pre && p_info->callback_pre->GetFunctionCount()) {
p_info->callback_pre->RemoveListener(callback);
ConCommandRefAbstract conCommandRefAbstract;
auto conCommand =
new ConCommand(&conCommandRefAbstract, strdup(name), CommandCallback, strdup(description), flags);
ConCommandInfo* pInfo = m_cmd_lookup[std::string(name)];
if (!pInfo) {
pInfo = new ConCommandInfo();
m_cmd_lookup[std::string(name)] = pInfo;
}
if (p_info->callback_post && p_info->callback_post->GetFunctionCount()) {
p_info->callback_post->RemoveListener(callback);
}
if (!p_info->callback_pre || p_info->callback_pre->GetFunctionCount() == 0) {
// It does not look like this actually removes the con command.
// You can still find with `find` command.
globals::cvars->UnregisterConCommand(p_info->p_cmd.handle);
}
pInfo->p_cmd = conCommandRefAbstract;
pInfo->command = conCommand;
pInfo->server_only = server_only;
return true;
}
ConCommandInfo* ConCommandManager::FindCommand(const char* name)
bool ConCommandManager::RemoveValveCommand(const char* name)
{
ConCommandInfo* p_info = m_cmd_lookup[std::string(name)];
auto hFoundCommand = globals::cvars->FindCommand(name);
if (p_info == nullptr) {
auto found = std::find_if(m_cmd_list.begin(), m_cmd_list.end(), [&](ConCommandInfo* info) {
return V_strcasecmp(info->command->GetName(), name) == 0;
});
if (found != m_cmd_list.end()) {
return *found;
}
ConCommandHandle p_cmd = globals::cvars->FindCommand(name);
if (!p_cmd.IsValid())
return nullptr;
p_info = new ConCommandInfo();
p_info->command = globals::cvars->GetCommand(p_cmd);
p_info->p_cmd = *p_info->command->GetRef();
p_info->callback_pre = globals::callbackManager.CreateCallback(name);
p_info->callback_post = globals::callbackManager.CreateCallback(name);
p_info->server_only = false;
m_cmd_list.push_back(p_info);
m_cmd_lookup[name] = p_info;
return p_info;
if (!hFoundCommand.IsValid()) {
return false;
}
return p_info;
globals::cvars->UnregisterConCommand(hFoundCommand);
auto pInfo = m_cmd_lookup[std::string(name)];
if (!pInfo) {
return true;
}
pInfo->command = nullptr;
return true;
}
int ConCommandManager::GetCommandClient() { return last_command_client; }
void ConCommandManager::SetCommandClient(int client) { last_command_client = client + 1; }
bool ConCommandManager::InternalDispatch(CPlayerSlot slot, const CCommand* args)
HookResult ConCommandManager::ExecuteCommandCallbacks(const char* name, const CCommandContext& ctx,
const CCommand& args, HookMode mode)
{
const char* cmd = args->Arg(0);
CSSHARP_CORE_TRACE("[ConCommandManager::ExecuteCommandCallbacks][{}]: {}",
mode == Pre ? "Pre" : "Post", name);
ConCommandInfo* pInfo = m_cmd_lookup[std::string(name)];
ConCommandInfo* p_info = m_cmd_lookup[cmd];
if (p_info == nullptr) {
if (slot.Get() == 0 && !globals::engine->IsDedicatedServer())
return false;
HookResult result = HookResult::Continue;
for (ConCommandInfo* cmdInfo : m_cmd_list) {
if ((cmdInfo != nullptr) && strcasecmp(cmdInfo->command->GetName(), cmd) == 0) {
p_info = cmdInfo;
auto globalCallback = mode == HookMode::Pre ? m_global_cmd.callback_pre : m_global_cmd.callback_post;
if (globalCallback->GetFunctionCount() > 0) {
globalCallback->ScriptContext().Reset();
globalCallback->ScriptContext().Push(ctx.GetPlayerSlot().Get());
globalCallback->ScriptContext().Push(&args);
for (auto fnMethodToCall : globalCallback->GetFunctions()) {
if (!fnMethodToCall)
continue;
fnMethodToCall(&globalCallback->ScriptContextStruct());
auto hookResult = globalCallback->ScriptContext().GetResult<HookResult>();
if (hookResult >= HookResult::Stop) {
if (mode == HookMode::Pre) {
return HookResult::Stop;
}
result = hookResult;
break;
}
if (hookResult >= HookResult::Handled) {
result = hookResult;
}
}
}
if (!p_info) {
return false;
if (!pInfo) {
return result;
}
int realClient = slot.Get();
auto pCallback = mode == HookMode::Pre ? pInfo->callback_pre : pInfo->callback_post;
bool result = false;
if (p_info->callback_pre) {
p_info->callback_pre->ScriptContext().Reset();
p_info->callback_pre->ScriptContext().SetArgument(0, realClient);
p_info->callback_pre->ScriptContext().SetArgument(1, args);
p_info->callback_pre->Execute(false);
pCallback->Reset();
pCallback->ScriptContext().Push(ctx.GetPlayerSlot().Get());
pCallback->ScriptContext().Push(&args);
result = p_info->callback_pre->ScriptContext().GetResult<bool>();
for (auto fnMethodToCall : pCallback->GetFunctions()) {
if (!fnMethodToCall)
continue;
fnMethodToCall(&pCallback->ScriptContextStruct());
auto thisResult = pCallback->ScriptContext().GetResult<HookResult>();
if (thisResult >= HookResult::Handled) {
return result;
} else if (thisResult > result) {
result = thisResult;
}
}
return result;
}
bool ConCommandManager::InternalDispatch_Post(CPlayerSlot slot, const CCommand* args)
void ConCommandManager::Hook_DispatchConCommand(ConCommandHandle cmd, const CCommandContext& ctx,
const CCommand& args)
{
const char* cmd = args->Arg(0);
const char* name = args.Arg(0);
ConCommandInfo* p_info = m_cmd_lookup[cmd];
if (p_info == nullptr) {
if (slot.Get() == 0 && !globals::engine->IsDedicatedServer())
return false;
CSSHARP_CORE_TRACE("[ConCommandManager::Hook_DispatchConCommand]: {}", name);
for (ConCommandInfo* cmdInfo : m_cmd_list) {
if ((cmdInfo != nullptr) && strcasecmp(cmdInfo->command->GetName(), cmd) == 0) {
p_info = cmdInfo;
continue;
}
}
auto result = ExecuteCommandCallbacks(name, ctx, args, HookMode::Pre);
if (result >= HookResult::Handled) {
RETURN_META(MRES_SUPERCEDE);
}
int realClient = slot.Get();
bool result = false;
if (p_info->callback_post) {
p_info->callback_post->ScriptContext().Reset();
p_info->callback_post->ScriptContext().SetArgument(0, realClient);
p_info->callback_post->ScriptContext().SetArgument(1, args);
p_info->callback_post->Execute(false);
result = p_info->callback_post->ScriptContext().GetResult<bool>();
}
return result;
}
bool ConCommandManager::DispatchClientCommand(CPlayerSlot slot, const char* cmd,
const CCommand* args)
void ConCommandManager::Hook_DispatchConCommand_Post(ConCommandHandle cmd,
const CCommandContext& ctx,
const CCommand& args)
{
ConCommandInfo* p_info = m_cmd_lookup[cmd];
if (p_info == nullptr) {
auto found =
std::find_if(m_cmd_list.begin(), m_cmd_list.end(), [&](const ConCommandInfo* info) {
return V_strcasecmp(info->command->GetName(), cmd) == 0;
});
if (found == m_cmd_list.end()) {
return false;
}
const char* name = args.Arg(0);
p_info = *found;
auto result = ExecuteCommandCallbacks(name, ctx, args, HookMode::Post);
if (result >= HookResult::Handled) {
RETURN_META(MRES_SUPERCEDE);
}
if (p_info->server_only)
return false;
bool result = false;
if (p_info->callback_pre) {
p_info->callback_pre->ScriptContext().Reset();
p_info->callback_pre->ScriptContext().Push(slot.Get());
p_info->callback_pre->ScriptContext().Push(args);
p_info->callback_pre->Execute();
result = true;
}
if (result) {
if (p_info->callback_post) {
p_info->callback_post->ScriptContext().Reset();
p_info->callback_post->ScriptContext().Push(slot.Get());
p_info->callback_post->ScriptContext().Push(args);
p_info->callback_post->Execute();
result = true;
}
}
return result;
}
bool ConCommandManager::IsValidValveCommand(const char* name) {
ConCommandHandle pCmd = globals::cvars->FindCommand(name);
return pCmd.IsValid();
}
} // namespace counterstrikesharp

View File

@@ -40,6 +40,16 @@
#include <string>
#include "playerslot.h"
struct CaseInsensitiveComparator {
bool operator()(const std::string& lhs, const std::string& rhs) const {
return std::lexicographical_compare(
lhs.begin(), lhs.end(),
rhs.begin(), rhs.end(),
[](char a, char b) { return std::tolower(a) < std::tolower(b); }
);
}
};
namespace counterstrikesharp {
class ScriptCallback;
@@ -47,7 +57,9 @@ class ConCommandInfo {
friend class ConCommandManager;
public:
ConCommandInfo() {}
ConCommandInfo();
ConCommandInfo(bool bNoCallbacks);
~ConCommandInfo();
public:
void HookChange(CallbackT callback, bool post);
@@ -64,39 +76,27 @@ private:
class ConCommandManager : public GlobalClass {
friend class ConCommandInfo;
friend void CommandCallback(const CCommand& command);
friend void CommandCallback_Post(const CCommand& command);
public:
ConCommandManager();
~ConCommandManager();
void OnAllInitialized() override;
void OnShutdown() override;
ConCommandInfo* AddOrFindCommand(const char* name,
const char* description,
bool server_only,
int flags);
bool DispatchClientCommand(CPlayerSlot slot, const char* cmd, const CCommand* args);
bool InternalDispatch(CPlayerSlot slot, const CCommand* args);
int GetCommandClient();
bool InternalDispatch_Post(CPlayerSlot slot, const CCommand* args);
public:
ConCommandInfo* AddCommand(
const char* name, const char* description, bool server_only, int flags, CallbackT callback);
bool RemoveCommand(const char* name, CallbackT callback);
ConCommandInfo* FindCommand(const char* name);
void AddCommandListener(const char* name, CallbackT callback, HookMode mode);
void RemoveCommandListener(const char* name, CallbackT callback, HookMode mode);
bool IsValidValveCommand(const char* name);
bool AddValveCommand(const char* name, const char* description, bool server_only, int flags);
bool RemoveValveCommand(const char* name);
void Hook_DispatchConCommand(ConCommandHandle cmd, const CCommandContext& ctx, const CCommand& args);
void Hook_DispatchConCommand_Post(ConCommandHandle cmd, const CCommandContext& ctx, const CCommand& args);
HookResult ExecuteCommandCallbacks(const char* name, const CCommandContext& ctx,
const CCommand& args, HookMode mode);
private:
void SetCommandClient(int client);
private:
int last_command_client;
std::vector<ConCommandInfo*> m_cmd_list;
std::map<std::string, ConCommandInfo*> m_cmd_lookup;
std::map<std::string, ConCommandInfo*, CaseInsensitiveComparator> m_cmd_lookup;
ConCommandInfo m_global_cmd = ConCommandInfo(true);
};
} // namespace counterstrikesharp

View File

@@ -30,7 +30,7 @@
*/
#include "core/managers/player_manager.h"
#include "core/managers/client_command_manager.h"
#include "core/managers/con_command_manager.h"
#include <public/eiface.h>
#include <public/inetchannelinfo.h>
@@ -287,8 +287,10 @@ void PlayerManager::OnClientCommand(CPlayerSlot slot, const CCommand& args) cons
const char* cmd = args.Arg(0);
bool response = globals::clientCommandManager.DispatchClientCommand(slot, cmd, &args);
if (response) {
auto result = globals::conCommandManager.ExecuteCommandCallbacks(
cmd, CCommandContext(CommandTarget_t::CT_NO_TARGET, slot), args, HookMode::Pre);
if (result >= HookResult::Handled) {
RETURN_META(MRES_SUPERCEDE);
}
}

View File

@@ -20,7 +20,9 @@ inline std::string GameDirectory() {
return gameDirectory;
}
inline std::string PluginDirectory() { return GameDirectory() + "/addons/counterstrikesharp"; }
inline std::string GetRootDirectory() { return GameDirectory() + "/addons/counterstrikesharp"; }
inline std::string PluginsDirectory() { return GameDirectory() + "/addons/counterstrikesharp/plugins"; }
inline std::string ConfigsDirectory() { return GameDirectory() + "/addons/counterstrikesharp/configs"; }
inline std::string GamedataDirectory() { return GameDirectory() + "/addons/counterstrikesharp/gamedata"; }
} // namespace utils

View File

@@ -18,6 +18,7 @@
#include "core/global_listener.h"
#include "core/log.h"
#include "core/coreconfig.h"
#include "core/gameconfig.h"
#include "core/timer_system.h"
#include "core/utils.h"
@@ -83,6 +84,17 @@ bool CounterStrikeSharpMMPlugin::Load(PluginId id, ISmmAPI* ismm, char* error, s
GET_V_IFACE_ANY(GetEngineFactory, globals::gameEventSystem, IGameEventSystem,
GAMEEVENTSYSTEM_INTERFACE_VERSION);
auto coreconfig_path = std::string(utils::ConfigsDirectory() + "/core.json");
globals::coreConfig = new CCoreConfig(coreconfig_path);
char coreconfig_error[255] = "";
if (!globals::coreConfig->Init(coreconfig_error, sizeof(coreconfig_error))) {
CSSHARP_CORE_ERROR("Could not read \'{}\'. Error: {}", coreconfig_path, coreconfig_error);
return false;
}
CSSHARP_CORE_INFO("CoreConfig loaded.");
auto gamedata_path = std::string(utils::GamedataDirectory() + "/gamedata.json");
globals::gameConfig = new CGameConfig(gamedata_path);
char conf_error[255] = "";

View File

@@ -96,7 +96,7 @@ void* get_export(void* h, const char* name)
// Using the nethost library, discover the location of hostfxr and get exports
bool load_hostfxr()
{
std::string base_dir = counterstrikesharp::utils::PluginDirectory();
std::string base_dir = counterstrikesharp::utils::GetRootDirectory();
namespace css = counterstrikesharp;
#if _WIN32
std::wstring buffer =
@@ -164,7 +164,7 @@ CDotNetManager::~CDotNetManager() {}
bool CDotNetManager::Initialize()
{
const std::string base_dir = counterstrikesharp::utils::PluginDirectory();
const std::string base_dir = counterstrikesharp::utils::GetRootDirectory();
CSSHARP_CORE_INFO("Loading .NET runtime...");
@@ -184,7 +184,7 @@ bool CDotNetManager::Initialize()
std::string((base_dir + "/api/CounterStrikeSharp.API.runtimeconfig.json").c_str());
CSSHARP_CORE_INFO("Loading CSS API, Runtime Config: {}", wide_str);
#endif
const auto load_assembly_and_get_function_pointer = get_dotnet_load_assembly(wide_str.c_str());
if (load_assembly_and_get_function_pointer == nullptr) {
CSSHARP_CORE_ERROR("Failed to load CSS API.");

View File

@@ -16,7 +16,6 @@
#include <eiface.h>
#include "core/managers/client_command_manager.h"
#include "scripting/autonative.h"
#include "scripting/callback_manager.h"
#include "core/managers/con_command_manager.h"
@@ -26,7 +25,7 @@
namespace counterstrikesharp {
static ConCommandInfo* AddCommand(ScriptContext& script_context)
static void AddCommand(ScriptContext& script_context)
{
auto name = script_context.GetArgument<const char*>(0);
auto description = script_context.GetArgument<const char*>(1);
@@ -37,7 +36,8 @@ static ConCommandInfo* AddCommand(ScriptContext& script_context)
CSSHARP_CORE_TRACE("Adding command {}, {}, {}, {}, {}", name, description, server_only, flags,
(void*)callback);
return globals::conCommandManager.AddCommand(name, description, server_only, flags, callback);
globals::conCommandManager.AddValveCommand(name, description, server_only, flags);
globals::conCommandManager.AddCommandListener(name, callback, HookMode::Pre);
}
static void RemoveCommand(ScriptContext& script_context)
@@ -45,7 +45,8 @@ static void RemoveCommand(ScriptContext& script_context)
auto name = script_context.GetArgument<const char*>(0);
auto callback = script_context.GetArgument<CallbackT>(1);
globals::conCommandManager.RemoveCommand(name, callback);
globals::conCommandManager.RemoveCommandListener(name, callback, HookMode::Pre);
globals::conCommandManager.RemoveValveCommand(name);
}
static void AddCommandListener(ScriptContext& script_context)
@@ -54,7 +55,7 @@ static void AddCommandListener(ScriptContext& script_context)
auto callback = script_context.GetArgument<CallbackT>(1);
auto post = script_context.GetArgument<bool>(2);
globals::clientCommandManager.AddCommandListener(name, callback, post);
globals::conCommandManager.AddCommandListener(name, callback, post ? HookMode::Post : HookMode::Pre);
}
static void RemoveCommandListener(ScriptContext& script_context)
@@ -63,7 +64,7 @@ static void RemoveCommandListener(ScriptContext& script_context)
auto callback = script_context.GetArgument<CallbackT>(1);
auto post = script_context.GetArgument<bool>(2);
globals::clientCommandManager.RemoveCommandListener(name, callback, post);
globals::conCommandManager.RemoveCommandListener(name, callback, post ? HookMode::Post : HookMode::Pre);
}
static int CommandGetArgCount(ScriptContext& script_context)

View File

@@ -1,4 +1,4 @@
ADD_COMMAND: name:string,description:string,serverOnly:bool,flags:int,callback:callback -> pointer
ADD_COMMAND: name:string,description:string,serverOnly:bool,flags:int,callback:callback -> void
REMOVE_COMMAND: name:string,callback:callback -> void
ADD_COMMAND_LISTENER: cmd:string, callback:callback, post:bool -> void
REMOVE_COMMAND_LISTENER: cmd:string, callback:callback, post:bool -> void

View File

@@ -23,6 +23,7 @@
#include "core/log.h"
#include "schema.h"
#include "core/function.h"
#include "core/coreconfig.h"
namespace counterstrikesharp {
@@ -122,6 +123,12 @@ void SetSchemaValueByName(ScriptContext& script_context)
auto dataType = script_context.GetArgument<DataType_t>(1);
auto className = script_context.GetArgument<const char*>(2);
auto memberName = script_context.GetArgument<const char*>(3);
if (globals::coreConfig->FollowCS2ServerGuidelines && std::find(schema::CS2BadList.begin(), schema::CS2BadList.end(), memberName) != schema::CS2BadList.end()) {
CSSHARP_CORE_ERROR("Cannot set '{}::{}' with \"FollowCS2ServerGuidelines\" option enabled.", className, memberName);
return;
}
auto classKey = hash_32_fnv1a_const(className);
auto memberKey = hash_32_fnv1a_const(memberName);

View File

@@ -40,6 +40,11 @@ enum HookResult {
Stop = 4,
};
enum HookMode {
Pre = 0,
Post = 1,
};
inline uint32_t hash_string(const char *string) {
unsigned long result = 5381;