mirror of
https://codeberg.org/icewind/tf-asset-loader.git
synced 2026-06-03 16:44:10 +02:00
initial version
This commit is contained in:
commit
9fd30d00ca
9 changed files with 1568 additions and 0 deletions
1
.envrc
Normal file
1
.envrc
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
use flake
|
||||||
64
.github/workflows/ci.yml
vendored
Normal file
64
.github/workflows/ci.yml
vendored
Normal file
|
|
@ -0,0 +1,64 @@
|
||||||
|
name: "CI"
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
push:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
check:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- uses: cachix/install-nix-action@v20
|
||||||
|
- uses: icewind1991/attic-action@v1
|
||||||
|
with:
|
||||||
|
name: ci
|
||||||
|
instance: https://cache.icewind.me
|
||||||
|
authToken: '${{ secrets.ATTIC_TOKEN }}'
|
||||||
|
- run: nix build .#check
|
||||||
|
|
||||||
|
clippy:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs: check
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- uses: cachix/install-nix-action@v20
|
||||||
|
- uses: icewind1991/attic-action@v1
|
||||||
|
with:
|
||||||
|
name: ci
|
||||||
|
instance: https://cache.icewind.me
|
||||||
|
authToken: '${{ secrets.ATTIC_TOKEN }}'
|
||||||
|
- run: nix build .#clippy
|
||||||
|
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs: check
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- uses: cachix/install-nix-action@v20
|
||||||
|
- uses: icewind1991/attic-action@v1
|
||||||
|
with:
|
||||||
|
name: ci
|
||||||
|
instance: https://cache.icewind.me
|
||||||
|
authToken: '${{ secrets.ATTIC_TOKEN }}'
|
||||||
|
- run: nix build .#test
|
||||||
|
|
||||||
|
semver:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs: check
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- name: Check semver
|
||||||
|
uses: obi1kenobi/cargo-semver-checks-action@v2
|
||||||
|
|
||||||
|
msrv:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs: check
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- uses: cachix/install-nix-action@v20
|
||||||
|
- uses: icewind1991/attic-action@v1
|
||||||
|
with:
|
||||||
|
name: ci
|
||||||
|
instance: https://cache.icewind.me
|
||||||
|
authToken: '${{ secrets.ATTIC_TOKEN }}'
|
||||||
|
- run: nix build .#msrv
|
||||||
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
target
|
||||||
|
*.bench
|
||||||
|
*.obj
|
||||||
|
result
|
||||||
|
.direnv
|
||||||
|
*.snap.new
|
||||||
1092
Cargo.lock
generated
Normal file
1092
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
17
Cargo.toml
Normal file
17
Cargo.toml
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
[package]
|
||||||
|
name = "tf-asset-loader"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
rust-version = "1.67.0"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
steamlocate = "=2.0.0-alpha.0"
|
||||||
|
tracing = "0.1.40"
|
||||||
|
vpk = { version = "0.2.0", optional = true }
|
||||||
|
vbsp = { version = "0.3.0", optional = true }
|
||||||
|
thiserror = "1.0.50"
|
||||||
|
zip-lzma = { version = "0.6.3", optional = true }
|
||||||
|
|
||||||
|
[features]
|
||||||
|
bsp = ["vbsp", "zip-lzma"]
|
||||||
|
default = ["vpk"]
|
||||||
105
flake.lock
generated
Normal file
105
flake.lock
generated
Normal file
|
|
@ -0,0 +1,105 @@
|
||||||
|
{
|
||||||
|
"nodes": {
|
||||||
|
"naersk": {
|
||||||
|
"inputs": {
|
||||||
|
"nixpkgs": [
|
||||||
|
"nixpkgs"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1698420672,
|
||||||
|
"narHash": "sha256-/TdeHMPRjjdJub7p7+w55vyABrsJlt5QkznPYy55vKA=",
|
||||||
|
"owner": "nix-community",
|
||||||
|
"repo": "naersk",
|
||||||
|
"rev": "aeb58d5e8faead8980a807c840232697982d47b9",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "nix-community",
|
||||||
|
"repo": "naersk",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nixpkgs": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1702346276,
|
||||||
|
"narHash": "sha256-eAQgwIWApFQ40ipeOjVSoK4TEHVd6nbSd9fApiHIw5A=",
|
||||||
|
"owner": "NixOS",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "cf28ee258fd5f9a52de6b9865cdb93a1f96d09b7",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"id": "nixpkgs",
|
||||||
|
"ref": "nixos-23.11",
|
||||||
|
"type": "indirect"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": {
|
||||||
|
"inputs": {
|
||||||
|
"naersk": "naersk",
|
||||||
|
"nixpkgs": "nixpkgs",
|
||||||
|
"rust-overlay": "rust-overlay",
|
||||||
|
"utils": "utils"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"rust-overlay": {
|
||||||
|
"inputs": {
|
||||||
|
"flake-utils": [
|
||||||
|
"utils"
|
||||||
|
],
|
||||||
|
"nixpkgs": [
|
||||||
|
"nixpkgs"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1702520151,
|
||||||
|
"narHash": "sha256-jxJWosN7hgcW+dFT8V3EBDCYUOjv5tpjEBRmlakS7tU=",
|
||||||
|
"owner": "oxalica",
|
||||||
|
"repo": "rust-overlay",
|
||||||
|
"rev": "d6a1d8f80dbcda4c13993b859a3574c3dde61072",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "oxalica",
|
||||||
|
"repo": "rust-overlay",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"systems": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1681028828,
|
||||||
|
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
|
||||||
|
"owner": "nix-systems",
|
||||||
|
"repo": "default",
|
||||||
|
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "nix-systems",
|
||||||
|
"repo": "default",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"utils": {
|
||||||
|
"inputs": {
|
||||||
|
"systems": "systems"
|
||||||
|
},
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1701680307,
|
||||||
|
"narHash": "sha256-kAuep2h5ajznlPMD9rnQyffWG8EM/C73lejGofXvdM8=",
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "flake-utils",
|
||||||
|
"rev": "4022d587cbbfd70fe950c1e2083a02621806a725",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "flake-utils",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": "root",
|
||||||
|
"version": 7
|
||||||
|
}
|
||||||
102
flake.nix
Normal file
102
flake.nix
Normal file
|
|
@ -0,0 +1,102 @@
|
||||||
|
{
|
||||||
|
inputs = {
|
||||||
|
nixpkgs.url = "nixpkgs/nixos-23.11";
|
||||||
|
utils.url = "github:numtide/flake-utils";
|
||||||
|
naersk.url = "github:nix-community/naersk";
|
||||||
|
naersk.inputs.nixpkgs.follows = "nixpkgs";
|
||||||
|
rust-overlay.url = "github:oxalica/rust-overlay";
|
||||||
|
rust-overlay.inputs.nixpkgs.follows = "nixpkgs";
|
||||||
|
rust-overlay.inputs.flake-utils.follows = "utils";
|
||||||
|
};
|
||||||
|
|
||||||
|
outputs = {
|
||||||
|
self,
|
||||||
|
nixpkgs,
|
||||||
|
utils,
|
||||||
|
naersk,
|
||||||
|
rust-overlay,
|
||||||
|
}:
|
||||||
|
utils.lib.eachDefaultSystem (system: let
|
||||||
|
overlays = [(import rust-overlay)];
|
||||||
|
pkgs = (import nixpkgs) {
|
||||||
|
inherit system overlays;
|
||||||
|
};
|
||||||
|
inherit (pkgs) lib callPackage rust-bin mkShell;
|
||||||
|
inherit (lib.sources) sourceByRegex;
|
||||||
|
|
||||||
|
msrv = (fromTOML (readFile ./Cargo.toml)).package.rust-version;
|
||||||
|
inherit (builtins) fromTOML readFile;
|
||||||
|
toolchain = rust-bin.stable.latest.default;
|
||||||
|
msrvToolchain = rust-bin.stable."${msrv}".default;
|
||||||
|
|
||||||
|
naersk' = callPackage naersk {
|
||||||
|
rustc = toolchain;
|
||||||
|
cargo = toolchain;
|
||||||
|
};
|
||||||
|
msrvNaersk = callPackage naersk {
|
||||||
|
rustc = msrvToolchain;
|
||||||
|
cargo = msrvToolchain;
|
||||||
|
};
|
||||||
|
|
||||||
|
src = sourceByRegex ./. ["Cargo.*" "(src|derive|benches|tests|examples|koth_bagel.*)(/.*)?"];
|
||||||
|
nearskOpt = {
|
||||||
|
pname = "tf-asset-loader";
|
||||||
|
root = src;
|
||||||
|
};
|
||||||
|
in rec {
|
||||||
|
packages = {
|
||||||
|
check = naersk'.buildPackage (nearskOpt
|
||||||
|
// {
|
||||||
|
mode = "check";
|
||||||
|
});
|
||||||
|
clippy = naersk'.buildPackage (nearskOpt
|
||||||
|
// {
|
||||||
|
mode = "clippy";
|
||||||
|
});
|
||||||
|
test = naersk'.buildPackage (nearskOpt
|
||||||
|
// {
|
||||||
|
release = false;
|
||||||
|
mode = "test";
|
||||||
|
});
|
||||||
|
msrv = msrvNaersk.buildPackage (nearskOpt
|
||||||
|
// {
|
||||||
|
mode = "check";
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
devShells = let
|
||||||
|
tools = with pkgs; [
|
||||||
|
bacon
|
||||||
|
cargo-edit
|
||||||
|
cargo-outdated
|
||||||
|
cargo-audit
|
||||||
|
cargo-msrv
|
||||||
|
cargo-semver-checks
|
||||||
|
cargo-insta
|
||||||
|
(writeShellApplication {
|
||||||
|
name = "cargo-fuzz";
|
||||||
|
runtimeInputs = [cargo-fuzz toolchain];
|
||||||
|
text = ''
|
||||||
|
# shellcheck disable=SC2068
|
||||||
|
RUSTC_BOOTSTRAP=1 cargo-fuzz $@
|
||||||
|
'';
|
||||||
|
})
|
||||||
|
(writeShellApplication {
|
||||||
|
name = "cargo-expand";
|
||||||
|
runtimeInputs = [cargo-expand toolchain];
|
||||||
|
text = ''
|
||||||
|
# shellcheck disable=SC2068
|
||||||
|
RUSTC_BOOTSTRAP=1 cargo-expand $@
|
||||||
|
'';
|
||||||
|
})
|
||||||
|
];
|
||||||
|
in {
|
||||||
|
default = mkShell {
|
||||||
|
nativeBuildInputs = [toolchain] ++ tools;
|
||||||
|
};
|
||||||
|
msrv = mkShell {
|
||||||
|
nativeBuildInputs = [msrvToolchain] ++ tools;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
111
src/lib.rs
Normal file
111
src/lib.rs
Normal file
|
|
@ -0,0 +1,111 @@
|
||||||
|
pub mod source;
|
||||||
|
|
||||||
|
pub use source::AssetSource;
|
||||||
|
use std::env::var_os;
|
||||||
|
use std::fmt::Debug;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use steamlocate::SteamDir;
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
#[derive(Debug, Error)]
|
||||||
|
pub enum LoaderError {
|
||||||
|
#[error("Failed to find tf2 install location")]
|
||||||
|
Tf2NotFound,
|
||||||
|
#[error(transparent)]
|
||||||
|
Io(#[from] std::io::Error),
|
||||||
|
#[cfg(feature = "zip-lzma")]
|
||||||
|
#[error(transparent)]
|
||||||
|
Zip(#[from] zip_lzma::result::ZipError),
|
||||||
|
#[error("{0}")]
|
||||||
|
Other(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Loader {
|
||||||
|
sources: Vec<Box<dyn AssetSource>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Loader {
|
||||||
|
/// Create the loader
|
||||||
|
pub fn new() -> Result<Self, LoaderError> {
|
||||||
|
let tf2_dir = tf2_path()?;
|
||||||
|
|
||||||
|
let tf_dir = tf2_dir.join("tf");
|
||||||
|
let hl_dir = tf2_dir.join("hl2");
|
||||||
|
let download = tf_dir.join("download");
|
||||||
|
|
||||||
|
#[cfg(feature = "bsp")]
|
||||||
|
let vpks = tf_dir
|
||||||
|
.read_dir()?
|
||||||
|
.chain(hl_dir.read_dir()?)
|
||||||
|
.filter_map(|item| item.ok())
|
||||||
|
.filter_map(|item| Some(item.path().to_str()?.to_string()))
|
||||||
|
.filter(|path| path.ends_with("dir.vpk"))
|
||||||
|
.map(|path| vpk::from_path(&path))
|
||||||
|
.filter_map(|res| res.ok())
|
||||||
|
.map(|vpk| Box::new(vpk) as Box<dyn AssetSource>);
|
||||||
|
|
||||||
|
#[allow(unused_mut)]
|
||||||
|
let mut sources = vec![
|
||||||
|
Box::new(tf_dir) as Box<dyn AssetSource>,
|
||||||
|
Box::new(download),
|
||||||
|
Box::new(hl_dir),
|
||||||
|
];
|
||||||
|
|
||||||
|
#[cfg(feature = "bsp")]
|
||||||
|
sources.extend(vpks);
|
||||||
|
|
||||||
|
Ok(Loader { sources })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_source<S: AssetSource + 'static>(&mut self, source: S) {
|
||||||
|
self.sources.push(Box::new(source))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(skip(self))]
|
||||||
|
pub fn exists(&self, name: &str) -> Result<bool, LoaderError> {
|
||||||
|
for source in self.sources.iter() {
|
||||||
|
if source.has(name)? {
|
||||||
|
return Ok(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(skip(self))]
|
||||||
|
pub fn load(&self, name: &str) -> Result<Option<Vec<u8>>, LoaderError> {
|
||||||
|
for source in self.sources.iter() {
|
||||||
|
if let Some(data) = source.load(name)? {
|
||||||
|
return Ok(Some(data));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn find_in_paths(&self, name: &str, paths: &[String]) -> Option<String> {
|
||||||
|
for path in paths {
|
||||||
|
let full_path = format!("{}{}", path, name);
|
||||||
|
if self.exists(&full_path).unwrap_or_default() {
|
||||||
|
return Some(full_path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tf2_path() -> Result<PathBuf, LoaderError> {
|
||||||
|
if let Some(path) = var_os("TF_DIR") {
|
||||||
|
let path: PathBuf = path.into();
|
||||||
|
if path.is_dir() {
|
||||||
|
Ok(path)
|
||||||
|
} else {
|
||||||
|
Err(LoaderError::Tf2NotFound)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Ok(SteamDir::locate()
|
||||||
|
.ok_or(LoaderError::Tf2NotFound)?
|
||||||
|
.app(&440)
|
||||||
|
.ok_or(LoaderError::Tf2NotFound)?
|
||||||
|
.path
|
||||||
|
.clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
70
src/source.rs
Normal file
70
src/source.rs
Normal file
|
|
@ -0,0 +1,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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue