diff --git a/Scripts/Ex.cs b/Scripts/Ex.cs index f71db26..f9abc5a 100644 --- a/Scripts/Ex.cs +++ b/Scripts/Ex.cs @@ -1,4 +1,5 @@ -using System; +using PotionCraftBookmarkOrganizer.Scripts.Storage; +using System; using System.Collections.Generic; using System.Text; @@ -15,7 +16,7 @@ public static class Ex /// the code to run. /// optional. Runs this code on error if provided. /// true on error unless an errorAction is specified - public static bool RunSafe(Func action, Func errorAction = null) + public static bool RunSafe(Func action, Func errorAction = null, bool logErrorToStorage = false) { try { @@ -23,7 +24,7 @@ public static bool RunSafe(Func action, Func errorAction = null) } catch (Exception ex) { - LogException(ex); + LogException(ex, logErrorToStorage); } if (errorAction == null) return true; @@ -37,7 +38,7 @@ public static bool RunSafe(Func action, Func errorAction = null) /// the code to run. /// optional. Runs this code on error if provided. /// true on error unless an errorAction is specified - public static void RunSafe(Action action, Action errorAction = null) + public static void RunSafe(Action action, Action errorAction = null, bool logErrorToStorage = false) { try { @@ -45,15 +46,24 @@ public static void RunSafe(Action action, Action errorAction = null) } catch (Exception ex) { - LogException(ex); + LogException(ex, logErrorToStorage); if (errorAction != null) errorAction(); } } - public static void LogException(Exception ex) + public static void LogException(Exception ex, bool logErrorToStorage = false) { - var errorMessage = $"{ex.GetType()}: {ex.Message}\r\n{ex.StackTrace}\r\n{ex.InnerException?.Message}"; - Plugin.PluginLogger.LogError(errorMessage); + var exceptionText = GetExceptionText(ex); + Plugin.PluginLogger.LogError(exceptionText); + if (logErrorToStorage) + { + StaticStorage.ErrorLog.Add(exceptionText); + } + } + + public static string GetExceptionText(Exception ex) + { + return $"{DateTime.UtcNow}: {ex.GetType()}: {ex.Message}\r\n{ex.StackTrace}\r\n{ex.InnerException?.Message}"; } } } diff --git a/Scripts/Patches/SaveLoad/RemoveSubBookmarksFromMainRailsOnLoadPatch.cs b/Scripts/Patches/SaveLoad/RemoveSubBookmarksFromMainRailsOnLoadPatch.cs index d0202fc..1b88692 100644 --- a/Scripts/Patches/SaveLoad/RemoveSubBookmarksFromMainRailsOnLoadPatch.cs +++ b/Scripts/Patches/SaveLoad/RemoveSubBookmarksFromMainRailsOnLoadPatch.cs @@ -16,7 +16,7 @@ public class BookmarkController_LoadFrom { static void Postfix(BookmarkController __instance) { - Ex.RunSafe(() => RemoveSubBookmarksFromMainRailsOnLoad( __instance)); + Ex.RunSafe(() => RemoveSubBookmarksFromMainRailsOnLoad( __instance), null, true); } } diff --git a/Scripts/Patches/SaveLoad/StoreSubRecipesOntoAvailableRailsOnSavePatch.cs b/Scripts/Patches/SaveLoad/StoreSubRecipesOntoAvailableRailsOnSavePatch.cs index 276f0d8..c42a77a 100644 --- a/Scripts/Patches/SaveLoad/StoreSubRecipesOntoAvailableRailsOnSavePatch.cs +++ b/Scripts/Patches/SaveLoad/StoreSubRecipesOntoAvailableRailsOnSavePatch.cs @@ -41,10 +41,10 @@ private static bool AssignSubBookmarksToRailsBeforeSerialization(ref SerializedB var allSubBookmarks = StaticStorage.BookmarkGroups.SelectMany(bg => bg.Value).ToList(); allSubBookmarks.ForEach(bookmark => { - var spawnPosition = SubRailService.GetSpawnPosition(instance, SpaceType.Large) - ?? SubRailService.GetSpawnPosition(instance, SpaceType.Medium) - ?? SubRailService.GetSpawnPosition(instance, SpaceType.Small) - ?? SubRailService.GetSpawnPosition(instance, SpaceType.Min); + var spawnPosition = GetSpawnPosition(instance, SpaceType.Large, subBookmarkPositions) + ?? GetSpawnPosition(instance, SpaceType.Medium, subBookmarkPositions) + ?? GetSpawnPosition(instance, SpaceType.Small, subBookmarkPositions) + ?? GetSpawnPosition(instance, SpaceType.Min, subBookmarkPositions); if (!subBookmarkPositions.ContainsKey(spawnPosition.Item1)) { @@ -61,7 +61,7 @@ private static bool AssignSubBookmarksToRailsBeforeSerialization(ref SerializedB serialized.serializedRails = instance.rails.Select(rail => GetSerialized(rail, subBookmarkPositions)).ToList(); //Since we messed with the order of the bookmarks we need to change the saved recipe order to ensure recipes are in their proper bookmarks on load (if the mod gets uninstalled) RearrangeSavedBookmarksToWorkForBookmarkOrder(serialized, subBookmarkPositions); - }); + }, null, true); result = serialized; return false; } diff --git a/Scripts/Services/SaveLoadService.cs b/Scripts/Services/SaveLoadService.cs index bf95ce8..4ee08fa 100644 --- a/Scripts/Services/SaveLoadService.cs +++ b/Scripts/Services/SaveLoadService.cs @@ -31,7 +31,9 @@ public static void StoreBookmarkGroups(ref string result) var toSerialize = new StaticStorage.SavedStaticStorage { BookmarkGroups = StaticStorage.BookmarkGroups, - SavedRecipePositions = StaticStorage.SavedRecipePositions + SavedRecipePositions = StaticStorage.SavedRecipePositions, + ErrorLog = StaticStorage.ErrorLog, + BookmarkManagerVersion = Plugin.PLUGIN_GUID }; var serializedGroups = JsonConvert.SerializeObject(toSerialize, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); diff --git a/Scripts/Storage/StaticStorage.cs b/Scripts/Storage/StaticStorage.cs index dd7bf89..e524b1f 100644 --- a/Scripts/Storage/StaticStorage.cs +++ b/Scripts/Storage/StaticStorage.cs @@ -18,6 +18,7 @@ public static class StaticStorage public static Dictionary> BookmarkGroups = new(); public static List SavedRecipePositions; + public static List ErrorLog = new(); public static BookmarkRail SubRail; public static GameObject SubRailPages; @@ -41,6 +42,8 @@ public class SavedStaticStorage { public Dictionary> BookmarkGroups { get; set; } public List SavedRecipePositions { get; set; } + public List ErrorLog { get; set; } + public string BookmarkManagerVersion { get; set; } } } }