-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathappsystemgroup.cpp
More file actions
120 lines (100 loc) · 3.18 KB
/
appsystemgroup.cpp
File metadata and controls
120 lines (100 loc) · 3.18 KB
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
#include "filesystem.h"
#define protected public
#include "appframework/IAppSystemGroup.h"
#undef protected
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, requested from EXE.\n", pSystemList->m_pInterfaceName, pSystemList->m_pModuleName );
return false;
}
++pSystemList;
}
return true;
}
IAppSystem *CAppSystemGroup::AddSystem( AppModule_t module, const char *pInterfaceName )
{
if (module == APP_MODULE_INVALID)
return NULL;
int nFoundIndex = m_SystemDict.Find( pInterfaceName );
if ( nFoundIndex != m_SystemDict.InvalidIndex() )
{
Warning("AppFramework : Attempted to add two systems with the same interface name %s!\n", pInterfaceName );
return m_Systems[ m_SystemDict[nFoundIndex] ];
}
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 );
MEM_ALLOC_CREDIT();
m_SystemDict.Insert( pInterfaceName, sysIndex );
return pAppSystem;
}
AppModule_t CAppSystemGroup::LoadModule( const char *pDLLName )
{
int nLen = Q_strlen( pDLLName ) + 1;
char *pModuleName = (char*)stackalloc( nLen );
Q_StripExtension( pDLLName, pModuleName, nLen );
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)
{
#ifdef _X360
Warning("AppFramework : Unable to load module %s! (err #%d)\n", pDLLName, GetLastError() );
#else
Warning("AppFramework : Unable to load module %s!\n", pDLLName );
#endif
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;
}
void *CAppSystemGroup::FindSystem( const char *pSystemName )
{
unsigned short i = m_SystemDict.Find( pSystemName );
if (i != m_SystemDict.InvalidIndex())
return m_Systems[m_SystemDict[i]];
for ( i = 0; i < m_Systems.Count(); ++i )
{
void *pInterface = m_Systems[i]->QueryInterface( pSystemName );
if (pInterface)
return pInterface;
}
int nExternalCount = m_NonAppSystemFactories.Count();
for ( i = 0; i < nExternalCount; ++i )
{
void *pInterface = m_NonAppSystemFactories[i]( pSystemName, NULL );
if (pInterface)
return pInterface;
}
if ( m_pParentAppSystem )
{
void* pInterface = m_pParentAppSystem->FindSystem( pSystemName );
if ( pInterface )
return pInterface;
}
return NULL;
}