summaryrefslogtreecommitdiff
path: root/utils/valvelib/cstm1dlg.cpp
blob: aecf9dda25a2c419907b72196daedbc31e6450b1 (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//
//=============================================================================//
// cstm1dlg.cpp : implementation file
//

#include "stdafx.h"
#include "valvelib.h"
#include "cstm1dlg.h"
#include "valvelibaw.h"

#ifdef _PSEUDO_DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CCustom1Dlg dialog


CCustom1Dlg::CCustom1Dlg()
	: CAppWizStepDlg(CCustom1Dlg::IDD)
{
	//{{AFX_DATA_INIT(CCustom1Dlg)
	m_RootPath = _T("");
	m_TargetPath = _T("");
	m_ProjectType = -1;
	m_ToolProject = FALSE;
	m_ImplibPath = _T("");
	m_PublicProject = FALSE;
	m_ConsoleApp = FALSE;
	m_PublishImportLib = FALSE;
	m_SrcPath = _T("");
	//}}AFX_DATA_INIT

	m_ProjectType = 0;
	m_PublicProject = TRUE;
	m_SrcPath = "src";
}


void CCustom1Dlg::DoDataExchange(CDataExchange* pDX)
{
	if (pDX->m_bSaveAndValidate == 0)
	{
		// Refresh the paths based on project information...
		if (m_RootPath.GetLength() == 0)
		{
			if (!Valvelibaw.m_Dictionary.Lookup("FULL_DIR_PATH", m_RootPath))
				m_RootPath = "u:\\hl2\\";

			m_RootPath.MakeLower();

			m_ToolProject = (m_RootPath.Find( "util" ) >= 0);

			int idx = m_RootPath.Find( m_SrcPath );	// look for src tree
			if (idx >= 0)
			{
				m_RootPath = m_RootPath.Left( idx );
			}

			m_TargetPath = m_RootPath;
			m_TargetPath += m_SrcPath;
			m_TargetPath += "\\lib\\public\\";

			m_ImplibPath = "unused";
			EnableCheckboxes();
		}
	}

	CAppWizStepDlg::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCustom1Dlg)
	DDX_Text(pDX, IDC_EDIT_ROOT_PATH, m_RootPath);
	DDX_Text(pDX, IDC_EDIT_TARGET_PATH, m_TargetPath);
	DDX_CBIndex(pDX, IDC_SELECT_PROJECT_TYPE, m_ProjectType);
	DDX_Check(pDX, IDC_CHECK_TOOL, m_ToolProject);
	DDX_Text(pDX, IDC_EDIT_IMPLIB_PATH, m_ImplibPath);
	DDX_Check(pDX, IDC_CHECK_PUBLIC, m_PublicProject);
	DDX_Check(pDX, IDC_CHECK_CONSOLE_APP, m_ConsoleApp);
	DDX_Check(pDX, IDC_CHECK_PUBLISH_IMPORT, m_PublishImportLib);
	DDX_Text(pDX, IDC_EDIT_SRC_PATH, m_SrcPath);
	//}}AFX_DATA_MAP
}

static void FixupPath( CString& path )
{
	int idx;
	idx = path.Find("/");
	while (idx >= 0)
	{
		path.SetAt(idx, '\\' );
		idx = path.Find("/");
	}

	bool hasTerminatingSlash = ( path.Right(1).Find("\\") >= 0 );
	if (!hasTerminatingSlash)
		path += '\\';

	path.MakeLower();
}

static int CountSlashes( CString& path )
{
	int count = 0;

	int idx = path.Find("\\", 0);
	while (idx >= 0)
	{
		++count;
		idx = path.Find("\\", idx+1 );
	}

	return count;
}

bool CCustom1Dlg::ComputeRelativePath( )
{
	if ( m_RootPath.GetAt(1) != ':' )
	{
		MessageBox( "Error! The root path must specify a drive!", "Bogus Root Path!", MB_ICONERROR | MB_OK );
		return false;
	}

	if ( m_TargetPath.GetAt(1) != ':' )
	{
		MessageBox( "Error! The target path must specify a drive!", "Bogus Target Path!", MB_ICONERROR | MB_OK );
		return false;
	}

	CString sourcePath;
	if (!Valvelibaw.m_Dictionary.Lookup("FULL_DIR_PATH", sourcePath ))
	{
		MessageBox( "I can't seem to find the source path!??!", "Umm... Get Brian", MB_ICONERROR | MB_OK );
		return false;
	}

	FixupPath( m_RootPath );
	FixupPath( m_TargetPath );
	FixupPath( sourcePath );

	CString srcRootPath = m_RootPath;
	srcRootPath += m_SrcPath;
	srcRootPath += "\\";
	FixupPath( srcRootPath );

	if (sourcePath.Find( srcRootPath ) != 0)
	{
		MessageBox( "Error! The source path must lie under the root source path!", "Bogus Root Path!", MB_ICONERROR | MB_OK );
		return false;
	}

	if (m_TargetPath.Find( m_RootPath ) != 0)
	{
		MessageBox( "Error! The target path must lie under the root path!", "Bogus Target Path!", MB_ICONERROR | MB_OK );
		return false;
	}

	int rootLen = m_RootPath.GetLength();
	int rootSrcLen = srcRootPath.GetLength();
	int sourceLen = sourcePath.GetLength();
	int targetLen = m_TargetPath.GetLength();
	CString relativePath = m_TargetPath.Right( targetLen - rootLen );

	// Now that we've got the relative source path, 
	// find out how many slashes are in it;
	// that'll tell us how many paths to back up....
	int i;
	CString relativeSourcePath = sourcePath.Right( sourceLen - rootLen );
	int numSlashes = CountSlashes(relativeSourcePath);
	CString targetRelativePath;
	for ( i = 0; i < numSlashes; ++i )
	{
		targetRelativePath += "..\\";
	}

	// Now that we've got the relative source path, 
	// find out how many slashes are in it;
	// that'll tell us how many paths to back up....
	CString rootSrcToProj = sourcePath.Right( sourceLen - rootSrcLen );
	numSlashes = CountSlashes(rootSrcToProj);
	CString projToRootSrc;
	for ( i = 0; i < numSlashes; ++i )
	{
		projToRootSrc += "..\\";
	}

	Valvelibaw.m_Dictionary["VALVE_ROOT_RELATIVE_PATH"] = targetRelativePath;
	Valvelibaw.m_Dictionary["VALVE_SRC_RELATIVE_PATH"] = projToRootSrc;
	targetRelativePath += relativePath;
	Valvelibaw.m_Dictionary["VALVE_ROOT_PATH"] = m_RootPath;
	Valvelibaw.m_Dictionary["VALVE_ROOT_SRC_PATH"] = srcRootPath;
	Valvelibaw.m_Dictionary["VALVE_TARGET_PATH"] = m_TargetPath;
	Valvelibaw.m_Dictionary["VALVE_RELATIVE_PATH"] = targetRelativePath;

	if (m_ToolProject)
		Valvelibaw.m_Dictionary["VALVE_TOOL"] = "1";
	if (m_PublicProject && (m_ProjectType != 2))
		Valvelibaw.m_Dictionary["VALVE_PUBLIC_PROJECT"] = "1";
	if (m_PublishImportLib && (m_ProjectType == 1))
		Valvelibaw.m_Dictionary["VALVE_PUBLISH_IMPORT_LIB"] = "1";

	// Import libraries
	if (m_ProjectType == 1)
	{
		if ( m_ImplibPath.GetAt(1) != ':' )
		{
			MessageBox( "Error! The import library path must specify a drive!", "Bogus Import Library Path!", MB_ICONERROR | MB_OK );
			return false;
		}

		if (m_ImplibPath.Find( srcRootPath ) != 0)
		{
			MessageBox( "Error! The import library path must lie under the root src path!", "Bogus Target Path!", MB_ICONERROR | MB_OK );
			return false;
		}

		int implibLen = m_ImplibPath.GetLength();
		relativePath = m_ImplibPath.Right( implibLen - rootSrcLen );
		int numSlashes = CountSlashes(rootSrcToProj);
		CString implibRelativePath;
		for (int i = 0; i < numSlashes; ++i )
		{
			implibRelativePath += "..\\";
		}
		implibRelativePath += relativePath;

		Valvelibaw.m_Dictionary["VALVE_IMPLIB_PATH"] = m_ImplibPath;
		Valvelibaw.m_Dictionary["VALVE_IMPLIB_RELATIVE_PATH"] = implibRelativePath;
	}

	return true;
}

// This is called whenever the user presses Next, Back, or Finish with this step
//  present.  Do all validation & data exchange from the dialog in this function.
BOOL CCustom1Dlg::OnDismiss()
{
	if (!UpdateData(TRUE))
		return FALSE;

	if (!ComputeRelativePath())
		return FALSE;

	switch( m_ProjectType )
	{
	case 0:
		Valvelibaw.m_Dictionary["VALVE_TARGET_TYPE"] = "lib"; 
		Valvelibaw.m_Dictionary["PROJTYPE_LIB"]	= "1";
		break;

	case 1:
		Valvelibaw.m_Dictionary["VALVE_TARGET_TYPE"] = "dll"; 
		Valvelibaw.m_Dictionary["PROJTYPE_DLL"]	= "1";
		break;

	case 2:
		Valvelibaw.m_Dictionary["VALVE_TARGET_TYPE"] = "exe"; 

		if (m_ConsoleApp)
		{
			Valvelibaw.m_Dictionary["PROJTYPE_CON"]	= "1";
		}
		break;
	}
   
	return TRUE;	// return FALSE if the dialog shouldn't be dismissed
}


BEGIN_MESSAGE_MAP(CCustom1Dlg, CAppWizStepDlg)
	//{{AFX_MSG_MAP(CCustom1Dlg)
	ON_CBN_SELCHANGE(IDC_SELECT_PROJECT_TYPE, OnSelchangeSelectProjectType)
	ON_EN_CHANGE(IDC_EDIT_ROOT_PATH, OnChangeEditRootPath)
	ON_BN_CLICKED(IDC_CHECK_PUBLIC, OnCheckPublic)
	ON_BN_CLICKED(IDC_CHECK_TOOL, OnCheckTool)
	ON_BN_CLICKED(IDC_CHECK_PUBLISH_IMPORT, OnCheckPublishImport)
	ON_EN_CHANGE(IDC_EDIT_SRC_PATH, OnChangeEditSrcPath)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CCustom1Dlg message handlers

void CCustom1Dlg::RecomputeTargetPath() 
{
	bool hasTerminatingSlash = ( m_RootPath.Right(1).FindOneOf("\\/") >= 0 );

	m_TargetPath = m_RootPath;
	if (!hasTerminatingSlash)
		m_TargetPath += '\\';
	m_ImplibPath = m_TargetPath;

	switch( m_ProjectType )
	{
	case 0:
		// static library
		m_TargetPath += m_SrcPath;
		m_TargetPath += m_PublicProject ? "\\lib\\public\\" : "\\lib\\common\\";
		m_ImplibPath = "unused";
		break;

	case 1:
		m_TargetPath += "bin\\";
		m_ImplibPath += m_SrcPath;
		m_ImplibPath += m_PublicProject ? "\\lib\\public\\" : "\\lib\\common\\";
		break;

	case 2:
		m_TargetPath += "bin\\";
		m_ImplibPath = "unused";
		break;
	}

	UpdateData(FALSE);
}

void CCustom1Dlg::EnableCheckboxes()
{
	CWnd* pConsoleApp = GetDlgItem(IDC_CHECK_CONSOLE_APP);
	CWnd* pPublishImport = GetDlgItem(IDC_CHECK_PUBLISH_IMPORT);
	CWnd* pImportLib = GetDlgItem(IDC_EDIT_IMPLIB_PATH);
	switch (m_ProjectType)
	{
	case 0:
		pConsoleApp->EnableWindow( false );
		pPublishImport->EnableWindow( false );
		pImportLib->EnableWindow( false );
		break;

	case 1:
		pConsoleApp->EnableWindow( false );
		pPublishImport->EnableWindow( true );
		pImportLib->EnableWindow( m_PublishImportLib );
		break;

	case 2:
		pConsoleApp->EnableWindow( true );
		pPublishImport->EnableWindow( false );
		pImportLib->EnableWindow( false );
		break;
	}
}

void CCustom1Dlg::OnSelchangeSelectProjectType() 
{
	if (!UpdateData(TRUE))
		return;

	RecomputeTargetPath();
	EnableCheckboxes();
}

void CCustom1Dlg::OnChangeEditRootPath() 
{	
	if (!UpdateData(TRUE))
		return;

	RecomputeTargetPath();
}

void CCustom1Dlg::OnCheckPublic() 
{
	if (!UpdateData(TRUE))
		return;

	RecomputeTargetPath();
}

void CCustom1Dlg::OnCheckTool() 
{
	if (!UpdateData(TRUE))
		return;

	RecomputeTargetPath();
}

void CCustom1Dlg::OnCheckPublishImport() 
{
	if (!UpdateData(TRUE))
		return;
	
	EnableCheckboxes();
}

void CCustom1Dlg::OnChangeEditSrcPath() 
{
	if (!UpdateData(TRUE))
		return;

	RecomputeTargetPath();
}