use std::{env, fmt::Write, fs, path::Path};
use syn::{Expr, ExprArray, ExprLit, ExprTuple, Item, Lit};
fn to_const_name(s: &str) -> String {
let mut seen_underscore = false;
s.chars()
.map(|c| {
if c == ' ' {
'_'
} else {
c.to_ascii_uppercase()
}
})
.filter(|c| !matches!(c, '/' | '\'' | '+' | '?' | '-' | '"' | '.'))
.filter(|c| {
let keep = !(*c == '_' && seen_underscore);
seen_underscore = *c == '_';
keep
})
.collect()
}
fn handle_tuple(buffer: &mut String, tuple: ExprTuple) {
let mut id = 0i32;
let mut name = String::new();
for item in tuple.elems {
if let Expr::Lit(ExprLit { lit, .. }) = item {
match lit {
Lit::Int(int) => id = int.base10_parse().unwrap(),
Lit::Str(s) => name = s.value(),
_ => {}
}
}
}
let const_name = to_const_name(&name);
writeln!(buffer, " pub const {const_name}: i32 = {id};").unwrap();
}
fn get_map_from_line(file: &str, start_str: &str, gpi: &mut Vec<String>) -> String {
let mut out_str = String::new();
let mut seen_map = false;
for line in file.split('\n') {
if seen_map {
if line.starts_with("};") {
break;
}
if line.trim_start().starts_with("/*") || line.trim_start().starts_with("//") {
continue;
}
let mut split = line.trim().split(" => (");
let id = split.next().unwrap();
let mut tuple_split = split.next().unwrap().split(", ");
let desc = tuple_split.next().unwrap();
let const_name = to_const_name(desc);
let prop_type = tuple_split.next().unwrap_or_default();
writeln!(out_str, " pub const {const_name}: u16 = {id};").unwrap();
if prop_type.contains("GDObjPropType::Group") {
gpi.push(id.to_string());
}
} else if line.starts_with(start_str) {
seen_map = true;
}
}
out_str
}
fn main() {
let mut objects_out_str = String::new();
let mut group_property_ids = Vec::new();
let file = fs::read_to_string("src/cclocallevels/properties.rs").unwrap();
let ast: syn::File = syn::parse_str(&file).unwrap();
for item in ast.items {
if let Item::Const(c) = item
&& let Expr::Reference(expr_ref) = *c.expr
&& let Expr::Array(ExprArray { elems, .. }) = *expr_ref.expr
&& c.ident == "OBJECT_NAMES"
{
objects_out_str = String::with_capacity(elems.len() * 48);
for elem in elems {
if let Expr::Tuple(tuple) = elem {
handle_tuple(&mut objects_out_str, tuple);
}
}
}
}
let properties_out_str = get_map_from_line(
&file,
"pub static PROPERTY_TABLE: Map<u16, (&'static str, GDObjPropType)> = phf_map!",
&mut group_property_ids,
);
let level_header_props = get_map_from_line(
&file,
"pub static LEVEL_HEADER_PROPERTIES: Map<u16, (&'static str, HeaderValueType)> = phf_map!",
&mut group_property_ids,
);
let gids_len = group_property_ids.len();
let group_ids_literal = group_property_ids.join(", ");
let out_str = format!(
"\
/// Object IDs
pub mod objects {{
{objects_out_str}}}
/// Object property IDs
pub mod properties {{
{properties_out_str}}}
/// Level header properties
pub mod level_header {{
{level_header_props}}}
/// Property metadata submodule
pub mod metadata {{
pub static GROUP_PROPERTY_IDS: &[u16; {gids_len}] = &[{group_ids_literal}];
}}
"
);
let out_dir = env::var_os("OUT_DIR").unwrap();
let out_path = "ids.rs";
fs::write(Path::new(&out_dir).join(out_path), out_str).unwrap();
}