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.

362 lines
11 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "VGuiSystemModuleLoader.h"
  8. #include "Sys_Utils.h"
  9. #include "IVGuiModule.h"
  10. #include "ServerBrowser/IServerBrowser.h"
  11. #include <vgui/IPanel.h>
  12. #include <vgui/ISystem.h>
  13. #include <vgui/IVGui.h>
  14. #include <vgui/ILocalize.h>
  15. #include <KeyValues.h>
  16. #include <vgui_controls/Controls.h>
  17. #include <vgui_controls/Panel.h>
  18. #include "FileSystem.h"
  19. // memdbgon must be the last include file in a .cpp file!!!
  20. #include "tier0/memdbgon.h"
  21. // instance of class
  22. CVGuiSystemModuleLoader g_VModuleLoader;
  23. extern vgui::VPANEL GetGameUIBasePanel();
  24. bool bSteamCommunityFriendsVersion = false;
  25. #include <tier0/dbg.h>
  26. // memdbgon must be the last include file in a .cpp file!!!
  27. #include <tier0/memdbgon.h>
  28. EXPOSE_SINGLE_INTERFACE_GLOBALVAR(CVGuiSystemModuleLoader, IVGuiModuleLoader, VGUIMODULELOADER_INTERFACE_VERSION, g_VModuleLoader);
  29. //-----------------------------------------------------------------------------
  30. // Purpose: Constructor
  31. //-----------------------------------------------------------------------------
  32. CVGuiSystemModuleLoader::CVGuiSystemModuleLoader()
  33. {
  34. m_bModulesInitialized = false;
  35. m_bPlatformShouldRestartAfterExit = false;
  36. m_pPlatformModuleData = NULL;
  37. }
  38. //-----------------------------------------------------------------------------
  39. // Purpose: Destructor
  40. //-----------------------------------------------------------------------------
  41. CVGuiSystemModuleLoader::~CVGuiSystemModuleLoader()
  42. {
  43. }
  44. //-----------------------------------------------------------------------------
  45. // Purpose: returns true if the module loader has acquired the platform mutex and loaded the modules
  46. //-----------------------------------------------------------------------------
  47. bool CVGuiSystemModuleLoader::IsPlatformReady()
  48. {
  49. return m_bModulesInitialized;
  50. }
  51. //-----------------------------------------------------------------------------
  52. // Purpose: sets up all the modules for use
  53. //-----------------------------------------------------------------------------
  54. bool CVGuiSystemModuleLoader::InitializeAllModules(CreateInterfaceFn *factorylist, int factorycount)
  55. {
  56. if ( IsGameConsole() )
  57. {
  58. // not valid for 360
  59. return false;
  60. }
  61. bool bSuccess = true;
  62. // Init vgui in the modules
  63. int i;
  64. for ( i = 0; i < m_Modules.Count(); i++ )
  65. {
  66. if (!m_Modules[i].moduleInterface->Initialize(factorylist, factorycount))
  67. {
  68. bSuccess = false;
  69. Error("Platform Error: module failed to initialize\n");
  70. }
  71. }
  72. // create a table of all the loaded modules
  73. CreateInterfaceFn *moduleFactories = (CreateInterfaceFn *)_alloca(sizeof(CreateInterfaceFn) * m_Modules.Count());
  74. for ( i = 0; i < m_Modules.Count(); i++ )
  75. {
  76. moduleFactories[i] = Sys_GetFactory(m_Modules[i].module);
  77. }
  78. // give the modules a chance to link themselves together
  79. for (i = 0; i < m_Modules.Count(); i++)
  80. {
  81. if (!m_Modules[i].moduleInterface->PostInitialize(moduleFactories, m_Modules.Count()))
  82. {
  83. bSuccess = false;
  84. Error("Platform Error: module failed to initialize\n");
  85. }
  86. m_Modules[i].moduleInterface->SetParent(GetGameUIBasePanel());
  87. }
  88. m_bModulesInitialized = true;
  89. return bSuccess;
  90. }
  91. //-----------------------------------------------------------------------------
  92. // Purpose: Loads and initializes all the modules specified in the platform file
  93. //-----------------------------------------------------------------------------
  94. bool CVGuiSystemModuleLoader::LoadPlatformModules(CreateInterfaceFn *factorylist, int factorycount, bool useSteamModules)
  95. {
  96. if ( IsGameConsole() )
  97. {
  98. // not valid for 360
  99. return false;
  100. }
  101. bool bSuccess = true;
  102. // load platform menu
  103. KeyValues *kv = new KeyValues("Platform");
  104. if (!kv->LoadFromFile(g_pFullFileSystem, "steam/games/PlatformMenu.vdf", "PLATFORM"))
  105. {
  106. kv->deleteThis();
  107. return false;
  108. }
  109. // walk the platform menu loading all the interfaces
  110. KeyValues *menuKeys = kv->FindKey("Menu", true);
  111. for (KeyValues *it = menuKeys->GetFirstSubKey(); it != NULL; it = it->GetNextKey())
  112. {
  113. // see if we should skip steam modules
  114. if (!useSteamModules && it->GetInt("SteamApp"))
  115. continue;
  116. const char *pchInterface = it->GetString("interface");
  117. // don't load friends if we are using Steam Community
  118. if ( !Q_stricmp( pchInterface, "VGuiModuleTracker001" ) && bSteamCommunityFriendsVersion )
  119. continue;
  120. // get copy out of steam cache
  121. const char *dllPath = it->GetString("dll");
  122. // load the module (LoadModule calls GetLocalCopy() under steam)
  123. CSysModule *mod = g_pFullFileSystem->LoadModule(dllPath, "EXECUTABLE_PATH");
  124. if (!mod)
  125. {
  126. Error("Platform Error: bad module '%s', not loading\n", it->GetString("dll"));
  127. bSuccess = false;
  128. continue;
  129. }
  130. // make sure we get the right version
  131. IVGuiModule *moduleInterface = (IVGuiModule *)Sys_GetFactory(mod)(pchInterface, NULL);
  132. if (!moduleInterface)
  133. {
  134. Warning("Platform Error: module version ('%s, %s) invalid, not loading\n", it->GetString("dll"), it->GetString("interface"));
  135. bSuccess = false;
  136. continue;
  137. }
  138. // store off the module
  139. int newIndex = m_Modules.AddToTail();
  140. m_Modules[newIndex].module = mod;
  141. m_Modules[newIndex].moduleInterface = moduleInterface;
  142. m_Modules[newIndex].data = it;
  143. }
  144. m_pPlatformModuleData = kv;
  145. return InitializeAllModules(factorylist, factorycount) && bSuccess;
  146. }
  147. //-----------------------------------------------------------------------------
  148. // Purpose: gives all platform modules a chance to Shutdown gracefully
  149. //-----------------------------------------------------------------------------
  150. void CVGuiSystemModuleLoader::ShutdownPlatformModules()
  151. {
  152. if ( IsGameConsole() )
  153. {
  154. // not valid for 360
  155. return;
  156. }
  157. // static include guard to prevent recursive calls
  158. static bool runningFunction = false;
  159. if (runningFunction)
  160. return;
  161. runningFunction = true;
  162. // deactivate all the modules first
  163. DeactivatePlatformModules();
  164. // give all the modules notice of quit
  165. int i;
  166. for ( i = 0; i < m_Modules.Count(); i++ )
  167. {
  168. vgui::ivgui()->PostMessage(m_Modules[i].moduleInterface->GetPanel(), new KeyValues("Command", "command", "Quit"), NULL);
  169. }
  170. for ( i = 0; i < m_Modules.Count(); i++ )
  171. {
  172. m_Modules[i].moduleInterface->Shutdown();
  173. }
  174. runningFunction = false;
  175. }
  176. //-----------------------------------------------------------------------------
  177. // Purpose: Deactivates all the modules (puts them into in inactive but recoverable state)
  178. //-----------------------------------------------------------------------------
  179. void CVGuiSystemModuleLoader::DeactivatePlatformModules()
  180. {
  181. for (int i = 0; i < m_Modules.Count(); i++)
  182. {
  183. m_Modules[i].moduleInterface->Deactivate();
  184. }
  185. }
  186. //-----------------------------------------------------------------------------
  187. // Purpose: Reenables all the deactivated platform modules
  188. //-----------------------------------------------------------------------------
  189. void CVGuiSystemModuleLoader::ReactivatePlatformModules()
  190. {
  191. for (int i = 0; i < m_Modules.Count(); i++)
  192. {
  193. m_Modules[i].moduleInterface->Reactivate();
  194. }
  195. }
  196. //-----------------------------------------------------------------------------
  197. // Purpose: Disables and unloads platform
  198. //-----------------------------------------------------------------------------
  199. void CVGuiSystemModuleLoader::UnloadPlatformModules()
  200. {
  201. for (int i = 0; i < m_Modules.Count(); i++)
  202. {
  203. g_pFullFileSystem->UnloadModule(m_Modules[i].module);
  204. }
  205. m_Modules.RemoveAll();
  206. if (m_pPlatformModuleData)
  207. {
  208. m_pPlatformModuleData->deleteThis();
  209. m_pPlatformModuleData = NULL;
  210. }
  211. }
  212. //-----------------------------------------------------------------------------
  213. // Purpose: Called every frame
  214. //-----------------------------------------------------------------------------
  215. void CVGuiSystemModuleLoader::RunFrame()
  216. {
  217. }
  218. //-----------------------------------------------------------------------------
  219. // Purpose: returns number of modules loaded
  220. //-----------------------------------------------------------------------------
  221. int CVGuiSystemModuleLoader::GetModuleCount()
  222. {
  223. return m_Modules.Count();
  224. }
  225. //-----------------------------------------------------------------------------
  226. // Purpose: returns the string menu name (unlocalized) of a module
  227. // moduleIndex is of the range [0, GetModuleCount())
  228. //-----------------------------------------------------------------------------
  229. const char *CVGuiSystemModuleLoader::GetModuleLabel(int moduleIndex)
  230. {
  231. return m_Modules[moduleIndex].data->GetString("MenuName", "< unknown >");
  232. }
  233. //-----------------------------------------------------------------------------
  234. // Purpose:
  235. //-----------------------------------------------------------------------------
  236. bool CVGuiSystemModuleLoader::IsModuleVisible(int moduleIndex)
  237. {
  238. return vgui::ipanel()->IsVisible( m_Modules[moduleIndex].moduleInterface->GetPanel() );
  239. }
  240. //-----------------------------------------------------------------------------
  241. // Purpose: brings the specified module to the foreground
  242. //-----------------------------------------------------------------------------
  243. bool CVGuiSystemModuleLoader::IsModuleHidden(int moduleIndex)
  244. {
  245. return m_Modules[moduleIndex].data->GetBool("Hidden", false);
  246. }
  247. //-----------------------------------------------------------------------------
  248. // Purpose: brings the specified module to the foreground
  249. //-----------------------------------------------------------------------------
  250. bool CVGuiSystemModuleLoader::ActivateModule(int moduleIndex)
  251. {
  252. if (!m_Modules.IsValidIndex(moduleIndex))
  253. return false;
  254. m_Modules[moduleIndex].moduleInterface->Activate();
  255. return true;
  256. }
  257. //-----------------------------------------------------------------------------
  258. // Purpose: activates a module by name
  259. //-----------------------------------------------------------------------------
  260. bool CVGuiSystemModuleLoader::ActivateModule(const char *moduleName)
  261. {
  262. for (int i = 0; i < GetModuleCount(); i++)
  263. {
  264. if (!stricmp(GetModuleLabel(i), moduleName) || !stricmp(m_Modules[i].data->GetName(), moduleName))
  265. {
  266. ActivateModule(i);
  267. return true;
  268. }
  269. }
  270. return false;
  271. }
  272. //-----------------------------------------------------------------------------
  273. // Purpose: returns a modules interface factory
  274. //-----------------------------------------------------------------------------
  275. CreateInterfaceFn CVGuiSystemModuleLoader::GetModuleFactory(int moduleIndex)
  276. {
  277. return Sys_GetFactory(m_Modules[moduleIndex].module);
  278. }
  279. //-----------------------------------------------------------------------------
  280. // Purpose:
  281. //-----------------------------------------------------------------------------
  282. void CVGuiSystemModuleLoader::PostMessageToAllModules(KeyValues *message)
  283. {
  284. for (int i = 0; i < m_Modules.Count(); i++)
  285. {
  286. vgui::ivgui()->PostMessage(m_Modules[i].moduleInterface->GetPanel(), message->MakeCopy(), NULL);
  287. }
  288. message->deleteThis();
  289. }
  290. //-----------------------------------------------------------------------------
  291. // Purpose: sets the the platform should update and restart when it quits
  292. //-----------------------------------------------------------------------------
  293. void CVGuiSystemModuleLoader::SetPlatformToRestart()
  294. {
  295. m_bPlatformShouldRestartAfterExit = true;
  296. }
  297. //-----------------------------------------------------------------------------
  298. // Purpose: data accessor
  299. //-----------------------------------------------------------------------------
  300. bool CVGuiSystemModuleLoader::ShouldPlatformRestart()
  301. {
  302. return m_bPlatformShouldRestartAfterExit;
  303. }