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.

219 lines
7.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "ModWizard_GetModInfo.h"
  7. #include <vgui_controls/DirectorySelectDialog.h>
  8. #include <vgui/IInput.h>
  9. #include <vgui_controls/Button.h>
  10. #include <vgui_controls/WizardPanel.h>
  11. #include "sdklauncher_main.h"
  12. #include <ctype.h>
  13. #include "CreateModWizard.h"
  14. #include "ModWizard_CopyFiles.h"
  15. using namespace vgui;
  16. extern char g_engineDir[50];
  17. // ---------------------------------------------------------------------------------------- //
  18. // Directory select dialog that preserves the modalness of the previous dialog.
  19. // ---------------------------------------------------------------------------------------- //
  20. class CModalPreserveDirectorySelectDialog : public DirectorySelectDialog
  21. {
  22. public:
  23. typedef vgui::DirectorySelectDialog BaseClass;
  24. CModalPreserveDirectorySelectDialog(vgui::Panel *pParent, const char *title)
  25. : BaseClass( pParent, title )
  26. {
  27. m_PrevAppFocusPanel = vgui::input()->GetAppModalSurface();
  28. vgui::input()->SetAppModalSurface( GetVPanel() );
  29. }
  30. ~CModalPreserveDirectorySelectDialog()
  31. {
  32. vgui::input()->SetAppModalSurface( m_PrevAppFocusPanel );
  33. }
  34. public:
  35. vgui::VPANEL m_PrevAppFocusPanel;
  36. };
  37. CModWizardSubPanel_GetModInfo::CModWizardSubPanel_GetModInfo( Panel *parent, const char *panelName )
  38. : BaseClass( parent, panelName )
  39. {
  40. m_pModPath = new vgui::TextEntry( this, "ModPath" );
  41. m_pModName = new vgui::TextEntry( this, "ModName" );
  42. new vgui::Button( this, "SearchButton", "", this, "SearchButton" );
  43. m_pModNameInfoLabel = new Label( this, "ModNameInfoLabel", "" );
  44. LoadControlSettings( "ModWizardSubPanel_GetModInfo.res");
  45. if ( g_bModWizard_CmdLineFields )
  46. {
  47. m_pModPath->SetText( g_ModWizard_CmdLine_ModDir );
  48. m_pModPath->SetEditable( false );
  49. m_pModPath->SetEnabled( false );
  50. m_pModName->SetText( g_ModWizard_CmdLine_ModName );
  51. m_pModName->SetEditable( false );
  52. m_pModName->SetEnabled( false );
  53. }
  54. }
  55. WizardSubPanel *CModWizardSubPanel_GetModInfo::GetNextSubPanel()
  56. {
  57. // In scratch/template, go to the template options panel - orange box only!
  58. if ( m_ModType == ModType_FromScratch && !V_strcmp( g_engineDir, "source2007" ) )
  59. return dynamic_cast<WizardSubPanel *>(GetWizardPanel()->FindChildByName("CModWizardSubPanel_TemplateOptions"));
  60. else
  61. return dynamic_cast<WizardSubPanel *>(GetWizardPanel()->FindChildByName("CModWizardSubPanel_CopyFiles"));
  62. }
  63. void CModWizardSubPanel_GetModInfo::PerformLayout()
  64. {
  65. BaseClass::PerformLayout();
  66. GetWizardPanel()->SetFinishButtonEnabled(false);
  67. }
  68. void CModWizardSubPanel_GetModInfo::OnDisplayAsNext()
  69. {
  70. GetWizardPanel()->SetTitle( "#ModWizard_GetModInfo_Title", true );
  71. }
  72. bool CModWizardSubPanel_GetModInfo::OnNextButton()
  73. {
  74. char modPath[512];
  75. m_pModPath->GetText( modPath, sizeof( modPath ) );
  76. Q_AppendSlash( modPath, sizeof( modPath ) );
  77. char modName[512];
  78. m_pModName->GetText( modName, sizeof( modName ) );
  79. // Validate the path they passed in.
  80. if ( modPath[0] == 0 )
  81. {
  82. VGUIMessageBox( GetWizardPanel(), "Error", "#PleaseEnterModPath" );
  83. return false;
  84. }
  85. else if ( !Q_IsAbsolutePath( modPath ) )
  86. {
  87. VGUIMessageBox( this, "Error", "Please enter a full path (like C:\\MyMod) to install the mod." );
  88. return false;
  89. }
  90. // Validate the mod name.
  91. char outputModGamedirName[MAX_PATH];
  92. if ( m_ModType == ModType_SourceCodeOnly )
  93. {
  94. outputModGamedirName[0] = 0;
  95. modName[0] = 0;
  96. }
  97. else
  98. {
  99. if ( modName[0] == 0 )
  100. {
  101. VGUIMessageBox( this, "Error", "#PleaseEnterModName" );
  102. m_pModName->RequestFocus();
  103. return false;
  104. }
  105. int modNameLen = strlen( modName );
  106. for ( int i=0; i < modNameLen; i++ )
  107. {
  108. if ( !isalnum( modName[i] ) && modName[i] != '-' && modName[i] != '_' && modName[i] != ' ' )
  109. {
  110. VGUIMessageBox( this, "Error", "#ModNameInvalidCharacters" );
  111. return false;
  112. }
  113. }
  114. //Tony; after the other check for invalid characters, make the actual outputModGamedirName lowercase with spaces stripped out.
  115. char strippedModName[512]; //this is what gets thrown on below!
  116. //Tony; reuse modNameLen when I copy it to the new string.
  117. //I could have sworn there was an easier way to do this, but it completely escapes me.
  118. int i,j;
  119. modNameLen = strlen( modName );
  120. for (i=0, j=0;i < modNameLen; i++ )
  121. {
  122. if (modName[i] == ' ') continue;
  123. strippedModName[j++] = modName[i];
  124. }
  125. strippedModName[j] = '\0'; //Null terminate.
  126. // Q_strncpy( strippedModName, ModName, sizeof( strippedModName ) );
  127. Q_strlower( strippedModName ); //Tony; convert it to lower case
  128. // Build the name of the content directory (c:\steam\steamapps\sourcemods\modname).
  129. Q_strncpy( outputModGamedirName, GetSDKLauncherBaseDirectory(), sizeof( outputModGamedirName ) ); // steamapps\username\sourcesdk
  130. Q_StripLastDir( outputModGamedirName, sizeof( outputModGamedirName ) ); // steamapps\username
  131. Q_StripLastDir( outputModGamedirName, sizeof( outputModGamedirName ) ); // steamapps
  132. Q_AppendSlash( outputModGamedirName, sizeof( outputModGamedirName ) );
  133. Q_strncat( outputModGamedirName, "SourceMods", sizeof( outputModGamedirName ), COPY_ALL_CHARACTERS ); // steamapps\sourcemods
  134. Q_AppendSlash( outputModGamedirName, sizeof( outputModGamedirName ) );
  135. Q_strncat( outputModGamedirName, strippedModName, sizeof( outputModGamedirName ), COPY_ALL_CHARACTERS ); // steamapps\sourcemods\modname
  136. Q_AppendSlash( outputModGamedirName, sizeof( outputModGamedirName ) );
  137. if ( !CreateFullDirectory( outputModGamedirName ) ||
  138. !CreateSubdirectory( outputModGamedirName, "resource" ) ||
  139. !CreateSubdirectory( outputModGamedirName, "resource\\ui" ) ||
  140. !CreateSubdirectory( outputModGamedirName, "maps" ) ||
  141. !CreateSubdirectory( outputModGamedirName, "cfg" ) ||
  142. !CreateSubdirectory( outputModGamedirName, "scripts" )
  143. )
  144. {
  145. VGUIMessageBox( this, "Error", "Unable to create directory '%s' or one if its subdirectories.", modPath );
  146. return false;
  147. }
  148. }
  149. // Setup all the data for the copy panel.
  150. CModWizardSubPanel_CopyFiles *pCopyPanel = dynamic_cast<CModWizardSubPanel_CopyFiles *>(GetWizardPanel()->FindChildByName("CModWizardSubPanel_CopyFiles"));
  151. if ( !pCopyPanel )
  152. {
  153. VGUIMessageBox( this, "Error", "Can't find copy panel!" );
  154. return false;
  155. }
  156. pCopyPanel->GetReady( modPath, outputModGamedirName, modName );
  157. return true;
  158. }
  159. void CModWizardSubPanel_GetModInfo::OnCommand( const char *command )
  160. {
  161. if ( Q_stricmp( command, "SearchButton" ) == 0 )
  162. {
  163. CModalPreserveDirectorySelectDialog *pDlg = vgui::SETUP_PANEL( new CModalPreserveDirectorySelectDialog( this, "#SelectInstallDirectory" ) );
  164. pDlg->SetStartDirectory( "C:\\" );
  165. char szPath[MAX_PATH];
  166. m_pModPath->GetText( szPath, sizeof(szPath) );
  167. pDlg->ExpandTreeToPath( szPath );
  168. pDlg->SetDefaultCreateDirectoryName( "MyMod" );
  169. pDlg->MoveToCenterOfScreen();
  170. pDlg->DoModal();
  171. pDlg->AddActionSignalTarget( this );
  172. }
  173. BaseClass::OnCommand( command );
  174. }
  175. void CModWizardSubPanel_GetModInfo::OnChooseDirectory( const char *dir )
  176. {
  177. m_pModPath->SetText( dir );
  178. }