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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
use crate::edit_context::EditContext;
use crate::editor::undo::UndoStack;
use crate::{
    AssetId, AssetPath, AssetPathCache, AssetSourceId, DataSet, DataSource,
    FileSystemIdBasedDataSource, FileSystemPathBasedDataSource, HashMap, PathNode, PathNodeRoot,
    PendingFileOperations, SchemaNamedType, SchemaSet,
};
use hydrate_data::{
    AssetLocation, AssetName, CanonicalPathReference, DataSetError, DataSetResult, ImportInfo,
    PathReferenceHash, SingleObject,
};
use hydrate_pipeline::{
    DynEditorModel, HydrateProjectConfiguration, ImportJobToQueue, ImporterRegistry,
};
use hydrate_schema::{SchemaFingerprint, SchemaRecord};
use slotmap::DenseSlotMap;
use std::path::PathBuf;
slotmap::new_key_type! { pub struct EditContextKey; }

pub struct EditorModel {
    project_config: HydrateProjectConfiguration,
    schema_set: SchemaSet,
    undo_stack: UndoStack,
    root_edit_context_key: EditContextKey,
    edit_contexts: DenseSlotMap<EditContextKey, EditContext>,
    //TODO: slot_map?
    data_sources: HashMap<AssetSourceId, Box<dyn DataSource>>,

    path_node_schema: SchemaNamedType,
    path_node_root_schema: SchemaNamedType,
}

pub struct EditorModelWithCache<'a> {
    pub asset_path_cache: &'a AssetPathCache,
    pub editor_model: &'a mut EditorModel,
}

impl<'a> DynEditorModel for EditorModelWithCache<'a> {
    fn schema_set(&self) -> &SchemaSet {
        self.editor_model.schema_set()
    }

    fn handle_import_complete(
        &mut self,
        asset_id: AssetId,
        asset_name: AssetName,
        asset_location: AssetLocation,
        default_asset: &SingleObject,
        replace_with_default_asset: bool,
        import_info: ImportInfo,
        canonical_path_references: &HashMap<CanonicalPathReference, AssetId>,
        _path_references: &HashMap<PathReferenceHash, CanonicalPathReference>,
    ) -> DataSetResult<()> {
        //
        // If the asset is supposed to be regenerated, stomp the existing asset
        //
        let edit_context = self.editor_model.root_edit_context_mut();
        if replace_with_default_asset {
            if edit_context.has_asset(asset_id) {
                edit_context.delete_asset(asset_id)?;
            }

            edit_context.init_from_single_object(
                asset_id,
                asset_name,
                asset_location,
                default_asset,
            )?;
        }

        //
        // Whether it is regenerated or not, update import data
        //
        edit_context.set_import_info(asset_id, import_info)?;
        for (path_reference, referenced_asset_id) in canonical_path_references {
            edit_context.set_path_reference_override(
                asset_id,
                path_reference.clone(),
                *referenced_asset_id,
            )?;
        }

        Ok(())
    }

    fn data_set(&self) -> &DataSet {
        self.editor_model.root_edit_context().data_set()
    }

    fn is_path_node_or_root(
        &self,
        schema_record: &SchemaRecord,
    ) -> bool {
        self.editor_model
            .is_path_node_or_root(schema_record.fingerprint())
    }

    fn asset_display_name_long(
        &self,
        asset_id: AssetId,
    ) -> String {
        self.editor_model
            .asset_display_name_long(asset_id, &self.asset_path_cache)
    }
}

impl EditorModel {
    pub fn new(
        project_config: HydrateProjectConfiguration,
        schema_set: SchemaSet,
    ) -> Self {
        let undo_stack = UndoStack::default();
        let mut edit_contexts: DenseSlotMap<EditContextKey, EditContext> = Default::default();

        let root_edit_context_key = edit_contexts.insert_with_key(|key| {
            EditContext::new(&project_config, key, schema_set.clone(), &undo_stack)
        });

        let path_node_root_schema = schema_set
            .find_named_type(PathNodeRoot::schema_name())
            .unwrap()
            .clone();

        let path_node_schema = schema_set
            .find_named_type(PathNode::schema_name())
            .unwrap()
            .clone();

        EditorModel {
            project_config,
            schema_set,
            undo_stack,
            root_edit_context_key,
            edit_contexts,
            data_sources: Default::default(),
            //location_tree: Default::default(),
            //asset_path_cache: AssetPathCache::empty(),
            path_node_root_schema,
            path_node_schema,
        }
    }

    pub fn path_node_schema(&self) -> &SchemaNamedType {
        &self.path_node_schema
    }

    pub fn path_node_root_schema(&self) -> &SchemaNamedType {
        &self.path_node_root_schema
    }

    pub fn data_sources(&self) -> &HashMap<AssetSourceId, Box<dyn DataSource>> {
        &self.data_sources
    }

    pub fn is_path_node_or_root(
        &self,
        fingerprint: SchemaFingerprint,
    ) -> bool {
        self.path_node_schema.fingerprint() == fingerprint
            || self.path_node_root_schema.fingerprint() == fingerprint
    }

    pub fn is_generated_asset(
        &self,
        asset_id: AssetId,
    ) -> bool {
        for data_source in self.data_sources.values() {
            if data_source.is_generated_asset(asset_id) {
                return true;
            }
        }

        false
    }

    pub fn persist_generated_asset(
        &mut self,
        asset_id: AssetId,
    ) {
        for (_, data_source) in &mut self.data_sources {
            let root_edit_context = self
                .edit_contexts
                .get_mut(self.root_edit_context_key)
                .unwrap();

            data_source.persist_generated_asset(root_edit_context, asset_id);
        }
    }

    pub fn commit_all_pending_undo_contexts(&mut self) {
        for (_, context) in &mut self.edit_contexts {
            context.commit_pending_undo_context();
        }
    }

    pub fn cancel_all_pending_undo_contexts(&mut self) {
        for (_, context) in &mut self.edit_contexts {
            context.commit_pending_undo_context();
        }
    }

    pub fn any_edit_context_has_unsaved_changes(&self) -> bool {
        for (_, data_source) in &self.data_sources {
            for (_, edit_context) in &self.edit_contexts {
                if data_source.edit_context_has_unsaved_changes(edit_context) {
                    return true;
                }
            }
        }

        false
    }

    pub fn pending_file_operations(&self) -> PendingFileOperations {
        let mut pending_file_operations = PendingFileOperations::default();

        for (_, data_source) in &self.data_sources {
            for (_, edit_context) in &self.edit_contexts {
                data_source
                    .append_pending_file_operations(edit_context, &mut pending_file_operations);
            }
        }

        pending_file_operations
    }

    pub fn schema_set(&self) -> &SchemaSet {
        &self.schema_set
    }

    pub fn clone_schema_set(&self) -> SchemaSet {
        self.schema_set.clone()
    }

    pub fn root_edit_context(&self) -> &EditContext {
        self.edit_contexts.get(self.root_edit_context_key).unwrap()
    }

    pub fn root_edit_context_mut(&mut self) -> &mut EditContext {
        self.edit_contexts
            .get_mut(self.root_edit_context_key)
            .unwrap()
    }

    pub fn asset_path(
        &self,
        asset_id: AssetId,
        asset_path_cache: &AssetPathCache,
    ) -> Option<AssetPath> {
        let root_data_set = &self.root_edit_context().data_set;
        let location = root_data_set.asset_location(asset_id);

        // Look up the location, if we don't find it just assume the asset is at the root. This
        // allows some degree of robustness even when data is in a bad state (like cyclical references)
        let path = location
            .map(|x| asset_path_cache.path_to_id_lookup().get(&x.path_node_id()))
            .flatten()
            .cloned()?;

        let name = root_data_set.asset_name(asset_id).unwrap().as_string();
        if let Some(name) = name {
            Some(path.join(name))
        } else {
            Some(path.join(&format!("{}", asset_id.as_uuid())))
        }
    }

    pub fn asset_display_name_long(
        &self,
        asset_id: AssetId,
        asset_path_cache: &AssetPathCache,
    ) -> String {
        self.asset_path(asset_id, asset_path_cache)
            .map(|x| x.as_str().to_string())
            .unwrap_or_else(|| format!("{}", asset_id.as_uuid()))
    }

    pub fn data_source(
        &mut self,
        asset_source_id: AssetSourceId,
    ) -> Option<&dyn DataSource> {
        self.data_sources.get(&asset_source_id).map(|x| &**x)
    }

    pub fn is_a_root_asset(
        &self,
        asset_id: AssetId,
    ) -> bool {
        for source in self.data_sources.keys() {
            if *source.uuid() == asset_id.as_uuid() {
                return true;
            }
        }

        false
    }

    pub fn add_file_system_id_based_asset_source<RootPathT: Into<PathBuf>>(
        &mut self,
        project_config: &HydrateProjectConfiguration,
        data_source_name: &str,
        file_system_root_path: RootPathT,
        import_job_to_queue: &mut ImportJobToQueue,
    ) -> AssetSourceId {
        let file_system_root_path = dunce::canonicalize(&file_system_root_path.into()).unwrap();
        let path_node_root_schema = self.path_node_root_schema.as_record().unwrap().clone();
        let root_edit_context = self.root_edit_context_mut();

        // Commit any pending changes so we have a clean change tracking state
        root_edit_context.commit_pending_undo_context();

        //
        // Create the PathNodeRoot asset that acts as the root location for all assets in this DS
        //
        let asset_source_id = AssetSourceId::new();
        let root_asset_id = AssetId::from_uuid(*asset_source_id.uuid());
        root_edit_context
            .new_asset_with_id(
                root_asset_id,
                &AssetName::new(data_source_name),
                &AssetLocation::null(),
                &path_node_root_schema,
            )
            .unwrap();

        //
        // Create the data source and force full reload of it
        //
        let mut fs = FileSystemIdBasedDataSource::new(
            file_system_root_path.clone(),
            root_edit_context,
            asset_source_id,
        );
        fs.load_from_storage(project_config, root_edit_context, import_job_to_queue);

        self.data_sources.insert(asset_source_id, Box::new(fs));

        asset_source_id
    }

    pub fn add_file_system_path_based_data_source<RootPathT: Into<PathBuf>>(
        &mut self,
        project_config: &HydrateProjectConfiguration,
        data_source_name: &str,
        file_system_root_path: RootPathT,
        importer_registry: &ImporterRegistry,
        import_jobs_to_queue: &mut ImportJobToQueue,
    ) -> AssetSourceId {
        let file_system_root_path = dunce::canonicalize(&file_system_root_path.into()).unwrap();
        let path_node_root_schema = self.path_node_root_schema.as_record().unwrap().clone();
        let root_edit_context = self.root_edit_context_mut();

        // Commit any pending changes so we have a clean change tracking state
        root_edit_context.commit_pending_undo_context();

        //
        // Create the PathNodeRoot asset that acts as the root location for all assets in this DS
        //
        let asset_source_id = AssetSourceId::new();
        let root_asset_id = AssetId::from_uuid(*asset_source_id.uuid());
        root_edit_context
            .new_asset_with_id(
                root_asset_id,
                &AssetName::new(data_source_name),
                &AssetLocation::null(),
                &path_node_root_schema,
            )
            .unwrap();

        //
        // Create the data source and force full reload of it
        //
        let mut fs = FileSystemPathBasedDataSource::new(
            file_system_root_path.clone(),
            root_edit_context,
            asset_source_id,
            importer_registry,
        );
        fs.load_from_storage(project_config, root_edit_context, import_jobs_to_queue);

        self.data_sources.insert(asset_source_id, Box::new(fs));

        asset_source_id
    }

    pub fn save_root_edit_context(&mut self) {
        //
        // Ensure pending edits are flushed to the data set so that our modified assets list is fully up to date
        //
        let root_edit_context = self
            .edit_contexts
            .get_mut(self.root_edit_context_key)
            .unwrap();
        root_edit_context.commit_pending_undo_context();

        for (_id, data_source) in &mut self.data_sources {
            data_source.flush_to_storage(root_edit_context);
        }
    }

    pub fn revert_root_edit_context(
        &mut self,
        project_config: &HydrateProjectConfiguration,
        import_job_to_queue: &mut ImportJobToQueue,
    ) {
        //
        // Ensure pending edits are cleared
        //
        let root_edit_context = self
            .edit_contexts
            .get_mut(self.root_edit_context_key)
            .unwrap();
        root_edit_context.cancel_pending_undo_context().unwrap();

        //
        // Take the contents of the modified asset list, leaving the edit context with a cleared list
        //
        for (_id, data_source) in &mut self.data_sources {
            data_source.load_from_storage(project_config, root_edit_context, import_job_to_queue);
        }

        //
        // Clear modified assets list since we reloaded everything from disk.
        //
        //root_edit_context.cancel_pending_undo_context();

        //self.refresh_asset_path_lookups();
        //self.refresh_location_tree();
    }

    pub fn close_file_system_source(
        &mut self,
        _asset_source_id: AssetSourceId,
    ) {
        unimplemented!();
        // kill edit contexts or fail

        // clear root_edit_context of data from this source

        // drop the source
        //let old = self.data_sources.remove(&asset_source_id);
        //assert!(old.is_some());
    }

    // Spawns a separate edit context with copies of the given assets. The undo stack will be shared
    // globally, but changes will not be visible on the root context. The edit context will be flushed
    // to the root context in a single operation. Generally, we don't expect assets opened in a
    // separate edit context to change in the root context, but there is nothing that prevents it.
    pub fn open_edit_context(
        &mut self,
        assets: &[AssetId],
    ) -> DataSetResult<EditContextKey> {
        let new_edit_context_key = self.edit_contexts.insert_with_key(|key| {
            EditContext::new_with_data(
                &self.project_config,
                key,
                self.schema_set.clone(),
                &self.undo_stack,
            )
        });

        let [root_edit_context, new_edit_context] = self
            .edit_contexts
            .get_disjoint_mut([self.root_edit_context_key, new_edit_context_key])
            .unwrap();

        for asset in assets {
            if !root_edit_context.assets().contains_key(asset) {
                return Err(DataSetError::AssetNotFound)?;
            }
        }

        for &asset_id in assets {
            new_edit_context
                .data_set
                .copy_from(root_edit_context.data_set(), asset_id)
                .expect("Could not copy asset to newly created edit context");
        }

        Ok(new_edit_context_key)
    }

    pub fn flush_edit_context_to_root(
        &mut self,
        edit_context: EditContextKey,
    ) -> DataSetResult<()> {
        assert_ne!(edit_context, self.root_edit_context_key);
        let [root_context, context_to_flush] = self
            .edit_contexts
            .get_disjoint_mut([self.root_edit_context_key, edit_context])
            .unwrap();

        // In the case of failure we want to flush as much as we can, so keep the error around and
        // return it after trying to flush all the assetsa
        let mut first_error = None;
        for &asset_id in context_to_flush.assets().keys() {
            if let Err(e) = root_context
                .data_set
                .copy_from(&context_to_flush.data_set, asset_id)
            {
                if first_error.is_none() {
                    first_error = Some(Err(e));
                }
            }
        }

        first_error.unwrap_or(Ok(()))
    }

    pub fn close_edit_context(
        &mut self,
        edit_context: EditContextKey,
    ) {
        assert_ne!(edit_context, self.root_edit_context_key);
        self.edit_contexts.remove(edit_context);
    }

    pub fn undo(&mut self) -> DataSetResult<()> {
        self.undo_stack.undo(&mut self.edit_contexts)
    }

    pub fn redo(&mut self) -> DataSetResult<()> {
        self.undo_stack.redo(&mut self.edit_contexts)
    }
}