forked from chenxiaolong/DualBootPatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
libmbpatcher: Replace C callbacks with std::function
Signed-off-by: Andrew Gunnerson <[email protected]>
- Loading branch information
1 parent
1db5e5f
commit cfd3230
Showing
13 changed files
with
108 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright (C) 2014-2015 Andrew Gunnerson <[email protected]> | ||
* Copyright (C) 2014-2018 Andrew Gunnerson <[email protected]> | ||
* | ||
* This file is part of DualBootPatcher | ||
* | ||
|
@@ -35,10 +35,10 @@ MB_EXPORT /* enum ErrorCode */ int mbpatcher_patcher_error(const CPatcher *patch | |
MB_EXPORT char * mbpatcher_patcher_id(const CPatcher *patcher); | ||
MB_EXPORT void mbpatcher_patcher_set_fileinfo(CPatcher *patcher, const CFileInfo *info); | ||
MB_EXPORT bool mbpatcher_patcher_patch_file(CPatcher *patcher, | ||
ProgressUpdatedCallback progressCb, | ||
FilesUpdatedCallback filesCb, | ||
DetailsUpdatedCallback detailsCb, | ||
void *userData); | ||
ProgressUpdatedCallback progress_cb, | ||
FilesUpdatedCallback files_cb, | ||
DetailsUpdatedCallback details_cb, | ||
void *userdata); | ||
MB_EXPORT void mbpatcher_patcher_cancel_patching(CPatcher *patcher); | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright (C) 2014-2015 Andrew Gunnerson <[email protected]> | ||
* Copyright (C) 2014-2018 Andrew Gunnerson <[email protected]> | ||
* | ||
* This file is part of DualBootPatcher | ||
* | ||
|
@@ -19,6 +19,8 @@ | |
|
||
#pragma once | ||
|
||
#include <functional> | ||
|
||
#include "mbcommon/common.h" | ||
|
||
#include "mbpatcher/errors.h" | ||
|
@@ -39,9 +41,9 @@ namespace mb::patcher | |
class MB_EXPORT Patcher | ||
{ | ||
public: | ||
typedef void (*ProgressUpdatedCallback) (uint64_t, uint64_t, void *); | ||
typedef void (*FilesUpdatedCallback) (uint64_t, uint64_t, void *); | ||
typedef void (*DetailsUpdatedCallback) (const std::string &, void *); | ||
using ProgressUpdatedCallback = std::function<void(uint64_t, uint64_t)>; | ||
using FilesUpdatedCallback = std::function<void(uint64_t, uint64_t)>; | ||
using DetailsUpdatedCallback = std::function<void(const std::string &)>; | ||
|
||
virtual ~Patcher() {} | ||
|
||
|
@@ -73,12 +75,10 @@ class MB_EXPORT Patcher | |
* \param progress_cb Callback for receiving current progress values | ||
* \param files_cb Callback for receiving current files count | ||
* \param details_cb Callback for receiving detailed progress text | ||
* \param userdata Pointer to pass to callback functions | ||
*/ | ||
virtual bool patch_file(ProgressUpdatedCallback progress_cb, | ||
FilesUpdatedCallback files_cb, | ||
DetailsUpdatedCallback details_cb, | ||
void *userdata) = 0; | ||
virtual bool patch_file(const ProgressUpdatedCallback &progress_cb, | ||
const FilesUpdatedCallback &files_cb, | ||
const DetailsUpdatedCallback &details_cb) = 0; | ||
|
||
/*! | ||
* \brief Cancel the patching of a file | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright (C) 2015-2017 Andrew Gunnerson <[email protected]> | ||
* Copyright (C) 2015-2018 Andrew Gunnerson <[email protected]> | ||
* | ||
* This file is part of DualBootPatcher | ||
* | ||
|
@@ -55,10 +55,9 @@ class OdinPatcher : public Patcher | |
// Patching | ||
void set_file_info(const FileInfo * const info) override; | ||
|
||
bool patch_file(ProgressUpdatedCallback progress_cb, | ||
FilesUpdatedCallback files_cb, | ||
DetailsUpdatedCallback details_cb, | ||
void *userdata) override; | ||
bool patch_file(const ProgressUpdatedCallback &progress_cb, | ||
const FilesUpdatedCallback &files_cb, | ||
const DetailsUpdatedCallback &details_cb) override; | ||
|
||
void cancel_patching() override; | ||
|
||
|
@@ -85,9 +84,8 @@ class OdinPatcher : public Patcher | |
std::unordered_set<std::string> m_added_files; | ||
|
||
// Callbacks | ||
OdinPatcher::ProgressUpdatedCallback m_progress_cb; | ||
OdinPatcher::DetailsUpdatedCallback m_details_cb; | ||
void *m_userdata; | ||
const ProgressUpdatedCallback *m_progress_cb; | ||
const DetailsUpdatedCallback *m_details_cb; | ||
|
||
// Patching | ||
archive *m_a_input; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright (C) 2014-2017 Andrew Gunnerson <[email protected]> | ||
* Copyright (C) 2014-2018 Andrew Gunnerson <[email protected]> | ||
* | ||
* This file is part of DualBootPatcher | ||
* | ||
|
@@ -47,10 +47,9 @@ class RamdiskUpdater : public Patcher | |
// Patching | ||
void set_file_info(const FileInfo * const info) override; | ||
|
||
bool patch_file(ProgressUpdatedCallback progress_cb, | ||
FilesUpdatedCallback files_cb, | ||
DetailsUpdatedCallback details_cb, | ||
void *userdata) override; | ||
bool patch_file(const ProgressUpdatedCallback &progress_cb, | ||
const FilesUpdatedCallback &files_cb, | ||
const DetailsUpdatedCallback &details_cb) override; | ||
|
||
void cancel_patching() override; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright (C) 2014-2017 Andrew Gunnerson <[email protected]> | ||
* Copyright (C) 2014-2018 Andrew Gunnerson <[email protected]> | ||
* | ||
* This file is part of DualBootPatcher | ||
* | ||
|
@@ -50,10 +50,9 @@ class ZipPatcher : public Patcher | |
// Patching | ||
void set_file_info(const FileInfo * const info) override; | ||
|
||
bool patch_file(ProgressUpdatedCallback progress_cb, | ||
FilesUpdatedCallback files_cb, | ||
DetailsUpdatedCallback details_cb, | ||
void *userdata) override; | ||
bool patch_file(const ProgressUpdatedCallback &progress_cb, | ||
const FilesUpdatedCallback &files_cb, | ||
const DetailsUpdatedCallback &details_cb) override; | ||
|
||
void cancel_patching() override; | ||
|
||
|
@@ -73,10 +72,9 @@ class ZipPatcher : public Patcher | |
ErrorCode m_error; | ||
|
||
// Callbacks | ||
ProgressUpdatedCallback m_progress_cb; | ||
FilesUpdatedCallback m_files_cb; | ||
DetailsUpdatedCallback m_details_cb; | ||
void *m_userdata; | ||
const ProgressUpdatedCallback *m_progress_cb; | ||
const FilesUpdatedCallback *m_files_cb; | ||
const DetailsUpdatedCallback *m_details_cb; | ||
|
||
// Patching | ||
ZipCtx *m_z_input = nullptr; | ||
|
@@ -98,7 +96,7 @@ class ZipPatcher : public Patcher | |
void update_files(uint64_t files, uint64_t max_files); | ||
void update_details(const std::string &msg); | ||
|
||
static void la_progress_cb(uint64_t bytes, void *userdata); | ||
void la_progress_cb(uint64_t bytes); | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright (C) 2014-2017 Andrew Gunnerson <[email protected]> | ||
* Copyright (C) 2014-2018 Andrew Gunnerson <[email protected]> | ||
* | ||
* This file is part of DualBootPatcher | ||
* | ||
|
@@ -19,6 +19,7 @@ | |
|
||
#pragma once | ||
|
||
#include <functional> | ||
#include <string> | ||
#include <vector> | ||
|
||
|
@@ -61,13 +62,11 @@ class MinizipUtils | |
static bool copy_file_raw(void *source_handle, | ||
void *target_handle, | ||
const std::string &name, | ||
void (*cb)(uint64_t bytes, void *), | ||
void *userdata); | ||
const std::function<void(uint64_t bytes)> &cb); | ||
|
||
static bool read_to_memory(void *handle, | ||
std::vector<unsigned char> &output, | ||
void (*cb)(uint64_t bytes, void *), | ||
void *userdata); | ||
const std::function<void(uint64_t bytes)> &cb); | ||
|
||
static bool extract_file(void *handle, | ||
const std::string &directory); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright (C) 2014-2015 Andrew Gunnerson <[email protected]> | ||
* Copyright (C) 2014-2018 Andrew Gunnerson <[email protected]> | ||
* | ||
* This file is part of DualBootPatcher | ||
* | ||
|
@@ -54,39 +54,6 @@ | |
extern "C" | ||
{ | ||
|
||
struct CallbackWrapper | ||
{ | ||
ProgressUpdatedCallback progress_cb; | ||
FilesUpdatedCallback files_cb; | ||
DetailsUpdatedCallback details_cb; | ||
void *userdata; | ||
}; | ||
|
||
static void progress_cb_wrapper(uint64_t bytes, uint64_t max_bytes, | ||
void *userdata) | ||
{ | ||
CallbackWrapper *wrapper = reinterpret_cast<CallbackWrapper *>(userdata); | ||
if (wrapper->progress_cb != nullptr) { | ||
wrapper->progress_cb(bytes, max_bytes, wrapper->userdata); | ||
} | ||
} | ||
|
||
static void files_cb_wrapper(uint64_t files, uint64_t max_files, void *userdata) | ||
{ | ||
CallbackWrapper *wrapper = reinterpret_cast<CallbackWrapper *>(userdata); | ||
if (wrapper->files_cb) { | ||
wrapper->files_cb(files, max_files, userdata); | ||
} | ||
} | ||
|
||
static void details_cb_wrapper(const std::string &text, void *userdata) | ||
{ | ||
CallbackWrapper *wrapper = reinterpret_cast<CallbackWrapper *>(userdata); | ||
if (wrapper->details_cb) { | ||
wrapper->details_cb(text.c_str(), wrapper->userdata); | ||
} | ||
} | ||
|
||
/*! | ||
* \brief Get the error information | ||
* | ||
|
@@ -143,30 +110,39 @@ void mbpatcher_patcher_set_fileinfo(CPatcher *patcher, const CFileInfo *info) | |
* \brief Start patching the file | ||
* | ||
* \param patcher CPatcher object | ||
* \param progressCb Callback for receiving current progress value | ||
* \param detailsCb Callback for receiving detailed progress text | ||
* \param userData Pointer to pass to callback functions | ||
* \param progress_cb Callback for receiving current progress value | ||
* \param files_cb Callback for receiving current files count | ||
* \param details_cb Callback for receiving detailed progress text | ||
* \param userdata Pointer to pass to callback functions | ||
* \return true on success, otherwise false (and error set appropriately) | ||
* | ||
* \sa Patcher::patchFile() | ||
*/ | ||
bool mbpatcher_patcher_patch_file(CPatcher *patcher, | ||
ProgressUpdatedCallback progressCb, | ||
FilesUpdatedCallback filesCb, | ||
DetailsUpdatedCallback detailsCb, | ||
void *userData) | ||
ProgressUpdatedCallback progress_cb, | ||
FilesUpdatedCallback files_cb, | ||
DetailsUpdatedCallback details_cb, | ||
void *userdata) | ||
{ | ||
CASTP(patcher); | ||
|
||
CallbackWrapper wrapper; | ||
wrapper.progress_cb = progressCb; | ||
wrapper.files_cb = filesCb; | ||
wrapper.details_cb = detailsCb; | ||
wrapper.userdata = userData; | ||
|
||
return p->patch_file(&progress_cb_wrapper, &files_cb_wrapper, | ||
&details_cb_wrapper, | ||
reinterpret_cast<void *>(&wrapper)); | ||
return p->patch_file( | ||
[&](uint64_t bytes, uint64_t max_bytes) { | ||
if (progress_cb) { | ||
progress_cb(bytes, max_bytes, userdata); | ||
} | ||
}, | ||
[&](uint64_t files, uint64_t max_files) { | ||
if (files_cb) { | ||
files_cb(files, max_files, userdata); | ||
} | ||
}, | ||
[&](const std::string &text) { | ||
if (details_cb) { | ||
details_cb(text.c_str(), userdata); | ||
} | ||
} | ||
); | ||
} | ||
|
||
/*! | ||
|
Oops, something went wrong.