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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
|
/*
* Com.java
* Copyright (C) 2003
*
* $Id: Com.java,v 1.2 2004-07-08 15:58:46 hzi Exp $
*/
/*
Copyright (C) 1997-2001 Id Software, Inc.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package jake2.qcommon;
import jake2.Defines;
import jake2.Globals;
import jake2.client.CL;
import jake2.client.Console;
import jake2.game.Cmd;
import jake2.server.SV_MAIN;
import jake2.sys.Sys;
import jake2.util.*;
import java.io.*;
/**
* Com
* TODO complete Com interface
*/
public final class Com {
static int com_argc;
static String[] com_argv = new String[Defines.MAX_NUM_ARGVS];
public abstract static class RD_Flusher {
public abstract void rd_flush(int target, byte [] buffer);
}
static int rd_target;
static byte[] rd_buffer;
static int rd_buffersize;
static RD_Flusher rd_flusher;
public static void BeginRedirect(int target, byte [] buffer, int buffersize, RD_Flusher flush) {
if (0 == target || null == buffer || 0 == buffersize || null == flush)
return;
rd_target = target;
rd_buffer = buffer;
rd_buffersize = buffersize;
rd_flusher = flush;
rd_buffer = null;
}
public static void EndRedirect() {
rd_flusher.rd_flush(rd_target, rd_buffer);
rd_target = 0;
rd_buffer = null;
rd_buffersize = 0;
rd_flusher = null;
}
static boolean recursive = false;
static String msg = "";
// helper class to replace the pointer-pointer
public static class ParseHelp {
public ParseHelp(String in, int offset) {
this(in.toCharArray(), offset);
}
public ParseHelp(String in) {
if (in == null) {
data = null;
}
else {
data = in.toCharArray();
}
index = 0;
}
public ParseHelp(char in[]) {
this(in, 0);
}
public ParseHelp(char in[], int offset) {
if (in == null || in.length == 0)
data = null;
else
data = in;
index = offset;
}
public char getchar() {
// faster than if
try {
return data[index];
}
catch (Exception e) {
data = null;
// last char
return 0;
}
}
public char nextchar() {
// faster than if
try {
index++;
return data[index];
}
catch (Exception e) {
data = null;
// avoid int wraps;
index--;
// last char
return 0;
}
}
public boolean isEof() {
return data == null;
}
public int index;
public char data[];
public char skipwhites() {
char c;
while (((c = getchar()) <= ' ') && c != 0)
index++;
return c;
}
public char skipwhitestoeol() {
char c;
while (((c = getchar()) <= ' ') && c != '\n' && c != 0)
index++;
return c;
}
public char skiptoeol() {
char c;
while ((c = getchar()) != '\n' && c != 0)
index++;
return c;
}
}
public static char com_token[] = new char[Defines.MAX_TOKEN_CHARS];
// See GameSpanw.ED_ParseEdict() to see how to use it now.
// works perfect !
public static String Parse(ParseHelp hlp) {
int c;
int len = 0;
len = 0;
com_token[0] = 0;
if (hlp.data == null) {
return "";
}
// skip whitespace
hlp.skipwhites();
if (hlp.isEof()) {
return "";
}
// skip // comments
if (hlp.getchar() == '/') {
if (hlp.nextchar() == '/') {
if ((hlp.skiptoeol() == 0) || (hlp.skipwhites() == 0)) {
return "";
}
}
else {
com_token[len] = '/';
len++;
}
}
// handle quoted strings specially
if (hlp.getchar() == '\"') {
while (true) {
c = hlp.nextchar();
if (c == '\"' || c == 0) {
hlp.nextchar();
com_token[len] = '?';
return new String(com_token, 0, len);
}
if (len < Defines.MAX_TOKEN_CHARS) {
com_token[len] = hlp.getchar();
len++;
}
}
}
// parse a regular word
do {
if (len < Defines.MAX_TOKEN_CHARS) {
com_token[len] = hlp.getchar();
len++;
}
c = hlp.nextchar();
}
while (c > 32);
if (len == Defines.MAX_TOKEN_CHARS) {
Printf("Token exceeded " + Defines.MAX_TOKEN_CHARS + " chars, discarded.\n");
len = 0;
}
// trigger the eof
hlp.skipwhites();
com_token[len] = 0;
return new String(com_token, 0, len);
}
public static xcommand_t Error_f = new xcommand_t() {
public void execute() throws longjmpException {
Error(Globals.ERR_FATAL, Cmd.Argv(1));
}
};
public static void Error(int code, String fmt) throws longjmpException {
Error(code, fmt, null);
}
public static void Error(int code, String fmt, Vargs vargs) throws longjmpException {
// va_list argptr;
// static char msg[MAXPRINTMSG];
if (recursive) {
Sys.Error("recursive error after: " + msg);
}
recursive = true;
msg = sprintf(fmt, vargs);
if (code == Defines.ERR_DISCONNECT) {
CL.Drop();
recursive = false;
throw new longjmpException();
}
else if (code == Defines.ERR_DROP) {
Com.Printf("********************\nERROR: " + msg + "\n********************\n");
SV_MAIN.SV_Shutdown("Server crashed: " + msg + "\n", false);
CL.Drop();
recursive = false;
throw new longjmpException();
}
else {
SV_MAIN.SV_Shutdown("Server fatal crashed: %s" + msg + "\n", false);
CL.Shutdown();
}
Sys.Error(msg);
}
/**
* Com_InitArgv checks the number of command line arguments
* and copies all arguments with valid length into com_argv.
*/
static void InitArgv(String[] args) throws longjmpException {
if (args.length > Globals.MAX_NUM_ARGVS) {
Com.Error(Globals.ERR_FATAL, "argc > MAX_NUM_ARGVS");
}
Com.com_argc = args.length;
for (int i = 0; i < Com.com_argc; i++) {
if (args[i].length() >= Globals.MAX_TOKEN_CHARS)
Com.com_argv[i] = "";
else
Com.com_argv[i] = args[i];
}
}
public static void DPrintf(String fmt) {
DPrintf(fmt, null);
}
public static void d(String fmt) {
DPrintf(fmt + "\n", null);
}
public static void Printf(String fmt) {
Printf(fmt, null);
}
public static void DPrintf(String fmt, Vargs vargs) {
if (Globals.developer == null || Globals.developer.value == 0)
return; // don't confuse non-developers with techie stuff...
Printf(fmt, vargs);
}
public static void Printf(String fmt, Vargs vargs) {
// TODO Com.Printf ist nur zum testen
String msg = sprintf(fmt, vargs);
if (rd_target != 0)
{
if ((msg.length() + Lib.strlen(rd_buffer)) > (rd_buffersize - 1))
{
rd_flusher.rd_flush(rd_target, rd_buffer);
// *rd_buffer = 0;
rd_buffer[rd_buffersize] = '\0';
}
// TODO handle rd_buffer
// strcat(rd_buffer, msg);
return;
}
Console.Print(msg);
// also echo to debugging console
Sys.ConsoleOutput(msg);
// logfile
if (Globals.logfile_active != null && Globals.logfile_active.value != 0)
{
String name;
if (Globals.logfile == null)
{
name = FS.Gamedir() + "/qconsole.log";
if (Globals.logfile_active.value > 2)
try
{
Globals.logfile = new RandomAccessFile(name, "a");
}
catch (FileNotFoundException e)
{
// TODO: do quake2 error handling!
e.printStackTrace();
}
else
try
{
Globals.logfile = new RandomAccessFile(name, "rw");
}
catch (FileNotFoundException e1)
{
// TODO: do quake2 error handling!
e1.printStackTrace();
}
}
if (Globals.logfile != null)
try
{
Globals.logfile.writeChars(msg);
}
catch (IOException e)
{
// TODO: do quake2 error handling!
e.printStackTrace();
}
if (Globals.logfile_active.value > 1)
; // do nothing
// fflush (logfile); // force it to save every time
}
}
public static void Println(String fmt) {
Printf(fmt);
Printf("\n");
}
public static void p(String fmt) {
Printf(fmt);
Printf("\n");
}
public static String sprintf(String fmt, Vargs vargs) {
String msg = "";
if (vargs == null || vargs.size() == 0) {
msg = fmt;
}
else {
msg = new PrintfFormat(fmt).sprintf(vargs.toArray());
}
return msg;
}
public static int ServerState() {
return Globals.server_state;
}
public static int Argc() {
return Com.com_argc;
}
public static String Argv(int arg) {
if (arg < 0 || arg >= Com.com_argc || Com.com_argv[arg].length() < 1)
return "";
return Com.com_argv[arg];
}
public static void ClearArgv(int arg) {
if (arg < 0 || arg >= Com.com_argc || Com.com_argv[arg].length() < 1)
return;
Com.com_argv[arg] = "";
}
public static void Quit() {
SV_MAIN.SV_Shutdown("Server quit\n", false);
CL.Shutdown();
if (Globals.logfile != null) {
try
{
Globals.logfile.close();
}
catch (IOException e)
{
}
Globals.logfile = null;
}
Sys.Quit();
}
public static void SetServerState(int i)
{
Globals.server_state = i;
}
public static int BlockChecksum(byte[] buf, int length)
{
return MD4.Com_BlockChecksum(buf, length);
}
public static void StripExtension(String string, String string2) {
// TODO implement!
}
}
|