-
Notifications
You must be signed in to change notification settings - Fork 808
Expand file tree
/
Copy pathIconDatabase.cs
More file actions
136 lines (118 loc) · 4.51 KB
/
IconDatabase.cs
File metadata and controls
136 lines (118 loc) · 4.51 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
using System.Text.Json;
using UniGetUI.Core.Data;
using UniGetUI.Core.Logging;
using UniGetUI.Core.SettingsEngine;
using UniGetUI.Core.Tools;
namespace UniGetUI.Core.IconEngine
{
/// <summary>
/// This class represents the structure of the icon and screenshot database. It is used to deserialize the JSON data.
/// </summary>
public class IconDatabase
{
public struct IconCount
{
public int PackagesWithIconCount = 0;
public int TotalScreenshotCount = 0;
public int PackagesWithScreenshotCount = 0;
public IconCount() { }
}
private static IconDatabase? __instance;
public static IconDatabase Instance
{
get
{
if (__instance is null)
{
Logger.Error("IconStore.Instance was not initialized, creating an empty instance.");
InitializeInstance();
return Instance;
}
return __instance;
}
}
public static void InitializeInstance()
{
__instance = new();
}
/// <summary>
/// The icon and screenshot database
/// </summary>
private Dictionary<string, IconScreenshotDatabase_v2.PackageIconAndScreenshots> IconDatabaseData = [];
private IconCount __icon_count = new();
/// <summary>
/// Download the icon and screenshots database to a local file, and load it into memory
/// </summary>
public async void LoadIconAndScreenshotsDatabase()
{
await LoadIconAndScreenshotsDatabaseAsync();
}
public async Task LoadIconAndScreenshotsDatabaseAsync()
{
string IconsAndScreenshotsFile = Path.Join(CoreData.UniGetUICacheDirectory_Data, "Icon Database.json");
try
{
Uri DownloadUrl = new("https://raw.githubusercontent.com/marticliment/UniGetUI/main/WebBasedData/screenshot-database-v2.json");
if (Settings.Get("IconDataBaseURL"))
{
DownloadUrl = new Uri(Settings.GetValue("IconDataBaseURL"));
}
using (HttpClient client = new(CoreTools.GenericHttpClientParameters))
{
client.DefaultRequestHeaders.UserAgent.ParseAdd(CoreData.UserAgentString);
string fileContents = await client.GetStringAsync(DownloadUrl);
await File.WriteAllTextAsync(IconsAndScreenshotsFile, fileContents);
}
Logger.ImportantInfo("Downloaded new icons and screenshots successfully!");
}
catch (Exception e)
{
Logger.Warn("Failed to download icons and screenshots");
Logger.Warn(e);
}
if (!File.Exists(IconsAndScreenshotsFile))
{
Logger.Error("Icon Database file not found");
return;
}
try
{
IconScreenshotDatabase_v2 JsonData = JsonSerializer.Deserialize<IconScreenshotDatabase_v2>(
await File.ReadAllTextAsync(IconsAndScreenshotsFile),
SerializationHelpers.DefaultOptions
);
if (JsonData.icons_and_screenshots is not null)
{
IconDatabaseData = JsonData.icons_and_screenshots;
}
__icon_count = new IconCount
{
PackagesWithIconCount = JsonData.package_count.packages_with_icon,
PackagesWithScreenshotCount = JsonData.package_count.packages_with_screenshot,
TotalScreenshotCount = JsonData.package_count.total_screenshots,
};
}
catch (Exception ex)
{
Logger.Error("Failed to load icon and screenshot database");
Logger.Error(ex);
}
}
public string? GetIconUrlForId(string id)
{
if (IconDatabaseData.TryGetValue(id, out var value) && value.icon.Length != 0)
{
return value.icon;
}
return null;
}
public string[] GetScreenshotsUrlForId(string id)
{
return IconDatabaseData.TryGetValue(id, out var value) ? value.images.ToArray() : [];
}
public IconCount GetIconCount()
{
return __icon_count;
}
}
}