Skip to content

Commit

Permalink
use tokio for bigfile loading. also me author :)
Browse files Browse the repository at this point in the history
  • Loading branch information
pizzart committed Oct 30, 2023
1 parent faf8f48 commit d0dd084
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 19 deletions.
67 changes: 67 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion bff-gui/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "bff-gui"
version = "0.1.0"
authors = ["widberg"]
authors = ["widberg", "pizzart"]
edition = "2021"

[dependencies]
Expand All @@ -18,3 +18,4 @@ serde = "1.0.188"
serde_json = "1.0.107"
three-d = "0.16.1"
three-d-asset = "0.6.0"
tokio = { version = "1.33.0", features = ["rt-multi-thread", "time"] }
46 changes: 38 additions & 8 deletions bff-gui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::collections::HashMap;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use std::sync::mpsc::{Receiver, Sender};
use std::sync::Arc;

use bff::bigfile::BigFile;
Expand Down Expand Up @@ -35,6 +36,21 @@ pub enum Artifact {
}

fn main() -> Result<(), eframe::Error> {
let rt = tokio::runtime::Runtime::new().expect("Unable to create Runtime");

// Enter the runtime so that `tokio::spawn` is available immediately.
let _enter = rt.enter();

// Execute the runtime in its own thread.
// The future doesn't have to do anything. In this example, it just sleeps forever.
std::thread::spawn(move || {
rt.block_on(async {
loop {
tokio::time::sleep(std::time::Duration::from_secs(3600)).await;
}
})
});

let options = eframe::NativeOptions {
drag_and_drop_support: true,
renderer: eframe::Renderer::Glow,
Expand Down Expand Up @@ -66,8 +82,9 @@ fn setup_custom_font(ctx: &egui::Context) {
ctx.set_fonts(fonts);
}

#[derive(Default)]
struct Gui {
tx: Sender<(BigFile, PathBuf)>,
rx: Receiver<(BigFile, PathBuf)>,
bigfile: Option<BigFile>,
bigfile_path: Option<PathBuf>,
resource_name: Option<Name>,
Expand All @@ -83,7 +100,10 @@ impl Gui {
cc.egui_ctx.set_pixels_per_point(1.25);
egui_extras::install_image_loaders(&cc.egui_ctx);
setup_custom_font(&cc.egui_ctx);
let (tx, rx) = std::sync::mpsc::channel();
Self {
tx,
rx,
bigfile: None,
bigfile_path: None,
resource_name: None,
Expand All @@ -98,25 +118,35 @@ impl Gui {

impl eframe::App for Gui {
fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
if let Ok((bf, path)) = self.rx.try_recv() {
self.bigfile = Some(bf);
self.bigfile_path = Some(path);
self.nicknames.clear();
self.resource_name = None;
ctx.set_cursor_icon(egui::CursorIcon::Default);
}

egui::CentralPanel::default()
.frame(egui::Frame::none().inner_margin(egui::Margin::same(0.0)))
.show(ctx, |ui| {
let menubar_response = menubar(
menubar(
ui,
frame,
ctx,
"menubar".into(),
&self.bigfile,
&self.bigfile_path,
&self.resource_name,
&mut self.nicknames,
&self.artifacts,
&self.tx,
);
if let Some((bf, path)) = menubar_response.bigfile_open {
self.bigfile = Some(bf);
self.bigfile_path = Some(path);
self.nicknames.clear();
self.resource_name = None;
}
// if let Some((bf, path)) = menubar_response.bigfile_open {
// self.bigfile = Some(bf);
// self.bigfile_path = Some(path);
// self.nicknames.clear();
// self.resource_name = None;
// }

let resource_list_response = resource_list(
ui,
Expand Down
32 changes: 22 additions & 10 deletions bff-gui/src/panels/top.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use std::sync::mpsc::Sender;
use std::sync::Mutex;
use std::{collections::HashMap, sync::Arc};

use bff::bigfile::BigFile;
use bff::class::Class;
use bff::names::Name;
use bff::platforms::Platform;
use bff::traits::TryIntoVersionPlatform;

use crate::{load_bigfile, Artifact};

#[derive(Default)]
pub struct MenubarResponse {
pub bigfile_open: Option<(BigFile, PathBuf)>,
}
use crate::Artifact;

pub fn menubar(
ui: &mut egui::Ui,
frame: &mut eframe::Frame,
ctx: &egui::Context,
id_source: egui::Id,
bigfile: &Option<BigFile>,
bigfile_path: &Option<PathBuf>,
resource_name: &Option<Name>,
nicknames: &mut HashMap<Name, String>,
artifacts: &HashMap<Name, Artifact>,
) -> MenubarResponse {
let mut response = MenubarResponse::default();
tx: &Sender<(BigFile, PathBuf)>,
) {
egui::TopBottomPanel::top("top").show_inside(ui, |ui| {
ui.horizontal(|ui| {
ui.menu_button("File", |ui| {
Expand Down Expand Up @@ -66,7 +64,9 @@ pub fn menubar(
}
}
}
response.bigfile_open = Some((load_bigfile(&path), path));
ctx.set_cursor_icon(egui::CursorIcon::Progress);
load_bf(path, tx.clone());
// response.bigfile_open = Some((load_bigfile(&path), path));
}
}
if ui
Expand Down Expand Up @@ -271,7 +271,6 @@ pub fn menubar(
});
});
});
response
}

fn write_class(path: &PathBuf, bigfile: &BigFile, resource_name: &Name) {
Expand All @@ -294,3 +293,16 @@ fn write_class(path: &PathBuf, bigfile: &BigFile, resource_name: &Name) {
)
.unwrap();
}

fn load_bf(path: PathBuf, tx: Sender<(BigFile, PathBuf)>) {
tokio::spawn(async move {
let platform = match path.extension() {
Some(extension) => extension.try_into().unwrap_or(Platform::PC),
None => Platform::PC,
};
let f = File::open(&path).unwrap();
let mut reader = bff::BufReader::new(f);
let bf = BigFile::read_platform(&mut reader, platform).unwrap();
let _ = tx.send((bf, path));
});
}

0 comments on commit d0dd084

Please sign in to comment.