Skip to content

Commit

Permalink
Create common lib to be shared between main app and hook
Browse files Browse the repository at this point in the history
  • Loading branch information
trumank committed Dec 16, 2023
1 parent 79d175c commit bbc9838
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 135 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
members = [
"mint_lib",
"hook",
"hook_resolvers",
]
Expand All @@ -14,6 +15,7 @@ edition = "2021"
[workspace.dependencies]
anyhow = { version = "1.0.75", features = ["backtrace"] }
patternsleuth = { git = "https://github.com/trumank/patternsleuth" }
steamlocate = "2.0.0-alpha.0"

[package]
name = "mint"
Expand Down Expand Up @@ -45,6 +47,7 @@ image = { version = "0.24.7", default-features = false, features = ["png"] }
indexmap = { version = "2.1.0", features = ["serde"] }
inventory = "0.3.13"
lazy_static = "1.4.0"
mint_lib = { path = "mint_lib" }
modio = { git = "https://github.com/trumank/modio-rs.git", branch = "dev", default-features = false, features = ["rustls-tls"] }
obake = { version = "1.0.5", features = ["serde"] }
opener = "0.6.1"
Expand All @@ -60,7 +63,7 @@ semver = "1.0.20"
serde = { version = "1.0.193", features = ["derive"] }
serde_json = "1.0.108"
sha2 = "0.10.8"
steamlocate = "2.0.0-alpha.0"
steamlocate.workspace = true
task-local-extensions = "0.1.4"
tempfile = "3.8.1"
thiserror = "1.0.50"
Expand Down
1 change: 1 addition & 0 deletions hook/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ windows = { version = "0.52.0", features = [
"Win32_System_Memory",
"Win32_System_Threading",
] }
mint_lib = { path = "../mint_lib" }
26 changes: 2 additions & 24 deletions hook/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::{
path::{Path, PathBuf},
};

use anyhow::{bail, Context, Result};
use anyhow::{Context, Result};
use mint_lib::DRGInstallationType;
use retour::static_detour;
use windows::Win32::{
Foundation::HMODULE,
Expand Down Expand Up @@ -46,29 +47,6 @@ extern "system" fn DllMain(dll_module: HMODULE, call_reason: u32, _: *mut ()) ->
}
}

// TODO refactor crate layout so this can be shared between the hook and integrator
#[derive(Debug)]
pub enum DRGInstallationType {
Steam,
Xbox,
}

impl DRGInstallationType {
pub fn from_exe_path() -> Result<Self> {
let exe_name = std::env::current_exe()
.context("could not determine running exe")?
.file_name()
.context("failed to get exe path")?
.to_string_lossy()
.to_lowercase();
Ok(match exe_name.as_str() {
"fsd-win64-shipping.exe" => Self::Steam,
"fsd-wingdk-shipping.exe" => Self::Xbox,
_ => bail!("unrecognized exe file name: {exe_name}"),
})
}
}

unsafe extern "system" fn init(_: usize) {
patch().ok();
}
Expand Down
11 changes: 11 additions & 0 deletions mint_lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "mint_lib"
repository.workspace = true
authors.workspace = true
license.workspace = true
version.workspace = true
edition.workspace = true

[dependencies]
anyhow.workspace = true
steamlocate.workspace = true
125 changes: 125 additions & 0 deletions mint_lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
use std::path::{Path, PathBuf};

use anyhow::{bail, Context, Result};

#[derive(Debug)]
pub enum DRGInstallationType {
Steam,
Xbox,
}

impl DRGInstallationType {
pub fn from_exe_path() -> Result<Self> {
let exe_name = std::env::current_exe()
.context("could not determine running exe")?
.file_name()
.context("failed to get exe path")?
.to_string_lossy()
.to_lowercase();
Ok(match exe_name.as_str() {
"fsd-win64-shipping.exe" => Self::Steam,
"fsd-wingdk-shipping.exe" => Self::Xbox,
_ => bail!("unrecognized exe file name: {exe_name}"),
})
}
}

impl DRGInstallationType {
pub fn from_pak_path<P: AsRef<Path>>(pak: P) -> Result<Self> {
let pak_name = pak
.as_ref()
.file_name()
.context("failed to get pak file name")?
.to_string_lossy()
.to_lowercase();
Ok(match pak_name.as_str() {
"fsd-windowsnoeditor.pak" => Self::Steam,
"fsd-wingdk.pak" => Self::Xbox,
_ => bail!("unrecognized pak file name: {pak_name}"),
})
}
pub fn binaries_directory_name(&self) -> &'static str {
match self {
Self::Steam => "Win64",
Self::Xbox => "WinGDK",
}
}
pub fn main_pak_name(&self) -> &'static str {
match self {
Self::Steam => "FSD-WindowsNoEditor.pak",
Self::Xbox => "FSD-WinGDK.pak",
}
}
pub fn hook_dll_name(&self) -> &'static str {
match self {
Self::Steam => "x3daudio1_7.dll",
Self::Xbox => "d3d9.dll",
}
}
}

#[derive(Debug)]
pub struct DRGInstallation {
pub root: PathBuf,
pub installation_type: DRGInstallationType,
}

impl DRGInstallation {
/// Returns first DRG installation found. Only supports Steam version
/// TODO locate Xbox version
pub fn find() -> Option<Self> {
steamlocate::SteamDir::locate()
.and_then(|mut steamdir| {
steamdir
.app(&548430)
.map(|a| a.path.join("FSD/Content/Paks/FSD-WindowsNoEditor.pak"))
})
.and_then(|path| Self::from_pak_path(path).ok())
}
pub fn from_pak_path<P: AsRef<Path>>(pak: P) -> Result<Self> {
let root = pak
.as_ref()
.parent()
.and_then(Path::parent)
.and_then(Path::parent)
.context("failed to get pak parent directory")?
.to_path_buf();
Ok(Self {
root,
installation_type: DRGInstallationType::from_pak_path(pak)?,
})
}
pub fn binaries_directory(&self) -> PathBuf {
self.root
.join("Binaries")
.join(self.installation_type.binaries_directory_name())
}
pub fn paks_path(&self) -> PathBuf {
self.root.join("Content").join("Paks")
}
pub fn main_pak(&self) -> PathBuf {
self.root
.join("Content")
.join("Paks")
.join(self.installation_type.main_pak_name())
}
pub fn modio_directory(&self) -> Option<PathBuf> {
match self.installation_type {
DRGInstallationType::Steam => {
#[cfg(target_os = "windows")]
{
Some(PathBuf::from("C:\\Users\\Public\\mod.io\\2475"))
}
#[cfg(target_os = "linux")]
{
steamlocate::SteamDir::locate().map(|s| {
s.path.join(
"steamapps/compatdata/548430/pfx/drive_c/users/Public/mod.io/2475",
)
})
}
}
DRGInstallationType::Xbox => None,
}
}
}
3 changes: 2 additions & 1 deletion src/integrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ use std::io::{self, BufReader, BufWriter, Cursor, ErrorKind, Read, Seek};
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use mint_lib::DRGInstallation;
use repak::PakWriter;
use tracing::info;
use uasset_utils::splice::{
extract_tracked_statements, inject_tracked_statements, walk, AssetVersion, TrackedStatement,
};

use crate::providers::ModInfo;
use crate::{get_pak_from_data, open_file, DRGInstallation};
use crate::{get_pak_from_data, open_file};

use unreal_asset::{
exports::ExportBaseTrait,
Expand Down
108 changes: 1 addition & 107 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::{
path::{Path, PathBuf},
};

use anyhow::{bail, Context, Result};
use anyhow::{Context, Result};

use directories::ProjectDirs;
use error::IntegrationError;
Expand Down Expand Up @@ -70,112 +70,6 @@ impl Dirs {
}
}

#[derive(Debug)]
pub enum DRGInstallationType {
Steam,
Xbox,
}

impl DRGInstallationType {
pub fn from_pak_path<P: AsRef<Path>>(pak: P) -> Result<Self> {
let pak_name = pak
.as_ref()
.file_name()
.context("failed to get pak file name")?
.to_string_lossy()
.to_lowercase();
Ok(match pak_name.as_str() {
"fsd-windowsnoeditor.pak" => Self::Steam,
"fsd-wingdk.pak" => Self::Xbox,
_ => bail!("unrecognized pak file name: {pak_name}"),
})
}
pub fn binaries_directory_name(&self) -> &'static str {
match self {
Self::Steam => "Win64",
Self::Xbox => "WinGDK",
}
}
pub fn main_pak_name(&self) -> &'static str {
match self {
Self::Steam => "FSD-WindowsNoEditor.pak",
Self::Xbox => "FSD-WinGDK.pak",
}
}
pub fn hook_dll_name(&self) -> &'static str {
match self {
Self::Steam => "x3daudio1_7.dll",
Self::Xbox => "d3d9.dll",
}
}
}

#[derive(Debug)]
pub struct DRGInstallation {
pub root: PathBuf,
pub installation_type: DRGInstallationType,
}

impl DRGInstallation {
/// Returns first DRG installation found. Only supports Steam version
/// TODO locate Xbox version
pub fn find() -> Option<Self> {
steamlocate::SteamDir::locate()
.and_then(|mut steamdir| {
steamdir
.app(&548430)
.map(|a| a.path.join("FSD/Content/Paks/FSD-WindowsNoEditor.pak"))
})
.and_then(|path| Self::from_pak_path(path).ok())
}
pub fn from_pak_path<P: AsRef<Path>>(pak: P) -> Result<Self> {
let root = pak
.as_ref()
.parent()
.and_then(Path::parent)
.and_then(Path::parent)
.context("failed to get pak parent directory")?
.to_path_buf();
Ok(Self {
root,
installation_type: DRGInstallationType::from_pak_path(pak)?,
})
}
pub fn binaries_directory(&self) -> PathBuf {
self.root
.join("Binaries")
.join(self.installation_type.binaries_directory_name())
}
pub fn paks_path(&self) -> PathBuf {
self.root.join("Content").join("Paks")
}
pub fn main_pak(&self) -> PathBuf {
self.root
.join("Content")
.join("Paks")
.join(self.installation_type.main_pak_name())
}
pub fn modio_directory(&self) -> Option<PathBuf> {
match self.installation_type {
DRGInstallationType::Steam => {
#[cfg(target_os = "windows")]
{
Some(PathBuf::from("C:\\Users\\Public\\mod.io\\2475"))
}
#[cfg(target_os = "linux")]
{
steamlocate::SteamDir::locate().map(|s| {
s.path.join(
"steamapps/compatdata/548430/pfx/drive_c/users/Public/mod.io/2475",
)
})
}
}
DRGInstallationType::Xbox => None,
}
}
}

/// File::open with the file path included in any error messages
pub fn open_file<P: AsRef<Path>>(path: P) -> Result<std::fs::File> {
std::fs::File::open(&path)
Expand Down
Loading

0 comments on commit bbc9838

Please sign in to comment.