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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use super::{PathStrExt, ViewPathLocationExt};
use log::error;
use maplit::hashmap;
use std::{collections::HashMap, hash::Hash};
use thiserror::Error;
use yy_typings::{FilesystemPath, ViewPath, ViewPathLocation, YypFolder, YypResource};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FolderGraph {
    pub name: String,
    pub path_to_parent: Option<ViewPath>,
    pub files: HashMap<String, FileMember>,
    pub folders: HashMap<String, SubfolderMember>,
}

impl Default for FolderGraph {
    fn default() -> Self {
        FolderGraph {
            name: String::new(),
            path_to_parent: None,
            files: hashmap![],
            folders: hashmap![],
        }
    }
}

impl Hash for FolderGraph {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.name.hash(state);
    }
}

impl FolderGraph {
    pub fn root() -> FolderGraph {
        FolderGraph {
            name: "folders".to_string(),
            ..FolderGraph::default()
        }
    }

    pub fn new(name: String, parent: ViewPath) -> FolderGraph {
        FolderGraph {
            name,
            path_to_parent: Some(parent),
            ..FolderGraph::default()
        }
    }

    pub fn view_path(&self) -> ViewPath {
        ViewPath {
            name: self.name.to_string(),
            path: match &self.path_to_parent {
                Some(parent_path) => parent_path.path.join(&self.name),
                None => ViewPathLocation::root(),
            },
        }
    }

    pub fn find_subfolder_mut(
        &mut self,
        view_path: &ViewPath,
    ) -> Result<&mut FolderGraph, FolderGraphError> {
        if view_path.name == self.name {
            Ok(self)
        } else {
            let mut folder = self;

            for path in view_path.path.component_paths() {
                let path = path.trim_yy();
                folder = &mut folder
                    .folders
                    .get_mut(path)
                    .ok_or(FolderGraphError::PathNotFound)?
                    .child;
            }

            Ok(folder)
        }
    }

    /// This returns the max_suborder within this Folder. In a sense,
    /// this is the "size" of the folder's children, though due to A-Z sorting
    /// in the Gms2 IDE, order and size are not always directly related.
    ///
    /// Given the folder:
    /// ```norun
    /// Sprites/
    ///     - spr_player
    ///     - OtherMembers/
    ///     - spr_enemy
    /// ```
    /// `max_suborder` will return `2`, which is what `spr_enemy`'s suborder would be.
    pub fn max_suborder(&self) -> Option<usize> {
        let mut output = None;

        if let Some(file_max) = self.files.values().map(|file| file.order).max() {
            if file_max >= output.unwrap_or_default() {
                output = Some(file_max);
            }
        }

        if let Some(file_max) = self.folders.values().map(|file| file.order).max() {
            if file_max >= output.unwrap_or_default() {
                output = Some(file_max);
            }
        }

        output
    }
}

#[derive(Debug, Error)]
pub enum FolderGraphError {
    #[error("path was not found")]
    PathNotFound,
    #[error("folder already existed at that location")]
    FolderAlreadyPresent,
    #[error("file already existed at that location")]
    FileAlreadyPresent,
    #[error("foldergraph is out of sync with internal Yyp")]
    FolderGraphOutofSyncWithYyp,
}

pub trait FolderGraphMember {
    type YypReference;

    /// Applies the State of the Folder Graph to the current YypResource which each
    /// folder graph member corresponds to. Essentially, this keeps the foldergraph and the yyp
    /// in sync.
    fn update_yyp(
        &self,
        yyp_resource: &mut Vec<Self::YypReference>,
    ) -> Result<(), FolderGraphError>;
}

#[derive(Debug, Clone, Eq)]
pub struct FileMember {
    pub child: FilesystemPath,
    pub order: usize,
}

impl PartialEq for FileMember {
    fn eq(&self, other: &Self) -> bool {
        self.child == other.child && self.order <= other.order
    }
}

impl FolderGraphMember for FileMember {
    type YypReference = YypResource;

    fn update_yyp(&self, files: &mut Vec<YypResource>) -> Result<(), FolderGraphError> {
        let yyp_resource = files
            .iter_mut()
            .find(|f| f.id.name == self.child.name)
            .ok_or(FolderGraphError::FolderGraphOutofSyncWithYyp)?;

        yyp_resource.order = self.order;
        yyp_resource.id.path = self.child.path.clone();

        Ok(())
    }
}

#[derive(Debug, Clone, Eq)]
pub struct SubfolderMember {
    pub child: FolderGraph,
    pub order: usize,
}

impl PartialEq for SubfolderMember {
    fn eq(&self, other: &Self) -> bool {
        self.child == other.child && self.order <= other.order
    }
}

impl FolderGraphMember for SubfolderMember {
    type YypReference = YypFolder;
    fn update_yyp(&self, folders: &mut Vec<YypFolder>) -> Result<(), FolderGraphError> {
        let yyp_folder = folders
            .iter_mut()
            .find(|f| f.name == self.child.name)
            .ok_or(FolderGraphError::FolderGraphOutofSyncWithYyp)?;

        yyp_folder.order = self.order;
        yyp_folder.folder_path = self.child.view_path().path;

        Ok(())
    }
}