summaryrefslogtreecommitdiff
path: root/utils/xbox/MakeGameData/MakeMaps.cpp
blob: 618e5f68ff8ae65f7a0c9a0225f606ee3f753852 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: .360 Creation for all studiomdl generated files (mdl, vvd, vtx, ani, phy)
//
//=====================================================================================//

#include "MakeGameData.h"
#include "filesystem.h"
#include "../../common/bsplib.h"
#include "ibsppack.h"
#include "vtf/vtf.h"
#include "../../game/server/ai_hull.h"
#include "zip_utils.h"

#define AINET_VERSION_NUMBER	37
#define MAX_NODES				1500

bool ReadBSPHeader( const char *pFilename, dheader_t *pHeader )
{
	V_memset( pHeader, 0, sizeof( dheader_t ) );

	int handle = _open( pFilename, _O_RDONLY|_O_BINARY );
	if ( handle == -1 )
	{
		return false;
	}

	_read( handle, pHeader, sizeof( dheader_t ) );
	close( handle );

	return true;
}

//-----------------------------------------------------------------------------
// Run possible lod culling fixup
//-----------------------------------------------------------------------------
bool ConvertVHV( const char *pVhvFilename, const char *pModelName, CUtlBuffer &sourceBuffer, CUtlBuffer &targetBuffer )
{
	// find strip info from model
	char vsiFilename[MAX_PATH];
	V_strncpy( vsiFilename, pModelName, sizeof( vsiFilename ) );
	V_SetExtension( vsiFilename, ".vsi", sizeof( vsiFilename ) );

	CUtlBuffer vsiBuffer;
	if ( !g_pFullFileSystem->ReadFile( vsiFilename, NULL, vsiBuffer ) )
	{
		// cannot convert bsp's without model converions
		Msg( "Error! Missing expected model conversion file '%s'. Cannot perform VHV fixup.\n", vsiFilename );
		return false;
	}

	IMdlStripInfo *pMdlStripInfo = NULL;
	if ( !mdllib->CreateNewStripInfo( &pMdlStripInfo ) )
	{
		Msg( "Error! Failed to allocate strip info object\n" );
		return false;
	}

	if ( !pMdlStripInfo->UnSerialize( vsiBuffer ) )
	{
		Msg( "Error! Failed to unserialize strip info object '%s'\n", vsiFilename );
		pMdlStripInfo->DeleteThis();
		return false;
	}

	long originalChecksum = 0;
	long newChecksum = 0;
	if ( !pMdlStripInfo->GetCheckSum( &originalChecksum, &newChecksum ) )
	{
		Msg( "Error! Failed to get checksums from '%s'\n", vsiFilename );
		pMdlStripInfo->DeleteThis();
		return false;
	}
	
	HardwareVerts::FileHeader_t *pVHVhdr = (HardwareVerts::FileHeader_t*)sourceBuffer.Base();
	if ( pVHVhdr->m_nChecksum != originalChecksum )
	{
		// vhv file should have matching original checksums
		Msg( "Error! Mismatched checksums from '%s' and '%s'\n", vsiFilename, pVhvFilename );
		pMdlStripInfo->DeleteThis();
		return false;
	}

	targetBuffer.EnsureCapacity( sourceBuffer.TellMaxPut() );
	targetBuffer.Put( sourceBuffer.Base(), sourceBuffer.TellMaxPut() );
	if ( !pMdlStripInfo->StripHardwareVertsBuffer( targetBuffer ) )
	{
		pMdlStripInfo->DeleteThis();
		return false;
	}

	// success
	pMdlStripInfo->DeleteThis();
	return true;
}

//-----------------------------------------------------------------------------
// Purpose: Generate .360 bsp
//-----------------------------------------------------------------------------
bool CreateTargetFile_BSP( const char *pSourceName, const char *pTargetName, bool bWriteToZip )
{
	CUtlBuffer targetBuffer;
	CUtlBuffer zipBuffer;
	CUtlBuffer fileBuffer;
	CUtlBuffer tempBuffer;
	char tempZipName[MAX_PATH];
	char tempSwapName[MAX_PATH];

	tempZipName[0] = '\0';
	tempSwapName[0] = '\0';
	void *pPakData = NULL;
	int pakSize = 0;
	CXZipTool *pNewXZip = NULL;

	if ( !g_bModPathIsValid )
	{
		Msg( "Indeterminate mod path, Cannot perform BSP conversion\n" );
		return false;
	}

	// Load bsppack.dll
	void *pBSPPack;
	CSysModule *pBSPModule;
	if ( !Sys_LoadInterface( "bsppack.dll", IBSPPACK_VERSION_STRING, &pBSPModule, &pBSPPack ) )
	{
		Msg( "Failed to load bsppack interface\n" );
		return false;
	}

	scriptlib->MakeTemporaryFilename( g_szModPath, tempSwapName, sizeof( tempSwapName ) );	

	// Swaps the bsp directly to disk
	bool bOK = ((IBSPPack*)pBSPPack)->SwapBSPFile( g_pFullFileSystem, pSourceName, tempSwapName, false, ConvertVTFTo360Format, ConvertVHV, CompressCallback );
	if ( !bOK )
	{
		goto cleanUp;
	}

	// get the pak file from the swapped bsp
	bOK = ((IBSPPack*)pBSPPack)->GetPakFileLump( g_pFullFileSystem, tempSwapName, &pPakData, &pakSize );
	if ( !bOK )
	{
		goto cleanUp;
	}

	// build an xzip version of the pak file
	if ( pPakData && pakSize )
	{
		// mount current pak file
		IZip *pOldZip = IZip::CreateZip( false, true );
		pOldZip->ParseFromBuffer( pPakData, pakSize );

		// start a new xzip version
		scriptlib->MakeTemporaryFilename( g_szModPath, tempZipName, sizeof( tempZipName ) );		

		pNewXZip = new CXZipTool;
		pNewXZip->Begin( tempZipName, XBOX_DVD_SECTORSIZE );

		// iterate each file in existing zip, add to new zip
		int zipIndex = -1;
		for ( ;; ) 
		{
			char filename[MAX_PATH];
			filename[0] = '\0';
			int fileSize = 0;
			zipIndex = pOldZip->GetNextFilename( zipIndex, filename, sizeof( filename ), fileSize );
			if ( zipIndex == -1 )
			{
				break;
			}

			fileBuffer.Purge();
			bOK = pOldZip->ReadFileFromZip( filename, false, fileBuffer );
			if ( !bOK )
			{
				goto cleanUp;
			}

			bOK = pNewXZip->AddBuffer( filename, fileBuffer, true );
			if ( !bOK )
			{
				goto cleanUp;
			}
		}

		IZip::ReleaseZip( pOldZip );
		pNewXZip->End();

		// read the new zip into memory
		bOK = scriptlib->ReadFileToBuffer( tempZipName, zipBuffer );
		if ( !bOK )
		{
			goto cleanUp;
		}

		// replace old pak lump with new zip
		bOK = ((IBSPPack*)pBSPPack)->SetPakFileLump( g_pFullFileSystem, tempSwapName, tempSwapName, zipBuffer.Base(), zipBuffer.TellMaxPut() );
		if ( !bOK )
		{
			goto cleanUp;
		}
	}

	bOK = scriptlib->ReadFileToBuffer( tempSwapName, targetBuffer );
	if ( !bOK )
	{
		goto cleanUp;
	}
 
	// never zip, always write local file
	bOK = WriteBufferToFile( pTargetName, targetBuffer, false, WRITE_TO_DISK_ALWAYS );

cleanUp:
	if ( tempZipName[0] )
	{
		_unlink( tempZipName );
	}
	if ( tempSwapName[0] )
	{
		_unlink( tempSwapName );
	}

	Sys_UnloadModule( pBSPModule );

	if ( pPakData )
	{
		free( pPakData );
	}
	
	delete pNewXZip;

	return bOK;
}


//-----------------------------------------------------------------------------
// Purpose: Generate .360 node graphs
//-----------------------------------------------------------------------------
bool CreateTargetFile_AIN( const char *pSourceName, const char *pTargetName, bool bWriteToZip )
{
	CUtlBuffer sourceBuf;
	if ( !scriptlib->ReadFileToBuffer( pSourceName, sourceBuf ) )
	{
		return false;
	}

	// the pc ain is tied to the pc bsp and should have been generated after the bsp
	char szBspName[MAX_PATH];
	char szBspPath[MAX_PATH];
	V_FileBase( pSourceName, szBspName, sizeof( szBspName ) );
	V_ExtractFilePath( pSourceName, szBspPath, sizeof( szBspPath ) );
	V_AppendSlash( szBspPath, sizeof( szBspPath ) );
	V_strncat( szBspPath, "..\\", sizeof( szBspPath ) );
	V_strncat( szBspPath, szBspName, sizeof( szBspPath ) );
	V_strncat( szBspPath, ".bsp", sizeof( szBspPath ) );
	if ( scriptlib->CompareFileTime( pSourceName, szBspPath ) < 0 )
	{
		// ain has a smaller filetime, thus older than bsp
		Msg( "%s: Need to regenerate PC nodegraph (stale)\n", pSourceName );
		if ( !g_bForce )
		{
			return false;
		}
	}

	// Check the version
	if ( sourceBuf.GetChar() == 'V' && sourceBuf.GetChar() == 'e' && sourceBuf.GetChar() == 'r' )
	{
		Msg( "%s: Need to regenerate PC nodegraph (bad format)\n", pSourceName );
		return false;
	}

	// reset
	sourceBuf.SeekGet( CUtlBuffer::SEEK_HEAD, 0 );

	// Version number
	int version = sourceBuf.GetInt();
	if ( version != AINET_VERSION_NUMBER )
	{
		Msg( "%s: Need to regenerate PC nodegraph (got version '%d', expected '%d')\n", pSourceName, version, AINET_VERSION_NUMBER );
		return false;
	}

	// check the map revision
	int mapVersion = sourceBuf.GetInt();
	dheader_t bspHeader;
	if ( ReadBSPHeader( szBspPath, &bspHeader ) )
	{
		if ( mapVersion != bspHeader.mapRevision )
		{
			Msg( "%s: Need to regenerate PC nodegraph (ai revision '%d' does not match bsp revision '%d')\n", pSourceName, mapVersion, bspHeader.mapRevision  );
			return false;
		}
	}
	else
	{
		Msg( "%s: Could not find expected bsp '%s'\n", pSourceName, szBspPath );
	}

	// Nodes
	int nodeCt = sourceBuf.GetInt();
	if ( nodeCt > MAX_NODES || nodeCt < 0 )
	{
		Msg( "%s: Need to regenerate PC nodegraph (corrupt)\n", pSourceName );
		return false;
	}

	CUtlBuffer targetBuf;
	targetBuf.ActivateByteSwapping( true );

	CByteswap swap;
	swap.ActivateByteSwapping( true );

	targetBuf.PutInt( version );
	targetBuf.PutInt( mapVersion );
	targetBuf.PutInt( nodeCt );

	int numFloats = NUM_HULLS + 4;
	for ( int node = 0; node < nodeCt; ++node )
	{
		targetBuf.EnsureCapacity( targetBuf.TellPut() + numFloats * sizeof( float ) );
		swap.SwapBufferToTargetEndian<float>( (float*)targetBuf.PeekPut(), (float*)sourceBuf.PeekGet(), numFloats );
		sourceBuf.SeekGet( CUtlBuffer::SEEK_CURRENT, numFloats * sizeof( float ) );
		targetBuf.SeekPut( CUtlBuffer::SEEK_CURRENT, numFloats * sizeof( float ) );

		targetBuf.PutChar( sourceBuf.GetChar() );

		// Align the remaining data
		targetBuf.SeekPut( CUtlBuffer::SEEK_CURRENT, 3 );

		targetBuf.PutUnsignedShort( sourceBuf.GetUnsignedShort() );
		targetBuf.PutShort( sourceBuf.GetShort() );
	}

	// Node links
	int totalNumLinks = sourceBuf.GetInt();
	targetBuf.PutInt( totalNumLinks );

	for ( int link = 0; link < totalNumLinks; ++link )
	{
		targetBuf.PutShort( sourceBuf.GetShort() );
		targetBuf.PutShort( sourceBuf.GetShort() );
		targetBuf.Put( sourceBuf.PeekGet(), NUM_HULLS );
		sourceBuf.SeekGet( CUtlBuffer::SEEK_CURRENT, NUM_HULLS );
	}

	// WC lookup table
	targetBuf.EnsureCapacity( targetBuf.TellPut() + nodeCt * sizeof( int ) );
	swap.SwapBufferToTargetEndian<int>( (int*)targetBuf.PeekPut(), (int*)sourceBuf.PeekGet(), nodeCt );
	targetBuf.SeekPut( CUtlBuffer::SEEK_CURRENT, nodeCt * sizeof( int ) );

	// Write the file out
	return WriteBufferToFile( pTargetName, targetBuf, bWriteToZip, WRITE_TO_DISK_ALWAYS );
}

bool GetDependants_BSP( const char *pBspName, CUtlVector< CUtlString > *pList )
{
	if ( !g_bModPathIsValid )
	{
		Msg( "Indeterminate mod path, Cannot perform BSP conversion\n" );
		return false;
	}

	// Load bsppack.dll
	void *pBSPPack;
	CSysModule *pBSPModule;
	if ( !Sys_LoadInterface( "bsppack.dll", IBSPPACK_VERSION_STRING, &pBSPModule, &pBSPPack ) )
	{
		Msg( "Failed to load bsppack interface\n" );
		return false;
	}

	// 360 builds a more complete reslist that includes bsp internal files
	// build full path to bsp file
	char szBspFilename[MAX_PATH];
	V_ComposeFileName( g_szGamePath, pBspName, szBspFilename, sizeof( szBspFilename ) );

	bool bOK = ((IBSPPack*)pBSPPack)->GetBSPDependants( g_pFullFileSystem, szBspFilename, pList );

	Sys_UnloadModule( pBSPModule );

	return bOK;
}