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
use crate::shortcut::Shortcut;

use ascii::AsciiChar::*;

/// Serializes shortcuts to bytes, in a format that Steam will accept.
///
/// ### Examples
/// ```
/// use steam_shortcuts_util::parse_shortcuts;
/// use steam_shortcuts_util::shortcuts_to_bytes;
///
/// fn example() -> Result<(), Box<dyn std::error::Error>> {
///     // This path should be to your steams shortcuts file
///     // Usually located at $SteamDirectory/userdata/$SteamUserId/config/shortcuts.vdf
///     let content = std::fs::read("src/testdata/shortcuts.vdf")?;
///     let shortcuts = parse_shortcuts(content.as_slice())?;
///     let shortcut_bytes_vec = shortcuts_to_bytes(&shortcuts);
///     assert_eq!(shortcut_bytes_vec, content);
///     Ok(())
/// }
/// ```
pub fn shortcuts_to_bytes(shortcut: &Vec<Shortcut>) -> Vec<u8> {
    let null = Null.as_byte();

    let bs = BackSpace.as_byte();
    let mut result = vec![];

    result.push(null);
    result.extend_from_slice("shortcuts".as_bytes());
    result.push(null);

    let mut shortcut_bytes: Vec<u8> = shortcut
        .iter()
        .enumerate()
        .flat_map(|(index, shortcut)| shortcut_to_bytes(index, shortcut))
        .collect();

    result.append(&mut shortcut_bytes);

    result.push(bs);
    result.push(bs);

    result
}

fn shortcut_to_bytes(order: usize, shortcut: &Shortcut) -> Vec<u8> {
    let null = Null.as_byte();
    let bs = BackSpace.as_byte();

    let mut res = vec![];

    res.push(null);

    let order_string = format!("{}", order);
    let order = order_string.as_bytes();
    res.extend_from_slice(order);
    res.push(null);

    res.append(&mut stx_to_bytes("appid", shortcut.app_id));
    res.append(&mut soh_to_bytes("AppName", shortcut.app_name));
    res.append(&mut soh_to_bytes("Exe", shortcut.exe));
    res.append(&mut soh_to_bytes("StartDir", shortcut.start_dir));
    res.append(&mut soh_to_bytes("icon", shortcut.icon));
    res.append(&mut soh_to_bytes("ShortcutPath", shortcut.shortcut_path));
    res.append(&mut soh_to_bytes("LaunchOptions", shortcut.launch_options));
    res.append(&mut stx_to_bytes("IsHidden", shortcut.is_hidden as u32));
    res.append(&mut stx_single_to_bytes(
        "AllowDesktopConfig",
        shortcut.allow_desktop_config,
    ));
    res.append(&mut stx_single_to_bytes(
        "AllowOverlay",
        shortcut.allow_overlay,
    ));
    res.append(&mut stx_to_bytes("openvr", shortcut.open_vr));
    res.append(&mut stx_to_bytes("Devkit", shortcut.dev_kit));
    res.append(&mut soh_to_bytes("DevkitGameID", shortcut.dev_kit_game_id));
    res.append(&mut stx_to_bytes(
        "DevkitOverrideAppID",
        shortcut.dev_kit_overrite_app_id,
    ));

    res.append(&mut stx_to_bytes("LastPlayTime", shortcut.last_play_time));

    res.push(null);
    res.extend_from_slice("tags".as_bytes());
    res.push(null);

    res.append(&mut tags_to_bytes(&shortcut.tags));

    res.push(bs);
    res.push(bs);

    res
}

fn tags_to_bytes(input: &Vec<&str>) -> Vec<u8> {
    input
        .iter()
        .enumerate()
        .flat_map(|(index, tag)| tag_to_bytes(index, tag))
        .collect()
}

fn tag_to_bytes(index: usize, tag: &str) -> Vec<u8> {
    let soh = SOH.as_byte();
    let null = Null.as_byte();

    let mut res = vec![];
    res.push(soh);
    let order_string = format!("{}", index);
    let order = order_string.as_bytes();
    res.extend_from_slice(order);
    res.push(null);
    res.extend_from_slice(tag.as_bytes());
    res.push(null);
    res
}

fn soh_to_bytes(name: &str, input: &str) -> Vec<u8> {
    let mut res = vec![];
    let soh = SOH.as_byte();
    let null = Null.as_byte();
    res.push(soh);

    res.extend_from_slice(name.as_bytes());

    res.push(null);
    res.extend_from_slice(&input.as_bytes());
    res.push(null);
    res
}

fn stx_single_to_bytes(name: &str, input: bool) -> Vec<u8> {
    let mut res = vec![];
    let soh = SOH.as_byte();
    let stx = SOX.as_byte();
    let null = Null.as_byte();
    res.push(stx);

    res.extend_from_slice(name.as_bytes());
    res.push(null);
    res.push(soh);
    res.push(null);
    res.push(null);
    res.push(input as u8);
    res
}

fn stx_to_bytes(name: &str, input: u32) -> Vec<u8> {
    let mut res = vec![];
    let stx = SOX.as_byte();
    let null = Null.as_byte();
    res.push(stx);

    res.extend_from_slice(name.as_bytes());

    res.push(null);
    res.extend_from_slice(&input.to_le_bytes());
    res
}
#[cfg(test)]
mod tests {
    use crate::{shortcuts_parser, shortcuts_to_bytes, Shortcut};

    // #[test]
    // fn parse_back_and_forth() {
    //     let content = std::fs::read("src/testdata/shortcuts.vdf").unwrap();
    //     let shortcuts = shortcuts_parser::parse_shortcuts(content.as_slice()).unwrap();
    //     let shortcut_bytes_vec = shortcuts_to_bytes(&shortcuts);
    //     assert_eq!(shortcut_bytes_vec, content);
    // }

    // #[test]
    // fn parse_back_and_forth_linux() {
    //     let content = std::fs::read("src/testdata/linux_shortcut.vdf").unwrap();
    //     let shortcuts = shortcuts_parser::parse_shortcuts(content.as_slice()).unwrap();
    //     let shortcut_bytes_vec = shortcuts_to_bytes(&shortcuts);
    //     assert_eq!(shortcut_bytes_vec, content);
    // }

    #[test]
    fn write_same_as_steam() {
        let expected = std::fs::read("src/testdata/outerwilds_manual.vdf").unwrap();
        let shorcut = Shortcut::new(
            0,
            "OuterWilds",
            "\"D:\\Epic\\OuterWilds\\OuterWilds.exe\"",
            "\"D:\\Epic\\OuterWilds\\\"",
            "",
            "",
            "",
        );
        let shortcuts = vec![shorcut];
        let shortcut_bytes_vec = shortcuts_to_bytes(&shortcuts);
        assert_eq!(shortcut_bytes_vec, expected);
    }
}