/
Main.cpp
181 lines (139 loc) · 3.7 KB
/
Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <extdll.h>
#include <meta_api.h>
#include "Main.h"
// #include <string>
// #include <algorithm>
// #include <cctype>
// #include <memory>
// #include <vector>
// #include <string>
#include <thread>
#include <fstream>
using namespace std;
using namespace httplib;
enginefuncs_t g_engfuncs;
globalvars_t *gpGlobals;
C_DLLEXPORT
#ifdef _WIN32
__declspec(naked)
#endif
void GiveFnptrsToDll(enginefuncs_t *pEngFuncs, globalvars_t *pGlobalVars) {
#ifdef _WIN32
__asm
{
push ebp
mov ebp, esp
sub esp, __LOCAL_SIZE
push ebx
push esi
push edi
}
#endif
memcpy(&g_engfuncs, pEngFuncs, sizeof(g_engfuncs));
gpGlobals = pGlobalVars;
#ifdef _WIN32
__asm
{
pop edi
pop esi
pop ebx
mov esp, ebp
pop ebp
ret 8
}
#endif
}
template <typename T>
T Min(T a, T b) {
return (a < b) ? a : b;
}
// size_t GetPluginRelPath(char *path, size_t maxPathLength) {
// const char *pluginAbsPath = GET_PLUGIN_PATH(PLID);
// const char *gamedirAbsPath = GET_GAME_INFO(PLID, GINFO_GAMEDIR);
// const char *pluginRelPath = &pluginAbsPath[0 + 1]; // + slash
// // Find last slash
// const char *searchChar = pluginRelPath + strlen(pluginRelPath) - 1; // go to string end
// while (true) {
// if (*searchChar == '/' || *searchChar == '\\') {
// break; // ok we found it
// }
// searchChar--;
// }
// size_t charsToCopy = Min((size_t)searchChar - (size_t)pluginRelPath + 1, maxPathLength); // with slash
// strncpy(path, pluginRelPath, charsToCopy);
// path[charsToCopy] = '\0';
// return charsToCopy;
// }
void NormalizePath(char *path)
{
for (char *cp = path; *cp; cp++) {
if (isupper(*cp))
*cp = tolower(*cp);
if (*cp == '\\')
*cp = '/';
}
}
plugin_info_t Plugin_info = {
META_INTERFACE_VERSION, // ifvers
"httpd", // name
CPPHTTPLIB_VERSION, // version
"2023.01.01", // date
"Aly4", // author
"https://github.com/yhirose/cpp-httplib", // url
"HTTPD", // logtag, all caps please
PT_ANYTIME, // (when) loadable
PT_ANYTIME, // (when) unloadable
};
meta_globals_t *gpMetaGlobals;
gamedll_funcs_t *gpGamedllFuncs;
mutil_funcs_t *gpMetaUtilFuncs;
C_DLLEXPORT int Meta_Query(char *pchInterfaceVersion, plugin_info_t **pPluginInfo, mutil_funcs_t *pMetaUtilFuncs) {
*pPluginInfo = &Plugin_info;
gpMetaUtilFuncs = pMetaUtilFuncs;
return TRUE;
}
thread* th;
Server* svr;
void StartSRV(){
svr = new Server();
// load config
char g_ExecConfigCmd[PATH_MAX];
const char *pszGameDir = GET_GAME_INFO(PLID, GINFO_GAMEDIR);
const char *pszPluginDir = GET_PLUGIN_PATH(PLID);
char szRelativePath[PATH_MAX];
strncpy(szRelativePath, &pszPluginDir[strlen(pszGameDir) + 1], sizeof(szRelativePath) - 1);
szRelativePath[sizeof(szRelativePath) - 1] = '\0';
NormalizePath(szRelativePath);
char *pos = strrchr(szRelativePath, '/');
if (pos) {
*(pos + 1) = '\0';
}
snprintf(g_ExecConfigCmd, sizeof(g_ExecConfigCmd), "cstrike/%s%s.cfg", szRelativePath, Plugin_info.name);
//SERVER_PRINT(g_ExecConfigCmd);
ifstream file;
file.open(g_ExecConfigCmd);
std::string line;
while (std::getline(file, line)) {
svr->set_mount_point("/", line);
};
file.close();
svr->listen("0.0.0.0", 80);
}
void StopSRV(){
if(svr){
svr->stop();
}
}
C_DLLEXPORT int Meta_Detach(PLUG_LOADTIME now, PL_UNLOAD_REASON reason) {
StopSRV();
return TRUE;
}
C_DLLEXPORT int Meta_Attach(PLUG_LOADTIME now, META_FUNCTIONS *pFunctionTable, meta_globals_t *pMGlobals, gamedll_funcs_t *pGamedllFuncs) {
// pFunctionTable->pfnGetEntityAPI2 = &GetEntityAPI2;
// pFunctionTable->pfnGetEntityAPI2_Post = &GetEntityAPI2_Post;
gpMetaGlobals = pMGlobals;
gpGamedllFuncs = pGamedllFuncs;
th = new thread(StartSRV);
th->detach();
return TRUE;
}