forked from LittleBigRefresh/Refresh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameDatabaseContext.Assets.cs
More file actions
170 lines (140 loc) · 5.99 KB
/
Copy pathGameDatabaseContext.Assets.cs
File metadata and controls
170 lines (140 loc) · 5.99 KB
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
using Refresh.Database.Models.Assets;
using Refresh.Database.Models.Users;
using Refresh.Database.Models.Relations;
namespace Refresh.Database;
public partial class GameDatabaseContext // Assets
{
public IQueryable<GameAsset> GameAssetsIncluded => this.GameAssets
.Include(a => a.OriginalUploader);
public GameAsset? GetAssetFromHash(string hash)
{
if (hash.IsBlankHash() || hash.StartsWith('g')) return null;
return this.GameAssetsIncluded
.FirstOrDefault(a => a.AssetHash == hash);
}
public IQueryable<GameAsset> GetAssetsUploadedByUserInternal(GameUser? user)
=> this.GameAssetsIncluded.Where(a => a.OriginalUploader == user);
public DatabaseList<GameAsset> GetAssetsUploadedByUser(GameUser? user, int skip, int count)
=> new(this.GetAssetsUploadedByUserInternal(user), skip, count);
public DatabaseList<GameAsset> GetAssetsUploadedByUser(GameUser? user, int skip, int count, GameAssetType type)
=> new(this.GetAssetsUploadedByUserInternal(user).Where(a => a.AssetType == type), skip, count);
public GameAssetType? GetConvertedType(string hash)
{
GameAsset? asset = this.GameAssets.FirstOrDefault(a => a.AssetHash == hash ||
a.AsMainlineIconHash == hash ||
a.AsMipIconHash == hash);
//If the asset hash is the requested hash, its not a converted file
if (asset == null || asset.AssetHash == hash)
return null;
if (asset.AsMainlineIconHash == hash)
return GameAssetType.Png;
if (asset.AsMipIconHash == hash)
return GameAssetType.Mip;
return null;
}
// TODO: optimize this by returning a list of GameAsset instead
public IEnumerable<string> GetAssetDependencies(GameAsset asset)
=> this.AssetDependencyRelations.Where(a => a.Dependent == asset.AssetHash)
.AsEnumerable()
.Select(a => a.Dependency);
// TODO: optimize this by returning a list of GameAsset instead
public IEnumerable<string> GetAssetDependents(GameAsset asset)
=> this.AssetDependencyRelations.Where(a => a.Dependency == asset.AssetHash)
.AsEnumerable()
.Select(a => a.Dependent);
public void AddOrOverwriteAssetDependencyRelations(string dependent, IEnumerable<string> dependencies)
{
this.Write(() =>
{
// delete all existing relations. ensures duplicates won't exist when reprocessing
this.AssetDependencyRelations.RemoveRange(a => a.Dependent == dependent);
foreach (string dependency in dependencies)
this.AssetDependencyRelations.Add(new AssetDependencyRelation
{
Dependent = dependent,
Dependency = dependency,
});
});
}
public IEnumerable<GameAsset> GetAssetsByType(GameAssetType type) =>
this.GameAssetsIncluded
.Where(a => a.AssetType == type);
public void AddAssetToDatabase(GameAsset asset) =>
this.Write(() =>
{
this.GameAssets.Add(asset);
});
public void AddAssetsToDatabase(IEnumerable<GameAsset> assets)
{
this.GameAssets.AddRange(assets);
this.SaveChanges();
}
public void UpdateAssetsInDatabase(IEnumerable<GameAsset> assets)
{
this.GameAssets.UpdateRange(assets);
this.SaveChanges();
}
public void SetMainlineIconHash(GameAsset asset, string hash) =>
this.Write(() =>
{
asset.AsMainlineIconHash = hash;
});
public void SetMipIconHash(GameAsset asset, string hash) =>
this.Write(() =>
{
asset.AsMipIconHash = hash;
});
public void SetMainlinePhotoHash(GameAsset asset, string hash) =>
this.Write(() =>
{
asset.AsMainlinePhotoHash = hash;
});
public bool IsAssetDisallowed(string hash)
{
string hashLower = hash.ToLower();
return this.DisallowedAssets.Any(u => u.AssetHash == hashLower);
}
public DisallowedAsset? GetDisallowedAssetInfo(string hash)
{
string hashLower = hash.ToLower();
return this.DisallowedAssets.FirstOrDefault(d => d.AssetHash == hashLower);
}
/// <returns>
/// The asset's disallowance info + whether the asset wasn't already disallowed before
/// </returns
public (DisallowedAsset, bool) DisallowAsset(string hash, GameAssetType type, string reason)
{
string hashLower = hash.ToLower();
DisallowedAsset? existing = this.GetDisallowedAssetInfo(hashLower);
if (existing != null) return (existing, false);
DisallowedAsset disallowed = new()
{
AssetHash = hashLower,
AssetType = type,
Reason = reason,
DisallowedAt = this._time.Now,
};
this.DisallowedAssets.Add(disallowed);
this.SaveChanges();
return (disallowed, true);
}
public bool ReallowAsset(string hash)
{
string hashLower = hash.ToLower();
DisallowedAsset? existing = this.GetDisallowedAssetInfo(hashLower);
if (existing == null) return false;
this.DisallowedAssets.Remove(existing);
this.SaveChanges();
return true;
}
public IQueryable<string> FilterOutAllowedAssets(List<string> hashes)
=> this.DisallowedAssets
.Where(d => hashes.Contains(d.AssetHash))
.Select(d => d.AssetHash);
public DatabaseList<DisallowedAsset> GetDisallowedAssets(GameAssetType? type, int skip, int count)
{
IQueryable<DisallowedAsset> disallowedList = this.DisallowedAssets.OrderByDescending(d => d.DisallowedAt);
if (type != null) disallowedList = disallowedList.Where(d => d.AssetType == type);
return new(disallowedList, skip, count);
}
}