physis/
gearsets.rs

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
// SPDX-FileCopyrightText: 2023 Joshua Goins <josh@redstrate.com>
// SPDX-License-Identifier: GPL-3.0-or-later

use crate::ByteBuffer;
use crate::dat::DatHeader;
use crate::equipment::Slot;
use binrw::NullString;
use binrw::binrw;
use binrw::{BinRead, BinWrite};
use std::collections::HashMap;
use std::io::BufWriter;
use std::io::Cursor;
use std::io::Read;

// FIXME: unclear what this is
const UNKNOWN_FLAG: u32 = 1_000_000;

fn convert_from_gear_id(id: u32) -> u32 {
    id & !UNKNOWN_FLAG
}

fn convert_to_gear_id(id: &u32) -> u32 {
    id | UNKNOWN_FLAG
}

fn convert_to_string(s: NullString) -> String {
    s.to_string()
}

fn convert_from_string(s: &String) -> NullString {
    NullString::from(s.as_str())
}

const NUMBER_OF_GEARSETS: usize = 100;

fn convert_from_gearsets(gearsets: [GearSet; NUMBER_OF_GEARSETS]) -> Vec<Option<GearSet>> {
    gearsets
        .iter()
        .cloned()
        .map(|x| if !x.name.is_empty() { Some(x) } else { None })
        .collect()
}

fn convert_to_gearsets(gearsets: &Vec<Option<GearSet>>) -> Vec<GearSet> {
    let mut result = vec![GearSet::default(); NUMBER_OF_GEARSETS];
    for (i, gearset) in gearsets.iter().enumerate() {
        if i >= NUMBER_OF_GEARSETS {
            break;
        }
        if let Some(gearset) = gearset {
            result[i] = gearset.clone();
        }
    }
    result
}

const NUMBER_OF_GEARSLOTS: usize = 14;

fn convert_from_slots(slots: [GearSlot; NUMBER_OF_GEARSLOTS]) -> HashMap<GearSlotType, GearSlot> {
    slots
        .iter()
        .cloned()
        .enumerate()
        .filter_map(|(i, x)| match x.id {
            0 => None,
            _ => Some((i.try_into().ok()?, x)),
        })
        .collect()
}

fn convert_to_slots(slots: &HashMap<GearSlotType, GearSlot>) -> Vec<GearSlot> {
    let mut result = vec![GearSlot::default(); NUMBER_OF_GEARSLOTS];
    for (idx, slot) in slots.iter() {
        result[idx.clone() as usize] = slot.clone();
    }
    result
}

fn convert_id_opt(id: u32) -> Option<u32> {
    if id == 0 { None } else { Some(id) }
}

fn convert_opt_id(id: &Option<u32>) -> u32 {
    id.unwrap_or(0)
}

#[binrw]
#[derive(Debug, Clone, Default)]
pub struct GearSlot {
    #[br(map = convert_from_gear_id)]
    #[bw(map = convert_to_gear_id)]
    /// The ID of the item.
    pub id: u32,
    #[br(map = convert_id_opt)]
    #[bw(map = convert_opt_id)]
    /// The ID of the item used as glamour.
    pub glamour_id: Option<u32>,
    // FIXME: one of those is most likely dyes, no idea about the rest
    unknown1: u32,
    unknown2: u32,
    unknown3: u32,
    unknown4: u32,
    unknown5: u32,
}

#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub enum GearSlotType {
    MainHand = 0,
    SecondaryHand,
    Head,
    Body,
    Hands,
    Waist, // legacy
    Legs,
    Feet,
    Bracelets,
    Necklace,
    Earrings,
    Ring1,
    Ring2,
    Soul,
}

impl GearSlotType {
    pub fn to_slot(&self) -> Option<Slot> {
        match self {
            GearSlotType::Head => Some(Slot::Head),
            GearSlotType::Body => Some(Slot::Body),
            GearSlotType::Hands => Some(Slot::Hands),
            GearSlotType::Legs => Some(Slot::Legs),
            GearSlotType::Feet => Some(Slot::Feet),
            GearSlotType::Bracelets => Some(Slot::Wrists),
            GearSlotType::Necklace => Some(Slot::Neck),
            GearSlotType::Earrings => Some(Slot::Earring),
            GearSlotType::Ring1 => Some(Slot::RingLeft),
            GearSlotType::Ring2 => Some(Slot::RingRight),
            _ => None,
        }
    }
}

impl TryFrom<Slot> for GearSlotType {
    type Error = ();

    fn try_from(v: Slot) -> Result<Self, Self::Error> {
        match v {
            Slot::Head => Ok(GearSlotType::Head),
            Slot::Body => Ok(GearSlotType::Body),
            Slot::Hands => Ok(GearSlotType::Hands),
            Slot::Legs => Ok(GearSlotType::Legs),
            Slot::Feet => Ok(GearSlotType::Feet),
            Slot::Wrists => Ok(GearSlotType::Bracelets),
            Slot::Neck => Ok(GearSlotType::Necklace),
            Slot::Earring => Ok(GearSlotType::Earrings),
            Slot::RingLeft => Ok(GearSlotType::Ring1),
            Slot::RingRight => Ok(GearSlotType::Ring2),
        }
    }
}

impl TryFrom<usize> for GearSlotType {
    type Error = ();

    fn try_from(v: usize) -> Result<Self, Self::Error> {
        match v {
            x if x == GearSlotType::MainHand as usize => Ok(GearSlotType::MainHand),
            x if x == GearSlotType::SecondaryHand as usize => Ok(GearSlotType::SecondaryHand),
            x if x == GearSlotType::Head as usize => Ok(GearSlotType::Head),
            x if x == GearSlotType::Body as usize => Ok(GearSlotType::Body),
            x if x == GearSlotType::Hands as usize => Ok(GearSlotType::Hands),
            x if x == GearSlotType::Waist as usize => Ok(GearSlotType::Waist),
            x if x == GearSlotType::Legs as usize => Ok(GearSlotType::Legs),
            x if x == GearSlotType::Feet as usize => Ok(GearSlotType::Feet),
            x if x == GearSlotType::Bracelets as usize => Ok(GearSlotType::Bracelets),
            x if x == GearSlotType::Necklace as usize => Ok(GearSlotType::Necklace),
            x if x == GearSlotType::Earrings as usize => Ok(GearSlotType::Earrings),
            x if x == GearSlotType::Ring1 as usize => Ok(GearSlotType::Ring1),
            x if x == GearSlotType::Ring2 as usize => Ok(GearSlotType::Ring2),
            x if x == GearSlotType::Soul as usize => Ok(GearSlotType::Soul),
            _ => Err(()),
        }
    }
}

#[binrw]
#[derive(Debug, Clone, Default)]
pub struct GearSet {
    /// The index of the gear set in the list of gear sets.
    pub index: u8,
    #[brw(pad_size_to = 47)]
    #[br(map = convert_to_string)]
    #[bw(map = convert_from_string)]
    /// The name of the gear set.
    pub name: String,
    // FIXME: no clue what this is
    unknown1: u64,
    #[br(map = convert_from_slots)]
    #[bw(map = convert_to_slots)]
    /// The slots of the gear set.
    pub slots: HashMap<GearSlotType, GearSlot>,
    #[br(map = convert_id_opt)]
    #[bw(map = convert_opt_id)]
    /// The ID of the facewear item.
    pub facewear: Option<u32>,
}

#[binrw]
#[br(little)]
#[derive(Debug, Clone)]
pub struct GearSets {
    // FIXME: can't be a version because it's always 0
    unknown1: u8,
    /// The index of the current active geat set.
    pub current_gearset: u8,
    // FIXME: no clue what this is
    unknown3: u16,
    #[br(map = convert_from_gearsets)]
    #[bw(map = convert_to_gearsets)]
    /// The list of gear sets.
    pub gearsets: Vec<Option<GearSet>>,
}

const GEARSET_KEY: u8 = 0x73;

impl GearSets {
    /// Parses existing gearsets data.
    pub fn from_existing(buffer: &[u8]) -> Option<GearSets> {
        let mut cursor = Cursor::new(buffer);

        let header = DatHeader::read(&mut cursor).ok()?;

        let mut buffer = vec![0; header.content_size as usize - 1];
        cursor.read_exact(&mut buffer).ok()?;

        let decoded = buffer.iter().map(|x| *x ^ GEARSET_KEY).collect::<Vec<_>>();
        let mut cursor = Cursor::new(decoded);

        GearSets::read(&mut cursor).ok()
    }

    /// Write existing gearsets data to a buffer.
    pub fn write_to_buffer(&self) -> Option<ByteBuffer> {
        let mut buffer = ByteBuffer::new();

        // header
        {
            let mut cursor = Cursor::new(&mut buffer);

            let header = DatHeader {
                file_type: crate::dat::DatFileType::Gearset,
                max_size: 45205,
                content_size: 45205,
            };
            header.write_le(&mut cursor).ok()?
        }

        // buffer contents encoded
        {
            let mut cursor = Cursor::new(ByteBuffer::new());
            self.write_le(&mut cursor).ok()?;

            buffer.extend_from_slice(
                &cursor
                    .into_inner()
                    .iter()
                    .map(|x| *x ^ GEARSET_KEY)
                    .collect::<Vec<_>>(),
            );
        }

        Some(buffer)
    }
}

#[cfg(test)]
mod tests {
    use std::fs::read;
    use std::path::PathBuf;

    use super::*;

    #[test]
    fn test_invalid() {
        let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        d.push("resources/tests");
        d.push("random");

        // Feeding it invalid data should not panic
        GearSets::from_existing(&read(d).unwrap());
    }

    fn common_setup(name: &str) -> GearSets {
        let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        d.push("resources/tests/gearsets");
        d.push(name);

        GearSets::from_existing(&read(&d).unwrap()).unwrap()
    }

    #[test]
    fn read_simple() {
        let gearsets = common_setup("simple.dat");

        assert_eq!(gearsets.current_gearset, 0);
        let gearset = gearsets.gearsets[0].as_ref().unwrap();
        for i in 1..gearsets.gearsets.len() {
            assert!(gearsets.gearsets[i].is_none());
        }

        assert_eq!(gearset.index, 0);
        assert_eq!(gearset.name, "White Mage");
        assert!(gearset.facewear.is_none());
        assert_eq!(gearset.slots.len(), 2);
        let slot = gearset.slots.get(&GearSlotType::MainHand).unwrap();
        assert_eq!(slot.id, 5269);
        assert_eq!(slot.glamour_id, Some(2453));
        let slot = gearset.slots.get(&GearSlotType::Body).unwrap();
        assert_eq!(slot.id, 8395913);
        assert_eq!(slot.glamour_id, None);
    }

    #[test]
    fn write_simple() {
        let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        d.push("resources/tests/gearsets");
        d.push("simple.dat");

        let gearset_bytes = &read(d).unwrap();
        let gearset = GearSets::from_existing(gearset_bytes).unwrap();
        assert_eq!(*gearset_bytes, gearset.write_to_buffer().unwrap());
    }
}