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
//! The LiveSplit Saver saves Runs as LiveSplit splits files (*.lss).
//!
//! # Examples
//!
//! Using the LiveSplit Saver to save a Run as a LiveSplit splits file.
//!
//! ```no_run
//! use livesplit_core::run::saver::livesplit;
//! use livesplit_core::{Run, Segment};
//! use std::fs::File;
//! use std::io::BufWriter;
//!
//! // Create a run object that we can use.
//! let mut run = Run::new();
//! run.set_game_name("Super Mario Odyssey");
//! run.set_category_name("Any%");
//! run.push_segment(Segment::new("Cap Kingdom"));
//!
//! // Create the splits file.
//! let file = File::create("path/to/splits_file.lss");
//! let writer = BufWriter::new(file.expect("Failed creating the file"));
//!
//! // Save the splits file as a LiveSplit splits file.
//! livesplit::save_run(&run, writer).expect("Couldn't save the splits file");
//! ```

use crate::timing::formatter::{Complete, TimeFormatter};
use crate::{Image, Run, Time, TimeSpan, Timer, TimerPhase};
use byteorder::{WriteBytesExt, LE};
use chrono::{DateTime, Utc};
use quick_xml::events::{BytesDecl, BytesEnd, BytesStart, BytesText, Event};
use quick_xml::{Error as XmlError, Writer};
use std::borrow::Cow;
use std::fmt::Display;
use std::io::Write;
use std::mem::replace;
use std::result::Result as StdResult;

static LSS_IMAGE_HEADER: &[u8; 156] = include_bytes!("lss_image_header.bin");

quick_error! {
    #[derive(Debug)]
    /// The Error type for splits files that couldn't be saved by the LiveSplit
    /// Saver.
    pub enum Error {
        /// Failed writing as XML.
        Xml(err: XmlError) {
            from()
        }
    }
}

/// The Result type for the LiveSplit Saver.
pub type Result<T> = StdResult<T, Error>;

fn new_tag(name: &[u8]) -> BytesStart<'_> {
    BytesStart::borrowed(name, name.len())
}

fn write_start<W: Write>(writer: &mut Writer<W>, tag: BytesStart<'_>) -> Result<()> {
    writer.write_event(Event::Start(tag))?;
    Ok(())
}

fn write_end<W: Write>(writer: &mut Writer<W>, tag: &[u8]) -> Result<()> {
    writer.write_event(Event::End(BytesEnd::borrowed(tag)))?;
    Ok(())
}

fn split_tag<'a>(tag: &'a BytesStart<'a>) -> (BytesStart<'a>, BytesEnd<'a>) {
    (
        BytesStart::borrowed(&tag, tag.name().len()),
        BytesEnd::borrowed(tag.name()),
    )
}

fn bool(value: bool) -> &'static [u8] {
    if value {
        b"True"
    } else {
        b"False"
    }
}

fn scoped<W, F>(writer: &mut Writer<W>, tag: BytesStart<'_>, is_empty: bool, scope: F) -> Result<()>
where
    W: Write,
    F: FnOnce(&mut Writer<W>) -> Result<()>,
{
    if is_empty {
        writer.write_event(Event::Empty(tag))?;
    } else {
        let (start, end) = split_tag(&tag);
        writer.write_event(Event::Start(start))?;
        scope(writer)?;
        writer.write_event(Event::End(end))?;
    }
    Ok(())
}

fn scoped_iter<W, F, I>(
    writer: &mut Writer<W>,
    tag: BytesStart<'_>,
    iter: I,
    mut scope: F,
) -> Result<()>
where
    W: Write,
    I: IntoIterator,
    F: FnMut(&mut Writer<W>, <I as IntoIterator>::Item) -> Result<()>,
{
    let mut iter = iter.into_iter().peekable();
    scoped(writer, tag, iter.peek().is_none(), |writer| {
        for item in iter {
            scope(writer, item)?;
        }
        Ok(())
    })
}

fn text<W: Write, T: AsRef<[u8]>>(
    writer: &mut Writer<W>,
    tag: BytesStart<'_>,
    text: T,
) -> Result<()> {
    let text = text.as_ref();
    scoped(writer, tag, text.is_empty(), |writer| {
        writer.write_event(Event::Text(BytesText::from_plain(text)))?;
        Ok(())
    })
}

fn vec_as_string<F, R>(vec: &mut Vec<u8>, f: F) -> R
where
    F: FnOnce(&mut String) -> R,
{
    let taken = replace(vec, Vec::new());
    let mut string = String::from_utf8(taken).unwrap();
    let result = f(&mut string);
    let bytes = string.into_bytes();
    replace(vec, bytes);
    result
}

fn image<W: Write>(
    writer: &mut Writer<W>,
    tag: BytesStart<'_>,
    image: &Image,
    buf: &mut Vec<u8>,
    image_buf: &mut Cow<'_, [u8]>,
) -> Result<()> {
    let url = image.url();
    if url.starts_with("data:;base64,") {
        let src = &url["data:;base64,".len()..];
        buf.clear();
        if base64::decode_config_buf(src, base64::STANDARD, buf).is_ok() {
            let len = buf.len();
            let image_buf = image_buf.to_mut();
            image_buf.truncate(LSS_IMAGE_HEADER.len());
            image_buf.reserve(len + 6);
            image_buf.write_u32::<LE>(len as u32).unwrap();
            image_buf.push(0x2);
            image_buf.append(buf);
            image_buf.push(0xB);
            buf.clear();
            vec_as_string(buf, |s| {
                base64::encode_config_buf(image_buf, base64::STANDARD, s)
            });
            return scoped(writer, tag, buf.is_empty(), |writer| {
                writer.write_event(Event::CData(BytesText::from_plain(buf)))?;
                Ok(())
            });
        }
    }
    writer.write_event(Event::Empty(tag))?;
    Ok(())
}

fn fmt_date(date: DateTime<Utc>, buf: &mut Vec<u8>) -> &[u8] {
    fmt_buf(date.format("%m/%d/%Y %T"), buf)
}

fn fmt_buf<D: Display>(value: D, buf: &mut Vec<u8>) -> &[u8] {
    buf.clear();
    write!(buf, "{}", value).unwrap();
    buf
}

fn write_display<W: Write, D: Display>(
    writer: &mut Writer<W>,
    tag: BytesStart<'_>,
    value: D,
    buf: &mut Vec<u8>,
) -> Result<()> {
    text(writer, tag, fmt_buf(value, buf))
}

fn time_span<W: Write>(
    writer: &mut Writer<W>,
    tag: BytesStart<'_>,
    time: TimeSpan,
    buf: &mut Vec<u8>,
) -> Result<()> {
    write_display(writer, tag, Complete.format(time), buf)
}

fn time_inner<W: Write>(writer: &mut Writer<W>, time: Time, buf: &mut Vec<u8>) -> Result<()> {
    if let Some(time) = time.real_time {
        time_span(writer, new_tag(b"RealTime"), time, buf)?;
    }

    if let Some(time) = time.game_time {
        time_span(writer, new_tag(b"GameTime"), time, buf)?;
    }

    Ok(())
}

fn time<W: Write>(
    writer: &mut Writer<W>,
    tag: BytesStart<'_>,
    time: Time,
    buf: &mut Vec<u8>,
) -> Result<()> {
    scoped(
        writer,
        tag,
        time.real_time.is_none() && time.game_time.is_none(),
        |writer| time_inner(writer, time, buf),
    )
}

/// Saves the Run in use by the Timer provided as a LiveSplit splits file
/// (*.lss).
pub fn save_timer<W: Write>(timer: &Timer, writer: W) -> Result<()> {
    let run;
    let run = if timer.current_phase() == TimerPhase::NotRunning {
        timer.run()
    } else {
        run = timer.clone().into_run(true);
        &run
    };
    save_run(run, writer)
}

/// Saves a Run as a LiveSplit splits file (*.lss). Use the `save_timer`
/// function if the Run is in use by a timer in order to properly save the
/// current attempt as well.
pub fn save_run<W: Write>(run: &Run, writer: W) -> Result<()> {
    let writer = &mut Writer::new(writer);

    let buf = &mut Vec::new();
    let image_buf = &mut Cow::Borrowed(&LSS_IMAGE_HEADER[..]);

    writer.write_event(Event::Decl(BytesDecl::new(b"1.0", Some(b"UTF-8"), None)))?;
    writer.write_event(Event::Start(BytesStart::borrowed(
        br#"Run version="1.7.1""#,
        3,
    )))?;

    image(
        writer,
        new_tag(b"GameIcon"),
        run.game_icon(),
        buf,
        image_buf,
    )?;
    text(writer, new_tag(b"GameName"), run.game_name())?;
    text(writer, new_tag(b"CategoryName"), run.category_name())?;

    write_start(writer, new_tag(b"Metadata"))?;
    let metadata = run.metadata();

    let mut tag = new_tag(b"Run");
    tag.push_attribute((&b"id"[..], metadata.run_id().as_bytes()));
    writer.write_event(Event::Empty(tag))?;

    tag = new_tag(b"Platform");
    tag.push_attribute((&b"usesEmulator"[..], bool(metadata.uses_emulator())));
    text(writer, tag, metadata.platform_name())?;

    text(writer, new_tag(b"Region"), metadata.region_name())?;

    scoped_iter(
        writer,
        new_tag(b"Variables"),
        metadata.variables(),
        |writer, (name, value)| {
            let mut tag = new_tag(b"Variable");
            tag.push_attribute((&b"name"[..], name.as_bytes()));
            text(writer, tag, value)
        },
    )?;
    write_end(writer, b"Metadata")?;

    time_span(writer, new_tag(b"Offset"), run.offset(), buf)?;
    write_display(writer, new_tag(b"AttemptCount"), run.attempt_count(), buf)?;

    scoped_iter(
        writer,
        new_tag(b"AttemptHistory"),
        run.attempt_history(),
        |writer, attempt| {
            let mut tag = new_tag(b"Attempt");
            tag.push_attribute((&b"id"[..], fmt_buf(attempt.index(), buf)));

            if let Some(started) = attempt.started() {
                tag.push_attribute((&b"started"[..], fmt_date(started.time, buf)));
                tag.push_attribute((
                    &b"isStartedSynced"[..],
                    bool(started.synced_with_atomic_clock),
                ));
            }

            if let Some(ended) = attempt.ended() {
                tag.push_attribute((&b"ended"[..], fmt_date(ended.time, buf)));
                tag.push_attribute((&b"isEndedSynced"[..], bool(ended.synced_with_atomic_clock)));
            }

            let is_empty = attempt.time().real_time.is_none()
                && attempt.time().game_time.is_none()
                && attempt.pause_time().is_none();

            scoped(writer, tag, is_empty, |writer| {
                time_inner(writer, attempt.time(), buf)?;

                if let Some(pause_time) = attempt.pause_time() {
                    time_span(writer, new_tag(b"PauseTime"), pause_time, buf)?;
                }

                Ok(())
            })
        },
    )?;

    scoped_iter(
        writer,
        new_tag(b"Segments"),
        run.segments(),
        |writer, segment| {
            write_start(writer, new_tag(b"Segment"))?;

            text(writer, new_tag(b"Name"), segment.name())?;
            image(writer, new_tag(b"Icon"), segment.icon(), buf, image_buf)?;

            scoped_iter(
                writer,
                new_tag(b"SplitTimes"),
                run.custom_comparisons(),
                |writer, comparison| {
                    let mut tag = new_tag(b"SplitTime");
                    tag.push_attribute((&b"name"[..], comparison.as_bytes()));
                    time(writer, tag, segment.comparison(comparison), buf)
                },
            )?;

            time(
                writer,
                new_tag(b"BestSegmentTime"),
                segment.best_segment_time(),
                buf,
            )?;

            scoped_iter(
                writer,
                new_tag(b"SegmentHistory"),
                segment.segment_history(),
                |writer, &(index, history_time)| {
                    let mut tag = new_tag(b"Time");
                    tag.push_attribute((&b"id"[..], fmt_buf(index, buf)));
                    time(writer, tag, history_time, buf)
                },
            )?;

            write_end(writer, b"Segment")
        },
    )?;

    scoped(
        writer,
        new_tag(b"AutoSplitterSettings"),
        run.auto_splitter_settings().is_empty(),
        |writer| {
            writer.write(run.auto_splitter_settings())?;
            Ok(())
        },
    )?;

    write_end(writer, b"Run")?;
    Ok(())
}