summaryrefslogtreecommitdiff
path: root/shared/src/store.rs
blob: 61b5deef46d218b571a0b784f34a2d67b5269452 (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
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
/*
    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::{helper::ReadWrite, packets::Resource, respack::RespackReader};
use anyhow::{Result, bail};
use log::info;
use redb::{Database, TableDefinition};
use std::{
    collections::HashMap,
    env::var,
    fs::{File, create_dir_all, rename},
    io::{Read, Write},
    marker::PhantomData,
    path::{Path, PathBuf},
    sync::Mutex,
};

const T_ENTRIES: TableDefinition<[u8; 32], &[u8]> = TableDefinition::new("e");

pub enum ResourceStore {
    Redb(Database),
    Filesystem(PathBuf),
    Memory(Mutex<HashMap<Resource, Vec<u8>>>),
    Respack(Mutex<RespackReader<File>>),
}
impl ResourceStore {
    pub fn new_env() -> Result<Self> {
        if var("WEARECHAT_RES_CACHE_USE_MEMORY").is_ok() {
            Ok(Self::new_memory())
        } else if var("WEARECHAT_RES_CACHE_USE_REDB").is_ok() {
            Self::new_redb(
                &xdg::BaseDirectories::with_prefix("wearechat")?
                    .place_cache_file("resources.db")?,
            )
        } else {
            Self::new_filesystem(
                &xdg::BaseDirectories::with_prefix("wearechat")?
                    .create_cache_directory("resources")?,
            )
        }
    }
    pub fn new_filesystem(path: &Path) -> Result<Self> {
        info!("using filesystem resource cache in {path:?}");
        create_dir_all(path)?;
        Ok(Self::Filesystem(path.to_owned()))
    }
    pub fn new_respack_file(path: &Path) -> Result<Self> {
        info!("using static respack cache from {path:?}");
        Ok(Self::Respack(
            RespackReader::open(File::open(path)?)?.into(),
        ))
    }
    pub fn new_respack(pack: RespackReader<File>) -> Result<Self> {
        info!("using static respack as store");
        Ok(Self::Respack(pack.into()))
    }
    pub fn new_redb(path: &Path) -> Result<Self> {
        info!("initializing redb resource cache...");
        let db = Database::create(path)?;
        let txn = db.begin_write()?;
        txn.open_table(T_ENTRIES)?;
        txn.commit()?;
        info!("done");
        Ok(Self::Redb(db))
    }
    pub fn new_memory() -> Self {
        Self::Memory(HashMap::new().into())
    }
    pub fn get<T: ReadWrite>(&self, key: Resource<T>) -> Result<Option<T>> {
        self.get_raw(Resource(key.0, PhantomData))?
            .map(|b| T::read(&mut b.as_slice()))
            .transpose()
    }
    pub fn set<T: ReadWrite>(&self, value: &T) -> Result<Resource<T>> {
        Ok(Resource(self.set_raw(&value.write_alloc())?.0, PhantomData))
    }
    pub fn get_raw_size(&self, key: Resource) -> Result<Option<usize>> {
        match self {
            ResourceStore::Redb(_) => todo!(),
            ResourceStore::Filesystem(_) => todo!(),
            ResourceStore::Memory(mutex) => {
                let g = mutex.lock().unwrap();
                Ok(g.get(&key).map(|s| s.len()))
            }
            ResourceStore::Respack(r) => Ok(r.lock().unwrap().get_size(key)),
        }
    }
    pub fn get_raw(&self, key: Resource) -> Result<Option<Vec<u8>>> {
        match self {
            ResourceStore::Redb(database) => {
                let txn = database.begin_read()?;
                let ent = txn.open_table(T_ENTRIES)?;
                match ent.get(key.0)? {
                    Some(x) => Ok(Some(x.value().to_vec())),
                    None => Ok(None),
                }
            }
            ResourceStore::Memory(map) => Ok(map.lock().unwrap().get(&key).map(|x| x.to_vec())),
            ResourceStore::Filesystem(root) => {
                let path = fs_cache_path(root, key);
                if path.exists() {
                    let mut buf = Vec::new();
                    File::open(path)?.read_to_end(&mut buf)?;
                    Ok(Some(buf))
                } else {
                    Ok(None)
                }
            }
            ResourceStore::Respack(r) => {
                let mut r = r.lock().unwrap();
                match r.read(key)? {
                    Some(mut reader) => {
                        let mut buf = Vec::new();
                        reader.read_to_end(&mut buf)?;
                        Ok(Some(buf))
                    }
                    None => Ok(None),
                }
            }
        }
    }
    pub fn set_raw(&self, value: &[u8]) -> Result<Resource> {
        let key = Resource(resource_hash(value), PhantomData);
        match self {
            ResourceStore::Redb(database) => {
                let txn = database.begin_write()?;
                let mut ent = txn.open_table(T_ENTRIES)?;
                ent.insert(key.0, value)?;
                drop(ent);
                txn.commit()?;
            }
            ResourceStore::Memory(map) => {
                map.lock().unwrap().insert(key, value.to_vec());
            }
            ResourceStore::Filesystem(root) => {
                let path = fs_cache_path(root, key);
                if !path.exists() {
                    let path_temp = path.with_extension("part");
                    File::create(&path_temp)?.write_all(value)?;
                    rename(path_temp, path)?;
                }
            }
            ResourceStore::Respack(_) => bail!("tried to write to resback backed store"),
        }
        Ok(key)
    }
    pub fn iter(&self, mut cb: impl FnMut(Resource, usize)) -> Result<()> {
        match self {
            ResourceStore::Redb(_database) => todo!(),
            ResourceStore::Filesystem(_root) => {
                for e in _root.read_dir()? {
                    let e = e?;
                    let k = e.file_name();
                    let k = k.to_string_lossy();
                    let m = e.metadata()?.len();
                    let mut r = [0; 32];
                    for i in 0..32 {
                        r[i] = u8::from_str_radix(&k[i * 2..(i + 1) * 2], 16)?
                    }
                    cb(Resource(r, PhantomData), m as usize)
                }
                Ok(())
            }
            ResourceStore::Memory(mutex) => {
                mutex
                    .lock()
                    .unwrap()
                    .iter()
                    .for_each(|(k, v)| cb(*k, v.len()));
                Ok(())
            }
            ResourceStore::Respack(r) => Ok(r.lock().unwrap().iter(cb)),
        }
    }
}

pub fn resource_hash(x: &[u8]) -> [u8; 32] {
    let mut hasher = blake3::Hasher::new();
    hasher.update(x);
    hasher.finalize().into()
}

fn fs_cache_path(path: &Path, res: Resource) -> PathBuf {
    path.join(format!(
        "{:016x}{:016x}{:016x}{:016x}",
        u64::from_le_bytes(res.0[0..8].try_into().unwrap()),
        u64::from_le_bytes(res.0[8..16].try_into().unwrap()),
        u64::from_le_bytes(res.0[16..24].try_into().unwrap()),
        u64::from_le_bytes(res.0[24..32].try_into().unwrap()),
    ))
}