Files
threeSD/src/core/quick_decryptor.h
T
zhupengfei d55af0108e core, frontend: Better progress reporter
A new "quick" decryptor is implemented. This is not really much faster (not slower either) but provides the benefit of being able to report progress on a single file. The frontend is updated accordingly to support this feature.
2019-09-12 22:08:44 +08:00

63 lines
1.6 KiB
C++

// Copyright 2019 threeSD Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include <array>
#include <atomic>
#include <memory>
#include <string>
#include "common/common_types.h"
#include "common/thread.h"
namespace Core {
class QuickDecryptor {
public:
/// (current_size, total_size)
using ProgressCallback = std::function<void(std::size_t, std::size_t)>;
/**
* Initializes the decryptor.
* @param root_folder Path to the "Nintendo 3DS/<ID0>/<ID1>" folder.
*/
explicit QuickDecryptor(const std::string& root_folder);
~QuickDecryptor();
bool DecryptAndWriteFile(const std::string& source, const std::string& destination,
const ProgressCallback& callback = [](std::size_t, std::size_t) {});
void DataReadLoop();
void DataDecryptLoop();
void DataWriteLoop();
void Abort();
private:
static constexpr std::size_t BufferSize = 16 * 1024; // 16 KB
std::string root_folder;
std::string source;
std::string destination;
std::size_t total_size{};
std::array<std::array<u8, BufferSize>, 3> buffers;
std::array<Common::Event, 3> data_read_event;
std::array<Common::Event, 3> data_decrypted_event;
std::array<Common::Event, 3> data_written_event;
std::unique_ptr<std::thread> read_thread;
std::unique_ptr<std::thread> decrypt_thread;
std::unique_ptr<std::thread> write_thread;
ProgressCallback callback;
Common::Event completion_event;
bool is_good{true};
std::atomic_bool is_running{false};
};
} // namespace Core