Skip to content

Commit 26e4a18

Browse files
authored
Merge pull request #2 from swiftly-solution/new-ext
New ext
2 parents 670367c + 6563b53 commit 26e4a18

21 files changed

+1338
-1129
lines changed

.vscode/settings.json

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,31 @@
6969
"optional": "cpp",
7070
"codecvt": "cpp",
7171
"future": "cpp",
72-
"variant": "cpp"
72+
"variant": "cpp",
73+
"xstring": "cpp",
74+
"charconv": "cpp",
75+
"filesystem": "cpp",
76+
"format": "cpp",
77+
"ios": "cpp",
78+
"locale": "cpp",
79+
"queue": "cpp",
80+
"ranges": "cpp",
81+
"span": "cpp",
82+
"stack": "cpp",
83+
"xfacet": "cpp",
84+
"xhash": "cpp",
85+
"xiosbase": "cpp",
86+
"xlocale": "cpp",
87+
"xlocbuf": "cpp",
88+
"xlocinfo": "cpp",
89+
"xlocmes": "cpp",
90+
"xlocmon": "cpp",
91+
"xlocnum": "cpp",
92+
"xloctime": "cpp",
93+
"xmemory": "cpp",
94+
"xtr1common": "cpp",
95+
"xtree": "cpp",
96+
"xutility": "cpp",
97+
"csetjmp": "cpp"
7398
}
7499
}

AMBuilder

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ for sdk_target in MMSPlugin.sdk_targets:
6262
"-fexceptions",
6363
]
6464
binary.compiler.postlink += [
65-
os.path.join(builder.sourcePath, 'vendor', 'funchook', 'lib', 'libfunchook.a'),
66-
os.path.join(builder.sourcePath, 'vendor', 'funchook', 'lib', 'libdistorm.a'),
67-
6865
"-lgnutls",
6966
"-lz",
7067
"-lpthread",
@@ -101,12 +98,11 @@ for sdk_target in MMSPlugin.sdk_targets:
10198
"/TP",
10299
"/MT",
103100
"/W3",
104-
"/Z7"
101+
"/Z7",
102+
"/EHsc"
105103
]
106104
binary.compiler.postlink += [
107-
os.path.join(builder.sourcePath, 'vendor', 'funchook', 'lib', 'funchook.lib'),
108-
os.path.join(builder.sourcePath, 'vendor', 'funchook', 'lib', 'distorm.lib'),
109-
"psapi.lib",
105+
"psapi.lib",
110106
"winmm.lib",
111107
"ws2_32.lib",
112108
"wldap32.lib",
@@ -164,6 +160,9 @@ for sdk_target in MMSPlugin.sdk_targets:
164160
os.path.join(builder.sourcePath, 'vendor'),
165161
os.path.join(builder.sourcePath, 'vendor', 'embedder', 'src'),
166162

163+
os.path.join(builder.sourcePath, 'vendor', 'swiftly-ext', 'hooks'),
164+
os.path.join(builder.sourcePath, 'vendor', 'swiftly-ext', 'hooks', 'dynohook'),
165+
167166
os.path.join(sdk['path']),
168167
os.path.join(sdk['path'], 'public'),
169168
os.path.join(sdk['path'], 'public', 'entity2'),

PackageScript

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ 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', 'sdktools'))
37-
builder.AddCopy(os.path.join(builder.sourcePath, 'plugin_files', 'gamedata', 'sdktools', 'offsets.json'), gamedata_folder)
38-
builder.AddCopy(os.path.join(builder.sourcePath, 'plugin_files', 'gamedata', 'sdktools', 'signatures.json'), gamedata_folder)
36+
gamedata_folder = builder.AddFolder(os.path.join('addons', 'swiftly', 'gamedata', 'cs2', 'sdktools'))
37+
builder.AddCopy(os.path.join(builder.sourcePath, 'plugin_files', 'gamedata', 'cs2', 'sdktools', 'offsets.json'), gamedata_folder)
38+
builder.AddCopy(os.path.join(builder.sourcePath, 'plugin_files', 'gamedata', 'cs2', 'sdktools', 'signatures.json'), gamedata_folder)

alliedmodders/metamod

Submodule metamod updated 57 files

src/entities.cpp

Lines changed: 77 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#include "entities.h"
22
#include <swiftly-ext/event.h>
33
#include <vector>
4+
#include <set>
5+
#include <string>
46

57
std::any tmpret;
68
CEntityListener g_entityListener;
79

8-
SH_DECL_MANUALHOOK1_void(StartTouch, 0, 0, 0, CEntityInstance *);
9-
SH_DECL_MANUALHOOK1_void(Touch, 0, 0, 0, CEntityInstance *);
10-
SH_DECL_MANUALHOOK1_void(EndTouch, 0, 0, 0, CEntityInstance *);
10+
SH_DECL_MANUALHOOK1_void(StartTouch, 0, 0, 0, CEntityInstance*);
11+
SH_DECL_MANUALHOOK1_void(Touch, 0, 0, 0, CEntityInstance*);
12+
SH_DECL_MANUALHOOK1_void(EndTouch, 0, 0, 0, CEntityInstance*);
1113
SH_DECL_MANUALHOOK1_void(EntityUse, 0, 0, 0, class InputData_t*);
1214
SH_DECL_EXTERN3_void(INetworkServerService, StartupServer, SH_NOATTRIB, 0, const GameSessionConfiguration_t&, ISource2WorldSession*, const char*);
1315

@@ -23,9 +25,9 @@ void OnPostUse(class InputData_t* ent);
2325
void EntityListener::Initialize()
2426
{
2527
SH_MANUALHOOK_RECONFIGURE(StartTouch, GetOffset("CBaseEntity_StartTouch"), 0, 0);
26-
SH_MANUALHOOK_RECONFIGURE(Touch, GetOffset("CBaseEntity_Touch"), 0, 0);
27-
SH_MANUALHOOK_RECONFIGURE(EndTouch, GetOffset("CBaseEntity_EndTouch"), 0, 0);
28-
SH_MANUALHOOK_RECONFIGURE(EntityUse, GetOffset("CBaseEntity_Use"), 0, 0);
28+
SH_MANUALHOOK_RECONFIGURE(Touch, GetOffset("CBaseEntity_Touch"), 0, 0);
29+
SH_MANUALHOOK_RECONFIGURE(EndTouch, GetOffset("CBaseEntity_EndTouch"), 0, 0);
30+
SH_MANUALHOOK_RECONFIGURE(EntityUse, GetOffset("CBaseEntity_Use"), 0, 0);
2931

3032
SH_ADD_HOOK_MEMFUNC(INetworkServerService, StartupServer, g_pNetworkServerService, this, &EntityListener::StartupServer, true);
3133
}
@@ -45,6 +47,21 @@ void EntityListener::StartupServer(const GameSessionConfiguration_t& config, ISo
4547
bDone = true;
4648
}
4749

50+
std::set<std::string> classNames;
51+
52+
void entities_SetupScripting(EContext* ctx)
53+
{
54+
ADD_FUNCTION("ListenEntityTouchUse", [](FunctionContext* context) -> void {
55+
std::string className = context->GetArgumentOr<std::string>(0, "");
56+
classNames.insert(className);
57+
});
58+
59+
ADD_FUNCTION("RemoveListenEntityTouchUse", [](FunctionContext* context) -> void {
60+
std::string className = context->GetArgumentOr<std::string>(0, "");
61+
classNames.erase(className);
62+
});
63+
}
64+
4865
void CEntityListener::OnEntitySpawned(CEntityInstance* pEntity)
4966
{
5067
}
@@ -55,79 +72,117 @@ void CEntityListener::OnEntityParentChanged(CEntityInstance* pEntity, CEntityIns
5572

5673
void CEntityListener::OnEntityCreated(CEntityInstance* pEntity)
5774
{
58-
// SH_ADD_MANUALHOOK_STATICFUNC(StartTouch, pEntity, OnStartTouch, false);
59-
// SH_ADD_MANUALHOOK_STATICFUNC(StartTouch, pEntity, OnPostStartTouch, true);
60-
// SH_ADD_MANUALHOOK_STATICFUNC(Touch, pEntity, OnTouch, false);
61-
// SH_ADD_MANUALHOOK_STATICFUNC(Touch, pEntity, OnPostTouch, true);
62-
// SH_ADD_MANUALHOOK_STATICFUNC(EndTouch, pEntity, OnEndTouch, false);
63-
// SH_ADD_MANUALHOOK_STATICFUNC(EndTouch, pEntity, OnPostEndTouch, true);
64-
// SH_ADD_MANUALHOOK_STATICFUNC(EntityUse, pEntity, OnUse, false);
65-
// SH_ADD_MANUALHOOK_STATICFUNC(EntityUse, pEntity, OnPostUse, true);
75+
if (classNames.find(pEntity->GetClassname()) != classNames.end()) {
76+
SH_ADD_MANUALHOOK_STATICFUNC(StartTouch, pEntity, OnStartTouch, false);
77+
SH_ADD_MANUALHOOK_STATICFUNC(StartTouch, pEntity, OnPostStartTouch, true);
78+
SH_ADD_MANUALHOOK_STATICFUNC(Touch, pEntity, OnTouch, false);
79+
SH_ADD_MANUALHOOK_STATICFUNC(Touch, pEntity, OnPostTouch, true);
80+
SH_ADD_MANUALHOOK_STATICFUNC(EndTouch, pEntity, OnEndTouch, false);
81+
SH_ADD_MANUALHOOK_STATICFUNC(EndTouch, pEntity, OnPostEndTouch, true);
82+
SH_ADD_MANUALHOOK_STATICFUNC(EntityUse, pEntity, OnUse, false);
83+
SH_ADD_MANUALHOOK_STATICFUNC(EntityUse, pEntity, OnPostUse, true);
84+
}
6685
}
6786

6887
void CEntityListener::OnEntityDeleted(CEntityInstance* pEntity)
6988
{
89+
if (classNames.find(pEntity->GetClassname()) != classNames.end()) {
90+
SH_REMOVE_MANUALHOOK_STATICFUNC(StartTouch, pEntity, OnStartTouch, false);
91+
SH_REMOVE_MANUALHOOK_STATICFUNC(StartTouch, pEntity, OnPostStartTouch, true);
92+
SH_REMOVE_MANUALHOOK_STATICFUNC(Touch, pEntity, OnTouch, false);
93+
SH_REMOVE_MANUALHOOK_STATICFUNC(Touch, pEntity, OnPostTouch, true);
94+
SH_REMOVE_MANUALHOOK_STATICFUNC(EndTouch, pEntity, OnEndTouch, false);
95+
SH_REMOVE_MANUALHOOK_STATICFUNC(EndTouch, pEntity, OnPostEndTouch, true);
96+
SH_REMOVE_MANUALHOOK_STATICFUNC(EntityUse, pEntity, OnUse, false);
97+
SH_REMOVE_MANUALHOOK_STATICFUNC(EntityUse, pEntity, OnPostUse, true);
98+
}
7099
}
71100

72101
void OnStartTouch(CEntityInstance* ent)
73102
{
74-
if(TriggerEvent("sdktools.ext", "OnEntityStartTouch", { string_format("%p", META_IFACEPTR(CEntityInstance)), "CEntityInstance", string_format("%p", ent), "CEntityInstance" }, tmpret) == EventResult::Stop)
103+
ClassData thisEnt({ { "class_name", "CEntityInstance" }, { "class_ptr", META_IFACEPTR(CEntityInstance) } }, "SDKClass", nullptr);
104+
ClassData otherEnt({ { "class_name", "CEntityInstance" }, { "class_ptr", ent } }, "SDKClass", nullptr);
105+
106+
if (TriggerEvent("sdktools.ext", "OnEntityStartTouch", { &thisEnt, &otherEnt }, tmpret) == EventResult::Stop)
75107
RETURN_META(MRES_SUPERCEDE);
76108

77109
RETURN_META(MRES_IGNORED);
78110
}
79111

80112
void OnPostStartTouch(CEntityInstance* ent)
81113
{
82-
if(TriggerEvent("sdktools.ext", "OnPostEntityStartTouch", { string_format("%p", META_IFACEPTR(CEntityInstance)), "CEntityInstance", string_format("%p", ent), "CEntityInstance" }, tmpret) == EventResult::Stop)
114+
ClassData thisEnt({ { "class_name", "CEntityInstance" }, { "class_ptr", META_IFACEPTR(CEntityInstance) } }, "SDKClass", nullptr);
115+
ClassData otherEnt({ { "class_name", "CEntityInstance" }, { "class_ptr", ent } }, "SDKClass", nullptr);
116+
117+
if (TriggerEvent("sdktools.ext", "OnPostEntityStartTouch", { &thisEnt, &otherEnt }, tmpret) == EventResult::Stop)
83118
RETURN_META(MRES_SUPERCEDE);
84119

85120
RETURN_META(MRES_IGNORED);
86121
}
87122

88123
void OnTouch(CEntityInstance* ent)
89124
{
90-
if(TriggerEvent("sdktools.ext", "OnEntityTouching", { string_format("%p", META_IFACEPTR(CEntityInstance)), "CEntityInstance", string_format("%p", ent), "CEntityInstance" }, tmpret) == EventResult::Stop)
125+
ClassData thisEnt({ { "class_name", "CEntityInstance" }, { "class_ptr", META_IFACEPTR(CEntityInstance) } }, "SDKClass", nullptr);
126+
ClassData otherEnt({ { "class_name", "CEntityInstance" }, { "class_ptr", ent } }, "SDKClass", nullptr);
127+
128+
if (TriggerEvent("sdktools.ext", "OnEntityTouching", { &thisEnt, &otherEnt }, tmpret) == EventResult::Stop)
91129
RETURN_META(MRES_SUPERCEDE);
92130

93131
RETURN_META(MRES_IGNORED);
94132
}
95133

96134
void OnPostTouch(CEntityInstance* ent)
97135
{
98-
if(TriggerEvent("sdktools.ext", "OnPostEntityTouching", { string_format("%p", META_IFACEPTR(CEntityInstance)), "CEntityInstance", string_format("%p", ent), "CEntityInstance" }, tmpret) == EventResult::Stop)
136+
ClassData thisEnt({ { "class_name", "CEntityInstance" }, { "class_ptr", META_IFACEPTR(CEntityInstance) } }, "SDKClass", nullptr);
137+
ClassData otherEnt({ { "class_name", "CEntityInstance" }, { "class_ptr", ent } }, "SDKClass", nullptr);
138+
139+
if (TriggerEvent("sdktools.ext", "OnPostEntityTouching", { &thisEnt, &otherEnt }, tmpret) == EventResult::Stop)
99140
RETURN_META(MRES_SUPERCEDE);
100141

101142
RETURN_META(MRES_IGNORED);
102143
}
103144

104145
void OnEndTouch(CEntityInstance* ent)
105146
{
106-
if(TriggerEvent("sdktools.ext", "OnEntityStopTouch", { string_format("%p", META_IFACEPTR(CEntityInstance)), "CEntityInstance", string_format("%p", ent), "CEntityInstance" }, tmpret) == EventResult::Stop)
147+
ClassData thisEnt({ { "class_name", "CEntityInstance" }, { "class_ptr", META_IFACEPTR(CEntityInstance) } }, "SDKClass", nullptr);
148+
ClassData otherEnt({ { "class_name", "CEntityInstance" }, { "class_ptr", ent } }, "SDKClass", nullptr);
149+
150+
if (TriggerEvent("sdktools.ext", "OnEntityStopTouch", { &thisEnt, &otherEnt }, tmpret) == EventResult::Stop)
107151
RETURN_META(MRES_SUPERCEDE);
108152

109153
RETURN_META(MRES_IGNORED);
110154
}
111155

112156
void OnPostEndTouch(CEntityInstance* ent)
113157
{
114-
if(TriggerEvent("sdktools.ext", "OnPostEntityStopTouch", { string_format("%p", META_IFACEPTR(CEntityInstance)), "CEntityInstance", string_format("%p", ent), "CEntityInstance" }, tmpret) == EventResult::Stop)
158+
ClassData thisEnt({ { "class_name", "CEntityInstance" }, { "class_ptr", META_IFACEPTR(CEntityInstance) } }, "SDKClass", nullptr);
159+
ClassData otherEnt({ { "class_name", "CEntityInstance" }, { "class_ptr", ent } }, "SDKClass", nullptr);
160+
161+
if (TriggerEvent("sdktools.ext", "OnPostEntityStopTouch", { &thisEnt, &otherEnt }, tmpret) == EventResult::Stop)
115162
RETURN_META(MRES_SUPERCEDE);
116163

117164
RETURN_META(MRES_IGNORED);
118165
}
119166

120167
void OnUse(class InputData_t* ent)
121168
{
122-
if(TriggerEvent("sdktools.ext", "OnEntityUse", { string_format("%p", META_IFACEPTR(CEntityInstance)), "CEntityInstance", string_format("%p", ent->pActivator), "CEntityInstance", string_format("%p", ent->pCaller), "CEntityInstance", ent->nOutputID }, tmpret) == EventResult::Stop)
169+
ClassData thisEnt({ { "class_name", "CEntityInstance" }, { "class_ptr", META_IFACEPTR(CEntityInstance) } }, "SDKClass", nullptr);
170+
ClassData activator({ { "class_name", "CEntityInstance" }, { "class_ptr", ent->pActivator } }, "SDKClass", nullptr);
171+
ClassData caller({ { "class_name", "CEntityInstance" }, { "class_ptr", ent->pCaller } }, "SDKClass", nullptr);
172+
173+
if (TriggerEvent("sdktools.ext", "OnEntityUse", { &thisEnt, &activator, &caller, ent->nOutputID }, tmpret) == EventResult::Stop)
123174
RETURN_META(MRES_SUPERCEDE);
124175

125176
RETURN_META(MRES_IGNORED);
126177
}
127178

128179
void OnPostUse(class InputData_t* ent)
129180
{
130-
if(TriggerEvent("sdktools.ext", "OnPostEntityUse", { string_format("%p", META_IFACEPTR(CEntityInstance)), "CEntityInstance", string_format("%p", ent->pActivator), "CEntityInstance", string_format("%p", ent->pCaller), "CEntityInstance", ent->nOutputID }, tmpret) == EventResult::Stop)
181+
ClassData thisEnt({ { "class_name", "CEntityInstance" }, { "class_ptr", META_IFACEPTR(CEntityInstance) } }, "SDKClass", nullptr);
182+
ClassData activator({ { "class_name", "CEntityInstance" }, { "class_ptr", ent->pActivator } }, "SDKClass", nullptr);
183+
ClassData caller({ { "class_name", "CEntityInstance" }, { "class_ptr", ent->pCaller } }, "SDKClass", nullptr);
184+
185+
if (TriggerEvent("sdktools.ext", "OnPostEntityUse", { &thisEnt, &activator, &caller, ent->nOutputID }, tmpret) == EventResult::Stop)
131186
RETURN_META(MRES_SUPERCEDE);
132187

133188
RETURN_META(MRES_IGNORED);

src/entrypoint.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "entities.h"
33
#include "raytrace.h"
44

5-
#include <Embedder.h>
5+
#include <embedder/src/Embedder.h>
66

77
//////////////////////////////////////////////////////////////
88
///////////////// Core Variables //////////////
@@ -13,44 +13,40 @@ SH_DECL_HOOK3_void(INetworkServerService, StartupServer, SH_NOATTRIB, 0, const G
1313
SDKTools g_Ext;
1414
EntityListener g_entListener;
1515
IVPhysics2* g_Physics = nullptr;
16-
CUtlVector<FuncHookBase *> g_vecHooks;
1716
CREATE_GLOBALVARS();
1817

19-
void TraceShapeHook(IVPhysics2* _this, Ray_t& ray, Vector& start, Vector& end, CTraceFilter* filter, trace_t* trace);
20-
FuncHook<decltype(TraceShapeHook)> TTraceShape(TraceShapeHook, "TraceShape");
18+
dyno::ReturnAction TraceShapeHook(dyno::CallbackType cbType, dyno::IHook& hook);
19+
FunctionHook TraceShape("TraceShape", dyno::CallbackType::Pre, TraceShapeHook, "pppppppppppp", 'b');
2120

2221
//////////////////////////////////////////////////////////////
2322
///////////////// Core Class //////////////
2423
////////////////////////////////////////////////////////////
2524

26-
void TraceShapeHook(IVPhysics2* _this, Ray_t& ray, Vector& start, Vector& end, CTraceFilter* filter, trace_t* trace)
25+
dyno::ReturnAction TraceShapeHook(dyno::CallbackType cbType, dyno::IHook& hook)
2726
{
28-
if(g_Physics == nullptr) {
27+
IVPhysics2* _this = hook.getArgument<IVPhysics2*>(0);
28+
29+
if (g_Physics == nullptr) {
2930
g_Physics = _this;
30-
TTraceShape(_this, ray, start, end, filter, trace);
31-
TTraceShape.Disable();
31+
TraceShape.Disable();
3232
}
33+
34+
return dyno::ReturnAction::Ignored;
3335
}
3436

3537
EXT_EXPOSE(g_Ext);
36-
bool SDKTools::Load(std::string& error, SourceHook::ISourceHook *SHPtr, ISmmAPI* ismm, bool late)
38+
bool SDKTools::Load(std::string& error, SourceHook::ISourceHook* SHPtr, ISmmAPI* ismm, bool late)
3739
{
3840
SAVE_GLOBALVARS();
39-
40-
GET_IFACE_ANY(GetEngineFactory, g_pNetworkServerService, INetworkServerService, NETWORKSERVERSERVICE_INTERFACE_VERSION);
4141

42-
if(!InitializeHooks()) {
43-
error = "Failed to initialize hooks.";
44-
return false;
45-
}
42+
GET_IFACE_ANY(GetEngineFactory, g_pNetworkServerService, INetworkServerService, NETWORKSERVERSERVICE_INTERFACE_VERSION);
4643

4744
g_entListener.Initialize();
4845
return true;
4946
}
5047

5148
bool SDKTools::Unload(std::string& error)
5249
{
53-
UnloadHooks();
5450
g_entListener.Destroy();
5551
return true;
5652
}
@@ -66,11 +62,13 @@ void SDKTools::AllPluginsLoaded()
6662
}
6763

6864
void RayTrace_OnPluginLoad(EContext* ctx, std::string plugin_name);
65+
void entities_SetupScripting(EContext* ctx);
6966

7067
bool SDKTools::OnPluginLoad(std::string pluginName, void* pluginState, PluginKind_t kind, std::string& error)
7168
{
7269
EContext* ctx = (EContext*)pluginState;
7370

71+
entities_SetupScripting(ctx);
7472
RayTrace_OnPluginLoad(ctx, pluginName);
7573

7674
return true;

0 commit comments

Comments
 (0)