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
use crate::LoaderError;
use std::fs::read;
use std::io::ErrorKind;
use std::path::PathBuf;

pub trait AssetSource {
    fn has(&self, path: &str) -> Result<bool, LoaderError>;

    fn load(&self, path: &str) -> Result<Option<Vec<u8>>, LoaderError>;
}

impl AssetSource for PathBuf {
    fn has(&self, path: &str) -> Result<bool, LoaderError> {
        Ok(self.join(path).exists())
    }

    fn load(&self, path: &str) -> Result<Option<Vec<u8>>, LoaderError> {
        match read(self.join(path)) {
            Ok(data) => Ok(Some(data)),
            Err(e) if e.kind() == ErrorKind::NotFound => Ok(None),
            Err(e) => Err(e.into()),
        }
    }
}

#[cfg(feature = "vpk")]
mod vdf {
    use super::AssetSource;
    use crate::LoaderError;
    use vpk::VPK;

    impl AssetSource for VPK {
        fn has(&self, path: &str) -> Result<bool, LoaderError> {
            Ok(self.tree.contains_key(path))
        }

        fn load(&self, path: &str) -> Result<Option<Vec<u8>>, LoaderError> {
            if let Some(entry) = self.tree.get(path) {
                Ok(Some(entry.get()?.into()))
            } else {
                Ok(None)
            }
        }
    }
}

#[cfg(feature = "vbsp")]
mod vbsp {
    use super::AssetSource;
    use crate::LoaderError;
    use vbsp::{BspError, Packfile};

    impl AssetSource for Packfile {
        fn has(&self, path: &str) -> Result<bool, LoaderError> {
            match self.has(path) {
                Ok(found) => Ok(found),
                Err(BspError::Zip(err)) => Err(err.into()),
                Err(e) => Err(LoaderError::Other(e.to_string())), // the error *should* always be a zip error
            }
        }

        fn load(&self, path: &str) -> Result<Option<Vec<u8>>, LoaderError> {
            match self.get(path) {
                Ok(data) => Ok(data),
                Err(BspError::Zip(err)) => Err(err.into()),
                Err(e) => Err(LoaderError::Other(e.to_string())), // the error *should* always be a zip error
            }
        }
    }
}