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
/*
 * reanim-decode: decoder for PvZ reanim files.
 * Copyright (c) 2022  Ruifeng Xie
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

//! Command line interface for `reanim-decode`.

use std::ffi::OsStr;
use std::fmt::{Debug, Display, Formatter};
use std::fs::File;
use std::io::{BufReader, Write};
use std::path::{Path, PathBuf};
use anyhow::Context;
use clap::{ValueEnum, Parser, Subcommand};
use tracing_subscriber::filter::LevelFilter;
use serde::{Serialize, Serializer};
use libre_pvz_resources::animation as packed;
use libre_pvz_resources::model;
use crate::reanim::Animation;
use crate::xml::Xml as XmlWrapper;

/// Optionally packed animations.
pub enum MaybePacked {
    /// Plain format, structurally equivalent to reanim XML.
    Plain(Animation),
    /// Packed format, compact and structural.
    Packed(packed::AnimDesc),
}

use MaybePacked::*;

impl MaybePacked {
    /// Is this already packed?
    pub fn is_packed(&self) -> bool { matches!(self, Packed(_)) }

    /// Force this to be packed, fail if unpacking is requested.
    pub fn into_packed(self, packed: bool) -> anyhow::Result<MaybePacked> {
        Ok(match self {
            Packed(_) if !packed => anyhow::bail!("unpacking not supported"),
            Plain(anim) if packed => Packed(anim.into()),
            _ => self,
        })
    }
}

impl Debug for MaybePacked {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Plain(anim) => anim.fmt(f),
            Packed(anim) => anim.fmt(f),
        }
    }
}

impl Serialize for MaybePacked {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        match self {
            Plain(anim) => anim.serialize(serializer),
            Packed(anim) => anim.serialize(serializer),
        }
    }
}

/// Entry of the command line interface.
#[derive(Debug, Parser)]
#[clap(author, version, about)]
pub struct Cli {
    /// Verbosity, for filtering diagnostics messages.
    #[clap(long, value_enum, global = true)]
    pub verbose: Option<Option<Verbosity>>,
    /// All available subcommands.
    #[clap(subcommand)]
    pub commands: Commands,
}

/// Output format: JSON and YAML supported, guarded by crate features.
#[derive(Debug, Copy, Clone, Eq, PartialEq, ValueEnum)]
#[allow(missing_docs)]
pub enum Verbosity { Off, Error, Warn, Info, Debug, Trace }

impl From<Verbosity> for LevelFilter {
    fn from(verb: Verbosity) -> Self {
        match verb {
            Verbosity::Off => LevelFilter::OFF,
            Verbosity::Error => LevelFilter::ERROR,
            Verbosity::Warn => LevelFilter::WARN,
            Verbosity::Info => LevelFilter::INFO,
            Verbosity::Debug => LevelFilter::DEBUG,
            Verbosity::Trace => LevelFilter::TRACE,
        }
    }
}

/// Output format: JSON and YAML supported, guarded by crate features.
#[derive(Debug, Copy, Clone, Eq, PartialEq, ValueEnum)]
pub enum Format {
    /// Internal encoding (Rust `{:#?}` debug pretty printing)
    Internal,
    /// Original "compiled format": file extension `.reanim.compiled`.
    Compiled,
    /// Binary encoding using `bincode`. Only support packed format.
    Bin,
    /// XML format as is used in original PvZ game.
    Xml,
    /// JSON format.
    Json,
    /// YAML format.
    Yaml,
}

use Format::*;
use libre_pvz_resources::dynamic::DynamicRegistry;

impl Display for Format {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_str(match self {
            Internal => "internal",
            Compiled => "compiled",
            Bin => "bin",
            Xml => "xml",
            Json => "json",
            Yaml => "yaml",
        })
    }
}

impl Format {
    /// Infer whether or not the output should be packed.
    pub fn infer_packed<P: AsRef<Path>>(path: P) -> bool {
        let file = path.as_ref();
        let ext = file.extension().and_then(OsStr::to_str);
        let stem = file.file_stem().and_then(OsStr::to_str);
        ext == Some("bin") || stem.map_or(false, |s| s.ends_with(".anim"))
    }

    /// Infer a format from given file name.
    pub fn infer<P: AsRef<Path>>(path: P) -> Option<Format> {
        match path.as_ref().extension()?.to_str()? {
            "txt" => Some(Internal),
            "compiled" => Some(Compiled),
            "bin" => Some(Bin),
            "reanim" | "xml" => Some(Xml),
            "json" => Some(Json),
            "yaml" | "yml" => Some(Yaml),
            _ => None,
        }
    }

    /// Decide an input/output format for a given (option, path, default) tuple.
    pub fn decide<P: AsRef<Path>>(spec: Option<Format>, path: Option<P>, default: Format) -> Format {
        spec.or_else(|| path.and_then(|p| Format::infer(p.as_ref()))).unwrap_or(default)
    }
}

/// Subcommands.
#[derive(Debug, Subcommand)]
pub enum Commands {
    /// Conversion for model files.
    Model {
        /// Input file path.
        input: PathBuf,
        /// Input format.
        #[clap(short = 'I', long, value_enum)]
        input_format: Option<Format>,
        /// Output file path.
        #[clap(short, long)]
        output: Option<PathBuf>,
        /// Output format.
        #[clap(short = 'O', long, value_enum)]
        output_format: Option<Format>,
    },
    /// Conversion for animation files.
    Anim {
        /// Input file path.
        input: PathBuf,
        /// Input format.
        #[clap(short = 'I', long, value_enum)]
        input_format: Option<Format>,
        /// Use structural format for input.
        #[clap(long)]
        pack_input: bool,
        /// Output file path.
        #[clap(short, long)]
        output: Option<PathBuf>,
        /// Output format.
        #[clap(short = 'O', long, value_enum)]
        output_format: Option<Format>,
        /// Use structural format for output.
        #[clap(long)]
        pack_output: bool,
    },
}

fn setup_logger(verbose: LevelFilter) {
    tracing_subscriber::fmt::Subscriber::builder()
        .with_target(true)
        .without_time()
        .with_max_level(verbose)
        .init()
}

const BINCODE_CONFIG: bincode::config::Configuration = bincode::config::standard();

impl Cli {
    /// Start command line interface.
    pub fn run() -> anyhow::Result<()> {
        let args = Cli::parse();
        setup_logger(match args.verbose {
            None => LevelFilter::WARN, // no '--verbose', only errors
            Some(None) => LevelFilter::INFO, // default '--verbose'
            Some(Some(verbose)) => verbose.into(), // explicit '--verbose'
        });
        DynamicRegistry::initialize_without_bevy();
        match args.commands {
            Commands::Model {
                input, input_format,
                output_format, output,
            } => {
                // open input & decode
                let input_format = Format::decide(input_format, Some(&input), Bin);
                let input = File::open(&input).with_context(|| format!("failed to read file {input:?}"))?;
                let mut input = BufReader::new(input);
                let model: model::Model = match input_format {
                    Internal | Compiled | Xml => anyhow::bail!("unsupported input format: {input_format}"),
                    Bin => bincode::decode_from_std_read(&mut input, BINCODE_CONFIG)?,
                    Json => serde_json::from_reader(&mut input)?,
                    Yaml => serde_yaml::from_reader(&mut input)?,
                };

                // infer output format
                let output_format = Format::decide(output_format, output.as_ref(), Internal);
                // output file (or stdout)
                if let Some(output) = output {
                    let context = || format!("failed to open output file {output:?}");
                    let output = File::create(&output).with_context(context)?;
                    encode_model(model, output_format, output)?;
                } else {
                    encode_model(model, output_format, std::io::stdout().lock())?;
                }
            }
            Commands::Anim {
                input, input_format, mut pack_input,
                output_format, output, mut pack_output,
            } => {
                // open input & decode
                pack_input |= Format::infer_packed(&input);
                let input_format = Format::decide(input_format, Some(&input), Compiled);
                let input = File::open(&input).with_context(|| format!("failed to read file {input:?}"))?;
                let mut input = BufReader::new(input);
                let anim = match input_format {
                    Internal | Xml => anyhow::bail!("unsupported input format: {input_format}"),
                    Bin => Packed(bincode::decode_from_std_read(&mut input, BINCODE_CONFIG)?),
                    Compiled => Plain(Animation::decompress_and_decode(&mut input)?),
                    Json if pack_input => Packed(serde_json::from_reader(&mut input)?),
                    Yaml if pack_input => Packed(serde_yaml::from_reader(&mut input)?),
                    Json => Plain(serde_json::from_reader(&mut input)?),
                    Yaml => Plain(serde_yaml::from_reader(&mut input)?),
                };

                // infer output format
                pack_output |= output.as_ref().map_or(anim.is_packed(), Format::infer_packed);
                let output_format = Format::decide(
                    output_format, output.as_ref(),
                    if pack_output { Internal } else { Xml },
                );
                let anim = anim.into_packed(pack_output)?;

                // output file (or stdout)
                if let Some(output) = output {
                    let context = || format!("failed to open output file {output:?}");
                    let output = File::create(&output).with_context(context)?;
                    encode_anim(anim, output_format, output)?;
                } else {
                    encode_anim(anim, output_format, std::io::stdout().lock())?;
                }
            }
        }
        Ok(())
    }
}

/// Encode the animation into required format.
pub fn encode_anim(anim: MaybePacked, format: Format, mut output: impl Write) -> anyhow::Result<()> {
    match (format, anim) {
        (Internal, anim) => writeln!(output, "{anim:#?}")?,
        (Bin, Packed(anim)) => {
            bincode::encode_into_std_write(anim, &mut output, BINCODE_CONFIG)?;
        }
        (Xml, Plain(anim)) => write!(output, "{}", XmlWrapper(anim))?,
        (Json, anim) => serde_json::to_writer_pretty(output, &anim)?,
        (Yaml, anim) => serde_yaml::to_writer(output, &anim)?,
        (format, anim) => {
            anyhow::bail!("format '{format}' does not support 'packed={}'", anim.is_packed());
        }
    }
    Ok(())
}

/// Encode the model into required format.
pub fn encode_model(model: model::Model, format: Format, mut output: impl Write) -> anyhow::Result<()> {
    match format {
        Compiled | Xml => anyhow::bail!("unsupported output format: '{format}'"),
        Internal => writeln!(output, "{model:#?}")?,
        Bin => { bincode::encode_into_std_write(&model, &mut output, BINCODE_CONFIG)?; }
        Json => serde_json::to_writer_pretty(output, &model)?,
        Yaml => serde_yaml::to_writer(output, &model)?,
    }
    Ok(())
}