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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// The copyright to the contents herein is the property of Valve, L.L.C.
// The contents may be used and/or copied only with the written permission of
// Valve, L.L.C., or in accordance with the terms and conditions stipulated in
// the agreement/contract under which the contents have been supplied.
//
// $Header: $
// $NoKeywords: $
//
//=============================================================================
// Valve includes
#include "appframework/tier2app.h"
#include "filesystem.h"
#include "icommandline.h"
#include "tier2/p4helpers.h"
#include "p4lib/ip4.h"
#include "tier1/KeyValues.h"
#include "tier1/utlbuffer.h"
#include "bsplib.h"
#include "lumpfiles.h"
#include "filesystem_tools.h"
#include "cmdlib.h"
#ifdef _DEBUG
#include <windows.h>
#undef GetCurrentDirectory
#endif
//-----------------------------------------------------------------------------
// Standard spew functions
//-----------------------------------------------------------------------------
static SpewRetval_t SpewStdout( SpewType_t spewType, char const *pMsg )
{
if ( !pMsg )
return SPEW_CONTINUE;
#ifdef _DEBUG
OutputDebugString( pMsg );
#endif
printf( pMsg );
fflush( stdout );
return ( spewType == SPEW_ASSERT ) ? SPEW_DEBUGGER : SPEW_CONTINUE;
}
//-----------------------------------------------------------------------------
// The application object
//-----------------------------------------------------------------------------
class CMkEntityPatchApp : public CTier2SteamApp
{
typedef CTier2SteamApp BaseClass;
public:
// Methods of IApplication
virtual bool Create();
virtual bool PreInit( );
virtual int Main();
virtual void Destroy() {}
void PrintHelp( );
private:
};
DEFINE_CONSOLE_STEAM_APPLICATION_OBJECT( CMkEntityPatchApp );
//-----------------------------------------------------------------------------
// The application object
//-----------------------------------------------------------------------------
bool CMkEntityPatchApp::Create()
{
SpewOutputFunc( SpewStdout );
AppSystemInfo_t appSystems[] =
{
{ "p4lib.dll", P4_INTERFACE_VERSION },
{ "", "" } // Required to terminate the list
};
return AddSystems( appSystems );
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
bool CMkEntityPatchApp::PreInit( )
{
MathLib_Init( 2.2f, 2.2f, 0.0f, 2.0f, false, false, false, false );
if ( !BaseClass::PreInit() )
return false;
if ( !g_pFullFileSystem )
{
Error( "// ERROR: sfmgen is missing a required interface!\n" );
return false;
}
// Add paths...
if ( !SetupSearchPaths( NULL, false, true ) )
return false;
return true;
}
//-----------------------------------------------------------------------------
// Print help
//-----------------------------------------------------------------------------
void CMkEntityPatchApp::PrintHelp( )
{
Msg( "Usage: mkentitypatch [-nop4] [-vproject <path to gameinfo.txt>] <in .bsp file>\n" );
Msg( "\t-nop4\t: [Optional] Disables auto perforce checkout/add.\n" );
Msg( "\t-vproject\t: [Optional] Specifies path to a gameinfo.txt file (which mod to build for).\n" );
Msg( "\t Source .BSP file whose entity lump you wish to patch.\n" );
}
//-----------------------------------------------------------------------------
// Computes a full directory
//-----------------------------------------------------------------------------
static void ComputeFullPath( const char *pRelativeDir, char *pFullPath, int nBufLen )
{
if ( !Q_IsAbsolutePath( pRelativeDir ) )
{
char pDir[MAX_PATH];
if ( g_pFullFileSystem->GetCurrentDirectory( pDir, sizeof(pDir) ) )
{
Q_ComposeFileName( pDir, pRelativeDir, pFullPath, nBufLen );
}
}
else
{
Q_strncpy( pFullPath, pRelativeDir, nBufLen );
}
Q_StripTrailingSlash( pFullPath );
// Ensure the output directory exists
g_pFullFileSystem->CreateDirHierarchy( pFullPath );
}
//-----------------------------------------------------------------------------
// The application object
//-----------------------------------------------------------------------------
entity_t *FindEntity( KeyValues *pEntity )
{
int nHammerId = pEntity->GetInt( "id", INT_MIN );
if ( nHammerId != INT_MIN )
{
// First, look for hammerid
for ( int i = 0; i < num_entities; ++i )
{
int nId = IntForKeyWithDefault( &entities[i], "hammerid", INT_MIN );
if ( nId == nHammerId )
return &entities[i];
}
}
// Unfortunately, hammmerid appears to be a relatively new feature. Now, we must
// look for target name
int nMatch = -1;
const char *pTargetName = pEntity->GetString( "targetname" );
if ( pTargetName && pTargetName[0] )
{
// First, look for hammerid
for ( int i = 0; i < num_entities; ++i )
{
const char *pMatchTargetName = ValueForKey( &entities[i], "targetname" );
if ( !pMatchTargetName || !pMatchTargetName[0] )
continue;
if ( !V_stricmp( pTargetName, pMatchTargetName ) )
{
if ( nMatch >= 0 )
{
//Warning( "Encountered multiple entities that matched targetname %s!\n", pTargetName );
//return false;
nMatch = -1; // force a fallback to scanning classname and origin
break;
}
else
nMatch = i;
}
}
}
if ( nMatch >= 0 )
return &entities[nMatch];
// No target name? Well, let's try classname and origin.
const char *pClassName = pEntity->GetString( "classname" );
if ( pClassName && pClassName[0] )
{
// First, look for hammerid
for ( int i = 0; i < num_entities; ++i )
{
const char *pMatchClassName = ValueForKey( &entities[i], "classname" );
if ( !pMatchClassName || !pMatchClassName[0] )
continue;
if ( V_stricmp( pClassName, pMatchClassName ) )
continue;
const char *pOrigin = "(na)";
if ( V_stricmp( pClassName, "worldspawn" ) ) // allow worldspawn to match all
{
pOrigin = pEntity->GetString( "origin" );
const char *pMatchOrigin = ValueForKey( &entities[i], "origin" );
if ( !pMatchOrigin || !pMatchOrigin[0] )
continue;
if ( V_stricmp( pOrigin, pMatchOrigin ) )
continue;
}
if ( nMatch >= 0 )
{
Warning( "Encountered multiple entities that matched classname %s, origin %s!\n", pClassName, pOrigin );
return false;
}
nMatch = i;
}
}
if ( nMatch >= 0 )
return &entities[nMatch];
return NULL;
}
bool InsertEntity( entity_t *pEntity, KeyValues *pEntityKeys )
{
CUtlVector<KeyValues *> vecKVs;
for ( KeyValues *pKey = pEntityKeys->GetFirstValue(); pKey; pKey = pKey->GetNextValue() )
{
vecKVs.AddToTail( pKey );
}
FOR_EACH_VEC_BACK( vecKVs, i )
{
epair_t *e = (epair_t*)malloc( sizeof(epair_t) );
memset (e, 0, sizeof(epair_t));
const char *pName = vecKVs[i]->GetName();
if ( strlen(pName) >= MAX_KEY-1 )
{
Warning( "ParseEpar: token %s too long", pName );
return false;
}
e->key = copystring(pName);
const char *pValue = vecKVs[i]->GetString();
if ( strlen(pValue) >= MAX_VALUE-1 )
{
Warning( "ParseEpar: token %s too long", pValue );
return false;
}
e->value = copystring(pValue);
// strip trailing spaces
StripTrailing( e->key );
StripTrailing( e->value );
e->next = pEntity->epairs;
pEntity->epairs = e;
}
// Flatten everything ( specifically, 'connection' keys, necessary to
// make the patch file have the same format as the commentary files )
for ( KeyValues *pKey = pEntityKeys->GetFirstTrueSubKey(); pKey; pKey = pKey->GetNextTrueSubKey() )
{
InsertEntity( pEntity, pKey );
}
return true;
}
bool InsertEntity( KeyValues *pEntity )
{
entity_t &entity = entities[ num_entities++ ];
return InsertEntity( &entity, pEntity );
}
bool ReplaceEntity( KeyValues *pEntity )
{
entity_t *pReplace = FindEntity( pEntity );
if ( !pReplace )
{
Warning( "Tried to replace an entity %s, origin %s, but couldn't find the original!\n", pEntity->GetString( "classname" ), pEntity->GetString( "origin" ) );
return false;
}
epair_t *pNext;
for ( epair_t *e = pReplace->epairs; e; e = pNext )
{
pNext = e->next;
free( e->key );
free( e->value );
free( e );
}
pReplace->epairs = NULL;
return InsertEntity( pReplace, pEntity );
}
//-----------------------------------------------------------------------------
// The application object
//-----------------------------------------------------------------------------
int CMkEntityPatchApp::Main()
{
// Backward compat for bsplib
g_pFileSystem = g_pFullFileSystem;
// This bit of hackery allows us to access files on the harddrive
g_pFullFileSystem->AddSearchPath( "", "LOCAL", PATH_ADD_TO_HEAD );
if ( CommandLine()->CheckParm( "-h" ) || CommandLine()->CheckParm( "-help" ) || CommandLine()->ParmCount() == 1 )
{
PrintHelp();
return 0;
}
// The file name is the last argument
const char *pBSPFile = CommandLine()->GetParm( CommandLine()->ParmCount() - 1 );
if ( !pBSPFile || pBSPFile[0] == 0 || pBSPFile[0] == '-' )
{
PrintHelp();
return 0;
}
char pFullPath[MAX_PATH];
ComputeFullPath( pBSPFile, pFullPath, sizeof(pFullPath) );
char pBSPFileName[MAX_PATH];
char pPatchFileName[MAX_PATH];
char pOutputFileName[MAX_PATH];
V_strcpy( pBSPFileName, pFullPath );
V_strcpy( pPatchFileName, pFullPath );
V_SetExtension( pBSPFileName, ".bsp", sizeof(pBSPFileName) );
V_SetExtension( pPatchFileName, ".pat", sizeof(pPatchFileName) );
GenerateLumpFileName( pFullPath, pOutputFileName, sizeof(pOutputFileName), LUMP_ENTITIES );
if ( !g_pFullFileSystem->FileExists( pBSPFileName ) )
{
Warning( "BSP file %s doesn't exist!\n", pBSPFileName );
return 0;
}
if ( !g_pFullFileSystem->FileExists( pPatchFileName ) )
{
Warning( "BSP patch file %s doesn't exist!\n", pPatchFileName );
return 0;
}
KeyValues *pKeyValues = new KeyValues( "patch" );
if ( !pKeyValues->LoadFromFile( g_pFullFileSystem, pPatchFileName ) )
{
Warning( "Error parsing patch file %s!\n", pPatchFileName );
return 0;
}
LoadBSPFile( pFullPath );
ParseEntities();
for( int i = 0; i < num_entities; i++ )
{
entity_t *pCur = &entities[i];
epair_t *pNext = NULL;
epair_t *pPrev = NULL;
for ( epair_t *e = pCur->epairs; e; e = pNext )
{
pNext = e->next;
e->next = pPrev;
pPrev = e;
}
pCur->epairs = pPrev;
}
for ( KeyValues *pKey = pKeyValues->GetFirstTrueSubKey(); pKey; pKey = pKey->GetNextTrueSubKey() )
{
const char *pKeyName = pKey->GetName();
if ( !V_stricmp( pKeyName, "entity" ) )
{
if ( !InsertEntity( pKey ) )
return 0;
}
else if ( !V_stricmp( pKeyName, "replace_entity" ) )
{
if ( !ReplaceEntity( pKey ) )
return 0;
}
}
// Do Perforce Stuff
if ( CommandLine()->FindParm( "-nop4" ) )
{
g_p4factory->SetDummyMode( true );
}
g_p4factory->SetOpenFileChangeList( "Entity Patch Files" );
CP4AutoAddFile p4AddBSP( pBSPFileName );
CP4AutoAddFile p4AddPatch( pPatchFileName );
CP4AutoEditAddFile p4AddOutput( pOutputFileName );
UnparseEntities();
WriteLumpToFile( pBSPFileName, LUMP_ENTITIES, 0, dentdata.Base(), dentdata.Count() );
pKeyValues->deleteThis();
return -1;
}
|