diff --git a/asm/include/main_0200C4CC.inc b/asm/include/main_0200C4CC.inc index a1170aba..cf7f1fc6 100644 --- a/asm/include/main_0200C4CC.inc +++ b/asm/include/main_0200C4CC.inc @@ -750,7 +750,7 @@ .public ADVENTURE_LOG_ENCOUNTERS_MONSTER_IDS .public ADVENTURE_LOG_PTR .public ALERT_BOX_DEFAULT_WINDOW_PARAMS -.public AllocAndLoadFileInPack +.public DirectoryFileMngr_OpenDirectoryFile .public AREA_NAME_BOX_DEFAULT_WINDOW_PARAMS .public ARM9_UNKNOWN_DATA__NA_209E6BC .public ARM9_UNKNOWN_PTR__NA_20A2C84 @@ -816,7 +816,7 @@ .public Debug_GetLogFlag .public GetDungeonModeSpecial .public GetEvolutionPossibilities -.public GetFileLengthInPackWithPackNb +.public DirectoryFileMngr_GetDirectoryFileSize .public GetHeldButtons .public GetHero .public GetHeroMemberIdx @@ -859,7 +859,7 @@ .public LANGUAGE_INFO_DATA .public LIFE_SEED_HP_BOOST .public LoadFileFromRom -.public LoadFileInPackWithPackId +.public DirectoryFileMngr_LoadDirectoryFile .public LoadOverlay .public MemAlloc .public MemArenaAlloc diff --git a/asm/include/main_02054C24.inc b/asm/include/main_02054C24.inc index 1192c74e..db080d15 100644 --- a/asm/include/main_02054C24.inc +++ b/asm/include/main_02054C24.inc @@ -189,7 +189,7 @@ .public _s32_div_f .public _u32_div_f .public AddMoneyCarried -.public AllocAndLoadFileInPack +.public DirectoryFileMngr_OpenDirectoryFile .public ARM9_UNKNOWN_FUNCTION_TABLE__NA_20A3CF4 .public ARM9_UNKNOWN_TABLE__NA_20A3CC8 .public ARM9_UNKNOWN_TABLE__NA_20A3CE4 @@ -347,7 +347,7 @@ .public NO_SEND_ITEM_TABLE .public NoteLoadBase .public NoteSaveBase -.public OpenAllPackFiles +.public DirectoryFileMngr_ExtractAllDirectoryFiles .public ov00_022BE0C8 .public ov01_02337B3C .public ov01_02337B68 diff --git a/asm/include/overlay_10.inc b/asm/include/overlay_10.inc index a19becc2..1a528528 100644 --- a/asm/include/overlay_10.inc +++ b/asm/include/overlay_10.inc @@ -3,7 +3,7 @@ .public _020B0A5C .public _s32_div_f .public abs -.public AllocAndLoadFileInPack +.public DirectoryFileMngr_OpenDirectoryFile .public BAG_ITEMS_PTR_MIRROR .public CalcMenuHeightDiv8__020329E4 .public CleanStickyItemsInBag @@ -168,7 +168,7 @@ .public GetDungeonGroup .public GetDungeonMode .public GetDungeonModeSpecial -.public GetFileLengthInPackWithPackNb +.public DirectoryFileMngr_GetDirectoryFileSize .public GetGameMode .public GetItemAtIdx .public GetMoneyCarried diff --git a/asm/include/overlay_29_022DC240.inc b/asm/include/overlay_29_022DC240.inc index cabd42be..fc0b3709 100644 --- a/asm/include/overlay_29_022DC240.inc +++ b/asm/include/overlay_29_022DC240.inc @@ -2,7 +2,7 @@ .public _020AFC4C .public _020AFC70 .public _s32_div_f -.public AllocAndLoadFileInPack +.public DirectoryFileMngr_OpenDirectoryFile .public AllocTopScreenStatus .public ChangeGiratinaFormIfSkyDungeon .public ClearHiddenStairs diff --git a/asm/include/overlay_29_022E0378.inc b/asm/include/overlay_29_022E0378.inc index d2a7afd9..aeaba1c4 100644 --- a/asm/include/overlay_29_022E0378.inc +++ b/asm/include/overlay_29_022E0378.inc @@ -2,7 +2,7 @@ .public _020AFC28 .public _020AFC4C .public _020AFC70 -.public AllocAndLoadFileInPack +.public DirectoryFileMngr_OpenDirectoryFile .public AnimationDelayOrSomething .public CalcSpeedStageWrapper .public DisplayMessage2 diff --git a/asm/include/overlay_29_022F7388.inc b/asm/include/overlay_29_022F7388.inc index 6fa1a725..15f9e3e9 100644 --- a/asm/include/overlay_29_022F7388.inc +++ b/asm/include/overlay_29_022F7388.inc @@ -50,7 +50,7 @@ .public GetDirectionTowardsPosition .public GetExp .public GetExplorerMazeMonster -.public GetFileLengthInPackWithPackNb +.public DirectoryFileMngr_GetDirectoryFileSize .public GetFloorType .public GetForcedLossReason .public GetLeader diff --git a/asm/include/overlay_29_0231CBEC.inc b/asm/include/overlay_29_0231CBEC.inc index b3f71172..b9bbc38a 100644 --- a/asm/include/overlay_29_0231CBEC.inc +++ b/asm/include/overlay_29_0231CBEC.inc @@ -1,7 +1,7 @@ #pragma once .public _020A1894 .public AbilityIsActiveVeneer -.public AllocAndLoadFileInPack +.public DirectoryFileMngr_OpenDirectoryFile .public ATK_STAT_IDX .public BoostIQ .public BoostOffensiveStat diff --git a/asm/include/overlay_29_02337CCC.inc b/asm/include/overlay_29_02337CCC.inc index fb0958c0..fd85c981 100644 --- a/asm/include/overlay_29_02337CCC.inc +++ b/asm/include/overlay_29_02337CCC.inc @@ -3,7 +3,7 @@ .public _020AFC70 .public _s32_div_f .public abs -.public AllocAndLoadFileInPack +.public DirectoryFileMngr_OpenDirectoryFile .public CORNER_CARDINAL_NEIGHBOR_IS_OPEN .public CountStickyItemsInBag .public DetermineAllTilesWalkableNeighbors diff --git a/asm/main_0200C4CC.s b/asm/main_0200C4CC.s index e333cc82..6c8596f4 100644 --- a/asm/main_0200C4CC.s +++ b/asm/main_0200C4CC.s @@ -21762,11 +21762,11 @@ _0201D4C0: ldr sb, [r0, #0x500] mov r0, r8 mov r1, r7 - bl GetFileLengthInPackWithPackNb + bl DirectoryFileMngr_GetDirectoryFileSize mov r0, r8 mov r1, r7 mov r2, sb - bl LoadFileInPackWithPackId + bl DirectoryFileMngr_LoadDirectoryFile mov r0, sb bl GetAtSize mov r7, r0 @@ -21780,7 +21780,7 @@ _0201D4C0: _0201D53C: mov r0, r8 mov r1, r7 - bl GetFileLengthInPackWithPackNb + bl DirectoryFileMngr_GetDirectoryFileSize mov r1, r6 mov sb, r0 bl MemAlloc @@ -21788,7 +21788,7 @@ _0201D53C: mov r0, r8 mov r1, r7 mov r2, r6 - bl LoadFileInPackWithPackId + bl DirectoryFileMngr_LoadDirectoryFile str sb, [r4, #0x28] _0201D56C: str r6, [r4, #0x34] @@ -21832,11 +21832,11 @@ _0201D5CC: ldr sb, [r0, #0x500] mov r0, r8 mov r1, r7 - bl GetFileLengthInPackWithPackNb + bl DirectoryFileMngr_GetDirectoryFileSize mov r0, r8 mov r1, r7 mov r2, sb - bl LoadFileInPackWithPackId + bl DirectoryFileMngr_LoadDirectoryFile mov r0, sb bl GetAtSize mov r2, sb @@ -21847,11 +21847,11 @@ _0201D5CC: _0201D62C: mov r0, r8 mov r1, r7 - bl GetFileLengthInPackWithPackNb + bl DirectoryFileMngr_GetDirectoryFileSize mov r0, r8 mov r1, r7 mov r2, r6 - bl LoadFileInPackWithPackId + bl DirectoryFileMngr_LoadDirectoryFile _0201D648: mov r0, #2 strb r0, [r4, #0x21] @@ -21885,11 +21885,11 @@ ReplaceWanFromBinFile: ; 0x0201D684 mov r0, r5 mov r1, r4 ldr r8, [r6, #0x34] - bl GetFileLengthInPackWithPackNb + bl DirectoryFileMngr_GetDirectoryFileSize mov r0, r5 mov r1, r4 mov r2, r7 - bl LoadFileInPackWithPackId + bl DirectoryFileMngr_LoadDirectoryFile mov r0, r7 bl GetAtSize mov r2, r7 @@ -21901,11 +21901,11 @@ _0201D6E8: ldr r7, [r6, #0x34] mov r0, r5 mov r1, r4 - bl GetFileLengthInPackWithPackNb + bl DirectoryFileMngr_GetDirectoryFileSize mov r0, r5 mov r1, r4 mov r2, r7 - bl LoadFileInPackWithPackId + bl DirectoryFileMngr_LoadDirectoryFile _0201D708: ldr r1, [r6, #0x34] add r0, r6, #0x30 @@ -22558,7 +22558,7 @@ LoadWteFromFileDirectory: ; 0x0201DEC4 mov r0, r1 mov r1, r2 add r2, sp, #0 - bl AllocAndLoadFileInPack + bl DirectoryFileMngr_OpenDirectoryFile ldr r1, [sp] add r0, sp, #8 bl HandleSir0Translation @@ -22639,7 +22639,7 @@ LoadWtuFromBin: ; 0x0201DFB4 mov r0, r7 mov r1, r6 mov r3, r5 - bl AllocAndLoadFileInPack + bl DirectoryFileMngr_OpenDirectoryFile ldr r5, [sp] ldr r1, _0201E01C ; =_02099720 str r5, [r4] @@ -88843,7 +88843,7 @@ GetLvlUpEntry: ; 0x0205379C add r2, sp, #4 mov r0, #5 mov r3, #0 - bl AllocAndLoadFileInPack + bl DirectoryFileMngr_OpenDirectoryFile ldr r1, [sp, #4] add r0, sp, #0 bl HandleSir0Translation diff --git a/asm/main_02054C24.s b/asm/main_02054C24.s index 17599099..938547fb 100644 --- a/asm/main_02054C24.s +++ b/asm/main_02054C24.s @@ -4731,7 +4731,7 @@ sub_0205882C: ; 0x0205882C add r2, sp, #0x14 mov r0, #4 str r3, [sp, #0x1c] - bl AllocAndLoadFileInPack + bl DirectoryFileMngr_OpenDirectoryFile ldr r1, [sp, #0x14] add r0, sp, #0x1c bl HandleSir0Translation @@ -4861,7 +4861,7 @@ _02058928: add r2, sp, #0xc mov r0, #4 mov r3, #0 - bl AllocAndLoadFileInPack + bl DirectoryFileMngr_OpenDirectoryFile ldr r1, [sp, #0xc] add r0, sp, #8 bl HandleSir0Translation @@ -19568,7 +19568,7 @@ _02064B94: mov r3, r6 str r7, [r5, #0x48] add r2, r5, #0x50 - bl AllocAndLoadFileInPack + bl DirectoryFileMngr_OpenDirectoryFile ldrsh r2, [r5, #0x4c] add r1, sp, #0 mov r0, r5 @@ -20955,7 +20955,7 @@ MainLoop: ; 0x02065D1C ldr r0, [r0] bl InitWanTable bl sub_0201F208 - bl OpenAllPackFiles + bl DirectoryFileMngr_ExtractAllDirectoryFiles bl sub_0204AB18 bl InitKaomadoStream bl sub_02025AD8 diff --git a/asm/main_02075600.s b/asm/main_02075600.s index 8099d7c8..355b87c8 100644 --- a/asm/main_02075600.s +++ b/asm/main_02075600.s @@ -52459,15 +52459,15 @@ _020AFF3C_EU: ; pack file related - .global PACK_FILES_OPENED ; 0x20af69c - PACK_FILES_OPENED: - ; [Runtime] A pointer to the 6 opened Pack files (listed at PACK_FILE_PATHS_TABLE) + .global DIRECTORY_FILES_EXTRACTED ; 0x20af69c + DIRECTORY_FILES_EXTRACTED: + ; [Runtime] A pointer to the 6 opened Pack files (listed at DIRECTORY_FILE_TABLE) .word 0x0 ; List of pointers to path strings to all known pack files. ; The game uses this table to load its resources when launching dungeon mode. - .global PACK_FILE_PATHS_TABLE ; 0x020af6a0 - PACK_FILE_PATHS_TABLE: + .global DIRECTORY_FILE_TABLE ; 0x020af6a0 + DIRECTORY_FILE_TABLE: .word STRING_MONSTER_MONSTER_BIN .word STRING_MONSTER_M_ATTACK_BIN .word STRING_MONSTER_M_GROUND_BIN diff --git a/asm/overlay_10.s b/asm/overlay_10.s index ad36e170..25f6b818 100644 --- a/asm/overlay_10.s +++ b/asm/overlay_10.s @@ -2244,7 +2244,7 @@ _022BE830: add r1, r1, #0x10c mov r1, r1, lsl #0x10 mov r1, r1, lsr #0x10 - bl GetFileLengthInPackWithPackNb + bl DirectoryFileMngr_GetDirectoryFileSize cmp r4, r0 bge _022BE980 add r5, sp, #0x30 @@ -2306,7 +2306,7 @@ _022BE918: mov r1, r1, asr #0x10 mov r1, r1, lsl #0x10 mov r1, r1, lsr #0x10 - bl GetFileLengthInPackWithPackNb + bl DirectoryFileMngr_GetDirectoryFileSize cmp r4, r0 bge _022BE980 add r5, sp, #4 @@ -6980,7 +6980,7 @@ ov10_022C2588: ; 0x022C2588 mov r1, r0, lsr #0x10 add r2, sp, #4 mov r0, #4 - bl AllocAndLoadFileInPack + bl DirectoryFileMngr_OpenDirectoryFile ldr r1, [sp, #4] add r0, sp, #0 bl HandleSir0Translation @@ -7007,7 +7007,7 @@ ov10_022C25EC: ; 0x022C25EC mov r1, r0, lsr #0x10 mov r2, r4 mov r0, #4 - bl AllocAndLoadFileInPack + bl DirectoryFileMngr_OpenDirectoryFile ldr r1, [r4] add r0, sp, #0 bl HandleSir0Translation @@ -7041,7 +7041,7 @@ ov10_022C2654: ; 0x022C2654 mov r1, r0, lsr #0x10 mov r2, r4 mov r0, #4 - bl AllocAndLoadFileInPack + bl DirectoryFileMngr_OpenDirectoryFile ldr r1, [r4] add r0, sp, #0 bl HandleSir0Translation @@ -7076,7 +7076,7 @@ InitTilesetBuffer: ; 0x022C26BC mov r1, r0, lsr #0x10 add r2, sp, #4 mov r0, #4 - bl AllocAndLoadFileInPack + bl DirectoryFileMngr_OpenDirectoryFile ldr r1, [sp, #4] add r0, sp, #0 bl HandleSir0Translation @@ -7109,7 +7109,7 @@ ov10_022C2720: ; 0x022C2720 add r2, sp, #4 mov r3, r4 mov r0, #4 - bl AllocAndLoadFileInPack + bl DirectoryFileMngr_OpenDirectoryFile ldr r1, [sp, #4] add r0, sp, #0 bl HandleSir0Translation @@ -7126,7 +7126,7 @@ _022C2788: add r2, sp, #4 mov r3, r4 mov r0, #4 - bl AllocAndLoadFileInPack + bl DirectoryFileMngr_OpenDirectoryFile ldr r1, [sp, #4] add r0, sp, #0 bl HandleSir0Translation diff --git a/asm/overlay_29_022DC240.s b/asm/overlay_29_022DC240.s index 9fb3b3d8..7a7b61bb 100644 --- a/asm/overlay_29_022DC240.s +++ b/asm/overlay_29_022DC240.s @@ -2511,7 +2511,7 @@ ov29_022DE300: ; 0x022DE300 mov r3, #0 ldr r2, [r2] add r2, r2, #0x40 - bl AllocAndLoadFileInPack + bl DirectoryFileMngr_OpenDirectoryFile ldr r0, _022DE410 ; =ov29_02353530 ldr r1, [r0] add r0, r1, #0x48 diff --git a/asm/overlay_29_022E0378.s b/asm/overlay_29_022E0378.s index 811d479a..174d5831 100644 --- a/asm/overlay_29_022E0378.s +++ b/asm/overlay_29_022E0378.s @@ -996,7 +996,7 @@ _022E0F80: add r2, sp, #8 mov r0, #4 mov r3, #0 - bl AllocAndLoadFileInPack + bl DirectoryFileMngr_OpenDirectoryFile ldr r1, [sp, #8] add r0, sp, #0x14 bl HandleSir0Translation @@ -1020,7 +1020,7 @@ _022E0F80: mov r0, #4 add r2, sp, #8 mov r3, #0 - bl AllocAndLoadFileInPack + bl DirectoryFileMngr_OpenDirectoryFile ldr r1, [sp, #8] add r0, sp, #0x10 bl HandleSir0Translation @@ -1060,7 +1060,7 @@ _022E1038: add r2, sp, #8 mov r0, #4 mov r3, #0 - bl AllocAndLoadFileInPack + bl DirectoryFileMngr_OpenDirectoryFile ldr r1, [sp, #8] add r0, sp, #0x18 bl HandleSir0Translation @@ -1084,7 +1084,7 @@ _022E1038: mov r0, #4 add r2, sp, #8 mov r3, #0 - bl AllocAndLoadFileInPack + bl DirectoryFileMngr_OpenDirectoryFile ldr r1, [sp, #8] add r0, sp, #0x1c bl HandleSir0Translation @@ -1103,7 +1103,7 @@ _022E1038: mov r0, #4 mov r1, #0x3e4 mov r3, #0 - bl AllocAndLoadFileInPack + bl DirectoryFileMngr_OpenDirectoryFile ldr r1, _022E11EC ; =ov29_02353540 ldr r0, _022E11F0 ; =ov29_02353540 ldr r1, [r1, #4] @@ -1160,7 +1160,7 @@ ov29_022E11F8: ; 0x022E11F8 add r2, sp, #4 mov r0, #4 mov r3, #0 - bl AllocAndLoadFileInPack + bl DirectoryFileMngr_OpenDirectoryFile ldr r1, [sp, #4] add r0, sp, #0 bl HandleSir0Translation @@ -1242,7 +1242,7 @@ ov29_022E1304: ; 0x022E1304 add r2, sp, #8 mov r0, #4 mov r3, #0 - bl AllocAndLoadFileInPack + bl DirectoryFileMngr_OpenDirectoryFile ldr r1, [sp, #8] add r0, sp, #0x10 bl HandleSir0Translation diff --git a/asm/overlay_29_022F7388.s b/asm/overlay_29_022F7388.s index 31fb6d5a..c7c2f546 100644 --- a/asm/overlay_29_022F7388.s +++ b/asm/overlay_29_022F7388.s @@ -167,7 +167,7 @@ _022F756C: _022F7598: mov r1, r4 mov r0, #0 - bl GetFileLengthInPackWithPackNb + bl DirectoryFileMngr_GetDirectoryFileSize ldr r1, _022F75C4 ; =DUNGEON_PTR ldr r1, [r1] add r1, r1, #0x2c000 diff --git a/asm/overlay_29_0231CBEC.s b/asm/overlay_29_0231CBEC.s index fc807e2f..6cf59e14 100644 --- a/asm/overlay_29_0231CBEC.s +++ b/asm/overlay_29_0231CBEC.s @@ -940,7 +940,7 @@ ApplyGonePebbleEffect: ; 0x0231D838 mov r0, #4 add r2, sp, #4 mov r3, #0 - bl AllocAndLoadFileInPack + bl DirectoryFileMngr_OpenDirectoryFile ldr r1, [sp, #4] add r0, sp, #0 bl HandleSir0Translation diff --git a/asm/overlay_29_02337CCC.s b/asm/overlay_29_02337CCC.s index f7841991..ea2df29c 100644 --- a/asm/overlay_29_02337CCC.s +++ b/asm/overlay_29_02337CCC.s @@ -1533,7 +1533,7 @@ ov29_02338F68: ; 0x02338F68 ldr r2, _02338FC0 ; =ov29_02353784 mov r0, #4 mov r3, #0 - bl AllocAndLoadFileInPack + bl DirectoryFileMngr_OpenDirectoryFile ldr r1, _02338FC4 ; =ov29_0235376C add r0, sp, #0 ldr r1, [r1, #0x18] diff --git a/include/directory_file.h b/include/directory_file.h new file mode 100644 index 00000000..c8c77452 --- /dev/null +++ b/include/directory_file.h @@ -0,0 +1,42 @@ +#ifndef PMDSKY_DIRECTORY_FILE_H +#define PMDSKY_DIRECTORY_FILE_H + +#include + +struct directory_entry { + u32 offset; // The offset of the file relative to the start of the Pack file + u32 length; // The size of the file +}; + +/* Information for reading a .bin Pack file + + A pack file is an archive that contains multiple other files. They aren't named. + + File format documentation at + https://projectpokemon.org/docs/mystery-dungeon-nds/pmd2-pack-file-format-r42 */ +struct directory { + struct directory_entry* table_of_content; + u32 zero; // Loaded from file, but is always zero here. Seemingly unused. + u32 nb_entry_table_of_content; // loaded from file + struct file_stream opened_file; +}; + +// Open a Pack file, to be read later. Initialise the output structure. +// pack_file: [output] pack file struct +// file_path: file name +void DirectoryFile_ExtractDirectoryFile(struct directory *pack_file, char *file_path); + +// Get the length of a file entry from a Pack archive +// pack_file: pack file struct +// file_index: file index +// return: size of the file in bytes from the Pack Table of Content +u32 DirectoryFile_GetDirectoryFileSize(struct directory* pack_file, u32 file_index); + +// Load the indexed file from the Pack archive, itself loaded from the ROM. +// pack_file: pack file struct +// output_buffer: [output] target buffer +// file_index: file index +// return: number of read bytes (identical to the length of the pack from the Table of Content) +u32 DirectoryFile_LoadDirectoryFile(struct directory *pack_file, void *output_buffer,u32 file_index); + +#endif //PMDSKY_DIRECTORY_FILE_H diff --git a/include/directory_file_mngr.h b/include/directory_file_mngr.h new file mode 100644 index 00000000..1e61245e --- /dev/null +++ b/include/directory_file_mngr.h @@ -0,0 +1,44 @@ +#ifndef PMDSKY_DIRECTORY_FILE_MNGR_H +#define PMDSKY_DIRECTORY_FILE_MNGR_H + +// Correspond to a .bin pack archive +enum directory_id { + PACK_ARCHIVE_MONSTER = 0, + PACK_ARCHIVE_M_ATTACK = 1, + PACK_ARCHIVE_M_GROUND = 2, + PACK_ARCHIVE_EFFECT = 3, + PACK_ARCHIVE_DUNGEON = 4, + PACK_ARCHIVE_M_LEVEL = 5 +}; + +// Contain return result of the DirectoryFileMngr_OpenDirectoryFile function +struct directory_open_result { + void* data; + u32 length; +}; + +// Open the 6 files at DIRECTORY_FILE_TABLE into DIRECTORY_FILES_EXTRACTED. Called during game initialization. +void DirectoryFileMngr_ExtractAllDirectoryFiles(void); + +// Call DirectoryFile_GetDirectoryFileSize after looking up the global Pack archive by its number +// pack_id: pack file number +// file_index: file number +// return: size of the file in bytes from the Pack Table of Content +u32 DirectoryFileMngr_GetDirectoryFileSize(enum directory_id pack_id, u32 file_index); + +// Call DirectoryFile_LoadDirectoryFile after looking up the global Pack archive by its identifier +// pack_id: pack file identifier +// file_index: file index +// output_buffer: [output] target buffer +// return: number of read bytes (identical to the length of the pack from the Table of Content) +u32 DirectoryFileMngr_LoadDirectoryFile(enum directory_id pack_id, u32 file_index, void* output_buffer); + +// Allocate a file and load a file from the pack archive inside. +// The data pointed by the pointer in the output need to be freed once is not needed anymore. +// pack_id: pack file identifier +// file_index: file index +// output: [output] result struct (will contain length and pointer) +// malloc_flags: allocation flags +void DirectoryFileMngr_OpenDirectoryFile(enum directory_id pack_id, u32 file_index, struct directory_open_result *output, u32 malloc_flags); + +#endif //PMDSKY_DIRECTORY_FILE_MNGR_H diff --git a/include/files/pack.h b/include/files/pack.h deleted file mode 100644 index bceea285..00000000 --- a/include/files/pack.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef PMDSKY_FILES_PACK_H -#define PMDSKY_FILES_PACK_H - -#include - -struct pack_file_table_of_content { - u32 offset; // The offset of the file relative to the start of the Pack file - u32 length; // The size of the file -}; - -/* Information for reading a .bin Pack file - - A pack file is an archive that contains multiple other files. They aren't named. - - File format documentation at - https://projectpokemon.org/docs/mystery-dungeon-nds/pmd2-pack-file-format-r42 */ -struct pack_file_opened { - struct pack_file_table_of_content* table_of_content; - u32 zero; // Loaded from file, but is always zero here. Seemingly unused. - u32 nb_entry_table_of_content; // loaded from file - struct file_stream opened_file; -}; - -// Correspond to a .bin pack archive -enum pack_file_id { - PACK_ARCHIVE_MONSTER = 0, - PACK_ARCHIVE_M_ATTACK = 1, - PACK_ARCHIVE_M_GROUND = 2, - PACK_ARCHIVE_EFFECT = 3, - PACK_ARCHIVE_DUNGEON = 4, - PACK_ARCHIVE_M_LEVEL = 5 -}; - -// Contain return result of the AllocAndLoadFileInPack function -struct pack_alloc_and_load_result { - void* data; - u32 length; -}; - -#endif \ No newline at end of file diff --git a/include/main_pack.h b/include/main_pack.h deleted file mode 100644 index c7431e10..00000000 --- a/include/main_pack.h +++ /dev/null @@ -1,48 +0,0 @@ -#ifndef PMDSKY_MAIN_PACK_H -#define PMDSKY_MAIN_PACK_H - -#include - -// Open the 6 files at PACK_FILE_PATHS_TABLE into PACK_FILES_OPENED. Called during game initialization. -void OpenAllPackFiles(void); - -// Call GetFileLengthInPack after looking up the global Pack archive by its number -// pack_id: pack file number -// file_index: file number -// return: size of the file in bytes from the Pack Table of Content -u32 GetFileLengthInPackWithPackNb(enum pack_file_id pack_id, u32 file_index); - -// Call LoadFileInPack after looking up the global Pack archive by its identifier -// pack_id: pack file identifier -// file_index: file index -// output_buffer: [output] target buffer -// return: number of read bytes (identical to the length of the pack from the Table of Content) -u32 LoadFileInPackWithPackId(enum pack_file_id pack_id, u32 file_index, void* output_buffer); - -// Allocate a file and load a file from the pack archive inside. -// The data pointed by the pointer in the output need to be freed once is not needed anymore. -// pack_id: pack file identifier -// file_index: file index -// output: [output] result struct (will contain length and pointer) -// malloc_flags: allocation flags -void AllocAndLoadFileInPack(enum pack_file_id pack_id, u32 file_index, struct pack_alloc_and_load_result *output, u32 malloc_flags); - -// Open a Pack file, to be read later. Initialise the output structure. -// pack_file: [output] pack file struct -// file_path: file name -void OpenPackFile(struct pack_file_opened *pack_file, char *file_path); - -// Get the length of a file entry from a Pack archive -// pack_file: pack file struct -// file_index: file index -// return: size of the file in bytes from the Pack Table of Content -u32 GetFileLengthInPack(struct pack_file_opened* pack_file, u32 file_index); - -// Load the indexed file from the Pack archive, itself loaded from the ROM. -// pack_file: pack file struct -// output_buffer: [output] target buffer -// file_index: file index -// return: number of read bytes (identical to the length of the pack from the Table of Content) -u32 LoadFileInPack(struct pack_file_opened *pack_file, void *output_buffer,u32 file_index); - -#endif //PMDSKY_MAIN_PACK_H diff --git a/main.lsf b/main.lsf index 3e29c122..ba7e2075 100644 --- a/main.lsf +++ b/main.lsf @@ -11,7 +11,8 @@ Static main StackSize 0 2048 Object asm/main_02000C6C.o Object src/debug.o - Object src/main_pack.o + Object src/directory_file_mngr.o + Object src/directory_file.o Object asm/main_0200C4CC.o Object src/main_02054BE0.o Object asm/main_02054C24.o diff --git a/src/directory_file.c b/src/directory_file.c new file mode 100644 index 00000000..89114058 --- /dev/null +++ b/src/directory_file.c @@ -0,0 +1,45 @@ +#include "directory_file.h" + +extern void DataTransferInit(void); +extern void DataTransferStop(void); +extern void FileSeek(struct file_stream* file, int offset, int whence); +extern u32 FileRead(struct file_stream* file, void* buf, u32 size); +extern void FileInitVeneer(struct file_stream* file); +extern void FileOpen(struct file_stream* file, const char* filepath); + +#include "debug.h" + +extern void* MemAlloc(u32 len, u32 flags); + +extern char STRING_FILE_DIRECTORY_INIT; + + +void DirectoryFile_ExtractDirectoryFile(struct directory *pack_file, char *file_path) { + DataTransferInit(); + FileInitVeneer(&pack_file->opened_file); + FileOpen(&pack_file->opened_file,file_path); + FileRead(&pack_file->opened_file,&pack_file->zero,8); + FileSeek(&pack_file->opened_file,8,0); + u32 toc_total_size = pack_file->nb_entry_table_of_content * sizeof(struct directory_entry); + struct directory_entry *toc_alloc = (struct directory_entry *) MemAlloc(toc_total_size, 0); + pack_file->table_of_content = toc_alloc; + FileRead(&pack_file->opened_file,pack_file->table_of_content,toc_total_size); + Debug_Print0(&STRING_FILE_DIRECTORY_INIT,pack_file->zero, + pack_file->nb_entry_table_of_content,toc_total_size,file_path); + DataTransferStop(); + return; +} + +u32 DirectoryFile_GetDirectoryFileSize(struct directory* pack_file, u32 file_index) { + struct directory_entry* element = &pack_file->table_of_content[file_index]; + return element->length; +} + +u32 DirectoryFile_LoadDirectoryFile(struct directory *pack_file,void *output_buffer,u32 file_index) { + DataTransferInit(); + struct directory_entry *toc_entry = &pack_file->table_of_content[file_index]; + FileSeek(&pack_file->opened_file,toc_entry->offset,0); + FileRead(&pack_file->opened_file,output_buffer,toc_entry->length); + DataTransferStop(); + return toc_entry->length; +} diff --git a/src/directory_file_mngr.c b/src/directory_file_mngr.c new file mode 100644 index 00000000..0265e9f9 --- /dev/null +++ b/src/directory_file_mngr.c @@ -0,0 +1,35 @@ +#include "directory_file_mngr.h" +#include "directory_file.h" + +extern void* MemAlloc(u32 len, u32 flags); +extern void MemZero(void* ptr, u32 len); + +extern struct directory *DIRECTORY_FILES_EXTRACTED; +extern char* DIRECTORY_FILE_TABLE[6]; + +void DirectoryFileMngr_ExtractAllDirectoryFiles(void) { + u32 total_size = sizeof(struct directory[6]); + DIRECTORY_FILES_EXTRACTED = MemAlloc(total_size, 0); + MemZero(DIRECTORY_FILES_EXTRACTED, total_size); + for (s16 i = 0; i < 6; i++) { + DirectoryFile_ExtractDirectoryFile(&DIRECTORY_FILES_EXTRACTED[i], DIRECTORY_FILE_TABLE[i]); + }; +} + +u32 DirectoryFileMngr_GetDirectoryFileSize(enum directory_id pack_id, u32 file_index) { + struct directory* pack_file = &DIRECTORY_FILES_EXTRACTED[pack_id]; + return DirectoryFile_GetDirectoryFileSize(pack_file, file_index); +} + +u32 DirectoryFileMngr_LoadDirectoryFile(enum directory_id pack_id, u32 file_index, void* output_buffer) { + return DirectoryFile_LoadDirectoryFile(&DIRECTORY_FILES_EXTRACTED[pack_id], output_buffer, file_index); +} + +void DirectoryFileMngr_OpenDirectoryFile(enum directory_id pack_id, u32 file_index, struct directory_open_result *output, u32 malloc_flags) { + u32 entry_length = DirectoryFile_GetDirectoryFileSize(&DIRECTORY_FILES_EXTRACTED[pack_id], file_index); + output->length = entry_length; + void * data_dest = MemAlloc(output->length, malloc_flags); + output->data = data_dest; + DirectoryFile_LoadDirectoryFile(&DIRECTORY_FILES_EXTRACTED[pack_id], output->data, file_index); + return; +} diff --git a/src/main_pack.c b/src/main_pack.c deleted file mode 100644 index fca9b3cd..00000000 --- a/src/main_pack.c +++ /dev/null @@ -1,72 +0,0 @@ -#include "main_pack.h" - -extern void DataTransferInit(void); -extern void DataTransferStop(void); -extern void FileSeek(struct file_stream* file, int offset, int whence); -extern u32 FileRead(struct file_stream* file, void* buf, u32 size); -extern void FileInitVeneer(struct file_stream* file); -extern char STRING_FILE_DIRECTORY_INIT; -extern void Debug_Print0(const char* fmt, ...); -extern void FileOpen(struct file_stream* file, const char* filepath); -extern void* MemAlloc(u32 len, u32 flags); -extern void MemZero(void* ptr, u32 len); - -extern struct pack_file_opened *PACK_FILES_OPENED; -extern char* PACK_FILE_PATHS_TABLE[6]; - -void OpenAllPackFiles(void) { - u32 total_size = sizeof(struct pack_file_opened[6]); - PACK_FILES_OPENED = MemAlloc(total_size, 0); - MemZero(PACK_FILES_OPENED, total_size); - for (s16 i = 0; i < 6; i++) { - OpenPackFile(&PACK_FILES_OPENED[i], PACK_FILE_PATHS_TABLE[i]); - }; -} - -u32 GetFileLengthInPackWithPackNb(enum pack_file_id pack_id, u32 file_index) { - struct pack_file_opened* pack_file = &PACK_FILES_OPENED[pack_id]; - return GetFileLengthInPack(pack_file, file_index); -} - -u32 LoadFileInPackWithPackId(enum pack_file_id pack_id, u32 file_index, void* output_buffer) { - return LoadFileInPack(&PACK_FILES_OPENED[pack_id], output_buffer, file_index); -} - -void AllocAndLoadFileInPack(enum pack_file_id pack_id, u32 file_index, struct pack_alloc_and_load_result *output, u32 malloc_flags) { - u32 entry_length = GetFileLengthInPack(&PACK_FILES_OPENED[pack_id], file_index); - output->length = entry_length; - void * data_dest = MemAlloc(output->length, malloc_flags); - output->data = data_dest; - LoadFileInPack(&PACK_FILES_OPENED[pack_id], output->data, file_index); - return; -} - -void OpenPackFile(struct pack_file_opened *pack_file, char *file_path) { - DataTransferInit(); - FileInitVeneer(&pack_file->opened_file); - FileOpen(&pack_file->opened_file,file_path); - FileRead(&pack_file->opened_file,&pack_file->zero,8); - FileSeek(&pack_file->opened_file,8,0); - u32 toc_total_size = pack_file->nb_entry_table_of_content * sizeof(struct pack_file_table_of_content); - struct pack_file_table_of_content *toc_alloc = (struct pack_file_table_of_content *) MemAlloc(toc_total_size, 0); - pack_file->table_of_content = toc_alloc; - FileRead(&pack_file->opened_file,pack_file->table_of_content,toc_total_size); - Debug_Print0(&STRING_FILE_DIRECTORY_INIT,pack_file->zero, - pack_file->nb_entry_table_of_content,toc_total_size,file_path); - DataTransferStop(); - return; -} - -u32 GetFileLengthInPack(struct pack_file_opened* pack_file, u32 file_index) { - struct pack_file_table_of_content* element = &pack_file->table_of_content[file_index]; - return element->length; -} - -u32 LoadFileInPack(struct pack_file_opened *pack_file,void *output_buffer,u32 file_index) { - DataTransferInit(); - struct pack_file_table_of_content *toc_entry = &pack_file->table_of_content[file_index]; - FileSeek(&pack_file->opened_file,toc_entry->offset,0); - FileRead(&pack_file->opened_file,output_buffer,toc_entry->length); - DataTransferStop(); - return toc_entry->length; -}