-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExport.cpp
355 lines (290 loc) · 7.93 KB
/
Export.cpp
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
//
// Copyright (c) 2012, Wei Mingzhi <whistler_wmz@users.sf.net>.
// All Rights Reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, see <http://www.gnu.org/licenses/>.
//
#include "Main.h"
#define BOT_PAKFILE_PREFIX "zeta"
#define COPYRIGHT_MESSAGE "Doom3 Hook DLL Prototype (C) 2012, Wei Mingzhi"
idSys * sys = NULL;
idCommon * common = NULL;
idCmdSystem * cmdSystem = NULL;
idCVarSystem * cvarSystem = NULL;
idFileSystem * fileSystem = NULL;
idNetworkSystem * networkSystem = NULL;
idRenderSystem * renderSystem = NULL;
idSoundSystem * soundSystem = NULL;
idRenderModelManager * renderModelManager = NULL;
idUserInterfaceManager * uiManager = NULL;
idDeclManager * declManager = NULL;
idAASFileManager * AASFileManager = NULL;
idCollisionModelManager * collisionModelManager = NULL;
idRenderWorld * gameRenderWorld = NULL; // all drawing is done to this world
idSoundWorld * gameSoundWorld = NULL; // all audio goes to this world
idGame * game;
gameExport_t gameExport;
static int gameDLL;
static GetGameAPI_t pfnGetGameAPI;
idCVar * idCVar::staticVars = NULL;
/*
==============
Sys_ListFiles
==============
*/
#ifdef _WIN32
int Sys_ListFiles( const char *directory, const char *extension, idStrList &list ) {
char search[ MAX_OSPATH ];
struct _finddata_t findinfo;
int findhandle;
int flag;
if ( !extension) {
extension = "";
}
// passing a slash as extension will find directories
if ( extension[0] == '/' && extension[1] == 0 ) {
extension = "";
flag = 0;
} else {
flag = _A_SUBDIR;
}
sprintf( search, "%s\\*%s", directory, extension );
// search
list.Clear();
findhandle = _findfirst( search, &findinfo );
if ( findhandle == -1 ) {
return -1;
}
do {
if ( flag ^ ( findinfo.attrib & _A_SUBDIR ) ) {
list.Append( findinfo.name );
}
} while ( _findnext( findhandle, &findinfo ) != -1 );
_findclose( findhandle );
return list.Num();
}
#else
int Sys_ListFiles( const char *directory, const char *extension, idStrList &list ) {
struct dirent *d;
DIR *fdir;
bool dironly = false;
char search[ MAX_OSPATH ];
struct stat st;
bool debug;
list.Clear();
debug = cvarSystem->GetCVarBool( "fs_debug" );
if (!extension)
extension = "";
// passing a slash as extension will find directories
if (extension[0] == '/' && extension[1] == 0) {
extension = "";
dironly = true;
}
// search
// NOTE: case sensitivity of directory path can screw us up here
if ((fdir = opendir(directory)) == NULL) {
if (debug) {
common->Printf("Sys_ListFiles: opendir %s failed\n", directory);
}
return -1;
}
while ((d = readdir(fdir)) != NULL) {
idStr::snPrintf(search, sizeof(search), "%s/%s", directory, d->d_name);
if (stat(search, &st) == -1)
continue;
if (!dironly) {
idStr look(search);
idStr ext;
look.ExtractFileExtension(ext);
if (extension[0] != '\0' && ext.Icmp(&extension[1]) != 0) {
continue;
}
}
if ((dironly && !(st.st_mode & S_IFDIR)) ||
(!dironly && (st.st_mode & S_IFDIR)))
continue;
list.Append(d->d_name);
}
closedir(fdir);
if ( debug ) {
common->Printf( "Sys_ListFiles: %d entries in %s\n", list.Num(), directory );
}
return list.Num();
}
#endif
/*
=================
ExtractGameDLL
=================
*/
void ExtractGameDLL( void ) {
idStrList list;
int i;
char dllName[ MAX_OSPATH ];
sys->DLL_GetFileName( "game", dllName, MAX_OSPATH );
// scan the directory for all pk4 files
Sys_ListFiles( fileSystem->RelativePathToOSPath( ".", "fs_basepath" ), ".pk4", list );
// sort the list by filename
list.Sort();
// walk through all pak files in reverse order
for ( i = list.Num() - 1; i >= 0; --i ) {
if ( list[ i ].Icmpn( BOT_PAKFILE_PREFIX, idStr::Length( BOT_PAKFILE_PREFIX ) ) == 0 ) {
continue; // skip myself
}
unzFile uf;
unz_file_info file_info;
// open the pak file
uf = unzOpen( fileSystem->RelativePathToOSPath( list[ i ].c_str(), "fs_basepath" ) );
if ( !uf ) {
continue;
}
// try to locate game DLL inside pak
if ( unzLocateFile( uf, dllName, 0 ) != UNZ_OK ) {
unzClose( uf );
continue;
}
if ( unzGetCurrentFileInfo( uf, &file_info, dllName, sizeof( dllName ), NULL, 0, NULL, 0 ) != UNZ_OK ) {
unzClose( uf );
continue;
}
if ( unzOpenCurrentFile( uf ) != UNZ_OK ) {
unzClose( uf );
continue;
}
unsigned char *buf = new unsigned char[ file_info.uncompressed_size ];
if ( !buf ) {
unzCloseCurrentFile( uf );
unzClose( uf );
delete[] buf;
continue;
}
// read the file to buffer
if ( unzReadCurrentFile( uf, buf, file_info.uncompressed_size ) <= 0 ) {
unzCloseCurrentFile( uf );
unzClose( uf );
delete[] buf;
continue;
}
// extract the dll
fileSystem->WriteFile( va( "dlls/%s", dllName ), buf, file_info.uncompressed_size );
unzCloseCurrentFile( uf );
unzClose( uf );
delete[] buf;
break;
}
}
/*
=================
GetDLLPath
=================
*/
const char *GetDLLPath( void ) {
char dllName[ MAX_OSPATH ];
sys->DLL_GetFileName( "game", dllName, MAX_OSPATH );
return fileSystem->RelativePathToOSPath( va( "dlls/%s", dllName ) );
}
/*
=================
LoadGameDLL
=================
*/
void LoadGameDLL( void ) {
ExtractGameDLL();
gameDLL = sys->DLL_Load( GetDLLPath() );
if ( !gameDLL ) {
common->FatalError( "couldn't load game dynamic library" );
return;
}
pfnGetGameAPI = (GetGameAPI_t) sys->DLL_GetProcAddress( gameDLL, "GetGameAPI" );
if ( !pfnGetGameAPI ) {
sys->DLL_Unload( gameDLL );
gameDLL = NULL;
common->FatalError( "couldn't find game DLL API" );
return;
}
}
/*
=================
UnloadGameDLL
=================
*/
void UnloadGameDLL( void ) {
if ( gameDLL ) {
sys->DLL_Unload( gameDLL );
gameDLL = NULL;
}
pfnGetGameAPI = NULL;
game = NULL;
}
/*
===========
GetGameAPI
============
*/
#if __MWERKS__
#pragma export on
#endif
#if __GNUC__ >= 4
#pragma GCC visibility push(default)
#endif
extern "C" gameExport_t *GetGameAPI( gameImport_t *import ) {
#if __MWERKS__
#pragma export off
#endif
if ( import->version == GAME_API_VERSION ) {
// set interface pointers used by the game
sys = import->sys;
common = import->common;
cmdSystem = import->cmdSystem;
cvarSystem = import->cvarSystem;
fileSystem = import->fileSystem;
networkSystem = import->networkSystem;
renderSystem = import->renderSystem;
soundSystem = import->soundSystem;
renderModelManager = import->renderModelManager;
uiManager = import->uiManager;
declManager = import->declManager;
AASFileManager = import->AASFileManager;
collisionModelManager = import->collisionModelManager;
}
// load the actual game DLL
LoadGameDLL();
if ( !gameDLL ) {
common->FatalError( "Unable to load game DLL" );
return NULL;
}
// call game DLL's GetGameAPI()
gameExport_t *gameExportDLL = pfnGetGameAPI(&importLocal);
if ( !gameExportDLL ) {
common->FatalError( "GetGameAPI() failed" );
UnloadGameDLL();
return NULL;
}
// set interface pointers used by idLib
idLib::sys = sys;
idLib::common = common;
idLib::cvarSystem = cvarSystem;
idLib::fileSystem = fileSystem;
// save game API pointer
game = gameExportDLL->game;
// setup export interface
gameExport.version = gameExportDLL->version;
gameExport.game = (idGame *)&gameInterface;
gameExport.gameEdit = gameExportDLL->gameEdit;
common->Printf( "%s\n", COPYRIGHT_MESSAGE );
return &gameExport;
}
#if __GNUC__ >= 4
#pragma GCC visibility pop
#endif