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
//! Library for manipulating Game Maker Studio's "data.win" (GEN8) data files.

#![warn( /*missing_docs,*/
 trivial_casts, trivial_numeric_casts)]

extern crate byteorder;

mod serde;
mod io_util;

use std::io::{self, BufReader, BufWriter};
use std::path;
use std::error::Error;
use std::fs::File;

/// The data of a Game Maker Studio game.
///
/// This is the collective information acquired from "data.win".
pub struct GameData {
    pub gen8: Box<[u8]>,
    pub optn: Box<[u8]>,
    pub extn: Box<[u8]>,
    pub sond: Box<[u8]>,
    pub agrp: Option<Box<[u8]>>,
    pub sprt: Box<[u8]>,
    pub bgnd: Box<[u8]>,
    pub path: Box<[u8]>,
    pub scpt: Box<[u8]>,
    pub shdr: Box<[u8]>,
    pub font: Box<[u8]>,
    pub tmln: Box<[u8]>,
    pub objt: Box<[u8]>,
    pub room: Box<[u8]>,
    pub dafl: Box<[u8]>,
    pub tpag: Box<[u8]>,
    pub code: Box<[u8]>,
    pub vari: Box<[u8]>,
    pub func: Box<[u8]>,
    pub strg: Box<[u8]>,
    pub txtr: Txtr,
    pub audo: Box<[u8]>,
    pub lang: Option<Box<[u8]>>,
    pub glob: Option<Box<[u8]>>,
    reader: FileBufRead,
}

pub struct Texture {
    unknown: u32,
    source: TextureSource,
}

pub enum TextureSource {
    Original { offset: u64 },
}

pub struct Txtr {
    textures: Vec<Texture>,
    end_offset: u64,
}

type FileBufRead = BufReader<File>;
type FileBufWrite = BufWriter<File>;

impl GameData {
    /// Reads a GameData from a file.
    pub fn open<P: AsRef<path::Path>>(path: P) -> Result<GameData, Box<Error>> {
        let file = File::open(path)?;
        serde::open_and_read(BufReader::new(file))
    }
    /// Writes self to a file.
    pub fn save_to_file<P: AsRef<path::Path>>(&mut self, path: P) -> io::Result<()> {
        let file = File::create(path)?;
        serde::write_to(self, &mut BufWriter::new(file))
    }
}