This repository was archived by the owner on Mar 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCPlugin.cpp
More file actions
203 lines (164 loc) · 6.94 KB
/
Copy pathCPlugin.cpp
File metadata and controls
203 lines (164 loc) · 6.94 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
QMM - Q3 MultiMod
Copyright 2004-2024
https://github.com/thecybermind/qmm/
3-clause BSD license: https://opensource.org/license/bsd-3-clause
Created By:
Kevin Masterson < cybermind@gmail.com >
*/
#include "CPlugin.h"
#include "qmm.h"
#include "util.h"
CPlugin::CPlugin() {
this->QMM_Query = NULL;
this->QMM_Attach = NULL;
this->QMM_Detach = NULL;
this->QMM_vmMain = NULL;
this->QMM_vmMain_Post = NULL;
this->QMM_syscall = NULL;
this->QMM_syscall_Post = NULL;
this->plugininfo = NULL;
this->paused = 0;
this->iscmd = 0;
}
CPlugin::~CPlugin() {
this->dll.Unload();
}
//load the given file, and call the QMM_Query function
//if the plugin is not able to be loaded at runtime and iscmd==1, cancel
// - file is the path relative to the mod directory
// - iscmd determines if the file was loaded via server command (or from config file)
int CPlugin::LoadQuery(const char* file, int iscmd) {
if (!file || !*file)
return 0;
//load DLL (prepend special homepath)
int x = this->dll.Load(vaf("%s%s/%s", g_EngineMgr->GetHomepath(), g_EngineMgr->GetModDir(), file));
if (!x) {
ENG_SYSCALL(ENG_MSG(QMM_G_PRINT), vaf("[QMM] ERROR: CPlugin::LoadQuery(\"%s\"): DLL load failed for plugin: %s\n", file, dlerror()));
return 0;
} else if (x == -1) {
ENG_SYSCALL(ENG_MSG(QMM_G_PRINT), vaf("[QMM] ERROR: CPlugin::LoadQuery(\"%s\"): DLL load failed for plugin: module already loaded\n", file));
return 0;
}
//find QMM_Query() or fail
if ((this->QMM_Query = (plugin_query)this->dll.GetProc("QMM_Query")) == NULL) {
ENG_SYSCALL(ENG_MSG(QMM_G_PRINT), vaf("[QMM] ERROR: CPlugin::LoadQuery(\"%s\"): Unable to find \"QMM_Query\" function in plugin\n", file));
return 0;
}
//call QMM_Query() func to get the plugininfo
(this->QMM_Query)(&(this->plugininfo));
if (!this->plugininfo) {
ENG_SYSCALL(ENG_MSG(QMM_G_PRINT), vaf("[QMM] ERROR: CPlugin::LoadQuery(\"%s\"): Plugininfo NULL for plugin", file));
return 0;
}
//check for plugin interface versions
//if the plugin's major version is higher, don't load and suggest to upgrade QMM
if (this->plugininfo->pifv_major > QMM_PIFV_MAJOR) {
ENG_SYSCALL(ENG_MSG(QMM_G_PRINT), vaf("[QMM] ERROR: CPlugin::LoadQuery(\"%s\"): Plugin's major interface version (%d) is greater than QMM's (%d), suggest upgrading QMM.\n", file, this->plugininfo->pifv_major, QMM_PIFV_MAJOR));
return 0;
}
//if the plugin's major version is lower, don't load and suggest to upgrade plugin
if (this->plugininfo->pifv_major < QMM_PIFV_MAJOR) {
ENG_SYSCALL(ENG_MSG(QMM_G_PRINT), vaf("[QMM] ERROR: CPlugin::LoadQuery(\"%s\"): Plugin's major interface version (%d) is less than QMM's (%d), suggest upgrading plugin.\n", file, this->plugininfo->pifv_major, QMM_PIFV_MAJOR));
return 0;
}
//if the plugin's minor version is higher, don't load and suggest to upgrade QMM
if (this->plugininfo->pifv_minor > QMM_PIFV_MINOR) {
ENG_SYSCALL(ENG_MSG(QMM_G_PRINT), vaf("[QMM] ERROR: CPlugin::LoadQuery(\"%s\"): Plugin's minor interface version (%d) is greater than QMM's (%d), suggest upgrading QMM.\n", file, this->plugininfo->pifv_minor, QMM_PIFV_MINOR));
return 0;
}
//if the plugin's minor version is lower, load, but suggest to upgrade plugin anyway
if (this->plugininfo->pifv_minor < QMM_PIFV_MINOR)
ENG_SYSCALL(ENG_MSG(QMM_G_PRINT), vaf("[QMM] WARNING: CPlugin::LoadQuery(\"%s\"): Plugin's minor interface version (%d) is less than QMM's (%d), suggest upgrading plugin.\n", file, this->plugininfo->pifv_minor, QMM_PIFV_MINOR));
//if this is loaded via a command, but the plugin does not allow it, fail
if (iscmd && !this->plugininfo->loadcmd) {
ENG_SYSCALL(ENG_MSG(QMM_G_PRINT), vaf("[QMM] ERROR: CPlugin::LoadQuery(\"%s\"): Plugin cannot be loaded via command\n", file));
return 0;
}
//find remaining neccesary functions or fail
if ((this->QMM_Attach = (plugin_attach)this->dll.GetProc("QMM_Attach")) == NULL) {
ENG_SYSCALL(ENG_MSG(QMM_G_PRINT), vaf("[QMM] ERROR: CPlugin::LoadQuery(\"%s\"): Unable to find \"QMM_Attach\" function in plugin\n", file));
return 0;
}
if ((this->QMM_Detach = (plugin_detach)this->dll.GetProc("QMM_Detach")) == NULL) {
ENG_SYSCALL(ENG_MSG(QMM_G_PRINT), vaf("[QMM] ERROR: CPlugin::LoadQuery(\"%s\"): Unable to find \"QMM_Detach\" function in plugin\n", file));
return 0;
}
if ((this->QMM_vmMain = (plugin_vmmain)this->dll.GetProc("QMM_vmMain")) == NULL) {
ENG_SYSCALL(ENG_MSG(QMM_G_PRINT), vaf("[QMM] ERROR: CPlugin::LoadQuery(\"%s\"): Unable to find \"QMM_vmMain\" function in plugin\n", file));
return 0;
}
if ((this->QMM_syscall = (plugin_syscall)this->dll.GetProc("QMM_syscall")) == NULL) {
ENG_SYSCALL(ENG_MSG(QMM_G_PRINT), vaf("[QMM] ERROR: CPlugin::LoadQuery(\"%s\"): Unable to find \"QMM_syscall\" function in plugin\n", file));
return 0;
}
if ((this->QMM_vmMain_Post = (plugin_vmmain)this->dll.GetProc("QMM_vmMain_Post")) == NULL) {
ENG_SYSCALL(ENG_MSG(QMM_G_PRINT), vaf("[QMM] ERROR: CPlugin::LoadQuery(\"%s\"): Unable to find \"QMM_vmMain_Post\" function in plugin\n", file));
return 0;
}
if ((this->QMM_syscall_Post = (plugin_syscall)this->dll.GetProc("QMM_syscall_Post")) == NULL) {
ENG_SYSCALL(ENG_MSG(QMM_G_PRINT), vaf("[QMM] ERROR: CPlugin::LoadQuery(\"%s\"): Unable to find \"QMM_syscall_Post\" function in plugin\n", file));
return 0;
}
return 1;
}
//call plugin's QMM_Attach() function, pass real engine syscall, mod vmMain, and iscmd
int CPlugin::Attach(eng_syscall_t eng_syscall, mod_vmMain_t mod_vmMain, pluginfuncs_t* pluginfuncs, int vmbase, int iscmd) {
//store iscmd for "qmm info"
this->iscmd = iscmd;
//call QMM_Attach() func with the engine syscall, mod vmMain, result ptr, and iscmd
return (this->QMM_Attach)(eng_syscall, mod_vmMain, &this->result, pluginfuncs, vmbase, iscmd);
}
//pause plugin if it allows
int CPlugin::Pause() {
if (!this->plugininfo->canpause || this->paused)
return 0;
this->paused = 1;
return 1;
}
//unpause plugin
int CPlugin::Unpause() {
if (!this->paused)
return 0;
this->paused = 0;
return 1;
}
//unload plugin (with iscmd)
int CPlugin::Unload(int iscmd) {
if (iscmd && (this->plugininfo && !this->plugininfo->unloadcmd))
return 0;
//call QMM_Detach() with iscmd
if (this->QMM_Detach)
(this->QMM_Detach)(iscmd);
//unload DLL
this->dll.Unload();
return 1;
}
int CPlugin::Paused() {
return this->paused;
}
int CPlugin::IsCmd() {
return this->iscmd;
}
plugin_vmmain CPlugin::vmMain() {
return this->QMM_vmMain;
}
plugin_vmmain CPlugin::vmMain_Post() {
return this->QMM_vmMain_Post;
}
plugin_syscall CPlugin::syscall() {
return this->QMM_syscall;
}
plugin_syscall CPlugin::syscall_Post() {
return this->QMM_syscall_Post;
}
const plugininfo_t* CPlugin::PluginInfo() {
return this->plugininfo;
}
pluginres_t CPlugin::Result() {
return this->result;
}
void CPlugin::ResetResult() {
this->result = QMM_UNUSED;
}
pluginres_t CPlugin::result = QMM_UNUSED;