Skip to content

Commit

Permalink
Expressions simplified descriptions from hash
Browse files Browse the repository at this point in the history
  • Loading branch information
makscee committed Feb 8, 2025
1 parent 56e5365 commit 7fb2d7a
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 2 deletions.
1 change: 1 addition & 0 deletions assets/ron/descriptions.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(expressions: [(RandomUnit(AllEnemyUnits), "random enemy")])
2 changes: 1 addition & 1 deletion src/plugins/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl AdminPlugin {
(VarName::lvl, 3.into()),
(VarName::tier, 2.into()),
]),
expanded: false,
expanded: true,
reaction: Reaction {
trigger: Trigger::BattleStart,
actions: Actions(
Expand Down
6 changes: 5 additions & 1 deletion src/resources/game_assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ pub fn global_settings_local() -> &'static GlobalSettings {
pub fn parse_content_tree() {
let mut houses: HashMap<String, House> = default();
const CONTENT_DIR: Dir = include_dir!("./assets/ron/");
const GLOBAL_SETTINGS_STR: &str = include_str!("../.././assets/ron/_.global_settings.ron");
const GLOBAL_SETTINGS_STR: &str = include_str!("../../assets/ron/_.global_settings.ron");
const DESCRIPTIONS: &str = include_str!("../../assets/ron/descriptions.ron");

GLOBAL_SETTINGS
.set(
Expand All @@ -42,6 +43,9 @@ pub fn parse_content_tree() {
)
.unwrap();

let descriptions: Descriptions = ron::from_str(DESCRIPTIONS).unwrap();
descriptions.set();

for dir in include_dir!("./assets/ron/")
.get_dir("houses")
.unwrap()
Expand Down
1 change: 1 addition & 0 deletions ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ itertools = { workspace = true }
egui_extras = { workspace = true }
parking_lot = { workspace = true }
color-hex = { workspace = true }
serde = { workspace = true }
3 changes: 3 additions & 0 deletions ui/src/cstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,9 @@ impl ToCstr for Expression {
self.as_ref().cstr_c(self.color())
}
fn cstr_expanded(&self) -> Cstr {
if let Some(description) = Descriptions::get(self) {
return description.clone();
}
let inner = match self {
Expression::One
| Expression::Zero
Expand Down
31 changes: 31 additions & 0 deletions ui/src/descriptions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::hash::{DefaultHasher, Hash, Hasher};

use once_cell::sync::OnceCell;
use serde::{Deserialize, Serialize};

use super::*;

#[derive(Debug, Serialize, Deserialize)]
pub struct Descriptions {
expressions: Vec<(Expression, String)>,
}

static DESCRIPTIONS: OnceCell<HashMap<u64, String>> = OnceCell::new();

impl Descriptions {
pub fn set(self) {
dbg!(&self);
let mut map: HashMap<u64, String> = default();
for (x, text) in self.expressions {
let mut state = DefaultHasher::new();
x.hash(&mut state);
map.insert(state.finish(), text);
}
DESCRIPTIONS.set(map).unwrap();
}
pub fn get(key: impl Hash) -> Option<&'static String> {
let mut state = DefaultHasher::new();
key.hash(&mut state);
DESCRIPTIONS.get().unwrap().get(&state.finish())
}
}
2 changes: 2 additions & 0 deletions ui/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
mod cstr;
mod descriptions;
mod enum_colors;
mod ui;
mod utils;
mod widgets;

pub use cstr::*;
pub use descriptions::*;
pub use enum_colors::*;
pub use ui::*;
pub use utils::*;
Expand Down

0 comments on commit 7fb2d7a

Please sign in to comment.