ltk_meta 0.9.1

Metadata formats and utilities for League Toolkit
Documentation
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! `bin-streaming.md` section 10.3, over synthetic bins.

use std::io;

use ltk_hash::BinHash;

use crate::{property::values, Bin, BinDelta, BinObject, Error, PropertyValueEnum};

type BinStream = crate::BinStream<io::Cursor<Vec<u8>>>;

const A: u32 = 0x1111_0001;
const B: u32 = 0x1111_0002;
const C: u32 = 0x1111_0003;
const D: u32 = 0x1111_0004;

const F_NUMBER: u32 = 0x0001;
const F_NAME: u32 = 0x0002;
const F_NODE: u32 = 0x0003;

/// Three objects with a dependency and a nested node, in file order `A`, `B`, `C`.
fn base() -> Bin {
    Bin::builder()
        .dependency("common.bin")
        .object(
            BinObject::builder(A, 0xAAAA_0001u32)
                .property(F_NUMBER, values::I32::new(42))
                .property(F_NAME, values::String::from("hello"))
                .build(),
        )
        .object(
            BinObject::builder(B, 0xAAAA_0002u32)
                .property(
                    F_NODE,
                    values::Struct {
                        class_hash: 0xAAAA_00F0u32.into(),
                        properties: [(BinHash(F_NUMBER), values::U8::new(7).into())]
                            .into_iter()
                            .collect(),
                    },
                )
                .build(),
        )
        .object(
            BinObject::builder(C, 0xAAAA_0003u32)
                .property(F_NAME, values::String::from("world"))
                .build(),
        )
        .build()
}

fn bytes_of(bin: &Bin) -> Vec<u8> {
    let mut cursor = io::Cursor::new(Vec::new());
    bin.to_writer(&mut cursor).expect("the bin writes");
    cursor.into_inner()
}

/// `bytes`, a version-3 file, rewritten as `version`. Version 1 has no dependency list, and the
/// bytes must hold none.
fn at_version(mut bytes: Vec<u8>, version: u32) -> Vec<u8> {
    bytes[4..8].copy_from_slice(&version.to_le_bytes());
    if version == 1 {
        assert_eq!(
            &bytes[8..12],
            &[0; 4],
            "a version-1 file has no dependencies"
        );
        bytes.drain(8..12);
    }
    bytes
}

fn mount(bytes: &[u8]) -> BinStream {
    BinStream::mount(io::Cursor::new(bytes.to_vec())).expect("the stream mounts")
}

fn write(bytes: &[u8], delta: &BinDelta) -> Result<Vec<u8>, Error> {
    let mut out = Vec::new();
    mount(bytes).write_patched(delta, &mut out)?;
    Ok(out)
}

fn read(bytes: &[u8]) -> Bin {
    Bin::from_reader(&mut io::Cursor::new(bytes)).expect("the written bin reads")
}

/// The raw bytes of every object, by path hash.
fn object_bytes(bytes: &[u8]) -> Vec<(BinHash, Vec<u8>)> {
    let mut stream = mount(bytes);
    stream
        .toc()
        .expect("the TOC builds")
        .entries()
        .iter()
        .map(|entry| {
            let range = entry.byte_range();
            (
                entry.path_hash,
                bytes[range.start as usize..range.end as usize].to_vec(),
            )
        })
        .collect()
}

#[test]
fn an_empty_delta_writes_current_format_at_every_input_version() {
    let without_dependencies = Bin::new(base().objects.into_values(), std::iter::empty::<&str>());
    for (version, bin) in [(1, &without_dependencies), (2, &base()), (3, &base())] {
        let bytes = at_version(bytes_of(bin), version);
        let delta = BinDelta::new();
        assert!(delta.is_empty());
        assert_eq!(
            write(&bytes, &delta).unwrap(),
            bytes_of(bin),
            "version {version}"
        );
    }
}

#[test]
fn a_one_property_edit_rereads_as_the_eager_edit_and_keeps_every_other_object() {
    let bytes = bytes_of(&base());

    let mut stream = mount(&bytes);
    let mut object = stream
        .object(B)
        .unwrap()
        .expect("B is in the base")
        .read()
        .unwrap();
    let PropertyValueEnum::Struct(node) = object.properties.get_mut(&BinHash(F_NODE)).unwrap()
    else {
        panic!("F_NODE is a struct");
    };
    node.properties.insert(
        BinHash(F_NAME),
        values::String::from("a longer value").into(),
    );
    let mut delta = BinDelta::new();
    assert!(delta.replace(object.clone()).is_none());
    assert_eq!(delta.replacement(B), Some(&object));
    assert!(!delta.is_empty());

    let mut out = Vec::new();
    stream.write_patched(&delta, &mut out).unwrap();

    let mut expected = base();
    expected.objects.insert(BinHash(B), object);
    assert_eq!(read(&out), expected);

    let before = object_bytes(&bytes);
    let after = object_bytes(&out);
    assert_eq!(after.len(), 3);
    for (index, (hash, body)) in after.iter().enumerate() {
        assert_eq!(*hash, before[index].0, "file order");
        if hash.0 != B {
            assert_eq!(body, &before[index].1, "{hash:08x} was rewritten");
        } else {
            assert_ne!(body, &before[index].1, "the edit is in the output");
        }
    }
}

#[test]
fn edited_output_uses_the_current_version() {
    let without_dependencies = Bin::new(base().objects.into_values(), std::iter::empty::<&str>());
    for (version, bin) in [(1, &without_dependencies), (2, &base())] {
        let bytes = at_version(bytes_of(bin), version);
        let mut stream = mount(&bytes);
        let object = stream.object(A).unwrap().unwrap().read().unwrap();
        let mut delta = BinDelta::new();
        delta.replace(object);

        let out = write(&bytes, &delta).unwrap();
        assert_eq!(out[4..8], 3u32.to_le_bytes(), "version {version}");
        assert_eq!(
            out,
            bytes_of(bin),
            "current-format output matches the eager writer"
        );
    }
}

#[test]
fn a_dependency_list_over_a_version_one_base_writes_current_format() {
    let without_dependencies = Bin::new(base().objects.into_values(), std::iter::empty::<&str>());
    let bytes = at_version(bytes_of(&without_dependencies), 1);

    let mut delta = BinDelta::new();
    delta.set_dependencies(["common.bin", "other.bin"]);
    assert_eq!(
        delta.dependencies(),
        Some(&["common.bin".to_owned(), "other.bin".to_owned()][..])
    );
    let out = write(&bytes, &delta).unwrap();
    assert_eq!(out[4..8], 3u32.to_le_bytes());
    let written = read(&out);
    assert_eq!(written.version, 3);
    assert_eq!(written.dependencies, ["common.bin", "other.bin"]);
    assert_eq!(written.objects, without_dependencies.objects);

    let mut empty = BinDelta::new();
    empty.set_dependencies(std::iter::empty::<&str>());
    assert_eq!(
        write(&bytes, &empty).unwrap(),
        bytes_of(&without_dependencies),
        "an empty list also writes current format"
    );
}

#[test]
fn removing_replacing_and_appending_update_the_class_table_and_counts() {
    let bytes = bytes_of(&base());

    let replacement = BinObject::builder(A, 0xBBBB_0001u32)
        .property(F_NUMBER, values::F32::new(1.5))
        .build();
    let appended = BinObject::builder(D, 0xBBBB_0004u32)
        .property(F_NAME, values::String::from("new"))
        .build();
    let moved = BinObject::builder(C, 0xBBBB_0003u32).build();

    let mut delta = BinDelta::new();
    delta.replace(replacement.clone());
    assert_eq!(delta.remove(B), None);
    assert!(delta.is_removed(B));
    delta.remove(C);
    assert!(delta.append(appended.clone()).is_none());
    // An appended object may take the hash of a base object the delta removes.
    delta.append(moved.clone());
    delta.set_dependencies(["patched.bin"]);

    let out = write(&bytes, &delta).unwrap();

    let mut expected = base();
    expected.objects.insert(BinHash(A), replacement);
    expected.objects.shift_remove(&BinHash(B));
    expected.objects.shift_remove(&BinHash(C));
    expected.objects.insert(BinHash(D), appended);
    expected.objects.insert(BinHash(C), moved);
    expected.dependencies = vec!["patched.bin".to_owned()];

    let written = read(&out);
    assert_eq!(written, expected);
    let stream = mount(&out);
    assert_eq!(
        stream.class_hashes(),
        [
            BinHash(0xBBBB_0001),
            BinHash(0xBBBB_0004),
            BinHash(0xBBBB_0003)
        ]
    );
    assert_eq!(
        out,
        bytes_of(&expected),
        "the same bytes the eager writer writes"
    );
}

#[test]
fn the_last_call_for_a_hash_wins() {
    let object = |value: i32| {
        BinObject::builder(A, 0xAAAA_0001u32)
            .property(F_NUMBER, values::I32::new(value))
            .build()
    };
    let mut delta = BinDelta::new();
    delta.replace(object(1));
    assert_eq!(delta.replace(object(2)), Some(object(1)));
    assert_eq!(delta.remove(A), Some(object(2)));
    assert_eq!(delta.replacement(A), None);
    delta.replace(object(3));
    assert!(!delta.is_removed(A), "a replace cancels a removal");

    let appended = |value: i32| {
        BinObject::builder(D, 0xAAAA_0004u32)
            .property(F_NUMBER, values::I32::new(value))
            .build()
    };
    delta.append(appended(1));
    assert_eq!(delta.append(appended(2)), Some(appended(1)));
    assert_eq!(delta.appended().collect::<Vec<_>>(), [&appended(2)]);
}

#[test]
fn a_delta_that_names_another_base_writes_nothing() {
    let bytes = bytes_of(&base());
    let stray = BinObject::builder(D, 0xAAAA_0004u32).build();

    let mut replaced = BinDelta::new();
    replaced.replace(stray.clone());
    let mut removed = BinDelta::new();
    removed.remove(D);
    let mut duplicate = BinDelta::new();
    duplicate.append(BinObject::builder(B, 0xAAAA_0002u32).build());
    let mut duplicate_of_replaced = BinDelta::new();
    duplicate_of_replaced.replace(BinObject::builder(B, 0xAAAA_0002u32).build());
    duplicate_of_replaced.append(BinObject::builder(B, 0xAAAA_0002u32).build());

    for (delta, expected) in [
        (&replaced, "missing"),
        (&removed, "missing"),
        (&duplicate, "duplicate"),
        (&duplicate_of_replaced, "duplicate"),
    ] {
        let mut out = Vec::new();
        let error = mount(&bytes).write_patched(delta, &mut out).unwrap_err();
        match (expected, &error) {
            ("missing", Error::DeltaMissingObject(hash)) => assert_eq!(hash.0, D),
            ("duplicate", Error::DeltaDuplicateObject(hash)) => assert_eq!(hash.0, B),
            _ => panic!("{expected}: {error:?}"),
        }
        assert!(out.is_empty(), "{expected}: bytes were written");
    }
}

#[test]
fn a_legacy_latched_base_refuses_the_delta() {
    let object = BinObject::builder(A, 0xCCCC_0001u32)
        .property(F_NODE, values::Struct::default())
        .build();
    let mut bytes = bytes_of(&Bin::new([object], std::iter::empty::<&str>()));
    // `Struct` is 19 in the legacy numbering, which decodes as nothing in the current one.
    let modern: u8 = crate::PropertyKind::Struct.into();
    let at = bytes.iter().rposition(|&byte| byte == modern).unwrap();
    bytes[at] = 19;

    let mut stream = mount(&bytes);
    stream.object(A).unwrap().unwrap().read().unwrap();
    assert!(stream.numbering().is_legacy());

    let mut out = Vec::new();
    let error = stream
        .write_patched(&BinDelta::new(), &mut out)
        .unwrap_err();
    assert!(matches!(error, Error::DeltaLegacyNumbering), "{error:?}");
    assert!(error.to_string().contains("into_bin"), "{error}");
    assert!(out.is_empty());
}

#[test]
fn a_shipped_bin_rewrites_byte_for_byte_around_one_edit() {
    const UIBASE: &[u8] = include_bytes!("../../../tests/bins/lolminimap_uibase.bin");
    let bytes = UIBASE.to_vec();
    assert_eq!(write(&bytes, &BinDelta::new()).unwrap(), bytes);

    let mut stream = mount(&bytes);
    let entries = stream.toc().unwrap().entries().to_vec();
    let target = entries[entries.len() / 2];
    let mut object = stream
        .object(target.path_hash)
        .unwrap()
        .unwrap()
        .read()
        .unwrap();
    object
        .properties
        .insert(BinHash(0x0BAD_F00D), values::Bool::new(true).into());
    let mut delta = BinDelta::new();
    delta.replace(object.clone());
    let mut out = Vec::new();
    stream.write_patched(&delta, &mut out).unwrap();

    let mut expected = read(&bytes);
    expected.objects.insert(target.path_hash, object);
    assert_eq!(read(&out), expected);
    let before = object_bytes(&bytes);
    let after = object_bytes(&out);
    for ((hash, body), (_, original)) in after.iter().zip(&before) {
        if *hash != target.path_hash {
            assert_eq!(body, original, "{hash:08x}");
        }
    }
}

#[test]
fn unread_legacy_objects_refuse_output_before_any_bytes_are_written() {
    let legacy = BinObject::builder(B, 0xCCCC_0001u32)
        .property(F_NODE, values::Struct::default())
        .build();
    let prefix = BinObject::builder(A, 0xAAAA_0001u32)
        .property(F_NUMBER, values::I32::new(42))
        .build();
    let mut bytes = bytes_of(&Bin::new([prefix, legacy], std::iter::empty::<&str>()));
    let tag = mount(&bytes)
        .toc()
        .unwrap()
        .entry(BinHash(B))
        .unwrap()
        .offset as usize
        + 14;
    assert_eq!(bytes[tag], u8::from(crate::PropertyKind::Struct));
    bytes[tag] = 19;

    let mut appended = BinDelta::new();
    appended.append(
        BinObject::builder(D, 0xDDDD_0001u32)
            .property(F_NODE, values::Struct::default())
            .build(),
    );
    let mut replaced = appended.clone();
    replaced.replace(BinObject::builder(B, 0xCCCC_0001u32).build());
    let mut removed = appended.clone();
    removed.remove(B);
    for delta in [BinDelta::new(), appended, replaced, removed] {
        let mut stream = mount(&bytes);
        stream.object(A).unwrap().unwrap().read().unwrap();
        assert!(!stream.numbering().is_legacy());
        let mut out = Vec::new();
        assert!(matches!(
            stream.write_patched(&delta, &mut out),
            Err(Error::DeltaLegacyNumbering)
        ));
        assert!(out.is_empty());
    }
}

#[test]
fn malformed_untouched_objects_refuse_output_before_any_bytes_are_written() {
    let mut bytes = bytes_of(&base());
    let entry = mount(&bytes).toc().unwrap().entries()[0];
    let count = entry.offset as usize + 8;
    bytes[count..count + 2].copy_from_slice(&0u16.to_le_bytes());
    let mut out = Vec::new();
    assert!(matches!(
        mount(&bytes).write_patched(&BinDelta::new(), &mut out),
        Err(Error::InvalidSize(..))
    ));
    assert!(out.is_empty());
}