Skip to content

Commit

Permalink
tfx: Lookup channel names from wordlist
Browse files Browse the repository at this point in the history
  • Loading branch information
cohaereo committed Nov 27, 2024
1 parent f44f84b commit 15d5ce1
Show file tree
Hide file tree
Showing 5 changed files with 291,528 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ panic.log
keys.txt

/textures
/pipelines
/bytecode-stuff
2 changes: 2 additions & 0 deletions crates/alkahest/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use crate::{
context::{GuiContext, GuiViewManager, HiddenWindows},
gizmo::draw_transform_gizmos,
hotkeys,
inspector::FnvWordlist,
updater::{ChannelSelector, UpdateDownload},
SelectionGizmoMode,
},
Expand Down Expand Up @@ -110,6 +111,7 @@ impl AlkahestApp {
resources.insert(SelectedEntity::default());
resources.insert(args);
resources.insert(window.clone());
resources.insert(FnvWordlist::new());

let mut maps = MapList::default();
maps.maps.push(Map::create_empty("Empty Map"));
Expand Down
51 changes: 45 additions & 6 deletions crates/alkahest/src/gui/inspector/channels.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::collections::HashMap;

use alkahest_renderer::{
ecs::channels::ObjectChannels, icons::ICON_TABLE, tfx::channels::ChannelType,
};
use egui::Widget;

use crate::gui::UiExt;
use bevy_ecs::system::Commands;
use egui::{Color32, RichText, Widget};

use super::ComponentPanel;
use bevy_ecs::system::Commands;
use crate::gui::UiExt;

impl ComponentPanel for ObjectChannels {
fn inspector_name() -> &'static str {
Expand All @@ -28,11 +29,19 @@ impl ComponentPanel for ObjectChannels {
_: &mut Commands<'_, '_>,
_: bevy_ecs::world::EntityRef<'s>,
ui: &mut egui::Ui,
_: &alkahest_renderer::resources::AppResources,
resources: &alkahest_renderer::resources::AppResources,
) {
let wordlist = resources.get::<FnvWordlist>();
for (channel_id, (value, channel_type)) in &mut self.values {
ui.horizontal(|ui| {
ui.strong(format!("{channel_id:08X}"));
if let Some(name) = wordlist.get(*channel_id) {
ui.strong(
RichText::new(format!("{name} ({channel_id:08X})"))
.color(Color32::LIGHT_BLUE),
);
} else {
ui.strong(format!("{channel_id:08X}"));
}

match channel_type {
alkahest_renderer::tfx::channels::ChannelType::Vec4 => {
Expand Down Expand Up @@ -75,3 +84,33 @@ impl ComponentPanel for ObjectChannels {
}
}
}

pub struct FnvWordlist(HashMap<u32, String>);

impl FnvWordlist {
/// Initializes a hashmap with the fnv hashes of all the strings in the embedded wordlist
pub fn new() -> Self {
let mut map: HashMap<u32, String> = HashMap::new();

const WORDLIST: &'static str = include_str!("../../../wordlist_channels.txt");
for s in WORDLIST.lines() {
let s = s.to_string();
let h = fnv1(s.as_bytes());
map.insert(h, s);
}

Self(map)
}

pub fn get(&self, hash: u32) -> Option<&str> {
self.0.get(&hash).map(|v| v.as_str())
}
}

const FNV1_BASE: u32 = 0x811c9dc5;
const FNV1_PRIME: u32 = 0x01000193;
fn fnv1(data: &[u8]) -> u32 {
data.iter().fold(FNV1_BASE, |acc, b| {
acc.wrapping_mul(FNV1_PRIME) ^ (*b as u32)
})
}
2 changes: 1 addition & 1 deletion crates/alkahest/src/gui/inspector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ mod channels;
mod decorator;
mod light;
mod util;

use alkahest_data::map::{SLightCollection, SRespawnPoint};
use alkahest_renderer::{
camera::Camera,
Expand Down Expand Up @@ -32,6 +31,7 @@ use alkahest_renderer::{
util::{black_magic::EntityRefDarkMagic, Hocus},
};
use bevy_ecs::{entity::Entity, prelude::EntityRef, system::Commands};
pub use channels::FnvWordlist;
use egui::{Align2, Color32, FontId, Key, RichText, Ui, Widget};
use glam::{Quat, Vec3};
use winit::window::Window;
Expand Down
Loading

0 comments on commit 15d5ce1

Please sign in to comment.