Skip to content

Commit

Permalink
Add layered file provider for loose file and cook server loading
Browse files Browse the repository at this point in the history
  • Loading branch information
trumank committed Nov 4, 2024
1 parent 4f13350 commit 7beef8e
Show file tree
Hide file tree
Showing 10 changed files with 1,049 additions and 5 deletions.
11 changes: 10 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions hook/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ widestring = "1.1.0"
tokio = { workspace = true, features = ["full"] }
tracing-appender = "0.2.3"
proxy_dll = { git = "https://github.com/trumank/proxy_dll.git", version = "0.1.0" }
seq-macro = "0.3.5"
byteorder = "1.5.0"
glob = "0.3.1"
4 changes: 3 additions & 1 deletion hook/src/hooks/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![allow(clippy::missing_transmute_annotations)]

mod pak;
mod server_list;

use std::{
Expand Down Expand Up @@ -28,7 +29,6 @@ retour::static_detour! {
static DoesSaveGameExist: unsafe extern "system" fn(*const ue::FString, i32) -> bool;
static UObjectTemperatureComponentTimerCallback: unsafe extern "system" fn(*mut c_void);
static WinMain: unsafe extern "system" fn(*mut (), *mut (), *mut (), i32, *const ()) -> i32;

}

#[repr(C)]
Expand Down Expand Up @@ -63,6 +63,8 @@ pub unsafe fn initialize() -> Result<()> {
)?;
WinMain.enable()?;

pak::init()?;

HookUFunctionBind.initialize(
std::mem::transmute(globals().resolution.core.as_ref().unwrap().ufunction_bind.0),
move |function| {
Expand Down
62 changes: 62 additions & 0 deletions hook/src/hooks/pak/file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use std::path::PathBuf;

use anyhow::Result;
use serde::Deserialize;

use super::{FileInfo, FileProvider};

#[derive(Debug, Deserialize)]
pub struct PlainFileProviderConfig {
path: PathBuf,
globs: Vec<String>,
}
impl PlainFileProviderConfig {
pub fn build(self) -> Result<PlainFileProvider> {
PlainFileProvider::new(self)
}
}

pub struct PlainFileProvider {
path: PathBuf,
globs: Vec<glob::Pattern>,
}
impl PlainFileProvider {
pub fn new(config: PlainFileProviderConfig) -> Result<Self> {
Ok(Self {
path: config.path,
globs: config
.globs
.iter()
.flat_map(|g| glob::Pattern::new(g))
.collect(),
})
}
}

impl FileProvider for PlainFileProvider {
fn matches(&self, path: &str) -> bool {
self.globs.iter().any(|g| g.matches(path))
}
fn get_file_info(&mut self, path: &str) -> Result<FileInfo> {
match std::fs::File::open(self.path.join(path)) {
Ok(file) => {
let meta = file.metadata()?;
Ok(FileInfo {
file_exists: true,
read_only: meta.permissions().readonly(),
size: meta.len() as i64,
timestamp: 0, // TODO timestamp
access_timestamp: 0, // TODO timestamp
})
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(FileInfo {
file_exists: false,
..Default::default()
}),
Err(e) => Err(e.into()),
}
}
fn get_file(&mut self, path: &str) -> Result<Vec<u8>> {
Ok(std::fs::read(self.path.join(path))?)
}
}
Loading

0 comments on commit 7beef8e

Please sign in to comment.