Team Fortress 2 Source Code as on 22/4/2020
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.

161 lines
4.2 KiB

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