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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
#include "bs_archive.h"
namespace libbsarch {
bs_archive::bs_archive()
: archive_(bsa_create())
{}
bs_archive::bs_archive(const bsa_archive_type_t &type)
: archive_(bsa_create())
, type_(type)
{}
bs_archive::~bs_archive()
{
bsa_close(archive_);
bsa_free(archive_);
}
/* Properties */
convertible_string bs_archive::get_filename()
{
wchar_t name[max_string_buffer_size];
bsa_filename_get(archive_, max_string_buffer_size, name);
return name;
}
bsa_archive_type_t bs_archive::get_type()
{
return bsa_archive_type_get(archive_);
}
uint32_t bs_archive::get_version()
{
return bsa_version_get(archive_);
}
convertible_string bs_archive::get_format_name()
{
wchar_t format_name[max_string_buffer_size];
bsa_format_name_get(archive_, max_string_buffer_size, format_name);
return format_name;
}
uint32_t bs_archive::get_file_count()
{
return bsa_file_count_get(archive_);
}
uint32_t bs_archive::get_archive_flags()
{
return bsa_archive_flags_get(archive_);
}
void bs_archive::set_archive_flags(uint32_t flags)
{
bsa_archive_flags_set(archive_, flags);
}
uint32_t bs_archive::get_file_flags()
{
return bsa_file_flags_get(archive_);
}
void bs_archive::set_file_flags(uint32_t flags)
{
bsa_file_flags_set(archive_, flags);
}
void bs_archive::set_compressed(bool value)
{
bsa_compress_set(archive_, value);
}
void bs_archive::set_share_data(bool value)
{
bsa_share_data_set(archive_, value);
}
bool bs_archive::get_compressed()
{
return bsa_compress_get(archive_);
}
bool bs_archive::get_share_data()
{
return bsa_share_data_get(archive_);
}
void bs_archive::load_from_disk(const convertible_string &archive_path)
{
debug_log() << "Opening archive: " << archive_path;
const std::wstring str = archive_path;
const auto &result = bsa_load_from_file(archive_, str.c_str());
libbsarch::checkResult(result);
}
void bs_archive::create(const convertible_string &archiveName, const bs_archive_entries &entries)
{
create(archiveName, entries, type_);
}
void bs_archive::create(const convertible_string &archiveName,
const bs_archive_entries &entries,
const bsa_archive_type_t type)
{
debug_log() << "Creating archive. Archive name: " << archiveName << '\n'
<< "type: " << type_ << '\n'
<< "entries: " << entries.getEntries();
bsa_create_archive(archive_, archiveName, type, entries.getEntries());
}
void bs_archive::save() const
{
debug_log() << "Saving archive: " << archive_;
const auto &result = bsa_save(archive_);
libbsarch::checkResult(result);
}
void bs_archive::add_file_from_disk(const disk_blob &blob)
{
debug_log() << "Adding file from disk root. Path in archive: " << blob.path_in_archive << '\n'
<< "Filepath: " << blob.path_on_disk << '\n'
<< "Archive: " << archive_;
const auto &result = bsa_add_file_from_disk(archive_, blob.path_in_archive, blob.path_on_disk);
libbsarch::checkResult(result);
}
void bs_archive::add_file_from_memory(const convertible_string &path_in_archive,
const memory_blob &memory_data) //NOTE UNTESTED
{
debug_log() << "Adding file from memory. Filename: " << path_in_archive << '\n'
<< "Data size: " << memory_data.size;
const auto &result = bsa_add_file_from_memory(archive_, path_in_archive, memory_data.size, memory_data.data);
libbsarch::checkResult(result);
}
bsa_file_record_t bs_archive::find_file_record(const convertible_string &filename)
{
debug_log() << "Finding file record of: " << filename;
const auto &result = bsa_find_file_record(archive_, filename);
return result;
}
memory_blob bs_archive::extract_to_memory(bsa_file_record_t record)
{
const auto &result = bsa_extract_file_data_by_record(archive_, record);
libbsarch::checkResult(result);
return memory_blob(result.buffer, archive_);
}
memory_blob bs_archive::extract_to_memory(const convertible_string &filename)
{
const auto &result = bsa_extract_file_data_by_filename(archive_, filename);
libbsarch::checkResult(result);
return memory_blob(result.buffer, archive_);
}
void bs_archive::extract_to_disk(const convertible_string &filename, const convertible_string &save_as) const
{
debug_log() << "Extracting: " << filename << " saved as " << save_as;
const auto &result = bsa_extract_file(archive_, filename, save_as);
libbsarch::checkResult(result);
}
std::vector<convertible_string> bs_archive::list_files() const
{
bsa_entry_list_t list = bsa_entry_list_create();
const auto &result = bsa_get_resource_list(archive_, list, L"");
libbsarch::checkResult(result);
return bs_archive_entries(list).list();
}
bsa_archive_t bs_archive::get_archive() const
{
return archive_;
}
} // namespace libbsarch
|