Skip to content

Commit 55dd81b

Browse files
committed
update(src): "small changes"
1 parent 22fdd0b commit 55dd81b

17 files changed

+124
-948
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",

AMBuilder

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ for sdk_target in MMSPlugin.sdk_targets:
7272
"-fexceptions",
7373
]
7474
binary.compiler.postlink += [
75-
os.path.join(builder.sourcePath, 'vendor', 'funchook', 'lib', 'libfunchook.a'),
76-
os.path.join(builder.sourcePath, 'vendor', 'funchook', 'lib', 'libdistorm.a'),
77-
7875
"-lgnutls",
7976
"-lz",
8077
"-lpthread",
@@ -111,12 +108,11 @@ for sdk_target in MMSPlugin.sdk_targets:
111108
"/TP",
112109
"/MT",
113110
"/W3",
114-
"/Z7"
111+
"/Z7",
112+
"/EHsc"
115113
]
116114
binary.compiler.postlink += [
117-
os.path.join(builder.sourcePath, 'vendor', 'funchook', 'lib', 'funchook.lib'),
118-
os.path.join(builder.sourcePath, 'vendor', 'funchook', 'lib', 'distorm.lib'),
119-
"psapi.lib",
115+
"psapi.lib",
120116
"winmm.lib",
121117
"ws2_32.lib",
122118
"wldap32.lib",
@@ -173,6 +169,8 @@ for sdk_target in MMSPlugin.sdk_targets:
173169
binary.compiler.cxxincludes += [
174170
os.path.join(builder.sourcePath, 'vendor'),
175171
os.path.join(builder.sourcePath, 'vendor', 'embedder', 'src'),
172+
os.path.join(builder.sourcePath, 'vendor', 'swiftly-ext', 'hooks'),
173+
os.path.join(builder.sourcePath, 'vendor', 'swiftly-ext', 'hooks', 'dynohook'),
176174

177175
os.path.join(sdk['path']),
178176
os.path.join(sdk['path'], 'public'),

src/entrypoint.cpp

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,27 @@
11
#include "entrypoint.h"
2-
#include <Embedder.h>
2+
#include <embedder/src/embedder.h>
33
#include "ipclass.h"
44

55
//////////////////////////////////////////////////////////////
66
///////////////// Core Variables //////////////
77
////////////////////////////////////////////////////////////
88

99
IPExtension g_Ext;
10-
CUtlVector<FuncHookBase *> g_vecHooks;
1110
CREATE_GLOBALVARS();
1211

1312
//////////////////////////////////////////////////////////////
1413
///////////////// Core Class //////////////
1514
////////////////////////////////////////////////////////////
1615

1716
EXT_EXPOSE(g_Ext);
18-
bool IPExtension::Load(std::string& error, SourceHook::ISourceHook *SHPtr, ISmmAPI* ismm, bool late)
17+
bool IPExtension::Load(std::string& error, SourceHook::ISourceHook* SHPtr, ISmmAPI* ismm, bool late)
1918
{
2019
SAVE_GLOBALVARS();
21-
if(!InitializeHooks()) {
22-
error = "Failed to initialize hooks.";
23-
return false;
24-
}
25-
2620
return true;
2721
}
2822

2923
bool IPExtension::Unload(std::string& error)
3024
{
31-
UnloadHooks();
3225
return true;
3326
}
3427

@@ -45,29 +38,13 @@ bool IPExtension::OnPluginLoad(std::string pluginName, void* pluginState, Plugin
4538
{
4639
EContext* state = (EContext*)pluginState;
4740

48-
BeginClass<PluginIPAPI>("IPAPI", state)
49-
.addConstructor<std::string>()
50-
.addFunction("GetIsoCode", &PluginIPAPI::GetIsoCode)
51-
.addFunction("GetContinent", &PluginIPAPI::GetContinent)
52-
.addFunction("GetCountry", &PluginIPAPI::GetCountry)
53-
.addFunction("GetRegion", &PluginIPAPI::GetRegion)
54-
.addFunction("GetCity", &PluginIPAPI::GetCity)
55-
.addFunction("GetTimezone", &PluginIPAPI::GetTimezone)
56-
.addFunction("GetLatitude", &PluginIPAPI::GetLatitude)
57-
.addFunction("GetLongitude", &PluginIPAPI::GetLongitude)
58-
.addFunction("GetASN", &PluginIPAPI::GetASN)
59-
.endClass();
60-
61-
GetGlobalNamespace(state).addConstant("ip", PluginIPAPI(pluginName));
41+
SetupScripting(state);
6242

6343
return true;
6444
}
6545

6646
bool IPExtension::OnPluginUnload(std::string pluginName, void* pluginState, PluginKind_t kind, std::string& error)
6747
{
68-
EContext* state = (EContext*)pluginState;
69-
70-
GetGlobalNamespace(state).addConstant("ip", nullptr);
7148
return true;
7249
}
7350

src/entrypoint.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55

66
#include <swiftly-ext/core.h>
77
#include <swiftly-ext/extension.h>
8-
#include <swiftly-ext/hooks/NativeHooks.h>
8+
#include <swiftly-ext/hooks/function.h>
9+
#include <swiftly-ext/hooks/vfunction.h>
910

1011
class IPExtension : public SwiftlyExt
1112
{
1213
public:
13-
bool Load(std::string& error, SourceHook::ISourceHook *SHPtr, ISmmAPI* ismm, bool late);
14+
bool Load(std::string& error, SourceHook::ISourceHook* SHPtr, ISmmAPI* ismm, bool late);
1415
bool Unload(std::string& error);
15-
16+
1617
void AllExtensionsLoaded();
1718
void AllPluginsLoaded();
1819

src/ipclass.cpp

Lines changed: 100 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -2,103 +2,116 @@
22
#include "entrypoint.h"
33
#include <geolite2pp/GeoLite2PP.hpp>
44
#include <swiftly-ext/files.h>
5+
#include <string>
56

6-
GeoLite2PP::DB *maxminddbCity = nullptr;
7-
GeoLite2PP::DB *maxminddbASN = nullptr;
7+
GeoLite2PP::DB* maxminddbCity = nullptr;
8+
GeoLite2PP::DB* maxminddbASN = nullptr;
89

9-
PluginIPAPI::PluginIPAPI(std::string m_plugin_name)
10+
void SetupScripting(EContext* ctx)
1011
{
11-
this->plugin_name = m_plugin_name;
12-
if(!maxminddbCity) maxminddbCity = new GeoLite2PP::DB(GeneratePath("addons/swiftly/data/GeoLite2-City.mmdb"));
13-
if(!maxminddbASN) maxminddbASN = new GeoLite2PP::DB(GeneratePath("addons/swiftly/data/GeoLite2-ASN.mmdb"));
14-
}
12+
if (!maxminddbCity) maxminddbCity = new GeoLite2PP::DB(GeneratePath("addons/swiftly/data/GeoLite2-City.mmdb"));
13+
if (!maxminddbASN) maxminddbASN = new GeoLite2PP::DB(GeneratePath("addons/swiftly/data/GeoLite2-ASN.mmdb"));
1514

16-
std::string PluginIPAPI::GetIsoCode(std::string ip)
17-
{
18-
try {
19-
auto m = maxminddbCity->get_all_fields(ip);
20-
return m["country_iso_code"];
21-
} catch(std::exception& e) {
22-
return "";
23-
}
24-
}
15+
ADD_CLASS("IPAPI");
2516

26-
std::string PluginIPAPI::GetContinent(std::string ip)
27-
{
28-
try {
29-
auto m = maxminddbCity->get_all_fields(ip);
30-
return m["continent"];
31-
} catch(std::exception& e) {
32-
return "";
33-
}
34-
}
17+
ADD_CLASS_FUNCTION("IPAPI", "GetIsoCode", [](FunctionContext* context, ClassData* data) -> void {
18+
std::string ip = context->GetArgumentOr<std::string>(0, "0.0.0.0");
19+
try {
20+
auto m = maxminddbCity->get_all_fields(ip);
21+
context->SetReturn(m["country_iso_code"]);
22+
}
23+
catch (std::exception& e) {
24+
context->SetReturn("");
25+
}
26+
});
3527

36-
std::string PluginIPAPI::GetCountry(std::string ip)
37-
{
38-
try {
39-
auto m = maxminddbCity->get_all_fields(ip);
40-
return m["country"];
41-
} catch(std::exception& e) {
42-
return "";
43-
}
44-
}
28+
ADD_CLASS_FUNCTION("IPAPI", "GetContinent", [](FunctionContext* context, ClassData* data) -> void {
29+
std::string ip = context->GetArgumentOr<std::string>(0, "0.0.0.0");
30+
try {
31+
auto m = maxminddbCity->get_all_fields(ip);
32+
context->SetReturn(m["continent"]);
33+
}
34+
catch (std::exception& e) {
35+
context->SetReturn("");
36+
}
37+
});
4538

46-
std::string PluginIPAPI::GetRegion(std::string ip)
47-
{
48-
try {
49-
auto m = maxminddbCity->get_all_fields(ip);
50-
return m["subdivision"];
51-
} catch(std::exception& e) {
52-
return "";
53-
}
54-
}
39+
ADD_CLASS_FUNCTION("IPAPI", "GetCountry", [](FunctionContext* context, ClassData* data) -> void {
40+
std::string ip = context->GetArgumentOr<std::string>(0, "0.0.0.0");
41+
try {
42+
auto m = maxminddbCity->get_all_fields(ip);
43+
context->SetReturn(m["country"]);
44+
}
45+
catch (std::exception& e) {
46+
context->SetReturn("");
47+
}
48+
});
5549

56-
std::string PluginIPAPI::GetCity(std::string ip)
57-
{
58-
try {
59-
auto m = maxminddbCity->get_all_fields(ip);
60-
return m["city"];
61-
} catch(std::exception& e) {
62-
return "";
63-
}
64-
}
50+
ADD_CLASS_FUNCTION("IPAPI", "GetRegion", [](FunctionContext* context, ClassData* data) -> void {
51+
std::string ip = context->GetArgumentOr<std::string>(0, "0.0.0.0");
52+
try {
53+
auto m = maxminddbCity->get_all_fields(ip);
54+
context->SetReturn(m["subdivision"]);
55+
}
56+
catch (std::exception& e) {
57+
context->SetReturn("");
58+
}
59+
});
6560

66-
std::string PluginIPAPI::GetTimezone(std::string ip)
67-
{
68-
try {
69-
auto m = maxminddbCity->get_all_fields(ip);
70-
return m["time_zone"];
71-
} catch(std::exception& e) {
72-
return "";
73-
}
74-
}
61+
ADD_CLASS_FUNCTION("IPAPI", "GetCity", [](FunctionContext* context, ClassData* data) -> void {
62+
std::string ip = context->GetArgumentOr<std::string>(0, "0.0.0.0");
63+
try {
64+
auto m = maxminddbCity->get_all_fields(ip);
65+
context->SetReturn(m["city"]);
66+
}
67+
catch (std::exception& e) {
68+
context->SetReturn("");
69+
}
70+
});
7571

76-
double PluginIPAPI::GetLatitude(std::string ip)
77-
{
78-
try {
79-
auto m = maxminddbCity->get_all_fields(ip);
80-
return V_StringToFloat32(m["latitude"].c_str(), 0.0f);
81-
} catch(std::exception& e) {
82-
return 0.0f;
83-
}
84-
}
72+
ADD_CLASS_FUNCTION("IPAPI", "GetTimezone", [](FunctionContext* context, ClassData* data) -> void {
73+
std::string ip = context->GetArgumentOr<std::string>(0, "0.0.0.0");
74+
try {
75+
auto m = maxminddbCity->get_all_fields(ip);
76+
context->SetReturn(m["time_zone"]);
77+
}
78+
catch (std::exception& e) {
79+
context->SetReturn("");
80+
}
81+
});
8582

86-
double PluginIPAPI::GetLongitude(std::string ip)
87-
{
88-
try {
89-
auto m = maxminddbCity->get_all_fields(ip);
90-
return V_StringToFloat32(m["longitude"].c_str(), 0.0f);
91-
} catch(std::exception& e) {
92-
return 0.0f;
93-
}
94-
}
83+
ADD_CLASS_FUNCTION("IPAPI", "GetLatitude", [](FunctionContext* context, ClassData* data) -> void {
84+
std::string ip = context->GetArgumentOr<std::string>(0, "0.0.0.0");
85+
try {
86+
auto m = maxminddbCity->get_all_fields(ip);
87+
context->SetReturn(V_StringToFloat32(m["latitude"].c_str(), 0.0f));
88+
}
89+
catch (std::exception& e) {
90+
context->SetReturn(0.0f);
91+
}
92+
});
9593

96-
std::string PluginIPAPI::GetASN(std::string ip)
97-
{
98-
try {
99-
auto lookupRes = maxminddbASN->get_field(ip, "en", GeoLite2PP::VCStr{"autonomous_system_number", nullptr});
100-
return lookupRes;
101-
} catch(std::exception& e) {
102-
return "";
103-
}
94+
ADD_CLASS_FUNCTION("IPAPI", "GetLongitude", [](FunctionContext* context, ClassData* data) -> void {
95+
std::string ip = context->GetArgumentOr<std::string>(0, "0.0.0.0");
96+
try {
97+
auto m = maxminddbCity->get_all_fields(ip);
98+
context->SetReturn(V_StringToFloat32(m["longitude"].c_str(), 0.0f));
99+
}
100+
catch (std::exception& e) {
101+
context->SetReturn(0.0f);
102+
}
103+
});
104+
105+
ADD_CLASS_FUNCTION("IPAPI", "GetASN", [](FunctionContext* context, ClassData* data) -> void {
106+
std::string ip = context->GetArgumentOr<std::string>(0, "0.0.0.0");
107+
try {
108+
auto lookupRes = maxminddbASN->get_field(ip, "en", GeoLite2PP::VCStr{ "autonomous_system_number", nullptr });
109+
context->SetReturn(lookupRes);
110+
}
111+
catch (std::exception& e) {
112+
context->SetReturn("");
113+
}
114+
});
115+
116+
ADD_VARIABLE("_G", "ip", MAKE_CLASS_INSTANCE_CTX(ctx, "IPAPI", {}));
104117
}

src/ipclass.h

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,8 @@
11
#ifndef _ipclass_h
22
#define _ipclass_h
33

4-
#include <string>
4+
#include <embedder/src/embedder.h>
55

6-
class PluginIPAPI
7-
{
8-
private:
9-
std::string plugin_name;
10-
11-
public:
12-
PluginIPAPI(std::string m_plugin_name);
13-
14-
std::string GetIsoCode(std::string ip);
15-
std::string GetContinent(std::string ip);
16-
std::string GetCountry(std::string ip);
17-
std::string GetRegion(std::string ip);
18-
std::string GetCity(std::string ip);
19-
std::string GetTimezone(std::string ip);
20-
double GetLatitude(std::string ip);
21-
double GetLongitude(std::string ip);
22-
std::string GetASN(std::string ip);
23-
};
6+
void SetupScripting(EContext* ctx);
247

258
#endif

0 commit comments

Comments
 (0)