Triggering build.
1 #include "InstallerMain.h"
2 #include "InstallerUtil.h"
3 #include "PerformInstall.h"
4 #include "CCriticalSection.h"
6 #define WMU_INSTALLER_DONE WM_USER+2
7 #define PBAR_RANGE_HIGH 100
8 #define PBAR_RANGE_LOW 0
10 ICopyMethod *g_pCopyMethod = NULL;
11 HANDLE g_hCopyThread = NULL;
12 copy_thread_args_t g_thread_args = {NULL, NULL, NULL, false, false};
13 CCriticalSection g_update_window;
15 class TrackProgress : public ICopyProgress
18 void Initialize(HWND hTextBar, HWND hCurBar, HWND hTotalBar, size_t total_size)
20 m_hTextBar = hTextBar;
22 m_hTotalBar = hTotalBar;
23 m_TotalSize = total_size;
25 RedrawProgressBars(0.0, 0.0);
29 RedrawProgressBars(100.0, 100.0);
32 void StartingNewFile(const TCHAR *filename)
36 if (g_update_window.TryEnter())
38 UTIL_Format(buffer, sizeof(buffer) / sizeof(TCHAR), _T("Copying: %s"), filename);
39 SendMessage(m_hTextBar, WM_SETTEXT, 0, (LPARAM)buffer);
40 UpdateWindow(m_hTextBar);
41 g_update_window.Leave();
44 void UpdateProgress(size_t bytes, size_t total_bytes)
46 float fCur = (float)bytes / (float)total_bytes;
47 float fTotal = ((float)m_TotalDone + (float)bytes) / (float)m_TotalSize;
48 RedrawProgressBars(fCur, fTotal);
50 void FileDone(size_t total_size)
52 m_TotalDone += total_size;
53 RedrawProgressBars(0.0, (float)m_TotalDone / (float)m_TotalSize);
56 void RedrawProgressBar(HWND hBar, float fPerc)
58 /* Get a percentage point in the range */
59 float fPointInRange = (float)(PBAR_RANGE_HIGH - PBAR_RANGE_LOW) * fPerc;
60 int iPointInRange = (int)fPointInRange;
63 iPointInRange += PBAR_RANGE_LOW;
65 if (g_update_window.TryEnter())
71 g_update_window.Leave();
74 void RedrawProgressBars(float fCurrent, float fTotal)
76 RedrawProgressBar(m_hCurBar, fCurrent);
77 RedrawProgressBar(m_hTotalBar, fTotal);
87 void CancelPerformInstall()
89 delete g_thread_args.pFileList;
90 g_thread_args.pFileList = NULL;
93 void SetInstallMethod(ICopyMethod *pCopyMethod)
95 g_pCopyMethod = pCopyMethod;
98 bool CopyStructureRecursively(ICopyMethod *pCopyMethod,
100 const TCHAR *basepath,
101 const TCHAR *local_path,
105 TCHAR file_path[MAX_PATH];
109 if (!pCopyMethod->SetCurrentFolder(local_path, errbuf, maxchars))
115 while ((file = pFileList->PeekCurrentFile()) != NULL)
117 if (local_path == NULL)
119 UTIL_PathFormat(file_path,
120 sizeof(file_path) / sizeof(TCHAR),
127 UTIL_PathFormat(file_path,
128 sizeof(file_path) / sizeof(TCHAR),
135 if (!pCopyMethod->SendFile(file_path, errbuf, maxchars))
140 pFileList->PopCurrentFile();
143 /* Now copy folders */
144 while ((pSubList = pFileList->PeekCurrentFolder()) != NULL)
146 if (g_thread_args.m_bIsUpgrade)
148 /* :TODO: put this somewhere else because it technically
149 * means the progress bars get calculated wrong
151 if (tstrcasecmp(pSubList->GetFolderName(), _T("cfg")) == 0
152 || tstrcasecmp(pSubList->GetFolderName(), _T("configs")) == 0)
154 pFileList->PopCurrentFolder();
159 /* Try creating the folder */
160 if (!pCopyMethod->CreateFolder(pSubList->GetFolderName(), errbuf, maxchars))
165 TCHAR new_local_path[MAX_PATH];
166 if (local_path == NULL)
168 UTIL_PathFormat(new_local_path,
169 sizeof(new_local_path) / sizeof(TCHAR),
171 pSubList->GetFolderName());
175 UTIL_PathFormat(new_local_path,
176 sizeof(new_local_path) / sizeof(TCHAR),
179 pSubList->GetFolderName());
182 if (!CopyStructureRecursively(pCopyMethod,
192 pFileList->PopCurrentFolder();
194 /* Set the current folder again for the next operation */
195 if (!pCopyMethod->SetCurrentFolder(local_path, errbuf, maxchars))
204 DWORD WINAPI T_CopyFiles(LPVOID arg)
207 CopyStructureRecursively(g_thread_args.pCopyMethod,
208 g_thread_args.pFileList,
209 g_thread_args.basepath,
212 sizeof(g_thread_args.error) / sizeof(TCHAR));
214 PostMessage(g_thread_args.hWnd, WMU_INSTALLER_DONE, result ? TRUE : FALSE, 0);
219 bool StartFileCopy(HWND hWnd)
221 g_thread_args.m_bWasCancelled = false;
222 g_thread_args.hWnd = hWnd;
223 if ((g_hCopyThread = CreateThread(NULL,
233 _T("Could not initialize copy thread."),
234 _T("SourceMod Installer"),
243 g_thread_args.m_bWasCancelled = true;
244 g_pCopyMethod->CancelCurrentCopy();
246 if (g_hCopyThread != NULL)
248 g_update_window.Enter();
249 WaitForSingleObject(g_hCopyThread, INFINITE);
250 g_update_window.Leave();
251 CloseHandle(g_hCopyThread);
252 g_hCopyThread = NULL;
256 bool RequestCancelInstall(HWND hWnd)
260 int val = MessageBox(
262 _T("Are you sure you want to cancel the install?"),
263 _T("SourceMod Installer"),
264 MB_YESNO|MB_ICONQUESTION);
271 if (g_thread_args.pFileList == NULL)
276 /* Start the thread, note our return value is opposite */
277 return !StartFileCopy(hWnd);
280 bool StartInstallProcess(HWND hWnd)
282 if (g_pCopyMethod->CheckForExistingInstall())
284 int val = MessageBox(
286 _T("It looks like a previous SourceMod installation exists. Select \"Yes\" to skip copying configuration files. Select \"No\" to perform a full re-install."),
287 _T("SourceMod Installer"),
288 MB_YESNO|MB_ICONQUESTION);
290 if (val == 0 || val == IDYES)
292 g_thread_args.m_bIsUpgrade = true;
296 g_thread_args.m_bIsUpgrade = false;
301 TCHAR cur_path[MAX_PATH];
302 if (_tgetcwd(cur_path, sizeof(cur_path)) == NULL)
306 _T("Could not locate current directory!"),
307 _T("SourceMod Installer"),
314 UTIL_PathFormat(source_path,
315 sizeof(source_path) / sizeof(TCHAR),
319 UTIL_PathFormat(g_thread_args.basepath,
320 sizeof(g_thread_args.basepath) / sizeof(TCHAR),
321 _T("C:\\real\\done\\base"));
324 if (GetFileAttributes(g_thread_args.basepath) == INVALID_FILE_ATTRIBUTES)
328 _T("Could not locate the source installation files!"),
329 _T("SourceMod Installer"),
334 delete g_thread_args.pFileList;
335 g_thread_args.pFileList = CFileList::BuildFileList(_T(""), g_thread_args.basepath);
337 s_ProgressTracker.Initialize(
338 GetDlgItem(hWnd, IDC_PROGRESS_CURCOPY),
339 GetDlgItem(hWnd, IDC_PROGRESS_CURRENT),
340 GetDlgItem(hWnd, IDC_PROGRESS_TOTAL),
341 (size_t)g_thread_args.pFileList->GetRecursiveSize());
342 g_pCopyMethod->TrackProgress(&s_ProgressTracker);
343 g_thread_args.pCopyMethod = g_pCopyMethod;
345 return StartFileCopy(hWnd);
348 INT_PTR CALLBACK PerformInstallHandler(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
354 SetToGlobalPosition(hDlg);
355 return (INT_PTR)TRUE;
359 if (LOWORD(wParam) == ID_INSTALL_CANCEL
360 || LOWORD(wParam) == ID_CLOSE)
362 if (RequestCancelInstall(hDlg))
364 CancelPerformInstall();
365 UpdateGlobalPosition(hDlg);
366 EndDialog(hDlg, NULL);
368 return (INT_PTR)TRUE;
370 else if (LOWORD(wParam) == ID_INSTALL_START)
372 HWND hButton = GetDlgItem(hDlg, ID_INSTALL_START);
373 EnableWindow(hButton, FALSE);
374 StartInstallProcess(hDlg);
378 case WMU_INSTALLER_DONE:
382 s_ProgressTracker.Finished();
384 _T("SourceMod was successfully installed! Please visit http://www.sourcemod.net/ for documentation."),
385 _T("SourceMod Installer"),
387 CancelPerformInstall();
388 UpdateGlobalPosition(hDlg);
389 EndDialog(hDlg, NULL);
390 return (INT_PTR)TRUE;
392 else if (!g_thread_args.m_bWasCancelled)
397 sizeof(buffer) / sizeof(TCHAR),
398 _T("Encountered error: %s"),
399 g_thread_args.error);
400 int res = MessageBox(hDlg,
402 _T("SourceMod Installer"),
403 MB_ICONERROR|MB_RETRYCANCEL);
411 CancelPerformInstall();
412 UpdateGlobalPosition(hDlg);
413 EndDialog(hDlg, NULL);
414 return (INT_PTR)TRUE;
421 return (INT_PTR)FALSE;
424 void *DisplayPerformInstall(HWND hWnd)
428 if ((val = DialogBox(
430 MAKEINTRESOURCE(IDD_PERFORM_INSTALL),
432 PerformInstallHandler)) == -1)