abstreet/game/build.rs

114 lines
3.4 KiB
Rust
Raw Normal View History

2019-03-12 03:18:30 +03:00
use std::collections::BTreeMap;
use std::env;
use std::ffi::OsStr;
2019-03-12 03:18:30 +03:00
use std::fs::File;
use std::io::{Read, Write};
use std::path::PathBuf;
2019-03-12 03:18:30 +03:00
use walkdir::WalkDir;
2018-12-13 03:58:46 +03:00
2019-01-24 03:08:36 +03:00
// TODO See https://github.com/dtolnay/inventory for an alternate approach.
2018-12-13 03:58:46 +03:00
fn main() {
built::write_built_file().expect("Failed to acquire build-time information");
2019-03-12 03:18:30 +03:00
let mut mapping: BTreeMap<String, String> = BTreeMap::new();
for entry in WalkDir::new("src") {
let path = entry.unwrap().into_path();
if path.extension() == Some(OsStr::new("rs")) && path != PathBuf::from("src/helpers.rs") {
for (k, v) in read_file(&format!("{}", path.display())) {
2019-03-12 03:18:30 +03:00
if mapping.contains_key(&k) {
panic!("Color {} defined twice", k);
}
mapping.insert(k, v);
}
}
}
2019-03-12 03:18:30 +03:00
let mut f = File::create(format!("{}/init_colors.rs", env::var("OUT_DIR").unwrap())).unwrap();
writeln!(f, "fn default_colors() -> HashMap<String, Color> {{").unwrap();
writeln!(f, " let mut m = HashMap::new();").unwrap();
for (k, v) in mapping {
writeln!(f, " m.insert(\"{}\".to_string(), {});", k, v).unwrap();
}
writeln!(f, " m").unwrap();
writeln!(f, "}}").unwrap();
}
fn read_file(path: &str) -> Vec<(String, String)> {
let mut src = {
let mut s = String::new();
let mut f = File::open(path).unwrap();
f.read_to_string(&mut s).unwrap();
s
};
let mut entries: Vec<(String, String)> = Vec::new();
while !src.is_empty() {
if src.starts_with("get_def(") {
src = src["get_def(".len()..].to_string();
// Look for the opening "
while !src.starts_with("\"") {
2020-01-13 22:34:43 +03:00
shift(&mut src);
2019-03-12 03:18:30 +03:00
}
2020-01-13 22:34:43 +03:00
shift(&mut src);
2019-03-12 03:18:30 +03:00
// Read the key until the closing "
let mut key = String::new();
while !src.starts_with("\"") {
key.push(src.chars().next().unwrap());
2020-01-13 22:34:43 +03:00
shift(&mut src);
2019-03-12 03:18:30 +03:00
}
2020-01-13 22:34:43 +03:00
shift(&mut src);
2019-03-12 03:18:30 +03:00
// Look for the ,
while !src.starts_with(",") {
2020-01-13 22:34:43 +03:00
shift(&mut src);
2019-03-12 03:18:30 +03:00
}
2020-01-13 22:34:43 +03:00
shift(&mut src);
2019-03-12 03:18:30 +03:00
// Look for the Color
while !src.starts_with("Color") {
2020-01-13 22:34:43 +03:00
shift(&mut src);
2019-03-12 03:18:30 +03:00
}
// Wait for the ()'s to be mismatched, meaning we found the ) of the get_def()
let mut counter = 0;
let mut value = String::new();
loop {
value.push(src.chars().next().unwrap());
if src.starts_with("(") {
counter += 1;
} else if src.starts_with(")") {
counter -= 1;
if counter == -1 {
value.pop();
entries.push((key, value));
2020-01-13 22:34:43 +03:00
shift(&mut src);
2019-03-12 03:18:30 +03:00
break;
}
} else if src.starts_with(",") && counter == 0 {
value.pop();
entries.push((key, value));
2020-01-13 22:34:43 +03:00
shift(&mut src);
2019-03-12 03:18:30 +03:00
break;
}
2020-01-13 22:34:43 +03:00
shift(&mut src);
2019-03-12 03:18:30 +03:00
}
} else {
2020-01-13 22:34:43 +03:00
shift(&mut src);
2019-03-12 03:18:30 +03:00
}
}
entries
2018-12-13 03:58:46 +03:00
}
2020-01-13 22:34:43 +03:00
fn shift(s: &mut String) {
*s = s
.chars()
.next()
.map(|c| &s[c.len_utf8()..])
.unwrap_or("")
.to_string();
}