Skip to content

Commit

Permalink
feat: get forge mod base64 icon
Browse files Browse the repository at this point in the history
  • Loading branch information
Broken-Deer committed Jul 17, 2023
1 parent 188e346 commit 8f8d9a4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ anyhow = "1.0"
toml = "0.7.6"
# quartz_nbt = { version = "0.2.8", features = ["serde"] }
hematite-nbt = "0.5.2"
base64 = "0.21.2"
15 changes: 13 additions & 2 deletions src/mod_parser/forge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

use std::ffi::OsStr;
use std::{ffi::OsStr, io::Read};
use std::fs::File;
use std::path::Path;

use anyhow::Result;
use base64::Engine;
use base64::engine::general_purpose;
use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_json::Value;
Expand Down Expand Up @@ -329,7 +331,7 @@ pub fn parse_mod_ziparchive(archive: &mut ZipArchive<File>) -> Result<ResolvedMo
"META-INF/MANIFEST.MF".to_string(),
];
let entries = filter_entries(archive, &target_entries);
let result = if let Some(entry) = entries.get("mcmod.info") {
let mut result = if let Some(entry) = entries.get("mcmod.info") {
let file_content = String::from_utf8(entry.content.clone())?;
ForgeModMcmodInfo::from_info_file(&file_content)?.parse()
} else if let Some(entry) = entries.get("neimod.info") {
Expand All @@ -349,6 +351,15 @@ pub fn parse_mod_ziparchive(archive: &mut ZipArchive<File>) -> Result<ResolvedMo
std::io::ErrorKind::NotFound,
)));
};
fn parse_icon(archive: &mut ZipArchive<File>, icon_path: String) -> Result<String> {
let mut buf = Vec::new();
archive.by_name(&icon_path)?.read_to_end(&mut buf)?;
Ok(format!("data:image/png;base64,{}", general_purpose::STANDARD_NO_PAD.encode(buf)))
}
result.icon = match result.icon {
None => None,
Some(icon_path) => Some(parse_icon(archive, icon_path)?),
};
Ok(result)
}

Expand Down
6 changes: 3 additions & 3 deletions src/mod_parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub trait Parse {
fn parse(self) -> ResolvedMod;
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct ResolvedMod {
pub name: String,
pub description: Option<String>,
Expand All @@ -70,14 +70,14 @@ pub struct ResolvedMod {
pub icon: Option<String>,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct ResolvedDepends {
pub minecraft: Option<Value>,
pub java: Option<Value>,
pub mod_loader: Option<Value>,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct ResolvedAuthorInfo {
pub name: String,
pub contact: Option<HashMap<String, String>>,
Expand Down

0 comments on commit 8f8d9a4

Please sign in to comment.