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 +#include #include "LogSystem.h" #include "FileUtils.h" -// Temporary new global variable, avoiding conflict with the old system for now. -std::map ZPatcher::g_NewLogSystem; -// For now, the log directory will be specified here. -std::string ZPatcher::g_LogDirectory = "./Logs/"; +// Map that holds all the open log files. CloseLog is a Deleter that will close the file handle when destroying the program. +struct CloseLog { void operator()(FILE* f) { if (f) { fclose(f); } } }; +static std::map> g_NewLogSystem; -static std::string ActiveLogName = ""; +// This specifies the directory that will receive the log files. TODO: Make this more flexible in the future. +static std::string g_LogDirectory = "./Logs/"; -bool ZPatcher::SetActiveLog(const std::string& logName) +// Current active log name +static std::string ActiveLogName; + +/** + * Build a human-readable timestamp in the format yyyy-mm-dd-hh-mm-ss + */ +static std::string BuildHumanTimeStamp(); + +/** + * This function will create the log directory, defined by g_LogDirectory + * and prepare the file handle to receive the log data. + */ +static bool InitNewLogFile(const std::string& logName); + +/** + * The function that actually does the logging. + * This should not be called directly. + */ +static void DoLog(const std::string& logName, ZPatcher::LogLevel level, const char* format, va_list args); + +static std::string BuildHumanTimeStamp() { - if (logName == "") - { - fprintf(stderr, "Error attempting to set the active log: The log name can't be empty!"); - return false; - } + using std::chrono::system_clock; + time_t tt = system_clock::to_time_t(system_clock::now()); - ActiveLogName = logName; - return true; + tm timeinfo; + +#ifdef _WIN32 + localtime_s(&timeinfo, &tt); +#else + localtime_r(&tt, &timeinfo); +#endif + + constexpr size_t BUFFER_SIZE = 20; + char humanTimestamp[BUFFER_SIZE]; + snprintf(humanTimestamp, BUFFER_SIZE, "%02d-%02d-%02d-%02d-%02d-%02d", timeinfo.tm_year + 1900, timeinfo.tm_mon + 1, timeinfo.tm_mday, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec); + + return { humanTimestamp }; } -bool ZPatcher::InitNewLogFile(const std::string& logName) +static bool InitNewLogFile(const std::string& logName) { // Prepare the filename of the log and location that will receive it. std::string logFile = g_LogDirectory; @@ -48,22 +77,18 @@ bool ZPatcher::InitNewLogFile(const std::string& logName) if (g_LogDirectory.back() != '/') // Check if it has a trailing slash. If it doesn't, add it. logFile += "/"; - logFile += "Logs/"; - logFile += BuildHumanTimeStamp(); - logFile += " - "; - logFile += logName; - logFile += ".log"; + logFile += BuildHumanTimeStamp() + " - " + logName + ".log"; - NormalizeFileName(logFile); - CreateDirectoryTree(logFile, false); + ZPatcher::NormalizeFileName(logFile); + ZPatcher::CreateDirectoryTree(logFile, false); if (g_NewLogSystem[logName] == nullptr) { errno = 0; - g_NewLogSystem[logName] = fopen(logFile.c_str(), "wb"); + g_NewLogSystem[logName] = std::unique_ptr(fopen(logFile.c_str(), "wb")); if (errno != 0) { - int error = errno; + const int error = errno; fprintf(stderr, "Unable to open log file \"%s\" for writing: %s", logFile.c_str(), strerror(error)); return false; } @@ -78,49 +103,11 @@ bool ZPatcher::InitNewLogFile(const std::string& logName) return true; } -std::string ZPatcher::BuildHumanTimeStamp() -{ - using std::chrono::system_clock; - time_t tt = system_clock::to_time_t(system_clock::now()); - - tm timeinfo; - -#ifdef _WIN32 - localtime_s(&timeinfo, &tt); -#else - localtime_r(&tt, &timeinfo); -#endif - - std::string humanTimestamp; - - char buffer[16]; - sprintf(buffer, "%02d", timeinfo.tm_year + 1900); - humanTimestamp += buffer; - humanTimestamp += "-"; - sprintf(buffer, "%02d", timeinfo.tm_mon + 1); - humanTimestamp += buffer; - humanTimestamp += "-"; - sprintf(buffer, "%02d", timeinfo.tm_mday); - humanTimestamp += buffer; - humanTimestamp += "-"; - sprintf(buffer, "%02d", timeinfo.tm_hour); - humanTimestamp += buffer; - humanTimestamp += "-"; - sprintf(buffer, "%02d", timeinfo.tm_min); - humanTimestamp += buffer; - humanTimestamp += "-"; - sprintf(buffer, "%02d", timeinfo.tm_sec); - humanTimestamp += buffer; - - return humanTimestamp; -} - -// We don't want anyone calling this form somewhere else. -static void DoLog(std::string logName, ZPatcher::LogLevel level, const char* format, va_list args) +static void DoLog(const std::string& logName, ZPatcher::LogLevel level, const char* format, va_list args) { using namespace ZPatcher; - if (logName == "") + if (logName.empty()) { fprintf(stderr, "Error attempting to create log entry: The log name can't be empty!!!"); return; @@ -130,7 +117,7 @@ static void DoLog(std::string logName, ZPatcher::LogLevel level, const char* for if (!InitNewLogFile(logName)) return; - FILE* targetLog = g_NewLogSystem[logName]; + FILE* targetLog = g_NewLogSystem[logName].get(); fprintf(targetLog, "[%s] ", BuildHumanTimeStamp().c_str()); @@ -158,23 +145,21 @@ static void DoLog(std::string logName, ZPatcher::LogLevel level, const char* for fflush(targetLog); } -void ZPatcher::Log(std::string logName, LogLevel level, const char* format, ...) +bool ZPatcher::SetActiveLog(const std::string& logName) { - if (logName == "") + if (logName.empty()) { - fprintf(stderr, "Error attempting to log entry: The log name can't be empty!"); - return; + fprintf(stderr, "Error attempting to set the active log: The log name can't be empty!"); + return false; } - va_list args; - va_start(args, format); - DoLog(logName, level, format, args); - va_end(args); + ActiveLogName = logName; + return true; } void ZPatcher::Log(LogLevel level, const char* format, ...) { - if (ActiveLogName == "") + if (ActiveLogName.empty()) { fprintf(stderr, "Error attempting to log to active log: No active log set!"); return; @@ -186,13 +171,16 @@ void ZPatcher::Log(LogLevel level, const char* format, ...) va_end(args); } -void ZPatcher::DestroyLogSystem() +void ZPatcher::LogEx(std::string logName, LogLevel level, const char* format, ...) { - for(auto &logEntry : g_NewLogSystem) - if (logEntry.second) - { - fclose(logEntry.second); - logEntry.second = nullptr; - } -} + if (logName.empty()) + { + fprintf(stderr, "Error attempting to log entry: The log name can't be empty!"); + return; + } + va_list args; + va_start(args, format); + DoLog(logName, level, format, args); + va_end(args); +} diff --git a/ZPatcherLib/LogSystem.h b/ZPatcherLib/LogSystem.h index 12291bb..6b591cb 100644 --- a/ZPatcherLib/LogSystem.h +++ b/ZPatcherLib/LogSystem.h @@ -1,7 +1,7 @@ ////////////////////////////////////////////////////////////////////////// // // ZPatcher - Patcher system - Part of the ZUpdater suite -// Felipe "Zoc" Silveira - (c) 2016-2018 +// Felipe "Zoc" Silveira - (c) 2016-2022 // ////////////////////////////////////////////////////////////////////////// // @@ -13,18 +13,10 @@ #ifndef _LOGSYSTEM_H_ #define _LOGSYSTEM_H_ -#include #include -#include namespace ZPatcher { - // Map that holds all the open log files. - extern std::map g_NewLogSystem; - - // This specifies the directory that will receive the log files - extern std::string g_LogDirectory; - enum LogLevel { LOG, @@ -33,25 +25,14 @@ namespace ZPatcher LOG_FATAL, }; - // This will create the directory structure defined in - // This function will create a Log/ directory and place files in there. - //No logging will be done if this isn't called. - bool InitNewLogFile(const std::string& logName); - + // Set the current Active log file to be used by all Log() calls. bool SetActiveLog(const std::string& logName); - // Build a human-readable timestamp in the format yyyy-mm-dd-hh-mm-ss - std::string BuildHumanTimeStamp(); - - // Add a line to the target log file. The format should be the same as the one used in printf - void Log(std::string logName, LogLevel level, const char* format, ...); - - // Add a line to the ACTIVE log file. Use SetActiveLog() to set the active log file. + // Add a line to the Active log file - Use SetActiveLog() to set the active log file. Format is the same as printf() void Log(LogLevel level, const char* format, ...); - // Close the log file - void DestroyLogSystem(); + // Add a line to the Target log file. Format is the same as printf() + void LogEx(std::string logName, LogLevel level, const char* format, ...); } #endif //_LOGSYSTEM_H_ -#pragma once diff --git a/ZPatcherLib/Lzma2Decoder.cpp b/ZPatcherLib/Lzma2Decoder.cpp index 57fb461..79fa4f3 100644 --- a/ZPatcherLib/Lzma2Decoder.cpp +++ b/ZPatcherLib/Lzma2Decoder.cpp @@ -14,6 +14,7 @@ #include #include #include +#include "Alloc.h" #include "Lzma2Dec.h" #include "Lzma2Decoder.h" #include "LzmaInterfaces.h" @@ -32,7 +33,7 @@ CLzma2Dec* ZPatcher::InitLzma2Decoder(const Byte& props) CLzma2Dec* dec = static_cast(malloc(sizeof(CLzma2Dec))); Lzma2Dec_Construct(dec); - SRes res = Lzma2Dec_Allocate(dec, props, &LzmaSzAlloc); + SRes res = Lzma2Dec_Allocate(dec, props, &g_Alloc); assert(res == SZ_OK); return dec; @@ -40,7 +41,7 @@ CLzma2Dec* ZPatcher::InitLzma2Decoder(const Byte& props) void ZPatcher::DestroyLzma2Decoder(CLzma2Dec* decoder) { - Lzma2Dec_Free(decoder, &LzmaSzAlloc); + Lzma2Dec_Free(decoder, &g_Alloc); } Byte ZPatcher::ReadPatchFileHeader(FILE* source, Byte& Lzma2Properties) diff --git a/ZPatcherLib/Lzma2Decoder.h b/ZPatcherLib/Lzma2Decoder.h index ecb736f..d1435a1 100644 --- a/ZPatcherLib/Lzma2Decoder.h +++ b/ZPatcherLib/Lzma2Decoder.h @@ -14,11 +14,10 @@ #ifndef _LZMA2DECODER_H_ #define _LZMA2DECODER_H_ -#include "ZPatcherCurrentVersion.h" -#include "Lzma2Dec.h" #include #include -#include +#include "ZPatcherCurrentVersion.h" +#include "Lzma2Dec.h" namespace ZPatcher { @@ -42,8 +41,6 @@ namespace ZPatcher // Decompress the file data stored in the patch file (only call this in the correct operations!). This operation creates the target file. NOTE: version field is a hack to allow easy migration from patch version 1 to 2. TODO: Rewrite this in a flexible way. bool FileDecompress(CLzma2Dec* decoder, FILE* sourceFile, const std::string& destFileName, const Byte& version = ZPatcher_Version); - - } #endif // _LZMA2DECODER_H_ diff --git a/ZPatcherLib/Lzma2Encoder.cpp b/ZPatcherLib/Lzma2Encoder.cpp index 098b57a..2d1242e 100644 --- a/ZPatcherLib/Lzma2Encoder.cpp +++ b/ZPatcherLib/Lzma2Encoder.cpp @@ -11,13 +11,16 @@ ////////////////////////////////////////////////////////////////////////// #include "stdafx.h" +#include +#include +#include +#include "Alloc.h" #include "Lzma2Enc.h" #include "Lzma2Encoder.h" #include "LzmaInterfaces.h" #include "LogSystem.h" -#include -#include -#include +#include "ZPatcherCurrentVersion.h" + #ifdef _WIN32 #define ftell64 _ftelli64 @@ -29,7 +32,7 @@ CLzma2EncHandle ZPatcher::InitLzma2Encoder() { - CLzma2EncHandle enc = Lzma2Enc_Create(&LzmaSzAlloc, &LzmaSzAlloc); + CLzma2EncHandle enc = Lzma2Enc_Create(&g_Alloc, &g_BigAlloc); assert(enc); CLzma2EncProps props; diff --git a/ZPatcherLib/Lzma2Encoder.h b/ZPatcherLib/Lzma2Encoder.h index c0b6c17..4e1e5ff 100644 --- a/ZPatcherLib/Lzma2Encoder.h +++ b/ZPatcherLib/Lzma2Encoder.h @@ -14,11 +14,9 @@ #ifndef _LZMA2ENCODER_H_ #define _LZMA2ENCODER_H_ -#include "ZPatcherCurrentVersion.h" +#include #include "Lzma2Enc.h" #include "LzmaInterfaces.h" -#include -#include namespace ZPatcher { diff --git a/ZPatcherLib/LzmaInterfaces.cpp b/ZPatcherLib/LzmaInterfaces.cpp index cfa5103..a1bdf9a 100644 --- a/ZPatcherLib/LzmaInterfaces.cpp +++ b/ZPatcherLib/LzmaInterfaces.cpp @@ -61,6 +61,7 @@ SRes ZPatcher::OnProgress(ICompressProgress* p, UInt64 inSize, UInt64 outSize) void ZPatcher::PrintProgressBar(const float Percentage, UInt64 CurrentFileSize) { + (void)CurrentFileSize; // Suppress warning about unused parameter const int progressMaxSize = 10; int num = fprintf(stdout, " %0.2f %%", Percentage); diff --git a/ZPatcherLib/LzmaInterfaces.h b/ZPatcherLib/LzmaInterfaces.h index f007980..3aa8f53 100644 --- a/ZPatcherLib/LzmaInterfaces.h +++ b/ZPatcherLib/LzmaInterfaces.h @@ -15,25 +15,12 @@ #define _LZMAALLOCATORS_H_ #include -#include #include #include - -#ifdef _WIN32 - #include -#endif - #include "7zFile.h" namespace ZPatcher { - ////////////////////////////////////////////////////////////////////////// - // Allocators - - static void* LzmaAlloc(ISzAllocPtr p, size_t size) { return malloc(size); } - static void LzmaFree(ISzAllocPtr p, void *address) { free(address); } - static ISzAlloc LzmaSzAlloc = { &LzmaAlloc, &LzmaFree }; - ////////////////////////////////////////////////////////////////////////// // File Read Struct diff --git a/ZPatcherLib/ZPatcherLib.vcxproj b/ZPatcherLib/ZPatcherLib.vcxproj index e3659c9..68112af 100644 --- a/ZPatcherLib/ZPatcherLib.vcxproj +++ b/ZPatcherLib/ZPatcherLib.vcxproj @@ -22,32 +22,32 @@ {A6C9C679-5FC1-4238-8FEB-1C3394B1DE69} Win32Proj ZPatcherLib - 8.1 + 10.0 StaticLibrary true - v140 + v143 Unicode StaticLibrary false - v140 + v143 true Unicode StaticLibrary true - v140 + v143 Unicode StaticLibrary false - v140 + v143 true Unicode @@ -88,7 +88,7 @@ Use - Level3 + Level4 Disabled WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) true @@ -106,7 +106,7 @@ Use - Level3 + Level4 Disabled _DEBUG;_LIB;%(PreprocessorDefinitions) true @@ -123,7 +123,7 @@ - Level3 + Level4 Use MaxSpeed true @@ -144,7 +144,7 @@ - Level3 + Level4 Use MaxSpeed true diff --git a/ZUpdater/Main.cpp b/ZUpdater/Main.cpp index 3e1c945..d17059c 100644 --- a/ZUpdater/Main.cpp +++ b/ZUpdater/Main.cpp @@ -58,14 +58,12 @@ int main() if (!SelfUpdate(shouldRestart)) { WINDOWS_PAUSE(); - ZPatcher::DestroyLogSystem(); exit(EXIT_FAILURE); } else { if (shouldRestart) { - ZPatcher::DestroyLogSystem(); exit(EXIT_SUCCESS); } } @@ -83,7 +81,6 @@ int main() { fprintf(stderr, "An error occurred while getting current application version.\n"); WINDOWS_PAUSE(); - ZPatcher::DestroyLogSystem(); exit(EXIT_FAILURE); } @@ -94,7 +91,6 @@ int main() { fprintf(stderr, "An error occurred while checking for updates.\n"); WINDOWS_PAUSE(); - ZPatcher::DestroyLogSystem(); exit(EXIT_FAILURE); } @@ -104,12 +100,10 @@ int main() if (!DownloadAndApplyPatch(targetDirectory, versionFile, currentVersion)) { WINDOWS_PAUSE(); - ZPatcher::DestroyLogSystem(); exit(EXIT_FAILURE); } fprintf(stdout, "\n"); WINDOWS_PAUSE(); - ZPatcher::DestroyLogSystem(); exit(EXIT_SUCCESS); } diff --git a/ZUpdater/ZUpdater.cpp b/ZUpdater/ZUpdater.cpp index 1e841b7..43d65c0 100644 --- a/ZUpdater/ZUpdater.cpp +++ b/ZUpdater/ZUpdater.cpp @@ -403,17 +403,14 @@ namespace ZUpdater { if (!SaveTargetNewVersion(versionFile, patch.targetBuildNumber)) { - ZPatcher::DestroyLogSystem(); system("pause"); return false; } currentVersion = patch.targetBuildNumber; - ZPatcher::DestroyLogSystem(); } else { - ZPatcher::DestroyLogSystem(); system("pause"); return false; } diff --git a/ZUpdater/ZUpdater.vcxproj b/ZUpdater/ZUpdater.vcxproj index 73b3d29..199b922 100644 --- a/ZUpdater/ZUpdater.vcxproj +++ b/ZUpdater/ZUpdater.vcxproj @@ -22,32 +22,32 @@ {2F868D36-FD16-4C3A-B444-8C2CCA1A6A0D} Win32Proj ZUpdater - 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 @@ -103,7 +103,7 @@ Console true - $(SolutionDir)\_Output\$(PlatformTarget)\$(Configuration)\LzmaLib\;$(SolutionDir)\_Output\$(PlatformTarget)\$(Configuration)\ZPatcherLib\;$(SolutionDir)\$(Configuration)-Lib\;$(SolutionDir)\libs\wxWidgets\lib\vc_lib;$(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_lib;$(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;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) @@ -121,7 +121,7 @@ Console 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;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) @@ -142,7 +142,7 @@ true true true - $(SolutionDir)\_Output\$(PlatformTarget)\$(Configuration)\LzmaLib\;$(SolutionDir)\_Output\$(PlatformTarget)\$(Configuration)\ZPatcherLib\;$(SolutionDir)\$(Configuration)-Lib\;$(SolutionDir)\libs\wxWidgets\lib\vc_lib;$(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_lib;$(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;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) @@ -163,7 +163,7 @@ 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;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) diff --git a/appveyor.yml b/appveyor.yml index df68911..a312855 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,6 +1,6 @@ version: 3.0.{build} -image: Visual Studio 2015 +image: Visual Studio 2022 configuration: - Debug @@ -14,31 +14,7 @@ install: - ps: >- git submodule update -q --init --recursive - .\libs\curl\projects\generate.bat vc14 - - Invoke-WebRequest "https://github.com/wxWidgets/wxWidgets/releases/download/v3.1.6/wxWidgets-3.1.6-headers.7z" -outfile "wxWidgets-3.1.6-headers.7z" - - Invoke-WebRequest "https://github.com/wxWidgets/wxWidgets/releases/download/v3.1.6/wxMSW-3.1.6_vc14x_x64_Dev.7z" -outfile "wxMSW-3.1.6_vc14x_x64_Dev.7z" - - Invoke-WebRequest "https://github.com/wxWidgets/wxWidgets/releases/download/v3.1.6/wxMSW-3.1.6_vc14x_x64_ReleaseDLL.7z" -outfile "wxMSW-3.1.6_vc14x_x64_ReleaseDLL.7z" - - Invoke-WebRequest "https://github.com/wxWidgets/wxWidgets/releases/download/v3.1.6/wxMSW-3.1.6_vc14x_Dev.7z" -outfile "wxMSW-3.1.6_vc14x_Dev.7z" - - Invoke-WebRequest "https://github.com/wxWidgets/wxWidgets/releases/download/v3.1.6/wxMSW-3.1.6_vc14x_ReleaseDLL.7z" -outfile "wxMSW-3.1.6_vc14x_ReleaseDLL.7z" - - 7z.exe x -y ".\wxWidgets-3.1.6-headers.7z" -o".\libs\wxWidgets" - - 7z.exe x -y ".\wxMSW-3.1.6_vc14x_x64_Dev.7z" -o".\libs\wxWidgets" - - 7z.exe x -y ".\wxMSW-3.1.6_vc14x_x64_ReleaseDLL.7z" -o".\libs\wxWidgets" - - 7z.exe x -y ".\wxMSW-3.1.6_vc14x_Dev.7z" -o".\libs\wxWidgets" - - 7z.exe x -y ".\wxMSW-3.1.6_vc14x_ReleaseDLL.7z" -o".\libs\wxWidgets" - - move ".\libs\wxWidgets\lib\vc14x_x64_dll\" ".\libs\wxWidgets\lib\vc_x64_dll\" - - move ".\libs\wxWidgets\lib\vc14x_dll\" ".\libs\wxWidgets\lib\vc_dll\" + .\setup-external-dependencies.ps1 build: project: .\ZPatcher.sln verbosity: minimal diff --git a/libs/LzmaLib/LzmaLib.vcxproj b/libs/LzmaLib/LzmaLib.vcxproj index f5f881b..cb23322 100644 --- a/libs/LzmaLib/LzmaLib.vcxproj +++ b/libs/LzmaLib/LzmaLib.vcxproj @@ -22,30 +22,30 @@ {C72F7F04-8109-485F-88D4-CB5653CB7DDD} - 8.1 + 10.0 StaticLibrary - v140 + v143 false MultiByte StaticLibrary - v140 + v143 false MultiByte StaticLibrary - v140 + v143 false MultiByte StaticLibrary - v140 + v143 false MultiByte diff --git a/setup-external-dependencies.ps1 b/setup-external-dependencies.ps1 index e02b79b..4e142d8 100644 --- a/setup-external-dependencies.ps1 +++ b/setup-external-dependencies.ps1 @@ -1,6 +1,6 @@ git submodule update -q --init --recursive -.\libs\curl\projects\generate.bat vc14 +.\libs\curl\projects\generate.bat "vc14.30" Invoke-WebRequest "https://github.com/wxWidgets/wxWidgets/releases/download/v3.1.6/wxWidgets-3.1.6-headers.7z" -outfile "wxWidgets-3.1.6-headers.7z" Invoke-WebRequest "https://github.com/wxWidgets/wxWidgets/releases/download/v3.1.6/wxMSW-3.1.6_vc14x_x64_Dev.7z" -outfile "wxMSW-3.1.6_vc14x_x64_Dev.7z"