Skip to content

Commit

Permalink
Fixed issue introduced in last release
Browse files Browse the repository at this point in the history
Updated Bookmark Organizer to work for Potion Craft v1.0
  • Loading branch information
AndrewFahlgren committed Dec 14, 2022
1 parent f691435 commit 2269e4c
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 52 deletions.
6 changes: 6 additions & 0 deletions PotionCraftBookmarkOrganizer.csproj
Expand Up @@ -43,6 +43,9 @@
</ItemGroup>

<ItemGroup>
<Reference Include="DOTween">
<HintPath>..\PotionCraftPourBackIn\lib\DOTween.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json">
<HintPath>lib\Newtonsoft.Json.dll</HintPath>
</Reference>
Expand All @@ -64,6 +67,9 @@
<Reference Include="PotionCraft.SoundSystem">
<HintPath>lib\PotionCraft.SoundSystem.dll</HintPath>
</Reference>
<Reference Include="ThirdParty.DOTween">
<HintPath>..\PotionCraftPourBackIn\lib\ThirdParty.DOTween.dll</HintPath>
</Reference>
<Reference Include="Unity.TextMeshPro">
<HintPath>lib\Unity.TextMeshPro.dll</HintPath>
</Reference>
Expand Down
10 changes: 5 additions & 5 deletions Scripts/Patches/RecipeBook/AddGroupCornerIconToBookmarkPatch.cs
Expand Up @@ -27,19 +27,19 @@ static void Postfix(Bookmark __instance)
}
}

[HarmonyPatch(typeof(InactiveBookmarkButton), "OnGrabPrimary")]
[HarmonyPatch(typeof(BookmarkButtonInactive), "OnGrabPrimary")]
public class InactiveBookmarkButton_OnGrabPrimary
{
static void Postfix(InactiveBookmarkButton __instance)
static void Postfix(BookmarkButtonInactive __instance)
{
Ex.RunSafe(() => UpdateCornerIconScaleToMatchActiveState(__instance, true));
}
}

[HarmonyPatch(typeof(InactiveBookmarkButton), "OnReleasePrimary")]
[HarmonyPatch(typeof(BookmarkButtonInactive), "OnReleasePrimary")]
public class InactiveBookmarkButton_OnReleasePrimary
{
static void Postfix(InactiveBookmarkButton __instance)
static void Postfix(BookmarkButtonInactive __instance)
{
Ex.RunSafe(() => UpdateCornerIconScaleToMatchActiveState(__instance, false));
}
Expand Down Expand Up @@ -94,7 +94,7 @@ private static void UpdateCornerIconScaleToMatchActiveState(Bookmark instance)
UpdateCornerIconScaleToMatchActiveState(instance.inactiveBookmarkButton, false, instance);
}

private static void UpdateCornerIconScaleToMatchActiveState(InactiveBookmarkButton instance, bool isGrabbed, Bookmark bookmark = null)
private static void UpdateCornerIconScaleToMatchActiveState(BookmarkButtonInactive instance, bool isGrabbed, Bookmark bookmark = null)
{
if (bookmark == null) bookmark = instance.GetComponentInParent<Bookmark>();
if (bookmark == null) return;
Expand Down
Expand Up @@ -6,7 +6,7 @@
using PotionCraft.ObjectBased.UIElements.Books;
using PotionCraft.ObjectBased.UIElements.Books.RecipeBook;
using PotionCraft.ObjectBased.UIElements.PotionCraftPanel;
using PotionCraft.ScriptableObjects;
using PotionCraft.ScriptableObjects.Potion;
using PotionCraftBookmarkOrganizer.Scripts.Services;
using PotionCraftBookmarkOrganizer.Scripts.Storage;
using System;
Expand Down
101 changes: 66 additions & 35 deletions Scripts/Patches/SaveLoad/RemoveSubBookmarksFromMainRailsOnLoadPatch.cs
@@ -1,22 +1,11 @@
using HarmonyLib;
using PotionCraft.Core.ValueContainers;
using PotionCraft.ManagersSystem;
using PotionCraft.ObjectBased.RecipeMap;
using PotionCraft.ObjectBased.UIElements.Bookmarks;
using PotionCraft.ObjectBased.UIElements.Books.RecipeBook;
using PotionCraft.ObjectBased.UIElements.PotionCraftPanel;
using PotionCraft.SaveLoadSystem;
using PotionCraft.ScriptableObjects;
using PotionCraftBookmarkOrganizer.Scripts.Services;
using PotionCraftBookmarkOrganizer.Scripts.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
using static PotionCraft.ObjectBased.UIElements.Bookmarks.Bookmark;
using static PotionCraft.ObjectBased.UIElements.Bookmarks.BookmarkController;
using static PotionCraft.ObjectBased.UIElements.Bookmarks.BookmarkRail;

namespace PotionCraftBookmarkOrganizer.Scripts.Patches
{
Expand All @@ -35,36 +24,18 @@ private static void RemoveSubBookmarksFromMainRailsOnLoad(BookmarkController ins
{
if (instance != Managers.Potion.recipeBook.bookmarkControllersGroupController.controllers.First().bookmarkController) return;
//If there are no saved recipe positions then there is nothing to remove
if (StaticStorage.SavedRecipePositions == null || !StaticStorage.SavedRecipePositions.Any()) return;
if (StaticStorage.SavedRecipePositions == null || !StaticStorage.SavedRecipePositions.Any())
{
DoHardResetIfNeeded(instance);
return;
}
RemoveSubBookmarksFromMainRails(instance);
//Do this here because it is right before we load the recipes from the progress state into the recipe book
ReorganizeSavedRecipes();
DoIncorrectCountFailsafe();
StaticStorage.SavedRecipePositions = null;
}

//The actual cause of this issue may now be fixed however there may still be save files which are in a messed up state
private static void DoIncorrectCountFailsafe()
{
var bookmarkCount = Managers.Potion.recipeBook.bookmarkControllersGroupController.GetAllBookmarksList().Count;
var recipeCount = StaticStorage.SavedRecipePositions.Count;
if (bookmarkCount == recipeCount) return;
Plugin.PluginLogger.LogError("ERROR: There is an incorrect ammount of bookmarks saved. Running failsafe to fix file!");
while (bookmarkCount > recipeCount)
{
var invisiRailCount = StaticStorage.InvisiRail.railBookmarks.Count;
if (invisiRailCount == 0)
{
Plugin.PluginLogger.LogError("ERROR: Incorrect count failsafe failed to find enough bookmarks on the invisirail to fix count. To recover save file post it in the Potion Craft discord modding channel!");
return;
}
var bookmark = StaticStorage.InvisiRail.railBookmarks[invisiRailCount - 1];
StaticStorage.InvisiRail.railBookmarks.RemoveAt(invisiRailCount - 1);
UnityEngine.Object.Destroy(bookmark.gameObject);
bookmarkCount--;
}
}

private static void RemoveSubBookmarksFromMainRails(BookmarkController instance)
{
var railList = instance.rails.Except(new[] { StaticStorage.SubRail, StaticStorage.InvisiRail }).ToList();
Expand All @@ -85,7 +56,7 @@ private static void RemoveSubBookmarksFromMainRails(BookmarkController instance)
}
var curSavedIndex = StaticStorage.SavedRecipePositions[savedRecipeIndex];
var isOutOfPlace = curSavedIndex != oldListIndex;
RecipeBookService.GetBookmarkStorageRecipeIndex(curSavedIndex, out bool indexIsParent);
RecipeBookService.GetBookmarkStorageRecipeIndex(index, out bool indexIsParent);
if (!isOutOfPlace && !indexIsParent)
{
railIndex++;
Expand All @@ -112,5 +83,65 @@ private static void ReorganizeSavedRecipes()
}
progressState.savedRecipes = potionList;
}

//The actual cause of this issue may now be fixed however there may still be save files which are in a messed up state
private static void DoIncorrectCountFailsafe()
{
var bookmarkCount = Managers.Potion.recipeBook.bookmarkControllersGroupController.GetAllBookmarksList().Count;
var recipeCount = StaticStorage.SavedRecipePositions?.Count ?? Managers.Potion.recipeBook.savedRecipes.Count;
if (bookmarkCount == recipeCount) return;
while (bookmarkCount > recipeCount)
{
var invisiRailCount = StaticStorage.InvisiRail.railBookmarks.Count;
if (invisiRailCount == 0)
{
Plugin.PluginLogger.LogError("ERROR: Incorrect count failsafe failed to find enough bookmarks on the invisirail to fix count. To recover save file post it in the Potion Craft discord modding channel!");
return;
}
var bookmark = StaticStorage.InvisiRail.railBookmarks[invisiRailCount - 1];
StaticStorage.InvisiRail.railBookmarks.RemoveAt(invisiRailCount - 1);
UnityEngine.Object.Destroy(bookmark.gameObject);
bookmarkCount--;
}
}

private static void DoHardResetIfNeeded(BookmarkController instance)
{
//There is a bug where saved recipe positions do not save meaning we also need to do a failsafe here
var bookmarkCount = Managers.Potion.recipeBook.bookmarkControllersGroupController.GetAllBookmarksList().Count;
var recipeCount = Managers.SaveLoad.SelectedProgressState.savedRecipes.Count;
if (bookmarkCount == recipeCount) return;
Plugin.PluginLogger.LogError("ERROR: There is an incorrect ammount of bookmarks saved. Running failsafe to fix file! - 1");
var savedGroupCount = StaticStorage.BookmarkGroups.SelectMany(b => b.Value).Count();
var mainRails = instance.rails.Except(new[] { StaticStorage.SubRail, StaticStorage.InvisiRail }).ToList();
while (bookmarkCount > recipeCount)
{
BookmarkRail railToRemove = null;
if (StaticStorage.InvisiRail.railBookmarks.Count > 0)
{
railToRemove = StaticStorage.InvisiRail;
}
else if (StaticStorage.SubRail.railBookmarks.Count > 0)
{
railToRemove = StaticStorage.SubRail;
}
else
{
railToRemove = mainRails.LastOrDefault(r => r.railBookmarks.Count > 0);
}
if (railToRemove == null)
{
Plugin.PluginLogger.LogError("ERROR: Incorrect count failsafe failed to find enough bookmarks on any rails to fix count. To recover save file post it in the Potion Craft discord modding channel!");
return;
}
var bookmark = railToRemove.railBookmarks[railToRemove.railBookmarks.Count - 1];
railToRemove.railBookmarks.RemoveAt(railToRemove.railBookmarks.Count - 1);
UnityEngine.Object.Destroy(bookmark.gameObject);
bookmarkCount--;
}
//Clear out bookmark groups so we can start fresh
StaticStorage.BookmarkGroups.Clear();
StaticStorage.SavedRecipePositions = null;
}
}
}
Expand Up @@ -21,18 +21,18 @@ namespace PotionCraftBookmarkOrganizer.Scripts.Patches
/// </summary>
public class DisableGrabbingForInactiveStaticBookmarkPatch
{
[HarmonyPatch(typeof(InactiveBookmarkButton), "OnGrabPrimary")]
[HarmonyPatch(typeof(BookmarkButtonInactive), "OnGrabPrimary")]
public class InactiveBookmarkButton_OnGrabPrimary
{
static bool Prefix(InactiveBookmarkButton __instance)
static bool Prefix(BookmarkButtonInactive __instance)
{
return Ex.RunSafe(() => DisableGrabbingForInactiveStaticBookmark(__instance));
}
}

private static bool DisableGrabbingForInactiveStaticBookmark(InactiveBookmarkButton instance)
private static bool DisableGrabbingForInactiveStaticBookmark(BookmarkButtonInactive instance)
{
var bookmark = typeof(InactiveBookmarkButton).GetField("bookmark", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(instance) as Bookmark;
var bookmark = typeof(BookmarkButtonInactive).GetField("bookmark", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(instance) as Bookmark;
return bookmark != StaticStorage.StaticBookmark;
}
}
Expand Down
Expand Up @@ -32,7 +32,7 @@ static bool Prefix()

private static bool DisableRecipeBookHotkeysWhileDraggingBookmark()
{
return Managers.Cursor.grabbedInteractiveItem is not InactiveBookmarkButton;
return Managers.Cursor.grabbedInteractiveItem is not BookmarkButtonInactive;
}
}
}
Expand Up @@ -22,18 +22,18 @@ namespace PotionCraftBookmarkOrganizer.Scripts.Patches
/// </summary>
public class OpenParentRecipeOnInactiveStaticBookmarkClickPatch
{
[HarmonyPatch(typeof(InactiveBookmarkButton), "OnReleasePrimary")]
[HarmonyPatch(typeof(BookmarkButtonInactive), "OnReleasePrimary")]
public class InactiveBookmarkButton_OnReleasePrimary
{
static bool Prefix(InactiveBookmarkButton __instance)
static bool Prefix(BookmarkButtonInactive __instance)
{
return Ex.RunSafe(() => DisableGrabbingForInactiveStaticBookmark(__instance));
}
}

private static bool DisableGrabbingForInactiveStaticBookmark(InactiveBookmarkButton instance)
private static bool DisableGrabbingForInactiveStaticBookmark(BookmarkButtonInactive instance)
{
var bookmark = typeof(InactiveBookmarkButton).GetField("bookmark", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(instance) as Bookmark;
var bookmark = typeof(BookmarkButtonInactive).GetField("bookmark", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(instance) as Bookmark;
if (bookmark != StaticStorage.StaticBookmark) return true;
//Set the parent recipe of this group to be active
var recipeIndex = RecipeBookService.GetBookmarkStorageRecipeIndexForSelectedRecipe();
Expand All @@ -46,7 +46,7 @@ private static bool DisableGrabbingForInactiveStaticBookmark(InactiveBookmarkBut
if (bookmark.activeBookmarkButton.thisCollider.bounds.Contains(Managers.Input.controlsProvider.CurrentMouseWorldPosition))
bookmark.activeBookmarkButton.SetHovered(true);

typeof(InactiveBookmarkButton).GetField("grabConditionChecked", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(instance, true);
typeof(BookmarkButtonInactive).GetField("grabConditionChecked", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(instance, true);

return false;
}
Expand Down
Expand Up @@ -39,6 +39,7 @@ private static bool AddStaticBookmarkToRailBookmarkList(BookmarkRail instance)
private static void UpdateRaycastPriorityForBookmarks(BookmarkRail instance)
{
if (!SubRailService.IsSubRail(instance)) return;
if (StaticStorage.StaticBookmark == null) return;
instance.railBookmarks.ForEach(b => b.SetRaycastPriorityLevel(b.inactiveBookmarkButton.raycastPriorityLevel - 500));
if (instance.railBookmarks.FirstOrDefault() == StaticStorage.StaticBookmark) instance.railBookmarks.RemoveAt(0);
StaticStorage.StaticBookmark.transform.parent = StaticStorage.SubRailActiveBookmarkLayer.transform;
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Services/RecipeBookService.cs
Expand Up @@ -245,7 +245,7 @@ public static bool IsBookmarkGroupParent(int index)

public static void FlipPageToIndex(int nextIndex)
{
if (Managers.Cursor.grabbedInteractiveItem is InactiveBookmarkButton) return;
if (Managers.Cursor.grabbedInteractiveItem is BookmarkButtonInactive) return;
var recipeBook = Managers.Potion.recipeBook;
var pagesCount = recipeBook.GetPagesCount();
recipeBook.curlPageController.HotkeyClicked(nextIndex > recipeBook.currentPageIndex
Expand Down

0 comments on commit 2269e4c

Please sign in to comment.