Skip to content

Commit 8180459

Browse files
committed
update(utils): Move to new extension system
1 parent 2e94733 commit 8180459

File tree

19 files changed

+104
-923
lines changed

19 files changed

+104
-923
lines changed

.vscode/c_cpp_properties.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
"${workspaceFolder}/vendor/**",
1212
"${workspaceFolder}/vendor/embedder/src/**",
1313
"${workspaceFolder}/vendor/embedder/libs/js/**",
14-
"${workspaceFolder}/vendor/embedder/libs/lua/**"
14+
"${workspaceFolder}/vendor/embedder/libs/lua/**",
15+
"${workspaceFolder}/vendor/swiftly-ext/hooks/**",
16+
"${workspaceFolder}/vendor/swiftly-ext/hooks/dynohook/**"
1517
],
1618
"defines": [
1719
"_DEBUG",
@@ -36,7 +38,9 @@
3638
"${workspaceFolder}/vendor/**",
3739
"${workspaceFolder}/vendor/embedder/src/**",
3840
"${workspaceFolder}/vendor/embedder/libs/js/**",
39-
"${workspaceFolder}/vendor/embedder/libs/lua/**"
41+
"${workspaceFolder}/vendor/embedder/libs/lua/**",
42+
"${workspaceFolder}/vendor/swiftly-ext/hooks/**",
43+
"${workspaceFolder}/vendor/swiftly-ext/hooks/dynohook/**"
4044
],
4145
"defines": [
4246
"_DEBUG",

.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
6767
"typeinfo": "cpp",
6868
"any": "cpp",
6969
"optional": "cpp",
70-
"codecvt": "cpp"
70+
"codecvt": "cpp",
71+
"xstring": "cpp",
72+
"xlocbuf": "cpp"
7173
}
7274
}

AMBuilder

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@ for sdk_target in MMSPlugin.sdk_targets:
6363
"-fexceptions",
6464
]
6565
binary.compiler.postlink += [
66-
os.path.join(builder.sourcePath, 'vendor', 'funchook', 'lib', 'libfunchook.a'),
67-
os.path.join(builder.sourcePath, 'vendor', 'funchook', 'lib', 'libdistorm.a'),
68-
6966
"-lgnutls",
7067
"-lz",
7168
"-lpthread",
@@ -102,11 +99,10 @@ for sdk_target in MMSPlugin.sdk_targets:
10299
"/TP",
103100
"/MT",
104101
"/W3",
105-
"/Z7"
102+
"/Z7",
103+
"/EHsc"
106104
]
107105
binary.compiler.postlink += [
108-
os.path.join(builder.sourcePath, 'vendor', 'funchook', 'lib', 'funchook.lib'),
109-
os.path.join(builder.sourcePath, 'vendor', 'funchook', 'lib', 'distorm.lib'),
110106
"psapi.lib",
111107
"winmm.lib",
112108
"ws2_32.lib",
@@ -164,7 +160,8 @@ for sdk_target in MMSPlugin.sdk_targets:
164160
binary.compiler.cxxincludes += [
165161
os.path.join(builder.sourcePath, 'vendor'),
166162
os.path.join(builder.sourcePath, 'vendor', 'embedder', 'src'),
167-
163+
os.path.join(builder.sourcePath, 'vendor', 'swiftly-ext', 'hooks'),
164+
os.path.join(builder.sourcePath, 'vendor', 'swiftly-ext', 'hooks', 'dynohook'),
168165
os.path.join(sdk['path']),
169166
os.path.join(sdk['path'], 'public'),
170167
os.path.join(sdk['path'], 'public', 'entity2'),

PackageScript

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@ with open(os.path.join(builder.buildPath, 'pdblog.txt'), 'wt') as fp:
3333
for line in pdb_list:
3434
fp.write(line.path + '\n')
3535

36-
gamedata_folder = builder.AddFolder(os.path.join('addons', 'swiftly', 'gamedata', 'utils'))
37-
builder.AddCopy(os.path.join(builder.sourcePath, 'plugin_files', 'gamedata', 'utils', 'signatures.json'), gamedata_folder)
36+
gamedata_folder = builder.AddFolder(os.path.join('addons', 'swiftly', 'gamedata', 'cs2', 'utils'))
37+
builder.AddCopy(os.path.join(builder.sourcePath, 'plugin_files', 'gamedata', 'cs2', 'utils', 'signatures.json'), gamedata_folder)

src/entrypoint.cpp

Lines changed: 81 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
11
#include "entrypoint.h"
2-
#include <Embedder.h>
3-
#include "utilsclass.h"
2+
#include <embedder/src/embedder.h>
43
#include <ehandle.h>
54
#include "sdk/services.h"
6-
5+
#include "sdk/CRecipientFilters.h"
6+
#include "networkbasetypes.pb.h"
77
#include <swiftly-ext/sdk.h>
8+
#include <swiftly-ext/memory.h>
89

9-
void Hook_CCSPlayer_MovementServices_CheckJumpPre(void* services, void* movementData);
10+
dyno::ReturnAction Hook_CCSPlayer_MovementServices_CheckJumpPre(dyno::CallbackType cbType, dyno::IHook& hook);
1011

1112
//////////////////////////////////////////////////////////////
1213
///////////////// Core Variables //////////////
1314
////////////////////////////////////////////////////////////
1415

15-
SH_DECL_HOOK5_void(IServerGameClients, ClientDisconnect, SH_NOATTRIB, 0, CPlayerSlot, ENetworkDisconnectionReason, const char *, uint64, const char *);
16+
SH_DECL_HOOK5_void(IServerGameClients, ClientDisconnect, SH_NOATTRIB, 0, CPlayerSlot, ENetworkDisconnectionReason, const char*, uint64, const char*);
1617
SH_DECL_HOOK2(IGameEventManager2, LoadEventsFromFile, SH_NOATTRIB, 0, int, const char*, bool);
1718

1819
int loadEventFromFileHookID = -1;
19-
IServerGameClients *gameclients = nullptr;
20+
IServerGameClients* gameclients = nullptr;
2021
IGameEventManager2* g_gameEventManager = nullptr;
2122
IGameEventSystem* g_pGameEventSystem = nullptr;
2223
IVEngineServer2* engine = nullptr;
2324
ICvar* icvar = nullptr;
2425

2526
UtilsExtension g_Ext;
26-
CUtlVector<FuncHookBase*> g_vecHooks;
2727
CREATE_GLOBALVARS();
2828

29-
FuncHook<decltype(Hook_CCSPlayer_MovementServices_CheckJumpPre)> TCCSPlayer_MovementServices_CheckJumpPre(Hook_CCSPlayer_MovementServices_CheckJumpPre, "CCSPlayer_MovementServices_CheckJumpPre");
29+
FunctionHook CCSPlayer_MovementServices_CheckJumpPre("CCSPlayer_MovementServices_CheckJumpPre", dyno::CallbackType::Pre, Hook_CCSPlayer_MovementServices_CheckJumpPre, "pp", 'v');
30+
FunctionHook CCSPlayer_MovementServices_CheckJumpPost("CCSPlayer_MovementServices_CheckJumpPre", dyno::CallbackType::Post, Hook_CCSPlayer_MovementServices_CheckJumpPre, "pp", 'v');
3031

3132
ConVar* autobunnyhoppingcvar = nullptr;
3233

@@ -37,11 +38,8 @@ ConVar* autobunnyhoppingcvar = nullptr;
3738
EXT_EXPOSE(g_Ext);
3839
bool UtilsExtension::Load(std::string& error, SourceHook::ISourceHook* SHPtr, ISmmAPI* ismm, bool late)
3940
{
40-
SAVE_GLOBALVARS();
41-
if (!InitializeHooks()) {
42-
error = "Failed to initialize hooks.";
43-
return false;
44-
}
41+
g_SHPtr = SHPtr;
42+
g_SMAPI = ismm;
4543

4644
GET_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
4745
GET_IFACE_CURRENT(GetEngineFactory, icvar, ICvar, CVAR_INTERFACE_VERSION);
@@ -59,18 +57,18 @@ bool UtilsExtension::Load(std::string& error, SourceHook::ISourceHook* SHPtr, IS
5957

6058
int UtilsExtension::LoadEventsFromFile(const char* filePath, bool searchAll)
6159
{
62-
if(!g_gameEventManager) {
60+
if (!g_gameEventManager) {
6361
g_gameEventManager = META_IFACEPTR(IGameEventManager2);
6462
}
6563

6664
RETURN_META_VALUE(MRES_IGNORED, 0);
6765
}
6866

69-
extern uint64_t bhop_state;
67+
uint64_t bhop_state = 0;
7068

71-
void UtilsExtension::Hook_ClientDisconnect( CPlayerSlot slot, ENetworkDisconnectionReason reason, const char *pszName, uint64 xuid, const char *pszNetworkID )
69+
void UtilsExtension::Hook_ClientDisconnect(CPlayerSlot slot, ENetworkDisconnectionReason reason, const char* pszName, uint64 xuid, const char* pszNetworkID)
7270
{
73-
if((bhop_state & (1ULL << slot.Get())) != 0) bhop_state &= ~(1ULL << slot.Get());
71+
if ((bhop_state & (1ULL << slot.Get())) != 0) bhop_state &= ~(1ULL << slot.Get());
7472
}
7573

7674
ConVar* FetchCVar(std::string cvarname)
@@ -85,8 +83,10 @@ ConVar* FetchCVar(std::string cvarname)
8583
return icvar->GetConVar(cvarHandle);
8684
}
8785

88-
void Hook_CCSPlayer_MovementServices_CheckJumpPre(void* services, void* movementData)
86+
dyno::ReturnAction Hook_CCSPlayer_MovementServices_CheckJumpPre(dyno::CallbackType cbType, dyno::IHook& hook)
8987
{
88+
void* services = hook.getArgument<void*>(0);
89+
9090
if (autobunnyhoppingcvar == nullptr)
9191
autobunnyhoppingcvar = FetchCVar("sv_autobunnyhopping");
9292

@@ -96,25 +96,20 @@ void Hook_CCSPlayer_MovementServices_CheckJumpPre(void* services, void* movement
9696
{
9797
CHandle<CEntityInstance> controller = SDKGetProp<CHandle<CEntityInstance>>(((CPlayer_MovementServices*)services)->m_pPawn, "CBasePlayerPawn", "m_hController");
9898
int pid = controller.GetEntryIndex() - 1;
99-
if((bhop_state & (1ULL << pid)) != 0) {
100-
autobunnyhopping = true;
101-
102-
TCCSPlayer_MovementServices_CheckJumpPre(services, movementData);
103-
104-
autobunnyhopping = false;
105-
return;
99+
if ((bhop_state & (1ULL << pid)) != 0) {
100+
autobunnyhopping = (cbType == dyno::CallbackType::Pre);
101+
return dyno::ReturnAction::Ignored;
106102
}
107103
}
108104

109-
TCCSPlayer_MovementServices_CheckJumpPre(services, movementData);
105+
return dyno::ReturnAction::Ignored;
110106
}
111107

112108
bool UtilsExtension::Unload(std::string& error)
113109
{
114110
SH_REMOVE_HOOK_ID(loadEventFromFileHookID);
115111
SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, gameclients, this, &UtilsExtension::Hook_ClientDisconnect, true);
116112

117-
UnloadHooks();
118113
return true;
119114
}
120115

@@ -130,25 +125,73 @@ void UtilsExtension::AllPluginsLoaded()
130125

131126
bool UtilsExtension::OnPluginLoad(std::string pluginName, void* pluginState, PluginKind_t kind, std::string& error)
132127
{
133-
EContext* state = (EContext*)pluginState;
128+
EContext* ctx = (EContext*)pluginState;
129+
130+
ADD_CLASS("PlayerUtils");
131+
132+
#ifndef _WIN32
133+
ADD_CLASS_FUNCTION("PlayerUtils", "SetBunnyhop", [](FunctionContext* context, ClassData* data) -> void {
134+
int playerid = context->GetArgumentOr<int>(0, -1);
135+
bool state = context->GetArgumentOr<bool>(1, false);
136+
137+
if (state == true) bhop_state |= (1ULL << playerid);
138+
else bhop_state &= ~(1ULL << playerid);
139+
140+
INetworkMessageInternal* netmsg = g_pNetworkMessages->FindNetworkMessagePartial("SetConVar");
141+
auto msg = netmsg->AllocateMessage()->ToPB<CNETMsg_SetConVar>();
142+
CMsg_CVars_CVar* cvar = msg->mutable_convars()->add_cvars();
143+
cvar->set_name("sv_autobunnyhopping");
144+
cvar->set_value(state == true ? "true" : "false");
134145

135-
BeginClass<PlayerUtils>("PlayerUtils", state)
136-
.addConstructor<std::string>()
137-
.addFunction("SetBunnyhop", &PlayerUtils::SetBunnyhop)
138-
.addFunction("GetBunnyhop", &PlayerUtils::GetBunnyhop)
139-
.addFunction("IsListeningToGameEvent", &PlayerUtils::IsListeningToGameEvent)
140-
.endClass();
146+
CSingleRecipientFilter filter(playerid);
147+
g_pGameEventSystem->PostEventAbstract(-1, false, &filter, netmsg, msg, 0);
141148

142-
GetGlobalNamespace(state).addConstant("playerutils", PlayerUtils(pluginName));
149+
delete msg;
150+
});
151+
#else
152+
ADD_CLASS_FUNCTION("PlayerUtils", "SetBunnyhop", [](FunctionContext* context, ClassData* data) -> void {
153+
int playerid = context->GetArgumentOr<int>(0, -1);
154+
bool state = context->GetArgumentOr<bool>(1, false);
155+
156+
if (state == true) bhop_state |= (1ULL << playerid);
157+
else bhop_state &= ~(1ULL << playerid);
158+
159+
INetworkMessageInternal* netmsg = g_pNetworkMessages->FindNetworkMessagePartial("SetConVar");
160+
auto msg = netmsg->AllocateMessage()->ToPB<CNETMsg_SetConVar>();
161+
CMsg_CVars_CVar* cvar = msg->mutable_convars()->add_cvars();
162+
cvar->set_name("sv_autobunnyhopping");
163+
cvar->set_value(state == true ? "true" : "false");
164+
165+
CSingleRecipientFilter filter(playerid);
166+
g_pGameEventSystem->PostEventAbstract(-1, false, &filter, netmsg, msg, 0);
167+
});
168+
#endif
169+
170+
ADD_CLASS_FUNCTION("PlayerUtils", "IsListeningToGameEvent", [](FunctionContext* context, ClassData* data) -> void {
171+
int playerid = context->GetArgumentOr<int>(0, -1);
172+
std::string game_event = context->GetArgumentOr<std::string>(1, "");
173+
174+
void* getListener = GetSignature("LegacyGameEventListener");
175+
if (!getListener) return context->SetReturn(false);
176+
177+
IGameEventListener2* playerListener = reinterpret_cast<IGameEventListener2 * (*)(int)>(getListener)(playerid);
178+
if (!playerListener) return context->SetReturn(false);
179+
180+
return context->SetReturn(g_gameEventManager->FindListener(playerListener, game_event.c_str()));
181+
});
182+
183+
ADD_CLASS_FUNCTION("PlayerUtils", "GetBunnyhop", [](FunctionContext* context, ClassData* data) -> void {
184+
int playerid = context->GetArgumentOr<int>(0, -1);
185+
context->SetReturn(((bhop_state & (1ULL << playerid)) != 0));
186+
});
187+
188+
ADD_VARIABLE("_G", "playerutils", MAKE_CLASS_INSTANCE_CTX(ctx, "PlayerUtils", {}));
143189

144190
return true;
145191
}
146192

147193
bool UtilsExtension::OnPluginUnload(std::string pluginName, void* pluginState, PluginKind_t kind, std::string& error)
148194
{
149-
EContext* state = (EContext*)pluginState;
150-
151-
GetGlobalNamespace(state).addConstant("playerutils", nullptr);
152195
return true;
153196
}
154197

src/entrypoint.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,22 @@
1111

1212
#include <swiftly-ext/core.h>
1313
#include <swiftly-ext/extension.h>
14-
#include <swiftly-ext/hooks/NativeHooks.h>
14+
#include <swiftly-ext/hooks/function.h>
15+
#include <swiftly-ext/hooks/vfunction.h>
1516

1617
class UtilsExtension : public SwiftlyExt
1718
{
1819
public:
19-
bool Load(std::string& error, SourceHook::ISourceHook *SHPtr, ISmmAPI* ismm, bool late);
20+
bool Load(std::string& error, SourceHook::ISourceHook* SHPtr, ISmmAPI* ismm, bool late);
2021
bool Unload(std::string& error);
21-
22+
2223
void AllExtensionsLoaded();
2324
void AllPluginsLoaded();
2425

2526
bool OnPluginLoad(std::string pluginName, void* pluginState, PluginKind_t kind, std::string& error);
2627
bool OnPluginUnload(std::string pluginName, void* pluginState, PluginKind_t kind, std::string& error);
2728

28-
void Hook_ClientDisconnect( CPlayerSlot slot, ENetworkDisconnectionReason reason, const char *pszName, uint64 xuid, const char *pszNetworkID );
29+
void Hook_ClientDisconnect(CPlayerSlot slot, ENetworkDisconnectionReason reason, const char* pszName, uint64 xuid, const char* pszNetworkID);
2930
int LoadEventsFromFile(const char* filePath, bool searchAll);
3031

3132
public:

src/utilsclass.cpp

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/utilsclass.h

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)