emulator on android: crashes but APKs are building

This commit is contained in:
2025-06-22 03:46:42 +01:00
parent a878483923
commit c7322d8171
7 changed files with 259 additions and 118 deletions
+114
View File
@@ -13,3 +13,117 @@
)]
pub mod emulator;
use std::{
sync::{
Arc,
mpsc::{Receiver, Sender},
},
thread,
};
#[cfg(target_os = "android")]
use winit::platform::android::{EventLoopBuilderExtAndroid, activity::AndroidApp};
use crate::emulator::{
misc::rpc::RpcClient,
system::{
emulator::run_emulator,
memory::MainStore,
model::{Command, State},
processor::Processor,
},
ui::{
control_unit::ControlPanel, display::Display, editor::Editor,
interface::EmulatorUI, memory_inspector::MemoryInspector,
stack_inspector::StackInspector,
},
};
#[cfg(target_os = "android")]
#[unsafe(no_mangle)]
pub fn android_main(app: AndroidApp) -> Result<(), Box<dyn std::error::Error>> {
use crate::emulator::{config::Config, misc::rpc::get_rpc_client_or_none};
use std::path::Path;
// Initialize channels and read in configuration.
let (cmd_sender, cmd_receiver) = std::sync::mpsc::channel();
let (state_sender, state_reciever) = std::sync::mpsc::channel();
let config = Config::load(Path::new(".dsa.emulator.toml"))?;
// Setup RPC if enabled.
let (rpc_sender, rpc_reciever) = std::sync::mpsc::channel();
let rpc_client =
get_rpc_client_or_none(&config, rpc_sender, rpc_reciever)?.map(Arc::new);
setup_emulator(cmd_receiver, state_sender, rpc_client);
let ui = setup_ui(cmd_sender, state_reciever);
// Run UI.
#[allow(unused_variables)]
let options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default().with_inner_size([800.0, 600.0]),
event_loop_builder: Some(Box::new(move |builder| {
#[cfg(target_os = "android")]
builder.with_android_app(app);
})),
..Default::default()
};
eframe::run_native(
"DSA Simulator (Damn Simple Architecture 🔥)",
options,
Box::new(move |cc| {
cc.egui_ctx.set_visuals(egui::Visuals::default());
Ok(Box::new(ui))
}),
)?;
Ok(())
}
pub fn setup_emulator(
cmd_receiver: Receiver<Command>,
state_sender: Sender<State>,
rpc_client: Option<Arc<RpcClient>>,
) {
let main_store = MainStore::new();
let processor = Processor::new(Box::new(main_store), vec![]);
thread::spawn(move || {
run_emulator(&cmd_receiver, &state_sender, processor, rpc_client.as_ref());
});
}
/// Creates the [`EmulatorUI`].
#[must_use]
pub fn setup_ui(
cmd_sender: Sender<Command>,
state_reciever: Receiver<State>,
) -> EmulatorUI {
let mut ui = EmulatorUI::new(cmd_sender.clone(), state_reciever);
// Create UI modules.
let control_unit = ControlPanel::new(cmd_sender.clone());
ui.add_component(Box::new(control_unit));
let mem_inspector = MemoryInspector::new(cmd_sender.clone());
ui.add_component(Box::new(mem_inspector));
let stack_inspector = StackInspector::new();
ui.add_component(Box::new(stack_inspector));
let editor = Editor::new(cmd_sender);
ui.add_component(Box::new(editor));
let display = Display::new();
ui.add_component(Box::new(display));
let history = emulator::ui::history::History::new();
ui.add_component(Box::new(history));
ui
}