/
d2itemreader.c
1813 lines (1626 loc) · 48.8 KB
/
d2itemreader.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
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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "d2itemreader.h"
#include "d2gamedata.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define BITS_PER_BYTE 8
#define D2ITEMREADER_DATA (data + curByte)
#define D2ITEMREADER_INC(T) curByte += sizeof(T)
#define D2ITEMREADER_SKIP(T) if (curByte+sizeof(T)<=dataSizeBytes) { D2ITEMREADER_INC(T); }
#define D2ITEMREADER_READ(T) (curByte+sizeof(T)<=dataSizeBytes ? *(T*)D2ITEMREADER_DATA : (T)0); D2ITEMREADER_SKIP(T)
#define D2ITEMREADER_STREAM_DATA (stream->source.data + stream->source.curByte)
#define D2ITEMREADER_STREAM_INC(T) stream->source.curByte += sizeof(T)
#define D2ITEMREADER_STREAM_SKIP(T) if (stream->source.curByte+sizeof(T)<=stream->source.dataSizeBytes) { D2ITEMREADER_STREAM_INC(T); }
#define D2ITEMREADER_STREAM_READ(T) (stream->source.curByte+sizeof(T)<=stream->source.dataSizeBytes ? *(T*)D2ITEMREADER_STREAM_DATA : (T)0); D2ITEMREADER_STREAM_SKIP(T)
d2filetype d2filetype_get(const uint8_t* data, size_t size)
{
if (size < 4)
return D2FILETYPE_UNKNOWN;
uint16_t header16 = *(uint16_t*)(data);
if (header16 == D2_JM_TAG)
{
return D2FILETYPE_D2_ITEM;
}
if (data[0] == 'D' && data[1] == '2' && data[2] == 'X')
{
return D2FILETYPE_ATMA_STASH;
}
uint32_t header = *(uint32_t*)(data);
switch (header)
{
case D2S_HEADER:
return D2FILETYPE_D2_CHARACTER;
case PLUGY_PERSONALSTASH_HEADER:
return D2FILETYPE_PLUGY_PERSONAL_STASH;
case PLUGY_SHAREDSTASH_HEADER:
return D2FILETYPE_PLUGY_SHARED_STASH;
default:
return D2FILETYPE_UNKNOWN;
}
}
d2filetype d2filetype_of_file(const char* filename)
{
FILE* file = fopen(filename, "rb");
if (file == NULL)
return D2FILETYPE_UNKNOWN;
uint32_t header;
size_t bytesRead = fread(&header, 1, 4, file);
fclose(file);
return d2filetype_get((uint8_t*)&header, bytesRead);
}
CHECK_RESULT d2err d2itemreader_parse_any_file(const char* filename, d2itemlist *itemList, d2gamedata *gameData, size_t* out_bytesRead)
{
d2err err;
uint8_t* data;
size_t dataSizeBytes;
err = d2util_read_file(filename, &data, &dataSizeBytes);
if (err != D2ERR_OK)
{
*out_bytesRead = 0;
return err;
}
err = d2itemreader_parse_any(data, dataSizeBytes, itemList, gameData, out_bytesRead);
free(data);
return err;
}
CHECK_RESULT d2err d2itemreader_parse_any(const uint8_t* const data, size_t dataSizeBytes, d2itemlist *itemList, d2gamedata *gameData, size_t* out_bytesRead)
{
d2err err;
*out_bytesRead = 0;
d2itemreader_stream stream;
if ((err = d2itemreader_open_buffer(&stream, data, dataSizeBytes, gameData)) != D2ERR_OK)
{
return err;
}
if ((err = d2itemlist_init(itemList, 1)) != D2ERR_OK)
{
d2itemreader_close(&stream);
return err;
}
d2item item;
while (d2itemreader_next(&stream, &item))
{
if ((err = d2itemlist_append(itemList, &item)) != D2ERR_OK)
{
*out_bytesRead = stream.source.curByte;
d2item_destroy(&item);
d2itemlist_destroy(itemList);
d2itemreader_close(&stream);
return err;
}
}
if (stream.err != D2ERR_OK)
{
*out_bytesRead = stream.source.curByte;
d2itemlist_destroy(itemList);
d2itemreader_close(&stream);
return stream.err;
}
*out_bytesRead = stream.source.curByte;
return D2ERR_OK;
}
// Parses the magical property list in the byte queue that belongs to an item
// and returns the list of properties.
CHECK_RESULT d2err d2itemproplist_parse(d2bitreader* br, d2itemproplist* list, d2gamedata *gameData)
{
if (gameData->initState != D2DATA_INIT_STATE_ALL)
{
return D2ERR_DATA_NOT_LOADED;
}
d2err err;
if ((err = d2itemproplist_init(list, 4)) != D2ERR_OK)
{
return err;
}
while (true)
{
uint16_t id = (uint16_t)d2bitreader_read(br, 9);
if (br->cursor == BIT_READER_CURSOR_BEYOND_EOF)
{
err = D2ERR_PARSE_UNEXPECTED_EOF;
goto err;
}
// If all 9 bits are set, we've hit the end of the list
if (id == D2DATA_ITEMSTAT_END_ID)
break;
if (id > D2DATA_ITEMSTAT_END_ID)
{
err = D2ERR_PARSE;
goto err;
}
d2gamedata_itemstat* stat = &gameData->itemstats[id];
d2itemprop prop = { id };
// saveBits being zero or >= 64 is unrecoverably bad, and
// encode type 4 is only used by stats that were never implemented (time-based stats)
if (stat->saveBits == 0 || stat->saveBits >= BIT_READER_RAW_READ_SIZE_BITS || stat->encode == 4)
{
err = D2ERR_PARSE;
goto err;
}
if (stat->encode == 2)
{
if (!(stat->saveBits == 7 && stat->saveParamBits == 16))
{
err = D2ERR_PARSE;
goto err;
}
prop.params[0] = (int)d2bitreader_read(br, 6) - stat->saveAdd;
prop.params[1] = (int)d2bitreader_read(br, 10) - stat->saveAdd;
prop.params[2] = (int)d2bitreader_read(br, stat->saveBits) - stat->saveAdd;
prop.numParams = 3;
}
else if (stat->encode == 3)
{
if (!(stat->saveBits == 16 && stat->saveParamBits == 16))
{
err = D2ERR_PARSE;
goto err;
}
prop.params[0] = (int)d2bitreader_read(br, 6) - stat->saveAdd;
prop.params[1] = (int)d2bitreader_read(br, 10) - stat->saveAdd;
prop.params[2] = (int)d2bitreader_read(br, 8) - stat->saveAdd;
prop.params[3] = (int)d2bitreader_read(br, 8) - stat->saveAdd;
prop.numParams = 4;
}
else if (stat->saveParamBits > 0)
{
prop.params[0] = (int)d2bitreader_read(br, stat->saveParamBits) - stat->saveAdd;
prop.params[1] = (int)d2bitreader_read(br, stat->saveBits) - stat->saveAdd;
prop.numParams = 2;
}
else
{
prop.params[0] = (int)d2bitreader_read(br, stat->saveBits) - stat->saveAdd;
prop.numParams = 1;
}
while (stat->nextInChain && prop.numParams < D2_ITEMPROP_MAX_PARAMS)
{
stat = &gameData->itemstats[stat->nextInChain];
if (stat->saveParamBits != 0)
{
err = D2ERR_PARSE;
goto err;
}
prop.params[prop.numParams] = (int)d2bitreader_read(br, stat->saveBits) - stat->saveAdd;
prop.numParams++;
}
if ((err = d2itemproplist_append(list, prop)) != D2ERR_OK)
{
goto err;
}
}
return D2ERR_OK;
err:
d2itemproplist_destroy(list);
return err;
}
CHECK_RESULT d2err d2itemproplist_init(d2itemproplist* list, size_t initialSize)
{
list->count = 0;
list->_size = initialSize;
list->properties = malloc(list->_size * sizeof(*list->properties));
return list->properties != NULL ? D2ERR_OK : D2ERR_OUT_OF_MEMORY;
}
CHECK_RESULT d2err d2itemproplist_append(d2itemproplist* list, d2itemprop prop)
{
if (list->count == list->_size)
{
list->_size *= 2;
void* tmp = realloc(list->properties, list->_size * sizeof(*list->properties));
if (tmp == NULL)
{
return D2ERR_OUT_OF_MEMORY;
}
list->properties = tmp;
}
list->properties[list->count++] = prop;
return D2ERR_OK;
}
CHECK_RESULT d2err d2itemproplist_copy(d2itemproplist* dest, const d2itemproplist* const src)
{
d2err err;
if ((err = d2itemproplist_init(dest, src->count)) != D2ERR_OK)
{
return err;
}
for (size_t i=0; i < src->count; i++)
{
if ((err = d2itemproplist_append(dest, src->properties[i])) != D2ERR_OK)
{
d2itemproplist_destroy(dest);
return err;
}
}
return D2ERR_OK;
}
void d2itemproplist_destroy(d2itemproplist* list)
{
if (list->properties)
{
free(list->properties);
}
list->properties = NULL;
list->count = list->_size = 0;
}
void d2itemlist_parse_header(d2itemreader_stream* stream)
{
uint16_t tag = D2ITEMREADER_STREAM_READ(uint16_t) else { goto eof; }
if (tag != D2_JM_TAG)
{
stream->source.curByte -= sizeof(uint16_t);
stream->err = D2ERR_PARSE_BAD_HEADER_OR_TAG;
return;
}
stream->state.curItem = 0;
stream->state.numItems = D2ITEMREADER_STREAM_READ(uint16_t) else { goto eof; }
stream->state.parseState = PARSE_STATE_ITEM_READY;
return;
eof:
stream->source.curByte = stream->source.dataSizeBytes;
stream->err = D2ERR_PARSE_UNEXPECTED_EOF;
}
CHECK_RESULT d2err d2itemlist_init(d2itemlist* list, size_t initialSize)
{
d2itemlist_init_empty(list);
if (list->_size > 0)
{
list->_size = initialSize;
list->items = malloc(list->_size * sizeof(*list->items));
if (list->items == NULL)
{
return D2ERR_OUT_OF_MEMORY;
}
}
return D2ERR_OK;
}
void d2itemlist_init_empty(d2itemlist* list)
{
list->count = 0;
list->_size = 0;
list->items = NULL;
}
CHECK_RESULT d2err d2itemlist_append(d2itemlist* list, const d2item* const item)
{
if (list->count == list->_size)
{
list->_size = list->_size > 0 ? list->_size * 2 : 4;
void* tmp = realloc(list->items, list->_size * sizeof(*list->items));
if (tmp == NULL)
{
return D2ERR_OUT_OF_MEMORY;
}
list->items = tmp;
}
list->items[list->count++] = *item;
return D2ERR_OK;
}
CHECK_RESULT d2err d2itemlist_append_copy(d2itemlist* list, const d2item* const item)
{
d2err err;
if (list->count == list->_size)
{
list->_size = list->_size > 0 ? list->_size * 2 : 4;
void* tmp = realloc(list->items, list->_size * sizeof(*list->items));
if (tmp == NULL)
{
return D2ERR_OUT_OF_MEMORY;
}
list->items = tmp;
}
d2item* dest = &list->items[list->count];
if ((err = d2item_copy(dest, item)) != D2ERR_OK)
{
return err;
}
list->count++;
return D2ERR_OK;
}
CHECK_RESULT d2err d2itemlist_append_list_copy(d2itemlist* dest, const d2itemlist* const src)
{
d2err err;
for (size_t i=0; i < src->count; i++)
{
d2item* item = &(src->items[i]);
if ((err = d2itemlist_append_copy(dest, item)) != D2ERR_OK)
{
return err;
}
}
return D2ERR_OK;
}
CHECK_RESULT d2err d2itemlist_copy(d2itemlist* dest, const d2itemlist* const src)
{
d2err err;
if ((err = d2itemlist_init(dest, src->count)) != D2ERR_OK)
{
return err;
}
for (size_t i=0; i < src->count; i++)
{
d2item* srcitem = &src->items[i];
d2item* destitem = &dest->items[i];
if ((err = d2item_copy(destitem, srcitem)) != D2ERR_OK)
{
d2itemlist_destroy(dest);
return err;
}
dest->count++;
}
return D2ERR_OK;
}
void d2itemlist_destroy(d2itemlist* list)
{
if (list->items)
{
for (unsigned int i = 0; i < list->count; i++)
{
d2item_destroy(&(list->items[i]));
}
free(list->items);
}
list->items = NULL;
list->count = list->_size = 0;
}
CHECK_RESULT d2err d2item_parse_file(const char* filename, d2item* item, d2gamedata *gameData, size_t* out_bytesRead)
{
d2err err;
uint8_t* data;
size_t dataSizeBytes;
err = d2util_read_file(filename, &data, &dataSizeBytes);
if (err != D2ERR_OK)
{
*out_bytesRead = 0;
return err;
}
err = d2item_parse(data, dataSizeBytes, 0, item, gameData, out_bytesRead);
free(data);
return err;
}
CHECK_RESULT d2err d2item_parse(const uint8_t* const data, size_t dataSizeBytes, size_t startByte, d2item* item, d2gamedata *gameData, size_t* out_bytesRead)
{
size_t curByte = startByte;
size_t bytesRead;
d2err err = d2item_parse_single(data, dataSizeBytes, startByte, item, gameData, &bytesRead);
if (err != D2ERR_OK)
{
*out_bytesRead = bytesRead;
return err;
}
// any socketed item here is an error (only child items should be socketed)
if (item->locationID == D2LOCATION_SOCKETED)
{
// don't increment curByte so that the error points to the start of the item
err = D2ERR_PARSE_UNEXPECTED_SOCKETED_ITEM;
goto err;
}
curByte += bytesRead;
if (item->simpleItem)
{
goto done;
}
for (uint8_t i = 0; i < item->numItemsInSockets; i++)
{
size_t itemSizeBytes;
d2item childItem = { 0 };
if ((err = d2item_parse_single(data, dataSizeBytes, curByte, &childItem, gameData, &itemSizeBytes)) != D2ERR_OK)
{
curByte += itemSizeBytes;
goto err;
}
if (childItem.locationID != D2LOCATION_SOCKETED)
{
err = D2ERR_PARSE_UNEXPECTED_NONSOCKETED_ITEM;
d2item_destroy(&childItem);
goto err;
}
if ((err = d2itemlist_append(&item->socketedItems, &childItem)) != D2ERR_OK)
{
d2item_destroy(&childItem);
goto err;
}
curByte += itemSizeBytes;
}
done:
*out_bytesRead = curByte - startByte;
return D2ERR_OK;
err:
*out_bytesRead = curByte - startByte;
d2item_destroy(item);
return err;
}
CHECK_RESULT d2err d2item_parse_single(const uint8_t* const data, size_t dataSizeBytes, size_t startByte, d2item* item, d2gamedata *gameData, size_t* out_bytesRead)
{
if (gameData->initState != D2DATA_INIT_STATE_ALL)
{
*out_bytesRead = 0;
return D2ERR_DATA_NOT_LOADED;
}
d2err err;
size_t curByte = startByte;
uint16_t tag = D2ITEMREADER_READ(uint16_t) else { goto eof; }
if (tag != D2_JM_TAG)
{
*out_bytesRead = 0;
return D2ERR_PARSE_BAD_HEADER_OR_TAG;
}
// memset everything to 0 just to be safe
memset(item, 0, sizeof(d2item));
d2bitreader br = { data, dataSizeBytes, curByte, curByte * BITS_PER_BYTE, 16 };
// offset: 16, unknown
d2bitreader_skip(&br, 4);
// offset: 20
item->identified = d2bitreader_read(&br, 1);
// offset: 21, unknown
d2bitreader_skip(&br, 6);
// offset: 27
item->socketed = d2bitreader_read(&br, 1);
// offset 28, unknown
d2bitreader_read(&br, 1);
// offset 29
item->isNew = d2bitreader_read(&br, 1);
// offset 30, unknown
d2bitreader_read(&br, 2);
// offset 32
item->isEar = d2bitreader_read(&br, 1);
// offset 33
item->starterItem = d2bitreader_read(&br, 1);
// offset 34, unknown
d2bitreader_read(&br, 3);
// offset 37, if it's a simple item, it only contains 111 bits data
item->simpleItem = d2bitreader_read(&br, 1);
// offset 38
item->ethereal = d2bitreader_read(&br, 1);
// offset 39, unknown
d2bitreader_read(&br, 1);
// offset 40
item->personalized = d2bitreader_read(&br, 1);
// offset 41, unknown
d2bitreader_read(&br, 1);
// offset 42
item->isRuneword = d2bitreader_read(&br, 1);
// offset 43, unknown
d2bitreader_skip(&br, 5);
// offset 48, version
item->version = (uint8_t)d2bitreader_read(&br, 8);
// offset 56, unknown
d2bitreader_skip(&br, 2);
// offset 58
item->locationID = (uint8_t)d2bitreader_read(&br, 3);
// offset 61
item->equippedID = (uint8_t)d2bitreader_read(&br, 4);
// offset 65
item->positionX = (uint8_t)d2bitreader_read(&br, 4);
// offset 69
item->positionY = (uint8_t)d2bitreader_read(&br, 4);
// offset 73, if item is neither equipped or in the belt, this tells us where it is.
item->panelID = (uint8_t)d2bitreader_read(&br, 3);
if (!item->isEar)
{
// offset 76, item type, 4 chars, each 8 bit (not byte aligned)
// also not null terminated, item codes can be 4 chars long
for (int i = 0; i < 4; i++)
{
char c = (char)d2bitreader_read(&br, 8);
if (c == ' ') c = '\0';
item->code[i] = c;
}
item->code[D2_ITEM_CODE_STRLEN] = '\0';
// offset 108
// If sockets exist, read the items, they'll be 108 bit basic items * nrOfSockets
item->numItemsInSockets = (uint8_t)d2bitreader_read(&br, 3);
if ((err = d2itemlist_init(&item->socketedItems, item->numItemsInSockets)) != D2ERR_OK)
{
goto exit;
}
}
else
{
// offset 76, the item is an ear, we need to read the ear data.
item->ear.classID = (uint8_t)d2bitreader_read(&br, 3);
item->ear.level = (uint8_t)d2bitreader_read(&br, 7);
for (int i = 0; i < D2_MAX_CHAR_NAME_BYTELEN; i++)
{
char c = (char)d2bitreader_read(&br, 7);
item->ear.name[i] = c;
if (c == '\0') break;
}
item->ear.name[D2_MAX_CHAR_NAME_STRLEN] = '\0';
}
if (!item->simpleItem)
{
// offset 111, item id is 8 chars, each 4 bit
// note: printf("%08X", item->id) will match what GoMule and
// Hero Editor display
item->id = (uint32_t)d2bitreader_read(&br, 32);
// offset 143
item->level = (uint8_t)d2bitreader_read(&br, 7);
// offset 150
item->rarity = (uint8_t)d2bitreader_read(&br, 4);
// If this is TRUE, it means the item has more than one picture associated
// with it.
item->multiplePictures = d2bitreader_read(&br, 1);
if (item->multiplePictures)
{
// The next 3 bits contain the picture ID.
item->pictureID = (uint8_t)d2bitreader_read(&br, 3);
}
// If this is TRUE, it means the item is class specific.
item->classSpecific = d2bitreader_read(&br, 1);
// If the item is class specific, the next 11 bits will
// contain the class specific data.
if (item->classSpecific)
{
item->automagicID = (uint16_t)d2bitreader_read(&br, 11);
}
switch (item->rarity)
{
case D2RARITY_LOW_QUALITY:
item->lowQualityID = (uint8_t)d2bitreader_read(&br, 3);
break;
case D2RARITY_NORMAL:
// No extra data present
break;
case D2RARITY_HIGH_QUALITY:
item->superiorID = (uint8_t)d2bitreader_read(&br, 3);
break;
case D2RARITY_MAGIC:
item->magicPrefix = (uint16_t)d2bitreader_read(&br, 11);
item->magicSuffix = (uint16_t)d2bitreader_read(&br, 11);
break;
case D2RARITY_SET:
item->setID = (uint16_t)d2bitreader_read(&br, 12);
break;
case D2RARITY_UNIQUE:
item->uniqueID = (uint16_t)d2bitreader_read(&br, 12);
break;
case D2RARITY_RARE:
case D2RARITY_CRAFTED:
case D2RARITY_TEMPERED:
item->nameID1 = (uint8_t)d2bitreader_read(&br, 8);
item->nameID2 = (uint8_t)d2bitreader_read(&br, 8);
item->numRarePrefixes = item->numRareSuffixes = 0;
// Following the name IDs, we got 6 possible magical affixes, the pattern
// is 1 bit id, 11 bit value... But the value will only exist if the prefix
// is 1. So we'll read the id first and check it against 1.
for (int i = 0; i < D2_MAX_RARE_AFFIXES; i++)
{
bool hasAffix = d2bitreader_read(&br, 1);
bool isPrefix = i % 2 == 0; // every other affix is a prefix
if (!hasAffix)
continue;
uint16_t affix = (uint16_t)d2bitreader_read(&br, 11);
if (isPrefix)
{
item->rarePrefixes[item->numRarePrefixes] = affix;
item->numRarePrefixes++;
}
else
{
item->rareSuffixes[item->numRareSuffixes] = affix;
item->numRareSuffixes++;
}
}
break;
}
if (item->isRuneword)
{
// These 16 bits (12 bit uint + 4 extra bits) are used in some way as a lookup for string entries in the game's .tbl files,
// so they are not useful in determining which runeword an item is, as the .tbl files can change
//
// Instead, the game seems to check runewords on load based on the socketed runes (and removes any invalid items)
//
// The following is a brain dump of info found on the way to the above finding:
//
// its unclear exactly what runewordID relates to, as it does not map in any
// obvious way to anything in Runes.txt
//
// for reference, these are some known values:
// 27 = Runeword1 (Ancient's Pledge)
// 30 = Runeword4 (Beast)
// 32 = Runeword6 (Black)
// 98 = Runeword72 (Leaf)
// 100 = Runeword74 (Lionheart)
// 101 = Runeword75 (Lore)
// 106 = Runeword81 (Malice)
// 123 = Runeword98 (Peace)
// ...
// 195 = Runeword170 (Zephyr)
// 2718 = Runeword22 (Delirium) [also the extra 4 bits are 2 for delirium instead of the usual 5]
//
// it seems like it might be related in some way to the language .tbl files, but
// even then it's not exactly clear
//
// some patterns/info:
// - The .txt file is completely out-of-order with the saved values, its unlikely there is any connection
// at all between row number and ID
// - There is a pattern of RunewordX 'Name' column and X+26 saved value for X < 80, and X+25 for X > 80
// + In the default Runes.txt file, there is no Runeword80 key, and that is the exact point where
// the pattern of RunewordX -> X+26 becomes RunewordX -> X+25
// - The values seem to be in alphabetical order (either by their .tbl string value or their .txt name column?)
// except for Delirium which is an outlier
// - Delirium is found in patchstring.tbl, all others are in expansionstring.tbl
// + The string value 'Delerium' is in expansionstring.tbl with key Runeword22, so that's what was patched
// + The extra bits being different could mean 'look in patchstring.tbl'
// + Removing Runeword22 from patchstring.tbl and creating Delirium creates an item with runewordID 48
// (the Runeword22 + 26 pattern matches) and extra bits of 5, so its clear that runewordID and the extra bits
// are string table lookups, and not related in any way to Runes.txt
d2bitreader_skip(&br, 16);
}
if (item->personalized)
{
for (int i = 0; i < D2_MAX_CHAR_NAME_BYTELEN; i++)
{
char c = (char)d2bitreader_read(&br, 7);
item->personalizedName[i] = c;
if (c == '\0') break;
}
item->personalizedName[D2_MAX_CHAR_NAME_STRLEN] = '\0';
}
// If the item is a tome, it will contain 5 extra bits, we're not
// interested in these bits, the value is usually 1, but not sure
// what it is.
if (strcmp(item->code, D2ITEMTYPE_TOME_ID) == 0 || strcmp(item->code, D2ITEMTYPE_TOME_TP) == 0)
{
d2bitreader_read(&br, 5);
}
// All items have this field between the personalization (if it exists)
// and the item specific data
item->timestamp = d2bitreader_read(&br, 1);
if (d2gamedata_is_armor(gameData, item->code))
{
// The -10 here matches ItemStatCost.txt's "Save Add" for the armor stat
// (see `d2itemproplist_parse`)
// TODO: Find out if this is actually linked, or if this -10 is hardcoded
// separate from the ItemStatCost.txt values
item->defenseRating = (uint16_t)(d2bitreader_read(&br, 11) - 10);
}
if (d2gamedata_is_armor(gameData, item->code) || d2gamedata_is_weapon(gameData, item->code))
{
item->maxDurability = (uint8_t)d2bitreader_read(&br, 8);
// Some weapons like phase blades don't have durability, so we'll
// check if the item has max durability, then we can safely assume
// it has current durability too.
if (item->maxDurability > 0)
{
item->currentDurability = (uint8_t)d2bitreader_read(&br, 8);
// Seems to be a random bit here (always seems to be 0).
d2bitreader_read(&br, 1);
}
}
if (d2gamedata_is_stackable(gameData, item->code))
{
// If the item is a stacked item, e.g. a javelin or something, these 9
// bits will contain the quantity.
item->quantity = (uint16_t)d2bitreader_read(&br, 9);
}
// If the item is socketed, it will contain 4 bits of data which are the nr
// of total sockets the item have, regardless of how many are occupied by
// an item.
if (item->socketed)
{
item->numSockets = (uint8_t)d2bitreader_read(&br, 4);
}
// If the item is part of a set, these bit will tell us how many lists
// of magical properties follow the one regular magical property list.
item->setBonusesBits = 0;
if (item->rarity == D2RARITY_SET)
{
item->setBonusesBits = (uint8_t)d2bitreader_read(&br, 5);
}
// magical properties
if ((err = d2itemproplist_parse(&br, &item->magicProperties, gameData)) != D2ERR_OK)
{
d2item_destroy(item);
goto exit;
}
// Item has more magical property lists due to being a set item
if (item->setBonusesBits)
{
for (int i = 0; i < D2_MAX_SET_PROPERTIES; i++)
{
unsigned short mask = 1 << i;
if (item->setBonusesBits & mask)
{
if ((err = d2itemproplist_parse(&br, &item->setBonuses[i], gameData)) != D2ERR_OK)
{
d2item_destroy(item);
goto exit;
}
}
}
}
// runewords have their own list of magical properties
if (item->isRuneword)
{
if ((err = d2itemproplist_parse(&br, &item->runewordProperties, gameData)) != D2ERR_OK)
{
d2item_destroy(item);
goto exit;
}
}
}
err = D2ERR_OK;
exit:
if (br.cursor == BIT_READER_CURSOR_BEYOND_EOF)
{
goto eof;
}
*out_bytesRead = (size_t)(br.bitsRead / BITS_PER_BYTE + (br.bitsRead % BITS_PER_BYTE != 0));
return err;
eof:
*out_bytesRead = dataSizeBytes - startByte;
return D2ERR_PARSE_UNEXPECTED_EOF;
}
CHECK_RESULT d2err d2item_copy(d2item* dest, const d2item* const src)
{
d2err err;
// simple copy
*dest = *src;
// NULL any heap allocated members so we can safely destroy on err
dest->socketedItems.items = NULL;
dest->magicProperties.properties = NULL;
dest->runewordProperties.properties = NULL;
for (int i=0; i < D2_MAX_SET_PROPERTIES; i++)
{
dest->setBonuses[i].properties = NULL;
}
// copy heap allocated members
if ((err = d2itemlist_copy(&dest->socketedItems, &src->socketedItems)) != D2ERR_OK)
{
d2item_destroy(dest);
return err;
}
if ((err = d2itemproplist_copy(&dest->magicProperties, &src->magicProperties)) != D2ERR_OK)
{
d2item_destroy(dest);
return err;
}
if ((err = d2itemproplist_copy(&dest->runewordProperties, &src->runewordProperties)) != D2ERR_OK)
{
d2item_destroy(dest);
return err;
}
for (int i=0; i < D2_MAX_SET_PROPERTIES; i++)
{
unsigned short mask = 1 << i;
if (src->setBonusesBits & mask)
{
if ((err = d2itemproplist_copy(&dest->setBonuses[i], &src->setBonuses[i])) != D2ERR_OK)
{
d2item_destroy(dest);
return err;
}
}
}
return D2ERR_OK;
}
void d2item_destroy(d2item *item)
{
d2itemlist_destroy(&item->socketedItems);
d2itemproplist_destroy(&item->magicProperties);
d2itemproplist_destroy(&item->runewordProperties);
for (int i = 0; i < D2_MAX_SET_PROPERTIES; i++)
{
unsigned short mask = 1 << i;
if (item->setBonusesBits & mask)
{
d2itemproplist_destroy(&item->setBonuses[i]);
}
}
}
void d2stashpage_parse_header(d2itemreader_stream* stream)
{
uint16_t tag = D2ITEMREADER_STREAM_READ(uint16_t) else { goto eof; }
if (tag != PLUGY_STASH_TAG)
{
stream->source.curByte -= sizeof(uint16_t);
stream->err = D2ERR_PARSE_BAD_HEADER_OR_TAG;
return;
}
stream->curPage.flags = 0;
// need to make sure we have at least one byte available to read
if (stream->source.curByte >= stream->source.dataSizeBytes)
{
goto eof;
}
size_t len = strnlen((char*)&stream->source.data[stream->source.curByte], stream->source.dataSizeBytes - stream->source.curByte - 1);
// check that we won't read past the end of data here
if (stream->source.curByte + len + 1 + sizeof(uint16_t) > stream->source.dataSizeBytes)
{
goto eof;
}
if (*(uint16_t*)&stream->source.data[stream->source.curByte + len + 1] != D2_JM_TAG)
{
stream->curPage.flags = D2ITEMREADER_STREAM_READ(uint32_t) else { goto eof; }
}
// need to make sure we have at least one byte available to read
if (stream->source.curByte >= stream->source.dataSizeBytes)
{
goto eof;
}
stream->curPage.name[0] = 0;
char* namePtr = (char*)&stream->source.data[stream->source.curByte];
size_t nameLen = strnlen(namePtr, stream->source.dataSizeBytes - stream->source.curByte - 1);
if (nameLen > D2_MAX_STASH_PAGE_NAME_STRLEN)
{
stream->err = D2ERR_PARSE_STRING_TOO_LONG;
return;
}
if (nameLen)
strncpy(stream->curPage.name, namePtr, nameLen + 1);
stream->source.curByte += nameLen + 1;
stream->curPage.pageNum = stream->state.curPage + 1;
stream->state.parseState = PARSE_STATE_ITEMLIST_READY;
return;
eof:
stream->source.curByte = stream->source.dataSizeBytes;
stream->err = D2ERR_PARSE_UNEXPECTED_EOF;
}
void d2stashpages_parse(d2itemreader_stream* stream, uint32_t expectedNumPages, uint32_t* numPages, d2stashpage** pages, d2itemlist** itemsByPage)
{
// numPages is incremented as pages are successfully parsed to
// ensure that calling d2<x>stash_destroy doesn't attempt to free pages
// that haven't been initialized
*numPages = 0;
*pages = NULL;
*itemsByPage = NULL;
if (expectedNumPages > 0)
{
*pages = malloc(expectedNumPages * sizeof(d2stashpage));
if (*pages == NULL)
{
stream->err = D2ERR_OUT_OF_MEMORY;
return;
}
*itemsByPage = malloc(expectedNumPages * sizeof(d2itemlist));
if (*itemsByPage == NULL)
{
stream->err = D2ERR_OUT_OF_MEMORY;
return;
}
d2item item;
while (stream->err == D2ERR_OK && stream->state.parseState != PARSE_STATE_FINISHED && *numPages < expectedNumPages)
{
// seek to the start of a page's items, even if the page is empty
if (!d2itemreader_seek_parse_state(stream, PARSE_STATE_NONE))
{
break;
}
uint32_t pageIndex = *numPages;
(*pages)[pageIndex] = stream->curPage;
if ((stream->err = d2itemlist_init(&(*itemsByPage)[pageIndex], stream->state.numItems)) != D2ERR_OK)
{
return;
}
(*numPages)++;
// now loop through the items, but stop after this page's itemlist
while (d2itemreader_next_but_stop_on(stream, &item, PARSE_STATE_ITEMLIST_DONE))
{
if ((stream->err = d2itemlist_append(&(*itemsByPage)[pageIndex], &item)) != D2ERR_OK)
{
d2item_destroy(&item);
return;
}