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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
//! Basic traits and types for script.
use crate::ext::io::*;
use crate::types::*;
use anyhow::Result;
use std::collections::HashMap;
use std::io::{Read, Seek, Write};
/// A trait for reading and seeking in a stream.
pub trait ReadSeek: Read + Seek + std::fmt::Debug {}
/// A trait for writing and seeking in a stream.
pub trait WriteSeek: Write + Seek {}
/// A trait for types that can be displayed in debug format and are also support downcasting.
pub trait AnyDebug: std::fmt::Debug + std::any::Any {}
/// A trait for reading in a stream with debug format.
pub trait ReadDebug: Read + std::fmt::Debug {}
impl<T: Read + Seek + std::fmt::Debug> ReadSeek for T {}
impl<T: Read + std::fmt::Debug> ReadDebug for T {}
impl<T: Write + Seek> WriteSeek for T {}
impl<T: std::fmt::Debug + std::any::Any> AnyDebug for T {}
/// A trait for script builders.
pub trait ScriptBuilder: std::fmt::Debug {
/// Returns the default encoding for the script.
fn default_encoding(&self) -> Encoding;
/// Returns the default encoding for the archive.
/// If None, the default encoding should be used.
fn default_archive_encoding(&self) -> Option<Encoding> {
None
}
/// Returns the default encoding for script files when patching scripts.
fn default_patched_encoding(&self) -> Encoding {
self.default_encoding()
}
/// Builds a script from the given buffer.
///
/// * `buf` - The buffer containing the script data.
/// * `filename` - The name of the file from which the script was read.
/// * `encoding` - The encoding of the script data.
/// * `archive_encoding` - The encoding of the archive, if applicable.
/// * `config` - Additional configuration options.
/// * `archive` - An optional archive to which the script belongs.
fn build_script(
&self,
buf: Vec<u8>,
filename: &str,
encoding: Encoding,
archive_encoding: Encoding,
config: &ExtraConfig,
archive: Option<&Box<dyn Script>>,
) -> Result<Box<dyn Script>>;
/// Builds a script from a file.
///
/// * `filename` - The name of the file to read.
/// * `encoding` - The encoding of the script data.
/// * `archive_encoding` - The encoding of the archive, if applicable.
/// * `config` - Additional configuration options.
/// * `archive` - An optional archive to which the script belongs.
fn build_script_from_file(
&self,
filename: &str,
encoding: Encoding,
archive_encoding: Encoding,
config: &ExtraConfig,
archive: Option<&Box<dyn Script>>,
) -> Result<Box<dyn Script>> {
let data = crate::utils::files::read_file(filename)?;
self.build_script(data, filename, encoding, archive_encoding, config, archive)
}
/// Builds a script from a reader.
///
/// * `reader` - A reader with seek capabilities.
/// * `filename` - The name of the file from which the script was read.
/// * `encoding` - The encoding of the script data.
/// * `archive_encoding` - The encoding of the archive, if applicable.
/// * `config` - Additional configuration options.
/// * `archive` - An optional archive to which the script belongs.
fn build_script_from_reader(
&self,
mut reader: Box<dyn ReadSeek>,
filename: &str,
encoding: Encoding,
archive_encoding: Encoding,
config: &ExtraConfig,
archive: Option<&Box<dyn Script>>,
) -> Result<Box<dyn Script>> {
let mut data = Vec::new();
reader
.read_to_end(&mut data)
.map_err(|e| anyhow::anyhow!("Failed to read from reader: {}", e))?;
self.build_script(data, filename, encoding, archive_encoding, config, archive)
}
/// Returns the extensions supported by this script builder.
fn extensions(&self) -> &'static [&'static str];
/// Checks if the given filename and buffer match this script format.
/// * `filename` - The name of the file to check.
/// * `buf` - The buffer containing the script data.
/// * `buf_len` - The length of the buffer.
///
/// Returns a score (0-255) indicating how well the format matches.
/// A higher score means a better match.
fn is_this_format(&self, _filename: &str, _buf: &[u8], _buf_len: usize) -> Option<u8> {
None
}
/// Returns the script type associated with this builder.
fn script_type(&self) -> &'static ScriptType;
/// Returns true if this script is an archive.
fn is_archive(&self) -> bool {
false
}
/// Creates an archive with the given files.
///
/// * `filename` - The path of the archive file to create.
/// * `files` - A list of files to include in the archive.
/// * `encoding` - The encoding to use for the archive.
/// * `config` - Additional configuration options.
fn create_archive(
&self,
_filename: &str,
_files: &[&str],
_encoding: Encoding,
_config: &ExtraConfig,
) -> Result<Box<dyn Archive>> {
Err(anyhow::anyhow!(
"This script type does not support creating an archive."
))
}
/// Returns true if this script type can create from a file directly.
fn can_create_file(&self) -> bool {
false
}
/// Creates a new script file.
///
/// * `filename` - The path to the input file.
/// * `writer` - A writer with seek capabilities to write the script data.
/// * `encoding` - The encoding to use for the script data.
/// * `file_encoding` - The encoding of the file.
/// * `config` - Additional configuration options.
fn create_file<'a>(
&'a self,
_filename: &'a str,
_writer: Box<dyn WriteSeek + 'a>,
_encoding: Encoding,
_file_encoding: Encoding,
_config: &ExtraConfig,
) -> Result<()> {
Err(anyhow::anyhow!(
"This script type does not support creating directly."
))
}
/// Creates a new script file with the given filename.
///
/// * `filename` - The path to the input file.
/// * `output_filename` - The path to the output file.
/// * `encoding` - The encoding to use for the script data.
/// * `file_encoding` - The encoding of the file.
/// * `config` - Additional configuration options.
fn create_file_filename(
&self,
filename: &str,
output_filename: &str,
encoding: Encoding,
file_encoding: Encoding,
config: &ExtraConfig,
) -> Result<()> {
let f = std::fs::File::create(output_filename)?;
let f = std::io::BufWriter::new(f);
self.create_file(filename, Box::new(f), encoding, file_encoding, config)
}
/// Returns true if this script is an image.
#[cfg(feature = "image")]
fn is_image(&self) -> bool {
false
}
/// Returns true if this script type can create from an image file directly.
#[cfg(feature = "image")]
fn can_create_image_file(&self) -> bool {
false
}
/// Creates an image file from the given data.
///
/// * `data` - The image data to write.
/// * `filename` - The path to the image file.
/// * `writer` - A writer with seek capabilities to write the image data.
/// * `options` - Additional configuration options.
#[cfg(feature = "image")]
fn create_image_file<'a>(
&'a self,
_data: ImageData,
_filename: &str,
_writer: Box<dyn WriteSeek + 'a>,
_options: &ExtraConfig,
) -> Result<()> {
Err(anyhow::anyhow!(
"This script type does not support creating an image file."
))
}
/// Creates an image file from the given data to the specified filename.
///
/// * `data` - The image data to write.
/// * `filename` - The path to the output file.
/// * `options` - Additional configuration options.
/// * `image_filename` - The path to the image file.
#[cfg(feature = "image")]
fn create_image_file_filename(
&self,
data: ImageData,
filename: &str,
image_filename: &str,
options: &ExtraConfig,
) -> Result<()> {
let f = std::fs::File::create(filename)?;
let f = std::io::BufWriter::new(f);
self.create_image_file(data, image_filename, Box::new(f), options)
}
}
/// A trait to present the file in an archive.
pub trait ArchiveContent: Read {
/// Returns the name of the file in the archive.
fn name(&self) -> &str;
/// Returns true if the file is a script.
fn is_script(&self) -> bool {
self.script_type().is_some()
}
/// Returns the script type if the file is a script.
fn script_type(&self) -> Option<&ScriptType> {
None
}
/// Returns the data of the file as a vector of bytes.
fn data(&mut self) -> Result<Vec<u8>> {
let mut data = Vec::new();
self.read_to_end(&mut data)?;
Ok(data)
}
/// Returns a reader that supports reading and seeking.
fn to_data<'a>(&'a mut self) -> Result<Box<dyn ReadSeek + 'a>> {
Ok(Box::new(MemReader::new(self.data()?)))
}
}
/// A trait for script types.
pub trait Script: std::fmt::Debug + std::any::Any {
/// Returns the default output script type for this script.
fn default_output_script_type(&self) -> OutputScriptType;
/// Checks if the given output script type is supported by this script.
fn is_output_supported(&self, output: OutputScriptType) -> bool {
!matches!(output, OutputScriptType::Custom)
}
/// Returns the output extension for this script when exporting with custom output.
fn custom_output_extension<'a>(&'a self) -> &'a str {
""
}
/// Returns the default format options for this script.
fn default_format_type(&self) -> FormatOptions;
/// Returns true if this script can contains multiple message files.
fn multiple_message_files(&self) -> bool {
false
}
/// Extract messages from this script.
fn extract_messages(&self) -> Result<Vec<Message>> {
if !self.is_archive() {
return Err(anyhow::anyhow!(
"This script type does not support extracting messages."
));
}
Ok(vec![])
}
/// Extract multiple messages from this script.
fn extract_multiple_messages(&self) -> Result<HashMap<String, Vec<Message>>> {
if !self.multiple_message_files() {
return Err(anyhow::anyhow!(
"This script type does not support extracting multiple message files."
));
}
Ok(HashMap::new())
}
/// Import messages into this script.
///
/// * `messages` - The messages to import.
/// * `file` - A writer with seek capabilities to write the patched scripts.
/// * `filename` - The path of the file to write the patched scripts.
/// * `encoding` - The encoding to use for the patched scripts.
/// * `replacement` - An optional replacement table for message replacements.
fn import_messages<'a>(
&'a self,
_messages: Vec<Message>,
_file: Box<dyn WriteSeek + 'a>,
_filename: &str,
_encoding: Encoding,
_replacement: Option<&'a ReplacementTable>,
) -> Result<()> {
if !self.is_archive() {
return Err(anyhow::anyhow!(
"This script type does not support importing messages."
));
}
Ok(())
}
/// Import multiple messages into this script.
///
/// * `messages` - A map of filenames to messages to import.
/// * `file` - A writer with seek capabilities to write the patched scripts.
/// * `filename` - The path of the file to write the patched scripts.
/// * `encoding` - The encoding to use for the patched scripts.
/// * `replacement` - An optional replacement table for message replacements.s
fn import_multiple_messages<'a>(
&'a self,
_messages: HashMap<String, Vec<Message>>,
_file: Box<dyn WriteSeek + 'a>,
_filename: &str,
_encoding: Encoding,
_replacement: Option<&'a ReplacementTable>,
) -> Result<()> {
if !self.multiple_message_files() {
return Err(anyhow::anyhow!(
"This script type does not support importing multiple message files."
));
}
Ok(())
}
/// Import messages into this script.
///
/// * `messages` - The messages to import.
/// * `filename` - The path of the file to write the patched scripts.
/// * `encoding` - The encoding to use for the patched scripts.
/// * `replacement` - An optional replacement table for message replacements.
fn import_messages_filename(
&self,
messages: Vec<Message>,
filename: &str,
encoding: Encoding,
replacement: Option<&ReplacementTable>,
) -> Result<()> {
let f = std::fs::File::create(filename)?;
let f = std::io::BufWriter::new(f);
self.import_messages(messages, Box::new(f), filename, encoding, replacement)
}
/// Import multiple messages into this script.
///
/// * `messages` - A map of filenames to messages to import.
/// * `filename` - The path of the file to write the patched scripts.
/// * `encoding` - The encoding to use for the patched scripts.
/// * `replacement` - An optional replacement table for message replacements.
fn import_multiple_messages_filename(
&self,
messages: HashMap<String, Vec<Message>>,
filename: &str,
encoding: Encoding,
replacement: Option<&ReplacementTable>,
) -> Result<()> {
let f = std::fs::File::create(filename)?;
let f = std::io::BufWriter::new(f);
self.import_multiple_messages(messages, Box::new(f), filename, encoding, replacement)
}
/// Exports data from this script.
///
/// * `filename` - The path of the file to write the exported data.
/// * `encoding` - The encoding to use for the exported data.
fn custom_export(&self, _filename: &std::path::Path, _encoding: Encoding) -> Result<()> {
Err(anyhow::anyhow!(
"This script type does not support custom export."
))
}
/// Imports data into this script.
///
/// * `custom_filename` - The path of the file to import.
/// * `file` - A writer with seek capabilities to write the patched scripts.
/// * `encoding` - The encoding of the patched scripts.
/// * `output_encoding` - The encoding to use for the imported file.
fn custom_import<'a>(
&'a self,
_custom_filename: &'a str,
_file: Box<dyn WriteSeek + 'a>,
_encoding: Encoding,
_output_encoding: Encoding,
) -> Result<()> {
Err(anyhow::anyhow!(
"This script type does not support custom import."
))
}
/// Imports data into this script.
///
/// * `custom_filename` - The path of the file to import.
/// * `filename` - The path of the file to write the patched scripts.
/// * `encoding` - The encoding of the patched scripts.
/// * `output_encoding` - The encoding to use for the imported file.
fn custom_import_filename(
&self,
custom_filename: &str,
filename: &str,
encoding: Encoding,
output_encoding: Encoding,
) -> Result<()> {
let f = std::fs::File::create(filename)?;
let f = std::io::BufWriter::new(f);
self.custom_import(custom_filename, Box::new(f), encoding, output_encoding)
}
/// Returns true if this script is an archive.
fn is_archive(&self) -> bool {
false
}
/// Returns an iterator over archive filenames.
fn iter_archive_filename<'a>(
&'a self,
) -> Result<Box<dyn Iterator<Item = Result<String>> + 'a>> {
Err(anyhow::anyhow!(
"This script type does not support iterating over archive filenames."
))
}
/// Returns an iterator over archive offsets.
fn iter_archive_offset<'a>(&'a self) -> Result<Box<dyn Iterator<Item = Result<u64>> + 'a>> {
Err(anyhow::anyhow!(
"This script type does not support iterating over archive offsets."
))
}
/// Opens a file in the archive by its index.
fn open_file<'a>(&'a self, _index: usize) -> Result<Box<dyn ArchiveContent + 'a>> {
Err(anyhow::anyhow!(
"This script type does not support opening files."
))
}
/// Opens a file in the archive by its name.
///
/// * `name` - The name of the file to open.
/// * `ignore_case` - If true, the name comparison will be case-insensitive.
fn open_file_by_name<'a>(
&'a self,
name: &str,
ignore_case: bool,
) -> Result<Box<dyn ArchiveContent + 'a>> {
for (i, fname) in self.iter_archive_filename()?.enumerate() {
if let Ok(fname) = fname {
if fname == name || (ignore_case && fname.eq_ignore_ascii_case(name)) {
return self.open_file(i);
}
}
}
Err(anyhow::anyhow!(
"File with name '{}' not found in archive.",
name
))
}
/// Opens a file in the archive by its offset.
fn open_file_by_offset<'a>(&'a self, offset: u64) -> Result<Box<dyn ArchiveContent + 'a>> {
for (i, off) in self.iter_archive_offset()?.enumerate() {
if let Ok(off) = off {
if off == offset {
return self.open_file(i);
}
}
}
Err(anyhow::anyhow!(
"File with offset '{}' not found in archive.",
offset
))
}
/// Returns output extension for archive output folder.
fn archive_output_ext<'a>(&'a self) -> Option<&'a str> {
None
}
#[cfg(feature = "image")]
/// Returns true if this script type is an image.
fn is_image(&self) -> bool {
false
}
#[cfg(feature = "image")]
/// Exports the image data from this script.
fn export_image(&self) -> Result<ImageData> {
Err(anyhow::anyhow!(
"This script type does not support to export image."
))
}
#[cfg(feature = "image")]
/// Imports an image into this script.
///
/// * `data` - The image data to import.
/// * `filename` - The path of the image file.
/// * `file` - A writer with seek capabilities to write the patched scripts.
fn import_image<'a>(
&'a self,
_data: ImageData,
_filename: &str,
_file: Box<dyn WriteSeek + 'a>,
) -> Result<()> {
Err(anyhow::anyhow!(
"This script type does not support to import image."
))
}
#[cfg(feature = "image")]
/// Imports an image into this script.
///
/// * `data` - The image data to import.
/// * `filename` - The path of the file to write the patched scripts.
/// * `image_filename` - The path of the image file.
fn import_image_filename(
&self,
data: ImageData,
image_filename: &str,
filename: &str,
) -> Result<()> {
let f = std::fs::File::create(filename)?;
let f = std::io::BufWriter::new(f);
self.import_image(data, image_filename, Box::new(f))
}
#[cfg(feature = "image")]
/// Returns true if this script is contains multiple images.
fn is_multi_image(&self) -> bool {
false
}
#[cfg(feature = "image")]
/// Exports multiple images from this script.
fn export_multi_image<'a>(
&'a self,
) -> Result<Box<dyn Iterator<Item = Result<ImageDataWithName>> + 'a>> {
Err(anyhow::anyhow!(
"This script type does not support to export multi image."
))
}
#[cfg(feature = "image")]
/// Imports multiple images into this script.
///
/// * `data` - A vector of image data with names to import.
/// * `file` - A writer with seek capabilities to write the patched scripts.
fn import_multi_image<'a>(
&'a self,
_data: Vec<ImageDataWithName>,
_file: Box<dyn WriteSeek + 'a>,
) -> Result<()> {
Err(anyhow::anyhow!(
"This script type does not support to import multi image."
))
}
#[cfg(feature = "image")]
/// Imports multiple images into this script.
///
/// * `data` - A vector of image data with names to import.
/// * `filename` - The path of the file to write the patched scripts.
fn import_multi_image_filename(
&self,
data: Vec<ImageDataWithName>,
filename: &str,
) -> Result<()> {
let f = std::fs::File::create(filename)?;
let f = std::io::BufWriter::new(f);
self.import_multi_image(data, Box::new(f))
}
/// Returns the extra information for this script.
fn extra_info<'a>(&'a self) -> Option<Box<dyn AnyDebug + 'a>> {
None
}
}
/// A trait for creating archives.
pub trait Archive {
/// Returns an iterator of a list of filenames must writed before other files.
///
/// Should return None if no such requirement.
fn prelist<'a>(&'a self) -> Result<Option<Box<dyn Iterator<Item = Result<String>> + 'a>>> {
Ok(None)
}
/// Creates a new file in the archive.
///
/// size is optional, if provided, size must be exactly the size of the file to be created.
fn new_file<'a>(&'a mut self, name: &str, size: Option<u64>)
-> Result<Box<dyn WriteSeek + 'a>>;
/// Creates a new file in the archive that does not require seeking.
///
/// size is optional, if provided, size must be exactly the size of the file to be created.
fn new_file_non_seek<'a>(
&'a mut self,
name: &str,
size: Option<u64>,
) -> Result<Box<dyn Write + 'a>> {
self.new_file(name, size)
.map(|f| Box::new(f) as Box<dyn Write + 'a>)
}
/// Writes the header of the archive. (Must be called after writing all files.)
fn write_header(&mut self) -> Result<()>;
}