Skip to content

Commit

Permalink
A bunch of WIP work
Browse files Browse the repository at this point in the history
  • Loading branch information
ethteck committed Oct 11, 2024
1 parent dd1adba commit ca77de4
Show file tree
Hide file tree
Showing 14 changed files with 718 additions and 719 deletions.
870 changes: 413 additions & 457 deletions Cargo.lock

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ anyhow = "1.0"
argh = "0.1.12"
dirs = "5.0.1"
dtoa = "1.0.9"
eframe = { version = "0.28.1", features = ["persistence"] }
egui-modal = "0.4.0"
egui-phosphor = "0.6.0"
eframe = { version = "0.29.1", features = ["persistence"] }
egui-modal = "0.5.0"
egui-phosphor = "0.7.2"
encoding_rs = "0.8.34"
bdiff_hex_view = { version = "0.8.5", path="../hex_view" }
iset = "0.3.0"
log = "0.4.22"
mapfile_parser = "2.6.0"
mapfile_parser = "2.7.1"
notify = "6.1.1"
parse_int = "0.6.0"
rfd = "0.14.1"
rfd = "0.15.0"
serde = "1.0"
serde_json = "1.0"

Expand All @@ -36,4 +36,4 @@ winres = "0.1.12"

[build-dependencies]
anyhow = "1.0"
vergen-gitcl = { version = "1.0.0", features = ["build", "cargo"] }
vergen-gitcl = { version = "1.0.1", features = ["build", "cargo"] }
92 changes: 60 additions & 32 deletions app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,19 @@ impl BdiffApp {
changed = true;
}
if ctx.input(|i| i.key_pressed(egui::Key::ArrowUp))
&& fv.hv.selection.start() >= fv.bytes_per_row
&& fv.hv.selection.end() >= fv.bytes_per_row
&& fv.hv.selection.start() >= fv.hv.bytes_per_row
&& fv.hv.selection.end() >= fv.hv.bytes_per_row
{
fv.hv.selection.adjust_cur_pos(-(fv.bytes_per_row as isize));
fv.hv
.selection
.adjust_cur_pos(-(fv.hv.bytes_per_row as isize));
changed = true;
}
if ctx.input(|i| i.key_pressed(egui::Key::ArrowDown))
&& fv.hv.selection.start() < fv.file.data.len() - fv.bytes_per_row
&& fv.hv.selection.end() < fv.file.data.len() - fv.bytes_per_row
&& fv.hv.selection.start() < fv.file.data.len() - fv.hv.bytes_per_row
&& fv.hv.selection.end() < fv.file.data.len() - fv.hv.bytes_per_row
{
fv.hv.selection.adjust_cur_pos(fv.bytes_per_row as isize);
fv.hv.selection.adjust_cur_pos(fv.hv.bytes_per_row as isize);
changed = true;
}

Expand All @@ -184,11 +186,11 @@ impl BdiffApp {
}

fn move_view(&mut self, ctx: &Context) {
let prev_positions: Vec<usize> = self
let prev_positions: Vec<isize> = self
.file_views
.iter()
.map(|fv| fv.cur_pos)
.collect::<Vec<usize>>();
.map(|fv| fv.hv.cur_pos)
.collect::<Vec<isize>>();

let diffing = self.file_views.len() > 1 && self.diff_state.enabled;

Expand All @@ -202,7 +204,7 @@ impl BdiffApp {
{
fv.hv.set_cur_pos(
&fv.file.data,
fv.file.data.len() - fv.hv.bytes_per_screen(&fv.file.data),
fv.file.data.len() as isize - fv.hv.bytes_per_screen(&fv.file.data) as isize,
)
}
if ctx.input(|i| i.key_pressed(egui::Key::PageUp)) {
Expand All @@ -225,30 +227,35 @@ impl BdiffApp {
}
if ctx.input(|i| i.key_pressed(egui::Key::ArrowUp)) {
fv.hv
.adjust_cur_pos(&fv.file.data, -(fv.bytes_per_row as isize))
.adjust_cur_pos(&fv.file.data, -(fv.hv.bytes_per_row as isize))
}
if ctx.input(|i| i.key_pressed(egui::Key::ArrowDown)) {
fv.hv
.adjust_cur_pos(&fv.file.data, fv.bytes_per_row as isize)
.adjust_cur_pos(&fv.file.data, fv.hv.bytes_per_row as isize)
}
if ctx.input(|i| i.key_pressed(egui::Key::Enter)) {
if diffing {
let last_byte = fv.cur_pos + fv.hv.bytes_per_screen(&fv.file.data);

if last_byte < fv.file.data.len() {
match self.diff_state.get_next_diff(last_byte) {
Some(next_diff) => {
// Move to the next diff
let new_pos = next_diff - (next_diff % fv.bytes_per_row);
fv.hv.set_cur_pos(&fv.file.data, new_pos);
}
None => {
// Move to the end of the file
if fv.file.data.len() >= fv.hv.bytes_per_screen(&fv.file.data) {
fv.hv.set_cur_pos(
&fv.file.data,
fv.file.data.len() - fv.hv.bytes_per_screen(&fv.file.data),
);
let last_byte = fv.hv.cur_pos + fv.hv.bytes_per_screen(&fv.file.data) as isize;

if last_byte >= 0 {
let last_byte = last_byte as usize;

if last_byte < fv.file.data.len() {
match self.diff_state.get_next_diff(last_byte) {
Some(next_diff) => {
// Move to the next diff
let new_pos = next_diff - (next_diff % fv.hv.bytes_per_row);
fv.hv.set_cur_pos(&fv.file.data, new_pos as isize);
}
None => {
// Move to the end of the file
if fv.file.data.len() >= fv.hv.bytes_per_screen(&fv.file.data) {
fv.hv.set_cur_pos(
&fv.file.data,
fv.file.data.len() as isize
- fv.hv.bytes_per_screen(&fv.file.data) as isize,
);
}
}
}
}
Expand Down Expand Up @@ -284,17 +291,32 @@ impl BdiffApp {
}
fv.hv.adjust_cur_pos(
&fv.file.data,
-scroll_amt * lines_per_scroll * fv.bytes_per_row as isize,
-scroll_amt * lines_per_scroll * fv.hv.bytes_per_row as isize,
)
}
}

let mut shared_negative = isize::MIN;
for fv in self.file_views.iter_mut() {
shared_negative = shared_negative.max(fv.hv.cur_pos);
}

// Don't allow views to endlessly become negative
if shared_negative < 0 {
let shared_negative: usize = shared_negative.abs() as usize;

for fv in self.file_views.iter_mut().filter(|fv| !fv.pos_locked) {
let amt_to_correct = (shared_negative / fv.hv.bytes_per_row) * fv.hv.bytes_per_row;
fv.hv.cur_pos += amt_to_correct as isize;
}
}

// If any of the current positions are different from the previous ones
let has_new_positions = self
.file_views
.iter()
.zip(prev_positions.iter())
.any(|(fv, &prev_pos)| fv.cur_pos != prev_pos);
.any(|(fv, &prev_pos)| fv.hv.cur_pos != prev_pos);

// and if any hex views are also locked, we need to recalculate the diff
if has_new_positions && self.file_views.iter().any(|fv| fv.pos_locked) {
Expand Down Expand Up @@ -636,7 +658,13 @@ impl eframe::App for BdiffApp {
}

if self.settings.theme_menu_open {
let cur_theme = self.settings.theme_settings.clone();
show_theme_settings(ctx, &mut self.settings);
if cur_theme != self.settings.theme_settings {
for fv in self.file_views.iter_mut() {
fv.hv.style = self.settings.theme_settings.hex_view_style.clone();
}
}
}
}
}
Expand All @@ -654,7 +682,7 @@ impl BdiffApp {
fn show_overwrite_modal(&mut self, modal: &Modal) {
modal.show(|ui| {
modal.title(ui, "Overwrite previous config");
ui.label(&format!(
ui.label(format!(
"By saving, you are going to overwrite existing configuration file at \"{}\".",
"./bdiff.json"
));
Expand Down Expand Up @@ -692,7 +720,7 @@ impl BdiffApp {
match pos {
Some(pos) => {
for fv in self.file_views.iter_mut() {
fv.hv.set_cur_pos(&fv.file.data, pos);
fv.hv.set_cur_pos(&fv.file.data, pos as isize);
}
goto_modal.close();
}
Expand Down
35 changes: 21 additions & 14 deletions app/src/file_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,36 @@ use crate::{
bin_file::{read_file_bytes, Endianness},
config::Config,
data_viewer::DataViewer,
map_tool::MapTool,
settings::Settings,
string_viewer::StringViewer,
symbol_tool::SymbolTool,
};

pub struct FileView {
pub id: usize,
pub file: BinFile,
pub bytes_per_row: usize,
pub cur_pos: usize,
pub pos_locked: bool,
pub show_selection_info: bool,
pub show_cursor_info: bool,
pub hv: HexView,
sv: StringViewer,
dv: DataViewer,
pub mt: MapTool,
pub mt: SymbolTool,
pub closed: bool,
}

impl FileView {
pub fn new(file: BinFile, id: usize) -> Self {
let default_bytes_per_row = 0x10;

Self {
id,
file,
bytes_per_row: default_bytes_per_row,
cur_pos: 0,
pos_locked: false,
show_selection_info: true,
show_cursor_info: true,
hv: HexView::new(id),
sv: StringViewer::default(),
dv: DataViewer::default(),
mt: MapTool::default(),
mt: SymbolTool::default(),
closed: false,
}
}
Expand Down Expand Up @@ -79,17 +73,24 @@ impl FileView {
.id(Id::new(format!("hex_view_window_{}", self.id)))
.title_bar(false)
.show(ctx, |ui| {
let file_name = self.file.path.as_path().to_str().unwrap();

ui.with_layout(
egui::Layout::left_to_right(eframe::emath::Align::Min),
|ui| {
// Truncate file_name with leading ellipsis
let name_limit = 50;
let file_name = self.file.path.as_path().to_str().unwrap();
let file_name_brief = if file_name.len() > name_limit {
format!("...{}", &file_name[file_name.len() - name_limit - 3..])
} else {
file_name.to_owned()
};
ui.label(
egui::RichText::new(file_name)
egui::RichText::new(file_name_brief)
.monospace()
.size(14.0)
.color(Color32::LIGHT_GRAY),
);
)
.on_hover_text(egui::RichText::new(file_name));

let (lock_text, hover_text) = match self.pos_locked {
true => (
Expand Down Expand Up @@ -133,7 +134,7 @@ impl FileView {
ui.checkbox(&mut self.show_cursor_info, "Cursor info");
ui.checkbox(&mut self.dv.show, "Data viewer");
ui.checkbox(&mut self.sv.show, "String viewer");
ui.checkbox(&mut self.mt.show, "Map tool");
ui.checkbox(&mut self.mt.show, "Symbols");
});

if ui.button("X").on_hover_text("Close").clicked() {
Expand Down Expand Up @@ -211,6 +212,12 @@ impl FileView {
if self.show_cursor_info {
let hover_text = match self.hv.cursor_pos {
Some(pos) => {
if pos < 0 || pos >= self.file.data.len() as isize {
return;
}

let pos = pos as usize;

let map_entry = match self.mt.map_file {
Some(ref map_file) => map_file.get_entry(pos, pos + 1),
None => None,
Expand Down
2 changes: 1 addition & 1 deletion app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ mod config;
mod data_viewer;
mod file_view;
mod map_file;
mod map_tool;
mod settings;
mod string_viewer;
mod symbol_tool;
mod watcher;

use std::path::PathBuf;
Expand Down
14 changes: 7 additions & 7 deletions app/src/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub mod theme;
pub use byte_grouping::byte_grouping_slider;
pub use theme::show_theme_settings;

#[derive(Deserialize, Serialize, Default, PartialEq, Eq, PartialOrd, Ord, Clone)]
#[derive(Deserialize, Serialize, Default, PartialEq, PartialOrd, Clone)]
pub struct Settings {
pub byte_grouping: ByteGrouping,
pub theme_settings: ThemeSettings,
Expand All @@ -25,28 +25,28 @@ pub struct Settings {

impl SettingsControl for Settings {
fn restore_defaults(&mut self) {
let prev_theme_menu_open = self.theme_menu_open;
*self = Settings::default();
self.save();
// todo dumb hack because the state of the window being open is part of the struct
self.theme_menu_open = prev_theme_menu_open;
}

fn reload(&mut self) {
let prev_theme_menu_open = self.theme_menu_open;
*self = read_json_settings().expect("Failed to read settings!");
// todo dumb hack because the state of the window being open is part of the struct
self.theme_menu_open = prev_theme_menu_open;
}

fn save(&self) {
write_json_settings(self).expect("Failed to save settings!");
}

fn toggle_menu_visibility(&mut self) {
self.theme_menu_open = !self.theme_menu_open;
}
}

pub trait SettingsControl {
fn restore_defaults(&mut self);
fn reload(&mut self);
fn save(&self);
fn toggle_menu_visibility(&mut self);
}

pub fn get_settings_path() -> PathBuf {
Expand Down
Loading

0 comments on commit ca77de4

Please sign in to comment.