/
dd_help.c
355 lines (295 loc) · 9.59 KB
/
dd_help.c
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
/**\file dd_help.c
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2003-2011 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2006-2011 Daniel Swanson <danij@dengine.net>
*\author Copyright © 2006 Jamie Jones <jamie_jones_au@yahoo.com.au>
*
* 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 2 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
/**
* Help Text Strings.
*/
// HEADER FILES ------------------------------------------------------------
#include "de_base.h"
#include "de_console.h"
#include "de_system.h"
#include "de_misc.h"
#include "de_filesys.h"
// MACROS ------------------------------------------------------------------
#define MAX_STRINGS 16
// TYPES -------------------------------------------------------------------
typedef struct helpstring_s {
int type;
char* text;
} helpstring_t;
typedef struct helpnode_s {
struct helpnode_s *prev, *next;
char* id;
helpstring_t str[MAX_STRINGS];
} helpnode_t;
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
D_CMD(LoadHelp);
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// PUBLIC DATA DEFINITIONS -------------------------------------------------
// PRIVATE DATA DEFINITIONS ------------------------------------------------
static boolean helpInited = false;
static helpnode_t helpRoot;
// CODE --------------------------------------------------------------------
void DH_Register(void)
{
C_CMD("loadhelp", "", LoadHelp);
}
/**
* Create and link a new help node.
*
* @return Ptr to the newly created helpnode.
*/
static helpnode_t* DH_NewNode(void)
{
helpnode_t* n = M_Calloc(sizeof(*n));
n->next = &helpRoot;
n->prev = helpRoot.prev;
helpRoot.prev = n->prev->next = n;
return n;
}
/**
* Unlink the helpnode from the dlist and destroy it.
*
* @param node Ptr to the node to be destroyed.
*/
static void DH_DeleteNode(helpnode_t* node)
{
int i;
if(!node)
return;
node->prev->next = node->next;
node->next->prev = node->prev;
// Free all memory associated with the node.
if(node->id)
M_Free(node->id);
for(i = 0; i < MAX_STRINGS; ++i)
M_Free(node->str[i].text);
M_Free(node);
}
/**
* Parses the given file looking for help strings.
*
* @param fileName Path to the file to be read.
*
* @return Non-zero if the file was read successfully.
*/
static int DH_ReadStrings(const char* fileName)
{
DFile* file = F_Open(fileName, "rt");
char line[2048], *ptr, *eol, *end;
helpnode_t* node = 0;
int count = 0, length;
if(!file)
{
Con_Message("DH_ReadStrings: Warning, %s not found.\n", fileName);
return false;
}
while(!DFile_AtEnd(file))
{
M_ReadLine(line, sizeof(line), file);
if(M_IsComment(line))
continue;
// Where does the line begin?
ptr = M_SkipWhite(line);
if(!*ptr)
continue; // An empty line.
eol = ptr + strlen(ptr); // End of line.
// A new node?
if(*ptr == '[')
{
node = DH_NewNode();
count = 0;
if(!(end = strchr(ptr, ']')))
end = eol;
node->id = M_Calloc(end - ptr);
strncpy(node->id, ptr + 1, end - ptr - 1);
}
else if(node && (end = strchr(ptr, '='))) // It must be a key?
{
helpstring_t* hst = node->str + count;
if(count == MAX_STRINGS)
continue; // No more room.
count++;
// The type of the string.
if(!strnicmp(ptr, "des", 3))
hst->type = HST_DESCRIPTION;
else if(!strnicmp(ptr, "cv", 2))
hst->type = HST_CONSOLE_VARIABLE;
else if(!strnicmp(ptr, "def", 3))
hst->type = HST_DEFAULT_VALUE;
else if(!strnicmp(ptr , "inf", 3))
hst->type = HST_INFO;
ptr = M_SkipWhite(end + 1);
// The value may be split over multiple lines.
// 64 kb should be quite enough.
length = 0, hst->text = M_Malloc(0x10000);
while(*ptr)
{
// Backslash escapes.
if(*ptr == '\\')
{
// Is this the last visible char?
if(!*M_SkipWhite(ptr + 1))
{
// Read the next line.
M_ReadLine(line, sizeof(line), file);
ptr = M_SkipWhite(line);
}
else // \ is not the last char on the line.
{
ptr++;
if(*ptr == '\\')
hst->text[length++] = '\\';
if(*ptr == 'n')
hst->text[length++] = '\n';
if(*ptr == 'b')
hst->text[length++] = '\b';
ptr++;
}
}
else
{
// Append to the text.
hst->text[length++] = *ptr++;
}
}
// Resize the memory to fit the text.
hst->text[length] = 0;
hst->text = M_Realloc(hst->text, length + 1);
}
}
F_Delete(file);
// The file was read successfully.
return true;
}
/**
* Finds a node matching the ID. Use DH_GetString to read strings from it.
*
* @param id Help node ID to be searched for.
*
* @return Ptr to helpnode if matched ELSE @c NULL,.
*/
void* DH_Find(const char* id)
{
helpnode_t* n;
size_t length;
if(!helpInited)
return NULL;
length = strlen(id);
for(n = helpRoot.next; n != &helpRoot; n = n->next)
{
/**
* Don't compare unless the string is long enough.
* This also stops us returning a false positive when a substring
* matches the search string e.g:
* [rend-light] != [rend-light-ambient]
*/
if(strlen(n->id) < length)
continue;
if(!stricmp(n->id, id))
return n;
}
return NULL;
}
/**
* Return a string from within the helpnode. Strings are stored internally
* and indexed by their type (e.g. HST_DESCRIPTION).
*
* @param foundNode The helpnode to return the string from.
* @param type The string type (index) to look for within the node.
*
* @return Ptr to the found string ELSE @c NULL,. Note,
* may also return @c NULL, if passed an
* invalid helpnode ptr OR the help string database has
* not beeen initialized yet.
*/
char* DH_GetString(void* foundNode, int type)
{
helpnode_t* n = foundNode;
int i;
if(!n || !helpInited || type < 0 || type > NUM_HELPSTRING_TYPES)
return NULL;
for(i = 0; i < MAX_STRINGS; ++i)
if(n->str[i].text && n->str[i].type == type)
return n->str[i].text;
return NULL;
}
/**
* Initializes the help string database. After which, attempts to read the engine's
* own help string file.
*/
void DD_InitHelp(void)
{
float starttime;
if(helpInited) return; // Already inited.
VERBOSE( Con_Message("Initializing Help subsystem...\n") )
starttime = (verbose >= 2? Sys_GetSeconds() : 0);
// Init the links.
helpRoot.next = helpRoot.prev = &helpRoot;
// Parse the control panel help file.
{ ddstring_t helpFileName; Str_Init(&helpFileName);
Str_Set(&helpFileName, DD_BASEPATH_DATA"cphelp.txt");
F_ExpandBasePath(&helpFileName, &helpFileName);
DH_ReadStrings(Str_Text(&helpFileName));
Str_Free(&helpFileName);
}
// Help is now available.
helpInited = true;
VERBOSE2( Con_Message("DD_InitHelp: Done in %.2f seconds.\n", Sys_GetSeconds() - starttime) );
}
/**
* Attempts to read help strings from the game-specific help file.
*/
void DD_ReadGameHelp(void)
{
ddstring_t helpFileName;
if(!helpInited || !DD_GameLoaded())
return; // Nothing to do.
Str_Init(&helpFileName);
Str_Appendf(&helpFileName, "%sconhelp.txt", Str_Text(Game_DataPath(theGame)));
F_ExpandBasePath(&helpFileName, &helpFileName);
DH_ReadStrings(Str_Text(&helpFileName));
Str_Free(&helpFileName);
}
/**
* Shuts down the help string database. Frees all storage and destroys
* database itself.
*/
void DD_ShutdownHelp(void)
{
if(!helpInited)
return;
helpInited = false;
// Delete all the nodes.
while(helpRoot.next != &helpRoot)
DH_DeleteNode(helpRoot.next);
}
D_CMD(LoadHelp)
{
DD_ShutdownHelp();
DD_InitHelp();
return true;
}