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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use crate::{
    utils, FileHolder, Resource, SerializedData, SerializedDataError, YyResource,
    YyResourceHandler, YypBoss,
};
use std::path::Path;
use yy_typings::{shader::Shader, utils::TrailingCommaUtility, ViewPath};

impl YyResource for Shader {
    type AssociatedData = ShaderFile;

    const SUBPATH_NAME: &'static str = "shaders";
    const RESOURCE: Resource = Resource::Shader;

    fn name(&self) -> &str {
        &self.name
    }

    fn set_name(&mut self, name: String) {
        self.name = name;
    }

    fn set_parent_view_path(&mut self, vp: ViewPath) {
        self.parent = vp;
    }

    fn parent_view_path(&self) -> ViewPath {
        self.parent.clone()
    }

    fn get_handler(yyp_boss: &YypBoss) -> &YyResourceHandler<Self> {
        &yyp_boss.shaders
    }

    fn get_handler_mut(yyp_boss: &mut YypBoss) -> &mut YyResourceHandler<Self> {
        &mut yyp_boss.shaders
    }

    fn serialize_associated_data(
        &self,
        wd: &Path,
        data: &Self::AssociatedData,
    ) -> anyhow::Result<()> {
        let vtx_path = wd.join(&self.name).with_extension(Self::VERT_FILE_ENDING);
        let frag_path = wd.join(&self.name).with_extension(Self::FRAG_FILE_ENDING);

        std::fs::write(vtx_path, &data.vertex)?;
        std::fs::write(frag_path, &data.pixel)?;

        Ok(())
    }

    fn deserialize_associated_data(
        &self,
        wd: &Path,
        _: &TrailingCommaUtility,
    ) -> Result<Self::AssociatedData, SerializedDataError> {
        let vtx_path = wd.join(&self.name).with_extension(Self::VERT_FILE_ENDING);
        let frag_path = wd.join(&self.name).with_extension(Self::FRAG_FILE_ENDING);

        let assoc_data = Self::AssociatedData {
            vertex: std::fs::read_to_string(vtx_path).map_err(|e| {
                SerializedDataError::CouldNotDeserializeFile(crate::FileSerializationError::Io(
                    e.to_string(),
                ))
            })?,
            pixel: std::fs::read_to_string(frag_path).map_err(|e| {
                SerializedDataError::CouldNotDeserializeFile(crate::FileSerializationError::Io(
                    e.to_string(),
                ))
            })?,
        };

        Ok(assoc_data)
    }

    fn serialize_associated_data_into_data(
        _: &std::path::Path,
        associated_data: &Self::AssociatedData,
    ) -> Result<SerializedData, SerializedDataError> {
        match serde_json::to_string_pretty(associated_data) {
            Ok(data) => Ok(SerializedData::Value { data }),
            Err(e) => Err(e.into()),
        }
    }

    fn deserialize_associated_data_from_data(
        &self,
        incoming_data: &SerializedData,
        tcu: &TrailingCommaUtility,
    ) -> Result<Self::AssociatedData, SerializedDataError> {
        match incoming_data {
            SerializedData::Value { data: v } => {
                serde_json::from_str(v).map_err(|e| SerializedDataError::InnerError(e.to_string()))
            }
            SerializedData::Filepath { data: v } => {
                utils::deserialize_json_tc(v, tcu).map_err(|e| e.into())
            }
            SerializedData::DefaultValue => Ok(Self::AssociatedData::default()),
        }
    }

    fn cleanup_on_replace(&self, _: impl FileHolder) {
        todo!()
    }
}

#[derive(
    Debug,
    Default,
    PartialEq,
    Eq,
    Ord,
    PartialOrd,
    Clone,
    Hash,
    serde::Serialize,
    serde::Deserialize,
)]
pub struct ShaderFile {
    pub vertex: String,
    pub pixel: String,
}

#[derive(Debug, PartialEq, Eq, Ord, PartialOrd, Clone, Copy, Hash, strum_macros::EnumIter)]
pub enum ShaderKind {
    Vertex,
    Frag,
}

impl ShaderKind {
    pub fn file_ending(&self) -> &'static str {
        match self {
            ShaderKind::Vertex => Shader::VERT_FILE_ENDING,
            ShaderKind::Frag => Shader::FRAG_FILE_ENDING,
        }
    }

    pub fn iter() -> impl IntoIterator<Item = ShaderKind> {
        <Self as strum::IntoEnumIterator>::iter()
    }
}

impl std::ops::Index<ShaderKind> for ShaderFile {
    type Output = String;

    fn index(&self, index: ShaderKind) -> &Self::Output {
        match index {
            ShaderKind::Vertex => &self.vertex,
            ShaderKind::Frag => &self.pixel,
        }
    }
}

impl std::ops::IndexMut<ShaderKind> for ShaderFile {
    fn index_mut(&mut self, index: ShaderKind) -> &mut Self::Output {
        match index {
            ShaderKind::Vertex => &mut self.vertex,
            ShaderKind::Frag => &mut self.pixel,
        }
    }
}