enum processing

This commit is contained in:
Janis 2023-04-18 23:24:07 +02:00
parent 8402f505bc
commit a2233bb3b4

View file

@ -5,7 +5,8 @@ pub mod types;
pub mod sdk { pub mod sdk {
use std::{ use std::{
borrow::Cow, borrow::Cow,
collections::{hash_map::Entry, BTreeSet, HashMap, HashSet}, collections::{hash_map::Entry, BTreeMap, BTreeSet, HashMap, HashSet},
fmt::Display,
sync::Mutex, sync::Mutex,
}; };
@ -158,6 +159,7 @@ pub mod sdk {
UAnyType::UProperty(_) => {} UAnyType::UProperty(_) => {}
UAnyType::UEnum(obj) => { UAnyType::UEnum(obj) => {
let enm = Self::process_enum(obj)?; let enm = Self::process_enum(obj)?;
log::info!("enum: {enm}");
} }
UAnyType::UStruct(_) => {} UAnyType::UStruct(_) => {}
UAnyType::UFunction(_) => {} UAnyType::UFunction(_) => {}
@ -168,12 +170,44 @@ pub mod sdk {
} }
fn process_enum(enm: UEnum) -> anyhow::Result<Enum> { fn process_enum(enm: UEnum) -> anyhow::Result<Enum> {
// get all the variants
let values = enm let values = enm
.get_names() .get_names()
.iter() .iter()
.map(|name| name.get_name().unwrap_or("AnonymousVariant".to_string())) .enumerate()
.map(|name| canonicalize_name(&name).to_string()) // canonicalize the names into valid rust
.collect::<Vec<_>>(); .map(|(value, name)| {
let name = name.get_name().unwrap_or("AnonymousVariant".to_string());
let name = canonicalize_name(&name).to_string();
(value, name)
})
// store conflicts next to each other
.fold(
HashMap::<String, Vec<usize>>::new(),
|mut acc, (value, name)| {
match acc.entry(name) {
Entry::Occupied(mut entry) => {
entry.get_mut().push(value);
}
Entry::Vacant(entry) => {
entry.insert(vec![value]);
}
}
acc
},
);
// sort by value
let mut variants = BTreeMap::new();
values.into_iter().for_each(|(name, values)| {
if values.len() > 0 {
for (i, value) in values.into_iter().enumerate() {
variants.insert(value, format!("{name}{i}"));
}
} else {
variants.insert(values.into_iter().next().unwrap(), name);
}
});
let name = enm let name = enm
.as_uobject() .as_uobject()
@ -181,7 +215,10 @@ pub mod sdk {
.context("enum name could not be found")?; .context("enum name could not be found")?;
let name = canonicalize_name(&name).to_string(); let name = canonicalize_name(&name).to_string();
Ok(Enum { name, values }) Ok(Enum {
name,
values: variants.into_iter().map(|(_, name)| name).collect(),
})
} }
} }
@ -299,4 +336,14 @@ pub mod sdk {
name: String, name: String,
values: Vec<String>, values: Vec<String>,
} }
impl Display for Enum {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "enum {} {{", self.name)?;
for (i, name) in self.values.iter().enumerate() {
writeln!(f, "\t{} = {},", name, i)?;
}
writeln!(f, "}}\n")
}
}
} }