From 8523d809c1b5c7c90547d877915b1555f1aa394b Mon Sep 17 00:00:00 2001 From: wormtql <584130248@qq.com> Date: Sun, 5 May 2024 22:51:25 +0800 Subject: [PATCH] feat(Genshin): Add csv format Close #149 --- Cargo.lock | 107 ++++++++++++++++-- yas-genshin/Cargo.toml | 7 +- yas-genshin/src/artifact/artifact.rs | 4 +- yas-genshin/src/export/artifact/csv.rs | 76 +++++++++++++ .../src/export/artifact/export_format.rs | 1 + yas-genshin/src/export/artifact/exporter.rs | 12 ++ yas-genshin/src/export/artifact/mod.rs | 2 +- 7 files changed, 191 insertions(+), 18 deletions(-) create mode 100644 yas-genshin/src/export/artifact/csv.rs diff --git a/Cargo.lock b/Cargo.lock index dcd7f5c7..d7ee1d75 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -47,16 +47,31 @@ dependencies = [ "anstyle", "anstyle-parse", "anstyle-query", - "anstyle-wincon", + "anstyle-wincon 2.1.0", "colorchoice", "utf8parse", ] +[[package]] +name = "anstream" +version = "0.6.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon 3.0.3", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + [[package]] name = "anstyle" -version = "1.0.2" +version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "15c4c2c83f81532e5845a733998b6971faca23490340a418e9b72a3ec9de12ea" +checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b" [[package]] name = "anstyle-parse" @@ -86,6 +101,16 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "anstyle-wincon" +version = "3.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19" +dependencies = [ + "anstyle", + "windows-sys 0.52.0", +] + [[package]] name = "anyhow" version = "1.0.75" @@ -237,7 +262,7 @@ version = "4.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2bb9faaa7c2ef94b2743a21f5a29e6f0010dff4caa69ac8e9d6cf8b6fa74da08" dependencies = [ - "anstream", + "anstream 0.5.0", "anstyle", "clap_lex", "strsim", @@ -604,6 +629,16 @@ dependencies = [ "windows 0.51.1", ] +[[package]] +name = "env_filter" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a009aa4810eb158359dda09d0c87378e4bbb89b5a801f016885a4707ba24f7ea" +dependencies = [ + "log", + "regex", +] + [[package]] name = "env_logger" version = "0.10.0" @@ -617,6 +652,19 @@ dependencies = [ "termcolor", ] +[[package]] +name = "env_logger" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c012a26a7f605efc424dd53697843a72be7dc86ad2d01f7814337794a12231d" +dependencies = [ + "anstream 0.6.14", + "anstyle", + "env_filter", + "humantime", + "log", +] + [[package]] name = "equivalent" version = "1.0.1" @@ -1106,6 +1154,12 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" + [[package]] name = "itertools" version = "0.10.5" @@ -2295,6 +2349,12 @@ version = "0.25.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290d54ea6f91c969195bdbcd7442c8c2a2ba87da8bf60a7ee86a235d4bc1e125" +[[package]] +name = "strum" +version = "0.26.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d8cec3501a5194c432b2b7976db6b7d10ec95c253208b45f83f7136aa985e29" + [[package]] name = "strum_macros" version = "0.25.2" @@ -2308,6 +2368,19 @@ dependencies = [ "syn 2.0.52", ] +[[package]] +name = "strum_macros" +version = "0.26.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6cf59daf282c0a494ba14fd21610a0325f9f90ec9d1231dea26bcb1d696c946" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "rustversion", + "syn 2.0.52", +] + [[package]] name = "syn" version = "1.0.109" @@ -3086,6 +3159,15 @@ dependencies = [ "windows-targets 0.48.5", ] +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.4", +] + [[package]] name = "windows-targets" version = "0.42.2" @@ -3353,8 +3435,8 @@ dependencies = [ "semver", "serde", "serde_json", - "strum", - "strum_macros", + "strum 0.25.0", + "strum_macros 0.25.2", "tract-onnx", "winapi", "windows-capture", @@ -3367,8 +3449,9 @@ version = "0.1.16" dependencies = [ "anyhow", "clap", + "csv", "edit-distance", - "env_logger", + "env_logger 0.11.2", "image", "lazy_static", "log", @@ -3376,8 +3459,8 @@ dependencies = [ "serde", "serde_json", "serde_yaml", - "strum", - "strum_macros", + "strum 0.26.2", + "strum_macros 0.26.2", "windows-capture", "winres", "yas_derive", @@ -3391,15 +3474,15 @@ dependencies = [ "anyhow", "clap", "edit-distance", - "env_logger", + "env_logger 0.10.0", "image", "lazy_static", "log", "regex", "serde", "serde_json", - "strum", - "strum_macros", + "strum 0.25.0", + "strum_macros 0.25.2", "winres", "yas_derive", "yas_scanner", diff --git a/yas-genshin/Cargo.toml b/yas-genshin/Cargo.toml index 577736cb..7638023f 100644 --- a/yas-genshin/Cargo.toml +++ b/yas-genshin/Cargo.toml @@ -20,12 +20,13 @@ image = "0.24" serde_json = "1.0" edit-distance = "2.1" regex = "1.5" -strum = "0.25" -strum_macros = "0.25" +strum = "0.26" +strum_macros = "0.26" lazy_static = "1.4" serde = { version = "1.0", features = ["derive"] } -env_logger = "0.10.0" +env_logger = "0.11" serde_yaml = "0.9" +csv = "1.3.0" [target.'cfg(target_os = "windows")'.dependencies] windows-capture = "1.0.65" diff --git a/yas-genshin/src/artifact/artifact.rs b/yas-genshin/src/artifact/artifact.rs index 353b8054..c78892fc 100644 --- a/yas-genshin/src/artifact/artifact.rs +++ b/yas-genshin/src/artifact/artifact.rs @@ -7,7 +7,7 @@ use strum_macros::Display; use crate::character::CHARACTER_NAMES; use crate::scanner::GenshinArtifactScanResult; -#[derive(Debug, Hash, Clone, PartialEq, Eq)] +#[derive(Debug, Hash, Clone, PartialEq, Eq, Display)] pub enum ArtifactStatName { HealingBonus, CriticalDamage, @@ -30,7 +30,7 @@ pub enum ArtifactStatName { DendroBonus, } -#[derive(Debug, Hash, Clone, PartialEq, Eq)] +#[derive(Debug, Hash, Clone, PartialEq, Eq, Display)] pub enum ArtifactSlot { Flower, Feather, diff --git a/yas-genshin/src/export/artifact/csv.rs b/yas-genshin/src/export/artifact/csv.rs new file mode 100644 index 00000000..e93fd3bb --- /dev/null +++ b/yas-genshin/src/export/artifact/csv.rs @@ -0,0 +1,76 @@ +use serde::{Serialize, Serializer}; +use crate::artifact::GenshinArtifact; + +pub struct GenshinArtifactCSVFormat<'a> { + artifacts: &'a [GenshinArtifact], +} + +/// CSV format: +/// set name, slot, star, level, main stat name, main stat value, [sub state name, sub state value]*4, equip +fn single_artifact_to_string(artifact: &GenshinArtifact) -> String { + let mut s = String::new(); + s = s + &artifact.set_name.to_string(); + s = s + "," + &artifact.slot.to_string(); + s = s + "," + &format!("{}", artifact.star); + s = s + "," + &format!("{}", artifact.level); + s = s + "," + &artifact.main_stat.name.to_string(); + s = s + "," + &format!("{}", artifact.main_stat.value); + if let Some(sub) = &artifact.sub_stat_1 { + s = s + "," + &sub.name.to_string(); + s = s + "," + &format!("{}", sub.value); + } else { + s = s + ",,"; + } + if let Some(sub) = &artifact.sub_stat_2 { + s = s + "," + &sub.name.to_string(); + s = s + "," + &format!("{}", sub.value); + } else { + s = s + ",,"; + } + if let Some(sub) = &artifact.sub_stat_3 { + s = s + "," + &sub.name.to_string(); + s = s + "," + &format!("{}", sub.value); + } else { + s = s + ",,"; + } + if let Some(sub) = &artifact.sub_stat_4 { + s = s + "," + &sub.name.to_string(); + s = s + "," + &format!("{}", sub.value); + } else { + s = s + ",,"; + } + if let Some(e) = &artifact.equip { + s = s + "," + e; + } else { + s = s + "," + } + + s +} + +impl<'a> GenshinArtifactCSVFormat<'a> { + pub fn new(artifacts: &'a [GenshinArtifact]) -> Self { + Self { + artifacts + } + } + + pub fn to_csv_string(&self) -> String { + let header = "套装,部位,星级,等级,主词条名,主词条值,副词条名1,副词条值1,副词条名2,副词条值2,副词条名3,副词条值3,副词条名4,副词条值4,装备"; + let mut result = String::from(header) + "\n"; + + for artifact in self.artifacts.iter() { + let line = single_artifact_to_string(artifact); + result = result + &line + "\n"; + } + + result + } +} + +impl<'a> Serialize for GenshinArtifactCSVFormat<'a> { + fn serialize(&self, serializer: S) -> Result where S: Serializer { + let s = self.to_csv_string(); + serializer.serialize_str(&s) + } +} diff --git a/yas-genshin/src/export/artifact/export_format.rs b/yas-genshin/src/export/artifact/export_format.rs index 52cd4fa0..81be80d4 100644 --- a/yas-genshin/src/export/artifact/export_format.rs +++ b/yas-genshin/src/export/artifact/export_format.rs @@ -5,6 +5,7 @@ pub enum GenshinArtifactExportFormat { Mona, MingyuLab, Good, + CSV, } impl Default for GenshinArtifactExportFormat { diff --git a/yas-genshin/src/export/artifact/exporter.rs b/yas-genshin/src/export/artifact/exporter.rs index 4ec07f05..60c01a28 100644 --- a/yas-genshin/src/export/artifact/exporter.rs +++ b/yas-genshin/src/export/artifact/exporter.rs @@ -7,6 +7,7 @@ use yas::export::{AssetEmitter, ExportAssets}; use crate::artifact::GenshinArtifact; use crate::export::artifact::{ExportArtifactConfig, GenshinArtifactExportFormat}; +use crate::export::artifact::csv::GenshinArtifactCSVFormat; use super::good::GOODFormat; use super::mingyu_lab::MingyuLabFormat; @@ -71,6 +72,17 @@ impl<'a> AssetEmitter for GenshinArtifactExporter<'a> { contents.into_bytes(), Some(String::from("GOOD圣遗物格式"))); }, + GenshinArtifactExportFormat::CSV => { + let path = self.output_dir.join("artifacts.csv"); + let value = GenshinArtifactCSVFormat::new(results); + let contents = value.to_csv_string(); + export_assets.add_asset( + Some(String::from("artifacts csv format")), + path, + contents.into_bytes(), + Some(String::from("CSV格式圣遗物")) + ); + } }; } } diff --git a/yas-genshin/src/export/artifact/mod.rs b/yas-genshin/src/export/artifact/mod.rs index 22c2f918..8804b1e9 100644 --- a/yas-genshin/src/export/artifact/mod.rs +++ b/yas-genshin/src/export/artifact/mod.rs @@ -8,4 +8,4 @@ mod mona_uranai; mod exporter; mod export_format; mod config; - +mod csv;