OpenBarnyard
 
Loading...
Searching...
No Matches
TFileManager.cpp
Go to the documentation of this file.
1
11
12#include "ToshiPCH.h"
13#include "TFile.h"
14#include "Thread/T2MutexLock.h"
15
16//-----------------------------------------------------------------------------
17// Enables memory debugging.
18// Note: Should be the last include!
19//-----------------------------------------------------------------------------
20#include <Core/TMemoryDebugOn.h>
21
23
25 : m_WorkingDirectory( "/" ), m_ValidatedCount( 0 )
26{
27 InvalidateSystemPath();
28}
29
34
35// $Barnyard: FUNCTION 006d9230
37{
38 {
39 auto pFileSystem = TFileManager::FindFileSystem( "mem" );
40 if ( pFileSystem ) delete pFileSystem;
41 }
42
43 {
44 auto pFileSystem = TFileManager::FindFileSystem( "null" );
45 if ( pFileSystem ) delete pFileSystem;
46 }
47
48 {
49 auto pFileSystem = TFileManager::FindFileSystem( "abs" );
50 if ( pFileSystem ) delete pFileSystem;
51 }
52
53 {
54 auto pFileSystem = TFileManager::FindFileSystem( "local" );
55 if ( pFileSystem ) delete pFileSystem;
56 }
57
58 DestroyCommon();
59}
60
61// $Barnyard: FUNCTION 006be290
63{
64 TASSERT( TFileManager::FindFileSystem( a_pFileSystem->GetName() ) == TNULL );
65 TASSERT( !a_pFileSystem->IsLinked() );
66
67 m_Invalidated.InsertTail( a_pFileSystem );
68 InvalidateSystemPath();
69}
70
71// $Barnyard: FUNCTION 006be260
73{
74 TFileSystem* pFileSystem = TFileManager::FindFileSystem( m_Validated, name );
75
76 if ( pFileSystem == TNULL )
77 {
78 pFileSystem = TFileManager::FindFileSystem( m_Invalidated, name );
79 }
80
81 return pFileSystem;
82}
83
84// $Barnyard: FUNCTION 006be200
86{
87 T2_FOREACH( a_rList, pFileSystem )
88 {
89 if ( pFileSystem->GetName() == name )
90 {
91 return pFileSystem;
92 }
93 }
94
95 return TNULL;
96}
97
98// $Barnyard: FUNCTION 006be4b0
100{
101 TASSERT( a_sName.Length() > 0, "Name can't be empty" );
102
103 ValidateSystemPath();
104 TINT pos = a_sName.Find( ':', 0 );
105
106 if ( pos >= 0 )
107 {
108 TString8 fsName;
109 TString8 fileName;
110
111 fsName.Copy( a_sName, pos );
112 fileName.Copy( a_sName.GetString( pos + 1 ) );
113
114 TFileSystem* pFileSystem = FindFileSystem( fsName );
115
116 if ( pFileSystem != TNULL )
117 {
118 return pFileSystem->CreateFile( fileName, flags );
119 }
120 }
121
122 T2_FOREACH( m_Validated, pFileSystem )
123 {
124 TFile* pFile = pFileSystem->CreateFile( a_sName, flags );
125
126 if ( pFile != TNULL )
127 {
128 return pFile;
129 }
130 }
131
132 return TNULL;
133}
134
135// $Barnyard: FUNCTION 006be5c0
136TBOOL TFileManager::GetFileInfo( const TString8& a_strPath, TNativeFileInfo& a_rNativeInfo )
137{
138 TASSERT( a_strPath.Length() > 0, "Path can't be empty" );
139
140 ValidateSystemPath();
141 TINT pos = a_strPath.Find( ':', 0 );
142
143 if ( pos >= 0 )
144 {
145 TString8 fsName;
146 TString8 fileName;
147
148 fsName.Copy( a_strPath, pos );
149 fileName.Copy( a_strPath.GetString( pos + 1 ) );
150
151 TFileSystem* pFileSystem = FindFileSystem( fsName );
152
153 if ( pFileSystem != TNULL )
154 {
155 return pFileSystem->ToNative( fileName, a_rNativeInfo );
156 }
157 }
158
159 // If there are few validated file systems or even zero, create a file to know what filesystem we should use
160 if ( m_ValidatedCount != 1 )
161 {
162 TFile* pFile = CreateFile( a_strPath, TFILEMODE_READ | TFILEMODE_NOBUFFER );
163
164 if ( pFile != TNULL )
165 {
166 TFileSystem* pFileSystem = pFile->GetFileSystem();
167 pFileSystem->DestroyFile( pFile );
168
169 return pFileSystem->ToNative( a_strPath, a_rNativeInfo );
170 }
171
172 return TFALSE;
173 }
174
175 TFileSystem* pFileSystem = m_Validated.Begin();
176 return pFileSystem->ToNative( a_strPath, a_rNativeInfo );
177}
178
179// $Barnyard: FUNCTION 006be370
180void TFileManager::ValidateSystemPath()
181{
182 if ( !m_IsValidated )
183 {
184 // Move all file systems to the invalidated list
185 for ( auto pFS = m_Validated.Tail(); pFS != m_Validated.End(); pFS = m_Validated.Tail() )
186 {
187 pFS->Remove();
188 m_Invalidated.InsertHead( pFS );
189 }
190
191 m_ValidatedCount = 0;
192
193 TString8 fsName;
194 TSysPathIter pathIter( m_SysPath );
195
196 TBOOL hasPath = pathIter.First( fsName );
197
198 while ( hasPath )
199 {
200 TFileSystem* pFS = FindFileSystem( m_Invalidated, fsName );
201
202 if ( pFS != TNULL )
203 {
204 pFS->Remove();
205 m_Validated.InsertTail( pFS );
206 m_ValidatedCount += 1;
207 }
208
209 hasPath = pathIter.Next( fsName );
210 }
211
212 m_IsValidated = TTRUE;
213 }
214}
215
217 : m_String( str ), m_Position( -1 )
218{
219}
220
222 : m_String( other.m_String ), m_Position( other.m_Position )
223{
224}
225
227{
228 if ( m_String.Length() > 0 )
229 {
230 m_Position = m_String.Find( ';', 0 );
231 path.Copy( m_String, m_Position );
232
233 return TTRUE;
234 }
235 else
236 {
237 m_Position = -1;
238 return TFALSE;
239 }
240}
241
243{
244 if ( m_Position >= 0 )
245 {
246 TINT strStart = m_Position + 1;
247 m_Position = m_String.Find( ';', strStart );
248
249 path.Copy(
250 m_String.GetString( strStart ),
251 ( m_Position >= 0 ) ? ( m_Position - strStart ) : -1
252 );
253
254 return TTRUE;
255 }
256 else
257 {
258 return TFALSE;
259 }
260}
261
262// $Barnyard: FUNCTION 006be1c0
263TString8 TFileManager::MakeAbsolutePath( const TString8& a_rcRelativePath ) const
264{
265 return TFile::ConcatPath( m_WorkingDirectory, a_rcRelativePath );
266}
267
268// $Barnyard: FUNCTION 006be140
269void TFileManager::CreateCommon()
270{
273}
274
275// $Barnyard: FUNCTION 006be320
276void TFileManager::DestroyCommon()
277{
278 ms_oMutex.Destroy();
280}
281
Core file system interface for the Toshi engine.
TUINT16 TFILEMODE
Definition TFile.h:35
@ TFILEMODE_READ
Definition TFile.h:38
@ TFILEMODE_NOBUFFER
Definition TFile.h:42
#define TASSERT(X,...)
Definition Defines.h:138
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
#define T2_FOREACH(vecName, iteratorName)
Definition T2Iterator.h:4
#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
virtual void DestroyFile(TFile *a_pFile)=0
Destroys specified file.
virtual TBOOL ToNative(const TString8 &a_rcPath, TNativeFileInfo &a_rOutInfo)
Fetches info about the path and returns internal path.
const TString8 & GetName() const
Definition TFile.h:116
virtual TFile * CreateFile(const TString8 &a_rcFileName, TFILEMODE a_eFileMode)=0
Creates file.
Definition TFile.h:128
static TString8 ConcatPath(const TString8 &a_rcPath1, const TString8 &a_rcPath2)
Definition TFile.cpp:48
TFileSystem * GetFileSystem() const
Definition TFile.h:216
static TMutex ms_oMutex
Definition TFile.h:253
TFileSystem * FindFileSystem(const TString8 &name)
TString8 MakeAbsolutePath(const TString8 &a_rcRelativePath) const
TFile * CreateFile(const TString8 &a_strName, TFILEMODE flags)
void MountFileSystem(TFileSystem *a_pFileSystem)
TBOOL GetFileInfo(const TString8 &a_strPath, TNativeFileInfo &a_rNativeInfo)
TBOOL Next(TString8 &path)
TSysPathIter(const TString8 &str)
TBOOL First(TString8 &path)
TBOOL Create()
void InsertHead(TNode *a_pNode)
Definition TDList.h:52
TBOOL IsLinked() const
Definition TDList.h:38
T * Tail()
Definition TDList.h:177
Iterator End()
Definition TDList.h:179
static TFORCEINLINE TFileManager * CreateSingleton(Args &&... args)
Definition TSingleton.h:17
static TFORCEINLINE TFileManager * GetSingletonSafe()
Definition TSingleton.h:37
TINT Find(TCHAR character, TINT pos=0) const
Definition TString8.cpp:107
TINT Length() const
Definition TString8.h:93
void Copy(const TString8 &src, TINT size=-1)
Definition TString8.h:32
const TCHAR * GetString(TINT a_iIndex=0) const
Definition TString8.cpp:286