· 8 years ago · Apr 25, 2018, 06:44 PM
1#include <sourcemod>
2#include <sdktools>
3#include <fpvm_interface>
4#include <multicolors>
5
6#define DATA "3.1"
7
8char sConfig[PLATFORM_MAX_PATH];
9Handle kv, db, array_weapons;
10
11//Spawn Message Cvar
12new Handle:cvarcwmspawnmsg = INVALID_HANDLE;
13
14char client_w[MAXPLAYERS+1];
15int client_id[MAXPLAYERS+1];
16
17Handle menu_cw;
18
19char sql_buffer[3096];
20
21bool ismysql;
22
23public Plugin myinfo =
24{
25 name = "SM FPVMI - Custom Weapons Menu",
26 author = "Franc1sco franug",
27 description = "",
28 version = DATA,
29 url = "http://steamcommunity.com/id/franug"
30}
31
32public OnPluginStart()
33{
34 CreateConVar("sm_customweaponsmenu_version", DATA, "plugin info", FCVAR_PLUGIN|FCVAR_SPONLY|FCVAR_REPLICATED|FCVAR_NOTIFY|FCVAR_DONTRECORD);
35
36 //Spawn Messege
37 cvarcwmspawnmsg = CreateConVar("sm_customweaponsmenu_spawnmsg", "1", "Enable or Disable Spawnmessages");
38
39 RegConsoleCmd("sm_cw", Command_cw);
40 RegAdminCmd("sm_reloadcw", ReloadSkins, ADMFLAG_ROOT);
41
42 LoadTranslations("franug_cwm.phrases");
43 LoadTranslations("common.phrases");
44
45 HookEvent("player_spawn", PlayerSpawn);
46
47 RefreshKV();
48 ComprobarDB(true);
49}
50
51public Action:ReloadSkins(client, args)
52{
53 RefreshKV();
54 ComprobarDB(true);
55 CReplyToCommand(client, "\x04[CW]\x01 %T","Custom Weapons Menu configuration reloaded", client);
56
57 return Plugin_Handled;
58}
59
60ComprobarDB(bool:reconnect = false, String:basedatos[64] = "customweapons")
61{
62 if(reconnect)
63 {
64 if (db != INVALID_HANDLE)
65 {
66 //LogMessage("Reconnecting DB connection");
67 CloseHandle(db);
68 db = INVALID_HANDLE;
69 }
70 }
71 else if (db != INVALID_HANDLE)
72 {
73 return;
74 }
75
76 if (!SQL_CheckConfig( basedatos ))
77 {
78 if(StrEqual(basedatos, "storage-local")) SetFailState("Databases not found");
79 else
80 {
81 //base = "clientprefs";
82 ComprobarDB(true,"storage-local");
83 }
84
85 return;
86 }
87 SQL_TConnect(OnSqlConnect, basedatos);
88}
89
90public OnSqlConnect(Handle:owner, Handle:hndl, const String:error[], any:data)
91{
92 if (hndl == INVALID_HANDLE)
93 {
94 LogError("Database failure: %s", error);
95
96 SetFailState("Databases dont work");
97 }
98 else
99 {
100 db = hndl;
101
102 SQL_GetDriverIdent(SQL_ReadDriver(db), sql_buffer, sizeof(sql_buffer));
103 ismysql = StrEqual(sql_buffer,"mysql", false) ? true : false;
104
105 if (ismysql)
106 {
107 Format(sql_buffer, sizeof(sql_buffer), "CREATE TABLE IF NOT EXISTS `customweapons` (`playername` varchar(128) NOT NULL, `steamid` varchar(32) NOT NULL,`last_accountuse` int(64) NOT NULL, `id` INT( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY)");
108
109 SQL_TQuery(db, tbasicoC, sql_buffer);
110
111 }
112 else
113 {
114 Format(sql_buffer, sizeof(sql_buffer), "CREATE TABLE IF NOT EXISTS customweapons (playername varchar(128) NOT NULL, steamid varchar(32) NOT NULL,last_accountuse int(64) NOT NULL, id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL)");
115
116 SQL_TQuery(db, tbasicoC, sql_buffer);
117 }
118 }
119}
120
121public OnMapStart()
122{
123 Downloads();
124
125}
126
127//Show Spawn Messege
128public Action:PlayerSpawn(Handle:event, const String:name[], bool:dontBroadcast)
129{
130 // Get Client
131 new client = GetClientOfUserId(GetEventInt(event, "userid"));
132
133 if (GetClientTeam(client) == 1 && !IsPlayerAlive(client))
134 {
135 return;
136 }
137
138 // Check Convar & Spawnmsg
139 if (GetConVarInt(cvarcwmspawnmsg) == 1)
140 {
141 CPrintToChat(client," \x04[CW]\x01 %T","spawnmsg", client);
142 }
143}
144
145public void RefreshKV()
146{
147 BuildPath(Path_SM, sConfig, PLATFORM_MAX_PATH, "configs/franug_cwm/configuration.txt");
148
149 if(kv != INVALID_HANDLE) CloseHandle(kv);
150
151 kv = CreateKeyValues("CustomModels");
152 FileToKeyValues(kv, sConfig);
153}
154
155Downloads()
156{
157 decl String:imFile[PLATFORM_MAX_PATH];
158 decl String:line[192];
159
160 BuildPath(Path_SM, imFile, sizeof(imFile), "configs/franug_cwm/downloads.txt");
161
162 new Handle:file = OpenFile(imFile, "r");
163
164 if(file != INVALID_HANDLE)
165 {
166 while (!IsEndOfFile(file))
167 {
168 if (!ReadFileLine(file, line, sizeof(line)))
169 {
170 break;
171 }
172
173 TrimString(line);
174 if( strlen(line) > 0 && FileExists(line))
175 {
176 AddFileToDownloadsTable(line);
177 }
178 }
179
180 CloseHandle(file);
181 }
182 else
183 {
184 LogError("[SM] no file found for downloads (configs/franug_cwm/downloads.txt)");
185 }
186}
187
188public Action Command_cw(int client, int args)
189{
190 SetMenuTitle(menu_cw, "Custom Weapons Menu v%s\n%T", DATA,"Select a weapon", client);
191 DisplayMenu(menu_cw, client, 0);
192 return Plugin_Handled;
193}
194
195public int Menu_Handler(Menu menu, MenuAction action, int client, int param2)
196{
197 switch (action)
198 {
199 case MenuAction_Select:
200 {
201
202 char item[64];
203 GetMenuItem(menu, param2, item, sizeof(item));
204 Format(client_w[client], 64, "weapon_%s", item);
205
206 KvJumpToKey(kv, client_w[client]);
207
208 char temp[64];
209 Menu menu_weapons = new Menu(Menu_Handler2);
210 SetMenuTitle(menu_weapons, "%T", "Select a custom view model", client);
211 AddMenuItem(menu_weapons, "default", "Default model");
212 if(KvGotoFirstSubKey(kv))
213 {
214 do
215 {
216 KvGetSectionName(kv, temp, 64);
217 AddMenuItem(menu_weapons, temp, temp);
218
219 } while (KvGotoNextKey(kv));
220 }
221 KvRewind(kv);
222 SetMenuExitBackButton(menu_weapons, true);
223 DisplayMenu(menu_weapons, client, 0);
224 }
225
226 }
227}
228
229public int Menu_Handler2(Menu menu, MenuAction action, int client, int param2)
230{
231 switch (action)
232 {
233 case MenuAction_Select:
234 {
235
236 char item[64];
237 GetMenuItem(menu, param2, item, sizeof(item));
238 if(StrEqual(item, "default"))
239 {
240 FPVMI_SetClientModel(client, client_w[client], -1, -1);
241 CPrintToChat(client, " \x04[CW]\x01 %T","Now you have the default weapon model", client);
242 Format(sql_buffer, sizeof(sql_buffer), "UPDATE %s SET saved = 'default' WHERE id = '%i';", client_w[client],client_id[client]);
243 SQL_TQuery(db, tbasico, sql_buffer);
244 return;
245 }
246 KvJumpToKey(kv, client_w[client]);
247 KvJumpToKey(kv, item);
248
249 char cwmodel[PLATFORM_MAX_PATH], cwmodel2[PLATFORM_MAX_PATH], cwmodel3[PLATFORM_MAX_PATH];
250 KvGetString(kv, "model", cwmodel, PLATFORM_MAX_PATH, "none");
251 KvGetString(kv, "worldmodel", cwmodel2, PLATFORM_MAX_PATH, "none");
252 KvGetString(kv, "dropmodel", cwmodel3, PLATFORM_MAX_PATH, "none");
253 if(StrEqual(cwmodel, "none") && StrEqual(cwmodel2, "none") && StrEqual(cwmodel3, "none"))
254 {
255 CPrintToChat(client, " \x04[CW]\x01 %T","Invalid configuration for this model", client);
256 }
257 else
258 {
259 char flag[8];
260 KvGetString(kv, "flag", flag, 8, "");
261 if(HasPermission(client, flag))
262 {
263 FPVMI_SetClientModel(client, client_w[client], !StrEqual(cwmodel, "none")?PrecacheModel(cwmodel):-1, !StrEqual(cwmodel2, "none")?PrecacheModel(cwmodel2):-1, cwmodel3);
264 CPrintToChat(client, " \x04[CW]\x01 %T","Now you have a custom weapon model in",client, client_w[client]);
265
266
267 Format(sql_buffer, sizeof(sql_buffer), "UPDATE %s SET saved = '%s' WHERE id = '%i';", client_w[client],item,client_id[client]);
268 SQL_TQuery(db, tbasico, sql_buffer);
269 }
270 else
271 {
272 CPrintToChat(client, " \x04[CW]\x01 %T","You dont have access to use this weapon model", client);
273 }
274 Command_cw(client, 0);
275 }
276 KvRewind(kv);
277 }
278 case MenuAction_Cancel:
279 {
280 if(param2==MenuCancel_ExitBack)
281 {
282 Command_cw(client, 0);
283 }
284 }
285 case MenuAction_End:
286 {
287 //param1 is MenuEnd reason, if canceled param2 is MenuCancel reason
288 CloseHandle(menu);
289
290 }
291
292 }
293}
294
295stock bool HasPermission(int iClient, char[] flagString)
296{
297 if (StrEqual(flagString, ""))
298 {
299 return true;
300 }
301
302 AdminId admin = GetUserAdmin(iClient);
303
304 if (admin != INVALID_ADMIN_ID)
305 {
306 int count, found, flags = ReadFlagString(flagString);
307 for (int i = 0; i <= 20; i++)
308 {
309 if (flags & (1<<i))
310 {
311 count++;
312
313 if (GetAdminFlag(admin, view_as<AdminFlag>(i)))
314 {
315 found++;
316 }
317 }
318 }
319
320 if (count == found) {
321 return true;
322 }
323 }
324
325 return false;
326}
327
328public OnClientPostAdminCheck(client)
329{
330 client_id[client] = 0;
331
332 if(!IsFakeClient(client)) CheckSteamID(client);
333}
334
335public OnClientDisconnect(client)
336{
337 if(!IsFakeClient(client)) SaveCookies(client);
338
339 client_id[client] = 0;
340}
341
342CheckSteamID(client)
343{
344 decl String:query[255], String:steamid[32];
345 GetClientAuthId(client, AuthId_Steam2, steamid, sizeof(steamid) );
346
347 Format(query, sizeof(query), "SELECT id FROM customweapons WHERE steamid = '%s'", steamid);
348 SQL_TQuery(db, T_CheckSteamID, query, GetClientUserId(client));
349}
350
351public T_CheckSteamID(Handle:owner, Handle:hndl, const String:error[], any:data)
352{
353 new client;
354
355 /* Make sure the client didn't disconnect while the thread was running */
356 if ((client = GetClientOfUserId(data)) == 0)
357 {
358 return;
359 }
360 if (hndl == INVALID_HANDLE)
361 {
362 LogError("Query failure: %s", error);
363 return;
364 }
365
366 if (!SQL_GetRowCount(hndl) || !SQL_FetchRow(hndl))
367 {
368 Nuevo(client);
369 return;
370 }
371
372 client_id[client] = SQL_FetchInt(hndl, 0);
373
374 char items[64];
375 for(new i=0;i<GetArraySize(array_weapons);++i)
376 {
377 GetArrayString(array_weapons, i, items, 64);
378 Format(sql_buffer, sizeof(sql_buffer), "SELECT saved FROM %s WHERE id = '%i'", items, client_id[client]);
379 SQL_TQuery(db, tbasico6, sql_buffer, (GetClientUserId(client)*10000)+i);
380 }
381
382 //PrintToServer("pasado con id %i", client_id[client]);
383}
384
385Nuevo(client)
386{
387 decl String:query[255], String:steamid[32];
388 GetClientAuthId(client, AuthId_Steam2, steamid, sizeof(steamid) );
389 new userid = GetClientUserId(client);
390
391 new String:Name[MAX_NAME_LENGTH+1];
392 new String:SafeName[(sizeof(Name)*2)+1];
393 if (!GetClientName(client, Name, sizeof(Name)))
394 Format(SafeName, sizeof(SafeName), "<noname>");
395 else
396 {
397 TrimString(Name);
398 SQL_EscapeString(db, Name, SafeName, sizeof(SafeName));
399 }
400
401 Format(query, sizeof(query), "INSERT INTO customweapons(playername, steamid, last_accountuse) VALUES('%s', '%s', '%d');", SafeName, steamid, GetTime());
402 SQL_TQuery(db, tbasico3, query, userid);
403}
404
405public tbasico3(Handle:owner, Handle:hndl, const String:error[], any:data)
406{
407 if (hndl == INVALID_HANDLE)
408 {
409 LogError("Query failure: %s", error);
410 return;
411 }
412 new client;
413
414 /* Make sure the client didn't disconnect while the thread was running */
415 if ((client = GetClientOfUserId(data)) == 0)
416 {
417 return;
418 }
419 decl String:steamid[32];
420 GetClientAuthId(client, AuthId_Steam2, steamid, sizeof(steamid) );
421
422 Format(sql_buffer, sizeof(sql_buffer), "SELECT id FROM customweapons WHERE steamid = '%s';", steamid);
423 SQL_TQuery(db, tbasico4, sql_buffer, GetClientUserId(client));
424}
425
426public tbasicoC(Handle:owner, Handle:hndl, const String:error[], any:data)
427{
428 if (hndl == INVALID_HANDLE)
429 {
430 LogError("Query failure: %s", error);
431 return;
432 }
433 //LogMessage("Database connection successful");
434
435 if(array_weapons != INVALID_HANDLE) CloseHandle(array_weapons);
436 array_weapons = CreateArray(64);
437
438 char temp[64];
439 menu_cw = new Menu(Menu_Handler);
440
441 if(KvGotoFirstSubKey(kv))
442 {
443 do
444 {
445 KvGetSectionName(kv, temp, 64);
446
447 if (ismysql) Format(sql_buffer, sizeof(sql_buffer), "CREATE TABLE IF NOT EXISTS `%s` (`id` int(11),`saved` varchar(128),PRIMARY KEY (`id`))", temp);
448 else Format(sql_buffer, sizeof(sql_buffer), "CREATE TABLE IF NOT EXISTS %s (id int(11),saved varchar(128),PRIMARY KEY (id))", temp);
449 SQL_TQuery(db, tbasico, sql_buffer);
450 PushArrayString(array_weapons, temp);
451 ReplaceString(temp, 64, "weapon_", "");
452 AddMenuItem(menu_cw, temp, temp);
453
454 } while (KvGotoNextKey(kv));
455 }
456 KvRewind(kv);
457
458 for(new client = 1; client <= MaxClients; client++)
459 {
460 if(IsClientInGame(client))
461 {
462 OnClientPostAdminCheck(client);
463 }
464 }
465}
466
467public tbasico(Handle:owner, Handle:hndl, const String:error[], any:data)
468{
469 if (hndl == INVALID_HANDLE)
470 {
471 LogError("Query failure: %s", error);
472 }
473}
474
475public tbasico4(Handle:owner, Handle:hndl, const String:error[], any:data)
476{
477 if (hndl == INVALID_HANDLE)
478 {
479 LogError("Query failure: %s", error);
480 return;
481 }
482 new client;
483
484 /* Make sure the client didn't disconnect while the thread was running */
485 if ((client = GetClientOfUserId(data)) == 0)
486 {
487 return;
488 }
489
490 if (!SQL_GetRowCount(hndl) || !SQL_FetchRow(hndl))
491 {
492 return;
493 }
494 char items[64];
495 client_id[client] = SQL_FetchInt(hndl, 0);
496 //PrintToServer("guardando");
497 for(new i=0;i<GetArraySize(array_weapons);++i)
498 {
499 GetArrayString(array_weapons, i, items, 64);
500 Format(sql_buffer, sizeof(sql_buffer), "INSERT INTO %s(id, saved) VALUES('%i', 'default');", items,client_id[client]);
501 SQL_TQuery(db, tbasico, sql_buffer);
502 }
503}
504
505public tbasico6(Handle:owner, Handle:hndl, const String:error[], any:data)
506{
507 if (hndl == INVALID_HANDLE)
508 {
509 LogError("Query failure: %s", error);
510 return;
511 }
512
513 int userid = data/10000;
514 int weapon = (data-(userid*10000));
515 int client = GetClientOfUserId(userid);
516 //PrintToServer("valor %i y %i del total de %i",weapon, client, data);
517
518 /* Make sure the client didn't disconnect while the thread was running */
519 if (client == 0)
520 {
521 return;
522 }
523
524 char items[64], item[64];
525 GetArrayString(array_weapons, weapon, items, 64);
526 if (!SQL_GetRowCount(hndl) || !SQL_FetchRow(hndl))
527 {
528
529 Format(sql_buffer, sizeof(sql_buffer), "INSERT INTO %s(id, saved) VALUES('%i', 'default');", items,client_id[client]);
530 SQL_TQuery(db, tbasico, sql_buffer);
531 return;
532 }
533
534 SQL_FetchString(hndl, 0, item, 64);
535 //PrintToServer("salto a %s y despues a %s", items, item);
536 KvJumpToKey(kv, items);
537 KvJumpToKey(kv, item);
538
539 char cwmodel[PLATFORM_MAX_PATH], cwmodel2[PLATFORM_MAX_PATH], cwmodel3[PLATFORM_MAX_PATH];
540 KvGetString(kv, "model", cwmodel, PLATFORM_MAX_PATH, "none");
541 KvGetString(kv, "worldmodel", cwmodel2, PLATFORM_MAX_PATH, "none");
542 KvGetString(kv, "dropmodel", cwmodel3, PLATFORM_MAX_PATH, "none");
543
544 char flag[8];
545 KvGetString(kv, "flag", flag, 8, "");
546
547 if(HasPermission(client, flag)) FPVMI_SetClientModel(client, items, !StrEqual(cwmodel, "none")?PrecacheModel(cwmodel):-1, !StrEqual(cwmodel2, "none")?PrecacheModel(cwmodel2):-1, cwmodel3);
548
549 KvRewind(kv);
550}
551
552SaveCookies(client)
553{
554 decl String:steamid[32];
555 GetClientAuthId(client, AuthId_Steam2, steamid, sizeof(steamid) );
556 new String:Name[MAX_NAME_LENGTH+1];
557 new String:SafeName[(sizeof(Name)*2)+1];
558 if (!GetClientName(client, Name, sizeof(Name)))
559 Format(SafeName, sizeof(SafeName), "<noname>");
560 else
561 {
562 TrimString(Name);
563 SQL_EscapeString(db, Name, SafeName, sizeof(SafeName));
564 }
565
566 decl String:buffer[3096];
567 Format(buffer, sizeof(buffer), "UPDATE customweapons SET last_accountuse = %d, playername = '%s' WHERE steamid = '%s';",GetTime(), SafeName,steamid);
568 SQL_TQuery(db, tbasico, buffer);
569}