-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
108 lines (93 loc) · 3.29 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use std::fs;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
use convert_case::{Case, Casing};
fn main() -> std::io::Result<()> {
let mut text_out = Vec::new();
writeln!(&mut text_out, "// This file contains autogenerated texture static objects from images contained in the ./resources/textures directory")?;
writeln!(&mut text_out)?;
let mut images = vec![];
for path in fs::read_dir("./resources/textures/")? {
let path_str = path?.path().to_str().unwrap().to_string();
let image_name = path_str
.split("/")
.last()
.unwrap()
.to_string()
.split(".")
.next()
.unwrap()
.to_string()
.to_case(Case::Pascal);
if let Ok(img) = image::open(&Path::new(&path_str)) {
let img_bytes = img.as_bytes();
images.push((image_name.clone(), img.width(), img_bytes.len()));
File::create(format!(
"./src/image/.{}.bin",
image_name.to_case(Case::Snake)
))?
.write_all(img_bytes)?;
}
}
writeln!(&mut text_out, "use std::sync::Arc;")?;
writeln!(&mut text_out, "use std::fmt;")?;
writeln!(&mut text_out, "use serde::{{Deserialize, Serialize}};")?;
writeln!(&mut text_out, "use lazy_static::lazy_static;")?;
writeln!(&mut text_out, "use crate::image::ImageData;")?;
writeln!(&mut text_out)?;
writeln!(&mut text_out, "#[allow(dead_code)]")?;
writeln!(
&mut text_out,
"#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]"
)
.unwrap();
writeln!(&mut text_out, "{}", r#"pub enum ImageID {"#)?;
images
.iter()
.for_each(|x| writeln!(&mut text_out, " {},", x.0).unwrap());
writeln!(&mut text_out, "{}", r#"}"#)?;
writeln!(&mut text_out)?;
writeln!(&mut text_out, "impl fmt::Display for ImageID {{")?;
writeln!(
&mut text_out,
" fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {{"
)?;
writeln!(
&mut text_out,
"{}",
format!("{}", r#" write!(f, "{:?}", self)"#)
)?;
writeln!(&mut text_out, " }}")?;
writeln!(&mut text_out, "}}")?;
writeln!(&mut text_out)?;
writeln!(
&mut text_out,
"{}",
r##"pub fn get_const_image(image: ImageID) -> Arc<ImageData> {"##
)?;
writeln!(&mut text_out, "{}", r##" match image {"##)?;
images.into_iter().for_each(|x| {
writeln!(&mut text_out, " ImageID::{} => {{", x.0,).unwrap();
writeln!(
&mut text_out,
" lazy_static! {{ static ref {}: Arc<ImageData> = Arc::new(ImageData::new(ImageID::{}, {}, (*include_bytes!({})).into())); }}",
x.0.to_case(Case::UpperSnake),
x.0,
x.1,
format!(r#"".{}.bin""#, x.0.to_case(Case::Snake))
)
.unwrap();
writeln!(
&mut text_out,
" {}.clone()",
x.0.to_case(Case::UpperSnake),
)
.unwrap();
writeln!(&mut text_out, " }}").unwrap();
});
writeln!(&mut text_out, "{}", r#" }"#)?;
writeln!(&mut text_out, "{}", r#"}"#)?;
File::create("./src/image/image_consts.rs")?.write_all(&text_out)?;
Ok(())
}