diff --git a/Cargo.lock b/Cargo.lock index 69d07cf..19163eb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -19,10 +19,11 @@ dependencies = [ "serde_json", "surf", "tokio 1.44.0", - "toml", + "toml 0.8.20", "ureq 3.0.10", "ureq_multipart", "winit", + "winres", "wry", "zip-extract", ] @@ -2457,7 +2458,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc2f4eb4bc735547cfed7c0a4922cbd04a4655978c09b54f1f7b228750664c34" dependencies = [ "cfg-if 1.0.0", - "windows-targets 0.48.5", + "windows-targets 0.52.6", ] [[package]] @@ -2795,7 +2796,7 @@ version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af1844ef2428cc3e1cb900be36181049ef3d3193c63e43026cfe202983b27a56" dependencies = [ - "proc-macro-crate 2.0.0", + "proc-macro-crate 3.3.0", "proc-macro2", "quote", "syn 2.0.100", @@ -4477,7 +4478,7 @@ dependencies = [ "cfg-expr", "heck 0.5.0", "pkg-config", - "toml", + "toml 0.8.20", "version-compare", ] @@ -4743,6 +4744,15 @@ dependencies = [ "tokio 0.2.25", ] +[[package]] +name = "toml" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234" +dependencies = [ + "serde", +] + [[package]] name = "toml" version = "0.8.20" @@ -5806,6 +5816,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "winres" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b68db261ef59e9e52806f688020631e987592bd83619edccda9c47d42cde4f6c" +dependencies = [ + "toml 0.5.11", +] + [[package]] name = "wit-bindgen-rt" version = "0.33.0" diff --git a/Cargo.toml b/Cargo.toml index 99655a6..d4ae5dc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ name = "CraftX" version = "0.1.0" edition = "2024" +build = "build.rs" [dependencies] winit = "0.30.9" @@ -24,6 +25,9 @@ toml = "0.8.20" nicotine = { git = "https://gitea.awain.net/alterwain/Nicotine.git", version = "0.1.22" } rfd = "0.14" +[target.'cfg(windows)'.build-dependencies] +winres = "0.1" + [profile.release] strip = true opt-level = "s" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..d5f2b0c --- /dev/null +++ b/build.rs @@ -0,0 +1,7 @@ +extern crate winres; + +fn main() { + let mut res = winres::WindowsResource::new(); + res.set_icon("icon.ico"); + res.compile().expect("Failed to compile Windows resource"); +} \ No newline at end of file diff --git a/icon.ico b/icon.ico new file mode 100644 index 0000000..a415712 Binary files /dev/null and b/icon.ico differ diff --git a/launcher.rc b/launcher.rc new file mode 100644 index 0000000..a61b2d5 --- /dev/null +++ b/launcher.rc @@ -0,0 +1 @@ +launcher ICON "icon.ico" \ No newline at end of file diff --git a/src/launcher.rs b/src/launcher.rs index 9b60373..a16f4bc 100644 --- a/src/launcher.rs +++ b/src/launcher.rs @@ -4,6 +4,11 @@ use std::io::Cursor; use std::path::PathBuf; use base64::Engine; use base64::prelude::BASE64_STANDARD; +use rand::rngs::StdRng; +use rand::seq::IndexedRandom; +use rand::SeedableRng; +use serde::{Deserialize, Serialize}; +use surf::StatusCode; use tokio::fs::File; use tokio::io::{AsyncBufReadExt, BufReader}; use tokio::process::Command; @@ -753,4 +758,24 @@ impl Launcher { let _ = std::fs::create_dir_all(&assets); let _ = std::fs::create_dir_all(&libraries); } +} + +#[derive(Serialize, Deserialize)] +struct BackgroundFiles { + name: String +} + +pub async fn get_random_bg() -> Result, Box> { + let mut r = surf::get("https://minecraft.awain.net/xcraft/").await?; + if r.status() != StatusCode::Ok { return Ok(None); } + let resp = r.body_bytes().await?; + let resp: Vec = serde_json::from_slice(&resp)?; + let mut rng = StdRng::from_os_rng(); + if let Some(resp) = resp.choose(&mut rng) { + let mut r = surf::get(["https://minecraft.awain.net/xcraft/", &resp.name].concat()).await?; + if r.status() != StatusCode::Ok { return Ok(None); } + let resp = r.body_bytes().await?; + return Ok(Some(["data:image/jpeg;base64,", &BASE64_STANDARD.encode(resp)].concat())); + } + Ok(None) } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index b1105e1..dc27a8b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -307,6 +307,8 @@ async fn main() { p.push("bg.base64"); if let Ok(data) = std::fs::read(p) { responder.respond(Response::new(serde_json::to_vec(&UIMessage { params: vec!["fetch_bg".to_string(), String::from_utf8(data).unwrap()] }).unwrap())); + } else if let Ok(Some(data)) = launcher::get_random_bg().await { + responder.respond(Response::new(serde_json::to_vec(&UIMessage { params: vec!["fetch_bg".to_string(), data] }).unwrap())); } } "update_settings" => {