summaryrefslogtreecommitdiff
path: root/engine/DownloadListGenerator.cpp
blob: b5bc3174065e7b4c785afa746c122f4c0955405b (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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================//

#include "mathlib/vector.h"
#include "DownloadListGenerator.h"
#include "filesystem.h"
#include "filesystem_engine.h"
#include "sys.h"
#include "cmd.h"
#include "common.h"
#include "quakedef.h"
#include "vengineserver_impl.h"
#include "tier1/strtools.h"
#include "tier0/icommandline.h"
#include "checksum_engine.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <tier0/dbg.h>

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

CDownloadListGenerator g_DownloadListGenerator;
CDownloadListGenerator &DownloadListGenerator()
{
	return g_DownloadListGenerator;
}

ConVar	sv_logdownloadlist( "sv_logdownloadlist", IsX360() ? "0" : "1" );

extern int GetSvPureMode();

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CDownloadListGenerator::CDownloadListGenerator()
	: m_AlreadyWrittenFileNames( 0, 0, true )
{
	m_hReslistFile = FILESYSTEM_INVALID_HANDLE;
	m_pStringTable = NULL;
	m_mapName[0] = 0;
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CDownloadListGenerator::SetStringTable( INetworkStringTable *pStringTable )
{
	if ( IsX360() )
	{
		// not supporting
		return;
	}

	m_pStringTable = pStringTable;

	// reset the duplication list
	m_AlreadyWrittenFileNames.RemoveAll();

	// add in the bsp file to the list, and its node graph and nav mesh
	char path[_MAX_PATH];
	Q_snprintf(path, sizeof(path), "maps\\%s.bsp", m_mapName);
	OnResourcePrecached(path);

	bool useNodeGraph = true;
	KeyValues *modinfo = new KeyValues("ModInfo");
	if ( modinfo->LoadFromFile( g_pFileSystem, "gameinfo.txt" ) )
	{
		useNodeGraph = modinfo->GetInt( "nodegraph", 1 ) != 0;
	}
	modinfo->deleteThis();

	if ( useNodeGraph )
	{
		Q_snprintf(path, sizeof(path), "maps\\graphs\\%s.ain", m_mapName);
		OnResourcePrecached(path);
	}

	Q_snprintf(path, sizeof(path), "maps\\%s.nav", m_mapName);
	OnResourcePrecached(path);

	char resfilename[MAX_OSPATH];
	KeyValues *resfilekeys = new KeyValues( "resourefiles" );

	Q_snprintf( resfilename, sizeof( resfilename), "maps/%s.res", m_mapName );

	if ( resfilekeys->LoadFromFile( g_pFileSystem, resfilename, "GAME" ) )
	{
		KeyValues *entry = resfilekeys->GetFirstSubKey();
		while ( entry )
		{
			OnResourcePrecached( entry->GetName() );
			entry = entry->GetNextKey();
		}
	}
	resfilekeys->deleteThis();
}

//-----------------------------------------------------------------------------
// Purpose: call to mark level load/end
//-----------------------------------------------------------------------------
void CDownloadListGenerator::OnLevelLoadStart(const char *levelName)
{
	if ( IsX360() )
	{
		// not supporting
		return;
	}

	// close the previous level reslist, if any
	if (m_hReslistFile != FILESYSTEM_INVALID_HANDLE)
	{
		g_pFileSystem->Close(m_hReslistFile);
		m_hReslistFile = FILESYSTEM_INVALID_HANDLE;
	}

	// reset the duplication list
	m_AlreadyWrittenFileNames.RemoveAll();

	if ( sv_logdownloadlist.GetBool() )
	{
		// open the new level reslist
		char path[MAX_OSPATH];
		g_pFileSystem->CreateDirHierarchy( "DownloadLists", "MOD" );
		Q_snprintf(path, sizeof(path), "DownloadLists/%s.lst", levelName);
		m_hReslistFile = g_pFileSystem->Open(path, "wt", "GAME");
	}

	// add a slash to the end of com_gamedir, so we can only deal with files for this mod
	Q_snprintf( m_gameDir, sizeof(m_gameDir), "%s/", com_gamedir );
	Q_FixSlashes( m_gameDir );

	// save off the map name
	Q_snprintf( m_mapName, sizeof( m_mapName ), "%s", levelName );
}

//-----------------------------------------------------------------------------
// Purpose: call to mark level load/end
//-----------------------------------------------------------------------------
void CDownloadListGenerator::OnLevelLoadEnd()
{
	if ( IsX360() )
	{
		// not supporting
		return;
	}

	if ( m_hReslistFile != FILESYSTEM_INVALID_HANDLE )
	{
		g_pFileSystem->Close(m_hReslistFile);
		m_hReslistFile = FILESYSTEM_INVALID_HANDLE;
	}
	m_pStringTable = NULL;
}

//-----------------------------------------------------------------------------
// Purpose: logs and handles mdl files being precaches
//-----------------------------------------------------------------------------
void CDownloadListGenerator::OnModelPrecached(const char *relativePathFileName)
{
	if ( IsX360() )
	{
		// not supporting
		return;
	}

	if (Q_strstr(relativePathFileName, ".vmt"))
	{
		// it's a materials file, make sure that it starts in the materials directory, and we get the .vtf
		char file[_MAX_PATH];

		if (!Q_strnicmp(relativePathFileName, "materials", strlen("materials")))
		{
			Q_strncpy(file, relativePathFileName, sizeof(file));
		}
		else
		{
			// prepend the materials directory
			Q_snprintf(file, sizeof(file), "materials\\%s", relativePathFileName);
		}

		OnResourcePrecached(file);

		// get the matching vtf file
		char *ext = Q_strstr(file, ".vmt");
		if (ext)
		{
			Q_strncpy(ext, ".vtf", 5);
			OnResourcePrecached(file);
		}
	}
	else
	{
		OnResourcePrecached(relativePathFileName);
	}
}

//-----------------------------------------------------------------------------
// Purpose: logs sound file access
//-----------------------------------------------------------------------------
void CDownloadListGenerator::OnSoundPrecached(const char *relativePathFileName)
{
	if ( IsX360() )
	{
		// not supporting
		return;
	}

	// skip any special characters
	if (!V_isalnum(relativePathFileName[0]))
	{
		++relativePathFileName;
	}

	// prepend the sound/ directory if necessary
	char file[_MAX_PATH];
	if (!Q_strnicmp(relativePathFileName, "sound", strlen("sound")))
	{
		Q_strncpy(file, relativePathFileName, sizeof(file));
	}
	else
	{
		// prepend the materials directory
		Q_snprintf(file, sizeof(file), "sound\\%s", relativePathFileName);
	}

	OnResourcePrecached(file);
}

//-----------------------------------------------------------------------------
// Purpose: logs the precache as a file access
//-----------------------------------------------------------------------------
void CDownloadListGenerator::OnResourcePrecached(const char *relativePathFileName)
{
	if ( IsX360() )
	{
		// not supporting
		return;
	}

	// ignore empty string
	if (relativePathFileName[0] == 0)
	{
		return;
	}

	// ignore files that start with '*' since they signify special models
	if (relativePathFileName[0] == '*')
	{
		return;
	}

	char fullPath[_MAX_PATH];
	if (g_pFileSystem->GetLocalPath(relativePathFileName, fullPath, sizeof(fullPath)))
	{
		OnResourcePrecachedFullPath( fullPath, relativePathFileName);
	}
}

//-----------------------------------------------------------------------------
// Purpose: marks a precached file as needing a specific CRC on the client
//-----------------------------------------------------------------------------
void CDownloadListGenerator::ForceSimpleMaterial( const char *relativePathFileName )
{
	if ( IsX360() )
	{
		// not supporting
		return;
	}

	if ( !m_pStringTable )
		return;

	if ( !Q_stristr(relativePathFileName, ".vmt") && !Q_stristr(relativePathFileName, ".vtf"))
	{
		DevMsg( "Tried to enforce simple material on %s\n", relativePathFileName );
		return;
	}

	// it's a materials file, make sure that it starts in the materials directory, and we get the .vtf
	char szFixedFilename[_MAX_PATH];
	if (!Q_strnicmp(relativePathFileName, "materials", strlen("materials")))
	{
		V_strcpy_safe( szFixedFilename, relativePathFileName );
	}
	else
	{
		// prepend the materials directory
		V_sprintf_safe( szFixedFilename, "materials\\%s", relativePathFileName );
	}
	V_FixSlashes( szFixedFilename );
	if ( !g_pFullFileSystem->FileExists( szFixedFilename, "game" ) )
	{
		DevMsg( "Cannot force simple material on %s; file not found\n", szFixedFilename );
		return;
	}

	ExactFileUserData userData;
	userData.consistencyType = CONSISTENCY_SIMPLE_MATERIAL;
	userData.crc = 0;

	// Only set consistency data if pure, otherwise just create entry in download list
	if ( GetSvPureMode() < 0 )
	{
		m_pStringTable->AddString( true, szFixedFilename );
	}
	else
	{
		int index = m_pStringTable->FindStringIndex( szFixedFilename );
		if ( index != INVALID_STRING_INDEX )
		{
			m_pStringTable->SetStringUserData( index, sizeof( ExactFileUserData ), &userData );
		}
		else
		{
			m_pStringTable->AddString( true, szFixedFilename, sizeof( ExactFileUserData ), &userData );
		}
	}
}


//-----------------------------------------------------------------------------
// Purpose: marks a precached model as having a maximum size on the client
//-----------------------------------------------------------------------------
void CDownloadListGenerator::ForceModelBounds( const char *relativePathFileName, const Vector &mins, const Vector &maxs )
{
	if ( IsX360() )
	{
		// not supporting
		return;
	}

	if ( !m_pStringTable )
		return;

	if ( !relativePathFileName )
		relativePathFileName = "";

	if (!Q_stristr(relativePathFileName, ".mdl"))
	{
		DevMsg( "Warning - trying to enforce model bounds on %s\n", relativePathFileName );
		return;
	}

	char relativeFileName[_MAX_PATH];
	Q_strncpy( relativeFileName, relativePathFileName, sizeof( relativeFileName ) );
	Q_FixSlashes( relativeFileName );

	// Only set consistency data if pure, otherwise just create entry in download list
	if ( GetSvPureMode() < 0 )
	{
		m_pStringTable->AddString( true, relativePathFileName );
	}
	else
	{
		ModelBoundsUserData userData;
		userData.consistencyType = CONSISTENCY_BOUNDS;
		userData.mins = mins;
		userData.maxs = maxs;

		int index = m_pStringTable->FindStringIndex( relativeFileName );
		if ( index != INVALID_STRING_INDEX )
		{
			m_pStringTable->SetStringUserData( index, sizeof( ModelBoundsUserData ), &userData );
		}
		else
		{
			m_pStringTable->AddString( true, relativeFileName, sizeof( ModelBoundsUserData ), &userData );
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: Logs out file access to a file
//-----------------------------------------------------------------------------
void CDownloadListGenerator::OnResourcePrecachedFullPath( char *fullPathFileName, const char *relativeFileName )
{
	if ( IsX360() )
	{
		// not supporting
		return;
	}

	Q_FixSlashes( fullPathFileName );

	if ( !g_pFileSystem->FileExists( fullPathFileName ) )
	{
		return;	// don't allow files for download the server doesn't have
	}

	if ( Q_strncasecmp( m_gameDir, fullPathFileName, Q_strlen( m_gameDir ) ) )
	{
		return;	// the game dir must be part of the full name
	}

	// make sure the filename hasn't already been written
	UtlSymId_t filename = m_AlreadyWrittenFileNames.Find( fullPathFileName );
	if ( filename != UTL_INVAL_SYMBOL )
	{
		return;
	}

	// record in list, so we don't write it again
	m_AlreadyWrittenFileNames.AddString( fullPathFileName );

	// add extras for mdl's
	if (Q_strstr(relativeFileName, ".mdl"))
	{
		// it's a model, get it's other files as well
		char file[_MAX_PATH];
		Q_strncpy(file, relativeFileName, sizeof(file) - 10);
		char *ext = Q_strstr(file, ".mdl");

		Q_strncpy(ext, ".vvd", 10);
		OnResourcePrecached(file);

		Q_strncpy(ext, ".ani", 10);
		OnResourcePrecached(file);

		Q_strncpy(ext, ".dx80.vtx", 10);
		OnResourcePrecached(file);

		Q_strncpy(ext, ".dx90.vtx", 10);
		OnResourcePrecached(file);

		Q_strncpy(ext, ".sw.vtx", 10);
		OnResourcePrecached(file);

		Q_strncpy(ext, ".phy", 10);
		OnResourcePrecached(file);

		Q_strncpy(ext, ".jpg", 10);
		OnResourcePrecached(file);
	}

	FileHandle_t handle = m_hReslistFile;
	if ( handle != FILESYSTEM_INVALID_HANDLE )
	{
		g_pFileSystem->Write("\"", 1, handle);
		g_pFileSystem->Write( relativeFileName, Q_strlen(relativeFileName), handle );
		g_pFileSystem->Write("\"\n", 2, handle);
	}
	if ( m_pStringTable )
	{
		m_pStringTable->AddString( true, relativeFileName );
	}
}