Skip to content

Commit

Permalink
Add mod install functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
RedstoneWizard08 committed Mar 10, 2023
1 parent 3a25fcd commit 9429570
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 1 deletion.
1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ reqwest = { version = "0.11.5", features = ["json"] }
zip-extensions = "0.6.1"
keyvalues-parser = "0.1.0"
ckandex = "0.4.2"
tokio = { version = "1.26.0", features = ["full"] }

[features]
default = ["custom-protocol"]
Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/installer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
pub mod bepinex;
pub mod bepinex_loader;
pub mod doorstop;
pub mod mods;
44 changes: 44 additions & 0 deletions src-tauri/src/installer/mods.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use std::{fs::{File, self}, path::PathBuf, io};

use crate::mods::spacedock::SpaceDockAPI;

pub struct ModInstaller {
pub install_path: PathBuf,
}

impl ModInstaller {
pub fn new(install_path: PathBuf) -> Self {
return Self {
install_path,
};
}

pub async fn install_from_spacedock(&self, id: i32) {
let api = SpaceDockAPI::new();
let url = api.get_mod_download(id).await;

let response = reqwest::get(url)
.await
.expect("Could not download the mod!");

let body = response
.bytes()
.await
.expect("Could not read the mod!");

let mut out_file = File::create(self.install_path.join(".mod.zip"))
.expect("Could not create the mod file!");

io::copy(&mut body.as_ref(), &mut out_file)
.expect("Could not copy the mod to the file!");

zip_extensions::read::zip_extract(
&PathBuf::from(&self.install_path.join(".mod.zip")),
&PathBuf::from(&self.install_path),
)
.expect("Could not extract the mod!");

fs::remove_file(self.install_path.join(".mod.zip"))
.expect("Could not delete the mod file!");
}
}
3 changes: 3 additions & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
#![allow(clippy::needless_return)]
#![feature(async_closure)]

use installer::{bepinex::BepInExInstallManager, doorstop::DoorstopInstallManager};
use instances::InstanceInfo;
Expand Down Expand Up @@ -125,3 +127,4 @@ pub fn main() {
.run(tauri::generate_context!())
.expect("Error while starting Wormhole!");
}
// BIE KSP2 = 3255
12 changes: 12 additions & 0 deletions src-tauri/src/models/latest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
pub struct LatestSchema {
pub friendly_version: String,
pub game_version: String,
pub id: i32,
pub created: String,
pub download_path: String,
pub changelog: Option<String>,
pub downloads: i32,
}
1 change: 1 addition & 0 deletions src-tauri/src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod release;
pub mod latest;
11 changes: 10 additions & 1 deletion src-tauri/src/mods/spacedock/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::{Serialize, Deserialize};

use super::schema::browse::{BrowseResult, ModInfo};
use crate::models::latest::LatestSchema;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct SpaceDockAPI {
Expand Down Expand Up @@ -52,4 +52,13 @@ impl SpaceDockAPI {

panic!("Found: {}", text);
}

pub async fn get_mod_download(&self, id: i32) -> String {
let uri = format!("{}/mod/${}/latest", self.base.clone(), id);
let resp = reqwest::get(uri).await.unwrap();
let text = resp.text().await.unwrap();
let data = serde_json::from_str::<LatestSchema>(&text).unwrap();

return format!("{}{}", self.base, data.download_path);
}
}

0 comments on commit 9429570

Please sign in to comment.