Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

302 lines
11 KiB

  1. //===== Copyright � 2005-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose: A higher level link library for general use in the game and tools.
  4. //
  5. //===========================================================================//
  6. #include "tier0/platform.h"
  7. #include "tier0/dbg.h"
  8. #include "interfaces/interfaces.h"
  9. //-----------------------------------------------------------------------------
  10. // Tier 1 libraries
  11. //-----------------------------------------------------------------------------
  12. ICvar *cvar = 0;
  13. ICvar *g_pCVar = 0;
  14. IProcessUtils *g_pProcessUtils = 0;
  15. static bool s_bConnected = false;
  16. IPhysics2 *g_pPhysics2 = 0;
  17. IPhysics2ActorManager *g_pPhysics2ActorManager = 0;
  18. IPhysics2ResourceManager *g_pPhysics2ResourceManager = 0;
  19. IEventSystem *g_pEventSystem = 0;
  20. ILocalize *g_pLocalize = 0;
  21. // for utlsortvector.h
  22. #ifndef _WIN32
  23. void *g_pUtlSortVectorQSortContext = NULL;
  24. #endif
  25. //-----------------------------------------------------------------------------
  26. // Tier 2 libraries
  27. //-----------------------------------------------------------------------------
  28. IResourceSystem *g_pResourceSystem = 0;
  29. IRenderDeviceMgr *g_pRenderDeviceMgr = 0;
  30. IFileSystem *g_pFullFileSystem = 0;
  31. IAsyncFileSystem *g_pAsyncFileSystem = 0;
  32. IMaterialSystem *materials = 0;
  33. IMaterialSystem *g_pMaterialSystem = 0;
  34. IMaterialSystem2 *g_pMaterialSystem2 = 0;
  35. IInputSystem *g_pInputSystem = 0;
  36. IInputStackSystem *g_pInputStackSystem = 0;
  37. INetworkSystem *g_pNetworkSystem = 0;
  38. ISoundSystem *g_pSoundSystem = 0;
  39. IMaterialSystemHardwareConfig *g_pMaterialSystemHardwareConfig = 0;
  40. IDebugTextureInfo *g_pMaterialSystemDebugTextureInfo = 0;
  41. IVBAllocTracker *g_VBAllocTracker = 0;
  42. IColorCorrectionSystem *colorcorrection = 0;
  43. IP4 *p4 = 0;
  44. IMdlLib *mdllib = 0;
  45. IQueuedLoader *g_pQueuedLoader = 0;
  46. IResourceAccessControl *g_pResourceAccessControl = 0;
  47. IPrecacheSystem *g_pPrecacheSystem = 0;
  48. ISceneSystem *g_pSceneSystem = 0;
  49. #if defined( PLATFORM_X360 )
  50. IXboxInstaller *g_pXboxInstaller = 0;
  51. #endif
  52. IMatchFramework *g_pMatchFramework = 0;
  53. IGameUISystemMgr *g_pGameUISystemMgr = 0;
  54. #if defined( INCLUDE_SCALEFORM )
  55. IScaleformUI *g_pScaleformUI = 0;
  56. #endif
  57. //-----------------------------------------------------------------------------
  58. // Not exactly a global, but we're going to keep track of these here anyways
  59. //-----------------------------------------------------------------------------
  60. IRenderDevice *g_pRenderDevice = 0;
  61. IRenderHardwareConfig *g_pRenderHardwareConfig = 0;
  62. //-----------------------------------------------------------------------------
  63. // Tier3 libraries
  64. //-----------------------------------------------------------------------------
  65. IMeshSystem *g_pMeshSystem = 0;
  66. IStudioRender *g_pStudioRender = 0;
  67. IStudioRender *studiorender = 0;
  68. IMatSystemSurface *g_pMatSystemSurface = 0;
  69. vgui::IInput *g_pVGuiInput = 0;
  70. vgui::ISurface *g_pVGuiSurface = 0;
  71. vgui::IPanel *g_pVGuiPanel = 0;
  72. vgui::IVGui *g_pVGui = 0;
  73. vgui::ILocalize *g_pVGuiLocalize = 0;
  74. vgui::ISchemeManager *g_pVGuiSchemeManager = 0;
  75. vgui::ISystem *g_pVGuiSystem = 0;
  76. IDataCache *g_pDataCache = 0;
  77. IMDLCache *g_pMDLCache = 0;
  78. IMDLCache *mdlcache = 0;
  79. IAvi *g_pAVI = 0;
  80. IBik *g_pBIK = 0;
  81. IDmeMakefileUtils *g_pDmeMakefileUtils = 0;
  82. IPhysicsCollision *g_pPhysicsCollision = 0;
  83. ISoundEmitterSystemBase *g_pSoundEmitterSystem = 0;
  84. IWorldRendererMgr *g_pWorldRendererMgr = 0;
  85. IVGuiRenderSurface *g_pVGuiRenderSurface = 0;
  86. //-----------------------------------------------------------------------------
  87. // Mapping of interface string to globals
  88. //-----------------------------------------------------------------------------
  89. struct InterfaceGlobals_t
  90. {
  91. const char *m_pInterfaceName;
  92. void *m_ppGlobal;
  93. };
  94. static InterfaceGlobals_t g_pInterfaceGlobals[] =
  95. {
  96. { CVAR_INTERFACE_VERSION, &cvar },
  97. { CVAR_INTERFACE_VERSION, &g_pCVar },
  98. { EVENTSYSTEM_INTERFACE_VERSION, &g_pEventSystem },
  99. { PROCESS_UTILS_INTERFACE_VERSION, &g_pProcessUtils },
  100. { VPHYSICS2_INTERFACE_VERSION, &g_pPhysics2 },
  101. { VPHYSICS2_ACTOR_MGR_INTERFACE_VERSION, &g_pPhysics2ActorManager },
  102. { VPHYSICS2_RESOURCE_MGR_INTERFACE_VERSION, &g_pPhysics2ResourceManager },
  103. { FILESYSTEM_INTERFACE_VERSION, &g_pFullFileSystem },
  104. { ASYNCFILESYSTEM_INTERFACE_VERSION, &g_pAsyncFileSystem },
  105. { RESOURCESYSTEM_INTERFACE_VERSION, &g_pResourceSystem },
  106. { MATERIAL_SYSTEM_INTERFACE_VERSION, &g_pMaterialSystem },
  107. { MATERIAL_SYSTEM_INTERFACE_VERSION, &materials },
  108. { MATERIAL_SYSTEM2_INTERFACE_VERSION, &g_pMaterialSystem2 },
  109. { INPUTSYSTEM_INTERFACE_VERSION, &g_pInputSystem },
  110. { INPUTSTACKSYSTEM_INTERFACE_VERSION, &g_pInputStackSystem },
  111. { NETWORKSYSTEM_INTERFACE_VERSION, &g_pNetworkSystem },
  112. { RENDER_DEVICE_MGR_INTERFACE_VERSION, &g_pRenderDeviceMgr },
  113. { MATERIALSYSTEM_HARDWARECONFIG_INTERFACE_VERSION, &g_pMaterialSystemHardwareConfig },
  114. { SOUNDSYSTEM_INTERFACE_VERSION, &g_pSoundSystem },
  115. { DEBUG_TEXTURE_INFO_VERSION, &g_pMaterialSystemDebugTextureInfo },
  116. { VB_ALLOC_TRACKER_INTERFACE_VERSION, &g_VBAllocTracker },
  117. { COLORCORRECTION_INTERFACE_VERSION, &colorcorrection },
  118. { P4_INTERFACE_VERSION, &p4 },
  119. { MDLLIB_INTERFACE_VERSION, &mdllib },
  120. { QUEUEDLOADER_INTERFACE_VERSION, &g_pQueuedLoader },
  121. { RESOURCE_ACCESS_CONTROL_INTERFACE_VERSION, &g_pResourceAccessControl },
  122. { PRECACHE_SYSTEM_INTERFACE_VERSION, &g_pPrecacheSystem },
  123. { STUDIO_RENDER_INTERFACE_VERSION, &g_pStudioRender },
  124. { STUDIO_RENDER_INTERFACE_VERSION, &studiorender },
  125. { VGUI_IVGUI_INTERFACE_VERSION, &g_pVGui },
  126. { VGUI_INPUT_INTERFACE_VERSION, &g_pVGuiInput },
  127. { VGUI_PANEL_INTERFACE_VERSION, &g_pVGuiPanel },
  128. { VGUI_SURFACE_INTERFACE_VERSION, &g_pVGuiSurface },
  129. { VGUI_SCHEME_INTERFACE_VERSION, &g_pVGuiSchemeManager },
  130. { VGUI_SYSTEM_INTERFACE_VERSION, &g_pVGuiSystem },
  131. { LOCALIZE_INTERFACE_VERSION, &g_pLocalize },
  132. { LOCALIZE_INTERFACE_VERSION, &g_pVGuiLocalize },
  133. { MAT_SYSTEM_SURFACE_INTERFACE_VERSION, &g_pMatSystemSurface },
  134. { DATACACHE_INTERFACE_VERSION, &g_pDataCache },
  135. { MDLCACHE_INTERFACE_VERSION, &g_pMDLCache },
  136. { MDLCACHE_INTERFACE_VERSION, &mdlcache },
  137. { AVI_INTERFACE_VERSION, &g_pAVI },
  138. { BIK_INTERFACE_VERSION, &g_pBIK },
  139. { DMEMAKEFILE_UTILS_INTERFACE_VERSION, &g_pDmeMakefileUtils },
  140. { VPHYSICS_COLLISION_INTERFACE_VERSION, &g_pPhysicsCollision },
  141. { SOUNDEMITTERSYSTEM_INTERFACE_VERSION, &g_pSoundEmitterSystem },
  142. { MESHSYSTEM_INTERFACE_VERSION, &g_pMeshSystem },
  143. { RENDER_DEVICE_INTERFACE_VERSION, &g_pRenderDevice },
  144. { RENDER_HARDWARECONFIG_INTERFACE_VERSION, &g_pRenderHardwareConfig },
  145. { SCENESYSTEM_INTERFACE_VERSION, &g_pSceneSystem },
  146. { WORLD_RENDERER_MGR_INTERFACE_VERSION, &g_pWorldRendererMgr },
  147. { RENDER_SYSTEM_SURFACE_INTERFACE_VERSION, &g_pVGuiRenderSurface },
  148. #if defined( _X360 )
  149. { XBOXINSTALLER_INTERFACE_VERSION, &g_pXboxInstaller },
  150. #endif
  151. { MATCHFRAMEWORK_INTERFACE_VERSION, &g_pMatchFramework },
  152. { GAMEUISYSTEMMGR_INTERFACE_VERSION, &g_pGameUISystemMgr },
  153. #if defined( INCLUDE_SCALEFORM )
  154. { SCALEFORMUI_INTERFACE_VERSION, &g_pScaleformUI },
  155. #endif
  156. };
  157. //-----------------------------------------------------------------------------
  158. // The # of times this DLL has been connected
  159. //-----------------------------------------------------------------------------
  160. static int s_nConnectionCount = 0;
  161. //-----------------------------------------------------------------------------
  162. // At each level of connection, we're going to keep track of which interfaces
  163. // we filled in. When we disconnect, we'll clear those interface pointers out.
  164. //-----------------------------------------------------------------------------
  165. struct ConnectionRegistration_t
  166. {
  167. void *m_ppGlobalStorage;
  168. int m_nConnectionPhase;
  169. };
  170. static int s_nRegistrationCount = 0;
  171. static ConnectionRegistration_t s_pConnectionRegistration[ ARRAYSIZE(g_pInterfaceGlobals) + 1 ];
  172. void RegisterInterface( CreateInterfaceFn factory, const char *pInterfaceName, void **ppGlobal )
  173. {
  174. if ( !(*ppGlobal) )
  175. {
  176. *ppGlobal = factory( pInterfaceName, NULL );
  177. if ( *ppGlobal )
  178. {
  179. Assert( s_nRegistrationCount < ARRAYSIZE(s_pConnectionRegistration) );
  180. ConnectionRegistration_t &reg = s_pConnectionRegistration[s_nRegistrationCount++];
  181. reg.m_ppGlobalStorage = ppGlobal;
  182. reg.m_nConnectionPhase = s_nConnectionCount;
  183. }
  184. }
  185. }
  186. void ReconnectInterface( CreateInterfaceFn factory, const char *pInterfaceName, void **ppGlobal )
  187. {
  188. *ppGlobal = factory( pInterfaceName, NULL );
  189. bool bFound = false;
  190. Assert( s_nRegistrationCount < ARRAYSIZE(s_pConnectionRegistration) );
  191. for ( int i = 0; i < s_nRegistrationCount; ++i )
  192. {
  193. ConnectionRegistration_t &reg = s_pConnectionRegistration[i];
  194. if ( reg.m_ppGlobalStorage != ppGlobal )
  195. continue;
  196. reg.m_ppGlobalStorage = ppGlobal;
  197. bFound = true;
  198. }
  199. if ( !bFound && *ppGlobal )
  200. {
  201. Assert( s_nRegistrationCount < ARRAYSIZE(s_pConnectionRegistration) );
  202. ConnectionRegistration_t &reg = s_pConnectionRegistration[s_nRegistrationCount++];
  203. reg.m_ppGlobalStorage = ppGlobal;
  204. reg.m_nConnectionPhase = s_nConnectionCount;
  205. }
  206. }
  207. //-----------------------------------------------------------------------------
  208. // Call this to connect to all tier 1 libraries.
  209. // It's up to the caller to check the globals it cares about to see if ones are missing
  210. //-----------------------------------------------------------------------------
  211. void ConnectInterfaces( CreateInterfaceFn *pFactoryList, int nFactoryCount )
  212. {
  213. if ( s_nRegistrationCount < 0 )
  214. {
  215. Error( "APPSYSTEM: In ConnectInterfaces(), s_nRegistrationCount is %d!\n", s_nRegistrationCount );
  216. }
  217. else if ( s_nRegistrationCount == 0 )
  218. {
  219. for ( int i = 0; i < nFactoryCount; ++i )
  220. {
  221. for ( int j = 0; j < ARRAYSIZE( g_pInterfaceGlobals ); ++j )
  222. {
  223. RegisterInterface( pFactoryList[i], g_pInterfaceGlobals[j].m_pInterfaceName, (void**)g_pInterfaceGlobals[j].m_ppGlobal );
  224. }
  225. }
  226. }
  227. else
  228. {
  229. // This is no longer questionable: ConnectInterfaces() is expected to be called multiple times for a file that exports multiple interfaces.
  230. // Warning("APPSYSTEM: ConnectInterfaces() was called twice for the same DLL.\nThis is expected behavior in building reslists, but questionable otherwise.\n");
  231. for ( int i = 0; i < nFactoryCount; ++i )
  232. {
  233. for ( int j = 0; j < ARRAYSIZE( g_pInterfaceGlobals ); ++j )
  234. {
  235. ReconnectInterface( pFactoryList[i], g_pInterfaceGlobals[j].m_pInterfaceName, (void**)g_pInterfaceGlobals[j].m_ppGlobal );
  236. }
  237. }
  238. }
  239. ++s_nConnectionCount;
  240. }
  241. void DisconnectInterfaces()
  242. {
  243. Assert( s_nConnectionCount > 0 );
  244. if ( --s_nConnectionCount < 0 )
  245. return;
  246. for ( int i = 0; i < s_nRegistrationCount; ++i )
  247. {
  248. if ( s_pConnectionRegistration[i].m_nConnectionPhase != s_nConnectionCount )
  249. continue;
  250. // Disconnect!
  251. *(void**)(s_pConnectionRegistration[i].m_ppGlobalStorage) = 0;
  252. }
  253. }
  254. //-----------------------------------------------------------------------------
  255. // Reloads an interface
  256. //-----------------------------------------------------------------------------
  257. void ReconnectInterface( CreateInterfaceFn factory, const char *pInterfaceName )
  258. {
  259. for ( int i = 0; i < ARRAYSIZE( g_pInterfaceGlobals ); ++i )
  260. {
  261. if ( strcmp( g_pInterfaceGlobals[i].m_pInterfaceName, pInterfaceName ) )
  262. continue;
  263. ReconnectInterface( factory, g_pInterfaceGlobals[i].m_pInterfaceName, (void**)g_pInterfaceGlobals[i].m_ppGlobal );
  264. }
  265. }