Allow void* user data in C API

This commit is contained in:
Caelan Sayler
2024-11-08 21:18:41 +00:00
committed by Caelan
parent bb3758a851
commit 65df8e1013
4 changed files with 30 additions and 24 deletions

View File

@@ -38,7 +38,7 @@ INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
std::wstring Utf8ToWString(std::string const& str); std::wstring Utf8ToWString(std::string const& str);
std::string WStringToUtf8(std::wstring const& wstr); std::string WStringToUtf8(std::wstring const& wstr);
void handle_vpkc_log(const char* pszLevel, const char* pszMessage) void handle_vpkc_log(void* pUserData, const char* pszLevel, const char* pszMessage)
{ {
std::cout << pszLevel << ": " << pszMessage << std::endl; std::cout << pszLevel << ": " << pszMessage << std::endl;
} }
@@ -57,7 +57,7 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
// Initialize Velopack log capture // Initialize Velopack log capture
std::cout << "Velopack C++ Sample App" << std::endl; std::cout << "Velopack C++ Sample App" << std::endl;
vpkc_set_log(handle_vpkc_log); vpkc_set_logger(handle_vpkc_log);
// This should run as early as possible in the main method. // This should run as early as possible in the main method.
// Velopack may exit / restart the app at this point. // Velopack may exit / restart the app at this point.

View File

@@ -41,7 +41,7 @@
#pragma comment(linker, "/EXPORT:vpkc_app_set_hook_restarted") #pragma comment(linker, "/EXPORT:vpkc_app_set_hook_restarted")
#pragma comment(linker, "/EXPORT:vpkc_app_run") #pragma comment(linker, "/EXPORT:vpkc_app_run")
#pragma comment(linker, "/EXPORT:vpkc_get_last_error") #pragma comment(linker, "/EXPORT:vpkc_get_last_error")
#pragma comment(linker, "/EXPORT:vpkc_set_log") #pragma comment(linker, "/EXPORT:vpkc_set_logger")
#pragma comment(linker, "/EXPORT:vpkc_free_update_manager") #pragma comment(linker, "/EXPORT:vpkc_free_update_manager")
#pragma comment(linker, "/EXPORT:vpkc_free_update_info") #pragma comment(linker, "/EXPORT:vpkc_free_update_info")
#pragma comment(linker, "/EXPORT:vpkc_free_asset") #pragma comment(linker, "/EXPORT:vpkc_free_asset")
@@ -58,9 +58,9 @@ extern "C" {
#endif #endif
typedef void vpkc_update_manager_t; typedef void vpkc_update_manager_t;
typedef void (*vpkc_progress_callback_t)(size_t progress); typedef void (*vpkc_progress_callback_t)(void* pUserData, size_t progress);
typedef void (*vpkc_log_callback_t)(const char* pszLevel, const char* pszMessage); typedef void (*vpkc_log_callback_t)(void* pUserData, const char* pszLevel, const char* pszMessage);
typedef void (*vpkc_hook_callback_t)(const char* pszAppVersion); typedef void (*vpkc_hook_callback_t)(void* pUserData, const char* pszAppVersion);
typedef enum vpkc_update_check_t { typedef enum vpkc_update_check_t {
UPDATE_AVAILABLE = 0, UPDATE_AVAILABLE = 0,
@@ -150,7 +150,7 @@ VPKC_EXPORT bool VPKC_CALL vpkc_update_pending_restart(vpkc_update_manager_t* pM
/// \group UpdateManager /// \group UpdateManager
VPKC_EXPORT vpkc_update_check_t VPKC_CALL vpkc_check_for_updates(vpkc_update_manager_t* pManager, vpkc_update_info_t* pUpdate); VPKC_EXPORT vpkc_update_check_t VPKC_CALL vpkc_check_for_updates(vpkc_update_manager_t* pManager, vpkc_update_info_t* pUpdate);
/// \group UpdateManager /// \group UpdateManager
VPKC_EXPORT bool VPKC_CALL vpkc_download_updates(vpkc_update_manager_t* pManager, vpkc_update_info_t* pUpdate, vpkc_progress_callback_t cbProgress); VPKC_EXPORT bool VPKC_CALL vpkc_download_updates(vpkc_update_manager_t* pManager, vpkc_update_info_t* pUpdate, vpkc_progress_callback_t cbProgress, void* pUserData = 0);
/// \group UpdateManager /// \group UpdateManager
VPKC_EXPORT bool VPKC_CALL vpkc_wait_exit_then_apply_update(vpkc_update_manager_t* pManager, vpkc_asset_t* pAsset, bool bSilent, bool bRestart, char** pRestartArgs, size_t cRestartArgs); VPKC_EXPORT bool VPKC_CALL vpkc_wait_exit_then_apply_update(vpkc_update_manager_t* pManager, vpkc_asset_t* pAsset, bool bSilent, bool bRestart, char** pRestartArgs, size_t cRestartArgs);
/// \group UpdateManager /// \group UpdateManager
@@ -162,7 +162,7 @@ VPKC_EXPORT void VPKC_CALL vpkc_free_asset(vpkc_asset_t* pAsset);
/// Should be run at the beginning of your application to handle Velopack events. /// Should be run at the beginning of your application to handle Velopack events.
/// \group VelopackApp /// \group VelopackApp
VPKC_EXPORT void VPKC_CALL vpkc_app_run(); VPKC_EXPORT void VPKC_CALL vpkc_app_run(void* pUserData = 0);
/// \group VelopackApp /// \group VelopackApp
VPKC_EXPORT void VPKC_CALL vpkc_app_set_auto_apply_on_startup(bool bAutoApply); VPKC_EXPORT void VPKC_CALL vpkc_app_set_auto_apply_on_startup(bool bAutoApply);
/// \group VelopackApp /// \group VelopackApp
@@ -186,7 +186,7 @@ VPKC_EXPORT void VPKC_CALL vpkc_app_set_hook_restarted(vpkc_hook_callback_t cbRe
VPKC_EXPORT size_t VPKC_CALL vpkc_get_last_error(char* pszError, size_t cError); VPKC_EXPORT size_t VPKC_CALL vpkc_get_last_error(char* pszError, size_t cError);
/// Sets the callback to be used/called with log messages from Velopack. /// Sets the callback to be used/called with log messages from Velopack.
VPKC_EXPORT void VPKC_CALL vpkc_set_log(vpkc_log_callback_t cbLog); VPKC_EXPORT void VPKC_CALL vpkc_set_logger(vpkc_log_callback_t cbLog, void* pUserData = 0);
#ifdef __cplusplus // end of extern "C" #ifdef __cplusplus // end of extern "C"
} }
@@ -497,8 +497,8 @@ public:
* Runs the Velopack startup logic. This should be the first thing to run in your app. * Runs the Velopack startup logic. This should be the first thing to run in your app.
* In some circumstances it may terminate/restart the process to perform tasks. * In some circumstances it may terminate/restart the process to perform tasks.
*/ */
void Run() { void Run(void* pUserData = 0) {
vpkc_app_run(); vpkc_app_run(pUserData);
}; };
}; };
@@ -611,9 +611,9 @@ public:
* - If there is no delta update available, or there is an error preparing delta * - If there is no delta update available, or there is an error preparing delta
* packages, this method will fall back to downloading the full version of the update. * packages, this method will fall back to downloading the full version of the update.
*/ */
void DownloadUpdates(const UpdateInfo& update, vpkc_progress_callback_t progress = nullptr) { void DownloadUpdates(const UpdateInfo& update, vpkc_progress_callback_t progress = nullptr, void* pUserData = 0) {
vpkc_update_info_t vpkc_update = to_c(update); vpkc_update_info_t vpkc_update = to_c(update);
if (!vpkc_download_updates(m_pManager, &vpkc_update, progress)) { if (!vpkc_download_updates(m_pManager, &vpkc_update, progress, pUserData)) {
throw_last_error(); throw_last_error();
} }
}; };

View File

@@ -299,7 +299,7 @@ VPKC_EXPORT vpkc_update_check_t VPKC_CALL vpkc_check_for_updates(vpkc_update_man
return vpkc_update_check_t::UPDATE_ERROR; return vpkc_update_check_t::UPDATE_ERROR;
} }
} }
VPKC_EXPORT bool VPKC_CALL vpkc_download_updates(vpkc_update_manager_t* pManager, vpkc_update_info_t* pUpdate, vpkc_progress_callback_t cbProgress) { VPKC_EXPORT bool VPKC_CALL vpkc_download_updates(vpkc_update_manager_t* pManager, vpkc_update_info_t* pUpdate, vpkc_progress_callback_t cbProgress, void* pUserData) {
clear_last_error(); clear_last_error();
try { try {
if (!pUpdate) { if (!pUpdate) {
@@ -312,6 +312,7 @@ VPKC_EXPORT bool VPKC_CALL vpkc_download_updates(vpkc_update_manager_t* pManager
DownloadCallbackManager download{}; DownloadCallbackManager download{};
download.progress_cb = cbProgress; download.progress_cb = cbProgress;
download.user_data = pUserData;
bridge_download_updates(*pOpaque, update, download); bridge_download_updates(*pOpaque, update, download);
return true; return true;
} }
@@ -382,14 +383,16 @@ VPKC_EXPORT void VPKC_CALL vpkc_app_set_hook_first_run(vpkc_hook_callback_t cbFi
VPKC_EXPORT void VPKC_CALL vpkc_app_set_hook_restarted(vpkc_hook_callback_t cbRestarted) { VPKC_EXPORT void VPKC_CALL vpkc_app_set_hook_restarted(vpkc_hook_callback_t cbRestarted) {
hooks.restarted = cbRestarted; hooks.restarted = cbRestarted;
} }
VPKC_EXPORT void VPKC_CALL vpkc_app_run() { VPKC_EXPORT void VPKC_CALL vpkc_app_run(void* pUserData) {
hooks.user_data = pUserData;
bridge_appbuilder_run(hooks, args, locator, autoApply); bridge_appbuilder_run(hooks, args, locator, autoApply);
} }
// Misc functions // Misc functions
LoggerCallbackManager logMgr{}; LoggerCallbackManager logMgr{};
VPKC_EXPORT void VPKC_CALL vpkc_set_log(vpkc_log_callback_t cbLog) { VPKC_EXPORT void VPKC_CALL vpkc_set_logger(vpkc_log_callback_t cbLog, void* pUserData) {
logMgr.lob_cb = cbLog; logMgr.lob_cb = cbLog;
logMgr.user_data = pUserData;
bridge_set_logger_callback(&logMgr); bridge_set_logger_callback(&logMgr);
} }
VPKC_EXPORT void VPKC_CALL vpkc_free_update_manager(vpkc_update_manager_t* pManager) { VPKC_EXPORT void VPKC_CALL vpkc_free_update_manager(vpkc_update_manager_t* pManager) {

View File

@@ -9,58 +9,61 @@ struct HookCallbackManager {
vpkc_hook_callback_t after_update = nullptr; vpkc_hook_callback_t after_update = nullptr;
vpkc_hook_callback_t first_run = nullptr; vpkc_hook_callback_t first_run = nullptr;
vpkc_hook_callback_t restarted = nullptr; vpkc_hook_callback_t restarted = nullptr;
void* user_data = nullptr;
void install_hook(::rust::String app_version) const { void install_hook(::rust::String app_version) const {
if (after_install) { if (after_install) {
after_install(app_version.c_str()); after_install(user_data, app_version.c_str());
} }
}; };
void update_hook(::rust::String app_version) const { void update_hook(::rust::String app_version) const {
if (after_update) { if (after_update) {
after_update(app_version.c_str()); after_update(user_data, app_version.c_str());
} }
}; };
void obsolete_hook(::rust::String app_version) const { void obsolete_hook(::rust::String app_version) const {
if (before_update) { if (before_update) {
before_update(app_version.c_str()); before_update(user_data, app_version.c_str());
} }
}; };
void uninstall_hook(::rust::String app_version) const { void uninstall_hook(::rust::String app_version) const {
if (before_uninstall) { if (before_uninstall) {
before_uninstall(app_version.c_str()); before_uninstall(user_data, app_version.c_str());
} }
}; };
void firstrun_hook(::rust::String app_version) const { void firstrun_hook(::rust::String app_version) const {
if (first_run) { if (first_run) {
first_run(app_version.c_str()); first_run(user_data, app_version.c_str());
} }
}; };
void restarted_hook(::rust::String app_version) const { void restarted_hook(::rust::String app_version) const {
if (restarted) { if (restarted) {
restarted(app_version.c_str()); restarted(user_data, app_version.c_str());
} }
}; };
}; };
struct DownloadCallbackManager { struct DownloadCallbackManager {
vpkc_progress_callback_t progress_cb = nullptr; vpkc_progress_callback_t progress_cb = nullptr;
void* user_data = nullptr;
void download_progress(int16_t progress) const { void download_progress(int16_t progress) const {
if (progress_cb) { if (progress_cb) {
progress_cb(progress); progress_cb(user_data, progress);
} }
}; };
}; };
struct LoggerCallbackManager { struct LoggerCallbackManager {
vpkc_log_callback_t lob_cb = nullptr; vpkc_log_callback_t lob_cb = nullptr;
void* user_data = nullptr;
void log(::rust::String level, ::rust::String message) const { void log(::rust::String level, ::rust::String message) const {
if (lob_cb) { if (lob_cb) {
lob_cb(level.c_str(), message.c_str()); lob_cb(user_data, level.c_str(), message.c_str());
} }
}; };
}; };