From f87ac1a38af96087e8a6927a6cad7ca19b48d76d Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Mon, 19 Dec 2022 16:48:21 +0100 Subject: basic io implementation finished --- .gitignore | 1 + Cargo.lock | 5 +-- Cargo.toml | 1 + data - Copy.db | Bin 0 -> 24576 bytes data.db | Bin 24576 -> 20480 bytes planmodlist.xopp | Bin 244518 -> 243533 bytes src/commands/io.rs | 79 ++++++++++++++++++++++++++++++++++++++++--- src/commands/modification.rs | 25 +++++++++----- src/input.rs | 2 +- 9 files changed, 97 insertions(+), 16 deletions(-) create mode 100644 data - Copy.db diff --git a/.gitignore b/.gitignore index efb6ba7..6aebc8a 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ /test_tmp .planmodlist.autosave.xopp data.db.cp +export.toml diff --git a/Cargo.lock b/Cargo.lock index 6c10e82..c7738d3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -657,6 +657,7 @@ dependencies = [ "serde", "serde_json", "tokio", + "toml", ] [[package]] @@ -1259,9 +1260,9 @@ dependencies = [ [[package]] name = "toml" -version = "0.5.9" +version = "0.5.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7" +checksum = "1333c76748e868a4d9d1017b5ab53171dfd095f70c712fdb4653a406547f598f" dependencies = [ "serde", ] diff --git a/Cargo.toml b/Cargo.toml index ccbc57e..339d708 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,3 +14,4 @@ config = "0.13.2" rusqlite = { version = "0.28.0", features = ["bundled"] } futures-util = "0.3.14" chrono = "0.4.22" +toml = "0.5.10" diff --git a/data - Copy.db b/data - Copy.db new file mode 100644 index 0000000..da57b82 Binary files /dev/null and b/data - Copy.db differ diff --git a/data.db b/data.db index 445923a..81613e4 100644 Binary files a/data.db and b/data.db differ diff --git a/planmodlist.xopp b/planmodlist.xopp index 6405294..30007d4 100644 Binary files a/planmodlist.xopp and b/planmodlist.xopp differ diff --git a/src/commands/io.rs b/src/commands/io.rs index dc1f408..47991c5 100644 --- a/src/commands/io.rs +++ b/src/commands/io.rs @@ -1,12 +1,81 @@ -use crate::{input::{Input, Subcmd}, config::Cfg}; +use std::fs::File; +use std::io::prelude::*; +use serde::{Serialize, Deserialize}; -pub fn io(_config: Cfg, input: Input) -> Result<(), Box> { +use crate::{input::{Input, Subcmd}, db::{lists_get, userlist_get_all_ids, lists_get_all_ids, lists_insert}, config::Cfg, Modloader, mod_add, List}; + +#[derive(Debug, Serialize, Deserialize)] +struct Export { + lists: Vec +} + +#[derive(Debug, Serialize, Deserialize)] +struct ExportList { + id: String, + mods: String, + launcher: String, + mc_version: String, + download_folder: Option, +} + +impl ExportList { + pub fn from(config: Cfg, list_id: String, download: bool) -> Result> { + + let list = lists_get(config.clone(), String::from(&list_id))?; + + let mut dl_folder = None; + if download == true { dl_folder = Some(list.download_folder) }; + + let mods = userlist_get_all_ids(config, list_id)?.join("|"); + + Ok(Self { id: list.id, mods, launcher: list.modloader.stringify(), mc_version: list.mc_version, download_folder: dl_folder }) + } +} + +pub async fn io(config: Cfg, input: Input) -> Result<(), Box> { match input.subcommand.ok_or("INVALID_INPUT")? { - Subcmd::Export => {}, - Subcmd::Import => {}, - _ => {}, + Subcmd::Export => { export(config, input.args)? }, + Subcmd::Import => { import(config).await? }, + _ => { }, + } + + Ok(()) +} + +fn export(config: Cfg, _args: Option>) -> Result<(), Box> { + let list_ids = lists_get_all_ids(config.clone())?; + let mut lists: Vec = vec![]; + for list_id in list_ids { + lists.push(ExportList::from(config.clone(), String::from(list_id), true)?); } + + let toml = toml::to_string( &Export { lists } )?; + + let mut file = File::create("export.toml")?; + file.write_all(&toml.as_bytes())?; Ok(()) } + +async fn import(config: Cfg) -> Result<(), Box> { + + let mut file = File::open("export.toml")?; + let mut content = String::new(); + file.read_to_string(&mut content)?; + let export: Export = toml::from_str(&content)?; + + println!("{:#?}", export); + + for exportlist in export.lists { + let list = List { id: exportlist.id, mc_version: exportlist.mc_version, modloader: Modloader::from(&exportlist.launcher)?, download_folder: exportlist.download_folder.ok_or("NO_DL")? }; + lists_insert(config.clone(), list.id.clone(), list.mc_version.clone(), list.modloader.clone(), String::from(&list.download_folder))?; + //TODO currently workaround, too many requests + let mods: Vec<&str> = exportlist.mods.split("|").collect(); + for mod_id in mods { + println!("Adding {}", mod_id); + mod_add(config.clone(), mod_id, list.clone(), false).await?; + } + } + Ok(()) +} diff --git a/src/commands/modification.rs b/src/commands/modification.rs index 91243fc..f66ce28 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -1,6 +1,6 @@ use std::io::{Error, ErrorKind}; -use crate::{modrinth::{project, versions, extract_current_version, Version}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, mods_get_all_ids, userlist_get_all_ids, userlist_get_current_version, lists_get_all_ids, mods_remove}, input::{Input, Subcmd}, get_current_list, files::{delete_version, download_versions}}; +use crate::{modrinth::{project, versions, extract_current_version, Version}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, mods_get_all_ids, userlist_get_all_ids, userlist_get_current_version, lists_get_all_ids, mods_remove}, input::{Input, Subcmd}, get_current_list, files::{delete_version, download_versions}, List}; pub async fn modification(config: Cfg, input: Input) -> Result<(), Box> { @@ -22,10 +22,19 @@ async fn add(config: Cfg, input: Input) -> Result<(), Box if args.is_empty() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); }; let current_list = get_current_list(config.clone())?; + + mod_add(config, &args[0], current_list, input.disable_download).await?; + + Ok(()) +} - let project = project(String::from(&config.apis.modrinth), &args[0]).await; +pub async fn mod_add(config: Cfg, mod_id: &str, list: List, disable_download: bool) -> Result<(), Box> { + + println!("Adding mod {}", mod_id); + + let project = project(String::from(&config.apis.modrinth), &mod_id).await; - let available_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), current_list.clone()).await; + let available_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; let mut available_versions_vec: Vec = Vec::new(); let current_version: Option; @@ -52,15 +61,15 @@ async fn add(config: Cfg, input: Input) -> Result<(), Box } //add to current list and mod table - match userlist_get_all_ids(config.clone(), current_list.clone().id) { + match userlist_get_all_ids(config.clone(), list.clone().id) { Ok(mods) => { if mods.contains(&project.id) { return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_ON_LIST"))); } else { - userlist_insert(config.clone(), String::from(¤t_list.id), String::from(&project.id), String::from(¤t_version_id), available_versions_vec, file)?; + userlist_insert(config.clone(), String::from(&list.id), String::from(&project.id), String::from(¤t_version_id), available_versions_vec, file)?; } }, - Err(..) => userlist_insert(config.clone(), String::from(¤t_list.id), String::from(&project.id), String::from(¤t_version_id), available_versions_vec, file)?, + Err(..) => userlist_insert(config.clone(), String::from(&list.id), String::from(&project.id), String::from(¤t_version_id), available_versions_vec, file)?, }; match mods_get_all_ids(config.clone()) { @@ -75,8 +84,8 @@ async fn add(config: Cfg, input: Input) -> Result<(), Box mods_insert(config.clone(), String::from(&project.id), String::from(&project.title), project.versions)?; }, }; - - if !input.disable_download && current_version.is_some() { download_versions(current_list, vec![current_version.unwrap()]).await?; } + + if !disable_download && current_version.is_some() { download_versions(list, vec![current_version.unwrap()]).await?; }; Ok(()) } diff --git a/src/input.rs b/src/input.rs index ffc1213..09d05a1 100644 --- a/src/input.rs +++ b/src/input.rs @@ -144,7 +144,7 @@ pub async fn get_input(config: Cfg) -> Result<(), Box> { download(config, input).await }, Cmd::Io => { - io(config, input) + io(config, input).await } } } -- cgit v1.2.3