private static UserConfig CreateOrDeserializeConfig() { try { return((!FileOps.DoesFileExist(PathToFile) ? new UserConfig() : JsonSerializer.Deserialize <UserConfig>(File.ReadAllText(PathToFile))) ?? new UserConfig()); } catch (Exception e) { MessageBox.Show("Exception thrown while attempting to deserialize the UserConfig, UserConfig will be deleted after pressing OK and a fresh new one will be made.\n\nThis may have been caused by upgrading to v1.2021.3.1 or greater from an older version, if so, this is not a bug. Error Message:\n\n" + e.Message, "Exception thrown when deserializing UserConfig", MessageBoxButtons.OK, MessageBoxIcon.Error); FileOps.DeleteFile(PathToFile); return(new UserConfig()); } }
public static UserConfig LoadConfig(bool isUnix) { if (isUnix) { PathToFile = Environment.GetEnvironmentVariable("HOME") + "/.config/imgdanke/"; if (!FileOps.DoesDirectoryExist(PathToFile)) { Directory.CreateDirectory(PathToFile); } PathToFile += CONFIG_FILENAME; } else if (!FileOps.DoesFileExist(PathToFile)) { // See if it's where it's being called from, which in most cases, will be next to the executable PathToFile = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); PathToFile ??= ""; PathToFile = Path.Combine(PathToFile, CONFIG_FILENAME); } UserConfig config = CreateOrDeserializeConfig(); config.Defaults(); if (config._validInputExtensions.Count == 0) { config._validInputExtensions = new List <string> { ".png", ".jpg", ".jpeg", ".psd", ".tif", ".gif", ".webp" }; } if (config._validOutputExtensions.Count == 0) { config._validOutputExtensions = new List <string> { ".png", ".jpg" }; } return(config); }