| 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 |
#ifdef BOTLIB |
| 33 |
#include "../qcommon/q_shared.h" |
| 34 |
#include "botlib.h" |
| 35 |
#include "l_script.h" |
| 36 |
#include "l_precomp.h" |
| 37 |
#include "l_struct.h" |
| 38 |
#include "l_utils.h" |
| 39 |
#include "be_interface.h" |
| 40 |
#endif |
| 41 |
|
| 42 |
#ifdef BSPC |
| 43 |
|
| 44 |
#include "../bspc/qbsp.h" |
| 45 |
#include "../bspc/l_log.h" |
| 46 |
#include "../bspc/l_mem.h" |
| 47 |
#include "l_precomp.h" |
| 48 |
#include "l_struct.h" |
| 49 |
|
| 50 |
#define qtrue true |
| 51 |
#define qfalse false |
| 52 |
#endif |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
fielddef_t *FindField(fielddef_t *defs, char *name) |
| 61 |
{ |
| 62 |
int i; |
| 63 |
|
| 64 |
for (i = 0; defs[i].name; i++) |
| 65 |
{ |
| 66 |
if (!strcmp(defs[i].name, name)) return &defs[i]; |
| 67 |
} |
| 68 |
return NULL; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
qboolean ReadNumber(source_t *source, fielddef_t *fd, void *p) |
| 77 |
{ |
| 78 |
token_t token; |
| 79 |
int negative = qfalse; |
| 80 |
long int intval, intmin = 0, intmax = 0; |
| 81 |
double floatval; |
| 82 |
|
| 83 |
if (!PC_ExpectAnyToken(source, &token)) return 0; |
| 84 |
|
| 85 |
|
| 86 |
if (token.type == TT_PUNCTUATION) |
| 87 |
{ |
| 88 |
if (fd->type & FT_UNSIGNED) |
| 89 |
{ |
| 90 |
SourceError(source, "expected unsigned value, found %s", token.string); |
| 91 |
return 0; |
| 92 |
} |
| 93 |
|
| 94 |
if (strcmp(token.string, "-")) |
| 95 |
{ |
| 96 |
SourceError(source, "unexpected punctuation %s", token.string); |
| 97 |
return 0; |
| 98 |
} |
| 99 |
negative = qtrue; |
| 100 |
|
| 101 |
if (!PC_ExpectAnyToken(source, &token)) return 0; |
| 102 |
} |
| 103 |
|
| 104 |
if (token.type != TT_NUMBER) |
| 105 |
{ |
| 106 |
SourceError(source, "expected number, found %s", token.string); |
| 107 |
return 0; |
| 108 |
} |
| 109 |
|
| 110 |
if (token.subtype & TT_FLOAT) |
| 111 |
{ |
| 112 |
if ((fd->type & FT_TYPE) != FT_FLOAT) |
| 113 |
{ |
| 114 |
SourceError(source, "unexpected float"); |
| 115 |
return 0; |
| 116 |
} |
| 117 |
floatval = token.floatvalue; |
| 118 |
if (negative) floatval = -floatval; |
| 119 |
if (fd->type & FT_BOUNDED) |
| 120 |
{ |
| 121 |
if (floatval < fd->floatmin || floatval > fd->floatmax) |
| 122 |
{ |
| 123 |
SourceError(source, "float out of range [%f, %f]", fd->floatmin, fd->floatmax); |
| 124 |
return 0; |
| 125 |
} |
| 126 |
} |
| 127 |
*(float *) p = (float) floatval; |
| 128 |
return 1; |
| 129 |
} |
| 130 |
|
| 131 |
intval = token.intvalue; |
| 132 |
if (negative) intval = -intval; |
| 133 |
|
| 134 |
if ((fd->type & FT_TYPE) == FT_CHAR) |
| 135 |
{ |
| 136 |
if (fd->type & FT_UNSIGNED) {intmin = 0; intmax = 255;} |
| 137 |
else {intmin = -128; intmax = 127;} |
| 138 |
} |
| 139 |
if ((fd->type & FT_TYPE) == FT_INT) |
| 140 |
{ |
| 141 |
if (fd->type & FT_UNSIGNED) {intmin = 0; intmax = 65535;} |
| 142 |
else {intmin = -32768; intmax = 32767;} |
| 143 |
} |
| 144 |
if ((fd->type & FT_TYPE) == FT_CHAR || (fd->type & FT_TYPE) == FT_INT) |
| 145 |
{ |
| 146 |
if (fd->type & FT_BOUNDED) |
| 147 |
{ |
| 148 |
intmin = Maximum(intmin, fd->floatmin); |
| 149 |
intmax = Minimum(intmax, fd->floatmax); |
| 150 |
} |
| 151 |
if (intval < intmin || intval > intmax) |
| 152 |
{ |
| 153 |
SourceError(source, "value %ld out of range [%ld, %ld]", intval, intmin, intmax); |
| 154 |
return 0; |
| 155 |
} |
| 156 |
} |
| 157 |
else if ((fd->type & FT_TYPE) == FT_FLOAT) |
| 158 |
{ |
| 159 |
if (fd->type & FT_BOUNDED) |
| 160 |
{ |
| 161 |
if (intval < fd->floatmin || intval > fd->floatmax) |
| 162 |
{ |
| 163 |
SourceError(source, "value %ld out of range [%f, %f]", intval, fd->floatmin, fd->floatmax); |
| 164 |
return 0; |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
if ((fd->type & FT_TYPE) == FT_CHAR) |
| 170 |
{ |
| 171 |
if (fd->type & FT_UNSIGNED) *(unsigned char *) p = (unsigned char) intval; |
| 172 |
else *(char *) p = (char) intval; |
| 173 |
} |
| 174 |
else if ((fd->type & FT_TYPE) == FT_INT) |
| 175 |
{ |
| 176 |
if (fd->type & FT_UNSIGNED) *(unsigned int *) p = (unsigned int) intval; |
| 177 |
else *(int *) p = (int) intval; |
| 178 |
} |
| 179 |
else if ((fd->type & FT_TYPE) == FT_FLOAT) |
| 180 |
{ |
| 181 |
*(float *) p = (float) intval; |
| 182 |
} |
| 183 |
return 1; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
qboolean ReadChar(source_t *source, fielddef_t *fd, void *p) |
| 192 |
{ |
| 193 |
token_t token; |
| 194 |
|
| 195 |
if (!PC_ExpectAnyToken(source, &token)) return 0; |
| 196 |
|
| 197 |
|
| 198 |
if (token.type == TT_LITERAL) |
| 199 |
{ |
| 200 |
StripSingleQuotes(token.string); |
| 201 |
*(char *) p = token.string[0]; |
| 202 |
} |
| 203 |
else |
| 204 |
{ |
| 205 |
PC_UnreadLastToken(source); |
| 206 |
if (!ReadNumber(source, fd, p)) return 0; |
| 207 |
} |
| 208 |
return 1; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
int ReadString(source_t *source, fielddef_t *fd, void *p) |
| 217 |
{ |
| 218 |
token_t token; |
| 219 |
|
| 220 |
if (!PC_ExpectTokenType(source, TT_STRING, 0, &token)) return 0; |
| 221 |
|
| 222 |
StripDoubleQuotes(token.string); |
| 223 |
|
| 224 |
Q_strncpyz((char *) p, token.string, MAX_STRINGFIELD); |
| 225 |
|
| 226 |
return 1; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
int ReadStructure(source_t *source, structdef_t *def, char *structure) |
| 235 |
{ |
| 236 |
token_t token; |
| 237 |
fielddef_t *fd; |
| 238 |
void *p; |
| 239 |
int num; |
| 240 |
|
| 241 |
if (!PC_ExpectTokenString(source, "{")) return 0; |
| 242 |
while(1) |
| 243 |
{ |
| 244 |
if (!PC_ExpectAnyToken(source, &token)) return qfalse; |
| 245 |
|
| 246 |
if (!strcmp(token.string, "}")) break; |
| 247 |
|
| 248 |
fd = FindField(def->fields, token.string); |
| 249 |
if (!fd) |
| 250 |
{ |
| 251 |
SourceError(source, "unknown structure field %s", token.string); |
| 252 |
return qfalse; |
| 253 |
} |
| 254 |
if (fd->type & FT_ARRAY) |
| 255 |
{ |
| 256 |
num = fd->maxarray; |
| 257 |
if (!PC_ExpectTokenString(source, "{")) return qfalse; |
| 258 |
} |
| 259 |
else |
| 260 |
{ |
| 261 |
num = 1; |
| 262 |
} |
| 263 |
p = (void *)(structure + fd->offset); |
| 264 |
while (num-- > 0) |
| 265 |
{ |
| 266 |
if (fd->type & FT_ARRAY) |
| 267 |
{ |
| 268 |
if (PC_CheckTokenString(source, "}")) break; |
| 269 |
} |
| 270 |
switch(fd->type & FT_TYPE) |
| 271 |
{ |
| 272 |
case FT_CHAR: |
| 273 |
{ |
| 274 |
if (!ReadChar(source, fd, p)) return qfalse; |
| 275 |
p = (char *) p + sizeof(char); |
| 276 |
break; |
| 277 |
} |
| 278 |
case FT_INT: |
| 279 |
{ |
| 280 |
if (!ReadNumber(source, fd, p)) return qfalse; |
| 281 |
p = (char *) p + sizeof(int); |
| 282 |
break; |
| 283 |
} |
| 284 |
case FT_FLOAT: |
| 285 |
{ |
| 286 |
if (!ReadNumber(source, fd, p)) return qfalse; |
| 287 |
p = (char *) p + sizeof(float); |
| 288 |
break; |
| 289 |
} |
| 290 |
case FT_STRING: |
| 291 |
{ |
| 292 |
if (!ReadString(source, fd, p)) return qfalse; |
| 293 |
p = (char *) p + MAX_STRINGFIELD; |
| 294 |
break; |
| 295 |
} |
| 296 |
case FT_STRUCT: |
| 297 |
{ |
| 298 |
if (!fd->substruct) |
| 299 |
{ |
| 300 |
SourceError(source, "BUG: no sub structure defined"); |
| 301 |
return qfalse; |
| 302 |
} |
| 303 |
ReadStructure(source, fd->substruct, (char *) p); |
| 304 |
p = (char *) p + fd->substruct->size; |
| 305 |
break; |
| 306 |
} |
| 307 |
} |
| 308 |
if (fd->type & FT_ARRAY) |
| 309 |
{ |
| 310 |
if (!PC_ExpectAnyToken(source, &token)) return qfalse; |
| 311 |
if (!strcmp(token.string, "}")) break; |
| 312 |
if (strcmp(token.string, ",")) |
| 313 |
{ |
| 314 |
SourceError(source, "expected a comma, found %s", token.string); |
| 315 |
return qfalse; |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
return qtrue; |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
int WriteIndent(FILE *fp, int indent) |
| 329 |
{ |
| 330 |
while(indent-- > 0) |
| 331 |
{ |
| 332 |
if (fprintf(fp, "\t") < 0) return qfalse; |
| 333 |
} |
| 334 |
return qtrue; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
int WriteFloat(FILE *fp, float value) |
| 343 |
{ |
| 344 |
char buf[128]; |
| 345 |
int l; |
| 346 |
|
| 347 |
Com_sprintf(buf, sizeof(buf), "%f", value); |
| 348 |
l = strlen(buf); |
| 349 |
|
| 350 |
while(l-- > 1) |
| 351 |
{ |
| 352 |
if (buf[l] != '0' && buf[l] != '.') break; |
| 353 |
if (buf[l] == '.') |
| 354 |
{ |
| 355 |
buf[l] = 0; |
| 356 |
break; |
| 357 |
} |
| 358 |
buf[l] = 0; |
| 359 |
} |
| 360 |
|
| 361 |
if (fprintf(fp, "%s", buf) < 0) return 0; |
| 362 |
return 1; |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
int WriteStructWithIndent(FILE *fp, structdef_t *def, char *structure, int indent) |
| 371 |
{ |
| 372 |
int i, num; |
| 373 |
void *p; |
| 374 |
fielddef_t *fd; |
| 375 |
|
| 376 |
if (!WriteIndent(fp, indent)) return qfalse; |
| 377 |
if (fprintf(fp, "{\r\n") < 0) return qfalse; |
| 378 |
|
| 379 |
indent++; |
| 380 |
for (i = 0; def->fields[i].name; i++) |
| 381 |
{ |
| 382 |
fd = &def->fields[i]; |
| 383 |
if (!WriteIndent(fp, indent)) return qfalse; |
| 384 |
if (fprintf(fp, "%s\t", fd->name) < 0) return qfalse; |
| 385 |
p = (void *)(structure + fd->offset); |
| 386 |
if (fd->type & FT_ARRAY) |
| 387 |
{ |
| 388 |
num = fd->maxarray; |
| 389 |
if (fprintf(fp, "{") < 0) return qfalse; |
| 390 |
} |
| 391 |
else |
| 392 |
{ |
| 393 |
num = 1; |
| 394 |
} |
| 395 |
while(num-- > 0) |
| 396 |
{ |
| 397 |
switch(fd->type & FT_TYPE) |
| 398 |
{ |
| 399 |
case FT_CHAR: |
| 400 |
{ |
| 401 |
if (fprintf(fp, "%d", *(char *) p) < 0) return qfalse; |
| 402 |
p = (char *) p + sizeof(char); |
| 403 |
break; |
| 404 |
} |
| 405 |
case FT_INT: |
| 406 |
{ |
| 407 |
if (fprintf(fp, "%d", *(int *) p) < 0) return qfalse; |
| 408 |
p = (char *) p + sizeof(int); |
| 409 |
break; |
| 410 |
} |
| 411 |
case FT_FLOAT: |
| 412 |
{ |
| 413 |
if (!WriteFloat(fp, *(float *)p)) return qfalse; |
| 414 |
p = (char *) p + sizeof(float); |
| 415 |
break; |
| 416 |
} |
| 417 |
case FT_STRING: |
| 418 |
{ |
| 419 |
if (fprintf(fp, "\"%s\"", (char *) p) < 0) return qfalse; |
| 420 |
p = (char *) p + MAX_STRINGFIELD; |
| 421 |
break; |
| 422 |
} |
| 423 |
case FT_STRUCT: |
| 424 |
{ |
| 425 |
if (!WriteStructWithIndent(fp, fd->substruct, structure, indent)) return qfalse; |
| 426 |
p = (char *) p + fd->substruct->size; |
| 427 |
break; |
| 428 |
} |
| 429 |
} |
| 430 |
if (fd->type & FT_ARRAY) |
| 431 |
{ |
| 432 |
if (num > 0) |
| 433 |
{ |
| 434 |
if (fprintf(fp, ",") < 0) return qfalse; |
| 435 |
} |
| 436 |
else |
| 437 |
{ |
| 438 |
if (fprintf(fp, "}") < 0) return qfalse; |
| 439 |
} |
| 440 |
} |
| 441 |
} |
| 442 |
if (fprintf(fp, "\r\n") < 0) return qfalse; |
| 443 |
} |
| 444 |
indent--; |
| 445 |
|
| 446 |
if (!WriteIndent(fp, indent)) return qfalse; |
| 447 |
if (fprintf(fp, "}\r\n") < 0) return qfalse; |
| 448 |
return qtrue; |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
int WriteStructure(FILE *fp, structdef_t *def, char *structure) |
| 457 |
{ |
| 458 |
return WriteStructWithIndent(fp, def, structure, 0); |
| 459 |
} |
| 460 |
|