OpenBarnyard
 
Loading...
Searching...
No Matches
AMaterialLibraryManager.cpp
Go to the documentation of this file.
1#include "pch.h"
3#include <File/TTRB.h>
4
5#include "GUI/AGUISystem.h"
9#include "ALoadScreen.h"
10
11//-----------------------------------------------------------------------------
12// Enables memory debugging.
13// Note: Should be the last include!
14//-----------------------------------------------------------------------------
15#include <Core/TMemoryDebugOn.h>
16
18
20
22 : m_NumRefLibraries( AMemory::GetAllocator( AMemory::POOL_Misc ) ), m_LoadedLibraries( AMemory::GetAllocator( AMemory::POOL_Misc ) )
23{
24 List::CreateSingleton();
25
26 m_iNumUsedTextures = 0;
27 m_iNumFreeTextures = 0;
28
29 for ( TUINT i = 0; i < MAX_NUM_TEXTURES; i++ )
30 {
31 m_FreeTextures.PushFront( &m_aSlots[ i ] );
32 m_iNumFreeTextures++;
33 }
34}
35
36void AMaterialLibraryManager::LoadLibrariesFromProperties( const PBPropertyValue* a_pProperty, TTRB* a_pTRB, TBOOL a_bUpdateGUIMaterials )
37{
38 if ( a_pProperty )
39 {
40 auto pArray = a_pProperty->GetArray();
41
42 for ( TUINT i = 0; i < pArray->GetSize(); i++ )
43 {
44 auto matlibName = pArray->GetValue( i )->GetTPString8();
45 auto eLangId = ALocaleManager::GetSingleton()->GetLanguage();
46
47 TBOOL bLocaliseAsset;
48
50 {
51 bLocaliseAsset = TFALSE;
52 }
53 else
54 {
55 bLocaliseAsset = TPString8( "gui_loc" ) == matlibName;
56 }
57
58 if ( bLocaliseAsset )
59 {
60 TString8 localisedAssetName = matlibName.GetString8();
61 localisedAssetName += "_";
62 localisedAssetName += ALocaleManager::Interface()->GetCurrentLanguageName();
63 localisedAssetName.MakeLower();
64
65 matlibName = TPString8( localisedAssetName );
66 }
67
68 LoadLibrary( matlibName, a_pTRB, TFALSE );
69 g_oLoadScreen.Update( 1.0f, TTRUE );
70 }
71
74
75 if ( a_bUpdateGUIMaterials && AGUISystem::GetSingleton() )
76 {
78 }
79
80 g_oLoadScreen.Update( 1.0f, TTRUE );
81 }
82}
83
84void AMaterialLibraryManager::LoadLibrary( const TPString8& a_rLibName, TTRB* a_pTRB, TBOOL a_bIsGUI )
85{
86 auto pRenderer = TRenderInterface::GetSingleton();
87
88 if ( !m_LoadedLibraries.IsValid( m_LoadedLibraries.Find( a_rLibName ) ) )
89 {
90 TString8 fileMiddle = "";
91 TString8 fileFormat = "";
92
93 if ( !pRenderer->Supports32BitTextures() )
94 {
95 fileFormat = "16";
96 }
97
98 TCHAR matlibPath[ 128 ];
100 matlibPath,
101 sizeof( matlibPath ),
102 "data/matlibs/%s%s%s.ttl",
103 a_rLibName.GetPooledString()->GetString8().GetString(),
104 fileMiddle.GetString(),
105 fileFormat.GetString()
106 );
107
108 AMaterialLibrary* pLibrary = List::GetSingleton()->CreateLibraryFromAsset( matlibPath, a_pTRB );
109
110 if ( pLibrary )
111 {
112 m_LoadedLibraries.Insert( a_rLibName, pLibrary );
113 CreateTextures( pLibrary );
114 m_NumRefLibraries.Insert( a_rLibName, 1 );
115
116 if ( a_rLibName.GetString8().Find( "gui_" ) == 0 )
117 {
118 a_bIsGUI = TTRUE;
119 }
120
121 OnLibraryLoaded( a_bIsGUI );
122 }
123 }
124 else
125 {
126 auto pRefCount = m_NumRefLibraries.Find( a_rLibName );
127 pRefCount->GetSecond() += 1;
128 }
129}
130
131void AMaterialLibraryManager::UnloadLibrary( const TPString8& a_rLibName, TBOOL a_bUnused )
132{
133 TPString8 matlibName = a_rLibName;
134 auto eLangId = ALocaleManager::GetSingleton()->GetLanguage();
135
136 TBOOL bLocaliseAsset;
137
139 {
140 bLocaliseAsset = TFALSE;
141 }
142 else
143 {
144 bLocaliseAsset = TPString8( "gui_loc" ) == matlibName;
145 }
146
147 if ( bLocaliseAsset )
148 {
149 TString8 localisedAssetName = matlibName.GetString8();
150 localisedAssetName += "_";
151 localisedAssetName += ALocaleManager::Interface()->GetCurrentLanguageName();
152 localisedAssetName.MakeLower();
153
154 matlibName = TPString8( localisedAssetName );
155 }
156
158
159 auto pMatlibNode = m_LoadedLibraries.Find( matlibName );
160
161 if ( pMatlibNode != m_LoadedLibraries.End() )
162 {
163 auto pRefCountNode = m_NumRefLibraries.Find( matlibName );
164
165 if ( m_NumRefLibraries.IsValid( pRefCountNode ) )
166 {
167 pRefCountNode->GetSecond() -= 1;
168
169 if ( pRefCountNode->GetSecond() == 0 )
170 {
171 DestroyLibrary( pMatlibNode, a_bUnused );
172 m_NumRefLibraries.Remove( pRefCountNode );
173 }
174 }
175 }
176}
177
178void AMaterialLibraryManager::DestroyLibrary( LibrariesMap::Iterator& a_rcMaterialLibraryNode, TBOOL a_bUpdateGUIMaterials )
179{
180 auto pMaterialLibrary = a_rcMaterialLibraryNode->GetSecond();
181 UnloadTexturesOfLibrary( pMaterialLibrary );
182
183 pMaterialLibrary->Destroy();
184 m_LoadedLibraries.Remove( a_rcMaterialLibraryNode );
185
188
189 if ( a_bUpdateGUIMaterials && AGUISystem::GetSingleton() )
190 {
192 }
193}
194
196{
197 for ( auto it = m_UsedTextures.Begin(); it != m_UsedTextures.End(); )
198 {
199 auto pNextNode = it->Next();
200
201 if ( it->GetLibrary() == a_pMaterialLibrary )
202 {
203 auto removedSlot = m_UsedTextures.Erase( it );
204 m_iNumUsedTextures -= 1;
205
206 removedSlot->GetTexture()->DestroyResource();
207 removedSlot->SetTexture( TNULL );
208 removedSlot->ResetName();
209
210 m_FreeTextures.PushFront( removedSlot );
211 m_iNumFreeTextures += 1;
212 }
213
214 it = it->Next();
215 }
216
218 TTODO( "FUN_006120e0" );
219}
220
222{
224
225 for ( TINT i = 0; i < a_pMatLibrary->GetNumTextures(); i++ )
226 {
227 auto pMatlibTexture = a_pMatLibrary->GetTexture( i );
228
229 auto pTexSlot = m_FreeTextures.PopFront();
230 m_iNumFreeTextures -= 1;
231
232 pTexSlot->SetName( pMatlibTexture->Name );
233 pTexSlot->SetLibrary( a_pMatLibrary );
234 pTexSlot->SetTexture( pTextureFactory->CreateFromT2Texture( pMatlibTexture->pTexture ) );
235
236 m_UsedTextures.PushFront( pTexSlot );
237 m_iNumUsedTextures += 1;
238 }
239}
240
242{
245
246 if ( a_bUpdateGUIMaterials && AGUISystem::GetSingleton() )
247 {
249 }
250}
251
253{
254 for ( auto it = m_UsedTextures.Begin(); it != m_UsedTextures.End(); it = it->Next() )
255 {
256 if ( TStringManager::String8CompareNoCase( it->GetName(), a_szTextureName ) == 0 )
257 {
258 return it->GetTexture();
259 }
260 }
261
262 return TNULL;
263}
264
266{
268
269 auto pLibrary = new AMaterialLibrary;
270 pLibrary->SetPath( a_szFilePath );
271
272 if ( !pLibrary->LoadTRBFile( a_pTRB ) )
273 {
274 delete pLibrary;
275 return TNULL;
276 }
277
278 m_Libraries.PushBack( pLibrary );
279 return pLibrary;
280}
281
283{
285
286 auto pLibrary = new AMaterialLibrary;
287 pLibrary->SetPath( a_szFilePath );
288
289 TBOOL bSuccess;
290
291 if ( a_pTRB )
292 {
293 auto iFilePathLength = TStringManager::String8Length( a_szFilePath );
294 auto iFileNamePos = iFilePathLength - 1;
295
296 while ( a_szFilePath[ iFileNamePos ] != '\\' && a_szFilePath[ iFileNamePos ] != '/' )
297 iFileNamePos--;
298
299 iFileNamePos++;
300
301 TCHAR symbolName[ 132 ];
302 auto iLen = iFilePathLength - iFileNamePos - 4;
303 TStringManager::String8Copy( symbolName, a_szFilePath + iFileNamePos, iFilePathLength - iFileNamePos - 4 );
304
305 symbolName[ iLen ] = '_';
306 symbolName[ iLen + 1 ] = '\0';
308 TStringManager::String8Copy( symbolName + iLen + 1, "TTL" );
309
310 auto pTTLData = a_pTRB->GetSymbolAddress( symbolName );
311
312 if ( pTTLData )
313 {
314 bSuccess = pLibrary->LoadTTLData( pTTLData );
315 }
316 else
317 {
318 bSuccess = pLibrary->LoadTTLFile( a_szFilePath );
319 }
320 }
321 else
322 {
323 bSuccess = pLibrary->LoadTTLFile( a_szFilePath );
324 }
325
326 if ( !bSuccess )
327 {
328 if ( pLibrary )
329 {
330 delete pLibrary;
331 pLibrary = TNULL;
332 }
333 }
334 else
335 {
336 m_Libraries.PushBack( pLibrary );
337 }
338
339 return pLibrary;
340}
341
342// $Barnyard: FUNCTION 00615900
344{
345 TVALIDPTR( a_pLibrary );
346 a_pLibrary->Destroy();
347}
348
349ATexture* AMaterialLibraryManager::List::FindTexture( const TCHAR* a_szTextureName, AMaterialLibrary** a_ppMaterialLibrary, TINT* a_pTextureIndex )
350{
351 TINT iIndex = -1;
352 auto it = m_Libraries.Begin();
353
354 while ( true )
355 {
356 if ( it == m_Libraries.End() )
357 {
358 return TNULL;
359 }
360
361 iIndex = it->FindTextureIndex( a_szTextureName );
362 if ( iIndex != -1 ) break;
363
364 it++;
365 }
366
367 if ( a_ppMaterialLibrary )
368 {
369 *a_ppMaterialLibrary = it;
370 }
371
372 if ( a_pTextureIndex )
373 {
374 *a_pTextureIndex = iIndex;
375 }
376
377 return it->GetTexture( iIndex );
378}
TRB (Toshi Relocatable Binary) resource system for the Toshi engine.
@ SYSRESOURCE_TEXTUREFACTORY
Definition TRender.h:20
Rendering system interface for the Toshi engine.
#define TOSHI_NAMESPACE_USING
Definition Defines.h:46
#define TTODO(DESC)
Definition Defines.h:134
#define TVALIDPTR(PTR)
Definition Defines.h:139
#define TPROFILER_SCOPE()
Definition Profiler.h:17
#define TDEFINE_CLASS(...)
Definition TObject.h:120
unsigned int TUINT
Definition Typedefs.h:8
char TCHAR
Definition Typedefs.h:20
#define TNULL
Definition Typedefs.h:23
int TINT
Definition Typedefs.h:7
#define TFALSE
Definition Typedefs.h:24
#define TTRUE
Definition Typedefs.h:25
bool TBOOL
Definition Typedefs.h:6
ALoadScreen g_oLoadScreen
Definition TTRB.h:253
void * GetSymbolAddress(const TCHAR *symbName)
Definition TTRB.cpp:251
const class PBPropertyValueArray * GetArray() const
T * GetSystemResource(SYSRESOURCE systemResource)
const TString8 & GetString8() const
Definition TPString8.h:39
TFORCEINLINE const TString8 & GetString8() const
Definition TPString8.h:172
TFORCEINLINE const TPooledString8 * GetPooledString() const
Definition TPString8.h:177
static TFORCEINLINE TRenderInterface * GetSingleton()
Definition TSingleton.h:49
TString8 & MakeLower()
Definition TString8.h:87
TINT Find(TCHAR character, TINT pos=0) const
Definition TString8.cpp:107
const TCHAR * GetString(TINT a_iIndex=0) const
Definition TString8.cpp:286
static void String8ToLowerCase(TCHAR *str)
static TSIZE String8Length(const TCHAR *str)
static TINT String8CompareNoCase(const TCHAR *str1, const TCHAR *str2, TSIZE size=-1)
static void String8Format(TCHAR *str, TINT size, const TCHAR *format,...)
static TCHAR * String8Copy(TCHAR *dst, const TCHAR *src, TSIZE size=-1)
void SetPath(const TCHAR *a_szFilePath)
ATexture * GetTexture(TINT a_uiIndex)
TINT GetNumTextures() const
void UnloadLibrary(const Toshi::TPString8 &a_rLibName, TBOOL a_bUnused)
void UnloadTexturesOfLibrary(AMaterialLibrary *a_pMaterialLibrary)
void LoadLibrariesFromProperties(const PBPropertyValue *a_pProperty, Toshi::TTRB *a_pTRB, TBOOL a_bUpdateGUIMaterials)
static constexpr TUINT MAX_NUM_TEXTURES
Toshi::TTexture * FindTexture(const TCHAR *a_szTextureName)
void CreateTextures(AMaterialLibrary *a_pMatLibrary)
void LoadLibrary(const Toshi::TPString8 &a_rLibName, Toshi::TTRB *a_pTRB, TBOOL a_bIsGUI)
AMaterialLibrary * CreateLibraryFromAsset(const TCHAR *a_szFilePath, Toshi::TTRB *a_pTRB)
ATexture * FindTexture(const TCHAR *a_szTextureName, AMaterialLibrary **a_ppMaterialLibrary, TINT *a_pTextureIndex)
void DestroyLibrary(AMaterialLibrary *a_pLibrary)
AMaterialLibrary * CreateLibraryFromTRB(Toshi::TTRB *a_pTRB, const TCHAR *a_szFilePath)
static ALocaleManager * Interface()
const TCHAR * GetCurrentLanguageName()