diff --git a/SlicerConfiguration/Settings/LayeredProfile.cs b/SlicerConfiguration/Settings/LayeredProfile.cs index f027da49f..bf4a3528a 100644 --- a/SlicerConfiguration/Settings/LayeredProfile.cs +++ b/SlicerConfiguration/Settings/LayeredProfile.cs @@ -36,6 +36,8 @@ using System.IO; using Newtonsoft.Json.Linq; using System.Text; using System.Collections.ObjectModel; +using MatterHackers.MatterControl.DataStorage; +using MatterHackers.MatterControl.SettingsManagement; namespace MatterHackers.MatterControl.SlicerConfiguration { @@ -201,10 +203,19 @@ namespace MatterHackers.MatterControl.SlicerConfiguration /// User settings overrides /// public PrinterSettingsLayer UserLayer { get; } = new PrinterSettingsLayer(); - + // public static PrinterSettings LoadFile(string printerProfilePath) { - var jObject = JObject.Parse(File.ReadAllText(printerProfilePath)); + + JObject jObject; + try + { + jObject = JObject.Parse(File.ReadAllText(printerProfilePath)); + } + catch + { + return RecoverProfile(printerProfilePath); + } int documentVersion = jObject?.GetValue("DocumentVersion")?.Value() ?? 0; @@ -214,7 +225,33 @@ namespace MatterHackers.MatterControl.SlicerConfiguration } // Reload the document with the new schema - return JsonConvert.DeserializeObject(File.ReadAllText(printerProfilePath)); + try + { + return JsonConvert.DeserializeObject(File.ReadAllText(printerProfilePath)); + } + catch + { + return RecoverProfile(printerProfilePath); + } + } + + public static PrinterSettings RecoverProfile(string printerProfilePath) + { + string profileKey = Path.GetFileNameWithoutExtension(printerProfilePath); + var profile = ProfileManager.Instance[profileKey]; + string publicProfileDeviceToken = OemSettings.Instance.OemProfiles[profile.Make][profile.Model]; + string publicProfileToLoad = Path.Combine(ApplicationDataStorage.ApplicationUserDataPath, "data", "temp", "cache", "profiles") + "\\" + publicProfileDeviceToken + ".json"; + + var oemProfile = JsonConvert.DeserializeObject(File.ReadAllText(publicProfileToLoad)); + oemProfile.ID = profile.ID; + oemProfile.SetValue(SettingsKey.printer_name, profile.Name); + oemProfile.DocumentVersion = PrinterSettings.LatestVersion; + + var profileHelper = new SettingsProfile(oemProfile); + profileHelper.SetComPort(profile.ComPort); + profileHelper.SaveChanges(); + + return oemProfile; } // TODO: Hookup OEM layers diff --git a/SlicerConfiguration/Settings/ProfileManager.cs b/SlicerConfiguration/Settings/ProfileManager.cs index e1d988c30..1db0c24aa 100644 --- a/SlicerConfiguration/Settings/ProfileManager.cs +++ b/SlicerConfiguration/Settings/ProfileManager.cs @@ -357,7 +357,9 @@ namespace MatterHackers.MatterControl.SlicerConfiguration Instance.Profiles.Add(new PrinterInfo { Name = printerName, - ID = guid + ID = guid, + Make = make, + Model = model }); // Update SHA1 diff --git a/SlicerConfiguration/Settings/SettingsProfile.cs b/SlicerConfiguration/Settings/SettingsProfile.cs index 272781fc0..5a9e7a7c3 100644 --- a/SlicerConfiguration/Settings/SettingsProfile.cs +++ b/SlicerConfiguration/Settings/SettingsProfile.cs @@ -938,6 +938,8 @@ namespace MatterHackers.MatterControl.SlicerConfiguration public string ComPort { get; set; } public string ID { get; set; } public string Name { get; set; } + public string Make { get; set; } + public string Model { get; set; } public string DeviceToken { get; set; } public bool IsDirty { get; set; } = false; public bool MarkedForDelete { get; set; } = false;