1 /* 2 This file is part of Warzone 2100. 3 Copyright (C) 1999-2004 Eidos Interactive 4 Copyright (C) 2005-2020 Warzone 2100 Project 5 6 Warzone 2100 is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2 of the License, or 9 (at your option) any later version. 10 11 Warzone 2100 is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with Warzone 2100; if not, write to the Free Software 18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 */ 20 /*! \file 21 * \brief Resource file processing functions 22 */ 23 24 #ifndef _frameresource_h 25 #define _frameresource_h 26 27 #include "lib/framework/frame.h" 28 29 /** Maximum number of characters in a resource type. */ 30 #define RESTYPE_MAXCHAR 20 31 32 33 /** Function pointer for a function that loads from a memory buffer. */ 34 typedef bool (*RES_BUFFERLOAD)(const char *pBuffer, UDWORD size, void **pData); 35 36 /** Function pointer for a function that loads from a filename. */ 37 typedef bool (*RES_FILELOAD)(const char *pFile, void **pData); 38 39 /** Function pointer for releasing a resource loaded by the above functions. */ 40 typedef void (*RES_FREE)(void *pData); 41 42 /** callback type for resload display callback. */ 43 typedef void (*RESLOAD_CALLBACK)(); 44 45 struct RES_DATA 46 { 47 void *pData; // pointer to the actual data 48 SDWORD blockID; // which of the blocks is it in (so we can clear some of them...) 49 50 UDWORD HashedID; // hashed version of the name of the id 51 RES_DATA *psNext; // next entry - most likely to be following on! 52 UDWORD usage; // Reference count 53 54 // ID of the resource - filename from the .wrf - e.g. "TRON.PIE" 55 const char *aID; 56 }; 57 58 59 // New reduced resource type ... specially for PSX 60 // These types are statically defined in data.c 61 struct RES_TYPE 62 { 63 // type is still needed on psx ... strings are defined in source - data.c (yak!) 64 char aType[RESTYPE_MAXCHAR]; // type string (e.g. "PIE" - just for debug use only, only applicable when loading from wrf (not wdg) 65 66 RES_BUFFERLOAD buffLoad; // routine to process the data for this type 67 RES_FREE release; // routine to release the data (NULL indicates none) 68 69 // we must have a pointer to the data here so that we can do a resGetData(); 70 RES_DATA *psRes; // Linked list of data items of this type 71 UDWORD HashedType; // hashed version of the name of the id - // a null hashedtype indicates end of list 72 73 RES_FILELOAD fileLoad; // This isn't really used any more ? 74 RES_TYPE *psNext; 75 }; 76 77 78 /** Set the function to call when loading files with resloadfile. */ 79 void resSetLoadCallback(RESLOAD_CALLBACK funcToCall); 80 81 /* do the callback for the resload display function */ 82 void resDoResLoadCallback(); 83 84 /** Initialise the resource module. */ 85 bool resInitialise(); 86 87 /** Shutdown the resource module. */ 88 void resShutDown(); 89 90 /** Set the base resource directory. */ 91 WZ_DECL_NONNULL(1) void resSetBaseDir(const char *pResDir); 92 WZ_DECL_NONNULL(1) void resForceBaseDir(const char *pResDir); 93 94 /** Parse the res file. */ 95 WZ_DECL_NONNULL(1) bool resLoad(const char *pResFile, SDWORD blockID); 96 97 /** Release all the resources currently loaded and the resource load functions. */ 98 void resReleaseAll(); 99 100 /** Release the data for a particular block ID. */ 101 void resReleaseBlockData(SDWORD blockID); 102 103 /** Release all the resources currently loaded but keep the resource load functions. */ 104 void resReleaseAllData(); 105 106 /** Add a buffer load and release function for a file type. */ 107 WZ_DECL_NONNULL(1) bool resAddBufferLoad(const char *pType, RES_BUFFERLOAD buffLoad, RES_FREE release); 108 109 /** Add a file name load and release function for a file type. */ 110 WZ_DECL_NONNULL(1) bool resAddFileLoad(const char *pType, RES_FILELOAD fileLoad, RES_FREE release); 111 112 /** Call the load function for a file. */ 113 WZ_DECL_NONNULL(1, 2) bool resLoadFile(const char *pType, const char *pFile); 114 115 /** Return the resource for a type and ID */ 116 WZ_DECL_NONNULL(1) void *resGetDataFromHash(const char *pType, UDWORD HashedID); 117 WZ_DECL_NONNULL(1, 2) void *resGetData(const char *pType, const char *pID); 118 WZ_DECL_NONNULL(1, 2) bool resPresent(const char *pType, const char *pID); 119 WZ_DECL_NONNULL(1) void resToLower(char *pStr); 120 121 /** Return the HashedID string for a piece of data. */ 122 WZ_DECL_NONNULL(1, 3) bool resGetHashfromData(const char *pType, const void *pData, UDWORD *pHash); 123 124 /** Retrieve the resource ID string 125 * \param type the resource type string (e.g. "IMG", "IMD", "TEXPAGE", "WAV", etc.) 126 * \param data the resource pointer to retrieve the ID string for 127 * \return the from the ID string (usually its filename without directory) 128 * \note passing a NULL pointer for either \c type or \c data is valid (the result will be an empty string though) 129 */ 130 const char *resGetNamefromData(const char *type, const void *data); 131 132 /** Return last imd resource */ 133 const char *GetLastResourceFilename() WZ_DECL_PURE; 134 135 /** Set the resource name of the last resource file loaded. */ 136 WZ_DECL_NONNULL(1) void SetLastResourceFilename(const char *pName); 137 138 #endif // _frameresource_h 139