21void* TTRB::s_pDefAllocatorUserData =
TNULL;
41 ERROR error = m_TTSFI.Open( a_szFilePath );
45 if ( m_TTSFI.m_Magic ==
TFourCC(
"TRBF" ) )
49 if ( ProcessForm( m_TTSFI ) )
77 static constexpr TUINT32 MAX_RELC_NUM_BATCH = 512;
78 RELCEntry relcEntries[ MAX_RELC_NUM_BATCH ];
81 TINT32 leftSize = fileSize;
97 uint8_t readResult = ttsf.
ReadHunk();
100 sectionName = ttsf.m_CurrentHunk.
Name;
101 sectionSize = ttsf.m_CurrentHunk.
Size;
102 leftSize -=
TAlignNumUp( sectionSize ) +
sizeof( Toshi::TTSF::Hunk );
104 if (
TFourCC(
"HEAD" ) < sectionName )
break;
106 if ( sectionName ==
TFourCC(
"HEAD" ) )
108 TINT numsections = ( sectionSize - 4 ) / 0xC;
113 ttsf.
ReadRaw( &m_pHeader->m_i32SectionCount,
sizeof( m_pHeader->m_i32SectionCount ) );
115 TASSERT( m_pHeader->m_i32SectionCount == numsections,
"HEAD section has wrong num of sections" );
117 for (
TINT i = 0; i < m_pHeader->m_i32SectionCount; i++ )
122 pSect->m_Data = m_MemAllocator(
AllocType_Unk1, pSect->m_Size, 0, 0, m_MemUserData );
123 pSect->m_Unk1 = ( pSect->m_Unk1 == 0 ) ? 16 : pSect->m_Unk1;
129 else if ( sectionName ==
TFourCC(
"SYMB" ) )
131 m_SYMB =
static_cast<SYMB*
>( m_MemAllocator(
AllocType_Unk2, ttsf.m_CurrentHunk.
Size, 0, 0, m_MemUserData ) );
134 else if ( sectionName ==
TFourCC(
"SECC" ) )
136 Toshi::TCompress::ms_bIsBigEndian = ( CURRENT_ENDIANESS ==
Endianess_Big );
138 for (
TINT i = 0; i < m_pHeader->m_i32SectionCount; i++ )
142 if ( secInfo->m_Data !=
TNULL )
150 else if ( sectionName ==
TFourCC(
"RELC" ) )
156 ttsf.
ReadRaw( &relocCount,
sizeof( relocCount ) );
158 if ( relocCount < 1 )
166 TUINT32 relocReadCount = relocCount - readedRelocs;
169 relocReadCount =
TMath::Min( relocReadCount, MAX_RELC_NUM_BATCH );
170 ttsf.
Read( relcEntries, relocReadCount );
171 curReloc = readedRelocs + relocReadCount;
173 auto& header = *m_pHeader;
174 for (
TUINT32 i = 0; i < relocReadCount; i++ )
176 auto& relcEntry = relcEntries[ i ];
180 if ( m_pHeader->m_ui32Version.Value >=
TVERSION( 1, 0 ) )
192 readedRelocs += relocReadCount;
193 }
while ( curReloc < relocCount );
207 if ( sectionName !=
TFourCC(
"FORM" ) )
209 if ( sectionName ==
TFourCC(
"SECT" ) )
211 for (
TINT i = 0; i < m_pHeader->m_i32SectionCount; i++ )
214 ttsf.
ReadRaw( pSect->m_Data, pSect->m_Size );
219 else if ( sectionName ==
TFourCC(
"HDRX" ) )
224 for (
TINT i = 0; i < m_pHeader->m_i32SectionCount; i++ )
227 pSect->m_Unk1 = ( pSect->m_Unk1 == 0 ) ? 16 : pSect->m_Unk1;
228 pSect->m_Data = m_MemAllocator(
AllocType_Unk1, pSect->m_Size, pSect->m_Unk1, pSect->m_Unk2, m_MemUserData );
244 TBOOL result = ProcessForm( ttsf );
255 if ( m_SYMB !=
TNULL && index != -1 && index < m_SYMB->m_i32SymbCount )
258 return static_cast<TCHAR*
>( GetSection( entry->HDRX ) ) + entry->DataOffset;
267 if ( m_SYMB !=
TNULL )
271 for (
TINT i = 0; i < m_SYMB->m_i32SymbCount; i++ )
275 if ( symbol->NameHash == hash )
277 if ( Toshi::TStringManager::String8Compare( symbName,
GetSymbolName( symbol ), -1 ) == 0 )
291 if ( m_pHeader !=
TNULL )
293 for (
TINT i = 0; i < m_pHeader->m_i32SectionCount; i++ )
297 if ( sec->m_Data !=
TNULL )
299 m_MemDeallocator(
AllocType_Unk1, sec->m_Data, sec->m_Unk1, sec->m_Unk2, m_MemUserData );
303 m_MemDeallocator(
AllocType_Unk0, m_pHeader, 0, 0, m_MemUserData );
313 m_MemAllocator = allocator;
314 m_MemDeallocator = deallocator;
315 m_MemUserData = userdata;
321 if ( m_SYMB !=
TNULL )
333 while ( *str !=
'\0' )
335 character = *( str++ );
336 hash = (
TINT16)character + hash * 0x1f;
345 if ( m_SYMB ==
TNULL )
350 return reinterpret_cast<const TCHAR*
>(
351 reinterpret_cast<uintptr_t
>( m_SYMB ) +
359 if ( m_SYMB ==
TNULL )
370 if ( m_SYMB ==
TNULL )
374 else if ( index < m_SYMB->m_i32SymbCount )
376 return reinterpret_cast<TTRBSymbol*
>( m_SYMB + 1 ) + index;
void * TMalloc(TSIZE a_uiSize, Toshi::TMemory::MemBlock *a_pMemBlock, const TCHAR *a_szFileName, TINT a_iLineNum)
Allocates memory from a specific memory block.
void TFree(void *a_pMem)
Frees previously allocated memory.
TRB (Toshi Relocatable Binary) resource system for the Toshi engine.
#define TSTATICCAST(POINTERTYPE, VALUE)
#define TREINTERPRETCAST(TYPE, VALUE)
#define TOSHI_NAMESPACE_START
#define TVERSION(VER_MAJOR, VER_MINOR)
#define TSTATICASSERT(...)
#define TOSHI_NAMESPACE_END
TFORCEINLINE constexpr T TAlignNumUp(T a_iValue, TSIZE a_uiAlignment=4)
TFORCEINLINE constexpr TUINT32 TFourCC(const TCHAR str[4])
#define TPROFILER_SCOPE()
TFORCEINLINE const T & Min(const T &a, const T &b)
void ReadCompressed(void *buffer, TUINT32 size)
void ReadRaw(void *dst, TUINT32 size)
TUINT8 ReadHunkData(void *dest)
TUINT8 ReadFORM(TFORM *section)
void(*)(AllocType alloctype, void *ptr, TINT16 unk1, TUINT32 unk2, void *userData) t_MemoryFuncDealloc
static TINT16 HashString(const TCHAR *str)
TTRBSymbol * GetSymbol(TINT index) const
static TUINT32 GetSymbolTableSize(TUINT32 count)
ERROR Load(const TCHAR *a_szFilePath, TUINT32 a_uiUnknown=0)
void * GetSymbolAddress(const TCHAR *symbName)
SecInfo * GetSectionInfo(TINT index) const
const TCHAR * GetSymbolName(TINT index) const
void SetMemoryFunctions(t_MemoryFuncAlloc allocator, t_MemoryFuncDealloc deallocator, void *userdata)
TINT GetSymbolIndex(const TCHAR *symbName)
void *(*)(AllocType alloctype, TUINT32 size, TINT16 unk1, TUINT32 unk2, void *userData) t_MemoryFuncAlloc