mirror of
https://github.com/a-b-street/abstreet.git
synced 2024-11-24 09:24:26 +03:00
porting extract_colorscheme.py to rust
This commit is contained in:
parent
3eb5270d51
commit
417615cf3a
@ -26,8 +26,3 @@ Cross-compilation notes: https://github.com/rust-embedded/cross
|
||||
|
||||
cross build --release --target x86_64-pc-windows-gnu --bin editor
|
||||
wine target/x86_64-pc-windows-gnu/release/editor.exe data/maps/montlake_no_edits.abst
|
||||
|
||||
Problems:
|
||||
- build.rs tries to invoke python
|
||||
- port the python script to rust ;)
|
||||
- GLSL 1.40 not supported (in wine at least)
|
||||
|
@ -3,8 +3,7 @@
|
||||
## Dependencies
|
||||
|
||||
To build, you need a Linux-like environment with `bash`, `wget`, `unzip`, etc.
|
||||
You also need `python` for the editor crate to build, and `osmosis` for the
|
||||
import script.
|
||||
You also `osmosis` for the import script.
|
||||
|
||||
At runtime if you want to use the screen-capture plugin, you need `scrot`.
|
||||
|
||||
|
@ -22,3 +22,6 @@ serde = "1.0.89"
|
||||
serde_derive = "1.0.89"
|
||||
sim = { path = "../sim" }
|
||||
structopt = "0.2.15"
|
||||
|
||||
[build-dependencies]
|
||||
walkdir = "2.2.7"
|
||||
|
102
editor/build.rs
102
editor/build.rs
@ -1,14 +1,100 @@
|
||||
use std::collections::BTreeMap;
|
||||
use std::env;
|
||||
use std::path::Path;
|
||||
use std::process::Command;
|
||||
use std::fs::File;
|
||||
use std::io::{Read, Write};
|
||||
use walkdir::WalkDir;
|
||||
|
||||
// TODO See https://github.com/dtolnay/inventory for an alternate approach.
|
||||
fn main() {
|
||||
let output = Path::new(&env::var("OUT_DIR").unwrap()).join("init_colors.rs");
|
||||
let mut mapping: BTreeMap<String, String> = BTreeMap::new();
|
||||
for entry in WalkDir::new("src") {
|
||||
let path = format!("{}", entry.unwrap().into_path().display());
|
||||
if path.ends_with(".rs") && path != "src/colors.rs" {
|
||||
for (k, v) in read_file(&path) {
|
||||
if mapping.contains_key(&k) {
|
||||
panic!("Color {} defined twice", k);
|
||||
}
|
||||
mapping.insert(k, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assert!(Command::new("/usr/bin/python2")
|
||||
.args(&["extract_colorscheme.py", output.to_str().unwrap()])
|
||||
.status()
|
||||
.unwrap()
|
||||
.success());
|
||||
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("\"") {
|
||||
src = src[1..].to_string();
|
||||
}
|
||||
src = src[1..].to_string();
|
||||
|
||||
// Read the key until the closing "
|
||||
let mut key = String::new();
|
||||
while !src.starts_with("\"") {
|
||||
key.push(src.chars().next().unwrap());
|
||||
src = src[1..].to_string();
|
||||
}
|
||||
src = src[1..].to_string();
|
||||
|
||||
// Look for the ,
|
||||
while !src.starts_with(",") {
|
||||
src = src[1..].to_string();
|
||||
}
|
||||
src = src[1..].to_string();
|
||||
|
||||
// Look for the Color
|
||||
while !src.starts_with("Color") {
|
||||
src = src[1..].to_string();
|
||||
}
|
||||
|
||||
// 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));
|
||||
src = src[1..].to_string();
|
||||
break;
|
||||
}
|
||||
} else if src.starts_with(",") && counter == 0 {
|
||||
value.pop();
|
||||
entries.push((key, value));
|
||||
src = src[1..].to_string();
|
||||
break;
|
||||
}
|
||||
src = src[1..].to_string();
|
||||
}
|
||||
} else {
|
||||
src = src[1..].to_string();
|
||||
}
|
||||
}
|
||||
|
||||
entries
|
||||
}
|
||||
|
@ -1,81 +0,0 @@
|
||||
#!/usr/bin/python2
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
def run():
|
||||
mapping = {}
|
||||
|
||||
for path, _, files in os.walk('src'):
|
||||
for f in files:
|
||||
if f.endswith('.rs') and f != 'colors.rs':
|
||||
for k, v in read_file(os.path.join(path, f)):
|
||||
if k in mapping:
|
||||
raise ValueError('Color {} defined twice'.format(k))
|
||||
mapping[k] = v
|
||||
|
||||
with open(sys.argv[1], 'w') as f:
|
||||
f.write('fn default_colors() -> HashMap<String, Color> {\n')
|
||||
f.write(' let mut m = HashMap::new();\n')
|
||||
for k in sorted(mapping.iterkeys()):
|
||||
f.write(' m.insert("{}".to_string(), {});\n'.format(k, mapping[k]))
|
||||
f.write(' m\n')
|
||||
f.write('}\n')
|
||||
|
||||
|
||||
def read_file(filename):
|
||||
entries = []
|
||||
with open(filename, 'r') as f:
|
||||
src = ''.join(f.readlines())
|
||||
while src:
|
||||
if src.startswith('get_def('):
|
||||
src = src[len('get_def('):]
|
||||
|
||||
# Look for the opening "
|
||||
while src[0] != '"':
|
||||
src = src[1:]
|
||||
src = src[1:]
|
||||
|
||||
# Read the key until the closing "
|
||||
key = ''
|
||||
while src[0] != '"':
|
||||
key += src[0]
|
||||
src = src[1:]
|
||||
src = src[1:]
|
||||
|
||||
# Look for the ,
|
||||
while src[0] != ',':
|
||||
src = src[1:]
|
||||
src = src[1:]
|
||||
|
||||
# Look for the Color
|
||||
while not src.startswith('Color'):
|
||||
src = src[1:]
|
||||
|
||||
# Wait for the ()'s to be mismatched, meaning we found the ) of the get_def()
|
||||
counter = 0
|
||||
value = ''
|
||||
while True:
|
||||
value += src[0]
|
||||
if src[0] == '(':
|
||||
counter += 1
|
||||
elif src[0] == ')':
|
||||
counter -= 1
|
||||
if counter == -1:
|
||||
value = value[:-1]
|
||||
entries.append((key, value))
|
||||
src = src[1:]
|
||||
break
|
||||
elif src[0] == ',' and counter == 0:
|
||||
value = value[:-1]
|
||||
entries.append((key, value))
|
||||
src = src[1:]
|
||||
break
|
||||
src = src[1:]
|
||||
else:
|
||||
src = src[1:]
|
||||
return entries
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run()
|
Loading…
Reference in New Issue
Block a user