-
Notifications
You must be signed in to change notification settings - Fork 1
/
kdb.cpp
673 lines (586 loc) · 16.8 KB
/
kdb.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
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
#include <stdio.h>
#include "kdb.h"
#define MAXLINELEN 4096
#ifdef MYDLL_RELEASE_BUILD
#define KDB_DEBUG(f,x)
#define KDB_DEBUG_OPEN(f, dir)
#define KDB_DEBUG_CLOSE(f)
#else
#define KDB_DEBUG(f,x) if (f != NULL) fprintf x
#define KDB_DEBUG_OPEN(f, dir) {\
char buf[MAXLINELEN]; \
ZeroMemory(buf, MAXLINELEN); \
lstrcpy(buf, dir); lstrcat(buf, "KDB.debug.log"); \
f = fopen(buf, "wt"); \
}
#define KDB_DEBUG_CLOSE(f) if (f != NULL) { fclose(f); f = NULL; }
#endif
#define PLAYERS 0
#define GOALKEEPERS 1
static FILE* klog;
// functions
//////////////////////////////////////////
static KitEntry* buildKitList(int id, char* dir, int kitType);
static BOOL ParseColor(char* str, RGBAColor* color);
static BOOL ParseByte(char* str, BYTE* byte);
// Load attributes for kits of specific team
// params:
// (in) kdb - pointer to KDB
// (in) id - team id.
void kdbLoadUniAttrib(KDB* kdb, int id)
{
char attFileName[MAXFILENAME];
ZeroMemory(attFileName, MAXFILENAME);
lstrcpy(attFileName, kdb->dir);
sprintf(attFileName + lstrlen(attFileName), "KDB/uni/%03d/attrib.cfg", id);
FILE* att = fopen(attFileName, "rt");
if (att == NULL)
return; // no attrib.cfg file => nothing to do.
KDB_DEBUG(klog, (klog, "Found %s\n", attFileName));
BOOL isExtra = FALSE;
Kit* kit = NULL;
KitEntry* extraKitEntry = NULL;
// go line by line
while (!feof(att))
{
char buf[MAXLINELEN];
ZeroMemory(buf, MAXLINELEN);
fgets(buf, MAXLINELEN, att);
if (lstrlen(buf) == 0) break;
// strip off comments
char* comm = strstr(buf, "#");
if (comm != NULL) comm[0] = '\0';
// look for start of the section
char* start = strstr(buf, "[");
if (start != NULL)
{
char* end = strstr(start+1, "]");
if (end != NULL)
{
// new section
char name[MAXFILENAME];
ZeroMemory(name, MAXFILENAME);
lstrcpy(name, start+1);
name[end-start-1] = '\0';
KDB_DEBUG(klog, (klog, "section: {%s}\n", name));
// check for special sections for default kits
isExtra = FALSE;
kit = NULL; extraKitEntry = NULL;
if (lstrcmp(name, "texga.bmp")==0 || lstrcmp(name, "texga.bin")==0)
{
// 1st-choice gk
kit = kdb->goalkeepers[id].a;
}
else if (lstrcmp(name, "texgb.bmp")==0 || lstrcmp(name, "texgb.bin")==0)
{
// 2nd-choice gk
kit = kdb->goalkeepers[id].b;
}
else if (lstrcmp(name, "texpa.bmp")==0 || lstrcmp(name, "texpa.bin")==0)
{
// 1st-choice pl
kit = kdb->players[id].a;
}
else if (lstrcmp(name, "texpb.bmp")==0 || lstrcmp(name, "texpb.bin")==0)
{
// 2nd-choice pl
kit = kdb->players[id].b;
}
else if (strncmp(name, "gx/", 3)==0)
{
// extra gk kit
// find matching kit among extras
KitEntry* p = kdb->goalkeepers[id].extra;
while (p != NULL)
{
if (lstrcmp(p->kit->fileName, name)==0)
{
kit = p->kit;
break;
}
p = p->next;
}
}
else if (strncmp(name, "px/", 3)==0)
{
// extra pl kit
// find matching kit among extras
KitEntry* p = kdb->players[id].extra;
while (p != NULL)
{
if (lstrcmp(p->kit->fileName, name)==0)
{
kit = p->kit;
break;
}
p = p->next;
}
}
}
}
// Otherwise, look for attribute definitions
else
{
if (kit == NULL)
continue; // unknown current kit, so nothing can be assigned.
char key[80]; ZeroMemory(key, 80);
char value[80]; ZeroMemory(value, 80);
if (sscanf(buf,"%s = %s",key,value)==2)
{
KDB_DEBUG(klog, (klog, "key: {%s} has value: {%s}\n", key, value));
RGBAColor color;
if (lstrcmp(key, "shirt.name")==0)
{
if (ParseColor(value, &kit->shirtName))
kit->attDefined |= SHIRT_NAME;
}
else if (lstrcmp(key, "shirt.number")==0)
{
if (ParseColor(value, &kit->shirtNumber))
kit->attDefined |= SHIRT_NUMBER;
}
else if (lstrcmp(key, "shorts.number")==0)
{
if (ParseColor(value, &kit->shortsNumber))
kit->attDefined |= SHORTS_NUMBER;
}
else if (lstrcmp(key, "collar")==0)
{
kit->collar = (lstrcmp(value,"yes")==0) ? 0 : 1;
kit->attDefined |= COLLAR;
}
else if (lstrcmp(key, "cuff")==0)
{
kit->cuff = (lstrcmp(value,"yes")==0) ? 1 : 0;
kit->attDefined |= CUFF;
}
else if (lstrcmp(key, "shorts.number.location")==0)
{
if (lstrcmp(value,"off")==0) {
kit->shortsNumberLocation = 0;
kit->attDefined |= SHORTS_NUMBER_LOCATION;
}
else if (lstrcmp(value,"left")==0) {
kit->shortsNumberLocation = 1;
kit->attDefined |= SHORTS_NUMBER_LOCATION;
}
else if (lstrcmp(value,"right")==0) {
kit->shortsNumberLocation = 2;
kit->attDefined |= SHORTS_NUMBER_LOCATION;
}
else if (lstrcmp(value,"both")==0) {
kit->shortsNumberLocation = 3;
kit->attDefined |= SHORTS_NUMBER_LOCATION;
}
}
else if (lstrcmp(key, "number.type")==0)
{
if (ParseByte(value, &kit->numberType))
kit->attDefined |= NUMBER_TYPE;
}
else if (lstrcmp(key, "name.type")==0)
{
if (ParseByte(value, &kit->nameType))
kit->attDefined |= NAME_TYPE;
}
else if (lstrcmp(key, "name.shape")==0)
{
if (lstrcmp(value, "straight")==0) {
kit->nameShape = 0;
kit->attDefined |= NAME_SHAPE;
}
else if (lstrcmp(value, "curved")==0) {
kit->nameShape = 1;
kit->attDefined |= NAME_SHAPE;
}
}
else if (lstrcmp(key, "model")==0)
{
if (ParseByte(value, &kit->model))
kit->attDefined |= MODEL;
}
}
}
// go to next line
}
fclose(att);
}
// Load balls
// (in) kdb - pointer to KDB
void kdbLoadBalls(KDB* kdb)
{
HANDLE heap = GetProcessHeap();
char attFileName[MAXFILENAME];
ZeroMemory(attFileName, MAXFILENAME);
lstrcpy(attFileName, kdb->dir);
sprintf(attFileName + lstrlen(attFileName), "KDB/balls/attrib.cfg");
FILE* att = fopen(attFileName, "rt");
if (att == NULL)
return; // no attrib.cfg file => nothing to do.
KDB_DEBUG(klog, (klog, "Found %s\n", attFileName));
Ball* ball = NULL;
BallEntry* ballEntry = NULL;
// go line by line
while (!feof(att))
{
char buf[MAXLINELEN];
ZeroMemory(buf, MAXLINELEN);
fgets(buf, MAXLINELEN, att);
if (lstrlen(buf) == 0) break;
// strip off comments
char* comm = strstr(buf, "#");
if (comm != NULL) comm[0] = '\0';
// look for start of the section
char* start = strstr(buf, "[");
if (start != NULL)
{
char* end = strstr(start+1, "]");
if (end != NULL)
{
// new section
char name[MAXFILENAME];
ZeroMemory(name, MAXFILENAME);
lstrcpy(name, start+1);
name[end-start-1] = '\0';
KDB_DEBUG(klog, (klog, "section: {%s}\n", name));
// store previous ball in BallEntry
if (ball != NULL)
{
kdb->ballsLast = (BallEntry*)HeapAlloc(heap, HEAP_ZERO_MEMORY, sizeof(BallEntry));
kdb->ballsLast->ball = ball;
kdb->ballsLast->next = ballEntry;
kdb->ballCount++;
ballEntry = kdb->ballsLast;
}
// set new current ball
ball = (Ball*)HeapAlloc(heap, HEAP_ZERO_MEMORY, sizeof(Ball));
lstrcpy(ball->name, name);
}
}
// Otherwise, look for attribute definitions
else
{
if (ball == NULL)
continue; // unknown current ball, so nothing can be assigned.
char key[80]; ZeroMemory(key, 80);
char value[80]; ZeroMemory(value, 80);
if (sscanf(buf,"%s = %s",key,value)==2)
{
KDB_DEBUG(klog, (klog, "key: {%s} has value: {%s}\n", key, value));
// strip off double quotes, if present
if (value[0]=='"' && value[strlen(value)-1]=='"')
{
char copy[80]; ZeroMemory(copy, 80);
memcpy(copy, value+1, strlen(value)-2);
ZeroMemory(value, 80);
lstrcpy(value, copy);
}
if (lstrcmp(key, "ball.model")==0)
{
lstrcpy(ball->mdlFileName, value);
}
else if (lstrcmp(key, "ball.texture")==0)
{
lstrcpy(ball->texFileName, value);
// see if it's a BMP file
char* ext = value + lstrlen(value) - 4;
if (lstrcmpi(ext, ".bmp")==0)
ball->isTexBmp = TRUE;
}
}
}
// go to next line
}
// store last ball in BallEntry
if (ball != NULL)
{
kdb->ballsLast = (BallEntry*)HeapAlloc(heap, HEAP_ZERO_MEMORY, sizeof(BallEntry));
kdb->ballsLast->ball = ball;
kdb->ballsLast->next = ballEntry;
kdb->ballCount++;
ballEntry = kdb->ballsLast;
}
// reverse the ball list so that balls appear in the
// same order as they are configured in attrib.cfg file
// Also, initialize the "prev" fields so that we have
// a proper double-linked list
BallEntry* bp = kdb->ballsLast;
kdb->ballsFirst = NULL;
while (bp != NULL)
{
BallEntry* bq = bp->next;
BallEntry* newb = (BallEntry*)HeapAlloc(heap, HEAP_ZERO_MEMORY, sizeof(BallEntry));
newb->ball = bp->ball;
// forward link
newb->next = kdb->ballsFirst;
// backward link for previos item
if (kdb->ballsFirst != NULL)
kdb->ballsFirst->prev = newb;
else
kdb->ballsLast = newb;
kdb->ballsFirst = newb;
HeapFree(heap, 0, bp);
bp = bq;
}
fclose(att);
}
KDB* kdbLoad(char* dir)
{
KDB_DEBUG_OPEN(klog, dir);
KDB_DEBUG(klog, (klog, "Loading KDB...\n"));
HANDLE heap = GetProcessHeap();
// initialize an empty database
KDB* kdb = (KDB*)HeapAlloc(heap, HEAP_ZERO_MEMORY, sizeof(KDB));
if (kdb == NULL) { fclose(klog); return NULL; }
strncpy(kdb->dir, dir, MAXFILENAME);
kdb->playerCount = kdb->goalkeeperCount = kdb->ballCount = 0;
ZeroMemory(kdb->players, sizeof(Kits) * 256);
ZeroMemory(kdb->goalkeepers, sizeof(Kits) * 256);
kdb->ballsFirst = NULL;
kdb->ballsLast = NULL;
// Kits
for (int n=0; n<256; n++)
{
// create Kit structures for default goalkeeper kits
kdb->goalkeepers[n].a = (Kit*)HeapAlloc(heap, HEAP_ZERO_MEMORY, sizeof(Kit));
if (kdb->goalkeepers[n].a == NULL)
{
KDB_DEBUG(klog, (klog, "memory allocation problem\n"));
}
else
lstrcpy(kdb->goalkeepers[n].a->fileName, "texga.bmp");
kdb->goalkeepers[n].b = (Kit*)HeapAlloc(heap, HEAP_ZERO_MEMORY, sizeof(Kit));
if (kdb->goalkeepers[n].b == NULL)
{
KDB_DEBUG(klog, (klog, "memory allocation problem\n"));
}
else
lstrcpy(kdb->goalkeepers[n].b->fileName, "texgb.bmp");
// create Kit structures for default player kits
kdb->players[n].a = (Kit*)HeapAlloc(heap, HEAP_ZERO_MEMORY, sizeof(Kit));
if (kdb->players[n].a == NULL)
{
KDB_DEBUG(klog, (klog, "memory allocation problem\n"));
}
else
lstrcpy(kdb->players[n].a->fileName, "texpa.bmp");
kdb->players[n].b = (Kit*)HeapAlloc(heap, HEAP_ZERO_MEMORY, sizeof(Kit));
if (kdb->players[n].b == NULL)
{
KDB_DEBUG(klog, (klog, "memory allocation problem\n"));
}
else
lstrcpy(kdb->players[n].b->fileName, "texpb.bmp");
// create list of gx kits
KitEntry* gx = buildKitList(n, dir, GOALKEEPERS);
// traverse the list and place the kits into the database
KitEntry* p = gx;
KitEntry* q = NULL;
while (p != NULL)
{
q = p->next;
KDB_DEBUG(klog, (klog, "p->kit->filename = %s\n", p->kit->fileName));
p->next = kdb->goalkeepers[n].extra;
kdb->goalkeepers[n].extra = p;
kdb->goalkeeperCount += 1;
KDB_DEBUG(klog, (klog, "[%03d] : %s\n", n, p->kit->fileName));
p = q;
}
// create list of px kits
KitEntry* px = buildKitList(n, dir, PLAYERS);
// traverse the list and place the kits into the database
p = px;
q = NULL;
while (p != NULL)
{
q = p->next;
KDB_DEBUG(klog, (klog, "p->kit->filename = %s\n", p->kit->fileName));
p->next = kdb->players[n].extra;
kdb->players[n].extra = p;
kdb->playerCount += 1;
KDB_DEBUG(klog, (klog, "[%03d] : %s\n", n, p->kit->fileName));
p = q;
}
// read the attributes from attrib.cfg file
kdbLoadUniAttrib(kdb, n);
// place 2 GK kits into "extra" list to allow for explicit switching
KitEntry* aKitEntry = (KitEntry*)HeapAlloc(heap, HEAP_ZERO_MEMORY, sizeof(KitEntry));
if (aKitEntry == NULL)
{
KDB_DEBUG(klog, (klog, "memory allocation problem\n"));
}
else
{
aKitEntry->kit = kdb->goalkeepers[n].a;
if (n == 78)
{
KDB_DEBUG(klog, (klog, "Newcastle GK: ke->kit = %08x\n", (DWORD)aKitEntry->kit));
}
}
KitEntry* bKitEntry = (KitEntry*)HeapAlloc(heap, HEAP_ZERO_MEMORY, sizeof(KitEntry));
if (bKitEntry == NULL)
{
KDB_DEBUG(klog, (klog, "memory allocation problem\n"));
}
else
{
bKitEntry->kit = kdb->goalkeepers[n].b;
if (n == 78)
{
KDB_DEBUG(klog, (klog, "Newcastle GK: ke->kit = %08x\n", (DWORD)bKitEntry->kit));
}
}
bKitEntry->next = kdb->goalkeepers[n].extra;
aKitEntry->next = bKitEntry;
kdb->goalkeepers[n].extra = aKitEntry;
if (n == 78)
{
KDB_DEBUG(klog, (klog, "Newcastle goalkeepers:\n"));
KitEntry* ke = kdb->goalkeepers[n].extra;
while (ke != NULL)
{
KDB_DEBUG(klog, (klog, "ke->kit = %08x\n", (DWORD)ke->kit));
ke = ke->next;
}
}
}
// Balls
kdbLoadBalls(kdb);
KDB_DEBUG(klog, (klog, "KDB loaded.\n"));
KDB_DEBUG_CLOSE(klog);
return kdb;
}
// parses a RRGGBB string into RGBAColor structure
BOOL ParseColor(char* str, RGBAColor* color)
{
int len = lstrlen(str);
if (!(len == 6 || len == 8))
return FALSE;
int num = 0;
if (sscanf(str,"%x",&num)!=1) return FALSE;
if (len == 6) {
// assume alpha as fully opaque.
color->r = (BYTE)((num >> 16) & 0xff);
color->g = (BYTE)((num >> 8) & 0xff);
color->b = (BYTE)(num & 0xff);
color->a = 0xff; // set alpha to opaque
}
else {
color->r = (BYTE)((num >> 24) & 0xff);
color->g = (BYTE)((num >> 16) & 0xff);
color->b = (BYTE)((num >> 8) & 0xff);
color->a = (BYTE)(num & 0xff);
}
KDB_DEBUG(klog, (klog, "RGBA color: %02x,%02x,%02x,%02x\n",
color->r, color->g, color->b, color->a));
return TRUE;
}
// parses a decimal number string into actual BYTE value
BOOL ParseByte(char* str, BYTE* byte)
{
int num = 0;
if (sscanf(str,"%d",&num)!=1) return FALSE;
*byte = (BYTE)num;
return TRUE;
}
KitEntry* buildKitList(int id, char* dir, int kitType)
{
WIN32_FIND_DATA fData;
char pattern[MAXLINELEN];
ZeroMemory(pattern, MAXLINELEN);
KitEntry* list = NULL;
KitEntry* p = NULL;
HANDLE heap = GetProcessHeap();
lstrcpy(pattern, dir);
if (kitType == PLAYERS)
sprintf(pattern + lstrlen(pattern), "KDB/uni/%03d/px/*.bmp", id);
else if (kitType == GOALKEEPERS)
sprintf(pattern + lstrlen(pattern), "KDB/uni/%03d/gx/*.bmp", id);
//KDB_DEBUG(klog, (klog, "pattern = {%s}\n", pattern));
HANDLE hff = FindFirstFile(pattern, &fData);
if (hff == INVALID_HANDLE_VALUE)
{
// none found.
return NULL;
}
while(true)
{
// build new list element
Kit* kit = (Kit*)HeapAlloc(heap, HEAP_ZERO_MEMORY, sizeof(Kit));
if (kit != NULL)
{
ZeroMemory(kit->fileName, MAXFILENAME);
lstrcpy(kit->fileName, (kitType == PLAYERS) ? "px/" : "gx/");
lstrcat(kit->fileName, fData.cFileName);
ZeroMemory(&kit->shirtName, sizeof(RGBAColor));
ZeroMemory(&kit->shirtNumber, sizeof(RGBAColor));
ZeroMemory(&kit->shortsNumber, sizeof(RGBAColor));
kit->attDefined = 0;
p = (KitEntry*)HeapAlloc(heap, HEAP_ZERO_MEMORY, sizeof(KitEntry));
if (p != NULL)
{
// insert new element
p->kit = kit;
p->next = list;
list = p;
KDB_DEBUG(klog, (klog, "list->kit->fileName = {%s}\n", list->kit->fileName));
}
else
{
HeapFree(heap, 0, kit);
}
}
// proceed to next file
if (!FindNextFile(hff, &fData)) break;
}
FindClose(hff);
return list;
}
void kdbUnload(KDB* kdb)
{
if (kdb == NULL) return;
HANDLE heap = GetProcessHeap();
KitEntry* q = NULL;
BallEntry* bq = NULL;
// free player and goalkeeper kits memory
for (int i=0; i<256; i++)
{
// default kits
if (kdb->goalkeepers[i].a != NULL)
HeapFree(heap, 0, kdb->goalkeepers[i].a);
if (kdb->goalkeepers[i].b != NULL)
HeapFree(heap, 0, kdb->goalkeepers[i].b);
if (kdb->players[i].a != NULL)
HeapFree(heap, 0, kdb->players[i].a);
if (kdb->players[i].b != NULL)
HeapFree(heap, 0, kdb->players[i].b);
// extra PLAYERS
KitEntry* p = kdb->players[i].extra;
while (p != NULL)
{
q = p->next;
if (p->kit != NULL) HeapFree(heap, 0, p->kit);
HeapFree(heap, 0, p);
p = q;
}
// extra GOALKEEPERS
int count = 0;
p = kdb->goalkeepers[i].extra;
while (p != NULL)
{
q = p->next;
if (count++ >= 2) HeapFree(heap, 0, p->kit); // first 2 are already freed earlier
HeapFree(heap, 0, p);
p = q;
}
}
// free balls memory
BallEntry* be = kdb->ballsFirst;
while (be != NULL)
{
bq = be->next;
HeapFree(heap, 0, be->ball);
HeapFree(heap, 0, be);
be = bq;
}
}