-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathextension.cpp
More file actions
312 lines (250 loc) · 8.43 KB
/
extension.cpp
File metadata and controls
312 lines (250 loc) · 8.43 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/**
* vim: set ts=4 :
* =============================================================================
* SourceMod Sample Extension
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
#include <conio.h>
#include "extension.h"
#include "dirent.h"
#include "SMJS_ClientWrapper.h"
#include "modules/MPlugin.h"
#include "modules/MConsole.h"
#include "modules/MServer.h"
#include "modules/MClient.h"
#include "modules/MGame.h"
#include "modules/MSocket.h"
#include "modules/MEntities.h"
#include "modules/MKeyValue.h"
#include "modules/MDota.h"
#include "modules/MEngine.h"
#include "metamod.h"
#include "SMJS_Interfaces.h"
/**
* @file extension.cpp
* @brief Implement extension code here.
*/
SMJS g_SMJS; /**< Global singleton for extension's main interface */
SMEXT_LINK(&g_SMJS);
bool FindAndRunPlugins();
bool FindAndRunAutoloadPlugins();
bool LoadTrustedList();
void CmdJS(void *pUnknown, const CCommand &command);
void CmdLoadPlugin(void *pUnknown, const CCommand &command);
void CmdUnloadPlugin(void *pUnknown, const CCommand &command);
void CmdReloadPlugin(void *pUnknown, const CCommand &command);
void RegCommand(const char *cmdName, void(*func)(void*, const CCommand&));
ConCommand jsCmd("js", CmdJS, "SourceMod.js");
std::vector<std::string> trustedPlugins;
bool SMJS::SDK_OnLoad(char *error, size_t maxlength, bool late){
sharesys->AddDependency(myself, "sdktools.ext", true, true);
sharesys->AddDependency(myself, "bintools.ext", true, true);
if(!SMJS_LoadConfs(error, maxlength, late)) return false;
v8::V8::Initialize();
SMJS_Init();
icvar->RegisterConCommand(&jsCmd);
RegCommand("js_load", CmdLoadPlugin);
RegCommand("js_unload", CmdUnloadPlugin);
RegCommand("js_reload", CmdReloadPlugin);
SMJS_AddModule(new MPlugin());
SMJS_AddModule(new MConsole());
SMJS_AddModule(new MServer());
SMJS_AddModule(new MEntities());
SMJS_AddModule(new MClient());
SMJS_AddModule(new MGame());
SMJS_AddModule(new MSocket());
SMJS_AddModule(new MKeyValue());
SMJS_AddModule(new MDota());
return true;
}
void SMJS::SDK_OnUnload(){
}
void SMJS::SDK_OnAllLoaded(){
SMJS_InitLateInterfaces();
LoadTrustedList();
FindAndRunAutoloadPlugins();
}
bool SMJS::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlength, bool late){
bool res = SMJS_InitInterfaces(ismm, error, maxlength, late);
if(res){
SMJS_AddModule(new MEngine());
}
return res;
}
bool LoadTrustedList(){
char trustedListPath[512];
smutils->BuildPath(Path_SM, trustedListPath, sizeof(trustedListPath), "plugins.js/trusted.txt");
FILE *trustedListFile = fopen(trustedListPath, "r");
if(trustedListFile == NULL) return false;
char line[256];
bool allSuccess = true;
while(fgets(line, sizeof(line), trustedListFile) != NULL){
int len = strlen(line);
while(len > 0 && (line[len - 1] == '\n' || line[len - 1] == '\r')){
line[len - 1] = '\0'; // Remove newline
--len;
}
trustedPlugins.push_back(std::string(line));
}
fclose(trustedListFile);
return true;
}
bool FindAndRunPlugins(){
char pluginsPath[512];
smutils->BuildPath(Path_SM, pluginsPath, sizeof(pluginsPath), "plugins.js");
DIR *dir;
struct dirent *ent;
if ((dir = opendir(pluginsPath)) != NULL) {
while ((ent = readdir (dir)) != NULL) {
if(strcmp(ent->d_name, "") == 0) continue;
if(strcmp(ent->d_name, ".") == 0) continue;
if(strcmp(ent->d_name, "..") == 0) continue;
if(strcmp(ent->d_name, "disabled") == 0) continue;
LoadPlugin(ent->d_name);
}
closedir(dir);
} else {
smutils->LogError(myself, "Couldn't open \"plugins.js\" folder");
return false;
}
return true;
}
bool FindAndRunAutoloadPlugins(){
char autoloadPath[512];
smutils->BuildPath(Path_SM, autoloadPath, sizeof(autoloadPath), "plugins.js/autoload.txt");
FILE *autoloadFile = fopen(autoloadPath, "r");
if(autoloadFile == NULL) return false;
char line[256];
bool allSuccess = true;
while(fgets(line, sizeof(line), autoloadFile) != NULL){
int len = strlen(line);
while(len > 0 && (line[len - 1] == '\n' || line[len - 1] == '\r')){
line[len - 1] = '\0'; // Remove newline
--len;
}
if(LoadPlugin(line) == NULL) allSuccess = false;
}
fclose(autoloadFile);
return allSuccess;
}
SMJS_Plugin *LoadPlugin(const char *dir){
auto plugin = SMJS_Plugin::GetPluginByDir(dir);
if(plugin != NULL) return plugin;
plugin = new SMJS_Plugin(dir);
char path[512];
smutils->BuildPath(Path_SM, path, sizeof(path), "plugins.js/%s", dir);
plugin->SetDir(dir);
plugin->SetPath(path);
plugin->CheckApi();
for(auto it = trustedPlugins.begin(); it != trustedPlugins.end(); ++it){
if(strcmp(dir, it->c_str()) == 0){
plugin->isSandboxed = false;
break;
}
}
plugin->LoadModules();
if(!plugin->LoadFile("Main.js", true)){
delete plugin;
return NULL;
}
// Late loading
if(smutils->IsMapRunning()){
HandleScope handle_scope(plugin->GetIsolate());
Context::Scope context_scope(plugin->GetContext());
auto hooks = plugin->GetHooks("OnMapStart");
for(auto it = hooks->begin(); it != hooks->end(); ++it){
(*it)->Call(plugin->GetContext()->Global(), 0, NULL);
}
for(int i = 0; i < sizeof(clients) / sizeof(clients[0]); ++i){
if(clients[i] == NULL) continue;
v8::Handle<v8::Value> arg = clients[i]->GetWrapper(plugin);
hooks = plugin->GetHooks("OnClientConnected");
for(auto it = hooks->begin(); it != hooks->end(); ++it){
(*it)->Call(plugin->GetContext()->Global(), 1, &arg);
}
if(clients[i]->inGame){
hooks = plugin->GetHooks("OnClientPutInGame");
for(auto it = hooks->begin(); it != hooks->end(); ++it){
(*it)->Call(plugin->GetContext()->Global(), 1, &arg);
}
}
}
}
return plugin;
}
void CmdJS(void *pUnknown, const CCommand &command){
}
void CmdLoadPlugin(void *pUnknown, const CCommand &command){
if(command.ArgC() != 2){
META_CONPRINT("Usage: js_load [plugin_dir]\n");
return;
}
auto pl = SMJS_Plugin::GetPluginByDir(command.Arg(1));
if(pl != NULL){
META_CONPRINTF("Plugin \"%s\" is already loaded!\n", command.Arg(1));
return;
}
pl = LoadPlugin(command.Arg(1));
if(pl == NULL){
META_CONPRINTF("Plugin \"%s\" failed to load!\n", command.Arg(1));
}else{
META_CONPRINTF("Plugin \"%s\" loaded successfully!\n", command.Arg(1));
}
}
void CmdUnloadPlugin(void *pUnknown, const CCommand &command){
if(command.ArgC() != 2){
META_CONPRINT("Usage: js_unload [plugin_dir]\n");
return;
}
auto pl = SMJS_Plugin::GetPluginByDir(command.Arg(1));
if(pl == NULL){
META_CONPRINTF("Plugin \"%s\" is not loaded!\n", command.Arg(1));
return;
}
delete pl;
META_CONPRINTF("Plugin \"%s\" unloaded successfully!\n", command.Arg(1));
}
void CmdReloadPlugin(void *pUnknown, const CCommand &command){
if(command.ArgC() != 2){
META_CONPRINT("Usage: js_reload [plugin_dir]\n");
return;
}
CmdUnloadPlugin(pUnknown, command);
CmdLoadPlugin(pUnknown, command);
}
void RegCommand(const char *cmdName, void(*func)(void*, const CCommand&)){
ConCommand *conCmd = icvar->FindCommand(cmdName);
if(!conCmd){
conCmd = new ConCommand(cmdName, func);
icvar->RegisterConCommand(conCmd);
/*if(!g_SMAPI->RegisterConCommandBase(g_PLAPI, conCmd)){
smutils->LogError(myself, "Couldn't register command %s", cmdName);
}*/
}else{
smutils->LogError(myself, "Couldn't register command %s", cmdName);
}
}