diff --git a/ApplyPatch/ApplyPatch.vcxproj b/ApplyPatch/ApplyPatch.vcxproj
index 9d18bb4..a67c565 100644
--- a/ApplyPatch/ApplyPatch.vcxproj
+++ b/ApplyPatch/ApplyPatch.vcxproj
@@ -22,32 +22,32 @@
{9DD0592D-6DDC-4434-AF28-3A65E61FC97A}
Win32Proj
ApplyPatch
- 8.1
+ 10.0
Application
true
- v140
+ v143
Unicode
Application
false
- v140
+ v143
true
Unicode
Application
true
- v140
+ v143
Unicode
Application
false
- v140
+ v143
true
Unicode
diff --git a/ApplyPatch/Main.cpp b/ApplyPatch/Main.cpp
index 4a12375..e23a093 100644
--- a/ApplyPatch/Main.cpp
+++ b/ApplyPatch/Main.cpp
@@ -51,8 +51,6 @@ int main(int argc, char* argv[])
uint64_t version = 1;
bool success = ApplyPatchFile(patchFileName, targetDirectory, version); // TODO: Add a version file
- DestroyLogSystem();
-
if (success)
exit(EXIT_SUCCESS);
else
diff --git a/CreatePatch/CreatePatch.vcxproj b/CreatePatch/CreatePatch.vcxproj
index 406f111..7448a21 100644
--- a/CreatePatch/CreatePatch.vcxproj
+++ b/CreatePatch/CreatePatch.vcxproj
@@ -22,32 +22,32 @@
{7B2587E3-02F9-4777-B70E-9016BEB741C2}
Win32Proj
CreatePatch
- 8.1
+ 10.0
Application
true
- v140
+ v143
Unicode
Application
false
- v140
+ v143
true
Unicode
Application
true
- v140
+ v143
Unicode
Application
false
- v140
+ v143
true
Unicode
diff --git a/CreatePatch/Main.cpp b/CreatePatch/Main.cpp
index a56304e..f1b6f9e 100644
--- a/CreatePatch/Main.cpp
+++ b/CreatePatch/Main.cpp
@@ -47,11 +47,6 @@ int main(int argc, char* argv[])
std::string newDirectory = argv[2];
std::string outputFilename = argv[3];
- // Make sure all directories are represented in the same format
- NormalizeFileName(oldDirectory);
- NormalizeFileName(newDirectory);
- NormalizeFileName(outputFilename);
-
Log(LOG, "Output patch file: %s", outputFilename.c_str());
Log(LOG, "Old version directory: %s", oldDirectory.c_str());
Log(LOG, "New version directory: %s", newDirectory.c_str());
@@ -63,8 +58,6 @@ int main(int argc, char* argv[])
if (patchFileList)
delete(patchFileList);
- DestroyLogSystem();
-
exit(EXIT_SUCCESS);
}
diff --git a/VisualCreatePatch/CreatePatchThread.cpp b/VisualCreatePatch/CreatePatchThread.cpp
index d3e1e84..a69250e 100644
--- a/VisualCreatePatch/CreatePatchThread.cpp
+++ b/VisualCreatePatch/CreatePatchThread.cpp
@@ -58,20 +58,15 @@ wxThread::ExitCode CreatePatchThread::Entry()
using namespace ZPatcher;
// Check through execution if we should destroy the thread and exit
- if (TestDestroy()) { ZPatcher::DestroyLogSystem(); return (wxThread::ExitCode)0; }
+ if (TestDestroy()) { return (wxThread::ExitCode)0; }
SetActiveLog("VisualCreatePatch");
- std::string oldDirectory = m_oldDirectory.ToStdString();
- NormalizeFileName(oldDirectory);
+ const std::string oldDirectory = m_oldDirectory.ToStdString();
+ const std::string newDirectory = m_newDirectory.ToStdString();
+ const std::string outputFilename = m_outputFilename.ToStdString();
- std::string newDirectory = m_newDirectory.ToStdString();
- NormalizeFileName(newDirectory);
-
- std::string outputFilename = m_outputFilename.ToStdString();
- NormalizeFileName(outputFilename);
-
- if (TestDestroy()) { ZPatcher::DestroyLogSystem(); return (wxThread::ExitCode)0; }
+ if (TestDestroy()) { return (wxThread::ExitCode)0; }
if (m_importXml)
{
@@ -86,10 +81,10 @@ wxThread::ExitCode CreatePatchThread::Entry()
else
{
// First, create the list of files to be added to the patch
- m_pPatchFileList = GetDifferences(oldDirectory, newDirectory, &CreatePatchFrame::UpdateComparisonDisplay);
+ m_pPatchFileList = GetDifferencesEx(oldDirectory, newDirectory, &CreatePatchFrame::UpdateComparisonDisplay);
}
- if (TestDestroy()) { ZPatcher::DestroyLogSystem(); return (wxThread::ExitCode)0; }
+ if (TestDestroy()) { return (wxThread::ExitCode)0; }
if (m_exportXml)
{
@@ -101,9 +96,8 @@ wxThread::ExitCode CreatePatchThread::Entry()
{
// Then, create the patch file.
// This is ugly, since there is no way to check inside CreatePatch() if the thread was destroyed. Check if there's a better way to do this.
- CreatePatchFile(outputFilename, newDirectory, m_pPatchFileList, &CreatePatchFrame::UpdatePatchProcessedDisplay, { &CreatePatchFrame::OnLZMAProgress });
+ CreatePatchFileEx(outputFilename, newDirectory, m_pPatchFileList, &CreatePatchFrame::UpdatePatchProcessedDisplay, reinterpret_cast(&CreatePatchFrame::OnLZMAProgress));
}
- ZPatcher::DestroyLogSystem();
return (wxThread::ExitCode)0; // success
}
diff --git a/VisualCreatePatch/VisualCreatePatch.vcxproj b/VisualCreatePatch/VisualCreatePatch.vcxproj
index a32a3a8..3d871f5 100644
--- a/VisualCreatePatch/VisualCreatePatch.vcxproj
+++ b/VisualCreatePatch/VisualCreatePatch.vcxproj
@@ -22,32 +22,32 @@
{33851E19-677D-4BCC-85ED-CBAFC6974B23}
Win32Proj
VisualCreatePatch
- 8.1
+ 10.0
Application
true
- v140
+ v143
Unicode
Application
false
- v140
+ v143
true
Unicode
Application
true
- v140
+ v143
Unicode
Application
false
- v140
+ v143
true
Unicode
diff --git a/ZLauncher/ZLauncher.vcxproj b/ZLauncher/ZLauncher.vcxproj
index b201c46..7407f9d 100644
--- a/ZLauncher/ZLauncher.vcxproj
+++ b/ZLauncher/ZLauncher.vcxproj
@@ -22,32 +22,32 @@
{13285DCD-009D-453A-9CC6-A02F584568B3}
Win32Proj
ZLauncher
- 8.1
+ 10.0
Application
true
- v140
+ v143
Unicode
Application
false
- v140
+ v143
true
Unicode
Application
true
- v140
+ v143
Unicode
Application
false
- v140
+ v143
true
Unicode
@@ -104,7 +104,7 @@
Windows
true
- $(SolutionDir)\_Output\$(PlatformTarget)\$(Configuration)\LzmaLib\;$(SolutionDir)\_Output\$(PlatformTarget)\$(Configuration)\ZPatcherLib\;$(SolutionDir)\$(Configuration)-Lib\;$(SolutionDir)\libs\wxWidgets\lib\vc_dll;$(SolutionDir)\libs\curl\build\Win32\VC14\LIB Debug - DLL Windows SSPI;%(AdditionalLibraryDirectories)
+ $(SolutionDir)\_Output\$(PlatformTarget)\$(Configuration)\LzmaLib\;$(SolutionDir)\_Output\$(PlatformTarget)\$(Configuration)\ZPatcherLib\;$(SolutionDir)\$(Configuration)-Lib\;$(SolutionDir)\libs\wxWidgets\lib\vc_dll;$(SolutionDir)\libs\curl\build\Win32\VC14.30\LIB Debug - DLL Windows SSPI;%(AdditionalLibraryDirectories)
LzmaLib.lib;ZPatcherLib.lib;ws2_32.lib;wininet.lib;wldap32.lib;comctl32.lib;crypt32.lib;libcurld.lib;%(AdditionalDependencies)
@@ -135,7 +135,7 @@ copy /y "$(SolutionDir)ZLauncher\ZLauncherRes" "$(OutDir)ZLauncherRes\" 1>NUL
Windows
true
- $(SolutionDir)\_Output\$(PlatformTarget)\$(Configuration)\LzmaLib\;$(SolutionDir)\_Output\$(PlatformTarget)\$(Configuration)\ZPatcherLib\;$(SolutionDir)\bin\$(Platform)-$(Configuration)-Lib\;$(SolutionDir)\libs\wxWidgets\lib\vc_x64_dll;$(SolutionDir)\libs\curl\build\Win64\VC14\LIB Debug - DLL Windows SSPI;%(AdditionalLibraryDirectories)
+ $(SolutionDir)\_Output\$(PlatformTarget)\$(Configuration)\LzmaLib\;$(SolutionDir)\_Output\$(PlatformTarget)\$(Configuration)\ZPatcherLib\;$(SolutionDir)\bin\$(Platform)-$(Configuration)-Lib\;$(SolutionDir)\libs\wxWidgets\lib\vc_x64_dll;$(SolutionDir)\libs\curl\build\Win64\VC14.30\LIB Debug - DLL Windows SSPI;%(AdditionalLibraryDirectories)
LzmaLib.lib;ZPatcherLib.lib;ws2_32.lib;wininet.lib;wldap32.lib;comctl32.lib;crypt32.lib;libcurld.lib;%(AdditionalDependencies)
@@ -168,7 +168,7 @@ copy /y "$(SolutionDir)ZLauncher\ZLauncherRes" "$(OutDir)ZLauncherRes\" 1>NUL
true
true
true
- $(SolutionDir)\_Output\$(PlatformTarget)\$(Configuration)\LzmaLib\;$(SolutionDir)\_Output\$(PlatformTarget)\$(Configuration)\ZPatcherLib\;$(SolutionDir)\$(Configuration)-Lib\;$(SolutionDir)\libs\wxWidgets\lib\vc_dll;$(SolutionDir)\libs\curl\build\Win32\VC14\LIB Release - DLL Windows SSPI;%(AdditionalLibraryDirectories)
+ $(SolutionDir)\_Output\$(PlatformTarget)\$(Configuration)\LzmaLib\;$(SolutionDir)\_Output\$(PlatformTarget)\$(Configuration)\ZPatcherLib\;$(SolutionDir)\$(Configuration)-Lib\;$(SolutionDir)\libs\wxWidgets\lib\vc_dll;$(SolutionDir)\libs\curl\build\Win32\VC14.30\LIB Release - DLL Windows SSPI;%(AdditionalLibraryDirectories)
LzmaLib.lib;ZPatcherLib.lib;ws2_32.lib;wininet.lib;wldap32.lib;comctl32.lib;crypt32.lib;libcurl.lib;%(AdditionalDependencies)
@@ -201,7 +201,7 @@ copy /y "$(SolutionDir)ZLauncher\ZLauncherRes" "$(OutDir)ZLauncherRes\" 1>NUL
true
true
true
- $(SolutionDir)\_Output\$(PlatformTarget)\$(Configuration)\LzmaLib\;$(SolutionDir)\_Output\$(PlatformTarget)\$(Configuration)\ZPatcherLib\;$(SolutionDir)\bin\$(Platform)-$(Configuration)-Lib\;$(SolutionDir)\libs\wxWidgets\lib\vc_x64_dll;$(SolutionDir)\libs\curl\build\Win64\VC14\LIB Release - DLL Windows SSPI;%(AdditionalLibraryDirectories)
+ $(SolutionDir)\_Output\$(PlatformTarget)\$(Configuration)\LzmaLib\;$(SolutionDir)\_Output\$(PlatformTarget)\$(Configuration)\ZPatcherLib\;$(SolutionDir)\bin\$(Platform)-$(Configuration)-Lib\;$(SolutionDir)\libs\wxWidgets\lib\vc_x64_dll;$(SolutionDir)\libs\curl\build\Win64\VC14.30\LIB Release - DLL Windows SSPI;%(AdditionalLibraryDirectories)
LzmaLib.lib;ZPatcherLib.lib;ws2_32.lib;wininet.lib;wldap32.lib;comctl32.lib;crypt32.lib;libcurl.lib;%(AdditionalDependencies)
diff --git a/ZLauncher/ZLauncherThread.cpp b/ZLauncher/ZLauncherThread.cpp
index 741fc70..f3e9a40 100644
--- a/ZLauncher/ZLauncherThread.cpp
+++ b/ZLauncher/ZLauncherThread.cpp
@@ -55,9 +55,6 @@ ZLauncherThread::~ZLauncherThread()
{
wxCriticalSectionLocker enter(m_pHandler->m_pThreadCS);
- // No harm in being overzealous here
- ZPatcher::DestroyLogSystem();
-
// the thread is being destroyed; make sure not to leave dangling pointers around
m_pHandler->m_pThread = NULL;
}
@@ -65,18 +62,17 @@ ZLauncherThread::~ZLauncherThread()
wxThread::ExitCode ZLauncherThread::Entry()
{
// TestDestroy() checks if the thread should be cancelled/destroyed
- if (TestDestroy()) { ZPatcher::DestroyLogSystem(); return (wxThread::ExitCode)0; }
+ if (TestDestroy()) { return (wxThread::ExitCode)0; }
ZPatcher::SetActiveLog("ZLauncher");
std::ostringstream tmphtml;
//////////////////////////////////////////////////////////////////////////
// Get the current version
- if (TestDestroy()) { ZPatcher::DestroyLogSystem(); return (wxThread::ExitCode)0; }
+ if (TestDestroy()) { return (wxThread::ExitCode)0; }
if (!GetTargetCurrentVersion(m_versionFile, m_LocalCurrentVersion))
{
ZLauncherFrame::UpdateProgress(0, "An error occurred while getting current application version. See the log for details.");
- ZPatcher::DestroyLogSystem();
return (wxThread::ExitCode)0;
}
@@ -85,11 +81,10 @@ wxThread::ExitCode ZLauncherThread::Entry()
//////////////////////////////////////////////////////////////////////////
// Check the Updates URL for the XML file with updates and process it.
- if (TestDestroy()) { ZPatcher::DestroyLogSystem(); return (wxThread::ExitCode)0; }
+ if (TestDestroy()) { return (wxThread::ExitCode)0; }
if (!CheckForUpdates(m_updateURL, m_LocalCurrentVersion))
{
ZLauncherFrame::UpdateProgress(0, "An error occurred while checking for updates. See the log for details.");
- ZPatcher::DestroyLogSystem();
return (wxThread::ExitCode)0;
}
@@ -109,7 +104,7 @@ wxThread::ExitCode ZLauncherThread::Entry()
//////////////////////////////////////////////////////////////////////////
// Create the directory where the updates will be downloaded.
- if (TestDestroy()) { ZPatcher::DestroyLogSystem(); return (wxThread::ExitCode)0; }
+ if (TestDestroy()) { return (wxThread::ExitCode)0; }
std::string updatesDirectory = "updates/";
errno = 0;
ZPatcher::CreateDirectoryTree(updatesDirectory);
@@ -117,7 +112,6 @@ wxThread::ExitCode ZLauncherThread::Entry()
{
ZPatcher::Log(ZPatcher::LOG_FATAL, "An error occurred while attempting to create the directory structure: %s", updatesDirectory.c_str(), strerror(errno));
ZLauncherFrame::UpdateProgress(0, "An error occurred while attempting to create the directory structure. See the log for details.");
- ZPatcher::DestroyLogSystem();
return (wxThread::ExitCode)0;
}
@@ -131,12 +125,10 @@ wxThread::ExitCode ZLauncherThread::Entry()
if (!SelfUpdate(shouldRestart))
{
ZPatcher::Log(ZPatcher::LOG_FATAL, "Error trying to perform a self update.");
- ZPatcher::DestroyLogSystem();
return (wxThread::ExitCode)0;;
}
else if (shouldRestart)
{
- ZPatcher::DestroyLogSystem();
wxQueueEvent(wxTheApp->GetTopWindow()->GetEventHandler(), new wxCloseEvent(wxEVT_CLOSE_WINDOW));
return (wxThread::ExitCode)0;
}
@@ -157,7 +149,7 @@ wxThread::ExitCode ZLauncherThread::Entry()
ZLauncherFrame::HTMLSetContent(tmphtml.str());
// Check if we should stop before each file download
- if (TestDestroy()) { ZPatcher::DestroyLogSystem(); return (wxThread::ExitCode)0; }
+ if (TestDestroy()) { return (wxThread::ExitCode)0; }
const Patch& patch = m_Patches[m_BestPatchPath[patchIndex]];
@@ -167,7 +159,6 @@ wxThread::ExitCode ZLauncherThread::Entry()
{
// This is bad! Our URL is malformed (no slashes in it!)
ZPatcher::Log(ZPatcher::LOG_FATAL, "Invalid Update URL: %s", patch.fileURL.c_str());
- ZPatcher::DestroyLogSystem();
return (wxThread::ExitCode)0;
}
@@ -216,14 +207,13 @@ wxThread::ExitCode ZLauncherThread::Entry()
{
// User attempted to close the window, abort operation!
ZPatcher::Log(ZPatcher::LOG_WARNING, "Download aborted by user.");
- ZPatcher::DestroyLogSystem();
return (wxThread::ExitCode)0;
}
}
}
// This is a great place to check for a stopping condition.
- if (TestDestroy()) { ZPatcher::DestroyLogSystem(); return (wxThread::ExitCode)0; }
+ if (TestDestroy()) { return (wxThread::ExitCode)0; }
m_pHandler->m_pThreadCS.Enter();
@@ -233,7 +223,6 @@ wxThread::ExitCode ZLauncherThread::Entry()
if (!SaveTargetNewVersion(m_versionFile, patch.targetBuildNumber))
{
ZPatcher::Log(ZPatcher::LOG_FATAL, "The patch was applied, but couldn't write the updated version info to %s", m_versionFile.c_str());
- ZPatcher::DestroyLogSystem();
m_pHandler->m_pThreadCS.Leave();
return (wxThread::ExitCode)0;
}
@@ -243,7 +232,6 @@ wxThread::ExitCode ZLauncherThread::Entry()
else
{
ZPatcher::Log(ZPatcher::LOG_FATAL, "Unable to apply the patch %s", localFullPath.c_str());
- ZPatcher::DestroyLogSystem();
m_pHandler->m_pThreadCS.Leave();
return (wxThread::ExitCode)0;
}
@@ -259,19 +247,16 @@ wxThread::ExitCode ZLauncherThread::Entry()
if (!SelfUpdate(shouldRestart))
{
ZPatcher::Log(ZPatcher::LOG_FATAL, "Error trying to perform a self update.");
- ZPatcher::DestroyLogSystem();
return (wxThread::ExitCode)0;;
}
else
if (shouldRestart)
{
- ZPatcher::DestroyLogSystem();
wxQueueEvent(wxTheApp->GetTopWindow()->GetEventHandler(), new wxCloseEvent(wxEVT_CLOSE_WINDOW));
return (wxThread::ExitCode)0;
}
#endif
}
- ZPatcher::DestroyLogSystem();
// Update progress bar and enable launch button
ZLauncherFrame::UpdateProgress(100, "Up-to-date!");
diff --git a/ZPatcher.sln b/ZPatcher.sln
index 4cd2ea4..6c2d43d 100644
--- a/ZPatcher.sln
+++ b/ZPatcher.sln
@@ -1,7 +1,7 @@
Microsoft Visual Studio Solution File, Format Version 12.00
-# Visual Studio 14
-VisualStudioVersion = 14.0.25420.1
+# Visual Studio Version 17
+VisualStudioVersion = 17.6.33712.159
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LzmaLib", "libs\LzmaLib\LzmaLib.vcxproj", "{C72F7F04-8109-485F-88D4-CB5653CB7DDD}"
EndProject
@@ -24,7 +24,7 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ZPatcherLib", "ZPatcherLib\
{C72F7F04-8109-485F-88D4-CB5653CB7DDD} = {C72F7F04-8109-485F-88D4-CB5653CB7DDD}
EndProjectSection
EndProject
-Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libcurl", "libs\curl\projects\Windows\VC14\lib\libcurl.vcxproj", "{DA6F56B4-06A4-441D-AD70-AC5A7D51FADB}"
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libcurl", "libs\curl\projects\Windows\VC14.30\lib\libcurl.vcxproj", "{DA6F56B4-06A4-441D-AD70-AC5A7D51FADB}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ZUpdater", "ZUpdater\ZUpdater.vcxproj", "{2F868D36-FD16-4C3A-B444-8C2CCA1A6A0D}"
ProjectSection(ProjectDependencies) = postProject
diff --git a/ZPatcherLib/ApplyPatch.cpp b/ZPatcherLib/ApplyPatch.cpp
index 20fe710..8449825 100644
--- a/ZPatcherLib/ApplyPatch.cpp
+++ b/ZPatcherLib/ApplyPatch.cpp
@@ -29,35 +29,48 @@
#define fseek64 fseek
#endif
-void ZPatcher::PrintPatchApplyingProgressBar(const float& Percentage)
+/**
+ * Apply the patch file to the target path
+ */
+static bool DoApplyPatchFile(FILE* patchFile, const std::string& targetPath, const uint64_t& previousVersionNumber, ZPatcher::ProgressCallback progressCallback = &ZPatcher::PrintPatchApplyingProgressBar);
+
+/**
+ * Restore the target backup list
+ */
+static bool RestoreBackup(std::vector& backupFileList, std::vector& addedFileList, const std::string& baseDirectory, const std::string& previousVersionNumber);
+
+
+void ZPatcher::PrintPatchApplyingProgressBar(const float& percentage)
{
- int barWidth = 60;
+ constexpr int barWidth = 60;
fprintf(stdout, "\xd[");
- int pos = (int)(barWidth * Percentage / 100.0f);
+ int pos = (int)(barWidth * percentage / 100.0f);
for (int i = 0; i < barWidth; ++i)
{
if (i < pos) fprintf(stdout, "=");
else if (i == pos) fprintf(stdout, ">");
else fprintf(stdout, " ");
}
- fprintf(stdout, "] %0.2f %% ", Percentage);
+ fprintf(stdout, "] %0.2f %% ", percentage);
fflush(stdout);
}
-bool ZPatcher::DoApplyPatchFile(FILE* patchFile, const std::string& targetPath, uint64_t &previousVersionNumber, ProgressCallback progressCallback)
+static bool DoApplyPatchFile(FILE* patchFile, const std::string& targetPath, const uint64_t& previousVersionNumber, ZPatcher::ProgressCallback progressCallback)
{
- std::string prevVersionNumber = std::to_string(previousVersionNumber);
+ using namespace ZPatcher;
+
+ const std::string prevVersionNumber = std::to_string(previousVersionNumber);
fseek64(patchFile, 0LL, SEEK_END);
- int64_t patchFileSize = ftell64(patchFile);
+ const int64_t patchFileSize = ftell64(patchFile);
rewind(patchFile);
std::string normalizedTargetPath = targetPath;
NormalizeFileName(normalizedTargetPath);
- size_t fileNameLength = normalizedTargetPath.length();
+ const size_t fileNameLength = normalizedTargetPath.length();
if (fileNameLength == 0 || normalizedTargetPath[fileNameLength - 1] != '/')
{
Log(LOG_FATAL, "Invalid target directory. It must end either with \\ or /.");
@@ -133,18 +146,16 @@ bool ZPatcher::DoApplyPatchFile(FILE* patchFile, const std::string& targetPath,
{
// Everything went fine, so, let's do a backup cleanup
Log(LOG, "Patching successful! Removing backup directory.");
- std::string backupDirectoryName = normalizedTargetPath + "/" + "backup-" + prevVersionNumber + "/";
+ const std::string backupDirectoryName = normalizedTargetPath + "/" + "backup-" + prevVersionNumber + "/";
DeleteDirectoryTree(backupDirectoryName);
progressCallback(100.0f);
}
else
{
// Something bad happened. Roll back.
- bool restoreSucess = true;
Log(LOG_FATAL, "Something went wrong with the patch process! Rolling back!");
- restoreSucess = restoreSucess && RestoreBackup(backupFileList, addedFileList, normalizedTargetPath, prevVersionNumber);
-
- if (!restoreSucess)
+ const bool restoreSuccess = RestoreBackup(backupFileList, addedFileList, normalizedTargetPath, prevVersionNumber);
+ if (!restoreSuccess)
Log(LOG_FATAL, "At least one file failed to be restored! The application is probably in an inconsistent state!");
}
@@ -153,15 +164,13 @@ bool ZPatcher::DoApplyPatchFile(FILE* patchFile, const std::string& targetPath,
return success;
}
-bool ZPatcher::ApplyPatchFile(const std::string& patchFileName, const std::string& targetPath, uint64_t& previousVersionNumber, ProgressCallback progressCallback)
+bool ZPatcher::ApplyPatchFile(const std::string& patchFileName, const std::string& targetPath, const uint64_t& previousVersionNumber, ProgressCallback progressCallback)
{
- FILE* patchFile;
-
std::string normalizedPatchFileName = patchFileName;
NormalizeFileName(normalizedPatchFileName);
+ FILE* patchFile = fopen(normalizedPatchFileName.c_str(), "rb");
errno = 0;
- patchFile = fopen(normalizedPatchFileName.c_str(), "rb");
if(errno != 0)
{
Log(LOG_FATAL, "Unable to open for reading the patch file %s: %s", normalizedPatchFileName.c_str(), strerror(errno));
@@ -170,15 +179,17 @@ bool ZPatcher::ApplyPatchFile(const std::string& patchFileName, const std::strin
Log(LOG, "Reading patch file %s", normalizedPatchFileName.c_str());
- bool success = DoApplyPatchFile(patchFile, targetPath, previousVersionNumber, progressCallback);
+ const bool success = DoApplyPatchFile(patchFile, targetPath, previousVersionNumber, progressCallback);
fclose(patchFile);
return success;
}
-bool ZPatcher::RestoreBackup(std::vector& backupFileList, std::vector& addedFileList, const std::string& baseDirectory, std::string previousVersionNumber)
+static bool RestoreBackup(std::vector& backupFileList, std::vector& addedFileList, const std::string& baseDirectory, const std::string& previousVersionNumber)
{
+ using namespace ZPatcher;
+
bool result = true;
for (std::vector::iterator itr = backupFileList.begin(); itr < backupFileList.end(); ++itr)
diff --git a/ZPatcherLib/ApplyPatch.h b/ZPatcherLib/ApplyPatch.h
index d89f039..508e201 100644
--- a/ZPatcherLib/ApplyPatch.h
+++ b/ZPatcherLib/ApplyPatch.h
@@ -13,7 +13,6 @@
#ifndef _APPLYPATCH_H_
#define _APPLYPATCH_H_
-#include
#include
namespace ZPatcher
@@ -24,23 +23,12 @@ namespace ZPatcher
/**
* Print the progress bar used applying a patch
*/
- void PrintPatchApplyingProgressBar(const float& Percentage);
-
- /**
- * Apply the patch file to the target path
- */
- bool DoApplyPatchFile(FILE* patchFile, const std::string& targetPath, uint64_t& previousVersionNumber, ProgressCallback progressCallback = &PrintPatchApplyingProgressBar);
+ void PrintPatchApplyingProgressBar(const float& percentage);
/**
* Apply the patch file to the target path, given the patch name
*/
- bool ApplyPatchFile(const std::string& patchFileName, const std::string& targetPath, uint64_t& previousVersionNumber, ProgressCallback progressCallback = &PrintPatchApplyingProgressBar);
-
- /**
- * Restore the target backup list
- */
- bool RestoreBackup(std::vector& backupFileList, std::vector& addedFileList, const std::string& baseDirectory, std::string previousVersionNumber);
-
+ bool ApplyPatchFile(const std::string& patchFileName, const std::string& targetPath, const uint64_t& previousVersionNumber, ProgressCallback progressCallback = &PrintPatchApplyingProgressBar);
}
diff --git a/ZPatcherLib/CreatePatch.cpp b/ZPatcherLib/CreatePatch.cpp
index 37e3fa3..301b323 100644
--- a/ZPatcherLib/CreatePatch.cpp
+++ b/ZPatcherLib/CreatePatch.cpp
@@ -1,7 +1,7 @@
//////////////////////////////////////////////////////////////////////////
//
// ZPatcher - Patcher system - Part of the ZUpdater suite
-// Felipe "Zoc" Silveira - (c) 2016-2018
+// Felipe "Zoc" Silveira - (c) 2016-2023
//
//////////////////////////////////////////////////////////////////////////
//
@@ -12,22 +12,35 @@
#include "stdafx.h"
-#include "CreatePatch.h"
-#include "FileUtils.h"
-#include "Lzma2Encoder.h"
#include
#include
#include
-#include
+#include
+#include "CreatePatch.h"
+#include "FileUtils.h"
+#include "Lzma2Encoder.h"
#include "LogSystem.h"
+#include "ZPatcherCurrentVersion.h"
-void ZPatcher::PrintCreatePatchProgressBar(const float& Percentage, const uint64_t& leftAmount, const uint64_t& rightAmount)
+
+/**
+ * Creates the patch file with all the changes listed in patchFileList.
+ */
+static bool DoCreatePatchFile(FILE* patchFile, const std::string& newVersionPath, ZPatcher::PatchFileList_t* patchFileList, ZPatcher::ProgressCallback progressFunction, ICompressProgress LZMAProgressCallback = { reinterpret_cast(&ZPatcher::OnProgress) });
+
+/**
+* Print the progress bar used when comparing directories
+*/
+static void PrintCreatePatchProgressBar(const float& percentage, const uint64_t& leftAmount, const uint64_t& rightAmount);
+
+
+static void PrintCreatePatchProgressBar(const float& percentage, const uint64_t& leftAmount, const uint64_t& rightAmount)
{
int barWidth = 60;
fprintf(stdout, "\xd[");
- int pos = (int)(barWidth * Percentage / 100.0f);
+ int pos = (int)(barWidth * percentage / 100.0f);
for (int i = 0; i < barWidth; ++i)
{
if (i < pos) fprintf(stdout, "=");
@@ -35,27 +48,38 @@ void ZPatcher::PrintCreatePatchProgressBar(const float& Percentage, const uint64
else fprintf(stdout, " ");
}
- fprintf(stdout, "] %0.2f%% %" PRIu64 "/%" PRIu64, Percentage, leftAmount, rightAmount);
+ fprintf(stdout, "] %0.2f%% %" PRIu64 "/%" PRIu64, percentage, leftAmount, rightAmount);
fflush(stdout);
}
-ZPatcher::PatchFileList_t* ZPatcher::GetDifferences(std::string& oldVersion, std::string& newVersion, ProgressCallback progressFunction)
+ZPatcher::PatchFileList_t* ZPatcher::GetDifferences(const std::string& oldVersion, const std::string& newVersion)
+{
+ return GetDifferencesEx(oldVersion, newVersion, &PrintCreatePatchProgressBar);
+}
+
+ZPatcher::PatchFileList_t* ZPatcher::GetDifferencesEx(const std::string& oldVersion, const std::string& newVersion, ProgressCallback progressFunction)
{
PatchFileList_t* patchFileList = new PatchFileList_t();
+ std::string normalizedOldVersion = oldVersion;
+ NormalizeFileName(normalizedOldVersion);
+
+ std::string normalizedNewVersion = newVersion;
+ NormalizeFileName(normalizedNewVersion);
+
std::vector oldVersionFileList;
- GetFilesInDirectory(oldVersionFileList, oldVersion);
+ GetFilesInDirectory(oldVersionFileList, normalizedOldVersion);
std::vector newVersionFileList;
- GetFilesInDirectory(newVersionFileList, newVersion);
+ GetFilesInDirectory(newVersionFileList, normalizedNewVersion);
// Sort them now to avoid worries later. (Easier to find added/deleted files)
std::sort(oldVersionFileList.begin(), oldVersionFileList.end());
std::sort(newVersionFileList.begin(), newVersionFileList.end());
- unsigned int oldFileIndex = 0;
- unsigned int newFileIndex = 0;
+ uint64_t oldFileIndex = 0;
+ uint64_t newFileIndex = 0;
fprintf(stdout, "Detecting file differences between folders...\n");
@@ -70,12 +94,12 @@ ZPatcher::PatchFileList_t* ZPatcher::GetDifferences(std::string& oldVersion, std
if (oldFileName == newFileName)
{
// Check if we are dealing with directories.
- size_t fileNameLength = oldFileName.length();
+ const size_t fileNameLength = oldFileName.length();
if (fileNameLength > 0 && oldFileName[fileNameLength - 1] != '/')
{
// Check if the files have the same contents
bool identical;
- bool success = AreFilesIdentical(oldVersion + "/" + oldFileName, newVersion + "/" + newFileName, identical);
+ const bool success = AreFilesIdentical(normalizedOldVersion + "/" + oldFileName, normalizedNewVersion + "/" + newFileName, identical);
assert(success == true); // TODO: Handle this.
@@ -133,8 +157,13 @@ ZPatcher::PatchFileList_t* ZPatcher::GetDifferences(std::string& oldVersion, std
return patchFileList;
}
-bool ZPatcher::DoCreatePatchFile(FILE* patchFile, const std::string& newVersionPath, PatchFileList_t* patchFileList, ProgressCallback progressFunction, ICompressProgress LZMAProgressCallback)
+static bool DoCreatePatchFile(FILE* patchFile, const std::string& newVersionPath, ZPatcher::PatchFileList_t* patchFileList, ZPatcher::ProgressCallback progressFunction, ICompressProgress LZMAProgressCallback)
{
+ using namespace ZPatcher;
+
+ std::string normalizedNewVersionPath = newVersionPath;
+ NormalizeFileName(normalizedNewVersionPath);
+
// Initialize our custom LZMA2 Encoder
CLzma2EncHandle hLzma2Enc = InitLzma2Encoder();
@@ -169,11 +198,11 @@ bool ZPatcher::DoCreatePatchFile(FILE* patchFile, const std::string& newVersionP
Log(LOG, "[add] %s", itr->c_str());
- size_t fileNameLength = itr->length();
+ const size_t fileNameLength = itr->length();
if (fileNameLength > 0 && (*itr)[fileNameLength - 1] != '/')
{
- WriteFileInfo(patchFile, Patch_File_Add, itr->c_str());
- std::string localPath = newVersionPath + "/" + *itr;
+ WriteFileInfo(patchFile, Patch_File_Add, *itr);
+ std::string localPath = normalizedNewVersionPath + "/" + *itr;
if (!WriteCompressedFile(hLzma2Enc, localPath, patchFile, LZMAProgressCallback))
{
@@ -182,7 +211,7 @@ bool ZPatcher::DoCreatePatchFile(FILE* patchFile, const std::string& newVersionP
}
else
{
- WriteFileInfo(patchFile, Patch_Dir_Add, itr->c_str());
+ WriteFileInfo(patchFile, Patch_Dir_Add, *itr);
}
}
@@ -196,7 +225,7 @@ bool ZPatcher::DoCreatePatchFile(FILE* patchFile, const std::string& newVersionP
Log(LOG, "[mod] %s", itr->c_str());
WriteFileInfo(patchFile, Patch_File_Replace, itr->c_str());
- std::string localPath = newVersionPath + "/" + *itr;
+ std::string localPath = normalizedNewVersionPath + "/" + *itr;
if (!WriteCompressedFile(hLzma2Enc, localPath, patchFile, LZMAProgressCallback))
{
@@ -221,20 +250,28 @@ bool ZPatcher::DoCreatePatchFile(FILE* patchFile, const std::string& newVersionP
return true;
}
+bool ZPatcher::CreatePatchFile(const std::string& patchFileName, const std::string& newVersionPath, PatchFileList_t* patchFileList)
+{
+ return CreatePatchFileEx(patchFileName, newVersionPath, patchFileList, &PrintCreatePatchProgressBar, reinterpret_cast(&OnProgress));
+}
-bool ZPatcher::CreatePatchFile(const std::string& patchFileName, const std::string& newVersionPath, PatchFileList_t* patchFileList, ProgressCallback progressFunction, ICompressProgress LZMAProgressCallback)
+
+bool ZPatcher::CreatePatchFileEx(const std::string& patchFileName, const std::string& newVersionPath, PatchFileList_t* patchFileList, ProgressCallback progressFunction, LZMA_ICompressProgress LZMAProgressCallback)
{
FILE* patchFile;
+ std::string normalizedPatchFileName = patchFileName;
+ NormalizeFileName(normalizedPatchFileName);
+
errno = 0;
- patchFile = fopen(patchFileName.c_str(), "wb");
+ patchFile = fopen(normalizedPatchFileName.c_str(), "wb");
if (errno != 0)
{
- Log(LOG_FATAL, "Error opening file \"%s\" to write patch data: %s", patchFileName.c_str(), strerror(errno));
+ Log(LOG_FATAL, "Error opening file \"%s\" to write patch data: %s", normalizedPatchFileName.c_str(), strerror(errno));
return false;
}
- bool result = DoCreatePatchFile(patchFile, newVersionPath, patchFileList, progressFunction, LZMAProgressCallback);
+ const bool result = DoCreatePatchFile(patchFile, newVersionPath, patchFileList, progressFunction, { reinterpret_cast(LZMAProgressCallback) });
fclose(patchFile);
diff --git a/ZPatcherLib/CreatePatch.h b/ZPatcherLib/CreatePatch.h
index b21c70b..c6be501 100644
--- a/ZPatcherLib/CreatePatch.h
+++ b/ZPatcherLib/CreatePatch.h
@@ -13,50 +13,57 @@
#ifndef _CREATEPATCH_H_
#define _CREATEPATCH_H_
+#include
#include
-#include "LzmaInterfaces.h"
namespace ZPatcher
{
/// Our function pointer callback for progress display
typedef void(*ProgressCallback)(const float& percentage, const uint64_t& processedAmount, const uint64_t& totalToBeProcessed);
+ /// This is the same as `CompressProgressCallback`, defined in LzmaInterfaces.h, but without the LZMA sdk dependence - using a void* in place of ICompressProgress*
+ typedef int(*LZMA_ICompressProgress)(const void* p, uint64_t inSize, uint64_t outSize);
+
/**
* This structure holds the files that were Removed, Modified or Added in the patch to be created.
* Note: This is purposely declared as a class to simplify calling the constructor of each vector ;)
*/
- class PatchFileList_t
+ struct PatchFileList_t
{
- public:
std::vector RemovedFileList;
std::vector ModifiedFileList;
std::vector AddedFileList;
};
/**
- * Print the progress bar used when comparing directories
+ * Get the difference between two directories (oldVersion and newVersion) and build a PatchFileList_t containing all the changes
*/
- void PrintCreatePatchProgressBar(const float& Percentage, const uint64_t& leftAmount, const uint64_t& rightAmount);
+ PatchFileList_t* GetDifferences(const std::string& oldVersion, const std::string& newVersion);
/**
* Get the difference between two directories (oldVersion and newVersion) and build a PatchFileList_t containing all the changes
+ * This version allows the customization of the Progress Callback
*/
- PatchFileList_t* GetDifferences(std::string& oldVersion, std::string& newVersion, ProgressCallback progressFunction = &PrintCreatePatchProgressBar);
+ PatchFileList_t* GetDifferencesEx(const std::string& oldVersion, const std::string& newVersion, ProgressCallback progressFunction);
/**
* Creates the patch file with all the changes listed in patchFileList.
- * patchfile is the file handle for the target patch file (output file)
+ * patchFileName is the name of the output file
* newVersionPath is the directory that contains the updated files
* patchFileList is a PatchFileList_t filled by GetDifferences() with the changes between directories
- * progressFunction is a pointer to a function to display the current progress. It defaults to our own PrintCreatePatchProgressBar(), but it can be changed.
- * LZMAProgressCallback is a pointer to a function to display the current progress of the file being compressed by the LZMA algorithm. It defaults to ZPatcher::OnProgress()
+ * This function will default to use PrintCreatePatchProgressBar() and OnProgress() (from LZMAInterfaces.h). To change those, use CreatePatchFileEx().
*/
- bool DoCreatePatchFile(FILE* patchFile, const std::string& newVersionPath, PatchFileList_t* patchFileList, ProgressCallback progressFunction = &PrintCreatePatchProgressBar, ICompressProgress LZMAProgressCallback = { reinterpret_cast(&OnProgress) });
+ bool CreatePatchFile(const std::string& patchFileName, const std::string& newVersionPath, PatchFileList_t* patchFileList);
/**
- * This is a shortcut to CreatePatchFile() that receives the output patch file as a string
+ * Creates the patch file with all the changes listed in patchFileList.
+ * patchFileName is the name of the output file
+ * newVersionPath is the directory that contains the updated files
+ * patchFileList is a PatchFileList_t filled by GetDifferences() with the changes between directories
+ * progressFunction is a pointer to a function to display the current progress of file processing.
+ * LZMAProgressCallback is a pointer to a function to display the current progress of the file being compressed by the LZMA algorithm.
*/
- bool CreatePatchFile(const std::string& patchFileName, const std::string& newVersionPath, PatchFileList_t* patchFileList, ProgressCallback progressFunction = &PrintCreatePatchProgressBar, ICompressProgress LZMAProgressCallback = { reinterpret_cast(&OnProgress) });
+ bool CreatePatchFileEx(const std::string& patchFileName, const std::string& newVersionPath, PatchFileList_t* patchFileList, ProgressCallback progressFunction, LZMA_ICompressProgress LZMAProgressCallback);
}
#endif // _CREATEPATCH_H_
diff --git a/ZPatcherLib/FileUtils.h b/ZPatcherLib/FileUtils.h
index d44a211..6bcbca9 100644
--- a/ZPatcherLib/FileUtils.h
+++ b/ZPatcherLib/FileUtils.h
@@ -16,7 +16,6 @@
#include
#include
-#include
namespace ZPatcher
{
diff --git a/ZPatcherLib/LogSystem.cpp b/ZPatcherLib/LogSystem.cpp
index af8151b..ec772cc 100644
--- a/ZPatcherLib/LogSystem.cpp
+++ b/ZPatcherLib/LogSystem.cpp
@@ -1,7 +1,7 @@
//////////////////////////////////////////////////////////////////////////
//
// ZPatcher - Patcher system - Part of the ZUpdater suite
-// Felipe "Zoc" Silveira - (c) 2016-2018
+// Felipe "Zoc" Silveira - (c) 2016-2022
//
//////////////////////////////////////////////////////////////////////////
//
@@ -11,36 +11,65 @@
//////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
-#include
-#include
#include
+#include
#include
-#include
#include
+#include
+#include