summaryrefslogtreecommitdiff
path: root/utils/vmpi/vmpi_filesystem.cpp
blob: 815f918426119f50d2f152cb6f1422f6912f7a4b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//
//=============================================================================//

#include "vmpi_filesystem_internal.h"
#include "tier1/utlbuffer.h"


bool g_bDisableFileAccess = false;


CBaseVMPIFileSystem *g_pBaseVMPIFileSystem = NULL;
IFileSystem *g_pOriginalPassThruFileSystem = NULL;

void* GetVMPIFileSystem()
{
	return (IBaseFileSystem*)g_pBaseVMPIFileSystem;
}

EXPOSE_INTERFACE_FN( GetVMPIFileSystem, IBaseFileSystem, BASEFILESYSTEM_INTERFACE_VERSION )


IFileSystem* VMPI_FileSystem_Init( int maxMemoryUsage, IFileSystem *pPassThru )
{
	Assert( g_bUseMPI );
	Assert( !g_pBaseVMPIFileSystem );
	g_pOriginalPassThruFileSystem = pPassThru;

	if ( g_bMPIMaster )
	{
		extern CBaseVMPIFileSystem* CreateMasterVMPIFileSystem( int maxMemoryUsage, IFileSystem *pPassThru );
		CreateMasterVMPIFileSystem( maxMemoryUsage, pPassThru );
	}
	else
	{
		extern CBaseVMPIFileSystem* CreateWorkerVMPIFileSystem();
		CreateWorkerVMPIFileSystem();
	}
	
	// The Create function should have set this. Normally, we'd set g_pBaseVMPIFileSystem right here, but 
	// the create functions may want to receive some messages, in which case they need to set g_pBaseVMPIFileSystem
	// so the packets get routed appropriately.
	Assert( g_pBaseVMPIFileSystem );
	return g_pBaseVMPIFileSystem;
}


IFileSystem* VMPI_FileSystem_Term()
{
	if ( g_pBaseVMPIFileSystem )
	{
		g_pBaseVMPIFileSystem->Release();
		g_pBaseVMPIFileSystem = NULL;

		if ( g_iVMPIVerboseLevel >= 1 )
		{
			if ( g_bMPIMaster )
				Msg( "Multicast send: %dk\n", (g_nMulticastBytesSent + 511) / 1024 );
			else
				Msg( "Multicast recv: %dk\n", (g_nMulticastBytesReceived + 511) / 1024 );
		}
	}
	
	IFileSystem *pRet = g_pOriginalPassThruFileSystem;
	g_pOriginalPassThruFileSystem = NULL;
	return pRet;
}


void VMPI_FileSystem_DisableFileAccess()
{
	g_bDisableFileAccess = true;
}


CreateInterfaceFn VMPI_FileSystem_GetFactory()
{
	return Sys_GetFactoryThis();
}


void VMPI_FileSystem_CreateVirtualFile( const char *pFilename, const void *pData, unsigned long fileLength )
{
	g_pBaseVMPIFileSystem->CreateVirtualFile( pFilename, pData, fileLength );
}


// Register our packet ID.
bool FileSystemRecv( MessageBuffer *pBuf, int iSource, int iPacketID )
{
	if ( g_pBaseVMPIFileSystem )
		return g_pBaseVMPIFileSystem->HandleFileSystemPacket( pBuf, iSource, iPacketID );
	else
		return false;
}


CDispatchReg g_DispatchReg_FileSystem( VMPI_PACKETID_FILESYSTEM, FileSystemRecv );



// ------------------------------------------------------------------------------------------------------------------------ //
// CVMPIFile_Memory implementation.
// ------------------------------------------------------------------------------------------------------------------------ //

void CVMPIFile_Memory::Init( const char *pData, long len, char chMode /* = 'b' */ )
{
	m_pData = pData;
	m_DataLen = len;
	m_iCurPos = 0;
	m_chMode = chMode;
}

void CVMPIFile_Memory::Close()
{
	delete this;
}

void CVMPIFile_Memory::Seek( int pos, FileSystemSeek_t seekType )
{
	if ( seekType == FILESYSTEM_SEEK_HEAD )
		m_iCurPos = pos;
	else if ( seekType == FILESYSTEM_SEEK_CURRENT )
		m_iCurPos += pos;
	else
		m_iCurPos = m_DataLen - pos;
}

unsigned int CVMPIFile_Memory::Tell()
{
	return m_iCurPos; 
}

unsigned int CVMPIFile_Memory::Size() 
{
	return m_DataLen; 
}

void CVMPIFile_Memory::Flush()
{
}

int CVMPIFile_Memory::Read( void* pOutput, int size ) 
{ 
	Assert( m_iCurPos >= 0 );
	int nToRead = min( (int)(m_DataLen - m_iCurPos), size );
	
	if ( m_chMode != 't' )
	{
		memcpy( pOutput, &m_pData[m_iCurPos], nToRead );
		m_iCurPos += nToRead;

		return nToRead;
	}
	else
	{
		int iRead = 0;
		const char *pData = m_pData + m_iCurPos;
		int len = m_DataLen - m_iCurPos;
		
		// Perform crlf translation
		while ( const char *crlf = ( const char * ) memchr( pData, '\r', len ) )
		{
			int canCopy = min( size, crlf - pData );
			memcpy( pOutput, pData, canCopy );
			
			m_iCurPos += canCopy;
			pData += canCopy;
			len -= canCopy;
			
			iRead += canCopy;
			( char * & ) pOutput += canCopy;
			size -= canCopy;

			if ( size && len )
			{
				if ( ( len > 1 ) && ( pData[1] == '\n' ) )
				{
					++ m_iCurPos;
					++ pData;
					-- len;
				}

				* ( char * & ) pOutput = *pData;
				
				++ m_iCurPos;
				++ pData;
				-- len;

				++ iRead;
				++ ( char * & ) pOutput;
				-- size;
			}
			else
				break;
		}
		
		if ( size && len )
		{
			// No crlf characters left
			int canCopy = min( size, len );
			memcpy( pOutput, pData, canCopy );

			m_iCurPos += canCopy;
			pData += canCopy;
			len -= canCopy;

			iRead += canCopy;
			( char * & ) pOutput += canCopy;
			size -= canCopy;
		}

		return iRead;
	}
}

int CVMPIFile_Memory::Write( void const* pInput, int size )
{
	Assert( false ); return 0; 
}


// ------------------------------------------------------------------------------------------------------------------------ //
// CBaseVMPIFileSystem implementation.
// ------------------------------------------------------------------------------------------------------------------------ //

CBaseVMPIFileSystem::~CBaseVMPIFileSystem()
{
}


void CBaseVMPIFileSystem::Release()
{
	delete this;
}


void CBaseVMPIFileSystem::Close( FileHandle_t file )
{
	if ( file )
		((IVMPIFile*)file)->Close();
}

int CBaseVMPIFileSystem::Read( void* pOutput, int size, FileHandle_t file )
{
	return ((IVMPIFile*)file)->Read( pOutput, size );
}

int CBaseVMPIFileSystem::Write( void const* pInput, int size, FileHandle_t file )
{
	return ((IVMPIFile*)file)->Write( pInput, size );
}

void CBaseVMPIFileSystem::Seek( FileHandle_t file, int pos, FileSystemSeek_t seekType )
{
	((IVMPIFile*)file)->Seek( pos, seekType );
}

unsigned int CBaseVMPIFileSystem::Tell( FileHandle_t file )
{
	return ((IVMPIFile*)file)->Tell();
}

unsigned int CBaseVMPIFileSystem::Size( FileHandle_t file )
{
	return ((IVMPIFile*)file)->Size();
}

unsigned int CBaseVMPIFileSystem::Size( const char *pFilename, const char *pathID = 0 )
{
	FileHandle_t hFile = Open( pFilename, "rb", NULL );
	if ( hFile == FILESYSTEM_INVALID_HANDLE )
	{
		return 0;
	}
	else
	{
		unsigned int ret = Size( hFile );
		Close( hFile );
		return ret;
	}
}

bool CBaseVMPIFileSystem::FileExists( const char *pFileName, const char *pPathID )
{
	FileHandle_t hFile = Open( pFileName, "rb", NULL );
	if ( hFile )
	{
		Close( hFile );
		return true;
	}
	else
	{
		return false;
	}
}

void CBaseVMPIFileSystem::Flush( FileHandle_t file )
{
	((IVMPIFile*)file)->Flush();
}	

bool CBaseVMPIFileSystem::Precache( const char* pFileName, const char *pPathID )
{
	return false;
} 


//-----------------------------------------------------------------------------
// NOTE: This is an exact copy of code in BaseFileSystem.cpp which
// has to be here because they want to call
// the implementation of Open/Size/Read/Write in CBaseVMPIFileSystem
//-----------------------------------------------------------------------------
bool CBaseVMPIFileSystem::ReadFile( const char *pFileName, const char *pPath, CUtlBuffer &buf, int nMaxBytes, int nStartingByte, FSAllocFunc_t pfnAlloc )
{
	const char *pReadFlags = "rb";
	if ( buf.IsText() && !buf.ContainsCRLF() )
	{
		pReadFlags = "rt";
	}

	FileHandle_t fp = Open( pFileName, buf.IsText() ? "rt" : "rb", pPath );
	if ( !fp )
		return false;

	int nBytesToRead = Size( fp );
	if ( nMaxBytes > 0 )
	{
		nBytesToRead = min( nMaxBytes, nBytesToRead );
	}
	buf.EnsureCapacity( nBytesToRead + buf.TellPut() );

	if ( nStartingByte != 0 )
	{
		Seek( fp, nStartingByte, FILESYSTEM_SEEK_HEAD );
	}

	int nBytesRead = Read( buf.PeekPut(), nBytesToRead, fp );
	buf.SeekPut( CUtlBuffer::SEEK_CURRENT, nBytesRead );

	Close( fp );
	return (nBytesRead != 0);
}

bool CBaseVMPIFileSystem::WriteFile( const char *pFileName, const char *pPath, CUtlBuffer &buf )
{
	const char *pWriteFlags = "wb";
	if ( buf.IsText() && !buf.ContainsCRLF() )
	{
		pWriteFlags = "wt";
	}

	FileHandle_t fp = Open( pFileName, buf.IsText() ? "wt" : "wb", pPath );
	if ( !fp )
		return false;

	int nBytesWritten = Write( buf.Base(), buf.TellPut(), fp );

	Close( fp );
	return (nBytesWritten != 0);
}