Skip to content

Commit

Permalink
Combine subaction.bin files into subaction.html files
Browse files Browse the repository at this point in the history
  • Loading branch information
rukai committed Jan 2, 2024
1 parent a44f8d8 commit 411267b
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 47 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

8 changes: 7 additions & 1 deletion fighter_renderer/Cargo.lock

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

6 changes: 1 addition & 5 deletions fighter_renderer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ console_log = "1.0"
console_error_panic_hook = "0.1"
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"
js-sys = "0.3.55"
base64 = "0.21.5"

[dependencies.web-sys]
version = "0.3.4"
Expand All @@ -27,10 +27,6 @@ features = [
'HtmlCollection',
'HtmlInputElement',
'HtmlSelectElement',
'Request',
'RequestInit',
'RequestMode',
'Response',
'Window',
]

Expand Down
38 changes: 8 additions & 30 deletions fighter_renderer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,34 @@

use brawllib_rs::high_level_fighter::HighLevelSubaction;
use brawllib_rs::renderer::app::App;

use js_sys::Uint8Array;
use log::Level;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use wasm_bindgen_futures::JsFuture;
use web_sys::Document;
use web_sys::{Request, RequestInit, RequestMode, Response};
use base64::{engine::general_purpose, Engine as _};

mod dom_ui;
mod hitbox_table_angles;

#[wasm_bindgen]
pub fn run(subaction_bin_path: String) {
pub fn run(subaction_bincode: String) {
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
console_log::init_with_level(Level::Warn).expect("could not initialize logger");

wasm_bindgen_futures::spawn_local(run_async(subaction_bin_path));
wasm_bindgen_futures::spawn_local(run_async(subaction_bincode));
}

async fn run_async(subaction_bin_path: String) {
async fn run_async(subaction_bincode: String) {
let document = web_sys::window().unwrap().document().unwrap();
hitbox_table_angles::draw_hitbox_table_angles(&document);

let subaction = get_subaction(&subaction_bin_path).await;
let subaction = get_subaction(&subaction_bincode).await;

run_renderer(document, subaction).await;
}

async fn get_subaction(subaction_bin_path: &str) -> HighLevelSubaction {
let mut opts = RequestInit::new();
opts.method("GET");
opts.mode(RequestMode::Cors);

let request = Request::new_with_str_and_init(subaction_bin_path, &opts).unwrap();

let window = web_sys::window().unwrap();
let resp_value = JsFuture::from(window.fetch_with_request(&request))
.await
.unwrap();

// `resp_value` is a `Response` object.
assert!(resp_value.is_instance_of::<Response>());
let resp: Response = resp_value.dyn_into().unwrap();

// Convert this other `Promise` into a rust `Future`.
let js_value = JsFuture::from(resp.array_buffer().unwrap()).await.unwrap();
let data = Uint8Array::new(&js_value).to_vec();

bincode::deserialize_from(&*data).unwrap()
async fn get_subaction(subaction_bincode: &str) -> HighLevelSubaction {
let data = general_purpose::STANDARD.decode(subaction_bincode).unwrap();
bincode::deserialize_from(data.as_slice()).unwrap()
}

pub async fn run_renderer(document: Document, subaction: HighLevelSubaction) {
Expand Down
1 change: 1 addition & 0 deletions website/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ tokio = { version = "1.35.1", features = ["full"] }
axum = "0.7.3"
tower-http = { version = "0.5.0", features = ["fs"], git = "https://github.com/rukai/tower-http", branch = "servedir_compress_in_place_hack" }
clap = { version = "4.4.12", features = ["derive"] }
base64 = "0.21.5"
17 changes: 8 additions & 9 deletions website/src/page/subaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::brawl_data::{BrawlMods, SubactionLinks};
use crate::output::OutDir;
use crate::page::{NavLink, Preload};
use crate::process_scripts;
use base64::{engine::general_purpose, Engine as _};
use brawllib_rs::high_level_fighter::CollisionBoxValues;
use brawllib_rs::script_ast::{AngleFlip, GrabTarget, HitBoxEffect};
use handlebars::Handlebars;
Expand Down Expand Up @@ -973,10 +974,6 @@ pub fn generate(
path: assets.fighter_renderer_js.clone(),
as_: "script".to_string(),
},
Preload {
path: format!("{}.bin", subaction.name),
as_: "fetch".to_string(),
},
];

let preload = if wasm_mode {
Expand All @@ -985,10 +982,12 @@ pub fn generate(
&[] as &[Preload]
};

if wasm_mode {
let subaction_bincode = if wasm_mode {
let bin = bincode::serialize(&subaction).unwrap();
dir.create_compressed_file(&format!("{}.bin",subaction.name), &bin);
}
general_purpose::STANDARD.encode(bin)
} else {
String::new()
};

let page = SubactionPage {
assets,
Expand All @@ -997,9 +996,9 @@ pub fn generate(
mod_links: &mod_links,
title: format!("{} - {} - Subaction - {}", brawl_mod.name, fighter_name, subaction.name),
subaction_links: brawl_mod.gen_subaction_links(&fighter.fighter, subaction.name.clone()),
subaction_bin_path: format!("{}.bin", subaction.name),
subaction: serde_json::to_string(&subaction).unwrap(),
subaction_extent: serde_json::to_string(&subaction_extent).unwrap(),
subaction_bincode,
attributes,
throw_table,
hitbox_tables,
Expand Down Expand Up @@ -1078,7 +1077,7 @@ pub struct SubactionPage<'a> {
attributes: Vec<Attribute>,
throw_table: Option<HitBoxTable>,
hitbox_tables: Vec<HitBoxTable>,
subaction_bin_path: String,
subaction_bincode: String,
subaction: String,
subaction_extent: String,
script_main: String,
Expand Down
5 changes: 3 additions & 2 deletions website/templates/subaction.html.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -454,10 +454,11 @@
{{#if wasm_mode}}
<script type="module">
import init, { run } from "{{assets.fighter_renderer_js}}";
const fighter_subaction_data = "{{{ subaction_bincode }}}";
// TODO: apparently we wont need to do just async wrapper + init call nonsense in the future
(async function () {
await init();
run('{{subaction_bin_path}}');
run(fighter_subaction_data);
})();
</script>
{{else}}
Expand All @@ -467,4 +468,4 @@

{{/inline}}

{{~> base ~}}
{{~> base ~}}

0 comments on commit 411267b

Please sign in to comment.