|
| 1 | +/* |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + * |
| 6 | + * Copyright © Vincent Bengtsson & Contributors 2022-2024 |
| 7 | + * https://github.com/Visual-Vincent/GuiStack |
| 8 | + */ |
| 9 | + |
| 10 | +using System; |
| 11 | +using System.Collections.Generic; |
| 12 | +using System.IO; |
| 13 | +using System.Linq; |
| 14 | +using System.Reflection; |
| 15 | + |
| 16 | +namespace GuiStack |
| 17 | +{ |
| 18 | + /// <summary> |
| 19 | + /// A static class containing information about the application. |
| 20 | + /// </summary> |
| 21 | + public static class ApplicationInfo |
| 22 | + { |
| 23 | + /// <summary> |
| 24 | + /// A record representing a license to a third-party resource. |
| 25 | + /// </summary> |
| 26 | + /// <param name="Name">The name of the third-party resource.</param> |
| 27 | + /// <param name="LicenseText">The text of the resource's copyright license.</param> |
| 28 | + public record class ThirdPartyLicense(string Name, string LicenseText); |
| 29 | + |
| 30 | +#pragma warning disable IDE0001 // Simplify name |
| 31 | + internal static readonly Type ProgramClass = typeof(global::GuiStack.Program); // Deliberate choice to ensure we always refer to GuiStack |
| 32 | +#pragma warning restore IDE0001 |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// The URL to the project that this application is part of. |
| 36 | + /// </summary> |
| 37 | + public const string ProjectUrl = "https://github.com/Visual-Vincent/GuiStack"; |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// The assembly that this application resides in. |
| 41 | + /// </summary> |
| 42 | + public static readonly Assembly MainAssembly = ProgramClass.Assembly; |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// The copyright information of the application. |
| 46 | + /// </summary> |
| 47 | + public static readonly string Copyright = ProgramClass.Assembly.GetCustomAttributes<AssemblyCopyrightAttribute>().FirstOrDefault()?.Copyright; |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// The application description. |
| 51 | + /// </summary> |
| 52 | + public static readonly string Description = |
| 53 | + MainAssembly.GetCustomAttributes<AssemblyDescriptionAttribute>().FirstOrDefault()?.Description ?? |
| 54 | + MainAssembly.GetCustomAttributes<AssemblyTitleAttribute>().FirstOrDefault()?.Title; |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// The copyright license of the application. |
| 58 | + /// </summary> |
| 59 | + public static readonly string License = GetStringResource("LICENSE.txt"); |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// The collection of copyright licenses of third-party resources used by the application. |
| 63 | + /// </summary> |
| 64 | + public static readonly IReadOnlyCollection<ThirdPartyLicense> ThirdPartyLicenses = ((Func<IReadOnlyCollection<ThirdPartyLicense>>)(() => { |
| 65 | + var licenses = new List<ThirdPartyLicense>(); |
| 66 | + var resources = MainAssembly.GetManifestResourceNames(); |
| 67 | + |
| 68 | + foreach(var resource in resources) |
| 69 | + { |
| 70 | + if(!resource.StartsWith($"{nameof(GuiStack)}.Licenses", StringComparison.OrdinalIgnoreCase)) |
| 71 | + continue; |
| 72 | + |
| 73 | + var resourceName = resource.Remove(0, nameof(GuiStack).Length + 1); |
| 74 | + var thirdPartyName = resource.Remove(0, $"{nameof(GuiStack)}.Licenses".Length + 1); |
| 75 | + var license = GetStringResource(resourceName); |
| 76 | + |
| 77 | + if(license == null) |
| 78 | + continue; |
| 79 | + |
| 80 | + var extension = Path.GetExtension(thirdPartyName); |
| 81 | + |
| 82 | + switch(extension.ToLower()) |
| 83 | + { |
| 84 | + case ".txt": |
| 85 | + case ".md": |
| 86 | + thirdPartyName = thirdPartyName.Remove(thirdPartyName.Length - extension.Length); |
| 87 | + break; |
| 88 | + } |
| 89 | + |
| 90 | + licenses.Add(new ThirdPartyLicense(thirdPartyName, license)); |
| 91 | + } |
| 92 | + |
| 93 | + return licenses.OrderBy(l => l.Name).ToArray(); |
| 94 | + })).Invoke(); |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Returns an embedded resource string, or null if none was found. |
| 98 | + /// </summary> |
| 99 | + /// <param name="resourceName">The name of the resource to fetch (case sensitive).</param> |
| 100 | + internal static string GetStringResource(string resourceName) |
| 101 | + { |
| 102 | + using var resourceStream = MainAssembly.GetManifestResourceStream($"{nameof(GuiStack)}.{resourceName}"); |
| 103 | + |
| 104 | + if(resourceStream == null) |
| 105 | + return null; |
| 106 | + |
| 107 | + using var streamReader = new StreamReader(resourceStream); |
| 108 | + |
| 109 | + return streamReader.ReadToEnd(); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments