-
Notifications
You must be signed in to change notification settings - Fork 0
/
Resources.cs
83 lines (72 loc) · 2.47 KB
/
Resources.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
using System.Reflection;
using Avalonia.Media.Imaging;
namespace CountryConverter;
public static class Resources
{
private const string NotFoundImage = "image-not-found.png";
private static bool _bakedAssets = true;
/// <summary>
/// Dictionary { File name --> Bitmap }
/// </summary>
public static Dictionary<string, Bitmap> Images { get; } = new();
public static Bitmap DefaultImage => Images[NotFoundImage];
public static string DefaultFileName => NotFoundImage;
static Resources()
{
ResetAssets();
}
/// <summary>
/// Set new folder with assets
/// </summary>
/// <remarks>Folder must contain "image-not-found.png" and "Flags" folder</remarks>
/// <param name="assetsFolder">Target directory</param>
public static void ChangeAssetsFolder(DirectoryInfo assetsFolder)
{
_bakedAssets = false;
UpdateImages(assetsFolder);
}
/// <summary>
/// Reset Assets to baked ones
/// </summary>
public static void ResetAssets()
{
_bakedAssets = true;
UpdateImages();
}
private static void UpdateImages(DirectoryInfo? assetsFolder = null)
{
Images.Clear();
if (_bakedAssets || assetsFolder is null)
{
var assembly = Assembly.GetExecutingAssembly();
var names = assembly.GetManifestResourceNames();
foreach (var name in names)
{
using var stream = assembly.GetManifestResourceStream(name)!;
try
{
var img = new Bitmap(stream);
// "CountryConverter.BakedAssets.*name*.png"
var dividedName = name.Split('.');
Images.Add(dividedName[^2] + ".png", img);
}
catch (ArgumentException)
{
Console.WriteLine("Resource is not an image");
}
}
}
else
{
// Reading default image
Images.Add(DefaultFileName, new Bitmap(Path.Combine(assetsFolder.FullName, DefaultFileName)));
// Reading flags
var flagsFolder = new DirectoryInfo(Path.Combine(assetsFolder.FullName, "Flags"));
foreach (var file in flagsFolder.EnumerateFiles())
{
if (file.Extension == ".png")
Images.Add(file.Name, new Bitmap(file.FullName));
}
}
}
}