tools/installer/LocalCopyMethod.cpp
changeset 1651 077571ea8942
child 1722 2c16f160f5dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/installer/LocalCopyMethod.cpp	Sun Oct 28 20:15:51 2007 +0000
     1.3 @@ -0,0 +1,158 @@
     1.4 +#include "InstallerUtil.h"
     1.5 +#include "LocalCopyMethod.h"
     1.6 +
     1.7 +LocalCopyMethod g_LocalCopier;
     1.8 +
     1.9 +DWORD CALLBACK CopyProgressRoutine(LARGE_INTEGER TotalFileSize,
    1.10 +								   LARGE_INTEGER TotalBytesTransferred,
    1.11 +								   LARGE_INTEGER StreamSize,
    1.12 +								   LARGE_INTEGER StreamBytesTransferred,
    1.13 +								   DWORD dwStreamNumber,
    1.14 +								   DWORD dwCallbackReason,
    1.15 +								   HANDLE hSourceFile,
    1.16 +								   HANDLE hDestinationFile,
    1.17 +								   LPVOID lpData)
    1.18 +{
    1.19 +	ICopyProgress *progress = (ICopyProgress *)lpData;
    1.20 +
    1.21 +	float percent = (float)(TotalBytesTransferred.QuadPart) / (float)(TotalFileSize.QuadPart);
    1.22 +
    1.23 +	progress->UpdateProgress(percent);
    1.24 +
    1.25 +	return PROGRESS_CONTINUE;
    1.26 +}
    1.27 +
    1.28 +LocalCopyMethod::LocalCopyMethod()
    1.29 +{
    1.30 +	m_pProgress = NULL;
    1.31 +}
    1.32 +
    1.33 +void LocalCopyMethod::SetOutputPath(const TCHAR *path)
    1.34 +{
    1.35 +	UTIL_PathFormat(m_OutputPath,
    1.36 +		sizeof(m_OutputPath) / sizeof(TCHAR),
    1.37 +		_T("%s"),
    1.38 +		path);
    1.39 +
    1.40 +	UTIL_PathFormat(m_CurrentPath,
    1.41 +		sizeof(m_CurrentPath) / sizeof(TCHAR),
    1.42 +		_T("%s"),
    1.43 +		path);
    1.44 +}
    1.45 +
    1.46 +void LocalCopyMethod::TrackProgress(ICopyProgress *pProgress)
    1.47 +{
    1.48 +	m_pProgress = pProgress;
    1.49 +}
    1.50 +
    1.51 +bool LocalCopyMethod::CreateFolder(const TCHAR *name, TCHAR *buffer, size_t maxchars)
    1.52 +{
    1.53 +	TCHAR path[MAX_PATH];
    1.54 +
    1.55 +	UTIL_PathFormat(path,
    1.56 +		sizeof(path) / sizeof(TCHAR),
    1.57 +		_T("%s\\%s"),
    1.58 +		m_CurrentPath,
    1.59 +		name);
    1.60 +
    1.61 +	if (CreateDirectory(name, NULL))
    1.62 +	{
    1.63 +		return true;
    1.64 +	}
    1.65 +
    1.66 +	DWORD error = GetLastError();
    1.67 +	if (error == ERROR_ALREADY_EXISTS)
    1.68 +	{
    1.69 +		return true;
    1.70 +	}
    1.71 +
    1.72 +	GenerateErrorMessage(error, buffer, maxchars);
    1.73 +
    1.74 +	return false;
    1.75 +}
    1.76 +
    1.77 +bool LocalCopyMethod::SetCurrentFolder(const TCHAR *path, TCHAR *buffer, size_t maxchars)
    1.78 +{
    1.79 +	if (path == NULL)
    1.80 +	{
    1.81 +		UTIL_PathFormat(m_CurrentPath,
    1.82 +			sizeof(m_CurrentPath) / sizeof(TCHAR),
    1.83 +			_T("%s"),
    1.84 +			m_OutputPath);
    1.85 +	}
    1.86 +	else
    1.87 +	{
    1.88 +		UTIL_PathFormat(m_CurrentPath,
    1.89 +			sizeof(m_CurrentPath) / sizeof(TCHAR),
    1.90 +			_T("%s\\%s"),
    1.91 +			m_OutputPath,
    1.92 +			path);
    1.93 +	}
    1.94 +
    1.95 +	return true;
    1.96 +}
    1.97 +
    1.98 +bool LocalCopyMethod::SendFile(const TCHAR *path, TCHAR *buffer, size_t maxchars)
    1.99 +{
   1.100 +	const TCHAR *filename = GetFileFromPath(path);
   1.101 +
   1.102 +	if (filename == NULL)
   1.103 +	{
   1.104 +		UTIL_Format(buffer, maxchars, _T("Invalid filename"));
   1.105 +		return false;
   1.106 +	}
   1.107 +
   1.108 +	TCHAR new_path[MAX_PATH];
   1.109 +	UTIL_PathFormat(new_path,
   1.110 +		sizeof(new_path) / sizeof(TCHAR),
   1.111 +		_T("%s\\%s"),
   1.112 +		m_CurrentPath,
   1.113 +		filename);
   1.114 +
   1.115 +	m_bCancelStatus = FALSE;
   1.116 +
   1.117 +	if (CopyFileEx(path,
   1.118 +		new_path,
   1.119 +		m_pProgress ? CopyProgressRoutine : NULL,
   1.120 +		m_pProgress,
   1.121 +		&m_bCancelStatus,
   1.122 +		0) == 0)
   1.123 +	{
   1.124 +		/* Delete the file in case it was a partial copy */
   1.125 +		DeleteFile(new_path);
   1.126 +
   1.127 +		GenerateErrorMessage(GetLastError(), buffer, maxchars);
   1.128 +
   1.129 +		return false;
   1.130 +	}
   1.131 +
   1.132 +	return true;
   1.133 +}
   1.134 +
   1.135 +void LocalCopyMethod::CancelCurrentCopy()
   1.136 +{
   1.137 +	m_bCancelStatus = TRUE;
   1.138 +}
   1.139 +
   1.140 +bool LocalCopyMethod::CheckForExistingInstall()
   1.141 +{
   1.142 +	TCHAR path[MAX_PATH];
   1.143 +
   1.144 +	UTIL_PathFormat(path,
   1.145 +		sizeof(path) / sizeof(TCHAR),
   1.146 +		_T("%s\\addons\\sourcemod"),
   1.147 +		m_CurrentPath);
   1.148 +	if (GetFileAttributes(path) == INVALID_FILE_ATTRIBUTES)
   1.149 +	{
   1.150 +		UTIL_PathFormat(path,
   1.151 +			sizeof(path) / sizeof(TCHAR),
   1.152 +			_T("%s\\cfg\\sourcemod"),
   1.153 +			m_CurrentPath);
   1.154 +		if (GetFileAttributes(path) == INVALID_FILE_ATTRIBUTES)
   1.155 +		{
   1.156 +			return false;
   1.157 +		}
   1.158 +	}
   1.159 +
   1.160 +	return true;
   1.161 +}