From e35d46254b3b58a5255ed7b82f3b6cd64e91b2fa Mon Sep 17 00:00:00 2001 From: Rafael Kitover Date: Sat, 9 Nov 2019 23:44:23 +0000 Subject: [PATCH] WinSparkle wrapper improvements. - Make WinSparkleDllWrapper a real singleton. Make the constructor private and add a static `GetInstance()` method that constructs the single instance and/or returns it. Make it encapsulate the function pointers and the wxDynamicLibrary instance, with the API as friend functions. - Make winsparkle API completely self-contained. Make the instance of the WinSparkleDllWrapper a factory function local static. This way the calling program does not have to manage the life-cycle, the singleton is initialized when the API is first invoked, and destroyed during global destruction. The destructor is now also private, because the singleton object is destroyed during global destruction. Found the solution here: https://stackoverflow.com/a/46139631/262458 - Cmake improvements. Only enable the ENABLE_ONLINEUPDATES option by default if we are going to use winsparkle on 32 or 64 bit intel windows. Do not inlclude winsparkle if the option is off. - Disable for now. Currently ENABLE_ONLINEUPDATES defaults to OFF even on intel/windows, until we do more work on this feature. Signed-off-by: Rafael Kitover --- CMakeLists.txt | 12 ++++++++++- dependencies | 2 +- src/wx/CMakeLists.txt | 7 +++--- src/wx/winsparkle-rc.h | 6 ++++++ src/wx/winsparkle-wrapper.cpp | 40 ++++++++++++----------------------- src/wx/winsparkle-wrapper.h | 36 +++++++++++++++++++++++++++++-- src/wx/wxvbam.cpp | 7 +----- src/wx/wxvbam.h | 2 -- src/wx/wxvbam.rc | 10 ++++++--- 9 files changed, 76 insertions(+), 46 deletions(-) create mode 100644 src/wx/winsparkle-rc.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 4142a45e..cfc3d95b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -141,7 +141,17 @@ endif() option(ENABLE_FFMPEG "Enable ffmpeg A/V recording" ${FFMPEG_DEFAULT}) -option(ENABLE_ONLINEUPDATES "Enable online update checks" ON) +set(ONLINEUPDATES_DEFAULT OFF) + +if(WIN32 AND (AMD64 OR X86_32)) + +# currently we do not turn this on until we do more work on this feature +# +# set(ONLINEUPDATES_DEFAULT ON) # use winsparkle + +endif() + +option(ENABLE_ONLINEUPDATES "Enable online update checks" ${ONLINEUPDATES_DEFAULT}) set(LTO_DEFAULT ON) diff --git a/dependencies b/dependencies index fa2265a8..fe50718d 160000 --- a/dependencies +++ b/dependencies @@ -1 +1 @@ -Subproject commit fa2265a8bedfbdc6169d9891c79320c76b7b0dd1 +Subproject commit fe50718d0e642c2c9c32b63abec510ced9f1ae0a diff --git a/src/wx/CMakeLists.txt b/src/wx/CMakeLists.txt index ba52363d..13e0cea7 100644 --- a/src/wx/CMakeLists.txt +++ b/src/wx/CMakeLists.txt @@ -696,12 +696,11 @@ set( # from external source with minor modifications widgets/wx/checkedlistctrl.h ../common/version_cpp.h - winsparkle-wrapper.h ) -if(WIN32) +if(WIN32 AND (AMD64 OR X86_32) AND ENABLE_ONLINEUPDATES) list(APPEND SRC_WX winsparkle-wrapper.cpp) - list(APPEND HDR_WX winsparkle-wrapper.h) + list(APPEND HDR_WX winsparkle-wrapper.h winsparkle-rc.h) endif() set( @@ -763,7 +762,7 @@ add_executable( ${CM_STUFF} ) -if(WIN32 AND (AMD64 OR X86_32)) +if(WIN32 AND (AMD64 OR X86_32) AND ENABLE_ONLINEUPDATES) if(NOT DEFINED WINSPARKLE_BIN_RELEASE_DIR) set(WINSPARKLE_BIN_RELEASE_DIR ${CMAKE_SOURCE_DIR}/dependencies/WinSparkle-0.6.0) endif() diff --git a/src/wx/winsparkle-rc.h b/src/wx/winsparkle-rc.h new file mode 100644 index 00000000..2ee08d3b --- /dev/null +++ b/src/wx/winsparkle-rc.h @@ -0,0 +1,6 @@ +#ifndef WINSPARKLE_RC_H +#define WINSPARKLE_RC_H + +#define WINSPARKLE_DLL_RC 300 + +#endif /* WINSPARKLE_RC_H */ diff --git a/src/wx/winsparkle-wrapper.cpp b/src/wx/winsparkle-wrapper.cpp index 5a6f86ae..3e0f233e 100644 --- a/src/wx/winsparkle-wrapper.cpp +++ b/src/wx/winsparkle-wrapper.cpp @@ -4,29 +4,17 @@ #include "winsparkle-wrapper.h" #include "wx/msw/private.h" -wxDynamicLibrary *winsparkle_dll = nullptr; +WinSparkleDllWrapper *WinSparkleDllWrapper::GetInstance() +{ + static WinSparkleDllWrapper instance; -typedef void (*func_win_sparkle_init)(); -func_win_sparkle_init winsparkle_init = nullptr; - -typedef void (*func_win_sparkle_check_update_with_ui)(); -func_win_sparkle_check_update_with_ui winsparkle_check_update_with_ui = nullptr; - -typedef void (*func_win_sparkle_set_appcast_url)(const char *); -func_win_sparkle_set_appcast_url winsparkle_set_appcast_url = nullptr; - -typedef void (*func_win_sparkle_set_app_details)(const wchar_t*, const wchar_t*, const wchar_t*); -func_win_sparkle_set_app_details winsparkle_set_app_details = nullptr; - -typedef void (*func_win_sparkle_cleanup)(); -func_win_sparkle_cleanup winsparkle_cleanup = nullptr; - -wxString *temp_file_name = nullptr; + return &instance; +} WinSparkleDllWrapper::WinSparkleDllWrapper() { wxFile temp_file; - temp_file_name = new wxString(wxFileName::CreateTempFileName("winsparkle", &temp_file)); + temp_file_name = wxFileName::CreateTempFileName("winsparkle", &temp_file); HRSRC res = FindResource(wxGetInstance(), MAKEINTRESOURCE(WINSPARKLE_DLL_RC), RT_RCDATA); @@ -46,7 +34,7 @@ WinSparkleDllWrapper::WinSparkleDllWrapper() temp_file.Close(); - winsparkle_dll = new wxDynamicLibrary(*temp_file_name, wxDL_NOW | wxDL_VERBATIM); + winsparkle_dll = new wxDynamicLibrary(temp_file_name, wxDL_NOW | wxDL_VERBATIM); winsparkle_init = reinterpret_cast(winsparkle_dll->GetSymbol("win_sparkle_init")); winsparkle_check_update_with_ui = reinterpret_cast(winsparkle_dll->GetSymbol("win_sparkle_check_update_with_ui")); @@ -59,35 +47,33 @@ WinSparkleDllWrapper::~WinSparkleDllWrapper() { delete winsparkle_dll; - wxRemoveFile(*temp_file_name); - - delete temp_file_name; + wxRemoveFile(temp_file_name); } void win_sparkle_init() { - winsparkle_init(); + WinSparkleDllWrapper::GetInstance()->winsparkle_init(); } void win_sparkle_check_update_with_ui() { - winsparkle_check_update_with_ui(); + WinSparkleDllWrapper::GetInstance()->winsparkle_check_update_with_ui(); } void win_sparkle_set_appcast_url(const char *url) { - winsparkle_set_appcast_url(url); + WinSparkleDllWrapper::GetInstance()->winsparkle_set_appcast_url(url); } void win_sparkle_set_app_details(const wchar_t *company_name, const wchar_t *app_name, const wchar_t *app_version) { - winsparkle_set_app_details(company_name, app_name, app_version); + WinSparkleDllWrapper::GetInstance()->winsparkle_set_app_details(company_name, app_name, app_version); } void win_sparkle_cleanup() { - winsparkle_cleanup(); + WinSparkleDllWrapper::GetInstance()->winsparkle_cleanup(); } diff --git a/src/wx/winsparkle-wrapper.h b/src/wx/winsparkle-wrapper.h index 99cfcb4c..16354bc6 100644 --- a/src/wx/winsparkle-wrapper.h +++ b/src/wx/winsparkle-wrapper.h @@ -1,11 +1,43 @@ #ifndef WINSPARKLE_WRAPPER_H #define WINSPARKLE_WRAPPER_H -#define WINSPARKLE_DLL_RC 300 +#include +#include -struct WinSparkleDllWrapper { +#include "winsparkle-rc.h" + +class WinSparkleDllWrapper { +public: + static WinSparkleDllWrapper *GetInstance(); +private: WinSparkleDllWrapper(); ~WinSparkleDllWrapper(); + + wxDynamicLibrary *winsparkle_dll = nullptr; + + typedef void (*func_win_sparkle_init)(); + func_win_sparkle_init winsparkle_init = nullptr; + + typedef void (*func_win_sparkle_check_update_with_ui)(); + func_win_sparkle_check_update_with_ui winsparkle_check_update_with_ui = nullptr; + + typedef void (*func_win_sparkle_set_appcast_url)(const char *); + func_win_sparkle_set_appcast_url winsparkle_set_appcast_url = nullptr; + + typedef void (*func_win_sparkle_set_app_details)(const wchar_t*, const wchar_t*, const wchar_t*); + func_win_sparkle_set_app_details winsparkle_set_app_details = nullptr; + + typedef void (*func_win_sparkle_cleanup)(); + func_win_sparkle_cleanup winsparkle_cleanup = nullptr; + + wxString temp_file_name; + + // the API needs to access the function pointers + friend void win_sparkle_init(); + friend void win_sparkle_check_update_with_ui(); + friend void win_sparkle_set_appcast_url(const char *url); + friend void win_sparkle_set_app_details(const wchar_t *company_name, const wchar_t *app_name, const wchar_t *app_version); + friend void win_sparkle_cleanup(); }; void win_sparkle_init(); diff --git a/src/wx/wxvbam.cpp b/src/wx/wxvbam.cpp index 35cbe65f..368067f6 100644 --- a/src/wx/wxvbam.cpp +++ b/src/wx/wxvbam.cpp @@ -198,7 +198,7 @@ static void init_check_for_updates() wxString version(vbam_version); //win_sparkle_set_appcast_url("https://github.com/visualboyadvance-m/visualboyadvance-m/data/appcast.xml"); win_sparkle_set_appcast_url("https://raw.githubusercontent.com/visualboyadvance-m/visualboyadvance-m/update-checker/data/appcast.xml"); - win_sparkle_set_app_details(L"visualboyadvance-m", L"VisualBoyAdvance-M", version.c_str()); + win_sparkle_set_app_details(L"visualboyadvance-m", L"VisualBoyAdvance-M", version.wc_str()); win_sparkle_init(); #endif // __WXMSW__ } @@ -451,10 +451,6 @@ bool wxvbamApp::OnInit() frame->ShowFullScreen(isFullscreen); frame->Show(true); -#if defined(__WXMSW__) && !defined(NO_ONLINEUPDATES) - winsparkle = new WinSparkleDllWrapper(); -#endif - #ifndef NO_ONLINEUPDATES init_check_for_updates(); #endif @@ -710,7 +706,6 @@ wxvbamApp::~wxvbamApp() { #if defined(__WXMSW__) && !defined(NO_ONLINEUPDATES) win_sparkle_cleanup(); - delete winsparkle; #endif } diff --git a/src/wx/wxvbam.h b/src/wx/wxvbam.h index b64d309d..e46471e5 100644 --- a/src/wx/wxvbam.h +++ b/src/wx/wxvbam.h @@ -73,7 +73,6 @@ inline std::string ToString(const wxChar* aString) } class MainFrame; -class WinSparkleDllWrapper; class wxvbamApp : public wxApp { public: @@ -158,7 +157,6 @@ protected: private: wxPathList config_path; char* home = nullptr; - WinSparkleDllWrapper *winsparkle = nullptr; }; DECLARE_APP(wxvbamApp); diff --git a/src/wx/wxvbam.rc b/src/wx/wxvbam.rc index 46372f0c..44213fb9 100644 --- a/src/wx/wxvbam.rc +++ b/src/wx/wxvbam.rc @@ -5,9 +5,6 @@ AAAAA_MAINICON ICON "icons/vbam.ico" #include "version.h" -#include "winsparkle-wrapper.h" -#include "winsparkle-path.h" - ///////////////////////////////////////////////////////////////////////////// // // Version @@ -16,8 +13,15 @@ AAAAA_MAINICON ICON "icons/vbam.ico" #define VER_PRODUCTVERSION VER_FILEVERSION #define VER_PRODUCTVERSION_STR VER_FILEVERSION_STR +#ifndef NO_ONLINEUPDATES + +#include "winsparkle-rc.h" +#include "winsparkle-path.h" + WINSPARKLE_DLL_RC RCDATA WINSPARKLE_DLL_PATH +#endif /* NO_ONLINEUPDATES */ + VS_VERSION_INFO VERSIONINFO FILEVERSION VER_FILEVERSION PRODUCTVERSION VER_PRODUCTVERSION