tools/installer/LocalCopyMethod.cpp
author David Anderson <dvander@alliedmods.net>
Sun Oct 28 20:15:51 2007 +0000 (2007-10-28)
changeset 1651 077571ea8942
child 1722 2c16f160f5dd
permissions -rw-r--r--
initial import of incomplete installer
     1 #include "InstallerUtil.h"
     2 #include "LocalCopyMethod.h"
     3 
     4 LocalCopyMethod g_LocalCopier;
     5 
     6 DWORD CALLBACK CopyProgressRoutine(LARGE_INTEGER TotalFileSize,
     7 								   LARGE_INTEGER TotalBytesTransferred,
     8 								   LARGE_INTEGER StreamSize,
     9 								   LARGE_INTEGER StreamBytesTransferred,
    10 								   DWORD dwStreamNumber,
    11 								   DWORD dwCallbackReason,
    12 								   HANDLE hSourceFile,
    13 								   HANDLE hDestinationFile,
    14 								   LPVOID lpData)
    15 {
    16 	ICopyProgress *progress = (ICopyProgress *)lpData;
    17 
    18 	float percent = (float)(TotalBytesTransferred.QuadPart) / (float)(TotalFileSize.QuadPart);
    19 
    20 	progress->UpdateProgress(percent);
    21 
    22 	return PROGRESS_CONTINUE;
    23 }
    24 
    25 LocalCopyMethod::LocalCopyMethod()
    26 {
    27 	m_pProgress = NULL;
    28 }
    29 
    30 void LocalCopyMethod::SetOutputPath(const TCHAR *path)
    31 {
    32 	UTIL_PathFormat(m_OutputPath,
    33 		sizeof(m_OutputPath) / sizeof(TCHAR),
    34 		_T("%s"),
    35 		path);
    36 
    37 	UTIL_PathFormat(m_CurrentPath,
    38 		sizeof(m_CurrentPath) / sizeof(TCHAR),
    39 		_T("%s"),
    40 		path);
    41 }
    42 
    43 void LocalCopyMethod::TrackProgress(ICopyProgress *pProgress)
    44 {
    45 	m_pProgress = pProgress;
    46 }
    47 
    48 bool LocalCopyMethod::CreateFolder(const TCHAR *name, TCHAR *buffer, size_t maxchars)
    49 {
    50 	TCHAR path[MAX_PATH];
    51 
    52 	UTIL_PathFormat(path,
    53 		sizeof(path) / sizeof(TCHAR),
    54 		_T("%s\\%s"),
    55 		m_CurrentPath,
    56 		name);
    57 
    58 	if (CreateDirectory(name, NULL))
    59 	{
    60 		return true;
    61 	}
    62 
    63 	DWORD error = GetLastError();
    64 	if (error == ERROR_ALREADY_EXISTS)
    65 	{
    66 		return true;
    67 	}
    68 
    69 	GenerateErrorMessage(error, buffer, maxchars);
    70 
    71 	return false;
    72 }
    73 
    74 bool LocalCopyMethod::SetCurrentFolder(const TCHAR *path, TCHAR *buffer, size_t maxchars)
    75 {
    76 	if (path == NULL)
    77 	{
    78 		UTIL_PathFormat(m_CurrentPath,
    79 			sizeof(m_CurrentPath) / sizeof(TCHAR),
    80 			_T("%s"),
    81 			m_OutputPath);
    82 	}
    83 	else
    84 	{
    85 		UTIL_PathFormat(m_CurrentPath,
    86 			sizeof(m_CurrentPath) / sizeof(TCHAR),
    87 			_T("%s\\%s"),
    88 			m_OutputPath,
    89 			path);
    90 	}
    91 
    92 	return true;
    93 }
    94 
    95 bool LocalCopyMethod::SendFile(const TCHAR *path, TCHAR *buffer, size_t maxchars)
    96 {
    97 	const TCHAR *filename = GetFileFromPath(path);
    98 
    99 	if (filename == NULL)
   100 	{
   101 		UTIL_Format(buffer, maxchars, _T("Invalid filename"));
   102 		return false;
   103 	}
   104 
   105 	TCHAR new_path[MAX_PATH];
   106 	UTIL_PathFormat(new_path,
   107 		sizeof(new_path) / sizeof(TCHAR),
   108 		_T("%s\\%s"),
   109 		m_CurrentPath,
   110 		filename);
   111 
   112 	m_bCancelStatus = FALSE;
   113 
   114 	if (CopyFileEx(path,
   115 		new_path,
   116 		m_pProgress ? CopyProgressRoutine : NULL,
   117 		m_pProgress,
   118 		&m_bCancelStatus,
   119 		0) == 0)
   120 	{
   121 		/* Delete the file in case it was a partial copy */
   122 		DeleteFile(new_path);
   123 
   124 		GenerateErrorMessage(GetLastError(), buffer, maxchars);
   125 
   126 		return false;
   127 	}
   128 
   129 	return true;
   130 }
   131 
   132 void LocalCopyMethod::CancelCurrentCopy()
   133 {
   134 	m_bCancelStatus = TRUE;
   135 }
   136 
   137 bool LocalCopyMethod::CheckForExistingInstall()
   138 {
   139 	TCHAR path[MAX_PATH];
   140 
   141 	UTIL_PathFormat(path,
   142 		sizeof(path) / sizeof(TCHAR),
   143 		_T("%s\\addons\\sourcemod"),
   144 		m_CurrentPath);
   145 	if (GetFileAttributes(path) == INVALID_FILE_ATTRIBUTES)
   146 	{
   147 		UTIL_PathFormat(path,
   148 			sizeof(path) / sizeof(TCHAR),
   149 			_T("%s\\cfg\\sourcemod"),
   150 			m_CurrentPath);
   151 		if (GetFileAttributes(path) == INVALID_FILE_ATTRIBUTES)
   152 		{
   153 			return false;
   154 		}
   155 	}
   156 
   157 	return true;
   158 }