Compare commits

...

12 Commits

Author SHA1 Message Date
samyyc
54ad6c0b79 feat: Expose Metamod MetaFactory to NativeAPI (#801) 2025-03-17 11:45:38 +10:00
Michael Wilson
7c2cc8a7f6 [no ci] chore: fix deprecated CI steps (#759) 2025-02-05 13:23:24 +10:00
qstage
e99d27ca30 fix: errors when reloading auto generated config (#754) 2025-01-30 21:08:44 +10:00
dependabot[bot]
44d3c51bc7 [no ci] chore(deps): bump libraries/Protobufs from b46090a to 157162d (#753)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-01-30 11:22:36 +10:00
Joakim
f72e6d5daf feat: add ReplicateConVar (#751) 2025-01-27 22:10:14 +00:00
samyyc
c8bccb07e0 Remove the characters limit for CenterHtmlMenu (#747) 2025-01-23 13:06:06 +00:00
qstage
2c0640700a fix: ArgumentOutOfRangeException when calling GetArgTargetResult (#745) 2025-01-23 22:53:51 +10:00
Ian Lucas
0f71e1aebe feat: handle quotes for FakeConVar<string> (#743) 2025-01-20 12:09:44 +10:00
dependabot[bot]
ac38ec249b chore(deps): bump libraries/hl2sdk-cs2 from a658a0f to a26ca82 (#739)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-01-15 23:45:00 +10:00
Roflmuffin
cff24f4321 Revert "Make NetworkedVector support Vector and QAngle (#728)"
This reverts commit bd1105d752.
2025-01-14 00:18:56 +10:00
Michael Wilson
f05cc5e043 fix: NetworkedVector throwing error 2025-01-09 11:07:40 +10:00
Yarukon
bd1105d752 Make NetworkedVector support Vector and QAngle (#728) 2025-01-09 09:14:56 +10:00
22 changed files with 174 additions and 75 deletions

View File

@@ -74,7 +74,7 @@ jobs:
mkdir build/output/
mv build/addons build/output
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
with:
name: counterstrikesharp-build-windows-${{ env.GITHUB_SHA_SHORT }}
path: build/output/
@@ -115,7 +115,7 @@ jobs:
mkdir build/output/
mv build/addons build/output
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
with:
name: counterstrikesharp-build-linux-${{ env.GITHUB_SHA_SHORT }}
path: build/output/
@@ -154,7 +154,7 @@ jobs:
run: dotnet test --logger trx --results-directory "TestResults-${{ env.GITHUB_SHA_SHORT }}" managed/CounterStrikeSharp.API.Tests/CounterStrikeSharp.API.Tests.csproj
- name: Upload dotnet test results
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: test-results-${{ env.GITHUB_SHA_SHORT }}
path: TestResults-${{ env.GITHUB_SHA_SHORT }}
@@ -165,7 +165,7 @@ jobs:
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
- uses: actions/upload-artifact@v4
with:
name: counterstrikesharp-build-api-${{ env.GITHUB_SHA_SHORT }}
path: managed/CounterStrikeSharp.API/bin/Release
@@ -181,17 +181,17 @@ jobs:
shell: bash
run: echo "GITHUB_SHA_SHORT=${GITHUB_SHA::7}" >> $GITHUB_ENV
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
with:
name: counterstrikesharp-build-windows-${{ env.GITHUB_SHA_SHORT }}
path: build/windows
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
with:
name: counterstrikesharp-build-linux-${{ env.GITHUB_SHA_SHORT }}
path: build/linux
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
with:
name: counterstrikesharp-build-api-${{ env.GITHUB_SHA_SHORT }}
path: build/api

View File

@@ -83,6 +83,7 @@ set(SOURCE_FILES
src/scripting/natives/natives_schema.cpp
src/scripting/natives/natives_entities.cpp
src/scripting/natives/natives_voice.cpp
src/scripting/natives/natives_metamod.cpp
src/core/managers/entity_manager.cpp
src/core/managers/entity_manager.h
src/core/managers/chat_manager.cpp

View File

@@ -206,6 +206,18 @@ namespace CounterStrikeSharp.API.Core
}
}
public static void ReplicateConvar(int clientslot, string convarname, string convarvalue){
lock (ScriptContext.GlobalScriptContext.Lock) {
ScriptContext.GlobalScriptContext.Reset();
ScriptContext.GlobalScriptContext.Push(clientslot);
ScriptContext.GlobalScriptContext.Push(convarname);
ScriptContext.GlobalScriptContext.Push(convarvalue);
ScriptContext.GlobalScriptContext.SetIdentifier(0xC8728BEC);
ScriptContext.GlobalScriptContext.Invoke();
ScriptContext.GlobalScriptContext.CheckErrors();
}
}
public static T DynamicHookGetReturn<T>(IntPtr hook, int datatype){
lock (ScriptContext.GlobalScriptContext.Lock) {
ScriptContext.GlobalScriptContext.Reset();
@@ -1160,6 +1172,17 @@ namespace CounterStrikeSharp.API.Core
}
}
public static IntPtr MetaFactory(string interfacename){
lock (ScriptContext.GlobalScriptContext.Lock) {
ScriptContext.GlobalScriptContext.Reset();
ScriptContext.GlobalScriptContext.Push(interfacename);
ScriptContext.GlobalScriptContext.SetIdentifier(0x61521EF3);
ScriptContext.GlobalScriptContext.Invoke();
ScriptContext.GlobalScriptContext.CheckErrors();
return (IntPtr)ScriptContext.GlobalScriptContext.GetResult(typeof(IntPtr));
}
}
public static short GetSchemaOffset(string classname, string propname){
lock (ScriptContext.GlobalScriptContext.Lock) {
ScriptContext.GlobalScriptContext.Reset();

View File

@@ -4489,14 +4489,6 @@ namespace CounterStrikeSharp.API.Core
}
// ip:port
public string Address
{
get => Get<string>("address");
set => Set<string>("address", value);
}
public bool Bot
{

View File

@@ -170,5 +170,11 @@ namespace CounterStrikeSharp.API.Core
/// <param name="infoList">Transmit info list</param>
[ListenerName("CheckTransmit")]
public delegate void CheckTransmit([CastFrom(typeof(nint))]CCheckTransmitInfoList infoList);
/// <summary>
/// Called when all metamod plugins are loaded.
/// </summary>
[ListenerName("OnMetamodAllPluginsLoaded")]
public delegate void OnMetamodAllPluginsLoaded();
}
}

View File

@@ -347,4 +347,9 @@ public partial class CCSPlayerController
{
base.Teleport(position, angles, velocity);
}
public void ReplicateConVar(string conVar, string value)
{
NativeAPI.ReplicateConvar(Slot, conVar, value);
}
}

View File

@@ -17,7 +17,7 @@ public partial class NetworkedVector<T> : NativeObject, IReadOnlyCollection<T>
}
public unsafe uint Size => Unsafe.Read<uint>((void*)Handle);
public unsafe int Count => NativeAPI.GetNetworkVectorSize(Handle);
public T this[int index]
@@ -28,7 +28,7 @@ public partial class NetworkedVector<T> : NativeObject, IReadOnlyCollection<T>
{
throw new NotSupportedException("Networked vectors currently only support CHandle<T>");
}
return (T)Activator.CreateInstance(typeof(T), NativeAPI.GetNetworkVectorElementAt(Handle, index));
}
}
@@ -50,4 +50,4 @@ public partial class NetworkedVector<T> : NativeObject, IReadOnlyCollection<T>
{
return GetEnumerator();
}
}
}

View File

@@ -36,7 +36,7 @@ public class Target
private static bool ConstTargetType(string target, out TargetType targetType)
{
targetType = TargetType.Invalid;
if (!target.StartsWith("@"))
if (!target.StartsWith('@'))
{
return false;
}
@@ -50,13 +50,13 @@ public class Target
{
targetType = TargetType.Invalid;
slug = null!;
if (!target.StartsWith("#"))
if (!target.StartsWith('#'))
{
return false;
}
slug = target.TrimStart('#');
if (slug.StartsWith("STEAM")) targetType = TargetType.IdSteamEscaped;
if (slug.StartsWith("STEAM") && slug.Contains(':')) targetType = TargetType.IdSteamEscaped;
else if (!ulong.TryParse(slug, out _)) targetType = TargetType.ExplicitName;
else if (slug.Length == 17) targetType = TargetType.IdSteam64;
else targetType = TargetType.IdUserid;
@@ -101,10 +101,9 @@ public class Target
TargetType.GroupNotMe => player.SteamID != caller?.SteamID,
TargetType.PlayerMe => player.SteamID == caller?.SteamID,
TargetType.IdUserid => player.UserId.ToString() == Slug,
TargetType.IdSteamEscaped => ((SteamID)player.SteamID).SteamId2 == Slug,
TargetType.IdSteam64 => ((SteamID)player.SteamID).SteamId64.ToString() == Slug,
TargetType.ExplicitName => player.PlayerName.Contains(Slug, StringComparison.OrdinalIgnoreCase),
TargetType.ImplicitName => player.PlayerName.Contains(Slug, StringComparison.OrdinalIgnoreCase),
TargetType.IdSteamEscaped when player.SteamID != 0 => (SteamID)player.SteamID == (SteamID)Slug,
TargetType.IdSteam64 => player.SteamID.ToString() == Slug,
TargetType.ExplicitName or TargetType.ImplicitName => player.PlayerName.Contains(Slug, StringComparison.OrdinalIgnoreCase),
_ => false
};
}

View File

@@ -72,6 +72,16 @@ public class FakeConVar<T> where T : IComparable<T>
try
{
var argString = args.ArgString;
if (typeof(T) == typeof(string))
{
if (argString.Length >= 2 && argString.StartsWith('"') && argString.EndsWith('"'))
{
argString = argString.Substring(1, argString.Length - 2);
}
}
// TODO(dotnet8): Replace with IParsable<T>
bool success = true;
T parsedValue = default(T);
@@ -80,11 +90,11 @@ public class FakeConVar<T> where T : IComparable<T>
{
try
{
parsedValue = (T)converter.ConvertFromString(args.ArgString);
parsedValue = (T)converter.ConvertFromString(argString);
}
catch
{
success = typeof(T) == typeof(bool) && TryConvertCustomBoolean(args.ArgString, out parsedValue);
success = typeof(T) == typeof(bool) && TryConvertCustomBoolean(argString, out parsedValue);
}
}
@@ -137,4 +147,4 @@ public class FakeConVar<T> where T : IComparable<T>
_value = value;
ValueChanged?.Invoke(this, _value);
}
}
}

View File

@@ -7,7 +7,8 @@ public static class PluginConfigExtensions
{
private static readonly JsonSerializerOptions _jsonSerializerOptions = new()
{
WriteIndented = true
WriteIndented = true,
ReadCommentHandling = JsonCommentHandling.Skip
};
public static JsonSerializerOptions JsonSerializerOptions => _jsonSerializerOptions;
@@ -62,7 +63,7 @@ public static class PluginConfigExtensions
var configContent = File.ReadAllText(configPath);
var newConfig = JsonSerializer.Deserialize<T>(configContent)
var newConfig = JsonSerializer.Deserialize<T>(configContent, JsonSerializerOptions)
?? throw new JsonException($"Deserialization failed for configuration file '{configPath}'.");
foreach (var property in typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public))

View File

@@ -29,13 +29,13 @@ public class CenterHtmlMenu : BaseMenu
public string NextPageColor { get; set; } = "yellow";
public string CloseColor { get; set; } = "red";
public CenterHtmlMenu(string title, BasePlugin plugin) : base(ModifyTitle(title))
public CenterHtmlMenu(string title, BasePlugin plugin) : base(title)
{
_plugin = plugin;
}
[Obsolete("Use the constructor that takes a BasePlugin")]
public CenterHtmlMenu(string title) : base(ModifyTitle(title))
public CenterHtmlMenu(string title) : base(title)
{
}
@@ -45,40 +45,18 @@ public class CenterHtmlMenu : BaseMenu
{
throw new InvalidOperationException("This method is unsupported with the CenterHtmlMenu constructor used." +
"Please provide a BasePlugin in the constructor.");
};
};
MenuManager.OpenCenterHtmlMenu(_plugin, player, this);
}
public override ChatMenuOption AddMenuOption(string display, Action<CCSPlayerController, ChatMenuOption> onSelect,
bool disabled = false)
{
var option = new ChatMenuOption(ModifyOptionDisplay(display), disabled, onSelect);
var option = new ChatMenuOption(display, disabled, onSelect);
MenuOptions.Add(option);
return option;
}
private static string ModifyTitle(string title)
{
if (title.Length > 32)
{
Application.Instance.Logger.LogWarning("Title should not be longer than 32 characters for a CenterHtmlMenu");
return title[..32];
}
return title;
}
private static string ModifyOptionDisplay(string display)
{
if (display.Length > 26)
{
Application.Instance.Logger.LogWarning("Display should not be longer than 26 characters for a CenterHtmlMenu item");
return display[..26];
}
return display;
}
}
public class CenterHtmlMenuInstance : BaseMenuInstance

View File

@@ -247,5 +247,20 @@ namespace CounterStrikeSharp.API
entity.LastNetworkChange = Server.CurrentTime;
entity.IsSteadyState.Clear();
}
/// <summary>
/// metamod method 'MetaFactory' to get the pointer of api interface exposed by metamod plugins.
/// Returns null when the interface cannot be found.
/// </summary>
/// <param name="interfaceName">The interface name of metamod api, can be found in their api header file</param>
public static IntPtr? MetaFactory(string interfaceName)
{
IntPtr ptr = NativeAPI.MetaFactory(interfaceName);
if (ptr == 0)
{
return null;
}
return ptr;
}
}
}

View File

@@ -134,6 +134,7 @@ bool CounterStrikeSharpMMPlugin::Load(PluginId id, ISmmAPI* ismm, char* error, s
CALL_GLOBAL_LISTENER(OnAllInitialized());
on_activate_callback = globals::callbackManager.CreateCallback("OnMapStart");
on_metamod_all_plugins_loaded_callback = globals::callbackManager.CreateCallback("OnMetamodAllPluginsLoaded");
SH_ADD_HOOK_MEMFUNC(IServerGameDLL, GameFrame, globals::server, this, &CounterStrikeSharpMMPlugin::Hook_GameFrame, true);
SH_ADD_HOOK_MEMFUNC(INetworkServerService, StartupServer, globals::networkServerService, this,
@@ -182,6 +183,7 @@ bool CounterStrikeSharpMMPlugin::Unload(char* error, size_t maxlen)
&CounterStrikeSharpMMPlugin::Hook_StartupServer, true);
globals::callbackManager.ReleaseCallback(on_activate_callback);
globals::callbackManager.ReleaseCallback(on_metamod_all_plugins_loaded_callback);
return true;
}
@@ -191,6 +193,8 @@ void CounterStrikeSharpMMPlugin::AllPluginsLoaded()
/* This is where we'd do stuff that relies on the mod or other plugins
* being initialized (for example, cvars added and events registered).
*/
on_metamod_all_plugins_loaded_callback->ScriptContext().Reset();
on_metamod_all_plugins_loaded_callback->Execute();
}
void CounterStrikeSharpMMPlugin::AddTaskForNextFrame(std::function<void()>&& task)

View File

@@ -71,6 +71,7 @@ class CounterStrikeSharpMMPlugin : public ISmmPlugin, public IMetamodListener
};
static ScriptCallback* on_activate_callback;
static ScriptCallback* on_metamod_all_plugins_loaded_callback;
extern CounterStrikeSharpMMPlugin gPlugin;
PLUGIN_GLOBALVARS();

View File

@@ -0,0 +1 @@
OnMetamodAllPluginsLoaded:

View File

@@ -15,13 +15,17 @@
*/
#include <eiface.h>
#include <networksystem/inetworkmessages.h>
#include "scripting/autonative.h"
#include "scripting/callback_manager.h"
#include "core/managers/con_command_manager.h"
#include "core/managers/player_manager.h"
#include "core/recipientfilters.h"
#include "igameeventsystem.h"
#include "scripting/script_engine.h"
#include "core/log.h"
#include <networkbasetypes.pb.h>
namespace counterstrikesharp {
@@ -191,6 +195,25 @@ void SetConVarStringValue(ScriptContext& script_context)
pCvar->values = reinterpret_cast<CVValue_t**>((char*)value);
}
void ReplicateConVar(ScriptContext& script_context)
{
auto slot = script_context.GetArgument<int>(0);
auto name = script_context.GetArgument<const char*>(1);
auto value = script_context.GetArgument<const char*>(2);
INetworkMessageInternal* pNetMsg = globals::networkMessages->FindNetworkMessagePartial("SetConVar");
auto msg = pNetMsg->AllocateMessage()->ToPB<CNETMsg_SetConVar>();
CMsg_CVars_CVar* cvarMsg = msg->mutable_convars()->add_cvars();
cvarMsg->set_name(name);
cvarMsg->set_value(value);
CSingleRecipientFilter filter(slot);
globals::gameEventSystem->PostEventAbstract(-1, false, &filter, pNetMsg, msg, 0);
delete msg;
}
REGISTER_NATIVES(commands, {
ScriptEngine::RegisterNativeHandler("ADD_COMMAND", AddCommand);
ScriptEngine::RegisterNativeHandler("REMOVE_COMMAND", RemoveCommand);
@@ -211,5 +234,6 @@ REGISTER_NATIVES(commands, {
IssueClientCommandFromServer);
ScriptEngine::RegisterNativeHandler("GET_CLIENT_CONVAR_VALUE", GetClientConVarValue);
ScriptEngine::RegisterNativeHandler("SET_FAKE_CLIENT_CONVAR_VALUE", SetFakeClientConVarValue);
ScriptEngine::RegisterNativeHandler("REPLICATE_CONVAR", ReplicateConVar);
})
} // namespace counterstrikesharp

View File

@@ -12,4 +12,5 @@ ISSUE_CLIENT_COMMAND_FROM_SERVER: slot:int,command:string -> void
FIND_CONVAR: name:string -> pointer
SET_CONVAR_STRING_VALUE: convar:pointer,value:string -> void
GET_CLIENT_CONVAR_VALUE: clientIndex:int,convarName:string -> string
SET_FAKE_CLIENT_CONVAR_VALUE: clientIndex:int,convarName:string,convarValue:string -> void
SET_FAKE_CLIENT_CONVAR_VALUE: clientIndex:int,convarName:string,convarValue:string -> void
REPLICATE_CONVAR: clientSlot:int,convarName:string,convarValue:string -> void

View File

@@ -17,11 +17,11 @@
#include <ios>
#include <sstream>
#include "scripting/autonative.h"
#include "core/function.h"
#include "scripting/script_engine.h"
#include "core/memory.h"
#include "core/log.h"
#include "core/memory.h"
#include "scripting/autonative.h"
#include "scripting/script_engine.h"
namespace counterstrikesharp {
std::vector<ValveFunction*> m_managed_ptrs;
@@ -44,21 +44,22 @@ ValveFunction* CreateVirtualFunctionBySignature(ScriptContext& script_context)
auto* function_addr = FindSignature(binary_name, signature_hex_string);
if (function_addr == nullptr) {
if (function_addr == nullptr)
{
script_context.ThrowNativeError("Could not find signature %s", signature_hex_string);
return nullptr;
}
auto args = std::vector<DataType_t>();
for (int i = 0; i < num_arguments; i++) {
for (int i = 0; i < num_arguments; i++)
{
args.push_back(script_context.GetArgument<DataType_t>(5 + i));
}
auto function = new ValveFunction(function_addr, CONV_CDECL, args, return_type);
function->SetSignature(signature_hex_string);
CSSHARP_CORE_TRACE("Created virtual function, pointer found at {}, signature {}", function_addr,
signature_hex_string);
CSSHARP_CORE_TRACE("Created virtual function, pointer found at {}, signature {}", function_addr, signature_hex_string);
m_managed_ptrs.push_back(function);
return function;
@@ -72,7 +73,8 @@ ValveFunction* CreateVirtualFunction(ScriptContext& script_context)
auto return_type = script_context.GetArgument<DataType_t>(3);
void** vtable = *(void***)ptr;
if (!vtable) {
if (!vtable)
{
script_context.ThrowNativeError("Failed to get the virtual function table.");
return nullptr;
}
@@ -80,7 +82,8 @@ ValveFunction* CreateVirtualFunction(ScriptContext& script_context)
auto function_addr = (void*)vtable[vtable_offset];
auto args = std::vector<DataType_t>();
for (int i = 0; i < num_arguments; i++) {
for (int i = 0; i < num_arguments; i++)
{
args.push_back(script_context.GetArgument<DataType_t>(4 + i));
}
@@ -97,7 +100,8 @@ void HookFunction(ScriptContext& script_context)
auto callback = script_context.GetArgument<CallbackT>(1);
auto post = script_context.GetArgument<bool>(2);
if (!function) {
if (!function)
{
script_context.ThrowNativeError("Invalid function pointer");
return;
}
@@ -111,7 +115,8 @@ void UnhookFunction(ScriptContext& script_context)
auto callback = script_context.GetArgument<CallbackT>(1);
auto post = script_context.GetArgument<bool>(2);
if (!function) {
if (!function)
{
script_context.ThrowNativeError("Invalid function pointer");
return;
}
@@ -123,7 +128,8 @@ void ExecuteVirtualFunction(ScriptContext& script_context)
{
auto function = script_context.GetArgument<ValveFunction*>(0);
if (!function) {
if (!function)
{
script_context.ThrowNativeError("Invalid function pointer");
return;
}
@@ -155,8 +161,7 @@ void RemoveAllNetworkVectorElements(ScriptContext& script_context)
REGISTER_NATIVES(memory, {
ScriptEngine::RegisterNativeHandler("CREATE_VIRTUAL_FUNCTION", CreateVirtualFunction);
ScriptEngine::RegisterNativeHandler("CREATE_VIRTUAL_FUNCTION_BY_SIGNATURE",
CreateVirtualFunctionBySignature);
ScriptEngine::RegisterNativeHandler("CREATE_VIRTUAL_FUNCTION_BY_SIGNATURE", CreateVirtualFunctionBySignature);
ScriptEngine::RegisterNativeHandler("EXECUTE_VIRTUAL_FUNCTION", ExecuteVirtualFunction);
ScriptEngine::RegisterNativeHandler("HOOK_FUNCTION", HookFunction);
ScriptEngine::RegisterNativeHandler("UNHOOK_FUNCTION", UnhookFunction);

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/>. *
*/
#include <scripting/autonative.h>
#include <scripting/script_engine.h>
namespace counterstrikesharp {
static void* MetaFactory(ScriptContext& script_context)
{
auto interfaceName = script_context.GetArgument<const char*>(0);
void* ptr = globals::ismm->MetaFactory(interfaceName, nullptr, nullptr);
return ptr;
}
REGISTER_NATIVES(metamod, {
ScriptEngine::RegisterNativeHandler("META_FACTORY", MetaFactory);
})
} // namespace counterstrikesharp

View File

@@ -0,0 +1 @@
META_FACTORY: interfaceName:string -> pointer