Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ name: Rust

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
shell.nix
.direnv/
.envrc
src/default.json
testing_jars/
61 changes: 61 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ edition = "2024"
[dependencies]
futures-util = "0.3.31"
reqwest = { version = "0.12.14", features = ["stream"] }
serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.140"
tokio = { version = "1.44.0", features = ["full"] }
zip = "2.2.3"
toml = "0.8.23"
6 changes: 3 additions & 3 deletions src/api_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use serde_json::Value;
pub async fn get_api_search_result(
client: Client,
fabricmod_id: String,
config: Value,
loader_version: String,
server_version: String,
) -> Result<Value, Box<dyn std::error::Error>> {
let search_result = client
.get("https://api.modrinth.com/v2/search")
Expand All @@ -14,8 +15,7 @@ pub async fn get_api_search_result(
"facets",
format!(
"[[\"categories:{}\"],[\"versions:{}\"]]",
config["loader_version"].as_str().unwrap(),
config["server_version"].as_str().unwrap()
loader_version, server_version
),
),
])
Expand Down
65 changes: 65 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use serde::{Deserialize, Serialize};
use std::{
fs::{self, File},
io::{self, Read, Write},
path::{Path, PathBuf},
};

#[derive(Deserialize, Serialize, Debug)]
#[serde(default)]
pub struct Config {
/// Path to the folder containing the mod jar's
pub target_path: PathBuf,

/// Minecraft Server version as a string
pub server_version: String,

/// Mod Loader that is being used as a string
pub loader_version: String,

#[serde(skip)]
path: PathBuf,
}

impl Default for Config {
fn default() -> Self {
Self {
target_path: Path::new(".").into(),
server_version: String::new(),
loader_version: String::new(),
path: "./settings.toml".into(),
}
}
}

impl Config {
pub fn open<P: AsRef<Path>>(path: &P) -> Result<Self, io::Error> {
let mut input_str = String::new();
if !path.as_ref().exists() {
let new_self = Self {
path: path.as_ref().to_path_buf(),
..Default::default()
};
new_self.save()?;
return Ok(new_self);
} else {
File::open(path).unwrap().read_to_string(&mut input_str)?;
}

let mut parsed_config: Self = toml::from_str(&input_str).unwrap();
parsed_config.path = path.as_ref().to_path_buf();

Ok(parsed_config)
}

pub fn save(&self) -> Result<(), io::Error> {
let out_path = &self.path.with_extension("new");
let mut file = File::create(out_path)?;
file.write_all(&toml::to_string_pretty(self).unwrap().into_bytes())?;

// Overwrite the original DB with
fs::rename(out_path, &self.path)?;

Ok(())
}
}
5 changes: 0 additions & 5 deletions src/default.json

This file was deleted.

64 changes: 32 additions & 32 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
mod api_calls;
mod config;

use crate::api_calls::{get_api_project_result, get_api_search_result, get_api_version_result};
use crate::config::Config;
use futures_util::StreamExt;
use reqwest::{Client, get};
use serde_json::Value;
use std::fs::{File, read_dir};
use std::io::{Read, Write};
use std::io::Write;
use std::path::Path;

#[tokio::main]
async fn main() {
//TODO Change this to a TOML file instead of using JSON.
//**********************************************************************
let mut file = File::open("./src/default.json").unwrap();
let mut data = String::new();
file.read_to_string(&mut data).unwrap();

let config: Value = serde_json::from_str(&data).unwrap();
let folder_path = config["target_path"].as_str().unwrap();
let folder = read_dir(folder_path).unwrap();
//**********************************************************************
let config = Config::open(&"./settings.toml").expect("Could not open settings file");
let folder = read_dir(config.target_path.clone()).unwrap();

for file in folder {
let jar_path = file.unwrap().path();
Expand All @@ -30,25 +24,31 @@ async fn main() {

let client = Client::new();

let project_id =
match get_api_search_result(client.clone(), fabricmod_id, config.clone()).await {
Ok(search_result) => {
if is_compatable(
config["loader_version"].clone(),
config["server_version"].clone(),
search_result["hits"][0]["versions"].as_array().unwrap(),
None,
) {
search_result["hits"][0]["project_id"]
.as_str()
.unwrap()
.to_string()
} else {
continue; //ToDo add information to console log
}
let project_id = match get_api_search_result(
client.clone(),
fabricmod_id,
config.loader_version.clone(),
config.server_version.clone(),
)
.await
{
Ok(search_result) => {
if is_compatable(
Value::String(config.loader_version.clone()),
Value::String(config.server_version.clone()),
search_result["hits"][0]["versions"].as_array().unwrap(),
None,
) {
search_result["hits"][0]["project_id"]
.as_str()
.unwrap()
.to_string()
} else {
continue; //ToDo add information to console log
}
Err(_) => continue,
};
}
Err(_) => continue,
};

let mut version_id_array =
match get_api_project_result(client.clone(), project_id.as_str().to_string()).await {
Expand All @@ -71,8 +71,8 @@ async fn main() {
{
Ok(version_result) => {
if is_compatable(
config["loader_version"].clone(),
config["server_version"].clone(),
Value::String(config.loader_version.clone()),
Value::String(config.server_version.clone()),
version_result["game_versions"].as_array().unwrap(),
Some(version_result["loaders"].as_array().unwrap()),
) {
Expand Down Expand Up @@ -101,7 +101,7 @@ async fn main() {
None => continue,
};

let download_path = format!("./{}{}", folder_path, filename);
let download_path = format!("./{}/{}", config.target_path.clone().display(), filename);

match download_files(download_url, &download_path).await {
Ok(_) => println!("file downloaded successfully!"),
Expand Down