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
use crate::{ResourceVersion, Tags, ViewPath};
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use smart_default::SmartDefault;
#[derive(Debug, Serialize, Deserialize, SmartDefault, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Shader {
#[serde(rename = "type")]
pub shader_type: ShaderType,
pub parent: ViewPath,
pub resource_version: ResourceVersion,
pub name: String,
pub tags: Tags,
pub resource_type: ConstGmShader,
}
impl Shader {
pub const FRAG_FILE_ENDING: &'static str = "fsh";
pub const VERT_FILE_ENDING: &'static str = "vsh";
}
#[derive(Debug, Copy, Serialize, Deserialize, SmartDefault, PartialEq, Eq, Clone)]
pub enum ConstGmShader {
#[serde(rename = "GMShader")]
#[default]
Const,
}
#[derive(Debug, Serialize_repr, Deserialize_repr, SmartDefault, PartialEq, Clone, Copy)]
#[repr(u8)]
pub enum ShaderType {
#[default]
GlslEs = 1,
Glsl,
Hlsl,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{utils::TrailingCommaUtility, ViewPathLocation};
use include_dir::{include_dir, Dir, DirEntry};
use pretty_assertions::assert_eq;
#[test]
fn trivial_sprite_parsing() {
let dir: Dir = include_dir!("data/shaders");
let tcu = TrailingCommaUtility::new();
for file in dir.find("**/*.yy").unwrap() {
if let DirEntry::File(file) = file {
println!("parsing {}", file.path);
let our_str = std::str::from_utf8(file.contents()).unwrap();
let our_str = tcu.clear_trailing_comma(our_str);
serde_json::from_str::<Shader>(&our_str).unwrap();
}
}
}
#[test]
fn deep_equality() {
let file_raw = include_str!("../../data/shaders/sh_draw_light_to_screen.yy");
let file_parsed: Shader =
serde_json::from_str(&TrailingCommaUtility::clear_trailing_comma_once(file_raw))
.unwrap();
let script = Shader {
shader_type: ShaderType::GlslEs,
parent: ViewPath {
name: "shaders".to_string(),
path: ViewPathLocation("folders/Objects/system/lighting/shaders.yy".to_string()),
},
resource_version: ResourceVersion::default(),
name: "sh_draw_light_to_screen".to_string(),
tags: vec![],
resource_type: ConstGmShader::Const,
};
assert_eq!(file_parsed, script);
}
}