aboutsummaryrefslogtreecommitdiff
path: root/dep/CascLib/src/CascRootFile_Diablo3.cpp
blob: c29f13f74a9c86223d5445810291f2558ebb6cc8 (plain)
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
/*****************************************************************************/
/* CascRootFile_Diablo3.cpp               Copyright (c) Ladislav Zezula 2015 */
/*---------------------------------------------------------------------------*/
/* Support for loading Diablo 3 ROOT file                                    */
/* Note: D3 offsets refer to Diablo III.exe 2.2.0.30013 (32-bit)             */
/* SHA1: e4f17eca8aad8dde70870bf932ac3f5b85f17a1f                            */
/*---------------------------------------------------------------------------*/
/*   Date    Ver   Who  Comment                                              */
/* --------  ----  ---  -------                                              */
/* 04.03.15  1.00  Lad  The first version of CascRootFile_Diablo3.cpp        */
/*****************************************************************************/

#define __CASCLIB_SELF__
#include "CascLib.h"
#include "CascCommon.h"

//-----------------------------------------------------------------------------
// Local structures

#define DIABLO3_SUBDIR_SIGNATURE   0xEAF1FE87
#define DIABLO3_PACKAGES_SIGNATURE 0xAABB0002
#define DIABLO3_MAX_SUBDIRS        0x20
#define DIABLO3_MAX_ASSETS         70               // Maximum possible number of assets
#define DIABLO3_MAX_ROOT_FOLDERS   0x20             // Maximum count of root directory named entries

// On-disk structure for a file given by file number
typedef struct _DIABLO3_ASSET_ENTRY
{
    CONTENT_KEY CKey;                              // Content key for the file
    DWORD FileIndex;                               // File index
} DIABLO3_ASSET_ENTRY, *PDIABLO3_ASSET_ENTRY;

// On-disk structure for a file given by file number and suffix
typedef struct _DIABLO3_ASSETIDX_ENTRY
{
    CONTENT_KEY CKey;                              // Content key for the file
    DWORD FileIndex;                               // File index
    DWORD SubIndex;                                // File subindex, like "SoundBank\3D Ambience\0000.smp"
} DIABLO3_ASSETIDX_ENTRY, *PDIABLO3_ASSETIDX_ENTRY;

// In-memory structure of the named entry
typedef struct _DIABLO3_NAMED_ENTRY
{
    PCONTENT_KEY pCKey;                             // Pointer to the content key
    const char * szFileName;                        // Pointer to the zero-terminated file name
    const char * szFileEnd;                         // Position of the zero terminator (aka end of the file name)
} DIABLO3_NAMED_ENTRY, *PDIABLO3_NAMED_ENTRY;

// On-disk structure of CoreToc.dat header
typedef struct _DIABLO3_CORE_TOC_HEADER
{
    DWORD EntryCounts[DIABLO3_MAX_ASSETS];          // Array of number of entries (files) for each asset
    DWORD EntryOffsets[DIABLO3_MAX_ASSETS];         // Array of offsets of each DIABLO3_CORE_TOC_ENTRY, relative to data after header
    DWORD Unknowns[DIABLO3_MAX_ASSETS];             // Unknown
    DWORD Alignment;
} DIABLO3_CORE_TOC_HEADER, *PDIABLO3_CORE_TOC_HEADER;

// On-disk structure of the entry in CoreToc.dat
typedef struct _DIABLO3_CORE_TOC_ENTRY
{
    DWORD AssetIndex;                               // Index of the Diablo3 asset (aka directory)
    DWORD FileIndex;                                // File index
    DWORD NameOffset;                               // Offset of the plain file name

} DIABLO3_CORE_TOC_ENTRY, *PDIABLO3_CORE_TOC_ENTRY;

// In-memory structure of parsed directory data
typedef struct _DIABLO3_DIRECTORY
{
    LPBYTE pbDirectoryData;                         // The begin of the directory data block
    LPBYTE pbDirectoryEnd;                          // The end of the directory data block
    LPBYTE pbAssetEntries;                          // Pointer to asset entries without subitem number. Example: "SoundBank\SoundFile.smp"
    LPBYTE pbAssetIdxEntries;                       // Pointer to asset entries with subitem number
    LPBYTE pbNamedEntries;                          // Pointer to named entries. These are for files with arbitrary names, and they do not belong to an asset
    DWORD dwAssetEntries;                           // Number of asset entries without subitem number
    DWORD dwAssetIdxEntries;
    DWORD dwNamedEntries;
    DWORD dwNodeIndex;                              // Index of file node for this folder
} DIABLO3_DIRECTORY, *PDIABLO3_DIRECTORY;

// Structure for conversion DirectoryID -> Directory name
typedef struct _DIABLO3_ASSET_INFO
{
    const char * szDirectoryName;                   // Directory name
    const char * szExtension;

} DIABLO3_ASSET_INFO;
typedef const DIABLO3_ASSET_INFO * PDIABLO3_ASSET_INFO;

//-----------------------------------------------------------------------------
// Local variables

static const DIABLO3_ASSET_INFO Assets[] =
{
//   DIR-NAME               EXTENSION
//   ==========             =========
    {NULL,                  NULL},         // 0x00
    {"Actor",               "acr"},        // 0x01
    {"Adventure",           "adv"},        // 0x02
    {NULL,                  NULL},         // 0x03
    {NULL,                  NULL},         // 0x04
    {"AmbientSound",        "ams"},        // 0x05
    {"Anim",                "ani"},        // 0x06
    {"Anim2D",              "an2"},        // 0x07
    {"AnimSet",             "ans"},        // 0x08
    {"Appearance",          "app"},        // 0x09
    {NULL,                  NULL},         // 0x0A
    {"Cloth",               "clt"},        // 0x0B
    {"Conversation",        "cnv"},        // 0x0C
    {NULL,                  NULL},         // 0x0D
    {"EffectGroup",         "efg"},        // 0x0E
    {"Encounter",           "enc"},        // 0x0F
    {NULL,                  NULL},         // 0x10
    {"Explosion",           "xpl"},        // 0x11
    {NULL,                  NULL},         // 0x12
    {"Font",                "fnt"},        // 0x13
    {"GameBalance",         "gam"},        // 0x14
    {"Globals",             "glo"},        // 0x15
    {"LevelArea",           "lvl"},        // 0x16
    {"Light",               "lit"},        // 0x17
    {"MarkerSet",           "mrk"},        // 0x18
    {"Monster",             "mon"},        // 0x19
    {"Observer",            "obs"},        // 0x1A
    {"Particle",            "prt"},        // 0x1B
    {"Physics",             "phy"},        // 0x1C
    {"Power",               "pow"},        // 0x1D
    {NULL,                  NULL},         // 0x1E
    {"Quest",               "qst"},        // 0x1F
    {"Rope",                "rop"},        // 0x20
    {"Scene",               "scn"},        // 0x21
    {"SceneGroup",          "scg"},        // 0x22
    {NULL,                  NULL},         // 0x23
    {"ShaderMap",           "shm"},        // 0x24
    {"Shaders",             "shd"},        // 0x25
    {"Shakes",              "shk"},        // 0x26
    {"SkillKit",            "skl"},        // 0x27
    {"Sound",               "snd"},        // 0x28
    {"SoundBank",           "sbk"},        // 0x29
    {"StringList",          "stl"},        // 0x2A
    {"Surface",             "srf"},        // 0x2B
    {"Textures",            "tex"},        // 0x2C
    {"Trail",               "trl"},        // 0x2D
    {"UI",                  "ui"},         // 0x2E
    {"Weather",             "wth"},        // 0x2F
    {"Worlds",              "wrl"},        // 0x30
    {"Recipe",              "rcp"},        // 0x31
    {NULL,                  NULL},         // 0x32
    {"Condition",           "cnd"},        // 0x33
    {NULL,                  NULL},         // 0x34
    {NULL,                  NULL},         // 0x35
    {NULL,                  NULL},         // 0x36
    {NULL,                  NULL},         // 0x37
    {"Act",                 "act"},        // 0x38
    {"Material",            "mat"},        // 0x39
    {"QuestRange",          "qsr"},        // 0x3A
    {"Lore",                "lor"},        // 0x3B
    {"Reverb",              "rev"},        // 0x3C
    {"PhysMesh",            "phm"},        // 0x3D
    {"Music",               "mus"},        // 0x3E
    {"Tutorial",            "tut"},        // 0x3F
    {"BossEncounter",       "bos"},        // 0x40
    {NULL,                  NULL},         // 0x41
    {"Accolade",            "aco"},        // 0x42
};

#define DIABLO3_ASSET_COUNT (sizeof(Assets) / sizeof(Assets[0]))

//-----------------------------------------------------------------------------
// Handler definitions for Diablo3 root file

struct TDiabloRoot : public TFileTreeRoot
{
    public:

    TDiabloRoot() : TFileTreeRoot(0)
    {
        memset(RootFolders, 0, sizeof(RootFolders));
        pFileIndices = NULL;
        pbCoreTocFile = NULL;
        pbCoreTocData = NULL;
        nFileIndices = 0;
        cbCoreTocFile = 0;

        // Map for searching a real file extension
        memset(&PackagesMap, 0, sizeof(CASC_MAP));
        pbPackagesDat = NULL;
        cbPackagesDat = 0;

        // We have file names and return CKey as result of search
        dwFeatures |= (CASC_FEATURE_FILE_NAMES | CASC_FEATURE_ROOT_CKEY);
    }

    ~TDiabloRoot()
    {
        FreeLoadingStuff();
    }

    PDIABLO3_ASSET_INFO GetAssetInfo(DWORD dwAssetIndex)
    {
        if(dwAssetIndex < DIABLO3_ASSET_COUNT && Assets[dwAssetIndex].szDirectoryName != NULL)
            return &Assets[dwAssetIndex];
        return NULL;
    }

    char * FindPackageName(const char * szAssetName, const char * szPlainName)
    {
        char szFileName[MAX_PATH+1];
        size_t nLength;

        // Construct the name without extension and find it in the map
        nLength = CascStrPrintf(szFileName, _countof(szFileName), "%s\\%s", szAssetName, szPlainName);
        return (char *)PackagesMap.FindString(szFileName, szFileName + nLength);
    }

    LPBYTE LoadFileToMemory(TCascStorage * hs, const char * szFileName, DWORD * pcbFileData)
    {
        PCASC_CKEY_ENTRY pCKeyEntry;
        LPBYTE pbFileData = NULL;

        // Try to find CKey for the file
        pCKeyEntry = GetFile(hs, szFileName);
        if(pCKeyEntry != NULL)
            pbFileData = LoadInternalFileToMemory(hs, pCKeyEntry, pcbFileData);

        return pbFileData;
    }

    static LPBYTE CaptureDirectoryData(
        DIABLO3_DIRECTORY & DirHeader,
        LPBYTE pbDirectory,
        DWORD cbDirectory)
    {
        LPBYTE pbDirectoryData = pbDirectory;
        LPBYTE pbDataEnd = pbDirectory + cbDirectory;
        DWORD Signature = 0;

        //
        // Structure of a Diablo3 directory header
        // 1) Signature (4 bytes)
        // 2) Number of DIABLO3_ASSET_ENTRY entries (4 bytes)
        // 3) Array of DIABLO3_ASSET_ENTRY entries
        // 4) Number of DIABLO3_ASSETIDX_ENTRY entries (4 bytes)
        // 5) Array of DIABLO3_ASSETIDX_ENTRY entries
        // 6) Number of DIABLO3_NAMED_ENTRY entries (4 bytes)
        // 7) Array of DIABLO3_NAMED_ENTRY entries
        //

        // Prepare the header signature
        memset(&DirHeader, 0, sizeof(DIABLO3_DIRECTORY));

        // Get the header signature
        pbDirectory = CaptureInteger32(pbDirectory, pbDataEnd, &Signature);
        if((pbDirectory == NULL) || (Signature != CASC_DIABLO3_ROOT_SIGNATURE && Signature != DIABLO3_SUBDIR_SIGNATURE))
            return NULL;

        // Subdirectories have extra two arrays
        if(Signature == DIABLO3_SUBDIR_SIGNATURE)
        {
            // Capture the number of DIABLO3_ASSET_ENTRY items
            pbDirectory = CaptureInteger32(pbDirectory, pbDataEnd, &DirHeader.dwAssetEntries);
            if(pbDirectory == NULL)
                return NULL;

            // Capture the array of DIABLO3_ASSET_ENTRY
            pbDirectory = CaptureArray(pbDirectory, pbDataEnd, &DirHeader.pbAssetEntries, DIABLO3_ASSET_ENTRY, DirHeader.dwAssetEntries);
            if(pbDirectory == NULL)
                return NULL;

            // Capture the number of DIABLO3_ASSETIDX_ENTRY items
            pbDirectory = CaptureInteger32(pbDirectory, pbDataEnd, &DirHeader.dwAssetIdxEntries);
            if(pbDirectory == NULL)
                return NULL;

            // Capture the array of DIABLO3_ASSETIDX_ENTRY
            pbDirectory = CaptureArray(pbDirectory, pbDataEnd, &DirHeader.pbAssetIdxEntries, DIABLO3_ASSETIDX_ENTRY, DirHeader.dwAssetIdxEntries);
            if(pbDirectory == NULL)
                return NULL;
        }

        // Capture the number of DIABLO3_NAMED_ENTRY array
        pbDirectory = CaptureInteger32(pbDirectory, pbDataEnd, &DirHeader.dwNamedEntries);
        if(pbDirectory == NULL)
            return NULL;

        // Note: Do not capture the array here. We will do that later,
        // when we will be parsing the directory
        DirHeader.pbNamedEntries = pbDirectory;

        // Put the directory range
        DirHeader.pbDirectoryData = pbDirectoryData;
        DirHeader.pbDirectoryEnd = pbDirectoryData + cbDirectory;
        return pbDirectory;
    }

    LPBYTE CaptureCoreTocHeader(
        PDIABLO3_CORE_TOC_HEADER * PtrHeader,
        PDWORD PtrMaxIndex,
        LPBYTE pbDataPtr,
        LPBYTE pbDataEnd)
    {
        PDIABLO3_CORE_TOC_HEADER pTocHeader = (PDIABLO3_CORE_TOC_HEADER)pbDataPtr;
        DWORD dwMaxFileIndex = 0;

        // Check the space for header
        if((pbDataPtr + sizeof(DIABLO3_CORE_TOC_HEADER)) > pbDataEnd)
            return NULL;
        pbDataPtr += sizeof(DIABLO3_CORE_TOC_HEADER);

        // Verify all asset arrays
        for(size_t i = 0; i < DIABLO3_MAX_ASSETS; i++)
        {
            PDIABLO3_CORE_TOC_ENTRY pTocEntry = (PDIABLO3_CORE_TOC_ENTRY)(pbDataPtr + pTocHeader->EntryOffsets[i]);
            DWORD EntryOffset = pTocHeader->EntryOffsets[i];
            DWORD EntryCount = pTocHeader->EntryCounts[i];

            // Verify file range
            if((pbDataPtr + EntryOffset + EntryCount * sizeof(DIABLO3_CORE_TOC_ENTRY)) > pbDataEnd)
                return NULL;

            // Find out the entry with the maximum index
            for(DWORD n = 0; n < EntryCount; n++)
            {
                if(pTocEntry->FileIndex >= dwMaxFileIndex)
                    dwMaxFileIndex = pTocEntry->FileIndex;
                pTocEntry++;
            }
        }

        // Give data and return
        PtrMaxIndex[0] = dwMaxFileIndex;
        PtrHeader[0] = pTocHeader;
        return pbDataPtr;
    }

    LPBYTE CaptureNamedEntry(
        LPBYTE pbDataPtr,
        LPBYTE pbDataEnd,
        PDIABLO3_NAMED_ENTRY pEntry)
    {
        // Capture the content key
        pbDataPtr = CaptureContentKey(pbDataPtr, pbDataEnd, &pEntry->pCKey);
        if(pbDataPtr == NULL)
            return NULL;

        // Capture file name. Must be ASCIIZ file name
        pEntry->szFileName = (const char *)pbDataPtr;
        while(pbDataPtr < pbDataEnd && pbDataPtr[0] != 0)
            pbDataPtr++;

        // Did we find a zero char?
        if(pbDataPtr < pbDataEnd && pbDataPtr[0] == 0)
        {
            pEntry->szFileEnd = (const char *)pbDataPtr;
            return pbDataPtr + 1;
        }

        return NULL;
    }

    int LoadDirectoryFile(TCascStorage * hs, DIABLO3_DIRECTORY & DirHeader, PCASC_CKEY_ENTRY pCKeyEntry)
    {
        LPBYTE pbData;
        DWORD cbData = 0;

        // Load the n-th folder
        pbData = LoadInternalFileToMemory(hs, pCKeyEntry, &cbData);
        if(pbData && cbData)
        {
            if(CaptureDirectoryData(DirHeader, pbData, cbData) == NULL)
            {
                // Clear the directory
                CASC_FREE(pbData);
                return ERROR_BAD_FORMAT;
            }
        }
        return ERROR_SUCCESS;
    }

    bool CreateAssetFileName(
        CASC_PATH<char> & PathBuffer,
        DWORD FileIndex,
        DWORD SubIndex)
    {
        PDIABLO3_CORE_TOC_ENTRY pTocEntry;
        PDIABLO3_ASSET_INFO pAssetInfo;
        LPCSTR szPackageName = NULL;
        LPCSTR szPlainName;
        LPCSTR szFormat;
        char szBuffer[MAX_PATH];

        // Find and check the entry
        pTocEntry = pFileIndices + FileIndex;
        if(pTocEntry->FileIndex == FileIndex)
        {
            // Retrieve the asset information
            szPlainName = (LPCSTR)(pbCoreTocData + pTocEntry->NameOffset);
            pAssetInfo = GetAssetInfo(pTocEntry->AssetIndex);

            // Construct the file name, up to the extension. Don't include the '.'
            szFormat = (SubIndex != CASC_INVALID_INDEX) ? "%s\\%04u" : "%s";
            CascStrPrintf(szBuffer, _countof(szBuffer), szFormat, szPlainName, SubIndex);

            // Try to fixup the file extension from the package name.
            // File extensions are not predictable because for subitems,
            // they are not always equal to the main items:
            //
            //  SoundBank\3D Ambience.sbk
            //  SoundBank\3D Ambience\0000.smp
            //  SoundBank\3D Ambience\0002.smp
            //  ...
            //  SoundBank\Angel.sbk
            //  SoundBank\Angel\0000.fsb
            //  SoundBank\Angel\0002.fsb
            //
            // We use the Base\Data_D3\PC\Misc\Packages.dat for real file extensions, where possible
            //

            if(pAssetInfo != NULL)
            {
                // Retrieve the asset name
                szPackageName = FindPackageName(pAssetInfo->szDirectoryName, szBuffer);
                if(szPackageName != NULL)
                {
                    PathBuffer.AppendString(szPackageName, false);
                    return true;
                }

                // Append the directory name
                PathBuffer.AppendString(pAssetInfo->szDirectoryName, false);
            }
            else
            {
                // Append generic name "Asset##" and continue
                PathBuffer.AppendString("Asset", false);
                PathBuffer.AppendChar((char)('0' + (pTocEntry->AssetIndex / 10)));
                PathBuffer.AppendChar((char)('0' + (pTocEntry->AssetIndex % 10)));
            }

            // Append the content of the buffer
            PathBuffer.AppendString(szBuffer, true);

            // If we have an extension, use it. Otherwise, supply "a##"
            if(pAssetInfo != NULL && pAssetInfo->szExtension != NULL)
            {
                PathBuffer.AppendChar('.');
                PathBuffer.AppendString(pAssetInfo->szExtension, false);
            }
            else
            {
                CascStrPrintf(szBuffer, _countof(szBuffer), ".a%02u", pTocEntry->AssetIndex);
                PathBuffer.AppendString(szBuffer, false);
            }
            return true;
        }

        return false;
    }

    // Parse the asset entries
    DWORD ParseAssetEntries(
        TCascStorage * hs,
        DIABLO3_DIRECTORY & Directory,
        CASC_PATH<char> & PathBuffer)
    {
        PDIABLO3_ASSET_ENTRY pEntry = (PDIABLO3_ASSET_ENTRY)Directory.pbAssetEntries;
        PCASC_CKEY_ENTRY pCKeyEntry;
        size_t nSavePos = PathBuffer.Save();
        DWORD dwEntries = Directory.dwAssetEntries;

        // Do nothing if there is no entries
        if(pEntry != NULL && dwEntries != 0)
        {
            // Insert all asset entries to the file tree
            for(DWORD i = 0; i < dwEntries; i++, pEntry++)
            {
                pCKeyEntry = FindCKeyEntry_CKey(hs, pEntry->CKey.Value);
                if(pCKeyEntry != NULL)
                {
                    // Construct the full path name of the entry
                    if(CreateAssetFileName(PathBuffer, pEntry->FileIndex, CASC_INVALID_INDEX))
                    {
                        // Insert the entry to the file tree
                        FileTree.InsertByName(pCKeyEntry, PathBuffer);
                    }

                    // Restore the path buffer position
                    PathBuffer.Restore(nSavePos);
                }
            }
        }

        return ERROR_SUCCESS;
    }

    DWORD ParseAssetAndIdxEntries(
        TCascStorage * hs,
        DIABLO3_DIRECTORY & Directory,
        CASC_PATH<char> & PathBuffer)
    {
        PDIABLO3_ASSETIDX_ENTRY pEntry = (PDIABLO3_ASSETIDX_ENTRY)Directory.pbAssetIdxEntries;
        PCASC_CKEY_ENTRY pCKeyEntry;
        size_t nSavePos = PathBuffer.Save();
        DWORD dwEntries = Directory.dwAssetIdxEntries;

        // Do nothing if there is no entries
        if(pEntry != NULL && dwEntries != 0)
        {
            // Insert all asset entries to the file tree
            for(DWORD i = 0; i < dwEntries; i++, pEntry++)
            {
                pCKeyEntry = FindCKeyEntry_CKey(hs, pEntry->CKey.Value);
                if(pCKeyEntry != NULL)
                {
                    // Construct the full path name of the entry
                    if(CreateAssetFileName(PathBuffer, pEntry->FileIndex, pEntry->SubIndex))
                    {
                        // Insert the entry to the file tree
//                      fprintf(fp, "%08u %04u %s\n", pEntry->FileIndex, pEntry->SubIndex, PathBuffer.szBegin);
                        FileTree.InsertByName(pCKeyEntry, PathBuffer);
                    }

                    // Restore the path buffer position
                    PathBuffer.Restore(nSavePos);
                }
            }
        }

        return ERROR_SUCCESS;
    }

    // Parse the named entries of all folders
    DWORD ParseDirectory_Phase1(
        TCascStorage * hs, 
        DIABLO3_DIRECTORY & Directory,
        CASC_PATH<char> & PathBuffer,
        bool bIsRootDirectory)
    {
        DIABLO3_NAMED_ENTRY NamedEntry;
        size_t nFolderIndex = 0;
        size_t nSavePos = PathBuffer.Save();
        DWORD dwErrCode = ERROR_SUCCESS;

        // Do nothing if there is no named headers
        if(Directory.pbNamedEntries && Directory.dwNamedEntries)
        {
            PCASC_CKEY_ENTRY pCKeyEntry;
            PCASC_FILE_NODE pFileNode;
            LPBYTE pbDataPtr = Directory.pbNamedEntries;
            LPBYTE pbDataEnd = Directory.pbDirectoryEnd;
            DWORD dwNodeIndex;

            // Parse all entries
            while(pbDataPtr < pbDataEnd)
            {
                // Capture the named entry
                pbDataPtr = CaptureNamedEntry(pbDataPtr, pbDataEnd, &NamedEntry);
                if(pbDataPtr == NULL)
                    return ERROR_BAD_FORMAT;

                // Append the path fragment to the total path
                PathBuffer.AppendStringN(NamedEntry.szFileName, (NamedEntry.szFileEnd - NamedEntry.szFileName), true);

                // Check whether the file exists in the storage
                pCKeyEntry = FindCKeyEntry_CKey(hs, NamedEntry.pCKey->Value);
                if(pCKeyEntry != NULL)
                {
                    // Create file node belonging to this folder
                    pFileNode = FileTree.InsertByName(pCKeyEntry, PathBuffer);
                    dwNodeIndex = (DWORD)FileTree.IndexOf(pFileNode);

                    // If we are parsing root folder, we also need to load the data of the sub-folder file
                    if(bIsRootDirectory)
                    {
                        // Mark the node as directory
                        pCKeyEntry->Flags |= CASC_CE_FOLDER_ENTRY;
                        pFileNode->Flags |= CFN_FLAG_FOLDER;

                        // Load the sub-directory file
                        dwErrCode = LoadDirectoryFile(hs, RootFolders[nFolderIndex], pCKeyEntry);
                        if(dwErrCode != ERROR_SUCCESS)
                            return dwErrCode;

                        // Parse the sub-directory file
                        dwErrCode = ParseDirectory_Phase1(hs, RootFolders[nFolderIndex], PathBuffer, false);
                        if(dwErrCode != ERROR_SUCCESS)
                            return dwErrCode;

                        // Also save the item pointer and increment the folder index
                        RootFolders[nFolderIndex].dwNodeIndex = dwNodeIndex;
                        nFolderIndex++;
                    }

                    // Restore the path pointer
                    PathBuffer.Restore(nSavePos);
                }
            }
        }

        return dwErrCode;
    }

    // Parse the nameless entries of all folders
    int ParseDirectory_Phase2(TCascStorage * hs)
    {
        CASC_PATH<char> PathBuffer;
        char szBuffer[MAX_PATH];

        // Parse each root subdirectory
        for(size_t i = 0; i < DIABLO3_MAX_ROOT_FOLDERS; i++)
        {
            // Is this root folder loaded?
            if(RootFolders[i].pbDirectoryData != NULL)
            {
                // Retrieve the parent name
                if(RootFolders[i].dwNodeIndex != 0)
                {
                    FileTree.PathAt(szBuffer, _countof(szBuffer), RootFolders[i].dwNodeIndex);
                    PathBuffer.SetPathRoot(szBuffer);
                }

                // Array of DIABLO3_ASSET_ENTRY entries.
                // These are for files belonging to an asset, without subitem number.
                // Example: "SoundBank\SoundFile.smp"
                ParseAssetEntries(hs, RootFolders[i], PathBuffer);

                // Array of DIABLO3_ASSETIDX_ENTRY entries.
                // These are for files belonging to an asset, with a subitem number.
                // Example: "SoundBank\SoundFile\0001.smp"
                ParseAssetAndIdxEntries(hs, RootFolders[i], PathBuffer);
            }
        }

        return ERROR_SUCCESS;
    }

    // Creates an array of DIABLO3_CORE_TOC_ENTRY entries indexed by FileIndex
    // Used as lookup table when we have FileIndex and need Asset+PlainName
    DWORD CreateMapOfFileIndices(TCascStorage * hs, const char * szFileName)
    {
        PDIABLO3_CORE_TOC_HEADER pTocHeader = NULL;
        LPBYTE pbCoreTocPtr = pbCoreTocFile;
        DWORD dwMaxFileIndex = 0;
        DWORD dwErrCode = ERROR_CAN_NOT_COMPLETE;

        // Load the entire file to memory
        pbCoreTocFile = pbCoreTocPtr = LoadFileToMemory(hs, szFileName, &cbCoreTocFile);
        if(pbCoreTocFile && cbCoreTocFile)
        {
            LPBYTE pbCoreTocEnd = pbCoreTocFile + cbCoreTocFile;

            // Capture the header
            if((pbCoreTocPtr = CaptureCoreTocHeader(&pTocHeader, &dwMaxFileIndex, pbCoreTocPtr, pbCoreTocEnd)) == NULL)
                return ERROR_BAD_FORMAT;

            // If there are no indices, return NULL
            if(dwMaxFileIndex == 0)
                return ERROR_SUCCESS;

            // Allocate and populate the array of DIABLO3_CORE_TOC_ENTRYs
            pFileIndices = CASC_ALLOC<DIABLO3_CORE_TOC_ENTRY>(dwMaxFileIndex + 1);
            if(pFileIndices != NULL)
            {
                // Initialize all entries to invalid
                memset(pFileIndices, 0xFF, (dwMaxFileIndex + 1) * sizeof(DIABLO3_CORE_TOC_ENTRY));

                // Populate the linear array with the file indices
                for(size_t i = 0; i < DIABLO3_MAX_ASSETS; i++)
                {
                    PDIABLO3_CORE_TOC_ENTRY pTocEntry = (PDIABLO3_CORE_TOC_ENTRY)(pbCoreTocPtr + pTocHeader->EntryOffsets[i]);
                    LPBYTE pbCoreTocNames = (LPBYTE)(pTocEntry + pTocHeader->EntryCounts[i]);

                    // Setup the entries
                    for(DWORD n = 0; n < pTocHeader->EntryCounts[i]; n++)
                    {
                        DWORD dwFileIndex = pTocEntry->FileIndex;

                        pFileIndices[dwFileIndex].AssetIndex = pTocEntry->AssetIndex;
                        pFileIndices[dwFileIndex].FileIndex  = pTocEntry->FileIndex;
                        pFileIndices[dwFileIndex].NameOffset = (DWORD)(pbCoreTocNames - pbCoreTocPtr) + pTocEntry->NameOffset;
                        pTocEntry++;
                    }
                }

                // Save the file to the root handler
                pbCoreTocData = pbCoreTocPtr;
                nFileIndices = dwMaxFileIndex;
                dwErrCode = ERROR_SUCCESS;
            }
        }
        return dwErrCode;
    }

    // Packages.dat contains a list of full file names (without locale prefix).
    // They are not sorted, nor they correspond to file IDs.
    // Does the sort order mean something? Perhaps we could use them as listfile?
    int CreateMapOfRealNames(TCascStorage * hs, const char * szFileName)
    {
        DWORD Signature = 0;
        DWORD NumberOfNames = 0;

        // Load the entire file to memory
        pbPackagesDat = LoadFileToMemory(hs, szFileName, &cbPackagesDat);
        if(pbPackagesDat && cbPackagesDat)
        {
            LPBYTE pbPackagesPtr = pbPackagesDat;
            LPBYTE pbPackagesEnd = pbPackagesDat + cbPackagesDat;

            // Get the header. There is just Signature + NumberOfNames
            if((pbPackagesPtr = CaptureInteger32(pbPackagesPtr, pbPackagesEnd, &Signature)) == NULL)
                return ERROR_BAD_FORMAT;
            if((pbPackagesPtr = CaptureInteger32(pbPackagesPtr, pbPackagesEnd, &NumberOfNames)) == NULL)
                return ERROR_BAD_FORMAT;
            if(Signature != DIABLO3_PACKAGES_SIGNATURE || NumberOfNames == 0)
                return ERROR_BAD_FORMAT;

            // Create the map for fast search of the file name
            if(PackagesMap.Create(NumberOfNames, 0, 0, KeyIsString) == ERROR_SUCCESS)
            {
                const char * szPackageName = (const char *)pbPackagesPtr;

                // Go as long as there is something
                for(DWORD i = 0; i < NumberOfNames; i++)
                {
                    // Get the file extension
                    if((LPBYTE)szPackageName >= pbPackagesEnd)
                        break;

                    // Insert the file name to the map. The file extension is not included
                    PackagesMap.InsertString(szPackageName, true);
                    szPackageName = szPackageName + strlen(szPackageName) + 1;
                }
            }
        }

        return ERROR_SUCCESS;
    }

    DWORD Load(TCascStorage * hs, DIABLO3_DIRECTORY & RootDirectory)
    {
        CASC_PATH<char> PathBuffer;
        DWORD dwErrCode;

        // Always parse the named entries first. They always point to a file.
        // These are entries with arbitrary names, and they do not belong to an asset
        dwErrCode = ParseDirectory_Phase1(hs, RootDirectory, PathBuffer, true);
        if(dwErrCode == ERROR_SUCCESS)
        {
            // The asset entries in the ROOT file don't contain file names, but indices.
            // To convert a file index to a file name, we need to load and parse the "Base\\CoreTOC.dat" file.
            dwErrCode = CreateMapOfFileIndices(hs, "Base\\CoreTOC.dat");
            if(dwErrCode == ERROR_SUCCESS)
            {
                // The file "Base\Data_D3\PC\Misc\Packages.dat" contains the file names
                // (without level-0 and level-1 directory).
                // We can use these names for supplying the missing extensions
                CreateMapOfRealNames(hs, "Base\\Data_D3\\PC\\Misc\\Packages.dat");

                // Now parse all folders and resolve the full names
                ParseDirectory_Phase2(hs);
            }

            // Free all stuff that was used during loading of the ROOT file
            FreeLoadingStuff();
        }

        return dwErrCode;
    }

    void FreeLoadingStuff()
    {
        // Free the captured root sub-directories
        for(size_t i = 0; i < DIABLO3_MAX_SUBDIRS; i++)
            CASC_FREE(RootFolders[i].pbDirectoryData);

        // Free the package map
        PackagesMap.Free();

        // Free the array of file indices
        CASC_FREE(pFileIndices);

        // Free the loaded CoreTOC.dat file
        CASC_FREE(pbCoreTocFile);

        // Free the loaded Packages.dat file
        CASC_FREE(pbPackagesDat);
    }

    // Array of root directory subdirectories
    DIABLO3_DIRECTORY RootFolders[DIABLO3_MAX_ROOT_FOLDERS];

    // Array of DIABLO3_TOC_ENTRY structures, sorted by the file index
    // Used for converting FileIndex -> Asset+PlainName during loading
    PDIABLO3_CORE_TOC_ENTRY pFileIndices;
    LPBYTE pbCoreTocFile;
    LPBYTE pbCoreTocData;
    size_t nFileIndices;
    DWORD cbCoreTocFile;

    // Map for searching a real file extension
    CASC_MAP PackagesMap;
    LPBYTE pbPackagesDat;
    DWORD cbPackagesDat;
};

//-----------------------------------------------------------------------------
// Public functions

DWORD RootHandler_CreateDiablo3(TCascStorage * hs, LPBYTE pbRootFile, DWORD cbRootFile)
{
    TDiabloRoot * pRootHandler = NULL;
    DIABLO3_DIRECTORY RootDirectory;
    DWORD dwErrCode = ERROR_BAD_FORMAT;

    // Verify the header of the ROOT file
    if(TDiabloRoot::CaptureDirectoryData(RootDirectory, pbRootFile, cbRootFile) != NULL)
    {
        // Allocate the root handler object
        pRootHandler = new TDiabloRoot();
        if(pRootHandler != NULL)
        {
            // Load the root directory. If load failed, we free the object
            dwErrCode = pRootHandler->Load(hs, RootDirectory);
            if(dwErrCode != ERROR_SUCCESS)
            {
                delete pRootHandler;
                pRootHandler = NULL;
            }
        }
    }

    // Assign the root directory (or NULL) and return error
    hs->pRootHandler = pRootHandler;
    return dwErrCode;
}