Skip to content

Commit

Permalink
did some stuff, added sort
Browse files Browse the repository at this point in the history
  • Loading branch information
RealRTTV committed Feb 18, 2024
1 parent 0f4f4e3 commit 299d97c
Show file tree
Hide file tree
Showing 35 changed files with 301 additions and 226 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.1.0"
version = "1.2.0"
edition = "2021"
description = "A modern NBT Editor written in Rust designed for performance and efficiency."
license-file = "LICENSE"
Expand Down
18 changes: 9 additions & 9 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use std::fs::write;
use std::io::Read;
use std::mem::MaybeUninit;

const UNICODE: &[u8] = include_bytes!("src/assets/unicode.hex");
const ATLAS: &[u8] = include_bytes!(r"src/assets/atlas.png");
const UNICODE: &[u8] = include_bytes!("src/assets/build/unicode.hex");
const ATLAS: &[u8] = include_bytes!(r"src/assets/build/atlas.png");

fn main() {
{ write(r"src\assets\atlas.hex", zune_png::PngDecoder::new(ATLAS).decode_raw().unwrap()).unwrap(); }
Expand Down Expand Up @@ -37,14 +37,14 @@ fn main() {
write(r"src/assets/unicode.hex.zib", &buf).unwrap();
}

if std::env::var("CARGO_CFG_TARGET_OS").as_deref() == Ok("windows") {
if std::env::var("CARGO_CFG_TARGET_OS").unwrap() == "windows" {
if let Err(e) = winres::WindowsResource::new()
.set_icon_with_id("src/assets/icon_16.ico", "16")
.set_icon_with_id("src/assets/icon_32.ico", "32")
.set_icon_with_id("src/assets/icon_48.ico", "48")
.set_icon_with_id("src/assets/icon_64.ico", "64")
.set_icon_with_id("src/assets/icon_128.ico", "128")
.set_icon_with_id("src/assets/icon_256.ico", "!")
.set_icon_with_id(r"src/assets/build/icon_256.ico", "1")
.set_icon_with_id(r"src/assets/build/icon_128.ico", "2")
.set_icon_with_id(r"src/assets/build/icon_64.ico", "3")
.set_icon_with_id(r"src/assets/build/icon_48.ico", "4")
.set_icon_with_id(r"src/assets/build/icon_32.ico", "5")
.set_icon_with_id(r"src/assets/build/icon_16.ico", "6")
.compile()
{
eprintln!("Error! {e}");
Expand Down
5 changes: 2 additions & 3 deletions clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ export function getClipboard() {
}

export function onInput() {
// is not firefox
if (typeof InstallTrigger === 'undefined') {
window.navigator.clipboard.readText().then((str) => clipboard = str)
if (!navigator.userAgent.toLowerCase().includes("firefox")) {
window.navigator.clipboard.readText().then((str) => clipboard = str).catch(x => x)
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/alert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::assets::{ALERT_TEXT_Z, ALERT_UV, ALERT_Z, HEADER_SIZE};
use crate::color::TextColor;
use crate::vertex_buffer_builder::{Vec2u, VertexBufferBuilder};
use crate::{since_epoch, smoothstep64, StrExt};
use std::time::{Duration, Instant};
use std::time::Duration;

pub struct Alert {
timestamp: Option<Duration>,
Expand Down Expand Up @@ -71,15 +71,15 @@ impl Alert {
}

pub fn is_invisible(&mut self) -> bool {
let ms = (since_epoch() - *self.timestamp.get_or_insert(since_epoch())).as_millis() as usize;
let display_time = (self.message.len() + self.title.len()) * 200 / 3 + 5000;
let ms = since_epoch().saturating_sub(*self.timestamp.get_or_insert(since_epoch())).as_millis() as usize;
let display_time = (self.message.len() + self.title.len()) * 60 + 3000;
ms > 500 + display_time
}

fn get_inset(&mut self) -> usize {
let mut ms = (since_epoch() - *self.timestamp.get_or_insert(since_epoch())).as_millis() as usize;
let mut ms = since_epoch().saturating_sub(*self.timestamp.get_or_insert(since_epoch())).as_millis() as usize;
let width = self.width + 24;
let display_time = (self.message.len() + self.title.len()) * 200 / 3 + 5000;
let display_time = (self.message.len() + self.title.len()) * 60 + 3000;
if ms < 250 { return (smoothstep64((250 - ms) as f64 / 250.0) * width as f64) as usize }
ms -= 250;
if ms < display_time {
Expand Down
36 changes: 19 additions & 17 deletions src/assets.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::mem::ManuallyDrop;
use crate::{since_epoch, log};
use crate::color::TextColor;
use crate::since_epoch;

use crate::vertex_buffer_builder::Vec2u;

Expand All @@ -14,14 +13,14 @@ pub const UNICODE_LEN: usize = 1_818_624;
pub const ICON_WIDTH: usize = 64;
pub const ICON_HEIGHT: usize = 64;

const OTHERSIDE_MUSIC_DISC_ICON: &[u8] = include_bytes!("assets/otherside.hex");
const PIGSTEP_MUSIC_DISC_ICON: &[u8] = include_bytes!("assets/pigstep.hex");
const MELLOHI_MUSIC_DISC_ICON: &[u8] = include_bytes!("assets/mellohi.hex");
const FIVE_MUSIC_DISC_ICON: &[u8] = include_bytes!("assets/5.hex");
const WARD_MUSIC_DISC_ICON: &[u8] = include_bytes!("assets/ward.hex");
const ELEVEN_MUSIC_DISC_ICON: &[u8] = include_bytes!("assets/11.hex");
const RELIC_MUSIC_DISC_ICON: &[u8] = include_bytes!("assets/relic.hex");
const STAL_MUSIC_DISC_ICON: &[u8] = include_bytes!("assets/stal.hex");
const OTHERSIDE_MUSIC_DISC_ICON: &[u8] = include_bytes!("assets/discs/otherside.hex");
const PIGSTEP_MUSIC_DISC_ICON: &[u8] = include_bytes!("assets/discs/pigstep.hex");
const MELLOHI_MUSIC_DISC_ICON: &[u8] = include_bytes!("assets/discs/mellohi.hex");
const FIVE_MUSIC_DISC_ICON: &[u8] = include_bytes!("assets/discs/5.hex");
const WARD_MUSIC_DISC_ICON: &[u8] = include_bytes!("assets/discs/ward.hex");
const ELEVEN_MUSIC_DISC_ICON: &[u8] = include_bytes!("assets/discs/11.hex");
const RELIC_MUSIC_DISC_ICON: &[u8] = include_bytes!("assets/discs/relic.hex");
const STAL_MUSIC_DISC_ICON: &[u8] = include_bytes!("assets/discs/stal.hex");

pub const CONNECTION_UV: Vec2u = Vec2u::new(64, 64);
pub const UNKNOWN_NBT_UV: Vec2u = Vec2u::new(112, 32);
Expand Down Expand Up @@ -60,10 +59,10 @@ pub const ZLIB_FILE_TYPE_UV: Vec2u = Vec2u::new(64, 80);
pub const SNBT_FILE_TYPE_UV: Vec2u = Vec2u::new(80, 80);
pub const MCA_FILE_TYPE_UV: Vec2u = Vec2u::new(96, 80);
pub const OPEN_FOLDER_UV: Vec2u = Vec2u::new(112, 80);
pub const UNSELECTED_TOGGLE_OFF_UV: Vec2u = Vec2u::new(0, 64);
pub const UNSELECTED_TOGGLE_ON_UV: Vec2u = Vec2u::new(8, 64);
pub const SELECTED_TOGGLE_OFF_UV: Vec2u = Vec2u::new(0, 72);
pub const SELECTED_TOGGLE_ON_UV: Vec2u = Vec2u::new(8, 72);
pub const UNSELECTED_TOGGLE_ON_UV: Vec2u = Vec2u::new(0, 64);
pub const UNSELECTED_TOGGLE_OFF_UV: Vec2u = Vec2u::new(8, 64);
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);
Expand Down Expand Up @@ -94,13 +93,17 @@ pub const HOVERED_STRIPE_UV: Vec2u = Vec2u::new(112, 128);
pub const INVALID_STRIPE_UV: Vec2u = Vec2u::new(112, 112);
pub const COPY_RAW_UV: Vec2u = Vec2u::new(3, 131);
pub const COPY_FORMATTED_UV: Vec2u = Vec2u::new(19, 131);
#[cfg(not(target_arch = "wasm32"))]
pub const OPEN_ARRAY_IN_HEX_UV: Vec2u = Vec2u::new(35, 131);
#[cfg(not(target_arch = "wasm32"))]
pub const OPEN_IN_TXT: Vec2u = Vec2u::new(51, 131);
pub const SORT_COMPOUND_BY_NAME: Vec2u = Vec2u::new(67, 131);
pub const SORT_COMPOUND_BY_TYPE: Vec2u = Vec2u::new(83, 131);
pub const SORT_COMPOUND_BY_NOTHING: Vec2u = Vec2u::new(0, 160);
pub const FREEHAND_MODE_UV: Vec2u = Vec2u::new(0, 144);
pub const ENABLED_FREEHAND_MODE_UV: Vec2u = Vec2u::new(16, 144);
pub const STEAL_ANIMATION_OVERLAY: Vec2u = Vec2u::new(64, 144);
pub const STEAL_ANIMATION_OVERLAY_UV: Vec2u = Vec2u::new(64, 144);
pub const STAMP_BACKDROP_UV: Vec2u = Vec2u::new(16, 160);

pub const BYTE_UV: Vec2u = Vec2u::new(0, 0);
pub const SHORT_UV: Vec2u = Vec2u::new(16, 0);
Expand Down Expand Up @@ -130,7 +133,6 @@ pub const LIST_GHOST_UV: Vec2u = Vec2u::new(32, 48);
pub const COMPOUND_GHOST_UV: Vec2u = Vec2u::new(48, 48);
pub const INT_ARRAY_GHOST_UV: Vec2u = Vec2u::new(112, 16);
pub const LONG_ARRAY_GHOST_UV: Vec2u = Vec2u::new(0, 48);
pub const REGION_GHOST_UV: Vec2u = Vec2u::new(96, 48);
pub const CHUNK_GHOST_UV: Vec2u = Vec2u::new(64, 48);
pub const ALERT_UV: Vec2u = Vec2u::new(112, 144);

Expand Down Expand Up @@ -204,7 +206,7 @@ pub fn icon() -> Vec<u8> {
}
let mut scaled = ManuallyDrop::new(core::hint::black_box(scaled));
#[cfg(all(debug_assertions, not(target_arch = "wasm32")))]
log!(
crate::log!(
"took {} cycles",
unsafe { core::arch::x86_64::_rdtsc() } - start
);
Expand Down
Binary file modified src/assets/atlas.hex
Binary file not shown.
Binary file removed src/assets/atlas.png
Binary file not shown.
Binary file added src/assets/build/atlas.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 10 additions & 1 deletion src/decoder.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
use std::intrinsics::likely;

use compact_str::CompactString;
use crate::elements::compound::CompoundMap;
use crate::SortAlgorithm;

pub struct Decoder {
pub data: *const u8,
end: *const u8,
sort: SortAlgorithm,
}

#[allow(improper_ctypes_definitions)]
impl Decoder {
#[inline]
#[optimize(speed)]
pub const fn new(data: &[u8]) -> Self {
pub const fn new(data: &[u8], sort: SortAlgorithm) -> Self {
Self {
end: unsafe { data.as_ptr().add(data.len()) },
data: data.as_ptr(),
sort,
}
}

#[inline]
pub fn sort(&self, map: &mut CompoundMap) {
self.sort.sort(map)
}

#[inline]
#[optimize(speed)]
#[must_use]
Expand Down
80 changes: 42 additions & 38 deletions src/element_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ use std::cmp::Ordering;
use std::convert::identity;
#[cfg(not(target_arch = "wasm32"))]
use std::fs::OpenOptions;
#[cfg(not(target_arch = "wasm32"))]
use std::process::Command;

use compact_str::CompactString;
#[cfg(not(target_arch = "wasm32"))]
use notify::{EventKind, PollWatcher, RecursiveMode, Watcher};
use uuid::Uuid;

use crate::{Bookmark, panic_unchecked, set_clipboard};
use crate::{FileUpdateSubscription, FileUpdateSubscriptionType, assets::{OPEN_ARRAY_IN_HEX_UV, OPEN_IN_TXT}};
use crate::{Bookmark, panic_unchecked, set_clipboard, FileUpdateSubscription};
#[cfg(not(target_arch = "wasm32"))]
use crate::{FileUpdateSubscriptionType, assets::{OPEN_ARRAY_IN_HEX_UV, OPEN_IN_TXT}};
use crate::assets::{ACTION_WHEEL_Z, COPY_FORMATTED_UV, COPY_RAW_UV, SORT_COMPOUND_BY_NAME, SORT_COMPOUND_BY_TYPE};
use crate::elements::chunk::NbtChunk;
use crate::elements::compound::NbtCompound;
Expand Down Expand Up @@ -76,9 +79,42 @@ impl ElementAction {
}
}

#[must_use]
pub fn by_name(a: (&str, &NbtElement), b: (&str, &NbtElement)) -> Ordering {
let (a_str, _) = a;
let (b_str, _) = b;
a_str.cmp(b_str)
}

#[must_use]
pub fn by_type(a: (&str, &NbtElement), b: (&str, &NbtElement)) -> Ordering {
const ORDERING: [usize; 256] = {
let mut array = [usize::MAX; 256];
array[NbtChunk::ID as usize] = 0;
array[NbtCompound::ID as usize] = 1;
array[NbtList::ID as usize] = 2;
array[NbtLongArray::ID as usize] = 3;
array[NbtIntArray::ID as usize] = 4;
array[NbtByteArray::ID as usize] = 5;
array[NbtString::ID as usize] = 6;
array[NbtDouble::ID as usize] = 7;
array[NbtFloat::ID as usize] = 8;
array[NbtLong::ID as usize] = 9;
array[NbtInt::ID as usize] = 10;
array[NbtShort::ID as usize] = 11;
array[NbtByte::ID as usize] = 12;
array
};

let (a_str, a_nbt) = a;
let (b_str, b_nbt) = b;
ORDERING[a_nbt.id() as usize].cmp(&ORDERING[b_nbt.id() as usize]).then_with(|| a_str.cmp(b_str))
}

#[allow(clippy::too_many_lines)]
pub fn apply(self, key: Option<CompactString>, indices: Box<[usize]>, tab_uuid: Uuid, true_line_number: usize, line_number: usize, element: &mut NbtElement, bookmarks: &mut Vec<Bookmark>, subscription: &mut Option<FileUpdateSubscription>) -> Option<WorkbenchAction> {
#[must_use]
#[cfg(not(target_arch = "wasm32"))]
fn open_file(str: &str) -> bool {
if cfg!(target_os = "windows") {
Command::new("cmd").args(["/c", "start", str]).status()
Expand All @@ -89,38 +125,6 @@ impl ElementAction {
}.is_ok()
}

#[must_use]
fn by_name(a: (&str, &NbtElement), b: (&str, &NbtElement)) -> Ordering {
let (a_str, _) = a;
let (b_str, _) = b;
a_str.cmp(b_str)
}

#[must_use]
fn by_type(a: (&str, &NbtElement), b: (&str, &NbtElement)) -> Ordering {
const ORDERING: [usize; 256] = {
let mut array = [usize::MAX; 256];
array[NbtChunk::ID as usize] = 0;
array[NbtCompound::ID as usize] = 1;
array[NbtList::ID as usize] = 2;
array[NbtLongArray::ID as usize] = 3;
array[NbtIntArray::ID as usize] = 4;
array[NbtByteArray::ID as usize] = 5;
array[NbtString::ID as usize] = 6;
array[NbtDouble::ID as usize] = 7;
array[NbtFloat::ID as usize] = 8;
array[NbtLong::ID as usize] = 9;
array[NbtInt::ID as usize] = 10;
array[NbtShort::ID as usize] = 11;
array[NbtByte::ID as usize] = 12;
array
};

let (a_str, a_nbt) = a;
let (b_str, b_nbt) = b;
ORDERING[a_nbt.id() as usize].cmp(&ORDERING[b_nbt.id() as usize]).then_with(|| a_str.cmp(b_str))
}

'm: {
match self {
Self::CopyRaw => {
Expand Down Expand Up @@ -270,9 +274,9 @@ impl ElementAction {
let bookmark_end = bookmarks.binary_search(&Bookmark::new(true_line_number + element.true_height() - 1, 0)).map_or_else(identity, |x| x + 1);
let bookmark_slice = if bookmark_end > bookmark_start || bookmark_end > bookmarks.len() { &mut [] } else { &mut bookmarks[bookmark_start..bookmark_end] };
let reordering_indices = if let Some(compound) = element.as_compound_mut() {
compound.entries.sort_by(by_name, line_number, true_line_number, true_height, open, bookmark_slice)
compound.entries.sort_by(Self::by_name, line_number, true_line_number, true_height, open, bookmark_slice)
} else if let Some(chunk) = element.as_chunk_mut() {
chunk.entries.sort_by(by_name, line_number, true_line_number, true_height, open, bookmark_slice)
chunk.entries.sort_by(Self::by_name, line_number, true_line_number, true_height, open, bookmark_slice)
} else {
unsafe { panic_unchecked("Unknown element kind for compound sorting") }
};
Expand All @@ -286,9 +290,9 @@ impl ElementAction {
let bookmark_end = bookmarks.binary_search(&Bookmark::new(true_line_number + element.true_height() - 1, 0)).map_or_else(identity, |x| x + 1);
let bookmark_slice = if bookmark_end < bookmark_start || bookmark_end > bookmarks.len() { &mut [] } else { &mut bookmarks[bookmark_start..bookmark_end] };
let reordering_indices = if let Some(compound) = element.as_compound_mut() {
compound.entries.sort_by(by_type, line_number, true_line_number, true_height, open, bookmark_slice)
compound.entries.sort_by(Self::by_type, line_number, true_line_number, true_height, open, bookmark_slice)
} else if let Some(chunk) = element.as_chunk_mut() {
chunk.entries.sort_by(by_type, line_number, true_line_number, true_height, open, bookmark_slice)
chunk.entries.sort_by(Self::by_type, line_number, true_line_number, true_height, open, bookmark_slice)
} else {
unsafe { panic_unchecked("Unknown element kind for compound sorting") }
};
Expand Down
4 changes: 2 additions & 2 deletions src/elements/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ macro_rules! array {
pub const ID: u8 = $my_id;

#[inline]
pub(in $crate::elements) fn from_str0(mut s: &str) -> Option<(&str, Self)> {
pub(in $crate::elements) fn from_str0(mut s: &str, sort: SortAlgorithm) -> Option<(&str, Self)> {
s = s
.strip_prefix('[')?
.trim_start()
.strip_prefix(concat!($char, ";"))?
.trim_start();
let mut array = Self::new();
while !s.starts_with(']') {
let (s2, element) = NbtElement::from_str0(s)?;
let (s2, element) = NbtElement::from_str0(s, sort)?;
array.insert(array.len(), element).ok()?;
s = s2.trim_start();
if let Some(s2) = s.strip_prefix(',') {
Expand Down
Loading

0 comments on commit 299d97c

Please sign in to comment.