hydrate-model 0.0.2

Game asset pipeline and authoring framework
Documentation
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
use crate::edit_context::EditContext;
use crate::{AssetId, AssetSourceId, DataSource, PathNodeRoot, PendingFileOperations};
use hydrate_base::hashing::HashMap;
use hydrate_base::uuid_path::{path_to_uuid, uuid_to_path};
use hydrate_data::{AssetLocation, HashObjectMode};
use hydrate_pipeline::{HydrateProjectConfiguration, ImportJobToQueue};
use hydrate_schema::SchemaNamedType;
use std::path::PathBuf;

struct FileMetadata {
    // size_in_bytes: u64,
    // last_modified_time: Option<SystemTime>,
}

impl FileMetadata {
    pub fn new(_metadata: &std::fs::Metadata) -> Self {
        FileMetadata {
            // size_in_bytes: metadata.len(),
            // last_modified_time: metadata.modified().ok()
        }
    }

    // pub fn has_changed(&self, metadata: &std::fs::Metadata) -> bool {
    //     self.size_in_bytes != metadata.len() || self.last_modified_time != metadata.modified().ok()
    // }
}

struct AssetDiskState {
    object_hash: u64,
    _file_metadata: FileMetadata,
}

pub struct FileSystemIdBasedDataSource {
    asset_source_id: AssetSourceId,
    file_system_root_path: PathBuf,

    // Any asset ID we know to exist on disk is in this list to help us quickly determine which
    // deleted IDs need to be cleaned up
    assets_disk_state: HashMap<AssetId, AssetDiskState>,

    path_node_root_schema: SchemaNamedType,
}

impl FileSystemIdBasedDataSource {
    fn is_asset_owned_by_this_data_source(
        &self,
        edit_context: &EditContext,
        asset_id: AssetId,
    ) -> bool {
        if edit_context.asset_schema(asset_id).unwrap().fingerprint()
            == self.path_node_root_schema.fingerprint()
        {
            return false;
        }

        //TODO: is_null means we default to using this source
        let root_location = edit_context
            .asset_location_chain(asset_id)
            .unwrap_or_default()
            .last()
            .cloned()
            .unwrap_or_else(AssetLocation::null);
        root_location.path_node_id().as_uuid() == *self.asset_source_id.uuid()
            || root_location.is_null()
    }

    pub fn asset_source_id(&self) -> AssetSourceId {
        self.asset_source_id
    }

    pub fn new<RootPathT: Into<PathBuf>>(
        file_system_root_path: RootPathT,
        edit_context: &mut EditContext,
        asset_source_id: AssetSourceId,
    ) -> Self {
        let path_node_root_schema = edit_context
            .schema_set()
            .find_named_type(PathNodeRoot::schema_name())
            .unwrap()
            .clone();

        let file_system_root_path = file_system_root_path.into();
        log::info!(
            "Creating file system asset data source {:?}",
            file_system_root_path,
        );

        FileSystemIdBasedDataSource {
            asset_source_id,
            file_system_root_path: file_system_root_path.into(),
            assets_disk_state: Default::default(),
            path_node_root_schema,
        }
    }

    fn path_for_asset(
        &self,
        asset_id: AssetId,
    ) -> PathBuf {
        uuid_to_path(&self.file_system_root_path, asset_id.as_uuid(), "af")
    }
}

impl DataSource for FileSystemIdBasedDataSource {
    fn is_generated_asset(
        &self,
        _asset_id: AssetId,
    ) -> bool {
        // this data source does not contain source files so can't have generated assets
        false
    }

    // fn asset_symbol_name(&self, asset_id: AssetId) -> Option<String> {
    //     None
    // }

    fn persist_generated_asset(
        &mut self,
        _edit_context: &mut EditContext,
        _asset_id: AssetId,
    ) {
        // this data source does not contain source files so can't have generated assets
    }

    #[profiling::function]
    fn load_from_storage(
        &mut self,
        _project_config: &HydrateProjectConfiguration,
        edit_context: &mut EditContext,
        _import_job_to_queue: &mut ImportJobToQueue,
    ) {
        profiling::scope!(&format!(
            "load_from_storage {:?}",
            self.file_system_root_path
        ));

        //
        // Delete all assets from the database owned by this data source
        //
        let mut assets_to_delete = Vec::default();
        for (asset_id, _) in edit_context.assets() {
            if self.is_asset_owned_by_this_data_source(edit_context, *asset_id) {
                assets_to_delete.push(*asset_id);
            }
        }

        for asset_to_delete in assets_to_delete {
            edit_context.delete_asset(asset_to_delete).unwrap();
        }

        self.assets_disk_state.clear();

        //
        // Recreate all assets from storage
        //
        let walker =
            globwalk::GlobWalkerBuilder::from_patterns(&self.file_system_root_path, &["**.af"])
                .file_type(globwalk::FileType::FILE)
                .build()
                .unwrap();

        for file in walker {
            if let Ok(file) = file {
                let file = dunce::canonicalize(&file.path()).unwrap();

                let asset_file_metadata = FileMetadata::new(&std::fs::metadata(&file).unwrap());

                //println!("asset file {:?}", file);
                let file_uuid = path_to_uuid(&self.file_system_root_path, &file).unwrap();
                let contents = std::fs::read_to_string(&file).unwrap();
                let default_asset_location =
                    AssetLocation::new(AssetId(*self.asset_source_id.uuid()));

                let schema_set = edit_context.schema_set().clone();
                crate::json_storage::AssetJson::load_asset_from_string(
                    edit_context,
                    &schema_set,
                    Some(file_uuid),
                    default_asset_location,
                    None,
                    &contents,
                )
                .unwrap();
                let asset_id = AssetId::from_uuid(file_uuid);

                let object_hash = edit_context
                    .data_set()
                    .hash_object(asset_id, HashObjectMode::FullObjectWithLocationId)
                    .unwrap();

                let old = self.assets_disk_state.insert(
                    asset_id,
                    AssetDiskState {
                        object_hash: object_hash,
                        _file_metadata: asset_file_metadata,
                    },
                );
                assert!(old.is_none());
            }
        }
    }

    fn flush_to_storage(
        &mut self,
        edit_context: &mut EditContext,
    ) {
        profiling::scope!(&format!(
            "flush_to_storage {:?}",
            self.file_system_root_path
        ));

        let mut pending_deletes = Vec::<AssetId>::default();
        let mut pending_writes = Vec::<AssetId>::default();

        for &asset_id in edit_context.assets().keys() {
            if self.is_asset_owned_by_this_data_source(edit_context, asset_id) {
                match self.assets_disk_state.get(&asset_id) {
                    None => {
                        // There is a newly created asset that has never been saved
                        pending_writes.push(asset_id);
                    }
                    Some(asset_disk_state) => {
                        let object_hash = edit_context
                            .data_set()
                            .hash_object(asset_id, HashObjectMode::FullObjectWithLocationId)
                            .unwrap();
                        if asset_disk_state.object_hash != object_hash {
                            // The object has been modified and no longer matches disk state
                            pending_writes.push(asset_id);
                        }
                    }
                }
            }
        }

        // Is there anything that's been deleted?
        for (&asset_id, _) in &self.assets_disk_state {
            if !edit_context.has_asset(asset_id)
                || !self.is_asset_owned_by_this_data_source(edit_context, asset_id)
            {
                // There is an asset that no longer exists, but the file is still on disk
                pending_deletes.push(asset_id);
            }
        }

        //
        // Save any created/updated assets
        //
        for asset_id in pending_writes {
            if asset_id.as_uuid() == *self.asset_source_id.uuid() {
                // never save the root asset
                continue;
            }

            let asset_info = edit_context.data_set().assets().get(&asset_id).unwrap();

            // If the asset doesn't have a location set or is set to the root of this data
            // source, serialize with a null location
            let asset_location = if asset_info.asset_location().is_null()
                || asset_info.asset_location().path_node_id().as_uuid()
                    == *self.asset_source_id.uuid()
            {
                None
            } else {
                Some(asset_info.asset_location())
            };

            let data = crate::json_storage::AssetJson::save_asset_to_string(
                edit_context.schema_set(),
                edit_context.assets(),
                asset_id,
                false, //don't include ID because we assume it by file name
                asset_location,
            );
            let file_path = self.path_for_asset(asset_id);

            if let Some(parent) = file_path.parent() {
                std::fs::create_dir_all(parent).unwrap();
            }

            std::fs::write(&file_path, data).unwrap();

            let object_hash = edit_context
                .data_set()
                .hash_object(asset_id, HashObjectMode::FullObjectWithLocationId)
                .unwrap();
            let asset_file_metadata = FileMetadata::new(&std::fs::metadata(&file_path).unwrap());

            self.assets_disk_state.insert(
                asset_id,
                AssetDiskState {
                    object_hash,
                    _file_metadata: asset_file_metadata,
                },
            );
        }

        //
        // Delete assets that no longer exist
        //
        for asset_id in pending_deletes {
            let file_path = self.path_for_asset(asset_id);
            std::fs::remove_file(&file_path).unwrap();
            self.assets_disk_state.remove(&asset_id);

            //TODO: Clean up empty parent dirs?
        }
    }

    fn edit_context_has_unsaved_changes(
        &self,
        edit_context: &EditContext,
    ) -> bool {
        for &asset_id in edit_context.assets().keys() {
            if asset_id.as_uuid() == *self.asset_source_id.uuid() {
                // ignore the root asset
                continue;
            }

            if self.is_asset_owned_by_this_data_source(edit_context, asset_id) {
                match self.assets_disk_state.get(&asset_id) {
                    None => {
                        // There is a newly created asset that has never been saved
                        return true;
                    }
                    Some(asset_disk_state) => {
                        let object_hash = edit_context
                            .data_set()
                            .hash_object(asset_id, HashObjectMode::FullObjectWithLocationId)
                            .unwrap();
                        if asset_disk_state.object_hash != object_hash {
                            // The object has been modified and no longer matches disk state
                            return true;
                        }
                    }
                }
            }
        }

        // Is there anything that's been deleted?
        for (&asset_id, _) in &self.assets_disk_state {
            if !edit_context.has_asset(asset_id)
                || !self.is_asset_owned_by_this_data_source(edit_context, asset_id)
            {
                // There is an asset that no longer exists, but the file is still on disk
                return true;
            }
        }

        return false;
    }

    fn append_pending_file_operations(
        &self,
        edit_context: &EditContext,
        pending_file_operations: &mut PendingFileOperations,
    ) {
        for &asset_id in edit_context.assets().keys() {
            if asset_id.as_uuid() == *self.asset_source_id.uuid() {
                // ignore the root asset
                continue;
            }

            if self.is_asset_owned_by_this_data_source(edit_context, asset_id) {
                match self.assets_disk_state.get(&asset_id) {
                    None => {
                        // There is a newly created asset that has never been saved
                        let file_path = self.path_for_asset(asset_id);
                        pending_file_operations
                            .create_operations
                            .push((asset_id, file_path));
                    }
                    Some(asset_disk_state) => {
                        let object_hash = edit_context
                            .data_set()
                            .hash_object(asset_id, HashObjectMode::FullObjectWithLocationId)
                            .unwrap();
                        if asset_disk_state.object_hash != object_hash {
                            // The object has been modified and no longer matches disk state
                            let file_path = self.path_for_asset(asset_id);
                            pending_file_operations
                                .modify_operations
                                .push((asset_id, file_path));
                        }
                    }
                }
            }
        }

        // Is there anything that's been deleted?
        for (&asset_id, _) in &self.assets_disk_state {
            if !edit_context.has_asset(asset_id)
                || !self.is_asset_owned_by_this_data_source(edit_context, asset_id)
            {
                // There is an asset that no longer exists, but the file is still on disk
                let file_path = self.path_for_asset(asset_id);
                pending_file_operations
                    .delete_operations
                    .push((asset_id, file_path));
            }
        }
    }
}