Skip to content

Commit

Permalink
Finish initial instance implementation :D
Browse files Browse the repository at this point in the history
  • Loading branch information
RedstoneWizard08 committed Mar 20, 2023
1 parent 0846081 commit 1197f02
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 108 deletions.
3 changes: 3 additions & 0 deletions app/src/components/Mod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export const Mod: FunctionalComponent<ModParams> = ({ mod }) => {
await invoke_proxy("install_mod", {
modId: mod.id,
gameId: mod.game_id,
instanceId: await invoke_proxy("get_active_instance", {
gameId: mod.game_id,
}),
});

setInstalling(false);
Expand Down
16 changes: 14 additions & 2 deletions app/src/invoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ export interface InstanceUpdateArgs {
description: string;
}

export interface ModIntegrity {
name: string;
date_installed: string;
size: number;
install_path: string;
}

export interface ModsIntegrity {
mods: ModIntegrity[];
}

export interface InvokeFunction {
install_spacewarp: [undefined, string];
uninstall_spacewarp: [undefined, string];
Expand All @@ -48,11 +59,12 @@ export interface InvokeFunction {
get_mods: [ModsArgs, BrowseResult];
get_distance: [QueryData, undefined];

install_mod: [ModArgs, undefined];
install_mod: [ModArgs & InstanceArgs, undefined];
backend_boot: [undefined, undefined];
read_mod_json: [undefined, undefined];
read_mod_json: [undefined, ModsIntegrity];

update_description: [InstanceUpdateArgs, undefined];
get_active_instance: [GameArgs, number];
}

export const invoke_proxy = async <K extends keyof InvokeFunction>(
Expand Down
6 changes: 3 additions & 3 deletions cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ pub enum Commands {

#[derive(Subcommand, Debug, Clone, Copy)]
pub enum ModCommands {
Install { id: i32, instance_id: Option<i32> },
Install { id: i32, instance_id: i32 },

Info { id: i32, instance_id: Option<i32> },
Info { id: i32, instance_id: i32 },

Remove { id: i32, instance_id: Option<i32> },
Remove { id: i32, instance_id: i32 },
}

#[derive(Subcommand, Debug, Clone, Copy)]
Expand Down
9 changes: 5 additions & 4 deletions cli/src/commands/mods/install.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
use wormhole_common::{
finder::find_install_dir, installer::mods::ModInstaller, instances::KSPGame,
installer::mods::ModInstaller, instances::Instance,
};

pub async fn install_mod(id: i32, verbose: bool) {
pub async fn install_mod(id: i32, instance_id: i32, verbose: bool) {
if verbose {
println!("Creating mod installer...");
}

let installer = ModInstaller::new(find_install_dir(KSPGame::KSP2));
let instance = Instance::from_id(instance_id).unwrap();
let installer = ModInstaller::new(instance.install_path);

if verbose {
println!("Installing mod...");
}

installer.install_from_spacedock(id).await;
installer.install_from_spacedock(id, instance_id).await;

if verbose {
println!("Mod installed!");
Expand Down
4 changes: 2 additions & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ pub async fn main() {
command:
Some(ModCommands::Install {
id,
instance_id: _iid,
instance_id,
}),
}) = cli.command
{
install_mod(id, verbose).await;
install_mod(id, instance_id, verbose).await;
}
}
233 changes: 140 additions & 93 deletions common/src/installer/mods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
use crate::{
instances::{Instance, InstanceMod, KSPGame},
mods::spacedock::SpaceDockAPI,
util::copy_dir_all,
util::{copy_dir_all, get_data_dir},
};

use rand::{distributions::Alphanumeric, thread_rng, Rng};
Expand All @@ -22,7 +22,7 @@ impl ModInstaller {
return Self { install_path };
}

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

Expand Down Expand Up @@ -72,109 +72,156 @@ impl ModInstaller {

if let Some(game_id) = mod_info.game_id {
if let Some(game) = KSPGame::from_id(game_id) {
let instance = Instance::get_active_instance(game.clone());

let mut instance_mod = InstanceMod {
id: mod_info.id.unwrap(),
name: mod_info.name.unwrap(),
paths: Vec::new(),
};

if game.eq(&KSPGame::KSP2) {
let bep_in_ex_dir = mod_tmp_path.join("BepInEx");

if bep_in_ex_dir.exists() {
copy_dir_all(bep_in_ex_dir.clone(), self.install_path.join("BepInEx"))
.expect("Could not move the BepInEx folder!");

for file in bep_in_ex_dir.read_dir().unwrap() {
let file = file.unwrap();

if file.file_name() == "plugins" || file.file_name() == "config" {
for file2 in file.path().read_dir().unwrap() {
let file2 = file2.unwrap();

instance_mod.paths.push(
"BepInEx/".to_string()
+ file.file_name().into_string().unwrap().as_str()
+ "/"
+ file2.file_name().into_string().unwrap().as_str(),
);
}
let instance = Instance::from_id(instance_id);

if let Some(mut instance) = instance {
let mut instance_mod = InstanceMod {
id: mod_info.id.unwrap(),
name: mod_info.name.unwrap(),
paths: Vec::new(),
};

if game.eq(&KSPGame::KSP2) {
let bep_in_ex_dir = mod_tmp_path.join("BepInEx");

if bep_in_ex_dir.exists() {
if instance.is_active() {
copy_dir_all(
bep_in_ex_dir.clone(),
self.install_path.join("BepInEx"),
)
.expect("Could not move the BepInEx folder!");
} else {
copy_dir_all(
bep_in_ex_dir.clone(),
get_data_dir()
.join("instances")
.join(instance.id.to_string())
.join("BepInEx"),
)
.expect("Could not move the BepInEx folder!");
}

instance_mod.paths.push(
"BepInEx/".to_string()
+ file.file_name().into_string().unwrap().as_str(),
);
}
} else {
copy_dir_all(
mod_tmp_path,
self.install_path.join("BepInEx").join("plugins"),
)
.expect("Could not move the BepInEx folder!");

for file in bep_in_ex_dir.read_dir().unwrap() {
let file = file.unwrap();

instance_mod.paths.push(
"BepInEx/plugins/".to_string()
+ file.file_name().into_string().unwrap().as_str(),
);
}
}
} else {
let mod_contents =
fs::read_dir(mod_tmp_path).expect("Could not read the mod tmp folder!");

let files = mod_contents
.filter_map(|entry| entry.ok())
.collect::<Vec<_>>();

let files_strs = files
.iter()
.map(|entry| entry.file_name().into_string().unwrap())
.collect::<Vec<_>>();

if files_strs.contains(&"GameData".to_string()) {
copy_dir_all(mod_tmp_path, self.install_path.clone())
.expect("Could not move the GameData folder!");

for file in mod_tmp_path.read_dir().unwrap() {
let file = file.unwrap();

if file.file_name() == "GameData" {
for file2 in file.path().read_dir().unwrap() {
let file2 = file2.unwrap();

instance_mod.paths.push(
"GameData/".to_string()
+ file2.file_name().into_string().unwrap().as_str(),
);
for file in bep_in_ex_dir.read_dir().unwrap() {
let file = file.unwrap();

if file.file_name() == "plugins" || file.file_name() == "config" {
for file2 in file.path().read_dir().unwrap() {
let file2 = file2.unwrap();

instance_mod.paths.push(
"BepInEx/".to_string()
+ file.file_name().into_string().unwrap().as_str()
+ "/"
+ file2.file_name().into_string().unwrap().as_str(),
);
}
}

instance_mod.paths.push(
"BepInEx/".to_string()
+ file.file_name().into_string().unwrap().as_str(),
);
}
} else {
if instance.is_active() {
copy_dir_all(
mod_tmp_path,
self.install_path.join("BepInEx").join("plugins"),
)
.expect("Could not move the BepInEx folder!");
} else {
copy_dir_all(
bep_in_ex_dir.clone(),
get_data_dir()
.join("instances")
.join(instance.id.to_string())
.join("BepInEx")
.join("plugins"),
)
.expect("Could not move the BepInEx folder!");
}

instance_mod
.paths
.push(file.file_name().into_string().unwrap());
for file in bep_in_ex_dir.read_dir().unwrap() {
let file = file.unwrap();

instance_mod.paths.push(
"BepInEx/plugins/".to_string()
+ file.file_name().into_string().unwrap().as_str(),
);
}
}
} else {
copy_dir_all(mod_tmp_path, self.install_path.join("GameData"))
.expect("Could not move the GameData folder!");
let mod_contents =
fs::read_dir(mod_tmp_path).expect("Could not read the mod tmp folder!");

let files = mod_contents
.filter_map(|entry| entry.ok())
.collect::<Vec<_>>();

let files_strs = files
.iter()
.map(|entry| entry.file_name().into_string().unwrap())
.collect::<Vec<_>>();

if files_strs.contains(&"GameData".to_string()) {
if instance.is_active() {
copy_dir_all(mod_tmp_path, self.install_path.clone())
.expect("Could not move the GameData folder!");
} else {
copy_dir_all(
mod_tmp_path,
get_data_dir()
.join("instances")
.join(instance.id.to_string()),
)
.expect("Could not move the GameData folder!");
}

for file in mod_tmp_path.read_dir().unwrap() {
let file = file.unwrap();

if file.file_name() == "GameData" {
for file2 in file.path().read_dir().unwrap() {
let file2 = file2.unwrap();

instance_mod.paths.push(
"GameData/".to_string()
+ file2.file_name().into_string().unwrap().as_str(),
);
}
}

for file in mod_tmp_path.read_dir().unwrap() {
let file = file.unwrap();
instance_mod
.paths
.push(file.file_name().into_string().unwrap());
}
} else {
if instance.is_active() {
copy_dir_all(mod_tmp_path, self.install_path.join("GameData"))
.expect("Could not move the GameData folder!");
} else {
copy_dir_all(
mod_tmp_path,
get_data_dir()
.join("instances")
.join(instance.id.to_string())
.join("GameData"),
)
.expect("Could not move the GameData folder!");
}

for file in mod_tmp_path.read_dir().unwrap() {
let file = file.unwrap();

instance_mod.paths.push(
"GameData/".to_string()
+ file.file_name().into_string().unwrap().as_str(),
);
instance_mod.paths.push(
"GameData/".to_string()
+ file.file_name().into_string().unwrap().as_str(),
);
}
}
}
}

if let Some(mut instance) = instance {
instance.mods.push(instance_mod);
instance.save();
}
Expand Down
12 changes: 12 additions & 0 deletions common/src/instances/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,16 @@ impl Instance {
}
}
}

pub fn is_active(&self) -> bool {
let active = Instance::get_active_instance(self.game.clone());

if let Some(active) = active {
if active.id == self.id {
return true;
}
}

return false;
}
}
Loading

0 comments on commit 1197f02

Please sign in to comment.