Skip to content

Commit

Permalink
feat: ✨ add support for local mods directories
Browse files Browse the repository at this point in the history
  • Loading branch information
zuygui committed Nov 11, 2023
1 parent 6ecc818 commit b89e91b
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 17 deletions.
99 changes: 96 additions & 3 deletions Cargo.lock

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

6 changes: 4 additions & 2 deletions projects/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "flowupdater-json-creator"
description = "Create JSON for flow-updater in a TUI"
authors = ["Zuygui", "Bricklou"]
license = "MIT"
version = "1.4.0"
version = "1.5.0"
edition = "2021"
readme = "README.md"
homepage = "https://github.com/zuygui/flowupdater-json-creator"
Expand Down Expand Up @@ -34,7 +34,9 @@ thiserror = { version = "1" }
time = { version = "0.3", features = ["serde", "serde-well-known"] }
openssl = { version = "0.10", features = ["vendored"] }
fujc-api = "1.1.1"

sha1 = "0.10.6"
walkdir = "2"
sha1_smol = "1.0"
[profile.release]
codegen-units = 1
strip = true
Expand Down
19 changes: 15 additions & 4 deletions projects/cli/src/json_creator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::io::Write;
use fujc::curse_api::mods::CurseMod;

use crate::questions::local_mods::LocalMod;

/**
*
* Structure of the output JSON file
Expand All @@ -15,15 +17,24 @@ use fujc::curse_api::mods::CurseMod;
* }
*/

pub fn compile_mods_to_json(mod_list: Vec<CurseMod>) {
pub fn compile_mods_to_json(curse_mod_list: Vec<CurseMod>, local_mod_list: Vec<LocalMod>) {
let json = serde_json::json!({
"curseFiles": mod_list.iter().map(|mod_| {
"curseFiles": curse_mod_list.iter().map(|mod_| {
serde_json::json!({
"projectId": mod_.mod_id,
"fileId": mod_.file_id
})
}).collect::<Vec<serde_json::Value>>()
});
}).collect::<Vec<serde_json::Value>>(),
// A "mods" field empty for now
"mods": local_mod_list.iter().map(|mod_| {
serde_json::json!({
"name": mod_.name,
"downloadUrl": mod_.download_url,
"sha1": mod_.sha1,
"size": mod_.size
})
}).collect::<Vec<serde_json::Value>>() //,
});

let mut file = std::fs::File::create("mods_list.json").unwrap();
file.write_all(json.to_string().as_bytes()).unwrap();
Expand Down
Empty file.
4 changes: 3 additions & 1 deletion projects/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod constants;
mod errors;
mod questions;
mod json_creator;
mod local_files_checker;

#[tokio::main]
async fn main() -> Result<(), Error> {
Expand All @@ -15,9 +16,10 @@ async fn main() -> Result<(), Error> {
questions.ask_modloader().await?;

let mods = questions.ask_mods().await?;
let local_mods = questions.ask_local_mods()?;

// compile mods to json
json_creator::compile_mods_to_json(mods);
json_creator::compile_mods_to_json(mods, local_mods);

println!("――――――――――――――――――――――――――――――――――");
println!("Your mods list has been generated!");
Expand Down
123 changes: 123 additions & 0 deletions projects/cli/src/questions/local_mods.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
use std::path::PathBuf;

use serde::{Serialize, Deserialize};


use crate::errors::Error;

use super::Questions;


#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct LocalMod {
/// Name of the mod (ex: "jei-1.20.2.jar")
pub name: String,
/// The Download URL (serverUrl + name)
#[serde(rename = "downloadUrl")]
pub download_url: String,
/// The SHA1 of the mod
pub sha1: String,
/// The size of the mod
/// (ex: 123456)
pub size: u64,
}

pub fn dir_to_local_mods(dir_path: PathBuf, server_url: String) -> Result<Vec<LocalMod>, Error> {
let mut mods = Vec::new();

// Parse the directory to get all .jar files (**/*.jar)
let walker = walkdir::WalkDir::new(dir_path)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| !e.file_type().is_dir())
.filter(|e| e.path().extension().unwrap() == "jar");

for entry in walker {
let path = entry.path();
let name = path.file_name().unwrap().to_str().unwrap().to_string();
let file_content = std::fs::read(path).unwrap();

let mut hasher = sha1_smol::Sha1::new();

hasher.update(&file_content);

let size = path.metadata().unwrap().len();
let local_mod = LocalMod {
name: name.clone(),
download_url: format!("{}/{}", server_url, name),
sha1: hasher.digest().to_string(),
size,
};

mods.push(local_mod);
}


Ok(mods)
}

impl Questions {

/**
* Questions are
* - Do you want to add a local mods from a directory ?
* - If yes, what is the directory ?
* - If yes, what is the server url ?
*/

fn check_if_user_wanna_add_local_mods(&self) -> bool {
let add_local_mods_question = requestty::Question::confirm("add_local_mods")
.message("Do you want to add local mods ?")
.build();

let binding = requestty::prompt_one(add_local_mods_question)
.unwrap()
.as_bool()
.unwrap();
return binding;
}

fn ask_local_mods_dir(&self) -> PathBuf {
let local_mods_dir_question = requestty::Question::input("local_mods_dir")
.message("What is the directory of the local mods ?")
.build();

let binding = requestty::prompt_one(local_mods_dir_question).unwrap();
let local_mods_dir = &binding.as_string();
PathBuf::from(local_mods_dir.unwrap())
}

fn ask_server_url(&self) -> String {
let server_url_question = requestty::Question::input("server_url")
.message("What is the server url ?")
.build();

let binding = requestty::prompt_one(server_url_question).unwrap();
let server_url = &binding.as_string();
server_url.unwrap().to_string()
}

pub fn ask_local_mods(&self) -> Result<Vec<LocalMod>, Error> {

let mut local_mods: Vec<LocalMod> = Vec::new();

let add_local_mods: bool = self.check_if_user_wanna_add_local_mods();

if add_local_mods {
let local_mods_dir: PathBuf = self.ask_local_mods_dir();
let server_url: String = self.ask_server_url();

// If the URL ends with a slash, remove it
let server_url = if server_url.ends_with("/") {
server_url[..server_url.len() - 1].to_string()
} else {
server_url
};

local_mods = dir_to_local_mods(local_mods_dir, server_url)?;
}

Ok(local_mods)
}

}
1 change: 1 addition & 0 deletions projects/cli/src/questions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use fujc::{curse_api::CurseApi, minecraft::modloader::ModLoaderType};

mod mc_versions;
mod modloader;
pub mod local_mods;
pub mod mods;

pub struct Questions {
Expand Down
Loading

0 comments on commit b89e91b

Please sign in to comment.