From e796ffb2a4f90f597a0be294d3ba89d4c428d070 Mon Sep 17 00:00:00 2001 From: ghostly_zsh Date: Fri, 17 Jan 2025 21:02:20 -0600 Subject: [PATCH] assets no longer part of binary, now dynamically loaded --- Cargo.lock | 294 ++++++++++++++++++- crates/client/Cargo.toml | 5 +- crates/client/src/lib.rs | 2 + crates/client/src/rendering/assets_native.rs | 14 + crates/client/src/rendering/assets_wasm.rs | 92 ++++++ crates/client/src/rendering/mod.rs | 6 + crates/client/src/rendering/renderer.rs | 26 +- crates/kabel/src/test.rs | 6 +- 8 files changed, 432 insertions(+), 13 deletions(-) create mode 100644 crates/client/src/rendering/assets_native.rs create mode 100644 crates/client/src/rendering/assets_wasm.rs diff --git a/Cargo.lock b/Cargo.lock index f6e4015e174e5735a532606758d123d703b8879b..ee15c464ee167d8f869c65255f12a25295840483 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2185,7 +2185,7 @@ dependencies = [ "bitflags 1.3.2", "core-foundation", "core-graphics-types", - "foreign-types", + "foreign-types 0.5.0", "libc", ] @@ -2883,6 +2883,15 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a0d2fde1f7b3d48b8395d5f2de76c18a528bd6a9cdde438df747bfcba3e05d6f" +[[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared 0.1.1", +] + [[package]] name = "foreign-types" version = "0.5.0" @@ -2890,7 +2899,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d737d9aa519fb7b749cbc3b962edcf310a8dd1f4b67c91c4f83975dbdd17d965" dependencies = [ "foreign-types-macros", - "foreign-types-shared", + "foreign-types-shared 0.3.1", ] [[package]] @@ -2904,6 +2913,12 @@ dependencies = [ "syn 2.0.94", ] +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + [[package]] name = "foreign-types-shared" version = "0.3.1" @@ -3415,6 +3430,17 @@ dependencies = [ "itoa", ] +[[package]] +name = "http-body" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" +dependencies = [ + "bytes", + "http 0.2.12", + "pin-project-lite", +] + [[package]] name = "httparse" version = "1.9.4" @@ -3449,6 +3475,43 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" +[[package]] +name = "hyper" +version = "0.14.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41dfc780fdec9373c01bae43289ea34c972e40ee3c9f6b3c8801a35f35586ce7" +dependencies = [ + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "h2", + "http 0.2.12", + "http-body", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "socket2", + "tokio", + "tower-service", + "tracing", + "want", +] + +[[package]] +name = "hyper-tls" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905" +dependencies = [ + "bytes", + "hyper", + "native-tls", + "tokio", + "tokio-native-tls", +] + [[package]] name = "iana-time-zone" version = "0.1.61" @@ -3603,6 +3666,12 @@ dependencies = [ "syn 2.0.94", ] +[[package]] +name = "ipnet" +version = "2.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddc24109865250148c2e0f3d25d4f0f479571723792d3802153c60922a4fb708" + [[package]] name = "is-terminal" version = "0.4.12" @@ -3997,7 +4066,7 @@ dependencies = [ "bitflags 2.6.0", "block", "core-graphics-types", - "foreign-types", + "foreign-types 0.5.0", "log", "objc", "paste", @@ -4012,7 +4081,7 @@ dependencies = [ "bitflags 2.6.0", "block", "core-graphics-types", - "foreign-types", + "foreign-types 0.5.0", "log", "objc", "paste", @@ -4190,6 +4259,23 @@ dependencies = [ "syn 2.0.94", ] +[[package]] +name = "native-tls" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8614eb2c83d59d1c8cc974dd3f920198647674a0a035e1af1fa58707e317466" +dependencies = [ + "libc", + "log", + "openssl", + "openssl-probe", + "openssl-sys", + "schannel", + "security-framework", + "security-framework-sys", + "tempfile", +] + [[package]] name = "ndk" version = "0.8.0" @@ -4698,6 +4784,50 @@ version = "11.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b410bbe7e14ab526a0e86877eb47c6996a2bd7746f027ba551028c925390e4e9" +[[package]] +name = "openssl" +version = "0.10.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6174bc48f102d208783c2c84bf931bb75927a617866870de8a4ea85597f871f5" +dependencies = [ + "bitflags 2.6.0", + "cfg-if", + "foreign-types 0.3.2", + "libc", + "once_cell", + "openssl-macros", + "openssl-sys", +] + +[[package]] +name = "openssl-macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.94", +] + +[[package]] +name = "openssl-probe" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" + +[[package]] +name = "openssl-sys" +version = "0.9.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45abf306cbf99debc8195b66b7346498d7b10c210de50418b5ccd7ceba08c741" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + [[package]] name = "orbclient" version = "0.3.47" @@ -5336,6 +5466,46 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "19b30a45b0cd0bcca8037f3d0dc3421eaf95327a17cad11964fb8179b4fc4832" +[[package]] +name = "reqwest" +version = "0.11.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd67538700a17451e7cba03ac727fb961abb7607553461627b97de0b89cf4a62" +dependencies = [ + "base64 0.21.7", + "bytes", + "encoding_rs", + "futures-core", + "futures-util", + "h2", + "http 0.2.12", + "http-body", + "hyper", + "hyper-tls", + "ipnet", + "js-sys", + "log", + "mime", + "native-tls", + "once_cell", + "percent-encoding", + "pin-project-lite", + "rustls-pemfile", + "serde", + "serde_json", + "serde_urlencoded", + "sync_wrapper", + "system-configuration", + "tokio", + "tokio-native-tls", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "winreg", +] + [[package]] name = "rgb" version = "0.8.50" @@ -5452,6 +5622,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "rustls-pemfile" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c74cae0a4cf6ccbbf5f359f08efdf8ee7e1dc532573bf0db71968cb56b1448c" +dependencies = [ + "base64 0.21.7", +] + [[package]] name = "rustls-pki-types" version = "1.10.1" @@ -5499,6 +5678,15 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "schannel" +version = "0.1.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f29ebaa345f945cec9fbbc532eb307f0fdad8161f281b6369539c8d84876b3d" +dependencies = [ + "windows-sys 0.59.0", +] + [[package]] name = "scoped-futures" version = "0.1.3" @@ -5534,6 +5722,29 @@ dependencies = [ "tiny-skia", ] +[[package]] +name = "security-framework" +version = "2.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "897b2245f0b511c87893af39b033e5ca9cce68824c4d7e7630b5a1d339658d02" +dependencies = [ + "bitflags 2.6.0", + "core-foundation", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49db231d56a190491cb4aeda9527f1ad45345af50b0851622a7adb8c03b01c32" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "semver" version = "1.0.23" @@ -5871,6 +6082,7 @@ dependencies = [ "image 0.25.5", "nalgebra 0.33.2", "pollster", + "reqwest", "thiserror 2.0.9", "tracing", "tracing-subscriber", @@ -5991,6 +6203,12 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "sync_wrapper" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" + [[package]] name = "sysinfo" version = "0.30.13" @@ -6005,6 +6223,27 @@ dependencies = [ "windows 0.52.0", ] +[[package]] +name = "system-configuration" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba3a3adc5c275d719af8cb4272ea1c4a6d668a777f37e115f6d11ddbc1c8e0e7" +dependencies = [ + "bitflags 1.3.2", + "core-foundation", + "system-configuration-sys", +] + +[[package]] +name = "system-configuration-sys" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75fb188eb626b924683e3b95e3a48e63551fcfb51949de2f06a9d91dbee93c9" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "system-deps" version = "6.2.2" @@ -6261,6 +6500,16 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "tokio-native-tls" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" +dependencies = [ + "native-tls", + "tokio", +] + [[package]] name = "tokio-postgres" version = "0.7.11" @@ -6370,6 +6619,12 @@ dependencies = [ "winnow 0.6.18", ] +[[package]] +name = "tower-service" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" + [[package]] name = "tracing" version = "0.1.40" @@ -6508,6 +6763,12 @@ dependencies = [ "cc", ] +[[package]] +name = "try-lock" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" + [[package]] name = "ttf-parser" version = "0.25.1" @@ -6680,6 +6941,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + [[package]] name = "version-compare" version = "0.2.0" @@ -6702,6 +6969,15 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "want" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa7760aed19e106de2c7c0b581b509f2f25d3dacaf737cb82ac61bc6d760b0e" +dependencies = [ + "try-lock", +] + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -7704,6 +7980,16 @@ dependencies = [ "memchr", ] +[[package]] +name = "winreg" +version = "0.50.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "524e57b2c537c0f9b1e69f1965311ec12182b4122e45035b1508cd24d2adadb1" +dependencies = [ + "cfg-if", + "windows-sys 0.48.0", +] + [[package]] name = "x11-dl" version = "2.21.0" diff --git a/crates/client/Cargo.toml b/crates/client/Cargo.toml index f4d5ab73b3e45df8ae3db893e27b7e2975057d79..87693f069004eb921f2a3da3076dcf9d09ac83f2 100644 --- a/crates/client/Cargo.toml +++ b/crates/client/Cargo.toml @@ -27,9 +27,10 @@ nalgebra = "0.33" tracing-web = "0.1" # Log output console_error_panic_hook = "0.1" # Give useful information in the panic response, other than the useless "entered unreachable code" wasm-bindgen = "0.2" -web-sys = "0.3" +web-sys = { version = "0.3", features = ["Window", "Location"] } wasm-bindgen-futures = "0.4" +reqwest = "0.11" # Native dependencies [target.'cfg(not(target_arch = "wasm32"))'.dependencies] -pollster = "0.4" \ No newline at end of file +pollster = "0.4" diff --git a/crates/client/src/lib.rs b/crates/client/src/lib.rs index 438f3ab87e0cd16169793658441c954c42c8b21f..aef5b864aa84f663e0211b7c8548cf8286d3fc16 100644 --- a/crates/client/src/lib.rs +++ b/crates/client/src/lib.rs @@ -8,6 +8,7 @@ use bevy_ecs::system::ResMut; use bevy_ecs::world::World; use ecs::Shear; use egui::{Context, DragValue}; +use rendering::assets::Assets; use tracing::info; use winit::event_loop::{ControlFlow, EventLoop}; @@ -45,6 +46,7 @@ pub fn start() { shear_y: 0.0, zoom: 1.0, }); + world.insert_resource(Assets::new()); let mut start_schedule = Schedule::default(); // Add startup things here diff --git a/crates/client/src/rendering/assets_native.rs b/crates/client/src/rendering/assets_native.rs new file mode 100644 index 0000000000000000000000000000000000000000..fbcef99f6d58c7e1111a87e35a3e6d6b2597651b --- /dev/null +++ b/crates/client/src/rendering/assets_native.rs @@ -0,0 +1,14 @@ +use bevy_ecs::system::Resource; + +#[derive(Resource)] +pub struct Assets { + +} +impl Assets { + pub fn new() -> Self { + Assets { } + } + pub fn get(&self, local_path: impl Into) -> Option> { + std::fs::read(format!("src/textures/{}", local_path.into())).ok() + } +} diff --git a/crates/client/src/rendering/assets_wasm.rs b/crates/client/src/rendering/assets_wasm.rs new file mode 100644 index 0000000000000000000000000000000000000000..ecb40ed4bc258735dc682ebdef9a33f8e0addd1c --- /dev/null +++ b/crates/client/src/rendering/assets_wasm.rs @@ -0,0 +1,92 @@ +use std::{collections::HashMap, fmt::Display, sync::{Arc, Mutex}}; + +use bevy_ecs::system::Resource; +use futures::channel::oneshot::{Receiver, Sender}; +use image::EncodableLayout; +use tracing::info; +use wasm_bindgen_futures::spawn_local; + +use super::texture::{self, Texture}; + +#[derive(Debug, Clone)] +pub enum AssetError { + AssetNotFound, + ResponseNotOk(u16), +} +impl Display for AssetError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + AssetError::AssetNotFound => write!(f, "Asset not found"), + AssetError::ResponseNotOk(code) => write!(f, "Server response was not ok {}", code), + } + } +} +impl std::error::Error for AssetError {} + +#[derive(Resource)] +pub struct Assets { + texture_receivers: Arc>>>>, + textures: Arc>>>, +} + +impl Assets { + pub fn new() -> Self { + Assets { + textures: Arc::new(Mutex::new(HashMap::new())), + texture_receivers: Arc::new(Mutex::new(HashMap::new())), + } + } + pub fn get(&self, local_path: impl Into) -> Option> { + let (tx, rx) = futures::channel::oneshot::channel(); + let local_path = local_path.into(); + let contains_texture = { + self.textures.lock().unwrap().contains_key(&local_path) + }; + let contains_texture_receiver = { + self.texture_receivers.lock().unwrap().contains_key(&local_path) + }; + if !contains_texture && !contains_texture_receiver { + let textures = self.textures.clone(); + self.texture_receivers.lock().unwrap().insert(local_path.clone(), rx); + spawn_local(async move { + let window = web_sys::window().unwrap(); + let resp = match reqwest::get(format!("{}/src/textures/{}", window.location().origin().unwrap(), local_path)).await { + Ok(resp) => resp, + Err(e) => { + if e.is_request() { + panic!("Error in request {}", e); + } else if e.is_body() { + panic!("Error in body {}", e); + } else if e.is_status() { + panic!("Bad status: {}", e.status().unwrap()); + } else if e.is_decode() { + panic!("Couldn't decode response's body {}", e); + } else if e.is_redirect() { + panic!("Response caused redirect loop {}", e); + } else if e.is_builder() { + panic!("Error in builder {}", e); + } + panic!(); + }, + }; + let bytes = match resp.bytes().await { + Ok(bytes) => bytes, + Err(e) => todo!(), + }.to_vec(); + textures.lock().unwrap().insert(local_path, bytes.clone()); + tx.send(bytes).unwrap(); + }); + None + } else if !contains_texture { + let mut texture_receivers = self.texture_receivers.lock().unwrap(); + let rx = texture_receivers.get_mut(&local_path).unwrap(); + if let Ok(Some(texture)) = rx.try_recv() { + self.texture_receivers.lock().unwrap().remove(&local_path); + return Some(texture); + } + None + } else { + self.textures.lock().unwrap().get(&local_path).cloned() + } + } +} diff --git a/crates/client/src/rendering/mod.rs b/crates/client/src/rendering/mod.rs index 64e0e17c5aa440458b9e590e0f283853433b8f58..c802af48f74102e3b3f66d5b9508b38413b2dd03 100644 --- a/crates/client/src/rendering/mod.rs +++ b/crates/client/src/rendering/mod.rs @@ -1,6 +1,12 @@ mod mipmap; mod renderer; mod texture; +#[cfg(not(target_arch="wasm32"))] +#[path = "assets_native.rs"] +pub mod assets; +#[cfg(target_arch="wasm32")] +#[path = "assets_wasm.rs"] +pub mod assets; pub mod ui; use crate::input::MouseWheelEvent; diff --git a/crates/client/src/rendering/renderer.rs b/crates/client/src/rendering/renderer.rs index 150ffd21cea9fa3ebffc9081ba4a7779cc9c8275..14c61022edc55861a09c1a45cf29e8b78585ef64 100644 --- a/crates/client/src/rendering/renderer.rs +++ b/crates/client/src/rendering/renderer.rs @@ -1,4 +1,5 @@ use crate::ecs::{Camera, Rotation, Scale, Shear, SpriteTexture, Translation}; +use crate::rendering::assets::Assets; use crate::rendering::mipmap::MipGenerator; use crate::rendering::renderer::RenderInitRes::{Initialized, NotReadyYet}; use crate::rendering::texture; @@ -266,16 +267,33 @@ impl Renderer { self.queue.write_buffer(&self.frame_uniform, 0, &frame_uniform); for (pos, shear, scale, tex, rot) in sprites_to_render { - let tex = self.textures.entry(tex.texture.clone()).or_insert_with(|| { + let tex = self.textures.entry(tex.texture.clone()).or_insert({ info!("loading texture {}", &tex.texture); - let b: &[u8] = match tex.texture.as_str() { + let assets = self.world.get_resource::().unwrap(); + let b = match tex.texture.as_str() { + "f" => match assets.get("f.png") { + Some(b) => b, + None => continue, + } + "happy-tree" => match assets.get("happy-tree.png") { + Some(b) => b, + None => continue, + } + "uv" => match assets.get("uv.png") { + Some(b) => b, + None => continue, + } + u => panic!("unknown texture {u}, has it been added in rendering::renderer::::render()?") + }; + tracing::warn!("{:?}", b); + /*let b: &[u8] = match tex.texture.as_str() { "f" => include_bytes!("../textures/f.png"), "happy-tree" => include_bytes!("../textures/happy-tree.png"), "uv" => include_bytes!("../textures/uv.png"), u => panic!("unknown texture {u}, has it been added in rendering::renderer::::render()?") - }; + };*/ texture::Texture::new( - b, + &b, &tex.texture, &self.device, &self.queue, diff --git a/crates/kabel/src/test.rs b/crates/kabel/src/test.rs index 84ad3da42ef8fbd4986c63eea75764413e940805..cca6b53fc0612757428962a7de54ed3386d9dbbb 100644 --- a/crates/kabel/src/test.rs +++ b/crates/kabel/src/test.rs @@ -2,9 +2,9 @@ use test_each_file::test_each_file; use crate::{debug::{debug_ast, debug_token_array}, run_lexer, run_parser}; -test_each_file! { for ["kab", "out"] in "./kabel/test/runtime/" => test } -test_each_file! { for ["kab", "out"] in "./kabel/test/lexer/" => test_lexer } -test_each_file! { for ["kab", "out"] in "./kabel/test/syntax/" => test_parser } +test_each_file! { for ["kab", "out"] in "./crates/kabel/test/runtime/" => test } +test_each_file! { for ["kab", "out"] in "./crates/kabel/test/lexer/" => test_lexer } +test_each_file! { for ["kab", "out"] in "./crates/kabel/test/syntax/" => test_parser } fn test_lexer([program, out]: [&str; 2]) { let mut output = "".to_string();