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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
use core::{assets::asset::AssetId, id::ID};
use std::collections::HashMap;

#[derive(Debug)]
pub enum ResourceMappingEntry<R, V> {
    Resource(ID<R>),
    VirtualResource(ID<V>, ID<R>),
}

impl<R, V> ResourceMappingEntry<R, V> {
    pub fn resource(&self) -> Option<ID<R>> {
        match self {
            Self::Resource(id) => Some(*id),
            _ => None,
        }
    }

    pub fn virtual_resource(&self) -> Option<(ID<V>, ID<R>)> {
        match self {
            Self::VirtualResource(vid, rid) => Some((*vid, *rid)),
            _ => None,
        }
    }
}

impl<R, V> Clone for ResourceMappingEntry<R, V> {
    fn clone(&self) -> Self {
        match self {
            Self::Resource(r) => Self::Resource(*r),
            Self::VirtualResource(v, r) => Self::VirtualResource(*v, *r),
        }
    }
}

type ResourceMappingValue<R, V> = (Option<AssetId>, ResourceMappingEntry<R, V>, bool);

#[derive(Debug)]
pub struct ResourceMapping<R, V = ()> {
    map: HashMap<AssetId, String>,
    table: HashMap<String, ResourceMappingValue<R, V>>,
    removed: Vec<String>,
}

impl<R, V> Default for ResourceMapping<R, V> {
    fn default() -> Self {
        Self {
            map: Default::default(),
            table: Default::default(),
            removed: Default::default(),
        }
    }
}

impl<R, V> ResourceMapping<R, V> {
    pub fn has_asset(&self, asset_id: AssetId) -> bool {
        self.map.contains_key(&asset_id)
    }

    pub fn has_name(&self, name: &str) -> bool {
        self.table.contains_key(name)
    }

    pub fn maintain(&mut self) {
        for (_, _, added) in self.table.values_mut() {
            *added = false;
        }
        self.removed.clear();
    }

    pub fn map_asset_entry(
        &mut self,
        name: impl ToString,
        asset_id: AssetId,
        entry: ResourceMappingEntry<R, V>,
    ) {
        self.map.insert(asset_id, name.to_string());
        self.table
            .insert(name.to_string(), (Some(asset_id), entry, true));
    }

    pub fn map_asset_resource(
        &mut self,
        name: impl ToString,
        asset_id: AssetId,
        resource_id: ID<R>,
    ) {
        self.map_asset_entry(name, asset_id, ResourceMappingEntry::Resource(resource_id));
    }

    pub fn map_asset_virtual_resource(
        &mut self,
        name: impl ToString,
        asset_id: AssetId,
        virtual_resource_id: ID<V>,
        resource_id: ID<R>,
    ) {
        self.map_asset_entry(
            name,
            asset_id,
            ResourceMappingEntry::VirtualResource(virtual_resource_id, resource_id),
        );
    }

    pub fn map_entry(&mut self, name: impl ToString, entry: ResourceMappingEntry<R, V>) {
        self.table.insert(name.to_string(), (None, entry, true));
    }

    pub fn map_resource(&mut self, name: impl ToString, resource_id: ID<R>) {
        self.map_entry(name, ResourceMappingEntry::Resource(resource_id));
    }

    pub fn map_virtual_resource(
        &mut self,
        name: impl ToString,
        virtual_resource_id: ID<V>,
        resource_id: ID<R>,
    ) {
        self.map_entry(
            name,
            ResourceMappingEntry::VirtualResource(virtual_resource_id, resource_id),
        );
    }

    pub fn unmap_asset(&mut self, asset_id: AssetId) -> Option<ResourceMappingEntry<R, V>> {
        if let Some(name) = self.map.remove(&asset_id) {
            let result = self.table.remove(&name).map(|(_, entry, _)| entry);
            self.removed.push(name);
            return result;
        }
        None
    }

    pub fn unmap_asset_resource(&mut self, asset_id: AssetId) -> Option<ID<R>> {
        self.unmap_asset(asset_id)?.resource()
    }

    pub fn unmap_asset_virtual_resource(&mut self, asset_id: AssetId) -> Option<(ID<V>, ID<R>)> {
        self.unmap_asset(asset_id)?.virtual_resource()
    }

    pub fn unmap_name(&mut self, name: &str) -> Option<ResourceMappingEntry<R, V>> {
        self.removed.push(name.to_owned());
        self.table.remove(name).map(|(_, entry, _)| entry)
    }

    pub fn unmap_name_resource(&mut self, name: &str) -> Option<ID<R>> {
        self.unmap_name(name)?.resource()
    }

    pub fn unmap_name_virtual_resource(&mut self, name: &str) -> Option<(ID<V>, ID<R>)> {
        self.unmap_name(name)?.virtual_resource()
    }

    pub fn entry_by_name(&self, name: &str) -> Option<&ResourceMappingEntry<R, V>> {
        self.table.get(name).map(|(_, entry, _)| entry)
    }

    pub fn resource_by_name(&self, name: &str) -> Option<ID<R>> {
        self.entry_by_name(name)?.resource()
    }

    pub fn virtual_resource_by_name(&self, name: &str) -> Option<(ID<V>, ID<R>)> {
        self.entry_by_name(name)?.virtual_resource()
    }

    pub fn entry_by_asset(&self, asset_id: AssetId) -> Option<&ResourceMappingEntry<R, V>> {
        if let Some(name) = self.map.get(&asset_id) {
            return self.entry_by_name(name);
        }
        None
    }

    pub fn resource_by_asset(&self, asset_id: AssetId) -> Option<ID<R>> {
        self.entry_by_asset(asset_id)?.resource()
    }

    pub fn virtual_resource_by_asset(&self, asset_id: AssetId) -> Option<(ID<V>, ID<R>)> {
        self.entry_by_asset(asset_id)?.virtual_resource()
    }

    pub fn entries(&self) -> impl Iterator<Item = (&str, &ResourceMappingEntry<R, V>)> {
        self.table
            .iter()
            .map(|(name, (_, entry, _))| (name.as_str(), entry))
    }

    pub fn resources(&self) -> impl Iterator<Item = (&str, ID<R>)> {
        self.table
            .iter()
            .filter_map(|(name, (_, entry, _))| Some((name.as_str(), entry.resource()?)))
    }

    pub fn virtual_resources(&self) -> impl Iterator<Item = (&str, ID<V>, ID<R>)> {
        self.table.iter().filter_map(|(name, (_, entry, _))| {
            let (vid, id) = entry.virtual_resource()?;
            Some((name.as_str(), vid, id))
        })
    }

    pub fn entries_added(&self) -> impl Iterator<Item = (&str, &ResourceMappingEntry<R, V>)> {
        self.table
            .iter()
            .filter(|(_, (_, _, added))| *added)
            .map(|(name, (_, entry, _))| (name.as_str(), entry))
    }

    pub fn resources_added(&self) -> impl Iterator<Item = (&str, ID<R>)> {
        self.table
            .iter()
            .filter(|(_, (_, _, added))| *added)
            .filter_map(|(name, (_, entry, _))| Some((name.as_str(), entry.resource()?)))
    }

    pub fn virtual_resources_added(&self) -> impl Iterator<Item = (&str, ID<V>, ID<R>)> {
        self.table
            .iter()
            .filter(|(_, (_, _, added))| *added)
            .filter_map(|(name, (_, entry, _))| {
                let (vid, id) = entry.virtual_resource()?;
                Some((name.as_str(), vid, id))
            })
    }

    pub fn removed(&self) -> impl Iterator<Item = &str> {
        self.removed.iter().map(|name| name.as_str())
    }
}