summaryrefslogtreecommitdiff
path: root/sdklauncher/ModWizard_GetModInfo.cpp
blob: dbfa769ecab1fbb85dde9df9fceb6450d96985c5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================

#include "ModWizard_GetModInfo.h"
#include <vgui_controls/DirectorySelectDialog.h>
#include <vgui/IInput.h>
#include <vgui_controls/Button.h>
#include <vgui_controls/WizardPanel.h>
#include "sdklauncher_main.h"
#include <ctype.h>
#include "CreateModWizard.h"
#include "ModWizard_CopyFiles.h"


using namespace vgui;


extern char g_engineDir[50];

// ---------------------------------------------------------------------------------------- //
// Directory select dialog that preserves the modalness of the previous dialog.
// ---------------------------------------------------------------------------------------- //

class CModalPreserveDirectorySelectDialog : public DirectorySelectDialog
{
public:
	typedef vgui::DirectorySelectDialog BaseClass;

	CModalPreserveDirectorySelectDialog(vgui::Panel *pParent, const char *title)
		: BaseClass( pParent, title )
	{
		m_PrevAppFocusPanel = vgui::input()->GetAppModalSurface();
		vgui::input()->SetAppModalSurface( GetVPanel() );
	}

	~CModalPreserveDirectorySelectDialog()
	{
		vgui::input()->SetAppModalSurface( m_PrevAppFocusPanel );
	}


public:
	vgui::VPANEL m_PrevAppFocusPanel;
};
		


CModWizardSubPanel_GetModInfo::CModWizardSubPanel_GetModInfo( Panel *parent, const char *panelName )
	: BaseClass( parent, panelName )
{
	m_pModPath = new vgui::TextEntry( this, "ModPath" );
	m_pModName = new vgui::TextEntry( this, "ModName" );
	new vgui::Button( this, "SearchButton", "", this, "SearchButton" );

	m_pModNameInfoLabel = new Label( this, "ModNameInfoLabel", "" );

	LoadControlSettings( "ModWizardSubPanel_GetModInfo.res");

	if ( g_bModWizard_CmdLineFields )
	{
		m_pModPath->SetText( g_ModWizard_CmdLine_ModDir );
		m_pModPath->SetEditable( false );
		m_pModPath->SetEnabled( false );

		m_pModName->SetText( g_ModWizard_CmdLine_ModName );
		m_pModName->SetEditable( false );
		m_pModName->SetEnabled( false );
	}
}								  

WizardSubPanel *CModWizardSubPanel_GetModInfo::GetNextSubPanel()
{
	// In scratch/template, go to the template options panel - orange box only!
	if ( m_ModType == ModType_FromScratch && !V_strcmp( g_engineDir, "source2007" ) )
		return dynamic_cast<WizardSubPanel *>(GetWizardPanel()->FindChildByName("CModWizardSubPanel_TemplateOptions"));
	else
		return dynamic_cast<WizardSubPanel *>(GetWizardPanel()->FindChildByName("CModWizardSubPanel_CopyFiles"));
}

void CModWizardSubPanel_GetModInfo::PerformLayout()
{
	BaseClass::PerformLayout();
	GetWizardPanel()->SetFinishButtonEnabled(false);
}

void CModWizardSubPanel_GetModInfo::OnDisplayAsNext()
{
	GetWizardPanel()->SetTitle( "#ModWizard_GetModInfo_Title", true );
}

bool CModWizardSubPanel_GetModInfo::OnNextButton()
{
	char modPath[512];	
	m_pModPath->GetText( modPath, sizeof( modPath ) );
	Q_AppendSlash( modPath, sizeof( modPath ) );

	char modName[512];
	m_pModName->GetText( modName, sizeof( modName ) );

	
	// Validate the path they passed in.
	if ( modPath[0] == 0 )
	{
		VGUIMessageBox( GetWizardPanel(), "Error", "#PleaseEnterModPath" );
		return false;
	}
	else if ( !Q_IsAbsolutePath( modPath ) )
	{
		VGUIMessageBox( this, "Error", "Please enter a full path (like C:\\MyMod) to install the mod." );
		return false;
	}

	// Validate the mod name.
	char outputModGamedirName[MAX_PATH];

	if ( m_ModType == ModType_SourceCodeOnly )
	{
		outputModGamedirName[0] = 0;
		modName[0] = 0;
	}
	else
	{
		if ( modName[0] == 0 )
		{
			VGUIMessageBox( this, "Error", "#PleaseEnterModName" );
			m_pModName->RequestFocus();
			return false;
		}
		int modNameLen = strlen( modName );
		for ( int i=0; i < modNameLen; i++ )
		{
			if ( !isalnum( modName[i] ) && modName[i] != '-' && modName[i] != '_' && modName[i] != ' ' )
			{
				VGUIMessageBox( this, "Error", "#ModNameInvalidCharacters" );
				return false;
			}
		}

		//Tony; after the other check for invalid characters, make the actual outputModGamedirName lowercase with spaces stripped out.
		char strippedModName[512]; //this is what gets thrown on below!
		//Tony; reuse modNameLen when I copy it to the new string.
		//I could have sworn there was an easier way to do this, but it completely escapes me.
		int i,j;
		modNameLen = strlen( modName );
		for (i=0, j=0;i < modNameLen; i++ )
		{
			 if (modName[i] == ' ') continue;
			 strippedModName[j++] = modName[i];
		}
		strippedModName[j] = '\0'; //Null terminate. 

//		Q_strncpy( strippedModName, ModName, sizeof( strippedModName ) );
		Q_strlower( strippedModName ); //Tony; convert it to lower case


		
		// Build the name of the content directory (c:\steam\steamapps\sourcemods\modname).
		Q_strncpy( outputModGamedirName, GetSDKLauncherBaseDirectory(), sizeof( outputModGamedirName ) );		// steamapps\username\sourcesdk

		Q_StripLastDir( outputModGamedirName, sizeof( outputModGamedirName ) );									// steamapps\username
		Q_StripLastDir( outputModGamedirName, sizeof( outputModGamedirName ) );									// steamapps
		Q_AppendSlash( outputModGamedirName, sizeof( outputModGamedirName ) );									
		Q_strncat( outputModGamedirName, "SourceMods", sizeof( outputModGamedirName ), COPY_ALL_CHARACTERS );	// steamapps\sourcemods
		Q_AppendSlash( outputModGamedirName, sizeof( outputModGamedirName ) );									
		Q_strncat( outputModGamedirName, strippedModName, sizeof( outputModGamedirName ), COPY_ALL_CHARACTERS );		// steamapps\sourcemods\modname
		Q_AppendSlash( outputModGamedirName, sizeof( outputModGamedirName ) );

		if ( !CreateFullDirectory( outputModGamedirName ) ||
			 !CreateSubdirectory( outputModGamedirName, "resource" ) ||
			 !CreateSubdirectory( outputModGamedirName, "resource\\ui" ) ||
			 !CreateSubdirectory( outputModGamedirName, "maps" ) ||
			 !CreateSubdirectory( outputModGamedirName, "cfg" ) ||
			 !CreateSubdirectory( outputModGamedirName, "scripts" )
			 )
		{
			VGUIMessageBox( this, "Error", "Unable to create directory '%s' or one if its subdirectories.", modPath );
			return false;
		}
	}

	
	// Setup all the data for the copy panel.
	CModWizardSubPanel_CopyFiles *pCopyPanel = dynamic_cast<CModWizardSubPanel_CopyFiles *>(GetWizardPanel()->FindChildByName("CModWizardSubPanel_CopyFiles"));
	if ( !pCopyPanel )
	{
		VGUIMessageBox( this, "Error", "Can't find copy panel!" );
		return false;			
	}
	
	pCopyPanel->GetReady( modPath, outputModGamedirName, modName );
	return true;
}

void CModWizardSubPanel_GetModInfo::OnCommand( const char *command )
{
	if ( Q_stricmp( command, "SearchButton" ) == 0 )
	{
		CModalPreserveDirectorySelectDialog *pDlg = vgui::SETUP_PANEL( new CModalPreserveDirectorySelectDialog( this, "#SelectInstallDirectory" ) );
		pDlg->SetStartDirectory( "C:\\" );
		char szPath[MAX_PATH];
		m_pModPath->GetText( szPath, sizeof(szPath) );
		pDlg->ExpandTreeToPath( szPath );
		pDlg->SetDefaultCreateDirectoryName( "MyMod" );
		pDlg->MoveToCenterOfScreen();
		pDlg->DoModal();
		pDlg->AddActionSignalTarget( this );
	}

	BaseClass::OnCommand( command );
}

void CModWizardSubPanel_GetModInfo::OnChooseDirectory( const char *dir )
{
	m_pModPath->SetText( dir );
}