FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
TF2-Source-Code/tf2_src/appframework/AppSystemGroup.cpp at master · sr2echa/TF2-Source-Code · GitHub
sr2echa
/
TF2-Source-Code
Public
Notifications
You must be signed in to change notification settings
Fork
22
Star
81
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
TF2-Source-Code
/
tf2_src
/
appframework
/
AppSystemGroup.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
573 lines (469 loc) · 16 KB
Breadcrumbs
TF2-Source-Code
/
tf2_src
/
appframework
/
AppSystemGroup.cpp
Copy path
File metadata and controls
573 lines (469 loc) · 16 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
//
========= Copyright Valve Corporation, All rights reserved. ============//
//
//
Purpose: Defines a group of app systems that all have the same lifetime
//
that need to be connected/initialized, etc. in a well-defined order
//
//
$Revision: $
//
$NoKeywords: $
//
=============================================================================//
#
include
"
appframework/IAppSystemGroup.h
"
#
include
"
appframework/IAppSystem.h
"
#
include
"
interface.h
"
#
include
"
filesystem.h
"
#
include
"
filesystem_init.h
"
//
memdbgon must be the last include file in a .cpp file!!!
#
include
"
tier0/memdbgon.h
"
//
-----------------------------------------------------------------------------
//
constructor, destructor
//
-----------------------------------------------------------------------------
//
extern ILoggingListener *g_pDefaultLoggingListener;
//
-----------------------------------------------------------------------------
//
constructor, destructor
//
-----------------------------------------------------------------------------
CAppSystemGroup::CAppSystemGroup
( CAppSystemGroup *pAppSystemParent ) : m_SystemDict(
false
,
0
,
16
)
{
m_pParentAppSystem = pAppSystemParent;
}
//
-----------------------------------------------------------------------------
//
Actually loads a DLL
//
-----------------------------------------------------------------------------
CSysModule *
CAppSystemGroup::LoadModuleDLL
(
const
char
*pDLLName )
{
return
Sys_LoadModule
( pDLLName );
}
//
-----------------------------------------------------------------------------
//
Methods to load + unload DLLs
//
-----------------------------------------------------------------------------
AppModule_t
CAppSystemGroup::LoadModule
(
const
char
*pDLLName )
{
//
Remove the extension when creating the name.
int
nLen =
Q_strlen
( pDLLName ) +
1
;
char
*pModuleName = (
char
*)
stackalloc
( nLen );
Q_StripExtension
( pDLLName, pModuleName, nLen );
//
See if we already loaded it...
for
(
int
i = m_Modules.
Count
(); --i >=
0
; )
{
if
( m_Modules[i].
m_pModuleName
)
{
if
( !
Q_stricmp
( pModuleName, m_Modules[i].
m_pModuleName
) )
return
i;
}
}
CSysModule *pSysModule =
LoadModuleDLL
( pDLLName );
if
(!pSysModule)
{
Warning
(
"
AppFramework : Unable to load module %s!
\n
"
, pDLLName );
return
APP_MODULE_INVALID
;
}
int
nIndex = m_Modules.
AddToTail
();
m_Modules[nIndex].
m_pModule
= pSysModule;
m_Modules[nIndex].
m_Factory
=
0
;
m_Modules[nIndex].
m_pModuleName
= (
char
*)
malloc
( nLen );
Q_strncpy
( m_Modules[nIndex].
m_pModuleName
, pModuleName, nLen );
return
nIndex;
}
AppModule_t
CAppSystemGroup::LoadModule
( CreateInterfaceFn factory )
{
if
(!factory)
{
Warning
(
"
AppFramework : Unable to load module %p!
\n
"
, factory );
return
APP_MODULE_INVALID
;
}
//
See if we already loaded it...
for
(
int
i = m_Modules.
Count
(); --i >=
0
; )
{
if
( m_Modules[i].
m_Factory
)
{
if
( m_Modules[i].
m_Factory
== factory )
return
i;
}
}
int
nIndex = m_Modules.
AddToTail
();
m_Modules[nIndex].
m_pModule
=
NULL
;
m_Modules[nIndex].
m_Factory
= factory;
m_Modules[nIndex].
m_pModuleName
=
NULL
;
return
nIndex;
}
void
CAppSystemGroup::UnloadAllModules
()
{
//
NOTE: Iterate in reverse order so they are unloaded in opposite order
//
from loading
for
(
int
i = m_Modules.
Count
(); --i >=
0
; )
{
if
( m_Modules[i].
m_pModule
)
{
Sys_UnloadModule
( m_Modules[i].
m_pModule
);
}
if
( m_Modules[i].
m_pModuleName
)
{
free
( m_Modules[i].
m_pModuleName
);
}
}
m_Modules.
RemoveAll
();
}
//
-----------------------------------------------------------------------------
//
Methods to add/remove various global singleton systems
//
-----------------------------------------------------------------------------
IAppSystem *
CAppSystemGroup::AddSystem
( AppModule_t
module
,
const
char
*pInterfaceName )
{
if
(
module
==
APP_MODULE_INVALID
)
return
NULL
;
Assert
( (
module
>=
0
) && (
module
< m_Modules.
Count
()) );
CreateInterfaceFn pFactory = m_Modules[
module
].
m_pModule
?
Sys_GetFactory
( m_Modules[
module
].
m_pModule
) : m_Modules[
module
].
m_Factory
;
int
retval;
void
*pSystem =
pFactory
( pInterfaceName, &retval );
if
((retval !=
IFACE_OK
) || (!pSystem))
{
Warning
(
"
AppFramework : Unable to create system %s!
\n
"
, pInterfaceName );
return
NULL
;
}
IAppSystem *pAppSystem =
static_cast
<IAppSystem*>(pSystem);
int
sysIndex = m_Systems.
AddToTail
( pAppSystem );
//
Inserting into the dict will help us do named lookup later
MEM_ALLOC_CREDIT
();
m_SystemDict.
Insert
( pInterfaceName, sysIndex );
return
pAppSystem;
}
static
char
const
*g_StageLookup[] =
{
"
CREATION
"
,
"
CONNECTION
"
,
"
PREINITIALIZATION
"
,
"
INITIALIZATION
"
,
"
SHUTDOWN
"
,
"
POSTSHUTDOWN
"
,
"
DISCONNECTION
"
,
"
DESTRUCTION
"
,
"
NONE
"
,
};
void
CAppSystemGroup::ReportStartupFailure
(
int
nErrorStage,
int
nSysIndex )
{
char
const
*pszStageDesc =
"
Unknown
"
;
if
( nErrorStage >=
0
&& nErrorStage <
ARRAYSIZE
( g_StageLookup ) )
{
pszStageDesc = g_StageLookup[ nErrorStage ];
}
char
const
*pszSystemName =
"
(Unknown)
"
;
for
(
int
i = m_SystemDict.
First
(); i != m_SystemDict.
InvalidIndex
(); i = m_SystemDict.
Next
( i ) )
{
if
( m_SystemDict[ i ] != nSysIndex )
continue
;
pszSystemName = m_SystemDict.
GetElementName
( i );
break
;
}
//
Walk the dictionary
Warning
(
"
System (%s) failed during stage %s
\n
"
, pszSystemName, pszStageDesc );
}
void
CAppSystemGroup::AddSystem
( IAppSystem *pAppSystem,
const
char
*pInterfaceName )
{
if
( !pAppSystem )
return
;
int
sysIndex = m_Systems.
AddToTail
( pAppSystem );
//
Inserting into the dict will help us do named lookup later
MEM_ALLOC_CREDIT
();
m_SystemDict.
Insert
( pInterfaceName, sysIndex );
}
void
CAppSystemGroup::RemoveAllSystems
()
{
//
NOTE: There's no deallcation here since we don't really know
//
how the allocation has happened. We could add a deallocation method
//
to the code in interface.h; although when the modules are unloaded
//
the deallocation will happen anyways
m_Systems.
RemoveAll
();
m_SystemDict.
RemoveAll
();
}
//
-----------------------------------------------------------------------------
//
Simpler method of doing the LoadModule/AddSystem thing.
//
-----------------------------------------------------------------------------
bool
CAppSystemGroup::AddSystems
( AppSystemInfo_t *pSystemList )
{
while
( pSystemList->
m_pModuleName
[
0
] )
{
AppModule_t
module
=
LoadModule
( pSystemList->
m_pModuleName
);
IAppSystem *pSystem =
AddSystem
(
module
, pSystemList->
m_pInterfaceName
);
if
( !pSystem )
{
Warning
(
"
Unable to load interface %s from %s
\n
"
, pSystemList->
m_pInterfaceName
, pSystemList->
m_pModuleName
);
return
false
;
}
++pSystemList;
}
return
true
;
}
//
-----------------------------------------------------------------------------
//
Methods to find various global singleton systems
//
-----------------------------------------------------------------------------
void
*
CAppSystemGroup::FindSystem
(
const
char
*pSystemName )
{
unsigned
short
i = m_SystemDict.
Find
( pSystemName );
if
(i != m_SystemDict.
InvalidIndex
())
return
m_Systems[m_SystemDict[i]];
//
If it's not an interface we know about, it could be an older
//
version of an interface, or maybe something implemented by
//
one of the instantiated interfaces...
//
QUESTION: What order should we iterate this in?
//
It controls who wins if multiple ones implement the same interface
for
( i =
0
; i < m_Systems.
Count
(); ++i )
{
void
*pInterface = m_Systems[i]->
QueryInterface
( pSystemName );
if
(pInterface)
return
pInterface;
}
if
( m_pParentAppSystem )
{
void
* pInterface = m_pParentAppSystem->
FindSystem
( pSystemName );
if
( pInterface )
return
pInterface;
}
//
No dice..
return
NULL
;
}
//
-----------------------------------------------------------------------------
//
Gets at the parent appsystem group
//
-----------------------------------------------------------------------------
CAppSystemGroup *
CAppSystemGroup::GetParent
()
{
return
m_pParentAppSystem;
}
//
-----------------------------------------------------------------------------
//
Method to connect/disconnect all systems
//
-----------------------------------------------------------------------------
bool
CAppSystemGroup::ConnectSystems
()
{
for
(
int
i =
0
; i < m_Systems.
Count
(); ++i )
{
IAppSystem *sys = m_Systems[i];
if
(!sys->
Connect
(
GetFactory
() ))
{
ReportStartupFailure
(
CONNECTION
, i );
return
false
;
}
}
return
true
;
}
void
CAppSystemGroup::DisconnectSystems
()
{
//
Disconnect in reverse order of connection
for
(
int
i = m_Systems.
Count
(); --i >=
0
; )
{
m_Systems[i]->
Disconnect
();
}
}
//
-----------------------------------------------------------------------------
//
Method to initialize/shutdown all systems
//
-----------------------------------------------------------------------------
InitReturnVal_t
CAppSystemGroup::InitSystems
()
{
for
(
int
i =
0
; i < m_Systems.
Count
(); ++i )
{
InitReturnVal_t nRetVal = m_Systems[i]->
Init
();
if
( nRetVal !=
INIT_OK
)
{
ReportStartupFailure
(
INITIALIZATION
, i );
return
nRetVal;
}
}
return
INIT_OK
;
}
void
CAppSystemGroup::ShutdownSystems
()
{
//
Shutdown in reverse order of initialization
for
(
int
i = m_Systems.
Count
(); --i >=
0
; )
{
m_Systems[i]->
Shutdown
();
}
}
//
-----------------------------------------------------------------------------
//
Returns the stage at which the app system group ran into an error
//
-----------------------------------------------------------------------------
CAppSystemGroup::AppSystemGroupStage_t
CAppSystemGroup::GetErrorStage
()
const
{
return
m_nErrorStage;
}
//
-----------------------------------------------------------------------------
//
Gets at a factory that works just like FindSystem
//
-----------------------------------------------------------------------------
//
This function is used to make this system appear to the outside world to
//
function exactly like the currently existing factory system
CAppSystemGroup *s_pCurrentAppSystem;
void
*
AppSystemCreateInterfaceFn
(
const
char
*pName,
int
*pReturnCode)
{
void
*pInterface = s_pCurrentAppSystem->
FindSystem
( pName );
if
( pReturnCode )
{
*pReturnCode = pInterface ?
IFACE_OK
:
IFACE_FAILED
;
}
return
pInterface;
}
//
-----------------------------------------------------------------------------
//
Gets at a class factory for the topmost appsystem group in an appsystem stack
//
-----------------------------------------------------------------------------
CreateInterfaceFn
CAppSystemGroup::GetFactory
()
{
return
AppSystemCreateInterfaceFn;
}
//
-----------------------------------------------------------------------------
//
Main application loop
//
-----------------------------------------------------------------------------
int
CAppSystemGroup::Run
()
{
//
The factory now uses this app system group
s_pCurrentAppSystem =
this
;
//
Load, connect, init
int
nRetVal =
OnStartup
();
if
( m_nErrorStage !=
NONE
)
return
nRetVal;
//
Main loop implemented by the application
//
FIXME: HACK workaround to avoid vgui porting
nRetVal =
Main
();
//
Shutdown, disconnect, unload
OnShutdown
();
//
The factory now uses the parent's app system group
s_pCurrentAppSystem =
GetParent
();
return
nRetVal;
}
//
-----------------------------------------------------------------------------
//
Virtual methods for override
//
-----------------------------------------------------------------------------
int
CAppSystemGroup::Startup
()
{
return
OnStartup
();
}
void
CAppSystemGroup::Shutdown
()
{
return
OnShutdown
();
}
//
-----------------------------------------------------------------------------
//
Use this version in cases where you can't control the main loop and
//
expect to be ticked
//
-----------------------------------------------------------------------------
int
CAppSystemGroup::OnStartup
()
{
//
The factory now uses this app system group
s_pCurrentAppSystem =
this
;
m_nErrorStage =
NONE
;
//
Call an installed application creation function
if
( !
Create
() )
{
m_nErrorStage =
CREATION
;
return
-
1
;
}
//
Let all systems know about each other
if
( !
ConnectSystems
() )
{
m_nErrorStage =
CONNECTION
;
return
-
1
;
}
//
Allow the application to do some work before init
if
( !
PreInit
() )
{
m_nErrorStage =
PREINITIALIZATION
;
return
-
1
;
}
//
Call Init on all App Systems
int
nRetVal =
InitSystems
();
if
( nRetVal !=
INIT_OK
)
{
m_nErrorStage =
INITIALIZATION
;
return
-
1
;
}
return
nRetVal;
}
void
CAppSystemGroup::OnShutdown
()
{
//
The factory now uses this app system group
s_pCurrentAppSystem =
this
;
switch
( m_nErrorStage )
{
case
NONE
:
break
;
case
PREINITIALIZATION
:
case
INITIALIZATION
:
goto
disconnect;
case
CREATION
:
case
CONNECTION
:
goto
destroy;
}
//
Cal Shutdown on all App Systems
ShutdownSystems
();
//
Allow the application to do some work after shutdown
PostShutdown
();
disconnect:
//
Systems should disconnect from each other
DisconnectSystems
();
destroy:
//
Unload all DLLs loaded in the AppCreate block
RemoveAllSystems
();
//
Have to do this because the logging listeners & response policies may live in modules which are being unloaded
//
@TODO: this seems like a bad legacy practice... app systems should unload their spew handlers gracefully.
//
LoggingSystem_ResetCurrentLoggingState();
//
Assert( g_pDefaultLoggingListener != NULL );
//
LoggingSystem_RegisterLoggingListener( g_pDefaultLoggingListener );
UnloadAllModules
();
//
Call an installed application destroy function
Destroy
();
}
//
-----------------------------------------------------------------------------
//
//
This class represents a group of app systems that are loaded through steam
//
//
-----------------------------------------------------------------------------
//
-----------------------------------------------------------------------------
//
Constructor
//
-----------------------------------------------------------------------------
CSteamAppSystemGroup::CSteamAppSystemGroup
( IFileSystem *pFileSystem, CAppSystemGroup *pAppSystemParent )
{
m_pFileSystem = pFileSystem;
m_pGameInfoPath[
0
] =
0
;
}
//
-----------------------------------------------------------------------------
//
Used by CSteamApplication to set up necessary pointers if we can't do it in the constructor
//
-----------------------------------------------------------------------------
void
CSteamAppSystemGroup::Setup
( IFileSystem *pFileSystem, CAppSystemGroup *pParentAppSystem )
{
m_pFileSystem = pFileSystem;
m_pParentAppSystem = pParentAppSystem;
}
//
-----------------------------------------------------------------------------
//
Loads the module from Steam
//
-----------------------------------------------------------------------------
CSysModule *
CSteamAppSystemGroup::LoadModuleDLL
(
const
char
*pDLLName )
{
return
m_pFileSystem->
LoadModule
( pDLLName );
}
//
-----------------------------------------------------------------------------
//
Returns the game info path
//
-----------------------------------------------------------------------------
const
char
*
CSteamAppSystemGroup::GetGameInfoPath
()
const
{
return
m_pGameInfoPath;
}
//
-----------------------------------------------------------------------------
//
Sets up the search paths
//
-----------------------------------------------------------------------------
bool
CSteamAppSystemGroup::SetupSearchPaths
(
const
char
*pStartingDir,
bool
bOnlyUseStartingDir,
bool
bIsTool )
{
CFSSteamSetupInfo steamInfo;
steamInfo.
m_pDirectoryName
= pStartingDir;
steamInfo.
m_bOnlyUseDirectoryName
= bOnlyUseStartingDir;
steamInfo.
m_bToolsMode
= bIsTool;
steamInfo.
m_bSetSteamDLLPath
=
true
;
steamInfo.
m_bSteam
= m_pFileSystem->
IsSteam
();
if
(
FileSystem_SetupSteamEnvironment
( steamInfo ) !=
FS_OK
)
return
false
;
CFSMountContentInfo fsInfo;
fsInfo.
m_pFileSystem
= m_pFileSystem;
fsInfo.
m_bToolsMode
= bIsTool;
fsInfo.
m_pDirectoryName
= steamInfo.
m_GameInfoPath
;
if
(
FileSystem_MountContent
( fsInfo ) !=
FS_OK
)
return
false
;
//
Finally, load the search paths for the "GAME" path.
CFSSearchPathsInit searchPathsInit;
searchPathsInit.
m_pDirectoryName
= steamInfo.
m_GameInfoPath
;
searchPathsInit.
m_pFileSystem
= fsInfo.
m_pFileSystem
;
if
(
FileSystem_LoadSearchPaths
( searchPathsInit ) !=
FS_OK
)
return
false
;
FileSystem_AddSearchPath_Platform
( fsInfo.
m_pFileSystem
, steamInfo.
m_GameInfoPath
);
Q_strncpy
( m_pGameInfoPath, steamInfo.
m_GameInfoPath
,
sizeof
(m_pGameInfoPath) );
return
true
;
}
Back
|
FazBrowse Home
|
New Git URL