|
| 1 | +/* |
| 2 | +** Command & Conquer Generals(tm) |
| 3 | +** Copyright 2025 TheSuperHackers |
| 4 | +** |
| 5 | +** This program is free software: you can redistribute it and/or modify |
| 6 | +** it under the terms of the GNU General Public License as published by |
| 7 | +** the Free Software Foundation, either version 3 of the License, or |
| 8 | +** (at your option) any later version. |
| 9 | +** |
| 10 | +** This program is distributed in the hope that it will be useful, |
| 11 | +** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +** GNU General Public License for more details. |
| 14 | +** |
| 15 | +** You should have received a copy of the GNU General Public License |
| 16 | +** along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | +*/ |
| 18 | + |
| 19 | +// DataChunk.h |
| 20 | +// Main public API for DataChunk library |
| 21 | +// TheSuperHackers @feature bobtista 14/11/2025 Extract chunk I/O to platform-neutral library |
| 22 | + |
| 23 | +#pragma once |
| 24 | + |
| 25 | +#include "DataChunk/Types.h" |
| 26 | +#include "DataChunk/Stream.h" |
| 27 | +#include "DataChunk/TableOfContents.h" |
| 28 | +#include <string> |
| 29 | + |
| 30 | +namespace DataChunk { |
| 31 | + |
| 32 | +//---------------------------------------------------------------------- |
| 33 | +// OutputChunk |
| 34 | +//---------------------------------------------------------------------- |
| 35 | +/** Internal structure for tracking open output chunks. */ |
| 36 | +struct OutputChunk |
| 37 | +{ |
| 38 | + OutputChunk* next; |
| 39 | + ChunkUInt id; // chunk symbol type from table of contents |
| 40 | + ChunkInt filepos; // position of file at start of data offset |
| 41 | + |
| 42 | + OutputChunk() : next(NULL), id(0), filepos(0) {} |
| 43 | +}; |
| 44 | + |
| 45 | +//---------------------------------------------------------------------- |
| 46 | +// InputChunk |
| 47 | +//---------------------------------------------------------------------- |
| 48 | +/** Internal structure for tracking open input chunks. */ |
| 49 | +struct InputChunk |
| 50 | +{ |
| 51 | + InputChunk* next; |
| 52 | + ChunkUInt id; // chunk symbol type from table of contents |
| 53 | + DataChunkVersionType version; // version of data |
| 54 | + ChunkInt chunkStart; // position of the start of chunk data (past header) |
| 55 | + ChunkInt dataSize; // total data size of chunk |
| 56 | + ChunkInt dataLeft; // data left to read in this chunk |
| 57 | + |
| 58 | + InputChunk() : next(NULL), id(0), version(0), chunkStart(0), dataSize(0), dataLeft(0) {} |
| 59 | +}; |
| 60 | + |
| 61 | +//---------------------------------------------------------------------- |
| 62 | +// DataChunkInfo |
| 63 | +//---------------------------------------------------------------------- |
| 64 | +/** Information about a chunk being parsed. */ |
| 65 | +struct DataChunkInfo |
| 66 | +{ |
| 67 | + ChunkString label; |
| 68 | + ChunkString parentLabel; |
| 69 | + DataChunkVersionType version; |
| 70 | + ChunkInt dataSize; |
| 71 | +}; |
| 72 | + |
| 73 | +//---------------------------------------------------------------------- |
| 74 | +// DataChunkParserPtr |
| 75 | +//---------------------------------------------------------------------- |
| 76 | +/** Function pointer type for parsing chunks. */ |
| 77 | +typedef bool (*DataChunkParserPtr)(class DataChunkInput& file, DataChunkInfo* info, void* userData); |
| 78 | + |
| 79 | +//---------------------------------------------------------------------- |
| 80 | +// UserParser |
| 81 | +//---------------------------------------------------------------------- |
| 82 | +/** Internal structure for registered parsers. */ |
| 83 | +struct UserParser |
| 84 | +{ |
| 85 | + UserParser* next; |
| 86 | + DataChunkParserPtr parser; // the user parsing function |
| 87 | + ChunkString label; // the data chunk label to match |
| 88 | + ChunkString parentLabel; // the parent chunk's label (the scope) |
| 89 | + void* userData; // user data pointer |
| 90 | + |
| 91 | + UserParser() : next(NULL), parser(NULL), userData(NULL) {} |
| 92 | +}; |
| 93 | + |
| 94 | +//---------------------------------------------------------------------- |
| 95 | +// DataChunkOutput |
| 96 | +//---------------------------------------------------------------------- |
| 97 | +/** Class for writing chunk-based data files. |
| 98 | + Platform-neutral replacement for engine's DataChunkOutput. */ |
| 99 | +class DataChunkOutput |
| 100 | +{ |
| 101 | + DataChunkOutputStream* m_pOut; // The actual output stream |
| 102 | + DataChunkTableOfContents m_contents; // table of contents of data chunk types |
| 103 | + OutputChunk* m_chunkStack; // current stack of open data chunks |
| 104 | + |
| 105 | + // Internal buffer for writing (replaces temp file) |
| 106 | + char* m_buffer; |
| 107 | + unsigned int m_bufferSize; |
| 108 | + unsigned int m_bufferPos; |
| 109 | + |
| 110 | + void growBuffer(unsigned int needed); |
| 111 | + |
| 112 | +public: |
| 113 | + DataChunkOutput(DataChunkOutputStream* pOut); |
| 114 | + ~DataChunkOutput(); |
| 115 | + |
| 116 | + /** Open a new data chunk. |
| 117 | + @param name Chunk type name (will be added to string table) |
| 118 | + @param ver Version number for this chunk */ |
| 119 | + void openDataChunk(const char* name, DataChunkVersionType ver); |
| 120 | + |
| 121 | + /** Close the current data chunk. */ |
| 122 | + void closeDataChunk(); |
| 123 | + |
| 124 | + /** Write a float value. */ |
| 125 | + void writeReal(ChunkReal r); |
| 126 | + |
| 127 | + /** Write an integer value. */ |
| 128 | + void writeInt(ChunkInt i); |
| 129 | + |
| 130 | + /** Write a byte value. */ |
| 131 | + void writeByte(ChunkByte b); |
| 132 | + |
| 133 | + /** Write an ASCII string (length-prefixed, no null terminator). */ |
| 134 | + void writeAsciiString(const ChunkString& string); |
| 135 | + |
| 136 | + /** Write a Unicode string (length-prefixed, no null terminator). */ |
| 137 | + void writeUnicodeString(const ChunkWideString& string); |
| 138 | + |
| 139 | + /** Write an array of bytes. */ |
| 140 | + void writeArrayOfBytes(const char* ptr, ChunkInt len); |
| 141 | +}; |
| 142 | + |
| 143 | +//---------------------------------------------------------------------- |
| 144 | +// DataChunkInput |
| 145 | +//---------------------------------------------------------------------- |
| 146 | +/** Class for reading chunk-based data files. |
| 147 | + Platform-neutral replacement for engine's DataChunkInput. */ |
| 148 | +class DataChunkInput |
| 149 | +{ |
| 150 | + enum { CHUNK_HEADER_BYTES = 4 }; // 2 shorts in chunk file header |
| 151 | + |
| 152 | + DataChunkInputStream* m_file; // input file stream |
| 153 | + DataChunkTableOfContents m_contents; // table of contents of data chunk types |
| 154 | + ChunkInt m_fileposOfFirstChunk; // seek position of first data chunk |
| 155 | + UserParser* m_parserList; // list of all registered parsers |
| 156 | + InputChunk* m_chunkStack; // current stack of open data chunks |
| 157 | + |
| 158 | + void clearChunkStack(); |
| 159 | + void decrementDataLeft(ChunkInt size); |
| 160 | + |
| 161 | +public: |
| 162 | + void* m_currentObject; // user parse routines can use this |
| 163 | + void* m_userData; // user data hook |
| 164 | + |
| 165 | + DataChunkInput(DataChunkInputStream* pStream); |
| 166 | + ~DataChunkInput(); |
| 167 | + |
| 168 | + /** Register a parser function for data chunks. |
| 169 | + @param label Chunk label to match |
| 170 | + @param parentLabel Parent chunk label (or empty for global scope) |
| 171 | + @param parser Parser function to call |
| 172 | + @param userData Optional user data to pass to parser */ |
| 173 | + void registerParser(const ChunkString& label, const ChunkString& parentLabel, |
| 174 | + DataChunkParserPtr parser, void* userData = NULL); |
| 175 | + |
| 176 | + /** Parse the chunk stream using registered parsers. |
| 177 | + @param userData Optional user data to pass to parsers |
| 178 | + @return true on success, false on failure */ |
| 179 | + bool parse(void* userData = NULL); |
| 180 | + |
| 181 | + /** Check if file has valid chunk format. |
| 182 | + @return true if valid format */ |
| 183 | + bool isValidFileType(); |
| 184 | + |
| 185 | + /** Open the next data chunk. |
| 186 | + @param ver Output parameter for chunk version |
| 187 | + @return Chunk label name */ |
| 188 | + ChunkString openDataChunk(DataChunkVersionType* ver); |
| 189 | + |
| 190 | + /** Close the current chunk and move to next. */ |
| 191 | + void closeDataChunk(); |
| 192 | + |
| 193 | + /** Check if at end of file. |
| 194 | + @return true if at end */ |
| 195 | + bool atEndOfFile(); |
| 196 | + |
| 197 | + /** Check if at end of current chunk. |
| 198 | + @return true if all data read from chunk */ |
| 199 | + bool atEndOfChunk(); |
| 200 | + |
| 201 | + /** Reset to just-opened state. */ |
| 202 | + void reset(); |
| 203 | + |
| 204 | + /** Get label of current chunk. |
| 205 | + @return Chunk label name */ |
| 206 | + ChunkString getChunkLabel(); |
| 207 | + |
| 208 | + /** Get version of current chunk. |
| 209 | + @return Chunk version number */ |
| 210 | + DataChunkVersionType getChunkVersion(); |
| 211 | + |
| 212 | + /** Get size of data in current chunk. |
| 213 | + @return Data size in bytes */ |
| 214 | + ChunkUInt getChunkDataSize(); |
| 215 | + |
| 216 | + /** Get size of data left to read in current chunk. |
| 217 | + @return Remaining data size in bytes */ |
| 218 | + ChunkUInt getChunkDataSizeLeft(); |
| 219 | + |
| 220 | + /** Read a float value. */ |
| 221 | + ChunkReal readReal(); |
| 222 | + |
| 223 | + /** Read an integer value. */ |
| 224 | + ChunkInt readInt(); |
| 225 | + |
| 226 | + /** Read a byte value. */ |
| 227 | + ChunkByte readByte(); |
| 228 | + |
| 229 | + /** Read an ASCII string. */ |
| 230 | + ChunkString readAsciiString(); |
| 231 | + |
| 232 | + /** Read a Unicode string. */ |
| 233 | + ChunkWideString readUnicodeString(); |
| 234 | + |
| 235 | + /** Read an array of bytes. |
| 236 | + @param ptr Buffer to read into |
| 237 | + @param len Number of bytes to read */ |
| 238 | + void readArrayOfBytes(char* ptr, ChunkInt len); |
| 239 | +}; |
| 240 | + |
| 241 | +} // namespace DataChunk |
| 242 | + |
0 commit comments