OpenBarnyard
 
Loading...
Searching...
No Matches
TTSF.cpp
Go to the documentation of this file.
1#include "ToshiPCH.h"
2#include "TTSF.h"
3
4//-----------------------------------------------------------------------------
5// Enables memory debugging.
6// Note: Should be the last include!
7//-----------------------------------------------------------------------------
9
11
12// $Barnyard: FUNCTION 006d8ca0
14{
15 m_pFile = a_pFile;
16 m_FileInfoCount = 0;
17 m_UNKFLAG = TFALSE;
18
19 if ( m_pFile == TNULL )
20 {
22 }
23
24 m_UNKFLAG = TTRUE;
25
26 m_pFile->Read( &m_Header, sizeof( TTSFI::Header ) );
27
28 if ( m_Header.Magic == IDMAGICL )
29 {
31 }
32 else
33 {
34 if ( m_Header.Magic != IDMAGICB )
35 {
37 }
38
40 }
41
42 m_ReadPos += m_pFile->Read( &m_Magic, 4 );
43
45 {
46 m_Header.FileSize = PARSEDWORD_BIG( m_Header.FileSize );
47 m_Magic = PARSEDWORD_BIG( m_Magic );
48 }
49
50 m_CurrentHunk.Name = m_Header.Magic;
51 m_CurrentHunk.Size = m_Header.FileSize;
52 PushForm();
53
54 return TTRB::ERROR_OK;
55}
56
57// $Barnyard: FUNCTION 006d8e70
58TUINT32 TTSFI::Open( const TCHAR* a_szFilePath )
59{
60 TFile* pFile = TFile::Create( a_szFilePath );
61 auto uiResult = Open( pFile );
62 m_UNKFLAG = TFALSE;
63 return uiResult;
64}
65
66// $Barnyard: FUNCTION 006d8be0
68{
69 if ( m_CurrentHunk.Name != TFourCC( "FORM" ) &&
70 m_CurrentHunk.Name != TFourCC( "TSFL" ) &&
71 m_CurrentHunk.Name != TFourCC( "TSFB" ) )
72 {
74 }
75
76 m_FileInfo[ m_FileInfoCount ].FileStartOffset = m_pFile->Tell() - 4;
77 m_FileInfo[ m_FileInfoCount ].FileSize = m_CurrentHunk.Size;
78 m_FileInfoCount++;
79
80 return TTRB::ERROR_OK;
81}
82
83// $Barnyard: FUNCTION 006d8c30
85{
86 if ( m_FileInfoCount < 1 ) return TTRB::ERROR_NO_FILEINFO_ON_STACK;
87
88 m_FileInfoCount--;
89 auto& fileInfo = m_FileInfo[ m_FileInfoCount ];
90
91 m_CurrentHunk.Size = fileInfo.FileSize;
92 m_ReadPos = m_pFile->Tell() - fileInfo.FileStartOffset;
93
94 TUINT32 alignedPos = TAlignNumUp( m_CurrentHunk.Size );
95 m_pFile->Seek( alignedPos - m_ReadPos, TSEEK_CUR );
96 m_ReadPos = alignedPos;
97
98 return TTRB::ERROR_OK;
99}
100
101// $Barnyard: FUNCTION 006d8a60
103{
104 m_pFile->Read( &m_CurrentHunk, sizeof( Hunk ) );
105
107 {
108 m_CurrentHunk.Name = PARSEDWORD_BIG( m_CurrentHunk.Name );
109 m_CurrentHunk.Size = PARSEDWORD_BIG( m_CurrentHunk.Size );
110 }
111
112 m_ReadPos = 0;
113
114 return TTRB::ERROR_OK;
115}
116
117// $Barnyard: FUNCTION 006d8ba0
119{
120 TUINT32 alignedSize = TAlignNumUp( m_CurrentHunk.Size );
121 m_pFile->Seek( alignedSize - m_ReadPos, TSEEK_CUR );
122 m_ReadPos = alignedSize;
123
124 return TTRB::ERROR_OK;
125}
126
127// $Barnyard: FUNCTION 006d8ae0
129{
130 if ( m_CurrentHunk.Name != TFourCC( "FORM" ) )
131 {
133 }
134
135 m_ReadPos += m_pFile->Read( section, sizeof( TFORM ) );
136
137 return TTRB::ERROR_OK;
138}
139
140// $Barnyard: FUNCTION 006d8e00
142{
143 if ( m_CurrentHunk.Name == TFourCC( "FORM" ) )
144 {
146 }
147
148 TASSERT( m_ReadPos == 0, "m_ReadPos should be zero" );
149 m_ReadPos += m_pFile->Read( dst, m_CurrentHunk.Size );
150 m_ReadPos += TTSFI::ReadAlignmentPad();
151
152 return TTRB::ERROR_OK;
153}
154
155// $Barnyard: FUNCTION 006d8dc0
156void TTSFI::Close( TBOOL free )
157{
158 PopForm();
159
160 if ( m_pFile != TNULL && free )
161 {
162 m_pFile->Destroy();
163 }
164
165 m_pFile = TNULL;
166 m_FileInfoCount = 0;
167}
168
169// $Barnyard: FUNCTION 006d8b40
170void TTSFI::ReadCompressed( void* buffer, TUINT32 size )
171{
172 TCompress::Header header;
173
174 TUINT32 headerStart = m_pFile->Tell();
175 int8_t error = TCompress::GetHeader( m_pFile, header );
176
177 if ( error == TCOMPRESS_ERROR_OK )
178 {
179 TUINT32 headerSize = m_pFile->Tell() - headerStart;
180 TCompress::Decompress( m_pFile, &header, (TBYTE*)buffer, size );
181 m_ReadPos += header.CompressedSize + headerSize;
182 }
183}
184
185// $Barnyard: FUNCTION 006d8a20
187{
188 m_pFile = TNULL;
189 m_FileInfoCount = 0;
190 m_UNKFLAG = TFALSE;
191 m_ReadPos = 0;
192 TUtil::MemClear( m_FileInfo, sizeof( m_FileInfo ) );
193 m_Magic = 0;
194 m_CurrentHunk.Name = 0;
195 m_CurrentHunk.Size = 0;
196}
197
198TTSFO::ERROR TTSFO::Create( const TCHAR* filepath, const TCHAR* magic, Endianess endianess )
199{
201
202 if ( m_pFile != TNULL )
203 {
204 TTSF::Hunk hunk{ TFourCCLE( "TSFL" ), 0 };
205 TUINT32 magicValue = TFourCC( magic );
206
207 if ( endianess == Endianess_Big )
208 {
209 hunk.Name = TFourCCLE( "TSFB" );
210 hunk.Size = PARSEDWORD_BIG( hunk.Size );
211 }
212
213 Write( hunk );
214 Write( magicValue );
215
216 m_Endianess = endianess;
217 m_Positions[ m_PositionCount ] = 0;
218 m_PositionCount += 1;
219
220 return TTRB::ERROR_OK;
221 }
222 else
223 {
224 return TTSFO::ERROR_FILE;
225 }
226}
227
229{
230 if ( m_pFile != TNULL )
231 {
232 EndForm();
233 m_pFile->Destroy();
234 m_pFile = TNULL;
235 }
236}
237
239{
240 TASSERT( m_pFile != TNULL, "TTSFO is not created" );
241
242 TTSF::Hunk hunk{ TFourCC( "FORM" ), 0 };
243 auto nameValue = TFourCC( name );
244
245 if ( m_Endianess == Endianess_Big )
246 {
247 hunk.Name = PARSEDWORD_BIG( hunk.Name );
248 nameValue = PARSEDWORD_BIG( nameValue );
249 }
250
251 m_Positions[ m_PositionCount++ ] = m_pFile->Tell();
252
253 auto written1 = Write( hunk );
254 auto written2 = Write( nameValue );
255
256 return written1 + written2;
257}
258
260{
261 TASSERT( m_pFile != TNULL, "TTSFO is not created" );
262
263 if ( m_PositionCount > 0 )
264 {
265 auto formPosition = m_Positions[ --m_PositionCount ];
266
267 auto oldPos = m_pFile->Tell();
268 auto formSize = ( oldPos - formPosition ) - 8;
269
270 if ( m_Endianess == Endianess_Big )
271 {
272 formSize = PARSEDWORD_BIG( formSize );
273 }
274
275 m_pFile->Seek( formPosition + 4, TSEEK_SET );
276 Write( formSize );
277 m_pFile->Seek( oldPos, TSEEK_SET );
278
279 return formSize;
280 }
281
282 return 0;
283}
284
285TBOOL TTSFO::OpenHunk( HunkMark* hunkMark, const TCHAR* hunkName )
286{
287 TASSERT( hunkMark != TNULL, "HunkMark is TNULL" );
288 hunkMark->Name = TFourCC( hunkName );
289 hunkMark->Pos = m_pFile->Tell();
290
291 WriteHunk( hunkMark->Name, TNULL, 0 );
292 return TTRUE;
293}
294
296{
297 TASSERT( hunkMark != TNULL, "HunkMark is TNULL" );
298 auto oldPos = m_pFile->Tell();
299
300 m_pFile->Seek( hunkMark->Pos, TSEEK_SET );
301 WriteHunk( hunkMark->Name, TNULL, ( oldPos - hunkMark->Pos ) - sizeof( TTSF::Hunk ) );
302 m_pFile->Seek( oldPos, TSEEK_SET );
304
305 return TTRUE;
306}
307
308TUINT32 TTSFO::WriteHunk( TUINT32 hunkName, void* buffer, TSIZE bufferSize )
309{
310 TTSF::Hunk hunk{ hunkName, bufferSize };
311
312 // Convert endianess
313 if ( m_Endianess == Endianess_Big )
314 {
315 hunk.Name = PARSEDWORD_BIG( hunk.Name );
316 hunk.Size = PARSEDWORD_BIG( hunk.Size );
317 }
318
319 // Write hunk
320 TSIZE writtenSize = Write( hunk );
321
322 // Write buffer
323 if ( buffer != TNULL && bufferSize > 0 )
324 {
325 writtenSize += m_pFile->Write( buffer, bufferSize );
326 }
327
328 // Return num of written bytes
329 return writtenSize + TTSFO::WriteAlignmentPad();
330}
331
333{
334 TASSERT( m_pFile != TNULL, "File is TNULL" );
335
336 static TCHAR s_AlignBuffer[ 4 ];
337 TUINT8 alignValue = 4 - ( m_pFile->Tell() & 3 );
338
339 if ( alignValue != 4 )
340 {
341 return m_pFile->Read( s_AlignBuffer, alignValue );
342 }
343
344 return 0;
345}
346
347void TTSFI::ReadRaw( void* dst, TUINT32 size )
348{
349 m_ReadPos += m_pFile->Read( dst, size );
350}
351
@ TCOMPRESS_ERROR_OK
Definition TCompress.h:9
@ TSEEK_SET
Definition TFile.h:30
@ TSEEK_CUR
Definition TFile.h:31
@ TFILEMODE_WRITE
Definition TFile.h:39
@ TFILEMODE_CREATENEW
Definition TFile.h:41
#define TASSERT(X,...)
Definition Defines.h:138
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
@ Endianess_Big
Definition Endianness.h:9
@ Endianess_Little
Definition Endianness.h:8
TUINT8 Endianess
Definition Endianness.h:5
TFORCEINLINE TUINT32 TFourCCLE(const TCHAR str[4])
Definition Helpers.h:21
TFORCEINLINE constexpr T TAlignNumUp(T a_iValue, TSIZE a_uiAlignment=4)
Definition Helpers.h:66
TFORCEINLINE constexpr TUINT32 TFourCC(const TCHAR str[4])
Definition Helpers.h:15
size_t TSIZE
Definition Typedefs.h:9
char TCHAR
Definition Typedefs.h:20
uint8_t TUINT8
Definition Typedefs.h:17
#define TNULL
Definition Typedefs.h:23
uint32_t TUINT32
Definition Typedefs.h:13
#define TFALSE
Definition Typedefs.h:24
#define TTRUE
Definition Typedefs.h:25
bool TBOOL
Definition Typedefs.h:6
uint8_t TBYTE
Definition Typedefs.h:19
static uintptr_t Decompress(TFile *file, TCompress::Header *header, TBYTE *buffer, TUINT32 bufferSize)
static int8_t GetHeader(TFile *file, TCompress::Header &btecHeader)
TUINT32 CompressedSize
Definition TCompress.h:40
Definition TFile.h:128
static TFile * Create(const TString8 &a_rcFilename, TFILEMODE a_eMode=TFILEMODE_READ)
Definition TFile.cpp:29
Definition TTRB.h:31
static constexpr TUINT32 IDMAGICL
Definition TTRB.h:40
TFile * m_pFile
Definition TTRB.h:70
static constexpr TUINT32 IDMAGICB
Definition TTRB.h:39
Endianess m_Endianess
Definition TTRB.h:69
TUINT32 Open(TFile *a_pFile)
Definition TTSF.cpp:13
void ReadCompressed(void *buffer, TUINT32 size)
Definition TTSF.cpp:170
TUINT8 ReadHunk()
Definition TTSF.cpp:102
TUINT8 SkipHunk()
Definition TTSF.cpp:118
void Close(TBOOL free=true)
Definition TTSF.cpp:156
TUINT32 PushForm()
Definition TTSF.cpp:67
void ReadRaw(void *dst, TUINT32 size)
Definition TTSF.cpp:347
TUINT32 ReadAlignmentPad()
Definition TTSF.cpp:332
TTSFI()
Definition TTSF.cpp:186
TUINT8 ReadHunkData(void *dest)
Definition TTSF.cpp:141
TUINT32 PopForm()
Definition TTSF.cpp:84
TUINT8 ReadFORM(TFORM *section)
Definition TTSF.cpp:128
void Close()
Closes the file.
Definition TTSF.cpp:228
TSIZE BeginForm(const TCHAR *name)
Begin a new form and saves it's info.
Definition TTSF.cpp:238
TUINT32 WriteAlignmentPad()
Aligns current position to 4.
Definition TTRB.h:144
@ ERROR_FILE
Definition TTRB.h:125
TTSFO::ERROR Create(const TCHAR *filepath, const TCHAR *magic="TRBF", Endianess endianess=Endianess_Little)
Creates file for writing.
Definition TTSF.cpp:198
TBOOL CloseHunk(HunkMark *hunkMark)
Closes the hunk.
Definition TTSF.cpp:295
TSIZE EndForm()
Ends the current form if it exists.
Definition TTSF.cpp:259
TBOOL OpenHunk(HunkMark *hunkMark, const TCHAR *hunkName)
Opens new hunk.
Definition TTSF.cpp:285
TUINT32 Write(const T &value)
Definition TTRB.h:216
TUINT8 ERROR
Definition TTRB.h:120
TUINT32 WriteHunk(TUINT32 hunkName, void *buffer=nullptr, TSIZE bufferSize=0)
Writes a hunk of data.
Definition TTSF.cpp:308
TUINT32 Pos
Definition TTRB.h:131
TUINT32 Name
Definition TTRB.h:130
@ ERROR_NOT_TRB
Definition TTRB.h:265
@ ERROR_WRONG_MAGIC
Definition TTRB.h:262
@ ERROR_FORM_MAGIC
Definition TTRB.h:263
@ ERROR_OK
Definition TTRB.h:258
@ ERROR_NO_FILE
Definition TTRB.h:264
@ ERROR_NO_FILEINFO_ON_STACK
Definition TTRB.h:266
static void MemClear(void *ptr, TSIZE size)
Definition TUtil.h:91