Skip to content

Commit 83e2cf2

Browse files
committed
feat(managed): OnStartupServer event
1 parent 333bc07 commit 83e2cf2

File tree

8 files changed

+74
-2
lines changed

8 files changed

+74
-2
lines changed

managed/src/SwiftlyS2.Core/Modules/Events/EventPublisher.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public static void Register()
5050
NativeEvents.RegisterOnClientProcessUsercmdsCallback((nint)(delegate* unmanaged< int, nint, int, byte, float, void >)&OnClientProcessUsercmds);
5151
NativeEvents.RegisterOnEntityTakeDamageCallback((nint)(delegate* unmanaged< nint, nint, nint, byte >)&OnEntityTakeDamage);
5252
NativeEvents.RegisterOnPrecacheResourceCallback((nint)(delegate* unmanaged< nint, void >)&OnPrecacheResource);
53+
NativeEvents.RegisterOnStartupServerCallback((nint)(delegate* unmanaged< void >)&OnStartupServer);
5354
NativeConvars.AddConvarCreatedListener((nint)(delegate* unmanaged< nint, void >)&OnConVarCreated);
5455
NativeConvars.AddConCommandCreatedListener((nint)(delegate* unmanaged< nint, void >)&OnConCommandCreated);
5556
NativeConvars.AddGlobalChangeListener((nint)(delegate* unmanaged< nint, int, nint, nint, void >)&OnConVarValueChanged);
@@ -534,6 +535,24 @@ public static void OnPrecacheResource( nint pResourceManifest )
534535
}
535536
}
536537

538+
[UnmanagedCallersOnly]
539+
public static void OnStartupServer( )
540+
{
541+
if (_subscribers.Count == 0) return;
542+
try
543+
{
544+
foreach (var subscriber in _subscribers)
545+
{
546+
subscriber.InvokeOnStartupServer();
547+
}
548+
}
549+
catch (Exception e)
550+
{
551+
if (!GlobalExceptionHandler.Handle(e)) return;
552+
AnsiConsole.WriteException(e);
553+
}
554+
}
555+
537556
[Obsolete("InvokeOnEntityTouchHook is deprecated. Use InvokeOnEntityStartTouch, InvokeOnEntityTouch, or InvokeOnEntityEndTouch instead.")]
538557
public static void InvokeOnEntityTouchHook( OnEntityTouchHookEvent @event )
539558
{

managed/src/SwiftlyS2.Core/Modules/Events/EventSubscriber.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public EventSubscriber( CoreContext id, IContextedProfilerService profiler, ILog
5959
public event EventDelegates.OnMovementServicesRunCommandHook? OnMovementServicesRunCommandHook;
6060
public event EventDelegates.OnPlayerPawnPostThink? OnPlayerPawnPostThink;
6161
public event EventDelegates.OnEntityIdentityAcceptInputHook? OnEntityIdentityAcceptInputHook;
62+
public event EventDelegates.OnStartupServer? OnStartupServer;
6263

6364
public void Dispose()
6465
{
@@ -638,4 +639,22 @@ public void InvokeOnEntityIdentityAcceptInputHook( OnEntityIdentityAcceptInputHo
638639
_Profiler.StopRecording("Event::OnEntityIdentityAcceptInput");
639640
}
640641
}
642+
643+
public void InvokeOnStartupServer()
644+
{
645+
try
646+
{
647+
if (OnStartupServer == null) return;
648+
_Profiler.StartRecording("Event::OnStartupServer");
649+
OnStartupServer?.Invoke();
650+
}
651+
catch (Exception e)
652+
{
653+
if (GlobalExceptionHandler.Handle(e)) _Logger.LogError(e, "Error invoking OnStartupServer.");
654+
}
655+
finally
656+
{
657+
_Profiler.StopRecording("Event::OnStartupServer");
658+
}
659+
}
641660
}

managed/src/SwiftlyS2.Generated/Natives/Events.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,13 @@ public unsafe static void RegisterOnPrecacheResourceCallback(nint callback) {
163163
public unsafe static void RegisterOnPreworldUpdateCallback(nint callback) {
164164
_RegisterOnPreworldUpdateCallback(callback);
165165
}
166+
167+
private unsafe static delegate* unmanaged<nint, void> _RegisterOnStartupServerCallback;
168+
169+
/// <summary>
170+
/// void
171+
/// </summary>
172+
public unsafe static void RegisterOnStartupServerCallback(nint callback) {
173+
_RegisterOnStartupServerCallback(callback);
174+
}
166175
}

managed/src/SwiftlyS2.Shared/Modules/Events/EventDelegates.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,9 @@ public class EventDelegates
166166
/// Called when an entity identity accept input hook is triggered.
167167
/// </summary>
168168
public delegate void OnEntityIdentityAcceptInputHook( IOnEntityIdentityAcceptInputHookEvent @event );
169+
170+
/// <summary>
171+
/// Called when the server is started.
172+
/// </summary>
173+
public delegate void OnStartupServer();
169174
}

managed/src/SwiftlyS2.Shared/Modules/Events/IEventSubscriber.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,9 @@ public interface IEventSubscriber
166166
/// Called when an entity identity accept input hook is triggered.
167167
/// </summary>
168168
public event EventDelegates.OnEntityIdentityAcceptInputHook? OnEntityIdentityAcceptInputHook;
169+
170+
/// <summary>
171+
/// Called when the server is started.
172+
/// </summary>
173+
public event EventDelegates.OnStartupServer? OnStartupServer;
169174
}

natives/engine/events.native

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ void RegisterOnMapLoadCallback = ptr callback // string mapname
1616
void RegisterOnMapUnloadCallback = ptr callback // string mapname
1717
void RegisterOnEntityTakeDamageCallback = ptr callback // CBaseEntity* entity, CTakeDamageInfo* info -> bool (true -> ignored, false -> supercede)
1818
void RegisterOnPrecacheResourceCallback = ptr callback // IEntityResourceManifest* pResourceManifest
19-
void RegisterOnPreworldUpdateCallback = ptr callback // bool simulating
19+
void RegisterOnPreworldUpdateCallback = ptr callback // bool simulating
20+
void RegisterOnStartupServerCallback = ptr callback // void

src/engine/entities/entitysystem.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <api/interfaces/manager.h>
3333
#include <s2binlib/s2binlib.h>
3434

35+
3536
typedef void (*CBaseEntity_DispatchSpawn)(void*, void*);
3637
typedef void (*UTIL_Remove)(void*);
3738
typedef void* (*UTIL_CreateEntityByName)(const char*, int);
@@ -44,6 +45,7 @@ void* g_pGameRules = nullptr;
4445

4546
extern void* g_pOnEntityTakeDamageCallback;
4647
extern void* g_pTraceManager;
48+
extern void* g_pOnStartupServerCallback;
4749

4850
IFunctionHook* g_pOnEntityTakeDamageHook = nullptr;
4951
IFunctionHook* g_pTraceShapeHook = nullptr;
@@ -134,6 +136,11 @@ void StartupServerHook(void* _this, const GameSessionConfiguration_t& config, IS
134136
g_pGameEntitySystem = entSystem;
135137
g_pGameEntitySystem->AddListenerEntity(&g_entityListener);
136138

139+
if (g_pOnStartupServerCallback)
140+
{
141+
reinterpret_cast<void(*)()>(g_pOnStartupServerCallback)();
142+
}
143+
137144
g_bDone = true;
138145
}
139146

src/scripting/engine/events.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ void* g_pOnMapUnloadCallback = nullptr;
3636
void* g_pOnEntityTakeDamageCallback = nullptr;
3737
void* g_pOnPrecacheResourceCallback = nullptr;
3838
void* g_pOnPreworldUpdateCallback = nullptr;
39+
void* g_pOnStartupServerCallback = nullptr;
3940

4041
void Bridge_Events_RegisterOnGameTickCallback(void* callback)
4142
{
@@ -122,6 +123,11 @@ void Bridge_Events_RegisterOnPreworldUpdateCallback(void* callback)
122123
g_pOnPreworldUpdateCallback = callback;
123124
}
124125

126+
void Bridge_Events_RegisterOnStartupServerCallback(void* callback)
127+
{
128+
g_pOnStartupServerCallback = callback;
129+
}
130+
125131
DEFINE_NATIVE("Events.RegisterOnGameTickCallback", Bridge_Events_RegisterOnGameTickCallback);
126132
DEFINE_NATIVE("Events.RegisterOnClientConnectCallback", Bridge_Events_RegisterOnClientConnectCallback);
127133
DEFINE_NATIVE("Events.RegisterOnClientDisconnectCallback", Bridge_Events_RegisterOnClientDisconnectCallback);
@@ -138,4 +144,5 @@ DEFINE_NATIVE("Events.RegisterOnMapLoadCallback", Bridge_Events_RegisterOnMapLoa
138144
DEFINE_NATIVE("Events.RegisterOnMapUnloadCallback", Bridge_Events_RegisterOnMapUnloadCallback);
139145
DEFINE_NATIVE("Events.RegisterOnEntityTakeDamageCallback", Bridge_Events_RegisterOnEntityTakeDamageCallback);
140146
DEFINE_NATIVE("Events.RegisterOnPrecacheResourceCallback", Bridge_Events_RegisterOnPrecacheResourceCallback);
141-
DEFINE_NATIVE("Events.RegisterOnPreworldUpdateCallback", Bridge_Events_RegisterOnPreworldUpdateCallback);
147+
DEFINE_NATIVE("Events.RegisterOnPreworldUpdateCallback", Bridge_Events_RegisterOnPreworldUpdateCallback);
148+
DEFINE_NATIVE("Events.RegisterOnStartupServerCallback", Bridge_Events_RegisterOnStartupServerCallback);

0 commit comments

Comments
 (0)