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.

159 lines
4.2 KiB

  1. //========= Copyright 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #undef fopen
  8. #if !defined( _GAMECONSOLE ) && !defined( _OSX ) && !defined (LINUX)
  9. #include <windows.h>
  10. #endif
  11. #include <stdio.h>
  12. #include "changegamedialog.h"
  13. #include "modinfo.h"
  14. #include "engineinterface.h"
  15. #include <vgui_controls/ListPanel.h>
  16. #include <keyvalues.h>
  17. // memdbgon must be the last include file in a .cpp file!!!
  18. #include <tier0/memdbgon.h>
  19. using namespace vgui;
  20. //-----------------------------------------------------------------------------
  21. // Purpose: Constructor
  22. //-----------------------------------------------------------------------------
  23. CChangeGameDialog::CChangeGameDialog(vgui::Panel *parent) : Frame(parent, "ChangeGameDialog")
  24. {
  25. SetSize(400, 340);
  26. SetMinimumSize(400, 340);
  27. SetTitle("#GameUI_ChangeGame", true);
  28. m_pModList = new ListPanel(this, "ModList");
  29. m_pModList->SetEmptyListText("#GameUI_NoOtherGamesAvailable");
  30. m_pModList->AddColumnHeader(0, "ModName", "#GameUI_Game", 128);
  31. LoadModList();
  32. LoadControlSettings("Resource/ChangeGameDialog.res");
  33. // if there's a mod in the list, select the first one
  34. if (m_pModList->GetItemCount() > 0)
  35. {
  36. m_pModList->SetSingleSelectedItem(m_pModList->GetItemIDFromRow(0));
  37. }
  38. }
  39. //-----------------------------------------------------------------------------
  40. // Purpose: Destructor
  41. //-----------------------------------------------------------------------------
  42. CChangeGameDialog::~CChangeGameDialog()
  43. {
  44. }
  45. //-----------------------------------------------------------------------------
  46. // Purpose: Fills the mod list
  47. //-----------------------------------------------------------------------------
  48. void CChangeGameDialog::LoadModList()
  49. {
  50. #if !defined( _OSX ) && !defined( _PS3 ) && !defined(LINUX)
  51. // look for third party games
  52. char szSearchPath[_MAX_PATH + 5];
  53. Q_strncpy(szSearchPath, "*.*", sizeof( szSearchPath ) );
  54. // use local filesystem since it has to look outside path system, and will never be used under steam
  55. WIN32_FIND_DATA wfd;
  56. HANDLE hResult;
  57. memset(&wfd, 0, sizeof(WIN32_FIND_DATA));
  58. hResult = FindFirstFile( szSearchPath, &wfd);
  59. if (hResult != INVALID_HANDLE_VALUE)
  60. {
  61. BOOL bMoreFiles;
  62. while (1)
  63. {
  64. if ( (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && wfd.cFileName[ 0 ] != '.' )
  65. {
  66. // Check for dlls\*.dll
  67. char szDllDirectory[MAX_PATH + 16];
  68. Q_snprintf(szDllDirectory, sizeof( szDllDirectory ), "%s\\gameinfo.txt", wfd.cFileName);
  69. FILE *f = fopen(szDllDirectory, "rb");
  70. if (f)
  71. {
  72. // find the description
  73. fseek(f, 0, SEEK_END);
  74. unsigned int size = ftell(f);
  75. fseek(f, 0, SEEK_SET);
  76. char *buf = (char *)malloc(size + 1);
  77. if (fread(buf, 1, size, f) == size)
  78. {
  79. buf[size] = 0;
  80. CModInfo modInfo;
  81. modInfo.LoadGameInfoFromBuffer(buf);
  82. if (strcmp(modInfo.GetGameName(), ModInfo().GetGameName()))
  83. {
  84. // Add the game directory.
  85. strlwr(wfd.cFileName);
  86. KeyValues *itemData = new KeyValues("Mod");
  87. itemData->SetString("ModName", modInfo.GetGameName());
  88. itemData->SetString("ModDir", wfd.cFileName);
  89. m_pModList->AddItem(itemData, 0, false, false);
  90. }
  91. }
  92. free(buf);
  93. fclose(f);
  94. }
  95. }
  96. bMoreFiles = FindNextFile(hResult, &wfd);
  97. if (!bMoreFiles)
  98. break;
  99. }
  100. FindClose(hResult);
  101. }
  102. #endif // !_OSX && !_PS3
  103. }
  104. //-----------------------------------------------------------------------------
  105. // Purpose:
  106. //-----------------------------------------------------------------------------
  107. void CChangeGameDialog::OnCommand(const char *command)
  108. {
  109. if (!stricmp(command, "OK"))
  110. {
  111. if (m_pModList->GetSelectedItemsCount() > 0)
  112. {
  113. KeyValues *kv = m_pModList->GetItem(m_pModList->GetSelectedItem(0));
  114. if (kv)
  115. {
  116. // change the game dir and restart the engine
  117. char szCmd[256];
  118. Q_snprintf(szCmd, sizeof( szCmd ), "_setgamedir %s\n", kv->GetString("ModDir"));
  119. engine->ClientCmd_Unrestricted(szCmd);
  120. // Force restart of entire engine
  121. engine->ClientCmd_Unrestricted("_restart\n");
  122. }
  123. }
  124. }
  125. else if (!stricmp(command, "Cancel"))
  126. {
  127. Close();
  128. }
  129. else
  130. {
  131. BaseClass::OnCommand(command);
  132. }
  133. }