-
Notifications
You must be signed in to change notification settings - Fork 1
/
lgofnoc_mapcvars.sp
266 lines (207 loc) · 8.33 KB
/
lgofnoc_mapcvars.sp
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
#pragma semicolon 1
#include <sourcemod>
#include <sdktools>
#include <left4downtown>
//#include <lgofnoc>
#define KV_MAPCVAR "/mapcvars.txt"
#define MAX_CONFIG_LEN 128
#define MAX_VARLENGTH 64
#define MAX_VALUELENGTH 128
#define MAX_SETVARS 64
#define DEBUG false
new Handle: g_hUseConfigDir;
new String: g_sUseConfigDir[MAX_CONFIG_LEN]; // which directory in lgofnoc are we using? none = cfg/
/*
to do:
- make it work with lgofnoc (lgofnoc?)
0.1b
- works properly with config dir changes on the fly
- unloads properly now (resets values)
*/
new Handle: g_hKvOrig = INVALID_HANDLE; // kv to store original values in
public Plugin:myinfo = {
name = "L4D(2) map-based convar loader.",
author = "Tabun",
version = "0.1b",
description = "Loads convars on map-load, based on currently active map and lgofnoc config."
};
public OnPluginStart() {
g_hUseConfigDir = CreateConVar("l4d_mapcvars_configdir", "", "Which lgofnoc config are we using?", FCVAR_PLUGIN);
GetConVarString(g_hUseConfigDir, g_sUseConfigDir, MAX_CONFIG_LEN);
HookConVarChange(g_hUseConfigDir, CvarConfigChange);
// prepare KV for saving old states
g_hKvOrig = CreateKeyValues("MapCvars_Orig"); // store original values
}
public OnPluginEnd() {
ResetMapPrefs();
if (g_hKvOrig != INVALID_HANDLE) { CloseHandle(g_hKvOrig); }
}
public CvarConfigChange( Handle:cvar, const String:oldValue[], const String:newValue[] ) {
strcopy(g_sUseConfigDir, MAX_CONFIG_LEN, newValue);
#if DEBUG
PrintToChatAll("[mcv] config directory changed [%s], reloading values.", newValue);
#endif
ResetMapPrefs(); // reset old
GetThisMapPrefs(); // apply new
}
public OnMapStart() {
GetThisMapPrefs();
}
public OnMapEnd() {
ResetMapPrefs();
}
public GetThisMapPrefs()
{
new iNumChanged = 0; // how many cvars were changed for this map
// reopen original keyvalues for clean slate:
if (g_hKvOrig != INVALID_HANDLE) { CloseHandle(g_hKvOrig); }
g_hKvOrig = CreateKeyValues("MapCvars_Orig"); // store original values for this map
// build path to current config's keyvalues file
new String:usePath[PLATFORM_MAX_PATH];
if (strlen(g_sUseConfigDir) > 0)
{
usePath = "mapcvars/";
StrCat(usePath, PLATFORM_MAX_PATH, g_sUseConfigDir);
StrCat(usePath, PLATFORM_MAX_PATH, KV_MAPCVAR);
} else {
usePath = "mapcvars/";
StrCat(usePath, PLATFORM_MAX_PATH, KV_MAPCVAR);
}
BuildPath(Path_SM, usePath, sizeof(usePath), usePath);
if (!FileExists(usePath)) {
#if DEBUG
PrintToChatAll("[mcv] file does not exist! (%s)", usePath);
#endif
return 0;
}
#if DEBUG
PrintToChatAll("[mcv] trying keyvalue read (from [%s])...", usePath);
#endif
new Handle: hKv = CreateKeyValues("MapCvars");
FileToKeyValues(hKv, usePath);
if (hKv == INVALID_HANDLE) {
#if DEBUG
PrintToChatAll("[mcv] couldn't read file.");
#endif
return 0;
}
// read keyvalues for current map
new String:sMapName[64];
GetCurrentMap(sMapName, 64);
if (!KvJumpToKey(hKv, sMapName))
{
// no special settings for this map
CloseHandle(hKv);
#if DEBUG
PrintToChatAll("[mcv] couldn't find map (%s)", sMapName);
#endif
return 0;
}
// find all cvar keys and save the original values
// then execute the change
new String:tmpKey[MAX_VARLENGTH];
new String:tmpValueNew[MAX_VALUELENGTH];
new String:tmpValueOld[MAX_VALUELENGTH];
new Handle: hConVar = INVALID_HANDLE;
//new iConVarFlags = 0;
if (KvGotoFirstSubKey(hKv, false)) // false to get values
{
do
{
// read key stuff
KvGetSectionName(hKv, tmpKey, sizeof(tmpKey)); // the subkey is a key-value pair, so get this to get the 'convar'
#if DEBUG
PrintToChatAll("[mcv] kv key found: [%s], reading value...", tmpKey);
#endif
// is it a convar?
hConVar = FindConVar(tmpKey);
/*
// what to do with non-existant cvars?
// don't add, for now
if (hConVar == INVALID_HANDLE) {
hConVar = CreateConVar(tmpKey, "", "[mcv] added because it didn't exist yet...", FCVAR_PLUGIN);
}
*/
if (hConVar != INVALID_HANDLE) {
// get type..
//iConVarFlags = GetConVarFlags(hConVar);
// types?
// FCVAR_CHEAT
KvGetString(hKv, NULL_STRING, tmpValueNew, sizeof(tmpValueNew), "[:none:]");
#if DEBUG
PrintToChatAll("[mcv] kv value read: [%s] => [%s])", tmpKey, tmpValueNew);
#endif
// read, save and set value
if (!StrEqual(tmpValueNew,"[:none:]")) {
GetConVarString(hConVar, tmpValueOld, sizeof(tmpValueOld));
PrintToChatAll("[mcv] cvar value changed: [%s] => [%s] (saved old: [%s]))", tmpKey, tmpValueNew, tmpValueOld);
if (!StrEqual(tmpValueNew,tmpValueOld)) {
// different, save the old
iNumChanged++;
KvSetString(g_hKvOrig, tmpKey, tmpValueOld);
// apply the new
SetConVarString(hConVar, tmpValueNew);
//if (iConVarFlags & FCVAR_CHEAT) {
//}
}
}
} else {
#if DEBUG
PrintToChatAll("[mcv] convar doesn't exist: [%s], not changing it.", tmpKey);
#endif
}
} while (KvGotoNextKey(hKv, false));
}
KvSetString(g_hKvOrig, "__EOF__", "1"); // a test-safeguard
CloseHandle(hKv);
return iNumChanged;
}
public ResetMapPrefs()
{
KvRewind(g_hKvOrig);
#if DEBUG
PrintToChatAll("[mcv] attempting to reset values, if any...");
#endif
// find all cvar keys and reset to original values
new String: tmpKey[64];
new String: tmpValueOld[512];
new Handle: hConVar = INVALID_HANDLE;
if (KvGotoFirstSubKey(g_hKvOrig, false)) // false to get values
{
do
{
// read key stuff
KvGetSectionName(g_hKvOrig, tmpKey, sizeof(tmpKey)); // the subkey is a key-value pair, so get this to get the 'convar'
if (StrEqual(tmpKey, "__EOF__")) {
#if DEBUG
PrintToChatAll("[mcv] kv original settings, all read. (EOF).");
#endif
break;
}
else
{
#if DEBUG
PrintToChatAll("[mcv] kv original saved setting found: [%s], reading value...", tmpKey);
#endif
// is it a convar?
hConVar = FindConVar(tmpKey);
if (hConVar != INVALID_HANDLE) {
KvGetString(g_hKvOrig, NULL_STRING, tmpValueOld, sizeof(tmpValueOld), "[:none:]");
#if DEBUG
PrintToChatAll("[mcv] kv saved value read: [%s] => [%s])", tmpKey, tmpValueOld);
#endif
// read, save and set value
if (!StrEqual(tmpValueOld,"[:none:]")) {
// reset the old
SetConVarString(hConVar, tmpValueOld);
PrintToChatAll("[mcv] cvar value reset to original: [%s] => [%s])", tmpKey, tmpValueOld);
}
} else {
#if DEBUG
PrintToChatAll("[mcv] convar doesn't exist: [%s], not resetting it.", tmpKey);
#endif
}
}
} while (KvGotoNextKey(g_hKvOrig, false));
}
}