-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobserver.cpp
More file actions
164 lines (138 loc) · 5.16 KB
/
observer.cpp
File metadata and controls
164 lines (138 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "observer.h"
#include "../../api.h"
#include <fstream>
#include <windows.h>
#include <catch2/catch_test_macros.hpp>
namespace test
{
class c_module final : public module
{
public:
explicit c_module(const std::string &dll_name)
{
dll_ = LoadLibrary(dll_name.c_str());
if (dll_ == nullptr) {
throw std::runtime_error("Failed to load DLL");
}
const auto load = reinterpret_cast<LoadSubModuleFunc>(GetProcAddress(dll_, "LoadSubModule"));
unload_module_ = reinterpret_cast<UnloadSubModuleFunc>(GetProcAddress(dll_, "UnloadSubModule"));
ModuleLoadParameters load_params{};
load_params.StructSize = sizeof(load_params);
load_params.Settings = nullptr;
load(&load_params);
api_ = load_params.ApiFuncs;
module_loaded_ = true;
}
~c_module() override
{
if (storage_ != nullptr) {
api_.CloseStorage(storage_);
}
if (module_loaded_) {
unload_module_();
unload_module_ = nullptr;
}
if (dll_ != nullptr) {
FreeLibrary(dll_);
}
}
bool open(const std::filesystem::path &path)
{
std::ifstream input(path, std::ios::binary);
REQUIRE(input.is_open());
input.exceptions(std::ofstream::failbit | std::ofstream::badbit);
auto len = 128 * 1024;
std::string signature;
signature.resize(len);
input.read(signature.data(), len);
StorageOpenParams open_params{
.StructSize = sizeof(StorageOpenParams),
.FilePath = path.c_str(),
.Password = nullptr,
.Data = signature.c_str(),
.DataSize = static_cast<size_t>(input.gcount()),
};
StorageGeneralInfo info{};
// TODO mimic observer behavior: call OpenStorage twice with different params
bool success = api_.OpenStorage(open_params, &storage_, &info) == SOR_SUCCESS;
if (success) {
REQUIRE(!std::wstring(info.Format).empty());
REQUIRE(!std::wstring(info.Compression).empty());
REQUIRE(!std::wstring(info.Comment).empty());
}
return success;
}
std::vector<file> list_files()
{
REQUIRE(storage_ != nullptr);
REQUIRE(api_.PrepareFiles(storage_));
std::vector<file> files{};
int item_index = 0;
while (true) {
StorageItemInfo item{};
const auto status = api_.GetItem(storage_, item_index, &item);
if (status == GET_ITEM_NOMOREITEMS) {
break;
}
REQUIRE(status == GET_ITEM_OK);
REQUIRE(item.Attributes == FILE_ATTRIBUTE_NORMAL);
REQUIRE(item.PackedSize > 0);
REQUIRE(item.Size >= item.PackedSize);
REQUIRE(item.NumHardlinks == 0);
REQUIRE(wcslen(item.Path) > 0);
auto extract = [this, item_index](const std::filesystem::path &path)
{
extract_file(item_index, path);
};
files.emplace_back(item.Path, item.Size, item.PackedSize, extract);
++item_index;
}
return files;
}
void extract_file(const int file_index, const std::filesystem::path &path) const
{
constexpr ExtractProcessCallbacks callbacks{
.signalContext = nullptr,
.FileProgress = [](void *context, int64_t bytes_read)
{
return TRUE;
},
};
const ExtractOperationParams params{
.ItemIndex = file_index,
.Flags = 0,
.DestPath = path.c_str(),
.Password = nullptr,
.Callbacks = callbacks,
};
REQUIRE(api_.ExtractItem(storage_, params) == SER_SUCCESS);
}
private:
HMODULE dll_;
bool module_loaded_;
UnloadSubModuleFunc unload_module_;
module_cbs api_{};
HANDLE storage_ = nullptr;
};
observer::observer()
{
modules_.push_back(std::make_unique<c_module>("renpy.so"));
modules_.push_back(std::make_unique<c_module>("rpgmaker.so"));
modules_.push_back(std::make_unique<c_module>("zanzarah.so"));
}
std::vector<file> observer::list_files(const std::filesystem::path &path) const
{
c_module *module = nullptr;
for (const auto &abstract_module: modules_) {
const auto candidate = dynamic_cast<c_module *>(abstract_module.get());
REQUIRE(candidate != nullptr);
if (candidate->open(path)) {
REQUIRE(module == nullptr);
module = candidate;
}
}
REQUIRE(module != nullptr);
// ReSharper disable once CppDFANullDereference
return module->list_files();
}
}