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