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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
#include "cmdlib.h"
#include "mathlib/mathlib.h"
#include "tier1/strtools.h"
#include "physdll.h"
#include "phyfile.h"
#include "phxfile.h"
#include "utlvector.h"
#include "utlbuffer.h"
#include "vphysics_interface.h"
#include "vcollide_parse.h"
#include "vphysics/constraints.h"
#include "tier0/icommandline.h"
#include "filesystem_tools.h"
#include "simplify.h"
#include "mathlib/compressed_vector.h"
#include "keyvalues.h"
IPhysicsCollision *physcollision = NULL;
IPhysicsSurfaceProps *physprops = NULL;
int g_TotalOut = 0;
int g_TotalCompress = 0;
void InitFilesystem( const char *pPath )
{
CmdLib_InitFileSystem( pPath );
//FileSystem_Init( ".", 0, FS_INIT_COMPATIBILITY_MODE );
// This bit of hackery allows us to access files on the harddrive
g_pFullFileSystem->AddSearchPath( "", "LOCAL", PATH_ADD_TO_HEAD );
}
static bool LoadSurfaceProps( const char *pMaterialFilename )
{
if ( !physprops )
return false;
// already loaded
if ( physprops->SurfacePropCount() )
return false;
FileHandle_t fp = g_pFileSystem->Open( pMaterialFilename, "rb", TOOLS_READ_PATH_ID );
if ( fp == FILESYSTEM_INVALID_HANDLE )
return false;
int len = g_pFileSystem->Size( fp );
char *pText = new char[len+1];
g_pFileSystem->Read( pText, len, fp );
g_pFileSystem->Close( fp );
pText[len]=0;
physprops->ParseSurfaceData( pMaterialFilename, pText );
delete[] pText;
return true;
}
void LoadSurfacePropsAll()
{
const char *SURFACEPROP_MANIFEST_FILE = "scripts/surfaceproperties_manifest.txt";
KeyValues *manifest = new KeyValues( SURFACEPROP_MANIFEST_FILE );
if ( manifest->LoadFromFile( g_pFileSystem, SURFACEPROP_MANIFEST_FILE, "GAME" ) )
{
for ( KeyValues *sub = manifest->GetFirstSubKey(); sub != NULL; sub = sub->GetNextKey() )
{
if ( !Q_stricmp( sub->GetName(), "file" ) )
{
// Add
LoadSurfaceProps( sub->GetString() );
continue;
}
}
}
manifest->deleteThis();
}
void InitVPhysics()
{
CreateInterfaceFn physicsFactory = GetPhysicsFactory();
physcollision = (IPhysicsCollision *)physicsFactory( VPHYSICS_COLLISION_INTERFACE_VERSION, NULL );
physprops = (IPhysicsSurfaceProps *)physicsFactory( VPHYSICS_SURFACEPROPS_INTERFACE_VERSION, NULL );
LoadSurfacePropsAll();
}
struct phyfile_t
{
phyheader_t header;
vcollide_t collide;
int fileSize;
};
void LoadPHYFile(phyfile_t *pOut, const char *name)
{
memset( pOut, 0, sizeof(*pOut) );
FileHandle_t file = g_pFullFileSystem->Open( name, "rb" );
if ( !file )
return;
g_pFullFileSystem->Read( &pOut->header, sizeof(pOut->header), file );
if ( pOut->header.size != sizeof(pOut->header) || pOut->header.solidCount <= 0 )
return;
pOut->fileSize = g_pFullFileSystem->Size( file );
char *buf = (char *)_alloca( pOut->fileSize );
g_pFullFileSystem->Read( buf, pOut->fileSize, file );
g_pFullFileSystem->Close( file );
physcollision->VCollideLoad( &pOut->collide, pOut->header.solidCount, (const char *)buf, pOut->fileSize );
}
void WritePHXFile( const char *pName, const phyfile_t &file )
{
if ( file.header.size != sizeof(file.header) || file.collide.solidCount <= 0 )
return;
CUtlBuffer out;
char outName[1024];
Q_snprintf( outName, sizeof(outName), "%s", pName );
Q_SetExtension( outName, ".phx", sizeof(outName) );
out.Put( &file.header, sizeof(file.header) );
int outSize = 0;
float tolerance = (file.collide.solidCount > 1) ? 3.0f : 7.0f;
vcollide_t *pNewCollide = ConvertVCollideToPHX( &file.collide, tolerance, &outSize, false );
g_TotalOut += file.fileSize;
for ( int i = 0; i < pNewCollide->solidCount; i++ )
{
int collideSize = physcollision->CollideSize( pNewCollide->solids[i] );
out.PutInt( collideSize );
char *pMem = new char[collideSize];
physcollision->CollideWrite( pMem, pNewCollide->solids[i] );
out.Put( pMem, collideSize );
delete[] pMem;
}
Msg("%s Compressed %d (%d text) to %d (%d text)\n", outName, file.fileSize, file.collide.descSize, out.TellPut(), pNewCollide->descSize );
out.Put( pNewCollide->pKeyValues, pNewCollide->descSize );
g_TotalCompress += out.TellPut();
Msg("OLD:\n-----------------------------------\n%s\n", file.collide.pKeyValues );
CPackedPhysicsDescription *pPacked = physcollision->CreatePackedDesc( pNewCollide->pKeyValues, pNewCollide->descSize );
Msg("NEW:\n-----------------------------------\n" );
for ( int i = 0; i < pPacked->m_solidCount; i++ )
{
solid_t solid;
pPacked->GetSolid( &solid, i );
Msg("index %d\n", solid.index );
Msg("name %s\n", solid.name );
Msg("mass %.2f\n", solid.params.mass );
Msg("surfaceprop %s\n", solid.surfaceprop);
Msg("damping %.2f\n", solid.params.damping );
Msg("rotdamping %.2f\n", solid.params.rotdamping );
Msg("drag %.2f\n", solid.params.dragCoefficient );
Msg("inertia %.2f\n", solid.params.inertia );
Msg("volume %.2f\n", solid.params.volume );
}
if ( !g_pFullFileSystem->WriteFile( outName, NULL, out ) )
Error("Error writing %s\n", outName );
}
void UnloadPHYFile( phyfile_t *pFile )
{
physcollision->VCollideUnload( &pFile->collide );
pFile->header.size = 0;
}
int main( int argc, char *argv[] )
{
CommandLine()->CreateCmdLine( argc, argv );
InstallSpewFunction();
InitFilesystem( argv[argc-1] );
InitVPhysics();
MathLib_Init( 2.2f, 2.2f, 0.0f, 2.0f, false, false, false, false );
g_TotalOut = 0;
g_TotalCompress = 0;
FileFindHandle_t handle;
char fullpath[1024], filebase[1024];
strcpy( fullpath, argv[argc-1] );
strcpy( fullpath, ExpandPath( fullpath ) );
strcpy( fullpath, ExpandArg( fullpath ) );
Q_FileBase(fullpath, filebase, sizeof(filebase));
const char *pFilename = g_pFullFileSystem->FindFirst( argv[argc-1], &handle );
while ( pFilename )
{
#if 0
if ( g_pFullFileSystem->FindIsDirectory( handle ) )
{
}
#endif
g_pFullFileSystem->RelativePathToFullPath( pFilename, NULL, filebase, sizeof( filebase ) );
phyfile_t phy;
strcpy( filebase, ExpandPath( filebase ) );
strcpy( filebase, ExpandArg( filebase ) );
LoadPHYFile( &phy, filebase );
WritePHXFile( filebase, phy );
UnloadPHYFile( &phy );
pFilename = g_pFullFileSystem->FindNext( handle );
}
g_pFullFileSystem->FindClose( handle );
Msg("Total %s, %s\nSaved %s\n", Q_pretifymem( g_TotalOut ), Q_pretifymem( g_TotalCompress ), Q_pretifymem( g_TotalOut - g_TotalCompress ) );
Msg("%.2f%% savings\n", ((float)(g_TotalOut-g_TotalCompress) / (float)g_TotalOut) * 100.0f );
return 0;
}
|