forked from FWGS/mainui_cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CFGScript.cpp
372 lines (296 loc) · 8.05 KB
/
CFGScript.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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
cfgscript.c - "Valve script" parsing routines
Copyright (C) 2016 mittorn
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
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.
*/
#include "extdll_menu.h"
#include "BaseMenu.h"
#include "enginecallback_menu.h"
#include "CFGScript.h"
#define CVAR_USERINFO BIT(1)
const char *cvartypes[] = { NULL, "BOOL" , "NUMBER", "LIST", "STRING" };
struct parserstate_t
{
parserstate_t() : buf( NULL ), filename( NULL ) { token[0] = 0;}
char *buf;
char token[MAX_STRING];
const char *filename;
};
/*
===================
CSCR_ExpectString
Return true if next token is pExpext and skip it
===================
*/
bool CSCR_ExpectString( parserstate_t *ps, const char *pExpect, bool skip, bool error )
{
char *tmp = EngFuncs::COM_ParseFile( ps->buf, ps->token, sizeof( ps->token ));
if( !stricmp( ps->token, pExpect ) )
{
ps->buf = tmp;
return true;
}
if( skip )
ps->buf = tmp;
if( error )
Con_DPrintf( "Syntax error in %s: got \"%s\" instead of \"%s\"\n", ps->filename, ps->token, pExpect );
return false;
}
/*
===================
CSCR_ParseType
Determine script variable type
===================
*/
cvartype_t CSCR_ParseType( parserstate_t *ps )
{
int i;
for ( i = 1; i < T_COUNT; ++i )
{
if( CSCR_ExpectString( ps, cvartypes[i], false, false ) )
return (cvartype_t)i;
}
Con_DPrintf( "Cannot parse %s: Bad type %s\n", ps->filename, ps->token );
return T_NONE;
}
/*
=========================
CSCR_ParseSingleCvar
=========================
*/
bool CSCR_ParseSingleCvar( parserstate_t *ps, scrvardef_t *result )
{
// clean linked list for list
result->list.iCount = 0;
result->list.pEntries = result->list.pLast = NULL;
result->list.pArray = NULL;
// read the name
ps->buf = EngFuncs::COM_ParseFile( ps->buf, result->name, sizeof( result->name ));
if( !CSCR_ExpectString( ps, "{", false, true ) )
goto error;
// read description
ps->buf = EngFuncs::COM_ParseFile( ps->buf, result->desc, sizeof( result->desc ));
if( !CSCR_ExpectString( ps, "{", false, true ) )
goto error;
result->type = CSCR_ParseType( ps );
switch( result->type )
{
case T_BOOL:
// bool only has description
if( !CSCR_ExpectString( ps, "}", false, true ) )
goto error;
break;
case T_NUMBER:
// min
ps->buf = EngFuncs::COM_ParseFile( ps->buf, ps->token, sizeof( ps->token ));
result->number.fMin = atof( ps->token );
// max
ps->buf = EngFuncs::COM_ParseFile( ps->buf, ps->token, sizeof( ps->token ));
result->number.fMax = atof( ps->token );
if( !CSCR_ExpectString( ps, "}", false, true ) )
goto error;
break;
case T_STRING:
if( !CSCR_ExpectString( ps, "}", false, true ) )
goto error;
break;
case T_LIST:
while( !CSCR_ExpectString( ps, "}", true, false ) )
{
// char szName[128];
char *szName = ps->token;
char szValue[64];
scrvarlistentry_t *entry = 0;
// Read token for each item here
// ExpectString already moves buffer pointer, so just read from ps->token
// ps->buf = EngFuncs::COM_ParseFile( ps->buf, szName, sizeof( szName ));
if( !szName[0] )
goto error;
ps->buf = EngFuncs::COM_ParseFile( ps->buf, szValue, sizeof( szValue ));
if( !szValue[0] )
goto error;
entry = new scrvarlistentry_t;
entry->next = NULL;
entry->szName = new char[strlen( szName ) + 1];
strcpy( entry->szName, szName );
entry->flValue = atof( szValue );
if( !result->list.pEntries )
result->list.pEntries = entry;
else
result->list.pLast->next = entry;
result->list.pLast = entry;
result->list.iCount++;
}
break;
default:
goto error;
}
if( !CSCR_ExpectString( ps, "{", false, true ) )
goto error;
// default value
ps->buf = EngFuncs::COM_ParseFile( ps->buf, result->value, sizeof( result->value ));
if( !CSCR_ExpectString( ps, "}", false, true ) )
goto error;
if( CSCR_ExpectString( ps, "SetInfo", false, false ) )
result->flags |= CVAR_USERINFO;
if( !CSCR_ExpectString( ps, "}", false, true ) )
goto error;
if( result->type == T_LIST )
{
scrvarlistentry_t *entry = result->list.pEntries;
result->list.pArray = new const char*[result->list.iCount];
result->list.pModel = new CStringArrayModel( result->list.pArray, result->list.iCount );
for( int i = 0; entry; entry = entry->next, i++ )
{
result->list.pArray[i] = L( entry->szName );
}
}
return true;
error:
if( result->type == T_LIST )
{
if( result->list.pArray )
delete[] result->list.pArray;
if( result->list.pModel )
delete result->list.pModel;
while( result->list.pEntries )
{
scrvarlistentry_t *next = result->list.pEntries->next;
delete[] result->list.pEntries->szName;
delete result->list.pEntries;
result->list.pEntries = next;
}
}
return false;
}
/*
======================
CSCR_ParseHeader
Check version and seek to first cvar name
======================
*/
bool CSCR_ParseHeader( parserstate_t *ps )
{
if( !CSCR_ExpectString( ps, "VERSION", false, true ) )
return false;
// Parse in the version #
// Get the first token.
ps->buf = EngFuncs::COM_ParseFile( ps->buf, ps->token, sizeof( ps->token ));
if( atof( ps->token ) != 1 )
{
Con_DPrintf( "File %s has wrong version %s!\n", ps->filename, ps->token );
return false;
}
if( !CSCR_ExpectString( ps, "DESCRIPTION", false, true ) )
return false;
ps->buf = EngFuncs::COM_ParseFile( ps->buf, ps->token, sizeof( ps->token ));
if( stricmp( ps->token, "INFO_OPTIONS") && stricmp( ps->token, "SERVER_OPTIONS" ) )
{
Con_DPrintf( "DESCRIPTION must be INFO_OPTIONS or SERVER_OPTIONS\n");
return false;
}
if( !CSCR_ExpectString( ps, "{", false, true ) )
return false;
return true;
}
/*
======================
CSCR_LoadDefaultCVars
Register all cvars declared in config file and set default values
======================
*/
scrvardef_t *CSCR_LoadDefaultCVars( const char *scriptfilename, int *count )
{
int length = 0;
char *start;
parserstate_t state;
bool success = false;
scrvardef_t *list = 0, *last = 0;
*count = 0;
state.filename = scriptfilename;
state.buf = (char*)EngFuncs::COM_LoadFile( scriptfilename, &length );
start = state.buf;
if( state.buf == 0 || length == 0)
{
if( start )
EngFuncs::COM_FreeFile( start );
return 0;
}
Con_DPrintf( "Reading config script file %s\n", scriptfilename );
if( !CSCR_ParseHeader( &state ) )
{
Con_DPrintf( "Failed to parse header!\n" );
goto finish;
}
while( !CSCR_ExpectString( &state, "}", false, false ) )
{
scrvardef_t var;
// Create a new object
if( CSCR_ParseSingleCvar( &state, &var ) )
{
// Cvar_Get( var.name, var.value, var.flags, var.desc );
scrvardef_t *entry = new scrvardef_t;
*entry = var;
if( !list )
{
list = last = entry;
}
else
{
last = last->next = entry;
}
(*count)++;
}
else
break;
if( *count > 1024 )
break;
}
if( EngFuncs::COM_ParseFile( state.buf, state.token, sizeof( state.token )))
Con_DPrintf( "Got extra tokens!\n" );
else
success = true;
finish:
if( !success )
{
state.token[ sizeof( state.token ) - 1 ] = 0;
if( start && state.buf )
Con_DPrintf( "Parse error in %s, byte %d, token %s\n", scriptfilename, (int)( state.buf - start ), state.token );
else
Con_DPrintf( "Parse error in %s, token %s\n", scriptfilename, state.token );
}
if( start )
EngFuncs::COM_FreeFile( start );
return list;
}
void CSCR_FreeList( scrvardef_t *list )
{
scrvardef_t *i = list;
while( i )
{
scrvardef_t *next = i->next;
if( i->type == T_LIST )
{
if( i->list.pModel )
delete i->list.pModel;
if( i->list.pArray )
delete[] i->list.pArray;
while( i->list.pEntries )
{
scrvarlistentry_t *next = i->list.pEntries->next;
delete[] i->list.pEntries->szName;
delete i->list.pEntries;
i->list.pEntries = next;
}
}
delete i;
i = next;
}
}