Skip to content

Commit

Permalink
- moved search mode and search flags over to config
Browse files Browse the repository at this point in the history
- removed display of undo/redo
- fixed annoying issue with dropping item over text causing to select text
- fixed bug with renaming elements from compound causing crashes
- fixed #10
  • Loading branch information
RealRTTV committed Jun 20, 2024
1 parent 690d436 commit 98f01bd
Show file tree
Hide file tree
Showing 17 changed files with 202 additions and 247 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nbtworkbench"
version = "1.3.3"
version = "1.3.4"
edition = "2021"
description = "A modern NBT Editor written in Rust designed for performance and efficiency."
license-file = "LICENSE"
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ NBT Workbench is written completely from scratch in [Rust](https://www.rust-lang
*\[1 to 8\] Jump to nth tab.
*\[9\] Jump to last tab.
* \[Ctrl + R\] Reload tab.
*\[Ctrl + Shift + R\] Toggle freehand mode. (Disables selecting text and makes toggle button extend horizontally to make for quick maneuvering)
*\[Ctrl + Shift + F\] Toggle freehand mode. (Disables selecting text and makes toggle button extend horizontally to make for quick maneuvering)
*\[Ctrl + Alt + T\] Change theme.
* \[Ctrl + N\] New tab.
* \[Ctrl + Shift + N\] New region file tab.
* \[Ctrl + O\] Open file.
Expand Down
19 changes: 1 addition & 18 deletions src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,9 @@ pub const SELECTED_TOGGLE_ON_UV: Vec2u = Vec2u::new(0, 72);
pub const SELECTED_TOGGLE_OFF_UV: Vec2u = Vec2u::new(8, 72);
pub const UNHELD_SCROLLBAR_UV: Vec2u = Vec2u::new(48, 64);
pub const HELD_SCROLLBAR_UV: Vec2u = Vec2u::new(54, 64);
pub const REMOVE_UV: Vec2u = Vec2u::new(0, 96);
pub const ADD_UV: Vec2u = Vec2u::new(16, 96);
pub const RENAME_UV: Vec2u = Vec2u::new(32, 96);
pub const MOVE_UV: Vec2u = Vec2u::new(48, 96);
pub const REPLACE_UV: Vec2u = Vec2u::new(64, 96);
pub const REORDER_UV: Vec2u = Vec2u::new(80, 96);
pub const REMOVE_TAIL_UV: Vec2u = Vec2u::new(0, 112);
pub const ADD_TAIL_UV: Vec2u = Vec2u::new(16, 112);
pub const RENAME_TAIL_UV: Vec2u = Vec2u::new(32, 112);
pub const MOVE_TAIL_UV: Vec2u = Vec2u::new(48, 112);
pub const REPLACE_TAIL_UV: Vec2u = Vec2u::new(64, 112);
pub const REORDER_TAIL_UV: Vec2u = Vec2u::new(80, 112);
pub const BULK_UV: Vec2u = Vec2u::new(240, 240);
pub const BULK_TAIL_UV: Vec2u = Vec2u::new(240, 240);
pub const UNDO_UV: Vec2u = Vec2u::new(32, 144);
pub const REDO_UV: Vec2u = Vec2u::new(48, 144);
pub const LINE_NUMBER_SEPARATOR_UV: Vec2u = Vec2u::new(60, 64);
pub const END_LINE_NUMBER_SEPARATOR_UV: Vec2u = Vec2u::new(62, 64);
pub const HORIZONTAL_SEPARATOR_UV: Vec2u = Vec2u::new(17, 80); // (14 by 2)
pub const HORIZONTAL_SEPARATOR_UV: Vec2u = Vec2u::new(17, 80);
pub const TEXT_UNDERLINE_UV: Vec2u = Vec2u::new(16, 82);
pub const INSERTION_UV: Vec2u = Vec2u::new(16, 84);
pub const TOOLTIP_UV: Vec2u = Vec2u::new(96, 144);
Expand Down Expand Up @@ -200,7 +184,6 @@ pub fn atlas(theme: Theme) -> &'static [u8] {

#[allow(clippy::cast_ptr_alignment)]
pub fn icon() -> Vec<u8> {
// error!("Hello, world!");
let original = match (since_epoch().as_millis() & 7) as u8 {
// it's a good random only because its used once
0 => OTHERSIDE_MUSIC_DISC_ICON,
Expand Down
Binary file modified src/assets/dark_atlas.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/assets/light_atlas.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion src/be_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ impl<'a> BigEndianDecoder<'a> {

#[inline]
pub fn sort(&self, map: &mut CompoundMap) {
config::get_sort_algorithm().sort(map)
// SAFETY: we can only call this on init of the compound
unsafe { config::get_sort_algorithm().sort(map) }
}

#[optimize(speed)]
Expand Down
8 changes: 4 additions & 4 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use glob::glob;

use crate::{create_regex, error, log, WindowProperties};
use crate::elements::element::NbtElement;
use crate::search_box::{SearchBox, SearchPredicate, SearchPredicateInner};
use crate::search_box::{SearchBox, SearchFlags, SearchPredicate, SearchPredicateInner};
use crate::tab::FileFormat;
use crate::workbench::Workbench;

Expand Down Expand Up @@ -59,9 +59,9 @@ fn get_predicate(args: &mut Vec<String>) -> SearchPredicate {
};

let search_flags = match get_argument("--search", args).or_else(|| get_argument("-s", args)).as_deref() {
Some("key") => 0b10_u8,
Some("value") => 0b01_u8,
Some("all") | None => 0b11_u8,
Some("key") => SearchFlags::Keys,
Some("value") => SearchFlags::Values,
Some("all") | None => SearchFlags::KeysValues,
Some(x) => {
error!("Invalid search kind '{x}', valid ones are: `key`, `value`, and `all`.");
std::process::exit(1);
Expand Down
59 changes: 47 additions & 12 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,43 @@
use fxhash::FxHashMap;
use winit::window::Theme;
use crate::search_box::{SearchFlags, SearchMode};

use crate::SortAlgorithm;

struct Config {
theme: Theme,
sort_algorithm: SortAlgorithm,
search_mode: SearchMode,
search_flags: SearchFlags,
}

static mut CONFIG: Config = Config {
theme: Theme::Dark,
sort_algorithm: SortAlgorithm::Type,
search_mode: SearchMode::String,
search_flags: SearchFlags::Values,
};

#[inline]
#[must_use]
fn config() -> &'static mut Config {
unsafe { &mut CONFIG }
}

#[cfg(not(target_arch = "wasm32"))]
pub fn read() -> bool {
let Some(config_dir) = dirs::config_dir() else { return false };
let path = config_dir.join("nbtworkbench/config.txt");
let Some(map) = std::fs::read(path).ok().and_then(|vec| String::from_utf8(vec).ok()).map(|str| str.lines().filter_map(|line| line.split_once('=')).map(|(a, b)| (a.to_owned(), b.to_owned())).collect::<FxHashMap<String, String>>()) else { return false };
let Some(map) = std::fs::read(path).ok().and_then(|vec| String::from_utf8(vec).ok()).map(|str| parse_lines(&str)) else { return false };
read0(&map);
true
}

#[cfg(target_arch = "wasm32")]
pub fn read() -> bool {
let Some(map) = web_sys::window().and_then(|window| window.local_storage().ok()).flatten().and_then(|storage| storage.get_item("config").ok()).flatten().map(|str| str.lines().filter_map(|line| line.split_once('=')).map(|(a, b)| (a.to_owned(), b.to_owned())).collect::<FxHashMap<String, String>>()) else { return false };
let Some(map) = web_sys::window().and_then(|window| window.local_storage().ok()).flatten().and_then(|storage| storage.get_item("config").ok()).flatten().map(|str| parse_lines(&str)) else { return false };
read0(&map);
true
}

fn parse_lines(str: &str) -> FxHashMap<String, String> {
str.lines().filter_map(|line| line.split_once('=')).map(|(a, b)| (a.to_owned(), b.to_owned())).collect::<FxHashMap<String, String>>()
}

#[inline]
fn read0(map: &FxHashMap<String, String>) {
if let Some(theme) = map.get("theme").and_then(|s| match s.as_str() { "dark" => Some(Theme::Dark), "light" => Some(Theme::Light), _ => None }) {
Expand All @@ -43,6 +46,12 @@ fn read0(map: &FxHashMap<String, String>) {
if let Some(sort_algorithm) = map.get("sort_algorithm").and_then(|s| match s.as_str() { "none" => Some(SortAlgorithm::None), "name" => Some(SortAlgorithm::Name), "type" => Some(SortAlgorithm::Type), _ => None }) {
set_sort_algorithm(sort_algorithm);
}
if let Some(search_mode) = map.get("search_mode").and_then(|s| match s.as_str() { "string" => Some(SearchMode::String), "regex" => Some(SearchMode::Regex), "snbt" => Some(SearchMode::Snbt), _ => None }) {
set_search_mode(search_mode);
}
if let Some(search_flags) = map.get("search_flags").and_then(|s| match s.as_str() { "key" => Some(SearchFlags::Keys), "value" => Some(SearchFlags::Values), "all" => Some(SearchFlags::KeysValues), _ => None }) {
set_search_flags(search_flags);
}
}

#[cfg(not(target_arch = "wasm32"))]
Expand All @@ -69,31 +78,57 @@ fn write0() -> String {
let mut builder = String::new();
writeln!(&mut builder, "theme={}", match get_theme() { Theme::Light => "light", Theme::Dark => "dark" }).unwrap_or(());
writeln!(&mut builder, "sort_algorithm={}", match get_sort_algorithm() { SortAlgorithm::None => "none", SortAlgorithm::Name => "name", SortAlgorithm::Type => "type" }).unwrap_or(());
writeln!(&mut builder, "search_mode={}", match get_search_mode() { SearchMode::String => "string", SearchMode::Regex => "regex", SearchMode::Snbt => "snbt" }).unwrap_or(());
writeln!(&mut builder, "search_flags={}", match get_search_flags() { SearchFlags::Keys => "key", SearchFlags::Values => "value", SearchFlags::KeysValues => "all" }).unwrap_or(());
builder
}

#[inline]
#[must_use]
pub fn get_theme() -> Theme {
config().theme
unsafe { CONFIG.theme }
}

#[inline]
pub fn set_theme(theme: Theme) -> Theme {
let old_theme = core::mem::replace(&mut config().theme, theme);
let old_theme = unsafe { core::mem::replace(&mut CONFIG.theme, theme) };
write();
old_theme
}


#[must_use]
pub fn get_sort_algorithm() -> SortAlgorithm {
config().sort_algorithm
unsafe { CONFIG.sort_algorithm }
}

#[inline]
pub fn set_sort_algorithm(sort_algorithm: SortAlgorithm) -> SortAlgorithm {
let old_sort_algorithm = core::mem::replace(&mut config().sort_algorithm, sort_algorithm);
let old_sort_algorithm = unsafe { core::mem::replace(&mut CONFIG.sort_algorithm, sort_algorithm) };
write();
old_sort_algorithm
}

#[must_use]
pub fn get_search_mode() -> SearchMode {
unsafe { CONFIG.search_mode }
}

#[inline]
pub fn set_search_mode(search_mode: SearchMode) -> SearchMode {
let old_search_mode = unsafe { core::mem::replace(&mut CONFIG.search_mode, search_mode) };
write();
old_search_mode
}

#[must_use]
pub fn get_search_flags() -> SearchFlags {
unsafe { CONFIG.search_flags }
}

#[inline]
pub fn set_search_flags(search_flags: SearchFlags) -> SearchFlags {
let old_search_flags = unsafe { core::mem::replace(&mut CONFIG.search_flags, search_flags) };
write();
old_search_flags
}
Loading

0 comments on commit 98f01bd

Please sign in to comment.