Skip to content

Commit aaae3fe

Browse files
authored
Collapse duplicate file hashing function. (#1390)
1 parent 685e2c8 commit aaae3fe

7 files changed

Lines changed: 25 additions & 34 deletions

File tree

Common/Cpp/Filesystem/Filesystem.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ bool create_directories(const Path& path){
4242
return std::filesystem::create_directories(path.stdpath());
4343
}
4444

45+
bool remove(const Path& path){
46+
return std::filesystem::remove(path);
47+
}
4548
std::uintmax_t remove_all(const Path& path){
4649
return std::filesystem::remove_all(path.stdpath());
4750
}

Common/Cpp/Filesystem/Filesystem.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ bool create_directories(const Path& path);
4040

4141
// Delete the contents of the path (if it is a directory) and the contents of all its subdirectories, recursively.
4242
// Then delete the file/directory of path itself. Symlinks are not followed (symlink is removed, not its target).
43+
bool remove(const Path& path);
4344
std::uintmax_t remove_all(const Path& path);
4445

4546
// Copy a file.

SerialPrograms/Source/CommonFramework/ResourceDownload/DownloadThread.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ using std::endl;
2323

2424
namespace PokemonAutomation{
2525

26-
namespace fs = std::filesystem;
27-
28-
2926

3027

3128
DownloadThread::~DownloadThread(){
@@ -104,7 +101,7 @@ void DownloadThread::run_download(DownloadedResourceMetadata resource_metadata){
104101
try{
105102

106103
// delete directory and the old resource
107-
fs::remove_all(Filesystem::Path(resource_directory));
104+
Filesystem::remove_all(resource_directory);
108105

109106
// download
110107
std::string zip_path = resource_directory + "/temp.zip";
@@ -122,7 +119,7 @@ void DownloadThread::run_download(DownloadedResourceMetadata resource_metadata){
122119
// hash
123120
std::string hash =
124121
hash_file(
125-
*this,
122+
this,
126123
zip_path,
127124
[this](uint64_t bytes_done, uint64_t total_bytes){
128125
m_hooks.report_hash_progress(bytes_done, total_bytes);
@@ -149,23 +146,23 @@ void DownloadThread::run_download(DownloadedResourceMetadata resource_metadata){
149146
);
150147

151148
// delete old zip file
152-
fs::remove(Filesystem::Path(zip_path));
149+
Filesystem::remove(zip_path);
153150

154151
throw_if_cancelled();
155152

156153
}catch(OperationCancelledException&){
157154
// delete directory and the resource
158-
fs::remove_all(Filesystem::Path(resource_directory));
155+
Filesystem::remove_all(resource_directory);
159156

160157
throw;
161158
}catch(OperationFailedException&){
162159
// delete directory and the resource
163-
fs::remove_all(Filesystem::Path(resource_directory));
160+
Filesystem::remove_all(resource_directory);
164161

165162
throw;
166163
}catch(...){
167164
// delete directory and the resource
168-
fs::remove_all(Filesystem::Path(resource_directory));
165+
Filesystem::remove_all(resource_directory);
169166

170167
throw;
171168
}

SerialPrograms/Source/CommonFramework/Tools/FileHash.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace PokemonAutomation{
1919

2020

2121
std::string hash_file(
22-
CancellableScope& scope,
22+
CancellableScope* scope,
2323
const std::string& file_path,
2424
std::function<void(uint64_t bytes_done, uint64_t total_bytes)> hash_progress
2525
){
@@ -34,7 +34,9 @@ std::string hash_file(
3434

3535
QByteArray buffer(1024 * 1024, 0); // Pre-allocate 1MB once
3636
while (!file.atEnd()){
37-
scope.throw_if_cancelled();
37+
if (scope != nullptr){
38+
scope->throw_if_cancelled();
39+
}
3840

3941
qint64 num_bytes_in_chunk = file.read(buffer.data(), buffer.size());
4042
if (num_bytes_in_chunk == -1){
@@ -44,7 +46,9 @@ std::string hash_file(
4446
hash.addData(QByteArrayView(buffer.data(), num_bytes_in_chunk));
4547
total_bytes_read += num_bytes_in_chunk;
4648

47-
hash_progress(total_bytes_read, file_size);
49+
if (hash_progress != nullptr){
50+
hash_progress(total_bytes_read, file_size);
51+
}
4852
}
4953

5054
return hash.result().toHex().toStdString();

SerialPrograms/Source/CommonFramework/Tools/FileHash.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ namespace PokemonAutomation{
1717

1818
// uses SHA 256
1919
std::string hash_file(
20-
CancellableScope& scope,
20+
CancellableScope* scope,
2121
const std::string& file_path,
22-
std::function<void(uint64_t bytes_done, uint64_t total_bytes)> hash_progress
22+
std::function<void(uint64_t bytes_done, uint64_t total_bytes)> hash_progress = nullptr
2323
);
24+
inline std::string hash_file(const std::string& file_path){
25+
return hash_file(nullptr, file_path, nullptr);
26+
}
2427

2528

2629
}

SerialPrograms/Source/ML/Models/ML_ONNXRuntimeHelpers.cpp

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
* Helper functions to work with ONNX Runtime library
66
*/
77

8-
#include <QString>
9-
#include <QFile>
10-
#include <QCryptographicHash>
11-
#include <QByteArray>
12-
138
#include <iostream>
149
#include <string>
1510
#include <fstream>
@@ -19,27 +14,14 @@
1914
#include "Common/Cpp/Exceptions.h"
2015
#include "Common/Cpp/Filesystem/Filesystem.h"
2116
#include "CommonFramework/Logging/Logger.h"
17+
#include "CommonFramework/Tools/FileHash.h"
2218
#include "ML_OrtEnv.h"
2319
#include "ML_ONNXRuntimeHelpers.h"
2420

2521
namespace PokemonAutomation{
2622
namespace ML{
2723

2824

29-
// Computes the cryptographic hash of a file.
30-
std::string create_file_hash(const std::string& filepath){
31-
QFile file(QString::fromStdString(filepath));
32-
if (!file.open(QIODevice::ReadOnly)){
33-
return "";
34-
}
35-
36-
QCryptographicHash hash(QCryptographicHash::Sha256);
37-
if (hash.addData(&file)){
38-
return hash.result().toHex(0).toStdString();
39-
}else{
40-
return "";
41-
}
42-
}
4325

4426

4527
Ort::SessionOptions create_session_options(const std::string& model_cache_path, bool use_gpu){
@@ -143,7 +125,7 @@ Ort::SessionOptions create_session_options(const std::string& model_cache_path,
143125
// model_path: the model path to load the ML model. This is needed to ensure we delete the old model cache
144126
// when a new model
145127
std::pair<bool, std::string> clean_up_old_model_cache(const std::string& model_cache_path, const std::string& model_path){
146-
std::string file_hash = create_file_hash(model_path);
128+
std::string file_hash = hash_file(model_path);
147129
if (file_hash.size() == 0){
148130
// the model file cannot be loaded
149131
return {true, ""};

SerialPrograms/Source/ML/Models/ML_ONNXRuntimeHelpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
namespace PokemonAutomation{
1717
namespace ML{
1818

19+
1920
// Create an ONNX SessionOptions
2021
// If on macOS, will use CoreML as the backend.
2122
// If on Windows, will try CUDA first (NVIDIA GPUs), then DirectML (all GPU vendors).

0 commit comments

Comments
 (0)