diff options
Diffstat (limited to 'shared/src/store.rs')
-rw-r--r-- | shared/src/store.rs | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/shared/src/store.rs b/shared/src/store.rs index e3e5949..61b5dee 100644 --- a/shared/src/store.rs +++ b/shared/src/store.rs @@ -14,8 +14,8 @@ 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}; -use anyhow::Result; +use crate::{helper::ReadWrite, packets::Resource, respack::RespackReader}; +use anyhow::{Result, bail}; use log::info; use redb::{Database, TableDefinition}; use std::{ @@ -34,6 +34,7 @@ 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> { @@ -56,6 +57,16 @@ impl ResourceStore { 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)?; @@ -84,6 +95,7 @@ impl ResourceStore { 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>>> { @@ -107,6 +119,17 @@ impl ResourceStore { 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> { @@ -130,6 +153,7 @@ impl ResourceStore { rename(path_temp, path)?; } } + ResourceStore::Respack(_) => bail!("tried to write to resback backed store"), } Ok(key) } @@ -158,6 +182,7 @@ impl ResourceStore { .for_each(|(k, v)| cb(*k, v.len())); Ok(()) } + ResourceStore::Respack(r) => Ok(r.lock().unwrap().iter(cb)), } } } |