diff --git a/Cargo.lock b/Cargo.lock index f2f83d4..85fca4a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -508,6 +508,16 @@ dependencies = [ "log", ] +[[package]] +name = "bstr" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6798148dccfbff0fae41c7574d2fa8f1ef3492fba0face179de5d8d447d67b05" +dependencies = [ + "memchr", + "serde", +] + [[package]] name = "bumpalo" version = "3.12.0" @@ -885,7 +895,16 @@ version = "4.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059" dependencies = [ - "dirs-sys", + "dirs-sys 0.3.7", +] + +[[package]] +name = "dirs" +version = "5.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225" +dependencies = [ + "dirs-sys 0.4.1", ] [[package]] @@ -899,6 +918,18 @@ dependencies = [ "winapi", ] +[[package]] +name = "dirs-sys" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c" +dependencies = [ + "libc", + "option-ext", + "redox_users", + "windows-sys 0.48.0", +] + [[package]] name = "dirs-sys-next" version = "0.1.2" @@ -1455,6 +1486,20 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" +[[package]] +name = "globset" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "759c97c1e17c55525b57192c06a267cda0ac5210b222d6b82189a2338fa1c13d" +dependencies = [ + "aho-corasick", + "bstr", + "fnv", + "log", + "regex", + "serde", +] + [[package]] name = "glow" version = "0.12.1" @@ -2421,11 +2466,13 @@ dependencies = [ "console_error_panic_hook", "const_format", "cwdemangle", + "dirs 5.0.1", "eframe", "egui", "egui_extras", "exec", "flagset", + "globset", "log", "memmap2 0.7.1", "notify", @@ -2438,6 +2485,8 @@ dependencies = [ "rfd", "self_update", "serde", + "serde_json", + "serde_yaml", "tempfile", "thiserror", "time", @@ -2519,6 +2568,12 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "option-ext" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" + [[package]] name = "orbclient" version = "0.3.45" @@ -3131,9 +3186,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.100" +version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f1e14e89be7aa4c4b78bdbdc9eb5bf8517829a600ae8eaa39a6e1d960b5185c" +checksum = "076066c5f1078eac5b722a31827a8832fe108bed65dfa75e233c89f8206e976c" dependencies = [ "itoa", "ryu", @@ -3172,6 +3227,19 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_yaml" +version = "0.9.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a49e178e4452f45cb61d0cd8cebc1b0fafd3e41929e996cef79aa3aca91f574" +dependencies = [ + "indexmap 2.0.0", + "itoa", + "ryu", + "serde", + "unsafe-libyaml", +] + [[package]] name = "sha1" version = "0.10.5" @@ -3701,6 +3769,12 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" +[[package]] +name = "unsafe-libyaml" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f28467d3e1d3c6586d8f25fa243f544f5800fec42d97032474e17222c2b75cfa" + [[package]] name = "untrusted" version = "0.7.1" @@ -3965,7 +4039,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "579cc485bd5ce5bfa0d738e4921dd0b956eca9800be1fd2e5257ebe95bc4617e" dependencies = [ "core-foundation", - "dirs", + "dirs 4.0.0", "jni", "log", "ndk-context", diff --git a/Cargo.toml b/Cargo.toml index 610ee15..d2d9794 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,14 +23,17 @@ wgpu = ["eframe/wgpu"] [dependencies] anyhow = "1.0.71" +byteorder = "1.4.3" bytes = "1.4.0" cfg-if = "1.0.0" const_format = "0.2.31" cwdemangle = "0.1.5" +dirs = "5.0.1" eframe = { version = "0.22.0", features = ["persistence"] } egui = "0.22.0" egui_extras = "0.22.0" flagset = "0.4.3" +globset = { version = "0.4.13", features = ["serde1"] } log = "0.4.19" memmap2 = "0.7.1" notify = "6.0.1" @@ -40,12 +43,13 @@ ppc750cl = { git = "https://github.com/terorie/ppc750cl", rev = "9ae36eef34aa6d7 rabbitizer = "1.7.4" rfd = { version = "0.11.4" } #, default-features = false, features = ['xdg-portal'] serde = { version = "1", features = ["derive"] } +serde_json = "1.0.104" +serde_yaml = "0.9.25" tempfile = "3.6.0" thiserror = "1.0.41" time = { version = "0.3.22", features = ["formatting", "local-offset"] } toml = "0.7.6" twox-hash = "1.6.3" -byteorder = "1.4.3" # For Linux static binaries, use rustls [target.'cfg(target_os = "linux")'.dependencies] diff --git a/src/app.rs b/src/app.rs index 700f307..760d836 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,6 +1,5 @@ use std::{ default::Default, - ffi::OsStr, path::{Path, PathBuf}, rc::Rc, sync::{ @@ -11,17 +10,24 @@ use std::{ }; use egui::{Color32, FontFamily, FontId, TextStyle}; +use globset::{Glob, GlobSet, GlobSetBuilder}; use notify::{RecursiveMode, Watcher}; use time::{OffsetDateTime, UtcOffset}; use crate::{ + config::{build_globset, load_project_config, ProjectUnit, ProjectUnitNode, CONFIG_FILENAMES}, jobs::{ check_update::{queue_check_update, CheckUpdateResult}, objdiff::{queue_build, BuildStatus, ObjDiffResult}, Job, JobResult, JobState, JobStatus, }, views::{ - config::config_ui, data_diff::data_diff_ui, function_diff::function_diff_ui, jobs::jobs_ui, + appearance::{appearance_window, DEFAULT_COLOR_ROTATION}, + config::{config_ui, project_window}, + data_diff::data_diff_ui, + demangle::demangle_window, + function_diff::function_diff_ui, + jobs::jobs_ui, symbol_diff::symbol_diff_ui, }, }; @@ -49,18 +55,6 @@ pub struct DiffConfig { // pub mapped_symbols: HashMap, } -const DEFAULT_COLOR_ROTATION: [Color32; 9] = [ - Color32::from_rgb(255, 0, 255), - Color32::from_rgb(0, 255, 255), - Color32::from_rgb(0, 128, 0), - Color32::from_rgb(255, 0, 0), - Color32::from_rgb(255, 255, 0), - Color32::from_rgb(255, 192, 203), - Color32::from_rgb(0, 0, 255), - Color32::from_rgb(0, 255, 0), - Color32::from_rgb(213, 138, 138), -]; - #[derive(serde::Deserialize, serde::Serialize)] #[serde(default)] pub struct ViewConfig { @@ -123,12 +117,16 @@ pub struct ViewState { #[serde(skip)] pub current_view: View, #[serde(skip)] - pub show_config: bool, + pub show_view_config: bool, + #[serde(skip)] + pub show_project_config: bool, #[serde(skip)] pub show_demangle: bool, #[serde(skip)] pub demangle_text: String, #[serde(skip)] + pub watch_pattern_text: String, + #[serde(skip)] pub diff_config: DiffConfig, #[serde(skip)] pub search: String, @@ -149,9 +147,11 @@ impl Default for ViewState { highlighted_symbol: None, selected_symbol: None, current_view: Default::default(), - show_config: false, + show_view_config: false, + show_project_config: false, show_demangle: false, demangle_text: String::new(), + watch_pattern_text: String::new(), diff_config: Default::default(), search: Default::default(), utc_offset: UtcOffset::UTC, @@ -162,7 +162,7 @@ impl Default for ViewState { } } -#[derive(Default, Clone, serde::Deserialize, serde::Serialize)] +#[derive(Clone, serde::Deserialize, serde::Serialize)] #[serde(default)] pub struct AppConfig { pub custom_make: Option, @@ -179,21 +179,53 @@ pub struct AppConfig { // Whole binary pub left_obj: Option, pub right_obj: Option, + #[serde(skip)] - pub project_dir_change: bool, + pub watcher_change: bool, + pub watcher_enabled: bool, #[serde(skip)] pub queue_update_check: bool, pub auto_update_check: bool, + // Project config + #[serde(skip)] + pub config_change: bool, + #[serde(skip)] + pub watch_patterns: Vec, + #[serde(skip)] + pub load_error: Option, + #[serde(skip)] + pub units: Vec, + #[serde(skip)] + pub unit_nodes: Vec, + #[serde(skip)] + pub config_window_open: bool, } -#[derive(Default, Clone, serde::Deserialize)] -#[serde(default)] -pub struct ProjectConfig { - pub custom_make: Option, - pub project_dir: Option, - pub target_obj_dir: Option, - pub base_obj_dir: Option, - pub build_target: bool, +impl Default for AppConfig { + fn default() -> Self { + Self { + custom_make: None, + available_wsl_distros: None, + selected_wsl_distro: None, + project_dir: None, + target_obj_dir: None, + base_obj_dir: None, + obj_path: None, + build_target: false, + left_obj: None, + right_obj: None, + config_change: false, + watcher_change: false, + watcher_enabled: true, + queue_update_check: false, + auto_update_check: false, + watch_patterns: vec![], + load_error: None, + units: vec![], + unit_nodes: vec![], + config_window_open: false, + } + } } /// We derive Deserialize/Serialize so we can persist app state on shutdown. @@ -206,6 +238,8 @@ pub struct App { #[serde(skip)] modified: Arc, #[serde(skip)] + config_modified: Arc, + #[serde(skip)] watcher: Option, #[serde(skip)] relaunch_path: Rc>>, @@ -219,6 +253,7 @@ impl Default for App { view_state: ViewState::default(), config: Arc::new(Default::default()), modified: Arc::new(Default::default()), + config_modified: Arc::new(Default::default()), watcher: None, relaunch_path: Default::default(), should_relaunch: false, @@ -244,7 +279,9 @@ impl App { let mut app: App = eframe::get_value(storage, eframe::APP_KEY).unwrap_or_default(); let mut config: AppConfig = eframe::get_value(storage, CONFIG_KEY).unwrap_or_default(); if config.project_dir.is_some() { - config.project_dir_change = true; + config.config_change = true; + config.watcher_change = true; + app.modified.store(true, Ordering::Relaxed); } config.queue_update_check = config.auto_update_check; app.config = Arc::new(RwLock::new(config)); @@ -313,15 +350,15 @@ impl eframe::App for App { egui::TopBottomPanel::top("top_panel").show(ctx, |ui| { egui::menu::bar(ui, |ui| { ui.menu_button("File", |ui| { - if ui.button("Show config").clicked() { - view_state.show_config = !view_state.show_config; + if ui.button("Appearance…").clicked() { + view_state.show_view_config = !view_state.show_view_config; } if ui.button("Quit").clicked() { frame.close(); } }); ui.menu_button("Tools", |ui| { - if ui.button("Demangle").clicked() { + if ui.button("Demangle…").clicked() { view_state.show_demangle = !view_state.show_demangle; } }); @@ -367,69 +404,9 @@ impl eframe::App for App { }); } - egui::Window::new("Config").open(&mut view_state.show_config).show(ctx, |ui| { - egui::ComboBox::from_label("Theme") - .selected_text(format!("{:?}", view_state.view_config.theme)) - .show_ui(ui, |ui| { - ui.selectable_value( - &mut view_state.view_config.theme, - eframe::Theme::Dark, - "Dark", - ); - ui.selectable_value( - &mut view_state.view_config.theme, - eframe::Theme::Light, - "Light", - ); - }); - ui.label("UI font:"); - egui::introspection::font_id_ui(ui, &mut view_state.view_config.ui_font); - ui.separator(); - ui.label("Code font:"); - egui::introspection::font_id_ui(ui, &mut view_state.view_config.code_font); - ui.separator(); - ui.label("Diff colors:"); - if ui.button("Reset").clicked() { - view_state.view_config.diff_colors = DEFAULT_COLOR_ROTATION.to_vec(); - } - let mut remove_at: Option = None; - let num_colors = view_state.view_config.diff_colors.len(); - for (idx, color) in view_state.view_config.diff_colors.iter_mut().enumerate() { - ui.horizontal(|ui| { - ui.color_edit_button_srgba(color); - if num_colors > 1 && ui.small_button("-").clicked() { - remove_at = Some(idx); - } - }); - } - if let Some(idx) = remove_at { - view_state.view_config.diff_colors.remove(idx); - } - if ui.small_button("+").clicked() { - view_state.view_config.diff_colors.push(Color32::BLACK); - } - }); - - egui::Window::new("Demangle").open(&mut view_state.show_demangle).show(ctx, |ui| { - ui.text_edit_singleline(&mut view_state.demangle_text); - ui.add_space(10.0); - if let Some(demangled) = - cwdemangle::demangle(&view_state.demangle_text, &Default::default()) - { - ui.scope(|ui| { - ui.style_mut().override_text_style = Some(TextStyle::Monospace); - ui.colored_label(view_state.view_config.replace_color, &demangled); - }); - if ui.button("Copy").clicked() { - ui.output_mut(|output| output.copied_text = demangled); - } - } else { - ui.scope(|ui| { - ui.style_mut().override_text_style = Some(TextStyle::Monospace); - ui.colored_label(view_state.view_config.replace_color, "[invalid]"); - }); - } - }); + project_window(ctx, config, view_state); + appearance_window(ctx, view_state); + demangle_window(ctx, view_state); // Windows + request_repaint_after breaks dialogs: // https://github.com/emilk/egui/issues/2003 @@ -535,15 +512,42 @@ impl eframe::App for App { } if let Ok(mut config) = self.config.write() { - if config.project_dir_change { + let config = &mut *config; + + if self.config_modified.load(Ordering::Relaxed) { + self.config_modified.store(false, Ordering::Relaxed); + config.config_change = true; + } + + if config.config_change { + config.config_change = false; + if let Err(e) = load_project_config(config) { + log::error!("Failed to load project config: {e}"); + config.load_error = Some(format!("{e}")); + } + } + + if config.watcher_change { drop(self.watcher.take()); + if let Some(project_dir) = &config.project_dir { - match create_watcher(self.modified.clone(), project_dir) { - Ok(watcher) => self.watcher = Some(watcher), - Err(e) => log::error!("Failed to create watcher: {e}"), + if !config.watch_patterns.is_empty() { + match build_globset(&config.watch_patterns) + .map_err(anyhow::Error::new) + .and_then(|globset| { + create_watcher( + self.modified.clone(), + self.config_modified.clone(), + project_dir, + globset, + ) + .map_err(anyhow::Error::new) + }) { + Ok(watcher) => self.watcher = Some(watcher), + Err(e) => log::error!("Failed to create watcher: {e}"), + } } - config.project_dir_change = false; - self.modified.store(true, Ordering::Relaxed); + config.watcher_change = false; } } @@ -572,22 +576,27 @@ impl eframe::App for App { fn create_watcher( modified: Arc, + config_modified: Arc, project_dir: &Path, + patterns: GlobSet, ) -> notify::Result { + let mut config_patterns = GlobSetBuilder::new(); + for filename in CONFIG_FILENAMES { + config_patterns.add(Glob::new(&format!("**/{filename}")).unwrap()); + } + let config_patterns = config_patterns.build().unwrap(); + let mut watcher = notify::recommended_watcher(move |res: notify::Result| match res { Ok(event) => { if matches!(event.kind, notify::EventKind::Modify(..)) { - let watch_extensions = &[ - Some(OsStr::new("c")), - Some(OsStr::new("cp")), - Some(OsStr::new("cpp")), - Some(OsStr::new("h")), - Some(OsStr::new("hpp")), - Some(OsStr::new("s")), - ]; - if event.paths.iter().any(|p| watch_extensions.contains(&p.extension())) { - modified.store(true, Ordering::Relaxed); + for path in &event.paths { + if config_patterns.is_match(path) { + config_modified.store(true, Ordering::Relaxed); + } + if patterns.is_match(path) { + modified.store(true, Ordering::Relaxed); + } } } } diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..356667d --- /dev/null +++ b/src/config.rs @@ -0,0 +1,123 @@ +use std::{ + fs::File, + path::{Component, Path, PathBuf}, +}; + +use anyhow::{Context, Result}; +use globset::{Glob, GlobSet, GlobSetBuilder}; + +use crate::app::AppConfig; + +#[derive(Default, Clone, serde::Deserialize)] +#[serde(default)] +pub struct ProjectConfig { + pub custom_make: Option, + pub target_dir: Option, + pub base_dir: Option, + pub build_target: bool, + pub watch_patterns: Vec, + pub units: Vec, +} + +#[derive(Default, Clone, serde::Deserialize)] +pub struct ProjectUnit { + pub name: String, + pub path: PathBuf, + #[serde(default)] + pub reverse_fn_order: bool, +} + +#[derive(Clone)] +pub enum ProjectUnitNode { + File(String, ProjectUnit), + Dir(String, Vec), +} + +fn find_dir<'a>(name: &str, nodes: &'a mut Vec) -> &'a mut Vec { + if let Some(index) = nodes + .iter() + .position(|node| matches!(node, ProjectUnitNode::Dir(dir_name, _) if dir_name == name)) + { + if let ProjectUnitNode::Dir(_, children) = &mut nodes[index] { + return children; + } + } else { + nodes.push(ProjectUnitNode::Dir(name.to_string(), vec![])); + if let Some(ProjectUnitNode::Dir(_, children)) = nodes.last_mut() { + return children; + } + } + unreachable!(); +} + +fn build_nodes(units: &[ProjectUnit]) -> Vec { + let mut nodes = vec![]; + for unit in units { + let mut out_nodes = &mut nodes; + let path = Path::new(&unit.name); + if let Some(parent) = path.parent() { + for component in parent.components() { + if let Component::Normal(name) = component { + let name = name.to_str().unwrap(); + out_nodes = find_dir(name, out_nodes); + } + } + } + let filename = path.file_name().unwrap().to_str().unwrap().to_string(); + out_nodes.push(ProjectUnitNode::File(filename, unit.clone())); + } + nodes +} + +pub const CONFIG_FILENAMES: [&str; 3] = ["objdiff.yml", "objdiff.yaml", "objdiff.json"]; + +pub fn load_project_config(config: &mut AppConfig) -> Result<()> { + let Some(project_dir) = &config.project_dir else { + return Ok(()); + }; + if let Some(result) = try_project_config(project_dir) { + let project_config = result?; + config.custom_make = project_config.custom_make; + config.target_obj_dir = project_config.target_dir.map(|p| project_dir.join(p)); + config.base_obj_dir = project_config.base_dir.map(|p| project_dir.join(p)); + config.build_target = project_config.build_target; + config.watch_patterns = project_config.watch_patterns; + config.watcher_change = true; + config.units = project_config.units; + config.unit_nodes = build_nodes(&config.units); + } + Ok(()) +} + +fn try_project_config(dir: &Path) -> Option> { + for filename in CONFIG_FILENAMES.iter() { + let config_path = dir.join(filename); + if config_path.is_file() { + return match filename.contains("json") { + true => Some(read_json_config(&config_path)), + false => Some(read_yml_config(&config_path)), + }; + } + } + None +} + +fn read_yml_config(config_path: &Path) -> Result { + let mut reader = File::open(config_path) + .with_context(|| format!("Failed to open config file '{}'", config_path.display()))?; + Ok(serde_yaml::from_reader(&mut reader)?) +} + +fn read_json_config(config_path: &Path) -> Result { + let mut reader = File::open(config_path) + .with_context(|| format!("Failed to open config file '{}'", config_path.display()))?; + Ok(serde_json::from_reader(&mut reader)?) +} + +pub fn build_globset(vec: &[Glob]) -> std::result::Result { + let mut builder = GlobSetBuilder::new(); + for glob in vec { + builder.add(glob.clone()); + } + builder.build() +} diff --git a/src/lib.rs b/src/lib.rs index 6d11985..fb48b8f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ pub use app::App; mod app; +mod config; mod diff; mod editops; mod jobs; diff --git a/src/obj/elf.rs b/src/obj/elf.rs index 7c064df..0c8c610 100644 --- a/src/obj/elf.rs +++ b/src/obj/elf.rs @@ -181,7 +181,7 @@ fn find_section_symbol( fn relocations_by_section( arch: ObjArchitecture, obj_file: &File<'_>, - section: &mut ObjSection, + section: &ObjSection, ) -> Result> { let obj_section = obj_file.section_by_index(SectionIndex(section.index))?; let mut relocations = Vec::::new(); diff --git a/src/views/appearance.rs b/src/views/appearance.rs new file mode 100644 index 0000000..b7f2d9e --- /dev/null +++ b/src/views/appearance.rs @@ -0,0 +1,56 @@ +use egui::Color32; + +use crate::app::ViewState; + +pub const DEFAULT_COLOR_ROTATION: [Color32; 9] = [ + Color32::from_rgb(255, 0, 255), + Color32::from_rgb(0, 255, 255), + Color32::from_rgb(0, 128, 0), + Color32::from_rgb(255, 0, 0), + Color32::from_rgb(255, 255, 0), + Color32::from_rgb(255, 192, 203), + Color32::from_rgb(0, 0, 255), + Color32::from_rgb(0, 255, 0), + Color32::from_rgb(213, 138, 138), +]; + +pub fn appearance_window(ctx: &egui::Context, view_state: &mut ViewState) { + egui::Window::new("Appearance").open(&mut view_state.show_view_config).show(ctx, |ui| { + egui::ComboBox::from_label("Theme") + .selected_text(format!("{:?}", view_state.view_config.theme)) + .show_ui(ui, |ui| { + ui.selectable_value(&mut view_state.view_config.theme, eframe::Theme::Dark, "Dark"); + ui.selectable_value( + &mut view_state.view_config.theme, + eframe::Theme::Light, + "Light", + ); + }); + ui.label("UI font:"); + egui::introspection::font_id_ui(ui, &mut view_state.view_config.ui_font); + ui.separator(); + ui.label("Code font:"); + egui::introspection::font_id_ui(ui, &mut view_state.view_config.code_font); + ui.separator(); + ui.label("Diff colors:"); + if ui.button("Reset").clicked() { + view_state.view_config.diff_colors = DEFAULT_COLOR_ROTATION.to_vec(); + } + let mut remove_at: Option = None; + let num_colors = view_state.view_config.diff_colors.len(); + for (idx, color) in view_state.view_config.diff_colors.iter_mut().enumerate() { + ui.horizontal(|ui| { + ui.color_edit_button_srgba(color); + if num_colors > 1 && ui.small_button("-").clicked() { + remove_at = Some(idx); + } + }); + } + if let Some(idx) = remove_at { + view_state.view_config.diff_colors.remove(idx); + } + if ui.small_button("+").clicked() { + view_state.view_config.diff_colors.push(Color32::BLACK); + } + }); +} diff --git a/src/views/config.rs b/src/views/config.rs index 8995da1..c026fca 100644 --- a/src/views/config.rs +++ b/src/views/config.rs @@ -1,19 +1,32 @@ #[cfg(windows)] use std::string::FromUtf16Error; -use std::sync::{Arc, RwLock}; +use std::{ + path::PathBuf, + sync::{Arc, RwLock}, +}; #[cfg(windows)] use anyhow::{Context, Result}; use const_format::formatcp; -use egui::output::OpenUrl; +use egui::{ + output::OpenUrl, text::LayoutJob, CollapsingHeader, FontFamily, FontId, RichText, + SelectableLabel, TextFormat, Widget, +}; +use globset::Glob; use self_update::cargo_crate_version; use crate::{ - app::{AppConfig, DiffKind, ViewState}, + app::{AppConfig, DiffKind, ViewConfig, ViewState}, + config::{ProjectUnit, ProjectUnitNode}, jobs::{bindiff::queue_bindiff, objdiff::queue_build, update::queue_update}, update::RELEASE_URL, }; +const DEFAULT_WATCH_PATTERNS: &[&str] = &[ + "*.c", "*.cp", "*.cpp", "*.cxx", "*.h", "*.hp", "*.hpp", "*.hxx", "*.s", "*.S", "*.asm", + "*.inc", "*.py", "*.yml", "*.txt", "*.json", +]; + #[cfg(windows)] fn process_utf16(bytes: &[u8]) -> Result { let u16_bytes: Vec = bytes @@ -50,19 +63,18 @@ fn fetch_wsl2_distros() -> Vec { pub fn config_ui(ui: &mut egui::Ui, config: &Arc>, view_state: &mut ViewState) { let mut config_guard = config.write().unwrap(); let AppConfig { - custom_make, available_wsl_distros, selected_wsl_distro, - project_dir, target_obj_dir, base_obj_dir, obj_path, - build_target, left_obj, right_obj, - project_dir_change, queue_update_check, auto_update_check, + units, + unit_nodes, + .. } = &mut *config_guard; ui.heading("Updates"); @@ -106,10 +118,9 @@ pub fn config_ui(ui: &mut egui::Ui, config: &Arc>, view_state: } ui.separator(); - ui.heading("Build config"); - #[cfg(windows)] { + ui.heading("Build"); if available_wsl_distros.is_none() { *available_wsl_distros = Some(fetch_wsl2_distros()); } @@ -121,6 +132,7 @@ pub fn config_ui(ui: &mut egui::Ui, config: &Arc>, view_state: ui.selectable_value(selected_wsl_distro, Some(distro.clone()), distro); } }); + ui.separator(); } #[cfg(not(windows))] { @@ -128,96 +140,58 @@ pub fn config_ui(ui: &mut egui::Ui, config: &Arc>, view_state: let _ = selected_wsl_distro; } - ui.label("Custom make program:"); - let mut custom_make_str = custom_make.clone().unwrap_or_default(); - if ui.text_edit_singleline(&mut custom_make_str).changed() { - if custom_make_str.is_empty() { - *custom_make = None; - } else { - *custom_make = Some(custom_make_str); + ui.horizontal(|ui| { + ui.heading("Project"); + if ui.button(RichText::new("Settings")).clicked() { + view_state.show_project_config = true; } - } - - ui.separator(); - - ui.heading("Project config"); + }); if view_state.diff_kind == DiffKind::SplitObj { - if ui.button("Select project dir").clicked() { - if let Some(path) = rfd::FileDialog::new().pick_folder() { - *project_dir = Some(path); - *project_dir_change = true; - *target_obj_dir = None; - *base_obj_dir = None; - *obj_path = None; - } - } - if let Some(dir) = project_dir { - ui.label(dir.to_string_lossy()); - } - - ui.separator(); - - if let Some(project_dir) = project_dir { - if ui.button("Select target build dir").clicked() { - if let Some(path) = rfd::FileDialog::new().set_directory(&project_dir).pick_folder() - { - *target_obj_dir = Some(path); - *obj_path = None; - } - } - if let Some(dir) = target_obj_dir { - ui.label(dir.to_string_lossy()); - } - ui.checkbox(build_target, "Build target"); - - ui.separator(); - - if ui.button("Select base build dir").clicked() { - if let Some(path) = rfd::FileDialog::new().set_directory(&project_dir).pick_folder() - { - *base_obj_dir = Some(path); - *obj_path = None; - } - } - if let Some(dir) = base_obj_dir { - ui.label(dir.to_string_lossy()); - } - - ui.separator(); - } - if let (Some(base_dir), Some(target_dir)) = (base_obj_dir, target_obj_dir) { - if ui.button("Select obj").clicked() { - if let Some(path) = rfd::FileDialog::new() - .set_directory(&target_dir) - .add_filter("Object file", &["o", "elf"]) - .pick_file() - { - let mut new_build_obj: Option = None; - if let Ok(obj_path) = path.strip_prefix(&base_dir) { - new_build_obj = Some(obj_path.display().to_string()); - } else if let Ok(obj_path) = path.strip_prefix(&target_dir) { - new_build_obj = Some(obj_path.display().to_string()); - } - if let Some(new_build_obj) = new_build_obj { - *obj_path = Some(new_build_obj); - view_state - .jobs - .push(queue_build(config.clone(), view_state.diff_config.clone())); + let mut new_build_obj = obj_path.clone(); + if units.is_empty() { + if ui.button("Select obj").clicked() { + if let Some(path) = rfd::FileDialog::new() + .set_directory(&target_dir) + .add_filter("Object file", &["o", "elf"]) + .pick_file() + { + if let Ok(obj_path) = path.strip_prefix(&base_dir) { + new_build_obj = Some(obj_path.display().to_string()); + } else if let Ok(obj_path) = path.strip_prefix(&target_dir) { + new_build_obj = Some(obj_path.display().to_string()); + } } } - } - if let Some(obj) = obj_path { - ui.label(&*obj); - if ui.button("Build").clicked() { - view_state - .jobs - .push(queue_build(config.clone(), view_state.diff_config.clone())); + if let Some(obj) = obj_path { + ui.label(&*obj); } + } else { + CollapsingHeader::new(RichText::new("Objects").font(FontId { + size: view_state.view_config.ui_font.size, + family: view_state.view_config.code_font.family.clone(), + })) + .default_open(true) + .show(ui, |ui| { + for node in unit_nodes { + display_node(ui, &mut new_build_obj, node, &view_state.view_config); + } + }); } - ui.separator(); + let mut build = false; + if new_build_obj != *obj_path { + *obj_path = new_build_obj; + // TODO apply reverse_fn_order + build = true; + } + if obj_path.is_some() && ui.button("Build").clicked() { + build = true; + } + if build { + view_state.jobs.push(queue_build(config.clone(), view_state.diff_config.clone())); + } } } else if view_state.diff_kind == DiffKind::WholeBinary { if ui.button("Select left obj").clicked() { @@ -249,6 +223,316 @@ pub fn config_ui(ui: &mut egui::Ui, config: &Arc>, view_state: } } - ui.checkbox(&mut view_state.view_config.reverse_fn_order, "Reverse function order (deferred)"); + // ui.checkbox(&mut view_state.view_config.reverse_fn_order, "Reverse function order (deferred)"); ui.separator(); } + +fn display_unit( + ui: &mut egui::Ui, + obj_path: &mut Option, + name: &str, + unit: &ProjectUnit, + view_config: &ViewConfig, +) { + let path_string = unit.path.to_string_lossy().to_string(); + let selected = matches!(obj_path, Some(path) if path == &path_string); + if SelectableLabel::new( + selected, + RichText::new(name).font(FontId { + size: view_config.ui_font.size, + family: view_config.code_font.family.clone(), + }), + ) + .ui(ui) + .clicked() + { + *obj_path = Some(path_string); + } +} + +fn display_node( + ui: &mut egui::Ui, + obj_path: &mut Option, + node: &ProjectUnitNode, + view_config: &ViewConfig, +) { + match node { + ProjectUnitNode::File(name, unit) => { + display_unit(ui, obj_path, name, unit, view_config); + } + ProjectUnitNode::Dir(name, children) => { + CollapsingHeader::new(RichText::new(name).font(FontId { + size: view_config.ui_font.size, + family: view_config.code_font.family.clone(), + })) + .default_open(false) + .show(ui, |ui| { + for node in children { + display_node(ui, obj_path, node, view_config); + } + }); + } + } +} + +const HELP_ICON: &str = "ℹ"; + +fn subheading(ui: &mut egui::Ui, text: &str, view_config: &ViewConfig) { + ui.label( + RichText::new(text).size(view_config.ui_font.size).color(view_config.emphasized_text_color), + ); +} + +pub fn project_window( + ctx: &egui::Context, + config: &Arc>, + view_state: &mut ViewState, +) { + let mut config_guard = config.write().unwrap(); + let AppConfig { + custom_make, + project_dir, + target_obj_dir, + base_obj_dir, + obj_path, + build_target, + config_change, + watcher_change, + watcher_enabled, + watch_patterns, + load_error, + .. + } = &mut *config_guard; + + egui::Window::new("Project").open(&mut view_state.show_project_config).show(ctx, |ui| { + let text_format = TextFormat::simple( + view_state.view_config.ui_font.clone(), + view_state.view_config.text_color, + ); + let code_format = TextFormat::simple( + FontId { + size: view_state.view_config.ui_font.size, + family: view_state.view_config.code_font.family.clone(), + }, + view_state.view_config.emphasized_text_color, + ); + + fn pick_folder_ui( + ui: &mut egui::Ui, + dir: &mut Option, + label: &str, + tooltip: impl FnOnce(&mut egui::Ui), + clicked: impl FnOnce(&mut Option), + view_config: &ViewConfig, + ) { + ui.horizontal(|ui| { + subheading(ui, label, view_config); + ui.link(HELP_ICON).on_hover_ui(tooltip); + if ui.button("Select").clicked() { + clicked(dir); + } + }); + if let Some(dir) = dir { + if let Some(home) = dirs::home_dir() { + if let Ok(rel) = dir.strip_prefix(&home) { + ui.label(RichText::new(format!("~/{}", rel.display())).color(view_config.replace_color).family(FontFamily::Monospace)); + return; + } + } + ui.label(RichText::new(format!("{}", dir.display())).color(view_config.replace_color).family(FontFamily::Monospace)); + } else { + ui.label(RichText::new("[none]").color(view_config.delete_color).family(FontFamily::Monospace)); + } + } + + if view_state.diff_kind == DiffKind::SplitObj { + pick_folder_ui( + ui, + project_dir, + "Project directory", + |ui| { + let mut job = LayoutJob::default(); + job.append( + "The root project directory.\n\n", + 0.0, + text_format.clone() + ); + job.append( + "If a configuration file exists, it will be loaded automatically.", + 0.0, + text_format.clone(), + ); + ui.label(job); + }, + |project_dir| { + if let Some(path) = rfd::FileDialog::new().pick_folder() { + *project_dir = Some(path); + *config_change = true; + *watcher_change = true; + *target_obj_dir = None; + *base_obj_dir = None; + *obj_path = None; + } + }, + &view_state.view_config, + ); + ui.separator(); + + ui.horizontal(|ui| { + subheading(ui, "Custom make program", &view_state.view_config); + ui.link(HELP_ICON).on_hover_ui(|ui| { + let mut job = LayoutJob::default(); + job.append("By default, objdiff will build with ", 0.0, text_format.clone()); + job.append("make", 0.0, code_format.clone()); + job.append( + ".\nIf the project uses a different build system (e.g. ", + 0.0, + text_format.clone(), + ); + job.append("ninja", 0.0, code_format.clone()); + job.append( + "), specify it here.\nThe program must be in your ", + 0.0, + text_format.clone(), + ); + job.append("PATH", 0.0, code_format.clone()); + job.append(".", 0.0, text_format.clone()); + ui.label(job); + }); + }); + let mut custom_make_str = custom_make.clone().unwrap_or_default(); + if ui.text_edit_singleline(&mut custom_make_str).changed() { + if custom_make_str.is_empty() { + *custom_make = None; + } else { + *custom_make = Some(custom_make_str); + } + } + ui.separator(); + + if let Some(project_dir) = project_dir { + pick_folder_ui( + ui, + target_obj_dir, + "Target build directory", + |ui| { + let mut job = LayoutJob::default(); + job.append( + "This contains the \"target\" or \"expected\" objects, which are the intended result of the match.\n\n", + 0.0, + text_format.clone(), + ); + job.append( + "These are usually created by the project's build system or assembled.", + 0.0, + text_format.clone(), + ); + ui.label(job); + }, + |target_obj_dir| { + if let Some(path) = + rfd::FileDialog::new().set_directory(&project_dir).pick_folder() + { + *target_obj_dir = Some(path); + *obj_path = None; + } + }, + &view_state.view_config, + ); + ui.checkbox(build_target, "Build target objects").on_hover_ui(|ui| { + let mut job = LayoutJob::default(); + job.append("Tells the build system to produce the target object.\n", 0.0, text_format.clone()); + job.append("For example, this would call ", 0.0, text_format.clone()); + job.append("make path/to/target.o", 0.0, code_format.clone()); + job.append(".\n\n", 0.0, text_format.clone()); + job.append("This is useful if the target objects are not already built\n", 0.0, text_format.clone()); + job.append("or if they can change based on project configuration,\n", 0.0, text_format.clone()); + job.append("but requires that the build system is configured correctly.", 0.0, text_format.clone()); + ui.label(job); + }); + ui.separator(); + + pick_folder_ui( + ui, + base_obj_dir, + "Base build directory", + |ui| { + let mut job = LayoutJob::default(); + job.append( + "This contains the objects built from your decompiled code.", + 0.0, + text_format.clone(), + ); + ui.label(job); + }, + |base_obj_dir| { + if let Some(path) = + rfd::FileDialog::new().set_directory(&project_dir).pick_folder() + { + *base_obj_dir = Some(path); + *obj_path = None; + } + }, + &view_state.view_config, + ); + ui.separator(); + } + + subheading(ui, "Watch settings", &view_state.view_config); + let response = ui.checkbox(watcher_enabled, "Rebuild on changes").on_hover_ui(|ui| { + let mut job = LayoutJob::default(); + job.append("Automatically re-run the build & diff when files change.", 0.0, text_format.clone()); + ui.label(job); + }); + if response.changed() { + *watcher_change = true; + }; + + ui.horizontal(|ui| { + ui.label(RichText::new("File Patterns").color(view_state.view_config.text_color)); + if ui.button("Reset").clicked() { + *watch_patterns = DEFAULT_WATCH_PATTERNS.iter().map(|s| Glob::new(s).unwrap()).collect(); + *watcher_change = true; + } + }); + let mut remove_at: Option = None; + for (idx, glob) in watch_patterns.iter().enumerate() { + ui.horizontal(|ui| { + ui.label(RichText::new(format!("{}", glob)) + .color(view_state.view_config.text_color) + .family(FontFamily::Monospace)); + if ui.small_button("-").clicked() { + remove_at = Some(idx); + } + }); + } + if let Some(idx) = remove_at { + watch_patterns.remove(idx); + *watcher_change = true; + } + ui.horizontal(|ui| { + egui::TextEdit::singleline(&mut view_state.watch_pattern_text) + .desired_width(100.0) + .show(ui); + if ui.small_button("+").clicked() { + if let Ok(glob) = Glob::new(&view_state.watch_pattern_text) { + watch_patterns.push(glob); + *watcher_change = true; + view_state.watch_pattern_text.clear(); + } + } + }); + } + }); + + if let Some(error) = &load_error { + let mut open = true; + egui::Window::new("Error").open(&mut open).show(ctx, |ui| { + ui.label("Failed to load project config:"); + ui.colored_label(view_state.view_config.delete_color, error); + }); + if !open { + *load_error = None; + } + } +} diff --git a/src/views/data_diff.rs b/src/views/data_diff.rs index 301dbbf..442dc21 100644 --- a/src/views/data_diff.rs +++ b/src/views/data_diff.rs @@ -213,7 +213,7 @@ pub fn data_diff_ui(ui: &mut egui::Ui, view_state: &mut ViewState) -> bool { ui.style_mut().override_text_style = Some(egui::TextStyle::Monospace); ui.style_mut().wrap = Some(false); if view_state.jobs.iter().any(|job| job.job_type == Job::ObjDiff) { - ui.label("Building..."); + ui.colored_label(view_state.view_config.replace_color, "Building…"); } else { ui.label("Last built:"); let format = diff --git a/src/views/demangle.rs b/src/views/demangle.rs new file mode 100644 index 0000000..88f268b --- /dev/null +++ b/src/views/demangle.rs @@ -0,0 +1,26 @@ +use egui::TextStyle; + +use crate::app::ViewState; + +pub fn demangle_window(ctx: &egui::Context, view_state: &mut ViewState) { + egui::Window::new("Demangle").open(&mut view_state.show_demangle).show(ctx, |ui| { + ui.text_edit_singleline(&mut view_state.demangle_text); + ui.add_space(10.0); + if let Some(demangled) = + cwdemangle::demangle(&view_state.demangle_text, &Default::default()) + { + ui.scope(|ui| { + ui.style_mut().override_text_style = Some(TextStyle::Monospace); + ui.colored_label(view_state.view_config.replace_color, &demangled); + }); + if ui.button("Copy").clicked() { + ui.output_mut(|output| output.copied_text = demangled); + } + } else { + ui.scope(|ui| { + ui.style_mut().override_text_style = Some(TextStyle::Monospace); + ui.colored_label(view_state.view_config.replace_color, "[invalid]"); + }); + } + }); +} diff --git a/src/views/function_diff.rs b/src/views/function_diff.rs index f109621..16a8d41 100644 --- a/src/views/function_diff.rs +++ b/src/views/function_diff.rs @@ -446,7 +446,7 @@ pub fn function_diff_ui(ui: &mut egui::Ui, view_state: &mut ViewState) -> bool { ui.style_mut().override_text_style = Some(egui::TextStyle::Monospace); ui.style_mut().wrap = Some(false); if view_state.jobs.iter().any(|job| job.job_type == Job::ObjDiff) { - ui.label("Building..."); + ui.colored_label(view_state.view_config.replace_color, "Building…"); } else { ui.label("Last built:"); let format = diff --git a/src/views/jobs.rs b/src/views/jobs.rs index 0e6df3e..2acbaa1 100644 --- a/src/views/jobs.rs +++ b/src/views/jobs.rs @@ -35,14 +35,14 @@ pub fn jobs_ui(ui: &mut egui::Ui, view_state: &mut ViewState) { ui.colored_label( view_state.view_config.delete_color, if err_string.len() > STATUS_LENGTH - 10 { - format!("Error: {}...", &err_string[0..STATUS_LENGTH - 10]) + format!("Error: {}…", &err_string[0..STATUS_LENGTH - 10]) } else { format!("Error: {:width$}", err_string, width = STATUS_LENGTH - 7) }, ); } else { ui.label(if status.status.len() > STATUS_LENGTH - 3 { - format!("{}...", &status.status[0..STATUS_LENGTH - 3]) + format!("{}…", &status.status[0..STATUS_LENGTH - 3]) } else { format!("{:width$}", &status.status, width = STATUS_LENGTH) }); diff --git a/src/views/mod.rs b/src/views/mod.rs index fb84ac5..613785b 100644 --- a/src/views/mod.rs +++ b/src/views/mod.rs @@ -1,7 +1,9 @@ use egui::{text::LayoutJob, Color32, FontId, TextFormat}; +pub(crate) mod appearance; pub(crate) mod config; pub(crate) mod data_diff; +pub(crate) mod demangle; pub(crate) mod function_diff; pub(crate) mod jobs; pub(crate) mod symbol_diff;