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
//! Split a SRM file into its components

use super::utils::word_swap;
use super::{Converter, IsEmpty, UserParams};

use crate::io::ReadExt;

use std::io::prelude::*;

#[derive(Clone, Debug, PartialEq)]
/// Parameters to split a SRM file
pub struct Params {
  srm_file: std::path::PathBuf,
  output_mupen: bool,
  out_dir: Option<std::path::PathBuf>,
  name: Option<String>,
}

impl Params {
  /// Creates a new [Params] value
  pub fn new<S>(srm_file: S) -> Self
  where
    S: Into<std::path::PathBuf>,
  {
    Self {
      srm_file: srm_file.into(),
      output_mupen: false,
      out_dir: None,
      name: None,
    }
  }

  /// Sets to output a mupen pack file instead of each controller pack
  pub fn set_output_mupen_pack(self, output_mupen: bool) -> Self {
    Self {
      output_mupen,
      ..self
    }
  }

  /// Sets the output directory
  pub fn set_out_dir<P>(self, out_dir: Option<P>) -> Self
  where
    P: Into<std::path::PathBuf>,
  {
    Self {
      out_dir: out_dir.map(|v| v.into()),
      ..self
    }
  }

  /// Sets the name of the file
  pub fn set_name<S>(self, name: Option<S>) -> Self
  where
    S: Into<String>,
  {
    Self {
      name: name.map(|v| v.into()),
      ..self
    }
  }

  /// Gets mutable access to this [Params].
  pub fn as_mut(&mut self) -> ParamsMut<'_> {
    ParamsMut(self)
  }
}

/// Mutable access to a [Params] value.
pub struct ParamsMut<'p>(&'p mut Params);

impl<'p> ParamsMut<'p> {
  /// Sets the SRM path.
  pub fn set_srm_file(&'p mut self, path: impl Into<std::path::PathBuf>) {
    self.0.srm_file = path.into();
  }
}

/// Enumerates the different validation results
#[derive(Debug)]
pub enum Validation {
  /// Successful validation
  Ok,
  /// SRM File Not Found
  FileNotFound,
  /// Invalid SRM File Size
  InvalidSrmSize,
  /// Output Directory is Not a Directory
  OutputDirIsNotDir,
  /// SRM File is Empty
  SrmEmpty,
}

impl Validation {
  /// Checks if the validation succeeded
  pub fn is_ok(&self) -> bool {
    matches!(self, Validation::Ok)
  }
}

impl std::fmt::Display for Validation {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      Validation::Ok => f.write_str("OK"),
      Validation::FileNotFound => f.write_str("SRM file does not exist"),
      Validation::InvalidSrmSize => f.write_str("invalid SRM file size"),
      Validation::OutputDirIsNotDir => f.write_str("output directory is not a directory"),
      Validation::SrmEmpty => f.write_str("SRM file has no data"),
    }
  }
}

impl std::ops::BitOr for Validation {
  type Output = Self;

  fn bitor(self, rhs: Self) -> Self::Output {
    match (self, rhs) {
      (Validation::Ok, rhs) => rhs,
      (lhs, _) => lhs,
    }
  }
}

impl From<Validation> for bool {
  fn from(value: Validation) -> Self {
    matches!(value, Validation::Ok)
  }
}

impl Converter for Params {
  type Error = super::Error;

  type Validation = Validation;

  fn validate(&self) -> Self::Validation {
    self
      .srm_file
      .metadata()
      .map_or(Validation::FileNotFound, |m| {
        if m.is_file() && m.len() == 0x48800 {
          let mut srm_data = super::SrmBuf::new();
          std::fs::File::open(&self.srm_file)
            .and_then(|mut f| f.read_up_to(&mut srm_data))
            .map_or(Validation::FileNotFound, |_| {
              if srm_data.has_save_data() {
                Validation::Ok
              } else {
                Validation::SrmEmpty
              }
            })
        } else {
          Validation::InvalidSrmSize
        }
      })
      | self.out_dir.as_ref().map_or(Validation::Ok, |o| {
        if o.is_dir() {
          Validation::Ok
        } else {
          Validation::OutputDirIsNotDir
        }
      })
  }

  fn convert(self, user_params: &UserParams) -> Result<(), Self::Error> {
    let Self {
      srm_file,
      output_mupen,
      out_dir,
      name,
    } = self;

    // get the name
    let name = name.map_or_else(
      || srm_file.file_name().map(std::path::PathBuf::from).unwrap(),
      std::path::PathBuf::from,
    );

    // build the base output path
    let base_path = out_dir.map_or_else(|| srm_file.clone(), |o| o.join(name));

    // load the srm data
    let mut srm_data = super::SrmBuf::new();
    std::fs::File::open(&srm_file)
      .and_then(|mut file| file.read_up_to(&mut srm_data))
      .map_err(|e| super::Error(srm_file, e))?;

    let file_writer = FileWriter::new(&base_path, user_params);

    if !srm_data.eeprom().is_empty() {
      if user_params.swap_bytes {
        word_swap(srm_data.eeprom_mut());
      }

      let buf = if srm_data.eeprom().is_4k() {
        srm_data.eeprom().as_4k()
      } else {
        srm_data.eeprom()
      };

      file_writer.create_file(buf.as_ref(), "eep")?;
    }

    if !srm_data.sram().is_empty() {
      file_writer.create_file(srm_data.sram(), "sra")?;
    }

    if !srm_data.flashram().is_empty() {
      if user_params.swap_bytes {
        word_swap(srm_data.flashram_mut());
      }
      file_writer.create_file(srm_data.flashram(), "fla")?;
    }

    if srm_data.controller_pack_iter().any(|cp| !cp.is_empty()) {
      if output_mupen {
        file_writer.create_file(srm_data.full_controller_pack(), "mpk")?;
      } else {
        for (i, cp) in srm_data.controller_pack_iter().enumerate() {
          if !cp.is_empty() {
            file_writer.create_file(cp.as_ref(), format!("mpk{}", i + 1))?;
          }
        }
      }
    }

    Ok(())
  }
}

struct FileWriter<'p> {
  base_path: &'p std::path::Path,
  create_opts: std::fs::OpenOptions,
}

impl<'p> FileWriter<'p> {
  fn new<'b: 'p>(base_path: &'b std::path::Path, user_params: &UserParams) -> Self {
    let mut create_opts = std::fs::OpenOptions::new();
    create_opts
      .create(user_params.overwrite)
      .create_new(!user_params.overwrite)
      .write(true);
    Self {
      base_path,
      create_opts,
    }
  }

  fn create_file<E: AsRef<std::ffi::OsStr>>(
    &self,
    buf: impl AsRef<[u8]>,
    ext: E,
  ) -> Result<(), super::Error> {
    let path = self.base_path.with_extension(ext);
    self
      .create_opts
      .open(&path)
      .and_then(|mut file| file.write_all(buf.as_ref()))
      .map_err(|e| super::Error(path, e))
  }
}

/// Checks the given path to determine if it can be used as a SRM file
pub fn can_be_srm<P>(path: P) -> Result<P, (P, crate::io::Error)>
where
  P: AsRef<std::path::Path>,
{
  match path.as_ref().metadata() {
    Ok(metadata) => {
      if metadata.is_file() && metadata.len() == 0x48800 {
        Ok(path)
      } else {
        Err(if metadata.is_dir() {
          (path, crate::io::Error::PathIsDirectory)
        } else {
          (path, crate::io::Error::InvalidSize)
        })
      }
    }
    Err(e) => {
      if e.kind() == std::io::ErrorKind::NotFound {
        if path
          .as_ref()
          .extension()
          .map_or(false, |ext| (ext.to_ascii_uppercase() == "SRM"))
        {
          Ok(path)
        } else {
          Err((path, crate::io::Error::InvalidExtension))
        }
      } else {
        Err((path, e.into()))
      }
    }
  }
}