Advertisement
Bond697

lpkm file

Jul 7th, 2013
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.38 KB | None | 0 0
  1. #ifndef  FILE_H
  2. #define  FILE_H
  3.  
  4. #include <nds.h>
  5. #include <libpkm/types.h>
  6.  
  7.  
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11.  
  12. /*-----------------------------------------------------------------------------
  13. -- macros
  14. -----------------------------------------------------------------------------*/
  15.  
  16.  
  17.  
  18.  
  19. /*-----------------------------------------------------------------------------
  20. -- type definitions
  21. -----------------------------------------------------------------------------*/
  22.  
  23. typedef u8 FSFile[0x48];
  24. typedef u8 FSArchive[0x5C];
  25.  
  26.  
  27. /*-----------------------------------------------------------------------------
  28. -- enums
  29. -----------------------------------------------------------------------------*/
  30.  
  31. typedef enum SeekMode {
  32.  
  33.     SEEK_SET,
  34.     SEEK_CUR,
  35.     SEEK_END
  36.  
  37. } SeekMode;
  38.  
  39.  
  40. /*-----------------------------------------------------------------------------
  41. -- structs
  42. -----------------------------------------------------------------------------*/
  43.  
  44. typedef struct FileID {
  45.  
  46.     FSArchive*  arc;
  47.     u32         f_id;
  48.  
  49. } FileID;
  50.  
  51.  
  52. /*-----------------------------------------------------------------------------
  53. -- types
  54. -----------------------------------------------------------------------------*/
  55.  
  56.  
  57.  
  58.  
  59. /*-----------------------------------------------------------------------------
  60. -- function declarations
  61. -----------------------------------------------------------------------------*/
  62.  
  63. // official functions
  64. void FS_InitFile (FSFile* p_file);
  65. void FS_CloseFile(FSFile* p_file);
  66. BOOL FS_OpenFile (FSFile* file, const char* path);
  67. BOOL FS_SeekFile (FSFile* p_file, s32 offset, SeekMode mode);
  68. s32 FS_ReadFile  (FSFile* p_file, void* dst, s32 len);
  69. u32 FS_GetLength (FSFile* p_file);
  70. u32 FS_GetFilePosition(FSFile* p_file);
  71. void FS_CancelFile(FSFile* p_file);
  72.  
  73.  
  74. // use these as much as possible
  75. // check the bottom for info on using these
  76.  
  77. /*---------------------------------------------------------------------------*
  78.   Name:         ArchiveMoveImageTop
  79.  
  80.   Description:  takes an open file and seeks into a file number in the file, making that the new top - it can also return the file length
  81.  
  82.   Arguments:    FSFile* p_file      :  file to pull data from
  83.                 int fileNum         :  number of file in narc to get data on
  84.                 int offset          :  length into file to seek
  85.                 int known_length    : if this is set, return this and don't get the file size
  86.                                    
  87.  
  88.   Returns:      either the length of the file number if known_length is 0 or known_length if set
  89. *---------------------------------------------------------------------------*/
  90. int ArchiveMoveImageTop(FSFile* p_file, u16 fileNum, int seek_length, int read_length);
  91.  
  92.  
  93. /*---------------------------------------------------------------------------*
  94.   Name:         getNarcToFile
  95.  
  96.   Description:  loads a narc into memory
  97.  
  98.   Arguments:    FSFile* p_file      :  file to pull data from
  99.                 int narcPtrIdxNum   :  narc number 0 -> 308, or greater if you've added some - for info on how to convert a narc to a number, see narc.h
  100.                              
  101.   Returns:      none
  102. *---------------------------------------------------------------------------*/
  103. void getNarcToFile(FSFile* p_file, int narcPtrIdxNum);
  104.  
  105.  
  106. /*---------------------------------------------------------------------------*
  107.   Name:         readFileToPresetBlk
  108.  
  109.   Description:  reads a file into a pre-allocated memblk
  110.  
  111.   Arguments:    void* pFileDest     :  destination buffer for fileNum
  112.                 int narcPtrIdxNum   :  narc to read data from fileNum in from
  113.                 int fileNum         :  file number to fetch in narc
  114.                              
  115.   Returns:      none
  116. *---------------------------------------------------------------------------*/
  117. void readFileToPresetBlkViaNarc(void* pFileDest, int narcPtrIdxNum, u16 fileNum);
  118.  
  119.  
  120. /*---------------------------------------------------------------------------*
  121.   Name:         readFileToAllocatedBlk
  122.  
  123.   Description:  reads a file into an allocated memblk
  124.  
  125.   Arguments:    int narcPtrIdxNum   :  narc to read data from fileNum in from
  126.                 int fileNum         :  file number to fetch in narc
  127.                 int blkGroupID      :  block id to allocate memory from for the file that will be read in
  128.                              
  129.   Returns:      pointer to the memblk that was allocated inside the function
  130. *---------------------------------------------------------------------------*/
  131. void* readFileToAllocatedBlk(int narcPtrIdxNum, u16 fileNum, s16 blkGroupID);
  132.  
  133.  
  134. /*---------------------------------------------------------------------------*
  135.   Name:         setReadLengthAndSeekIntoFile
  136.  
  137.   Description:  reads a file(or a piece of) into a pre-allocated memblk
  138.  
  139.   Arguments:    void* pFileDest     :  destination buffer for fileNum
  140.                 int narcPtrIdxNum   :  narc to read data from fileNum in from
  141.                 int fileNum         :  file number to fetch in narc
  142.                 int offset          :  offset into the file to jump
  143.                              
  144.   Returns:      none
  145. *---------------------------------------------------------------------------*/
  146. void setReadLengthAndSeekIntoFile(void* pFileDest, int narcPtrIdxNum, u16 fileNum, int seek_length);
  147.  
  148.  
  149. /*---------------------------------------------------------------------------*
  150.   Name:         readInFullFileFromNarc
  151.  
  152.   Description:  reads one entire file into memory from the given narc
  153.  
  154.   Arguments:    const char* path    :  narc path
  155.                 int fileNum         :  file number to fetch in narc
  156.                 int blkGroupID      :  block id to allocate memory from for the file that will be read in
  157.                              
  158.   Returns:      pointer to the memblk with the file data that was allocated inside the function
  159. *---------------------------------------------------------------------------*/
  160. void* readInFullFileFromNarc(const char* path, u16 fileNum, s16 blkGroupID);
  161.  
  162.  
  163. /*---------------------------------------------------------------------------*
  164.   Name:         getLengthOfFileInNarc
  165.  
  166.   Description:  get file length
  167.  
  168.   Arguments:    int narcPtrIdxNum   :  narc to read data from fileNum in from
  169.                 int fileNum         :  file number to fetch in narc
  170.                              
  171.   Returns:      file length
  172. *---------------------------------------------------------------------------*/
  173. u32 getLengthOfFileInNarc(int narcPtrIdxNum, u16 fileNum);
  174.  
  175.  
  176. /*---------------------------------------------------------------------------*
  177.   Name:         readNarcIntoFile
  178.  
  179.   Description:  read an entire narc into a file in memory
  180.  
  181.   Arguments:    int narcPtrIdxNum   :  narc to read in
  182.                 int fileNum         :  file number to fetch in narc
  183.                              
  184.   Returns:      ptr to Arc_Tool struct with the FSFile and narc info- size, file count, etc(see narc.h for Arc_Tool info)
  185. *---------------------------------------------------------------------------*/
  186. void* readNarcIntoFile(int narcPtrIdxNum, s16 blkGroupID);
  187.  
  188.  
  189. /*---------------------------------------------------------------------------*
  190.   Name:         allocForAndReadInFile
  191.  
  192.   Description:  read a full file into memory
  193.  
  194.   Arguments:    FSFile* p_file      :  file to pull data from
  195.                 u16 fileNum         :  file number to fetch in narc
  196.                 s16 blkGroupID      :  block id to allocate memory from for the file that will be read in
  197.                              
  198.   Returns:      ptr to file memory allocated inside function
  199. *---------------------------------------------------------------------------*/
  200. void* allocForAndReadInFile(Arc_Tool* arc, u16 fileNum, s16 blkGroupID);
  201.  
  202.  
  203. /*---------------------------------------------------------------------------*
  204.   Name:         readInFile
  205.  
  206.   Description:  read a file into pre-allocated memory
  207.  
  208.   Arguments:    FSFile* p_file      :  file to pull data from
  209.                 u16 fileNum         :  file number to fetch in narc
  210.                 void* dest          :  buffer to fill with the file data
  211.                              
  212.   Returns:      none
  213. *---------------------------------------------------------------------------*/
  214. void readInFileToPresetBlkViaFile(FSFile* p_file, u16 fileNum, void* dest);
  215.  
  216.  
  217. /*---------------------------------------------------------------------------*
  218.   Name:         getFileLength
  219.  
  220.   Description:  get length of file number from file
  221.  
  222.   Arguments:    FSFile* p_file      :  file to pull data from
  223.                 u16 fileNum         :  file number to fetch in narc
  224.                              
  225.   Returns:      u32 length
  226. *---------------------------------------------------------------------------*/
  227. u32 getFileLength(FSFile* p_file, u16 fileNum);
  228.  
  229.  
  230. /*---------------------------------------------------------------------------*
  231.   Name:         readFileIntoBuffer
  232.  
  233.   Description:  simple read file into buffer
  234.  
  235.   Arguments:    FSFile* p_file      :  file to pull data from
  236.                 s32 length          :  read length
  237.                 void* pBuf          :  buffer to write the file into
  238.                              
  239.   Returns:      none
  240. *---------------------------------------------------------------------------*/
  241. void readFileIntoBuffer(FSFile* p_file, s32 length, void* pBuf);
  242.  
  243.  
  244. /*---------------------------------------------------------------------------*
  245.   Name:         seekFileFromBase
  246.  
  247.   Description:  simple seek file from the beginning
  248.  
  249.   Arguments:    FSFile* p_file      :  file to pull data from
  250.                 s32 seek_offset     :  amount to seek into the file
  251.                              
  252.   Returns:      none
  253. *---------------------------------------------------------------------------*/
  254. void seekFileFromBase(FSFile* p_file, s32 seek_offset);
  255.  
  256.  
  257. /*---------------------------------------------------------------------------*
  258.   Name:         closeFileFreeBlk
  259.  
  260.   Description:  close an open file and free its memory
  261.  
  262.   Arguments:    FSFile* p_file      :  pointer to file memory to free
  263.                              
  264.   Returns:      none
  265. *---------------------------------------------------------------------------*/
  266. void closeFileFreeBlk(FSFile* p_file);
  267.  
  268.  
  269. /*-----------------------------------------------------------------------------
  270. -- inline Functions
  271. -----------------------------------------------------------------------------*/
  272.  
  273.  
  274.  
  275.  
  276. #ifdef __cplusplus
  277. } /* extern "C" */
  278. #endif
  279.  
  280. #endif
  281.  
  282.  
  283. /*
  284. how to open a file from rom manually:
  285.  
  286. -create an FSFile: ex. FSFile file;
  287. -FS_InitFile(file);
  288. -FS_OpenFile(file, path_ptr) - you will need to provide a pointer to your path here.  ideally by adding it to the end of the narc list using a hack that i should have implemented by the time you use this.
  289. (-FS_SeekFile)
  290. -FS_ReadFile - when you do this, you need to provide a destination pointer for it to  write to.  use the alloc header to create a block from free space.
  291. */
  292.  
  293. /*
  294. how to open a file with a custom function: (using fetching evolution info as an example)
  295.  
  296. void* f_blk = allocateBlockFromExpHeap(4, sizeof(struct EvoData), FALSE, g_EvoSrc, BLK_TYPE_INDICATOR);
  297. readFileToPresetBlk(f_blk, EVO_NARC, POKE_METANG_EVO_INFO);
  298.  
  299. * get info from evolution data block *
  300.  
  301. freeBlock(f_blk);
  302. */
  303.  
  304. /*
  305. how to open a file with a custom function AND using an FSFile
  306.  
  307. FSFile p_file;
  308. void* p_fileBlk;
  309.  
  310. FS_InitFile (p_file);
  311. FS_OpenFile(p_file, p_narcPath);
  312. p_fileBlk = allocForAndReadInFile(p_file, POKE_METAGROSS, BLK_GROUP_TYPE);
  313.  
  314. * get info from block *
  315.  
  316. closeFileFreeBlk(p_file);
  317. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement