rs2-cache 0.3.0

A cache library for RS2, based on OpenRS2.
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
use osrs_bytes::ReadExt;
use std::{
    collections::{BTreeMap, HashMap},
    io::Read,
};
use thiserror::Error;

#[allow(dead_code)]
pub enum Js5Protocol {
    Original = 5,
    Versioned = 6,
    Smart = 7,
}

enum Js5IndexFlags {
    Names = 0x1,
    Digests = 0x2,
    Lengths = 0x4,
    UncompressedChecksums = 0x8,
}

#[derive(Debug, PartialEq)]
pub struct Js5IndexFile {
    pub name_hash: i32,
}

#[derive(Debug, PartialEq)]
pub struct Js5IndexEntry {
    pub name_hash: i32,
    pub version: u32,
    pub checksum: u32,
    pub uncompressed_checksum: u32,
    pub length: u32,
    pub uncompressed_length: u32,
    pub digest: Vec<u8>,
    pub capacity: u32,
    pub files: BTreeMap<u32, Js5IndexFile>,
}

#[derive(Error, Debug)]
pub enum Js5IndexError {
    #[error("io error: {0}")]
    Io(#[from] std::io::Error),
    #[error("failed getting named hash table entry")]
    NamedHashTableEntry,
}

#[derive(Debug, PartialEq)]
pub struct Js5Index {
    pub protocol: u8,
    pub version: i32,
    pub has_names: bool,
    pub has_digests: bool,
    pub has_lengths: bool,
    pub has_uncompressed_checksums: bool,
    pub groups: BTreeMap<u32, Js5IndexEntry>,
    pub name_hash_table: HashMap<u32, u32>,
}

impl Js5Index {
    pub fn read<T: AsRef<[u8]>>(buf: T) -> Result<Js5Index, Js5IndexError> {
        let mut buf_ref = buf.as_ref();

        let protocol = buf_ref.read_u8()?;

        let read_func = if protocol >= Js5Protocol::Smart as u8 {
            |v: &mut &[u8]| -> Result<u32, Js5IndexError> { Ok(v.read_u32_smart()?) }
        } else {
            |v: &mut &[u8]| -> Result<u32, Js5IndexError> { Ok(v.read_u16()? as u32) }
        };

        let version = if protocol >= Js5Protocol::Versioned as u8 {
            buf_ref.read_i32()?
        } else {
            0
        };
        let flags = buf_ref.read_u8()?;
        let size = read_func(&mut buf_ref)?;

        // Create Js5Index
        let mut index = Js5Index {
            protocol,
            version,
            has_names: (flags & Js5IndexFlags::Names as u8) != 0,
            has_digests: (flags & Js5IndexFlags::Digests as u8) != 0,
            has_lengths: (flags & Js5IndexFlags::Lengths as u8) != 0,
            has_uncompressed_checksums: (flags & Js5IndexFlags::UncompressedChecksums as u8) != 0,
            groups: BTreeMap::new(),
            name_hash_table: HashMap::new(),
        };

        // Begin creating the groups
        let mut prev_group_id = 0;
        for _ in 0..size {
            prev_group_id += read_func(&mut buf_ref)?;
            index.groups.insert(
                prev_group_id,
                Js5IndexEntry {
                    name_hash: -1,
                    version: 0,
                    checksum: 0,
                    uncompressed_checksum: 0,
                    length: 0,
                    uncompressed_length: 0,
                    digest: Vec::new(),
                    capacity: 0,
                    files: BTreeMap::new(),
                },
            );
        }

        if index.has_names {
            for (id, group) in index.groups.iter_mut() {
                group.name_hash = buf_ref.read_i32()?;
                index.name_hash_table.insert(group.name_hash as u32, *id);
            }
        }

        for group in index.groups.values_mut() {
            group.checksum = buf_ref.read_u32()?;
        }

        if index.has_uncompressed_checksums {
            for group in index.groups.values_mut() {
                group.uncompressed_checksum = buf_ref.read_u32()?;
            }
        }

        if index.has_digests {
            for group in index.groups.values_mut() {
                let digest_bits = 512;
                let digest_bytes = digest_bits >> 3;
                let mut digest = vec![0; digest_bytes];
                buf_ref.read_exact(&mut digest)?;
                group.digest.extend(&digest);
            }
        }

        if index.has_lengths {
            for group in index.groups.values_mut() {
                group.length = buf_ref.read_u32()?;
                group.uncompressed_length = buf_ref.read_u32()?;
            }
        }

        for group in index.groups.values_mut() {
            group.version = buf_ref.read_u32()?;
        }

        let mut group_sizes = Vec::new();
        for _ in 0..size {
            group_sizes.push(read_func(&mut buf_ref)?);
        }

        for (i, group) in index.groups.values_mut().enumerate() {
            let group_size = group_sizes[i];

            let mut prev_file_id = 0;
            for _ in 0..group_size {
                prev_file_id += read_func(&mut buf_ref)?;
                group
                    .files
                    .insert(prev_file_id, Js5IndexFile { name_hash: -1 });
            }
        }

        if index.has_names {
            for group in index.groups.values_mut() {
                for file in group.files.values_mut() {
                    file.name_hash = buf_ref.read_i32()?;
                }
            }
        }

        Ok(index)
    }

    pub fn get_named(&self, name_hash: u32) -> Result<u32, Js5IndexError> {
        self.name_hash_table
            .get(&name_hash)
            .ok_or(Js5IndexError::NamedHashTableEntry)
            .copied()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::djb2::djb2_hash;
    use memmap2::Mmap;
    use std::{fs::File, path::Path};

    #[test]
    fn test_read_empty() {
        read("empty.dat", |data| {
            let index = Js5Index::read(data).unwrap();

            let empty_index = Js5Index {
                protocol: Js5Protocol::Original as u8,
                version: 0,
                has_names: false,
                has_digests: false,
                has_lengths: false,
                has_uncompressed_checksums: false,
                groups: BTreeMap::new(),
                name_hash_table: HashMap::new(),
            };

            assert_eq!(empty_index, index);
        });
    }

    #[test]
    fn test_read_versioned() {
        read("versioned.dat", |data| {
            let index = Js5Index::read(data).unwrap();

            let versioned_index = Js5Index {
                protocol: Js5Protocol::Versioned as u8,
                version: 0x12345678,
                has_names: false,
                has_digests: false,
                has_lengths: false,
                has_uncompressed_checksums: false,
                groups: BTreeMap::new(),
                name_hash_table: HashMap::new(),
            };

            assert_eq!(versioned_index, index);
        });
    }

    #[test]
    fn test_read_no_flags() {
        read("no-flags.dat", |data| {
            let index = Js5Index::read(data).unwrap();

            let files_1 = {
                let mut files = BTreeMap::new();
                files.insert(0, Js5IndexFile { name_hash: -1 });
                files
            };
            let files_2 = {
                let mut files = BTreeMap::new();
                files.insert(1, Js5IndexFile { name_hash: -1 });
                files.insert(3, Js5IndexFile { name_hash: -1 });
                files
            };
            let mut groups = BTreeMap::new();
            groups.insert(
                0,
                Js5IndexEntry {
                    name_hash: -1,
                    version: 0,
                    checksum: 0x01234567,
                    uncompressed_checksum: 0,
                    length: 0,
                    uncompressed_length: 0,
                    digest: Vec::new(),
                    capacity: 0,
                    files: files_1,
                },
            );
            groups.insert(
                1,
                Js5IndexEntry {
                    name_hash: -1,
                    version: 10,
                    checksum: 0x89ABCDEF,
                    uncompressed_checksum: 0,
                    length: 0,
                    uncompressed_length: 0,
                    digest: Vec::new(),
                    capacity: 0,
                    files: BTreeMap::new(),
                },
            );
            groups.insert(
                3,
                Js5IndexEntry {
                    name_hash: -1,
                    version: 20,
                    checksum: 0xAAAA5555,
                    uncompressed_checksum: 0,
                    length: 0,
                    uncompressed_length: 0,
                    digest: Vec::new(),
                    capacity: 0,
                    files: files_2,
                },
            );

            let no_flags_index = Js5Index {
                protocol: Js5Protocol::Original as u8,
                version: 0,
                has_names: false,
                has_digests: false,
                has_lengths: false,
                has_uncompressed_checksums: false,
                groups,
                name_hash_table: HashMap::new(),
            };

            assert_eq!(no_flags_index, index);
        });
    }

    #[test]
    fn test_read_named() {
        read("named.dat", |data| {
            let index = Js5Index::read(data).unwrap();

            let files = {
                let mut files = BTreeMap::new();
                files.insert(
                    0,
                    Js5IndexFile {
                        name_hash: djb2_hash("world") as i32,
                    },
                );
                files
            };
            let mut groups = BTreeMap::new();
            groups.insert(
                0,
                Js5IndexEntry {
                    name_hash: djb2_hash("hello") as i32,
                    version: 0x89ABCDEF,
                    checksum: 0x01234567,
                    uncompressed_checksum: 0,
                    length: 0,
                    uncompressed_length: 0,
                    digest: Vec::new(),
                    capacity: 0,
                    files,
                },
            );
            let name_hash_table = {
                let mut table = HashMap::new();
                table.insert(djb2_hash("hello"), 0);
                table
            };

            let named_index = Js5Index {
                protocol: Js5Protocol::Original as u8,
                version: 0,
                has_names: true,
                has_digests: false,
                has_lengths: false,
                has_uncompressed_checksums: false,
                groups: groups,
                name_hash_table,
            };

            assert_eq!(named_index, index);
        });
    }

    #[test]
    fn test_read_smart() {
        read("smart.dat", |data| {
            let index = Js5Index::read(data).unwrap();

            let files = {
                let mut files = BTreeMap::new();
                files.insert(0, Js5IndexFile { name_hash: -1 });
                files.insert(100000, Js5IndexFile { name_hash: -1 });
                files
            };

            let mut groups = BTreeMap::new();
            groups.insert(
                0,
                Js5IndexEntry {
                    name_hash: -1,
                    version: 0x89ABCDEF,
                    checksum: 0x01234567,
                    uncompressed_checksum: 0,
                    length: 0,
                    uncompressed_length: 0,
                    digest: Vec::new(),
                    capacity: 0,
                    files,
                },
            );
            groups.insert(
                100000,
                Js5IndexEntry {
                    name_hash: -1,
                    version: 0x5555AAAA,
                    checksum: 0xAAAA5555,
                    uncompressed_checksum: 0,
                    length: 0,
                    uncompressed_length: 0,
                    digest: Vec::new(),
                    capacity: 0,
                    files: BTreeMap::new(),
                },
            );

            let smart_index = Js5Index {
                protocol: Js5Protocol::Smart as u8,
                version: 0,
                has_names: false,
                has_digests: false,
                has_lengths: false,
                has_uncompressed_checksums: false,
                groups,
                name_hash_table: HashMap::new(),
            };

            assert_eq!(smart_index, index);
        });
    }

    #[test]
    fn test_read_digest() {
        read("digest.dat", |data| {
            let index = Js5Index::read(data).unwrap();

            let mut groups = BTreeMap::new();
            groups.insert(
                0,
                Js5IndexEntry {
                    name_hash: -1,
                    version: 0x89ABCDEF,
                    checksum: 0x01234567,
                    uncompressed_checksum: 0,
                    length: 0,
                    uncompressed_length: 0,
                    digest: vec![
                        25, 250, 97, 215, 85, 34, 164, 102, 155, 68, 227, 156, 29, 46, 23, 38, 197,
                        48, 35, 33, 48, 212, 7, 248, 154, 254, 224, 150, 73, 151, 247, 167, 62,
                        131, 190, 105, 139, 40, 143, 235, 207, 136, 227, 224, 60, 79, 7, 87, 234,
                        137, 100, 229, 155, 99, 217, 55, 8, 177, 56, 204, 66, 166, 110, 179,
                    ],
                    capacity: 0,
                    files: BTreeMap::new(),
                },
            );
            let digest_index = Js5Index {
                protocol: Js5Protocol::Original as u8,
                version: 0,
                has_names: false,
                has_digests: true,
                has_lengths: false,
                has_uncompressed_checksums: false,
                groups,
                name_hash_table: HashMap::new(),
            };

            assert_eq!(digest_index, index);
        });
    }

    #[test]
    fn test_read_lengths() {
        read("lengths.dat", |data| {
            let index = Js5Index::read(data).unwrap();

            let mut groups = BTreeMap::new();
            groups.insert(
                0,
                Js5IndexEntry {
                    name_hash: -1,
                    version: 0x89ABCDEF,
                    checksum: 0x01234567,
                    uncompressed_checksum: 0,
                    length: 1000,
                    uncompressed_length: 2000,
                    digest: Vec::new(),
                    capacity: 0,
                    files: BTreeMap::new(),
                },
            );
            let lengths_index = Js5Index {
                protocol: Js5Protocol::Original as u8,
                version: 0,
                has_names: false,
                has_digests: false,
                has_lengths: true,
                has_uncompressed_checksums: false,
                groups,
                name_hash_table: HashMap::new(),
            };

            assert_eq!(lengths_index, index);
        });
    }

    #[test]
    fn test_read_uncompressed_checksum() {
        read("uncompressed-checksum.dat", |data| {
            let index = Js5Index::read(data).unwrap();

            let mut groups = BTreeMap::new();
            groups.insert(
                0,
                Js5IndexEntry {
                    name_hash: -1,
                    version: 0x89ABCDEF,
                    checksum: 0x01234567,
                    uncompressed_checksum: 0xAAAA5555,
                    length: 0,
                    uncompressed_length: 0,
                    digest: Vec::new(),
                    capacity: 0,
                    files: BTreeMap::new(),
                },
            );
            let uncompressed_checksum_index = Js5Index {
                protocol: Js5Protocol::Original as u8,
                version: 0,
                has_names: false,
                has_digests: false,
                has_lengths: false,
                has_uncompressed_checksums: true,
                groups,
                name_hash_table: HashMap::new(),
            };

            assert_eq!(uncompressed_checksum_index, index);
        });
    }

    #[test]
    fn test_read_all_flaags() {
        read("all-flags.dat", |data| {
            let index = Js5Index::read(data).unwrap();

            let files = {
                let mut files = BTreeMap::new();
                files.insert(
                    0,
                    Js5IndexFile {
                        name_hash: djb2_hash("world") as i32,
                    },
                );
                files
            };

            let name_hash_table = {
                let mut table = HashMap::new();
                table.insert(djb2_hash("hello"), 0);
                table
            };

            let mut groups = BTreeMap::new();
            groups.insert(
                0,
                Js5IndexEntry {
                    name_hash: djb2_hash("hello") as i32,
                    version: 0x89ABCDEF,
                    checksum: 0x01234567,
                    uncompressed_checksum: 0xAAAA5555,
                    length: 1000,
                    uncompressed_length: 2000,
                    digest: vec![
                        25, 250, 97, 215, 85, 34, 164, 102, 155, 68, 227, 156, 29, 46, 23, 38, 197,
                        48, 35, 33, 48, 212, 7, 248, 154, 254, 224, 150, 73, 151, 247, 167, 62,
                        131, 190, 105, 139, 40, 143, 235, 207, 136, 227, 224, 60, 79, 7, 87, 234,
                        137, 100, 229, 155, 99, 217, 55, 8, 177, 56, 204, 66, 166, 110, 179,
                    ],
                    capacity: 0,
                    files,
                },
            );
            let all_flags_index = Js5Index {
                protocol: Js5Protocol::Original as u8,
                version: 0,
                has_names: true,
                has_digests: true,
                has_lengths: true,
                has_uncompressed_checksums: true,
                groups,
                name_hash_table,
            };

            assert_eq!(all_flags_index, index);
        });
    }

    fn read<P, F>(p: P, f: F)
    where
        P: AsRef<Path>,
        F: FnOnce(Mmap),
    {
        f(
            unsafe { Mmap::map(&File::open(Path::new("tests/data/index").join(p)).unwrap()) }
                .unwrap(),
        )
    }
}