tools/installer/ChooseMethod.cpp
author Scott Ehlert <ds@alliedmods.net>
Sun Sep 14 03:01:05 2008 -0500 (2008-09-14)
changeset 2289 44045ead61cd
parent 1952 b7aa2b626807
parent 1724 d9c2d3a9aaa1
permissions -rw-r--r--
Eliminated head: da55a80522cf, a remnant of sourcemod-1.1.0 branch
     1 #include "InstallerMain.h"
     2 #include "InstallerUtil.h"
     3 #include "ChooseMethod.h"
     4 #include "Welcome.h"
     5 #include "GamesList.h"
     6 #include "SelectGame.h"
     7 #include "PerformInstall.h"
     8 #include "LocalCopyMethod.h"
     9 
    10 game_group_t *g_game_group = NULL;
    11 unsigned int method_chosen = 0;
    12 TCHAR method_path[MAX_PATH];
    13 
    14 bool SelectFolder(HWND hOwner)
    15 {
    16 	BROWSEINFO info;
    17 	LPITEMIDLIST pidlist;
    18 	TCHAR path[MAX_PATH];
    19 
    20 	if (FAILED(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED)))
    21 	{
    22 		return false;
    23 	}
    24 	
    25 	info.hwndOwner = hOwner;
    26 	info.pidlRoot = NULL;
    27 	info.pszDisplayName = path;
    28 	info.lpszTitle = _T("Select a game/mod folder");
    29 	info.ulFlags = BIF_EDITBOX | BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;
    30 	info.lpfn = NULL;
    31 	info.lParam = 0;
    32 	info.iImage = 0;
    33 
    34 	if ((pidlist = SHBrowseForFolder(&info)) == NULL)
    35 	{
    36 		CoUninitialize();
    37 		return false;
    38 	}
    39 
    40 	/* This hellish code is from MSDN and translate shortcuts to real targets.
    41 	 * God almighty, I wish Window used real symlinks.
    42 	 */
    43 	bool acquire_success = false;
    44 	bool is_link = false;
    45 	IShellFolder *psf = NULL;
    46 	LPCITEMIDLIST new_item_list;
    47 	HRESULT hr;
    48 	
    49 	hr = SHBindToParent(pidlist, IID_IShellFolder, (void **)&psf, &new_item_list);
    50 	if (SUCCEEDED(hr))
    51 	{
    52 		IShellLink *psl = NULL;
    53 
    54 		hr = psf->GetUIObjectOf(hOwner, 1, &new_item_list, IID_IShellLink, NULL, (void **)&psl);
    55 		if (SUCCEEDED(hr))
    56 		{
    57 			LPITEMIDLIST new_item_list;
    58 
    59 			hr = psl->GetIDList(&new_item_list);
    60 			if (SUCCEEDED(hr))
    61 			{
    62 				is_link = true;
    63 
    64 				hr = SHGetPathFromIDList(new_item_list, method_path);
    65 				if (SUCCEEDED(hr))
    66 				{
    67 					acquire_success = true;
    68 				}
    69 
    70 				CoTaskMemFree(new_item_list);
    71 			}
    72 			psl->Release();
    73 		}
    74 		psf->Release();
    75 	}
    76 
    77 	if (!acquire_success && !is_link)
    78 	{
    79 		hr = SHGetPathFromIDList(pidlist, method_path);
    80 		if (SUCCEEDED(hr))
    81 		{
    82 			acquire_success = true;
    83 		}
    84 	}
    85 
    86 	/* That was awful.  shoo, shoo, COM */
    87 	CoTaskMemFree(pidlist);
    88 	CoUninitialize();
    89 
    90 	return acquire_success;
    91 }
    92 
    93 INT_PTR CALLBACK ChooseMethodHandler(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    94 {
    95 	switch (message)
    96 	{
    97 	case WM_COMMAND:
    98 		{
    99 			if (LOWORD(wParam) == ID_METHOD_BACK)
   100 			{
   101 				UpdateGlobalPosition(hDlg);
   102 				EndDialog(hDlg, (INT_PTR)DisplayWelcome);
   103 				return (INT_PTR)TRUE;
   104 			}
   105 			else if (LOWORD(wParam) == ID_METHOD_EXIT
   106 					 || LOWORD(wParam) == ID_CLOSE)
   107 			{
   108 				return AskToExit(hDlg);
   109 			}
   110 			else if (LOWORD(wParam) == IDC_METHOD_DED_SERVER
   111 					 || LOWORD(wParam) == IDC_METHOD_ALONE_SERVER
   112 					 || LOWORD(wParam) == IDC_METHOD_LISTEN_SERVER
   113 					 || LOWORD(wParam) == IDC_METHOD_UPLOAD_FTP
   114 					 || LOWORD(wParam) == IDC_METHOD_CUSTOM_FOLDER)
   115 			{
   116 				method_chosen = LOWORD(wParam);
   117 				HWND button = GetDlgItem(hDlg, ID_METHOD_NEXT);
   118 				EnableWindow(button, TRUE);
   119 				break;
   120 			}
   121 			else if (LOWORD(wParam) == ID_METHOD_NEXT)
   122 			{
   123 				unsigned int game_type = 0;
   124 
   125 				switch (method_chosen)
   126 				{
   127 				case IDC_METHOD_DED_SERVER:
   128 					{
   129 						game_type = GAMES_DEDICATED;
   130 						break;
   131 					}
   132 				case IDC_METHOD_ALONE_SERVER:
   133 					{
   134 						game_type = GAMES_STANDALONE;
   135 						break;
   136 					}
   137 				case IDC_METHOD_LISTEN_SERVER:
   138 					{
   139 						game_type = GAMES_LISTEN;
   140 						break;
   141 					}
   142 				case IDC_METHOD_UPLOAD_FTP:
   143 					{
   144 						break;
   145 					}
   146 				case IDC_METHOD_CUSTOM_FOLDER:
   147 					{
   148 						int val;
   149 
   150 						if (!SelectFolder(hDlg))
   151 						{
   152 							break;
   153 						}
   154 						
   155 						val = IsValidFolder(method_path);
   156 						if (val != GAMEINFO_IS_USABLE)
   157 						{
   158 							DisplayBadFolderDialog(hDlg, val);
   159 							break;
   160 						}
   161 
   162 						g_LocalCopier.SetOutputPath(method_path);
   163 						SetInstallMethod(&g_LocalCopier);
   164 
   165 						UpdateGlobalPosition(hDlg);
   166 						EndDialog(hDlg, (INT_PTR)DisplayPerformInstall);
   167 					}
   168 				}
   169 
   170 				if (game_type != 0)
   171 				{
   172 					g_game_group = NULL;
   173 
   174 					BuildGameDB();
   175 
   176 					if (game_type == GAMES_DEDICATED)
   177 					{
   178 						g_game_group = &g_games.dedicated;
   179 					}
   180 					else if (game_type == GAMES_LISTEN)
   181 					{
   182 						g_game_group = &g_games.listen;
   183 					}
   184 					else if (game_type == GAMES_STANDALONE)
   185 					{
   186 						g_game_group = &g_games.standalone;
   187 					}
   188 					
   189 					if (g_game_group == NULL)
   190 					{
   191 						return (INT_PTR)TRUE;
   192 					}
   193 
   194 					if (g_game_group->list_count == 0)
   195 					{
   196 						DisplayBadGamesDialog(hDlg, g_game_group->error_code);
   197 						return (INT_PTR)TRUE;
   198 					}
   199 
   200 					/* If we got a valid games list, we can display the next 
   201 					 * dialog box.
   202 					 */
   203 					UpdateGlobalPosition(hDlg);
   204 					EndDialog(hDlg, (INT_PTR)DisplaySelectGame);
   205 					return (INT_PTR)TRUE;
   206 				}
   207 			}
   208 			break;
   209 		}
   210 	case WM_INITDIALOG:
   211 		{
   212 			SetToGlobalPosition(hDlg);
   213 			return (INT_PTR)TRUE;
   214 		}
   215 	}
   216 
   217 	return (INT_PTR)FALSE;
   218 }
   219 
   220 void *DisplayChooseMethod(HWND hWnd)
   221 {
   222 	INT_PTR val;
   223 	
   224 	if ((val = DialogBox(
   225 		g_hInstance, 
   226 		MAKEINTRESOURCE(IDD_CHOOSE_METHOD),
   227 		hWnd,
   228 		ChooseMethodHandler)) == -1)
   229 	{
   230 		return NULL;
   231 	}
   232 
   233 	return (void *)val;
   234 }
   235