Automatically enable the mod.

This commit is contained in:
IQuant 2024-07-10 13:17:15 +03:00
parent 0597b1b7f9
commit 5b05d47799
3 changed files with 103 additions and 3 deletions

13
noita-proxy/Cargo.lock generated
View file

@ -1980,6 +1980,7 @@ dependencies = [
"image", "image",
"lz4_flex", "lz4_flex",
"poll-promise", "poll-promise",
"quick-xml 0.36.0",
"rand", "rand",
"reqwest", "reqwest",
"rustc-hash", "rustc-hash",
@ -2381,6 +2382,16 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "quick-xml"
version = "0.36.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4091e032efecb09d7b1f711f487b85ab925632a842627e3200fb088382cde32c"
dependencies = [
"memchr",
"serde",
]
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.36" version = "1.0.36"
@ -3777,7 +3788,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "63b3a62929287001986fb58c789dce9b67604a397c15c611ad9f747300b6c283" checksum = "63b3a62929287001986fb58c789dce9b67604a397c15c611ad9f747300b6c283"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quick-xml", "quick-xml 0.31.0",
"quote", "quote",
] ]

View file

@ -42,6 +42,7 @@ egui_plot = "0.27.2"
crc = "3.2.1" crc = "3.2.1"
argh = "0.1.12" argh = "0.1.12"
shlex = "1.3.0" shlex = "1.3.0"
quick-xml = { version = "0.36.0", features = ["serialize"] }
[build-dependencies] [build-dependencies]
winresource = "0.1.17" winresource = "0.1.17"

View file

@ -1,7 +1,8 @@
use std::{ use std::{
env, env,
error::Error,
fs::{self, File}, fs::{self, File},
io, io::{self, BufReader},
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
@ -10,7 +11,7 @@ use egui_file_dialog::{DialogState, FileDialog};
use poll_promise::Promise; use poll_promise::Promise;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use steamworks::AppId; use steamworks::AppId;
use tracing::{error, info}; use tracing::{error, info, warn};
use crate::{ use crate::{
lang::tr, lang::tr,
@ -54,6 +55,7 @@ impl Default for Modmanager {
#[derive(Debug, Serialize, Deserialize, Default)] #[derive(Debug, Serialize, Deserialize, Default)]
pub struct ModmanagerSettings { pub struct ModmanagerSettings {
pub game_exe_path: PathBuf, pub game_exe_path: PathBuf,
pub game_save_path: Option<PathBuf>,
} }
impl ModmanagerSettings { impl ModmanagerSettings {
@ -74,6 +76,41 @@ impl ModmanagerSettings {
} }
} }
} }
fn try_find_save_path(&mut self) {
if cfg!(target_os = "windows") {
let appdata_path = PathBuf::from(
std::env::var_os("APPDATA").expect("appdata to be defined on windows"),
);
info!("Appdata path: {}", appdata_path.display());
let save_path = appdata_path.join("LocalLow/Nolla_Games_Noita/");
if save_path.exists() {
info!("Save path exists");
self.game_save_path = Some(save_path);
}
}
if cfg!(target_os = "linux") {
let mut save_path = self.game_exe_path.clone();
// Reach steamapps/
save_path.pop();
save_path.pop();
save_path.pop();
save_path.push(
"compatdata/881100/pfx/drive_c/users/steamuser/AppData/LocalLow/Nolla_Games_Noita/",
);
info!("Probable save_path: {}", save_path.display());
if save_path.exists() {
info!("Save path exists");
self.game_save_path = Some(save_path);
}
}
match &self.game_save_path {
Some(path) => info!("Found game save path: {}", path.display()),
None => warn!("Could not find game save path"),
}
}
fn mod_path(&self) -> PathBuf { fn mod_path(&self) -> PathBuf {
let mut path = self.game_exe_path.clone(); let mut path = self.game_exe_path.clone();
path.pop(); path.pop();
@ -139,6 +176,12 @@ impl Modmanager {
} }
} }
State::PreCheckMod => { State::PreCheckMod => {
if settings.game_save_path.is_none() {
settings.try_find_save_path();
}
if let Some(path) = &settings.game_save_path {
info!("Trying to enable mod: {:?}", enable_mod(path));
}
ui.label("Will check mod install now..."); ui.label("Will check mod install now...");
self.state = State::CheckMod; self.state = State::CheckMod;
ctx.request_repaint(); ctx.request_repaint();
@ -297,3 +340,48 @@ fn is_mod_ok(mod_path: &Path) -> io::Result<bool> {
fn check_path_valid(game_path: &Path) -> bool { fn check_path_valid(game_path: &Path) -> bool {
game_path.ends_with("noita.exe") && game_path.exists() game_path.ends_with("noita.exe") && game_path.exists()
} }
// Mod enabled="0" name="daily_practice" settings_fold_open="0" workshop_item_id="0"
#[derive(Serialize, Deserialize)]
struct ModEntry {
#[serde(rename = "@enabled")]
enabled: u8,
#[serde(rename = "@name")]
name: String,
#[serde(rename = "@settings_fold_open")]
settings_fold_open: u8,
#[serde(rename = "@workshop_item_id")]
workshop_item_id: u64,
}
#[derive(Serialize, Deserialize)]
struct Mods {
#[serde(rename = "Mod")]
mod_entries: Vec<ModEntry>,
}
impl Mods {
fn entry(&mut self, name: &str) -> &mut ModEntry {
let index = self.mod_entries.iter().position(|ent| ent.name == name);
if let Some(index) = index {
&mut self.mod_entries[index]
} else {
self.mod_entries.push(ModEntry {
enabled: 0,
name: name.to_owned(),
settings_fold_open: 0,
workshop_item_id: 0,
});
self.mod_entries.last_mut().unwrap()
}
}
}
fn enable_mod(saves_path: &Path) -> Result<(), Box<dyn Error>> {
let config_path = saves_path.join("save00/mod_config.xml");
let mut data: Mods = quick_xml::de::from_reader(BufReader::new(File::open(&config_path)?))?;
data.entry("quant.ew").enabled = 1;
let xml = quick_xml::se::to_string(&data)?;
fs::write(saves_path.join("save00/mod_config.xml"), xml)?;
Ok(())
}