/*
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 .
*/
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>>),
Respack(Mutex>),
}
impl ResourceStore {
pub fn new_env() -> Result {
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 {
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 {
info!("using static respack cache from {path:?}");
Ok(Self::Respack(
RespackReader::open(File::open(path)?)?.into(),
))
}
pub fn new_respack(pack: RespackReader) -> Result {
info!("using static respack as store");
Ok(Self::Respack(pack.into()))
}
pub fn new_redb(path: &Path) -> Result {
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(&self, key: Resource) -> Result