summaryrefslogtreecommitdiff
path: root/shared/src/respack.rs
blob: dee0cd065924e27554e47651d85c442e4e01d826 (plain)
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
/*
    wearechat - generic multiplayer game with voip
    Copyright (C) 2025 metamuffin

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, version 3 of the License only.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/
use crate::{packets::Resource, resources::RespackEntry, store::ResourceStore};
use anyhow::{Result, bail};
use log::{info, warn};
use std::{
    collections::HashMap,
    io::{Read, Seek, SeekFrom, Write},
    marker::PhantomData,
};

const MAGIC: &[u8; 16] = b"\x0f\x0cWEARE\x01RESPACK\x02";

pub fn save_respack(
    mut output: impl Write,
    store: &ResourceStore,
    resources: &[Resource],
    entry: Option<Resource<RespackEntry>>,
) -> Result<()> {
    output.write_all(MAGIC)?;
    output.write_all(&entry.map(|e| e.0).unwrap_or([0u8; 32]))?;
    output.write_all(&u64::to_be_bytes(resources.len() as u64))?;
    let mut off =
        (MAGIC.len() + 32 + size_of::<u64>() + (32 + size_of::<u64>() * 2) * resources.len())
            as u64;
    for r in resources {
        let size = store.get_raw_size(*r)?.unwrap() as u64;
        output.write_all(&r.0)?;
        output.write_all(&u64::to_be_bytes(off))?;
        output.write_all(&u64::to_be_bytes(size))?;
        off += size;
    }
    for r in resources {
        output.write_all(&store.get_raw(*r)?.unwrap())?;
    }
    Ok(())
}

/// Copies the entire respack to the store. This is usually a dumb idea because the pack supportes on-demand reading.
pub fn load_respack(
    input: impl Read + Seek,
    store: &ResourceStore,
) -> Result<Option<Resource<RespackEntry>>> {
    let mut pack = RespackReader::open(input)?;
    let mut load_queue = Vec::new();
    for res in pack.index.keys() {
        if store.get_raw_size(*res)?.is_none() {
            load_queue.push(*res);
        }
    }
    info!(
        "loading {} of {} resources from pack",
        load_queue.len(),
        pack.index.len()
    );
    for res in load_queue {
        let mut buf = Vec::new();
        pack.read(res)?.unwrap().read_to_end(&mut buf)?;
        let key = store.set_raw(&buf)?;
        if key != res {
            warn!("respack containes mislabeled resources")
        }
    }

    Ok(pack.entry)
}

pub struct RespackReader<T> {
    entry: Option<Resource<RespackEntry>>,
    index: HashMap<Resource, (u64, u64)>,
    inner: T,
}
impl<T: Read + Seek> RespackReader<T> {
    pub fn open(mut inner: T) -> Result<Self> {
        let mut magic = [0u8; MAGIC.len()];
        inner.read_exact(&mut magic)?;
        if magic != *MAGIC {
            bail!("wrong magic bytes");
        }
        let mut entry = [0u8; 32];
        inner.read_exact(&mut entry)?;
        let entry = if entry != [0u8; 32] {
            Some(Resource(entry, PhantomData))
        } else {
            None
        };

        let mut count = [0u8; size_of::<u64>()];
        inner.read_exact(&mut count)?;
        let count = u64::from_be_bytes(count);

        let mut index = HashMap::new();
        let mut found_entry = false;
        for _ in 0..count {
            let mut res = [0u8; 32];
            let mut off = [0u8; size_of::<u64>()];
            let mut size = [0u8; size_of::<u64>()];
            inner.read_exact(&mut res)?;
            inner.read_exact(&mut off)?;
            inner.read_exact(&mut size)?;

            found_entry |= Some(Resource(res, PhantomData)) == entry;
            index.insert(
                Resource(res, PhantomData),
                (u64::from_be_bytes(off), u64::from_be_bytes(size)),
            );
        }
        if !found_entry && entry.is_some() {
            warn!("respack does not contain its entry resource")
        }
        info!("opened respack with {} resources", index.len());
        Ok(Self {
            entry,
            index,
            inner,
        })
    }
    pub fn entry(&self) -> Option<Resource<RespackEntry>> {
        self.entry.clone()
    }
    pub fn get_size(&self, key: Resource) -> Option<usize> {
        self.index.get(&key).map(|(_off, size)| *size as usize)
    }
    pub fn iter(&self, mut cb: impl FnMut(Resource, usize)) {
        for (r, (_, s)) in &self.index {
            cb(*r, *s as usize)
        }
    }
    pub fn read(&mut self, key: Resource) -> Result<Option<impl Read>> {
        let Some((off, size)) = self.index.get(&key).copied() else {
            return Ok(None);
        };
        self.inner.seek(SeekFrom::Start(off))?;
        Ok(Some(self.inner.by_ref().take(size)))
    }
}