public static class NativeEvents
{
public delegate void OnEntitySpawnedDelegate(CEntityInstance entity);
code
Assistant
public static readonly NativeEvent<OnEntitySpawnedDelegate> OnEntitySpawned =
new("OnEntitySpawned",
(context) => [context.GetArgument<CEntityInstance>(0)]);
public delegate void OnEntityCreatedDelegate(CEntityInstance entity);
public static readonly NativeEvent<OnEntityCreatedDelegate> OnEntityCreated =
new("OnEntityCreated",
(context) => [context.GetArgument<CEntityInstance>(0)]);
public delegate void OnEntityDeletedDelegate(CEntityInstance entity);
public static readonly NativeEvent<OnEntityDeletedDelegate> OnEntityDeleted =
new("OnEntityDeleted",
(context) => [context.GetArgument<CEntityInstance>(0)]);
public delegate void OnEntityParentChanged(CEntityInstance entity, CEntityInstance newParent);
public static readonly NativeEvent<OnEntityParentChanged> OnEntityParentChangedEvent =
new("OnEntityParentChanged",
(context) => [context.GetArgument<CEntityInstance>(0), context.GetArgument<CEntityInstance>(1)]);
public delegate void OnTick();
public static readonly NativeEvent<OnTick> OnTickEvent =
new("OnTick",
(context) => Array.Empty<object>());
public delegate void OnMapStart(string mapName);
public static readonly NativeEvent<OnMapStart> OnMapStartEvent =
new("OnMapStart",
(context) => [context.GetArgument<string>(0)]);
public delegate void OnMapEnd();
public static readonly NativeEvent<OnMapEnd> OnMapEndEvent =
new("OnMapEnd",
(context) => Array.Empty<object>());
public delegate void OnClientConnect(int playerSlot, string name, string ipAddress);
public static readonly NativeEvent<OnClientConnect> OnClientConnectEvent =
new("OnClientConnect",
(context) => [context.GetArgument<int>(0), context.GetArgument<string>(1), context.GetArgument<string>(2)]);
public delegate void OnClientConnected(int playerSlot);
public static readonly NativeEvent<OnClientConnected> OnClientConnectedEvent =
new("OnClientConnected",
(context) => [context.GetArgument<int>(0)]);
public delegate void OnClientPutInServer(int playerSlot);
public static readonly NativeEvent<OnClientPutInServer> OnClientPutInServerEvent =
new("OnClientPutInServer",
(context) => [context.GetArgument<int>(0)]);
public delegate void OnClientDisconnect(int playerSlot);
public static readonly NativeEvent<OnClientDisconnect> OnClientDisconnectEvent =
new("OnClientDisconnect",
(context) => [context.GetArgument<int>(0)]);
public delegate void OnClientDisconnectPost(int playerSlot);
public static readonly NativeEvent<OnClientDisconnectPost> OnClientDisconnectPostEvent =
new("OnClientDisconnectPost",
(context) => [context.GetArgument<int>(0)]);
public delegate void OnClientVoice(int playerSlot);
public static readonly NativeEvent<OnClientVoice> OnClientVoiceEvent =
new("OnClientVoice",
(context) => [context.GetArgument<int>(0)]);
public delegate void OnClientAuthorized(int playerSlot, SteamID steamId);
public static readonly NativeEvent<OnClientAuthorized> OnClientAuthorizedEvent =
new("OnClientAuthorized",
(context) => [context.GetArgument<int>(0), new SteamID(context.GetArgument<ulong>(1))]);
public delegate void OnServerHibernationUpdate(bool isHibernating);
public static readonly NativeEvent<OnServerHibernationUpdate> OnServerHibernationUpdateEvent =
new("OnServerHibernationUpdate",
(context) => [context.GetArgument<bool>(0)]);
public delegate void OnGameServerSteamAPIActivated();
public static readonly NativeEvent<OnGameServerSteamAPIActivated> OnGameServerSteamAPIActivatedEvent =
new("OnGameServerSteamAPIActivated",
(context) => Array.Empty<object>());
public delegate void OnGameServerSteamAPIDeactivated();
public static readonly NativeEvent<OnGameServerSteamAPIDeactivated> OnGameServerSteamAPIDeactivatedEvent =
new("OnGameServerSteamAPIDeactivated",
(context) => Array.Empty<object>());
public delegate void OnHostNameChanged(string hostname);
public static readonly NativeEvent<OnHostNameChanged> OnHostNameChangedEvent =
new("OnHostNameChanged",
(context) => [context.GetArgument<string>(0)]);
public delegate void OnServerPreFatalShutdown();
public static readonly NativeEvent<OnServerPreFatalShutdown> OnServerPreFatalShutdownEvent =
new("OnPreFatalShutdown",
(context) => new object[] { });
public delegate void OnUpdateWhenNotInGame(float frameTime);
public static readonly NativeEvent<OnUpdateWhenNotInGame> OnUpdateWhenNotInGameEvent =
new("OnUpdateWhenNotInGame",
(context) => [context.GetArgument<float>(0)]);
public delegate void OnServerPreWorldUpdate(bool simulating);
public static readonly NativeEvent<OnServerPreWorldUpdate> OnServerPreWorldUpdateEvent =
new("OnServerPreWorldUpdate",
(context) => [context.GetArgument<bool>(0)]);
public delegate void OnServerPrecacheResources(ResourceManifest manifest);
public static readonly NativeEvent<OnServerPrecacheResources> OnServerPrecacheResourcesEvent =
new("OnServerPrecacheResources",
(context) => [context.GetArgument<ResourceManifest>(0)]);
public delegate void CheckTransmit(CCSPlayerController player, ССheckTransmitInfo entities);
public static readonly NativeEvent<CheckTransmit> CheckTransmitEvent =
new("CheckTransmit",
(context) => [Utilities.GetPlayerFromSlot(context.GetArgument<int>(0))!, context.GetArgument<ССheckTransmitInfo>(1)]);
}
короче ало, убери отсюда делегаты, и в генерик засунь от сюда
using System;
using CounterStrikeSharp.API.Core.Attributes;
using CounterStrikeSharp.API.Modules.Entities;
using CounterStrikeSharp.API.Modules.Utils;
namespace CounterStrikeSharp.API.Core
{
public class Listeners {
code
/// <summary>
/// Called when an entity is spawned.
/// </summary>
/// <param name="entity">The spawned entity.</param>
[ListenerName("OnEntitySpawned")]
public delegate void OnEntitySpawned(CEntityInstance entity);
/// <summary>
/// Called when an entity is created.
/// </summary>
/// <param name="entity">The created entity.</param>
[ListenerName("OnEntityCreated")]
public delegate void OnEntityCreated(CEntityInstance entity);
/// <summary>
/// Called when an entity is deleted.
/// </summary>
/// <param name="entity">The deleted entity.</param>
[ListenerName("OnEntityDeleted")]
public delegate void OnEntityDeleted(CEntityInstance entity);
/// <summary>
/// Called when an entity's parent is changed.
/// </summary>
/// <param name="entity">The entity whose parent was changed.</param>
/// <param name="newParent">The new parent entity.</param>
[ListenerName("OnEntityParentChanged")]
public delegate void OnEntityParentChanged(CEntityInstance entity, CEntityInstance newParent);
/// <summary>
/// Called on every server tick (64 per second).
/// This handler should avoid containing expensive operations.
/// </summary>
[ListenerName("OnTick")]
public delegate void OnTick();
/// <summary>
/// Called when a new map is loaded.
/// </summary>
/// <param name="mapName">The name of the map that was started.</param>
[ListenerName("OnMapStart")]
public delegate void OnMapStart(string mapName);
/// <summary>
/// Called when the current map is about to end.
/// </summary>
[ListenerName("OnMapEnd")]
public delegate void OnMapEnd();
/// <summary>
/// Called when a client connects to the server.
/// </summary>
/// <param name="playerSlot">The player slot of the connected client.</param>
/// <param name="name">The name of the connected client.</param>
/// <param name="ipAddress">The IP address of the connected client.</param>
[ListenerName("OnClientConnect")]
public delegate void OnClientConnect(int playerSlot, string name, string ipAddress);
/// <summary>
/// Called when a client connects to the server.
/// </summary>
/// <param name="playerSlot">The player slot of the connected client.</param>
[ListenerName("OnClientConnected")]
public delegate void OnClientConnected(int playerSlot);
/// <summary>
/// Called when a client is entering the game.
/// </summary>
/// <param name="playerSlot">The player slot of the client.</param>
[ListenerName("OnClientPutInServer")]
public delegate void OnClientPutInServer(int playerSlot);
/// <summary>
/// Called when a client disconnects from the server.
/// </summary>
/// <param name="playerSlot">The player slot of the disconnected client.</param>
[ListenerName("OnClientDisconnect")]
public delegate void OnClientDisconnect(int playerSlot);
/// <summary>
/// Called after a client has disconnected from the server.
/// </summary>
/// <param name="playerSlot">The player slot of the disconnected client.</param>
[ListenerName("OnClientDisconnectPost")]
public delegate void OnClientDisconnectPost(int playerSlot);
/// <summary>
/// Called when a client transmits voice data
/// </summary>
/// <param name="playerSlot">The player slot of the client.</param>
[ListenerName("OnClientVoice")]
public delegate void OnClientVoice(int playerSlot);
/// <summary>
/// Called when a client has been authorized by Steam.
/// </summary>
/// <param name="playerSlot">The player slot of the authorized client.</param>
/// <param name="steamId">The Steam ID of the authorized client.</param>
[ListenerName("OnClientAuthorized")]
public delegate void OnClientAuthorized(int playerSlot, [CastFrom(typeof(ulong))]SteamID steamId);
/// <summary>
/// Called when the server is updating the hibernation state.
/// </summary>
/// <param name="isHibernating"><see langword="true"/> if the server is hibernating, <see langword="false"/> otherwise</param>
[ListenerName("OnServerHibernationUpdate")]
public delegate void OnServerHibernationUpdate(bool isHibernating);
/// <summary>
/// Called when the Steam API is activated.
/// </summary>
[ListenerName("OnGameServerSteamAPIActivated")]
public delegate void OnGameServerSteamAPIActivated();
/// <summary>
/// Called when the Steam API is deactivated.
/// </summary>
[ListenerName("OnGameServerSteamAPIDeactivated")]
public delegate void OnGameServerSteamAPIDeactivated();
/// <summary>
/// Called when the server has changed hostname.
/// </summary>
/// <param name="hostname">New hostname of the server</param>
[ListenerName("OnHostNameChanged")]
public delegate void OnHostNameChanged(string hostname);
/// <summary>
/// Called before the server enters fatal shutdown.
/// </summary>
[ListenerName("OnPreFatalShutdown")]
public delegate void OnServerPreFatalShutdown();
/// <summary>
/// Called when the server is in a loading stage.
/// </summary>
/// <param name="frameTime"></param>
[ListenerName("OnUpdateWhenNotInGame")]
public delegate void OnUpdateWhenNotInGame(float frameTime);
/// <summary>
/// Called before the world updates.
/// This seems to be called even when the server is hibernating.
/// </summary>
/// <param name="simulating"><see langword="true"/> if simulating, <see langword="false"/> otherwise</param>
[ListenerName("OnServerPreWorldUpdate")]
public delegate void OnServerPreWorldUpdate(bool simulating);
/// <summary>
/// Called when the server precaching resources (only when initial startup / changing map).
/// </summary>
/// <param name="manifest">Resource Manifest</param>
[ListenerName("OnServerPrecacheResources")]
public delegate void OnServerPrecacheResources(ResourceManifest manifest);
[ListenerName("CheckTransmit")]
public delegate void CheckTransmit(int playerSlot, ССheckTransmitInfo entities);
}
}
To refactor the given code by removing delegates from NativeEvents and leveraging generics, here's a simplified approach. We'll focus solely on the structure by converting delegate usage into a more generic event registration method:
First, update the NativeEvents class to use a generic event system. Here each native event should be able to have different arguments, so we'll introduce a mechanism to handle this generically.
csharp
using CounterStrikeSharp.API.Modules.Entities;
using CounterStrikeSharp.API.Modules.Utils;
using System;
using System.Collections.Generic;
namespace CounterStrikeSharp.API.Core.NativeEvents
{
public static class NativeEvents
{
private static readonly Dictionary<string, NativeEvent> eventRegistry = new();
public static NativeEvent<T> Register<T>(string eventName, Func<NativeEventContext, T[]> argumentResolver) where T : Delegate
{
var nativeEvent = new NativeEvent<T>(eventName, argumentResolver);
eventRegistry[eventName] = nativeEvent;
return nativeEvent;
}
public static NativeEvent<OnEntitySpawned> OnEntitySpawned =
Register<OnEntitySpawned>("OnEntitySpawned", context => new[] { context.GetArgument<CEntityInstance>(0) });
// Replicate similar registrations for other events...
public class NativeEvent
{
public string Name { get; }
protected NativeEvent(string name) { Name = name; }
}
public class NativeEvent<T> : NativeEvent where T : Delegate
{
private event T Handlers;
private readonly Func<NativeEventContext, T[]> ArgumentResolver;
internal NativeEvent(string name, Func<NativeEventContext, T[]> argumentResolver) : base(name)
{
ArgumentResolver = argumentResolver;
}
public void RegisterHandler(T handler) => Handlers += handler;
public void UnregisterHandler(T handler) => Handlers -= handler;
internal void Invoke(NativeEventContext context)
{
if (Handlers != null)
{
var args = ArgumentResolver(context);
Handlers.DynamicInvoke(args);
}
}
}
}
}
public class NativeEventContext
{
// Implementation for context handling, similar to the argument handling in your provided code.
public T GetArgument<T>(int index) => default; // Simulate argument retrieval.
}
Explanation:
Generic Event System: The NativeEvent class is made generic to accommodate any delegate type. This reduces boilerplate and makes event handling more flexible.
Registration Mechanism: Register<T> is responsible for registering events and assigning them to a dictionary for later access. This helps streamline the event setup.
Invocation Logic: Invoke method dynamically fetches arguments using the provided resolver and triggers registered handlers.
Adapt host code and usage examples to this updated event system, ensuring NativeEventContext is adequately designed for argument retrieval based on your specific needs. By removing individual delegates, we're focusing on a cleaner, scalable architecture.