diff --git a/OtterGui b/OtterGui index 6eb6ab15..0f8a8664 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit 6eb6ab156c1bc1cb61700f19768f3fa6c11e1e04 +Subproject commit 0f8a866491b246e819e0618a43078170686780ab diff --git a/Penumbra/Collections/Manager/ActiveCollections.cs b/Penumbra/Collections/Manager/ActiveCollections.cs index bae95885..7e6d691e 100644 --- a/Penumbra/Collections/Manager/ActiveCollections.cs +++ b/Penumbra/Collections/Manager/ActiveCollections.cs @@ -408,19 +408,15 @@ public class ActiveCollections : ISavable, IDisposable public static bool Load(FilenameService fileNames, out JObject ret) { var file = fileNames.ActiveCollectionsFile; - if (File.Exists(file)) - try - { - ret = JObject.Parse(File.ReadAllText(file)); - return true; - } - catch (Exception e) - { - Penumbra.Log.Error($"Could not read active collections from file {file}:\n{e}"); - } + var jObj = BackupService.GetJObjectForFile(fileNames, file); + if (jObj == null) + { + ret = new JObject(); + return false; + } - ret = new JObject(); - return false; + ret = jObj; + return true; } public string RedundancyCheck(CollectionType type, ActorIdentifier id) diff --git a/Penumbra/Mods/Manager/ModFileSystem.cs b/Penumbra/Mods/Manager/ModFileSystem.cs index 013d0e40..2d8b90ab 100644 --- a/Penumbra/Mods/Manager/ModFileSystem.cs +++ b/Penumbra/Mods/Manager/ModFileSystem.cs @@ -1,5 +1,7 @@ using System.Diagnostics.CodeAnalysis; using System.Text.RegularExpressions; +using Newtonsoft.Json.Linq; +using OtterGui.Classes; using OtterGui.Filesystem; using Penumbra.Communication; using Penumbra.Services; @@ -60,8 +62,8 @@ public sealed class ModFileSystem : FileSystem, IDisposable, ISavable // Used on construction and on mod rediscoveries. private void Reload() { - // TODO - if (Load(new FileInfo(_saveService.FileNames.FilesystemFile), _modManager, ModToIdentifier, ModToName)) + var jObj = BackupService.GetJObjectForFile(_saveService.FileNames, _saveService.FileNames.FilesystemFile); + if (Load(jObj, _modManager, ModToIdentifier, ModToName)) _saveService.ImmediateSave(this); Penumbra.Log.Debug("Reloaded mod filesystem."); diff --git a/Penumbra/Services/BackupService.cs b/Penumbra/Services/BackupService.cs index 0c13217a..f6e2c3e4 100644 --- a/Penumbra/Services/BackupService.cs +++ b/Penumbra/Services/BackupService.cs @@ -1,3 +1,4 @@ +using Newtonsoft.Json.Linq; using OtterGui.Classes; using OtterGui.Log; using Penumbra.Util; @@ -23,4 +24,26 @@ public class BackupService list.Add(new FileInfo(fileNames.ActiveCollectionsFile)); return list; } + + /// Try to parse a file to JObject and check backups if this does not succeed. + public static JObject? GetJObjectForFile(FilenameService fileNames, string fileName) + { + JObject? ret = null; + if (!File.Exists(fileName)) + return ret; + + try + { + var text = File.ReadAllText(fileName); + ret = JObject.Parse(text); + } + catch (Exception ex) + { + Penumbra.Log.Error($"Failed to load {fileName}, trying to restore from backup:\n{ex}"); + Backup.TryGetFile(new DirectoryInfo(fileNames.ConfigDirectory), fileName, out ret, out var messages, JObject.Parse); + Penumbra.Chat.NotificationMessage(messages); + } + + return ret; + } }