using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Text.Json; using System.Text.Json.Serialization; using Flow.Launcher.Plugin.WindowsSettings.Classes; namespace Flow.Launcher.Plugin.WindowsSettings.Helper { /// /// Helper class to easier work with the JSON file that contains all Windows settings /// internal static class JsonSettingsListHelper { /// /// The name of the file that contains all settings for the query /// private const string _settingsFile = "WindowsSettings.json"; /// /// Read all possible Windows settings. /// /// A list with all possible windows settings. internal static IEnumerable ReadAllPossibleSettings() { var assembly = Assembly.GetExecutingAssembly(); var type = assembly.GetTypes().FirstOrDefault(x => x.Name == nameof(Main)); IEnumerable? settingsList = null; try { var resourceName = $"{type?.Namespace}.{_settingsFile}"; using var stream = assembly.GetManifestResourceStream(resourceName); if (stream is null) { throw new Exception("stream is null"); } var options = new JsonSerializerOptions(); options.Converters.Add(new JsonStringEnumConverter()); using var reader = new StreamReader(stream); var text = reader.ReadToEnd(); settingsList = JsonSerializer.Deserialize>(text, options); } catch (Exception exception) { Log.Exception("Error loading settings JSON file", exception, typeof(Main)); } return settingsList ?? Enumerable.Empty(); } } }