forked from parzivail/RainbowForge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ForgeCatalog.cs
127 lines (105 loc) · 3.07 KB
/
ForgeCatalog.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using AssetCatalog.Model;
using RainbowForge.Core;
namespace AssetCatalog
{
public class ForgeCatalog : ICatalogApp, INotifyPropertyChanged
{
public static readonly ForgeCatalog Instance = new();
private CatalogAssetCategory[] _filteredCategories = Array.Empty<CatalogAssetCategory>();
private CatalogEntryStatus[] _filteredStatuses = Array.Empty<CatalogEntryStatus>();
private string _nameFilter = "";
private Forge _openedForge;
private CatalogEntry _selectedCatalogEntry;
private Entry _selectedEntry;
private string _status;
private ulong _uidFilter;
public event PropertyChangedEventHandler PropertyChanged;
public string Status
{
get => _status;
set
{
_status = value;
OnPropertyChanged();
}
}
public ICatalogDb CatalogDb { get; } = new LocalCatalogDb();
public Forge OpenedForge
{
get => _openedForge;
private set
{
_openedForge = value;
OnPropertyChanged();
OnPropertyChanged(nameof(FilteredEntries));
}
}
public Entry SelectedEntry
{
get => _selectedEntry;
set
{
_selectedEntry = value;
OnPropertyChanged();
OnPropertyChanged(nameof(IsEntrySelected));
SelectedCatalogEntry = value == null ? null : CatalogDb.Get(value.Uid);
}
}
public bool IsEntrySelected => SelectedEntry != null;
public CatalogEntry SelectedCatalogEntry
{
get => _selectedCatalogEntry;
private set
{
_selectedCatalogEntry = value;
OnPropertyChanged();
}
}
/// <inheritdoc />
public IEnumerable<Entry> FilteredEntries => OpenedForge == null ? Array.Empty<Entry>() : OpenedForge.Entries.Where(EntryMatchesFilter);
private ForgeCatalog()
{
}
public void OpenForge(Stream stream)
{
var forgeStream = new BinaryReader(stream);
OpenedForge = Forge.Read(forgeStream);
}
public void SetFilter(CatalogEntryStatus[] filteredStatuses, CatalogAssetCategory[] filteredCategories, ulong uidFilter, string nameFilter)
{
_uidFilter = uidFilter;
_nameFilter = nameFilter;
_filteredCategories = filteredCategories;
_filteredStatuses = filteredStatuses;
OnPropertyChanged(nameof(FilteredEntries));
}
public void OnCatalogChanged()
{
OnPropertyChanged(nameof(OpenedForge));
OnPropertyChanged(nameof(FilteredEntries));
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private bool EntryMatchesFilter(Entry arg)
{
if (_uidFilter != 0 && arg.Uid != _uidFilter)
return false;
var catalogEntry = CatalogDb.Get(arg.Uid);
if (!string.IsNullOrWhiteSpace(_nameFilter) && !catalogEntry.Name.Contains(_nameFilter))
return false;
if (_filteredStatuses.Length != 0 && !_filteredStatuses.Contains(catalogEntry.Status))
return false;
if (_filteredCategories.Length != 0 && !_filteredCategories.Contains(catalogEntry.Category))
return false;
return true;
}
}
}