-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.rs
291 lines (245 loc) · 8.45 KB
/
build.rs
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#![deny(missing_debug_implementations, missing_copy_implementations)]
#![deny(clippy::all, clippy::pedantic, clippy::nursery, clippy::unwrap_used)]
#![allow(
clippy::missing_panics_doc,
clippy::module_name_repetitions,
clippy::similar_names
)]
#[allow(warnings)]
#[allow(missing_debug_implementations, missing_copy_implementations)]
#[allow(clippy::all, clippy::pedantic, clippy::nursery, clippy::unwrap_used)]
#[rustfmt::skip]
#[path = "./src/generated/schema_generated.rs"]
mod schema_generated;
use std::io::{Result, Write};
use std::path::Path;
use std::{env, fs};
mod generated {
pub use super::schema_generated::rman;
}
use flatbuffers::WIPOffset;
use generated::rman::{Bundle, BundleArgs};
use generated::rman::{Chunk, ChunkArgs};
use generated::rman::{ChunkingParam, ChunkingParamArgs};
use generated::rman::{Directory, DirectoryArgs};
use generated::rman::{File, FileArgs};
use generated::rman::{Key, KeyArgs};
use generated::rman::{Manifest, ManifestArgs};
use generated::rman::{Tag, TagArgs};
trait MakeHeader {
fn make_header(&self, compressed_size: u32, uncompressed_size: u32) -> Vec<u8> {
const MAGIC: [u8; 4] = [b'R', b'M', b'A', b'N'];
const MAJOR: [u8; 1] = [2];
const MINOR: [u8; 1] = [0];
const FLAGS: [u8; 2] = 512u16.to_le_bytes();
const OFFSET: [u8; 4] = 28u32.to_le_bytes();
const MANIFEST_ID: [u8; 8] = 0u64.to_le_bytes();
[
&MAGIC[..],
&MAJOR[..],
&MINOR[..],
&FLAGS[..],
&OFFSET[..],
&compressed_size.to_le_bytes(),
&MANIFEST_ID[..],
&uncompressed_size.to_le_bytes(),
]
.concat()
}
}
trait WriteFile: MakeHeader {
fn name(&self) -> &str;
fn write_manifest_file(&self, bytes: &[u8]) -> Result<()> {
let uncompressed_size = bytes.len();
let compressed = zstd::encode_all(bytes, 19).expect("error compressing");
let compressed_size = compressed.len();
let head = self.make_header(
compressed_size
.try_into()
.expect("`usize` to `u32` conversion failed"),
uncompressed_size
.try_into()
.expect("`usize` to `u32` conversion failed"),
);
let bytes = [&head[..], &compressed[..]].concat();
let out_dir =
env::var_os("OUT_DIR").expect("environment variable `OUT_DIR` does not exist");
let mut path = Path::new(&out_dir).join(self.name());
path.set_extension("manifest");
let mut file = fs::File::create(path)?;
file.write_all(&bytes)?;
Ok(())
}
fn write_bundle_file(&self, bytes: &[u8]) -> Result<(u32, u32)> {
let uncompressed_size = bytes.len();
let compressed = zstd::encode_all(bytes, 19).expect("error compressing");
let compressed_size = compressed.len();
let out_dir =
env::var_os("OUT_DIR").expect("environment variable `OUT_DIR` does not exist");
let mut path = Path::new(&out_dir).join(self.name());
path.set_extension("bundle");
let mut file = fs::File::create(path)?;
file.write_all(&compressed)?;
Ok((
compressed_size
.try_into()
.expect("`usize` to `u32` conversion failed"),
uncompressed_size
.try_into()
.expect("`usize` to `u32` conversion failed"),
))
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct ValidManifest<'a> {
name: &'a str,
}
impl<'a> ValidManifest<'a> {
#[must_use]
pub const fn new() -> Self {
Self { name: "valid" }
}
pub fn generate(&self) {
let mut builder = flatbuffers::FlatBufferBuilder::new();
let bundle_data = vec![b'T', b'E', b'S', b'T'];
let (compressed_size, uncompressed_size) = self
.write_bundle_file(&bundle_data)
.expect("writing bundle file failed");
let chunk = Chunk::create(
&mut builder,
&ChunkArgs {
id: 0,
compressed_size,
uncompressed_size,
},
);
let chunks = Some(builder.create_vector(&[chunk]));
let bundle_0 = Bundle::create(&mut builder, &BundleArgs { id: 0, chunks });
let name = Some(builder.create_string(""));
let dir_root = Directory::create(
&mut builder,
&DirectoryArgs {
id: 0,
parent_id: 0,
name,
},
);
let name = Some(builder.create_string("Test"));
let dir_test = Directory::create(
&mut builder,
&DirectoryArgs {
id: 1,
parent_id: 0,
name,
},
);
let name = Some(builder.create_string("en_US"));
let tag_en_us = Tag::create(&mut builder, &TagArgs { id: 0, name });
let key_0 = Key::create(&mut builder, &KeyArgs { unk0: 1, unk1: 4 });
let param_0 = ChunkingParam::create(
&mut builder,
&ChunkingParamArgs {
unk0: 0,
chunking_version: 3,
min_chunk_size: 2,
chunk_size: 8,
max_chunk_size: 32,
},
);
let name = Some(builder.create_string("file.txt"));
let symlink = Some(builder.create_string(""));
let chunk_ids = Some(builder.create_vector(&[0i64]));
let file = File::create(
&mut builder,
&FileArgs {
id: 0,
directory_id: 1,
size_: 0,
name,
tag_bitmask: 0,
unk5: 0,
unk6: 0,
chunk_ids,
unk8: 0,
symlink,
unk10: 0,
chunking_param_id: 0,
permissions: 0,
},
);
let bundles = Some(builder.create_vector(&[bundle_0]));
let tags = Some(builder.create_vector(&[tag_en_us]));
let files = Some(builder.create_vector(&[file]));
let directories = Some(builder.create_vector(&[dir_root, dir_test]));
let keys = Some(builder.create_vector(&[key_0]));
let chunking_params = Some(builder.create_vector(&[param_0]));
let manifest = Manifest::create(
&mut builder,
&ManifestArgs {
bundles,
tags,
files,
directories,
keys,
chunking_params,
},
);
builder.finish(manifest, None);
self.write_manifest_file(builder.finished_data())
.expect("writing manifest file failed");
}
}
impl<'a> MakeHeader for ValidManifest<'a> {}
impl<'a> WriteFile for ValidManifest<'a> {
fn name(&self) -> &str {
self.name
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct ValidEmptyManifest<'a> {
name: &'a str,
}
impl<'a> ValidEmptyManifest<'a> {
#[must_use]
pub const fn new() -> Self {
Self {
name: "valid_empty",
}
}
pub fn generate(&self) {
let mut builder = flatbuffers::FlatBufferBuilder::new();
let bundles = Some(builder.create_vector::<WIPOffset<Bundle>>(&[]));
let tags = Some(builder.create_vector::<WIPOffset<Tag>>(&[]));
let files = Some(builder.create_vector::<WIPOffset<File>>(&[]));
let directories = Some(builder.create_vector::<WIPOffset<Directory>>(&[]));
let keys = Some(builder.create_vector::<WIPOffset<Key>>(&[]));
let chunking_params = Some(builder.create_vector::<WIPOffset<ChunkingParam>>(&[]));
let manifest = Manifest::create(
&mut builder,
&ManifestArgs {
bundles,
tags,
files,
directories,
keys,
chunking_params,
},
);
builder.finish(manifest, None);
self.write_manifest_file(builder.finished_data())
.expect("writing manifest file failed");
}
}
impl<'a> MakeHeader for ValidEmptyManifest<'a> {}
impl<'a> WriteFile for ValidEmptyManifest<'a> {
fn name(&self) -> &str {
self.name
}
}
fn main() {
println!("cargo:rerun-if-changed=build.rs");
let valid_file = ValidManifest::new();
valid_file.generate();
let valid_empty_file = ValidEmptyManifest::new();
valid_empty_file.generate();
}