Rewrite flea back button to handle flea being disposed

This commit is contained in:
Tyfon
2024-06-06 02:37:08 -07:00
parent dcb8f98f0c
commit 3f22ab6eea
2 changed files with 149 additions and 104 deletions

View File

@@ -23,12 +23,7 @@ namespace UIFixes
private static readonly Stack<HistoryEntry> History = new(); private static readonly Stack<HistoryEntry> History = new();
private static bool FirstFilter = true;
private static bool GoingBack = false;
private static string DelayedHandbookId = string.Empty; private static string DelayedHandbookId = string.Empty;
private static DefaultUIButton PreviousButton;
private static float PossibleScrollPosition = -1f; private static float PossibleScrollPosition = -1f;
public static void Enable() public static void Enable()
@@ -40,110 +35,114 @@ namespace UIFixes
Settings.EnableFleaHistory.SettingChanged += (object sender, EventArgs args) => Settings.EnableFleaHistory.SettingChanged += (object sender, EventArgs args) =>
{ {
if (!Settings.EnableFleaHistory.Value && PreviousButton != null) if (!Settings.EnableFleaHistory.Value && PreviousFilterButton.Instance != null)
{ {
UnityEngine.Object.Destroy(PreviousButton.gameObject); UnityEngine.Object.Destroy(PreviousFilterButton.Instance.gameObject);
PreviousButton = null; PreviousFilterButton.Instance = null;
History.Clear(); History.Clear();
} }
}; };
} }
public class RagfairScreenShowPatch : ModulePatch public class PreviousFilterButton : MonoBehaviour
{ {
protected override MethodBase GetTargetMethod() private RagFairClass ragfair;
private RagfairScreen ragfairScreen;
private DefaultUIButton button;
private LayoutElement layoutElement;
private bool goingBack = false;
public static PreviousFilterButton Instance;
public void Awake()
{ {
return AccessTools.Method(typeof(RagfairScreen), nameof(RagfairScreen.Show)); Instance = this;
button = GetComponent<DefaultUIButton>();
layoutElement = GetComponent<LayoutElement>();
button.OnClick.RemoveAllListeners();
button.OnClick.AddListener(OnClick);
} }
[PatchPrefix] public void Show(RagfairScreen ragfairScreen, RagFairClass ragfair)
public static void Prefix(RagfairScreen __instance, ISession session, DefaultUIButton ____addOfferButton)
{ {
// Create previous button this.ragfair = ragfair;
if (Settings.EnableFleaHistory.Value && PreviousButton == null) this.ragfairScreen = ragfairScreen;
{
var addOfferLayout = ____addOfferButton.GetComponent<LayoutElement>();
PreviousButton = UnityEngine.Object.Instantiate(____addOfferButton, ____addOfferButton.transform.parent, false);
PreviousButton.transform.SetAsFirstSibling();
PreviousButton.SetRawText("< " + "back".Localized(), 20);
session.RagFair.OnFilterRuleChanged += (source, clear, updateCategories) => OnFilterRuleChanged(__instance, session); button.SetRawText("< " + "back".Localized(), 20);
PreviousButton.OnClick.AddListener(() => layoutElement.minWidth = -1;
{ layoutElement.preferredWidth = -1;
History.Pop(); // remove current
if (History.Count < 2)
{
PreviousButton.Interactable = false;
}
HistoryEntry previousEntry = History.Peek(); // Prime the first filter
OnFilterRuleChanged();
// Manually update parts of the UI because BSG sucks ragfair.OnFilterRuleChanged += OnFilterRuleChanged;
UpdateColumnHeaders(__instance.R().OfferViewList.R().FiltersPanel, previousEntry.filterRule.SortType, previousEntry.filterRule.SortDirection);
GoingBack = true;
ApplyFullFilter(session.RagFair, previousEntry.filterRule);
GoingBack = false;
});
}
}
[PatchPostfix]
public static void Postfix(RagfairScreen __instance, ISession session, DefaultUIButton ____addOfferButton)
{
// Delete the upper right display options, since they aren't even implemented
var tabs = __instance.transform.Find("TopRightPanel/Tabs");
tabs?.gameObject.SetActive(false);
if (!Settings.EnableFleaHistory.Value)
{
return;
}
// Resize the Add Offer button to use less extra space
var addOfferLayout = ____addOfferButton.GetComponent<LayoutElement>();
addOfferLayout.minWidth = -1;
addOfferLayout.preferredWidth = -1;
// Recenter the add offer text
var addOfferLabel = ____addOfferButton.transform.Find("SizeLabel");
addOfferLabel.localPosition = new Vector3(0f, 0f, 0f);
// For some reason the widths revert
PreviousButton.SetRawText("< " + "back".Localized(), 20); // Update text in case language changes
var prevButtonLayout = PreviousButton.GetComponent<LayoutElement>();
prevButtonLayout.minWidth = -1;
prevButtonLayout.preferredWidth = -1;
// Tighten up the spacing
var layoutGroup = PreviousButton.transform.parent.GetComponent<HorizontalLayoutGroup>();
layoutGroup.spacing = 5f;
if (History.Count < 2) if (History.Count < 2)
{ {
PreviousButton.Interactable = false; button.Interactable = false;
} }
PreviousButton.gameObject.SetActive(session.RagFair.FilterRule.ViewListType == EViewListType.AllOffers); gameObject.SetActive(ragfair.FilterRule.ViewListType == EViewListType.AllOffers);
} }
private static void OnFilterRuleChanged(RagfairScreen ragScreen, ISession session) public void Close()
{ {
if (FirstFilter || ragfair.OnFilterRuleChanged -= OnFilterRuleChanged;
GoingBack || ragfair = null;
!String.IsNullOrEmpty(DelayedHandbookId) || ragfairScreen = null;
session.RagFair.FilterRule.ViewListType != EViewListType.AllOffers) }
public void OnOffersLoaded(OfferViewList offerViewList)
{
if (!String.IsNullOrEmpty(DelayedHandbookId))
{
// Super important to clear DelayedHandbookId *before* calling method_10, or infinite loops can occur!
string newHandbookId = DelayedHandbookId;
DelayedHandbookId = string.Empty;
offerViewList.method_10(newHandbookId, false);
return;
}
// Restore scroll position now that the we're loaded
if (History.Any())
{
offerViewList.R().Scroller.SetScrollPosition(History.Peek().scrollPosition);
}
}
private void OnClick()
{
History.Pop(); // remove current
if (History.Count < 2)
{
button.Interactable = false;
}
HistoryEntry previousEntry = History.Peek();
// Manually update parts of the UI because BSG sucks
UpdateColumnHeaders(ragfairScreen.R().OfferViewList.R().FiltersPanel, previousEntry.filterRule.SortType, previousEntry.filterRule.SortDirection);
goingBack = true;
ApplyFullFilter(previousEntry.filterRule);
goingBack = false;
}
private void OnFilterRuleChanged(RagFairClass.ESetFilterSource source = 0, bool clear = false, bool updateCategories = false)
{
if (goingBack || !string.IsNullOrEmpty(DelayedHandbookId) || ragfair.FilterRule.ViewListType != EViewListType.AllOffers)
{ {
FirstFilter = false;
return; return;
} }
HistoryEntry current = History.Any() ? History.Peek() : null; HistoryEntry current = History.Any() ? History.Peek() : null;
if (current != null && current.filterRule.IsSimilarTo(session.RagFair.FilterRule)) if (current != null && current.filterRule.IsSimilarTo(ragfair.FilterRule))
{ {
// Minor filter change, just update the current one // Minor filter change, just update the current one
current.filterRule = session.RagFair.FilterRule; current.filterRule = ragfair.FilterRule;
return; return;
} }
@@ -156,16 +155,16 @@ namespace UIFixes
} }
else else
{ {
LightScroller scroller = ragScreen.R().OfferViewList.R().Scroller; LightScroller scroller = ragfairScreen.R().OfferViewList.R().Scroller;
current.scrollPosition = scroller.NormalizedScrollPosition; current.scrollPosition = scroller.NormalizedScrollPosition;
} }
} }
History.Push(new HistoryEntry() { filterRule = session.RagFair.FilterRule }); History.Push(new HistoryEntry() { filterRule = ragfair.FilterRule });
if (History.Count >= 2) if (History.Count >= 2)
{ {
PreviousButton.Interactable = true; button.Interactable = true;
} }
// Basic sanity to keep this from growing out of control // Basic sanity to keep this from growing out of control
@@ -188,7 +187,7 @@ namespace UIFixes
// Using GClass because it's easier // Using GClass because it's easier
// Copied from RagFairClass.AddSearchesInRule, but actually all of the properties // Copied from RagFairClass.AddSearchesInRule, but actually all of the properties
private static void ApplyFullFilter(RagFairClass ragFair, FilterRule filterRule) private void ApplyFullFilter(FilterRule filterRule)
{ {
// Order impacts the order the filters show in the UI // Order impacts the order the filters show in the UI
var searches = new List<GClass3196>(); var searches = new List<GClass3196>();
@@ -226,17 +225,17 @@ namespace UIFixes
searches.Add(new(EFilterType.OfferOwnerType, filterRule.OfferOwnerType, filterRule.OfferOwnerType != 0)); searches.Add(new(EFilterType.OfferOwnerType, filterRule.OfferOwnerType, filterRule.OfferOwnerType != 0));
searches.Add(new(EFilterType.OnlyFunctional, filterRule.OnlyFunctional ? 1 : 0, filterRule.OnlyFunctional)); searches.Add(new(EFilterType.OnlyFunctional, filterRule.OnlyFunctional ? 1 : 0, filterRule.OnlyFunctional));
ragFair.method_24(filterRule.ViewListType, [.. searches], false, out FilterRule newRule); ragfair.method_24(filterRule.ViewListType, [.. searches], false, out FilterRule newRule);
// These properties don't consistute a new search, so much as a different view of the same search // These properties don't consistute a new search, so much as a different view of the same search
newRule.Page = filterRule.Page; newRule.Page = filterRule.Page;
newRule.SortType = filterRule.SortType; newRule.SortType = filterRule.SortType;
newRule.SortDirection = filterRule.SortDirection; newRule.SortDirection = filterRule.SortDirection;
// We can't set handbookId yet - it limits the result set and that in turn limits what categories even display // We can't set handbookId yet - it limits the result set and that in turn limits what categories even display
DelayedHandbookId = filterRule.HandbookId; DelayedHandbookId = filterRule.HandbookId;
ragFair.SetFilterRule(newRule, true, true); ragfair.SetFilterRule(newRule, true, true);
} }
private static void UpdateColumnHeaders(FiltersPanel filtersPanel, ESortType sortType, bool sortDirection) private static void UpdateColumnHeaders(FiltersPanel filtersPanel, ESortType sortType, bool sortDirection)
@@ -255,6 +254,63 @@ namespace UIFixes
} }
} }
public class RagfairScreenShowPatch : ModulePatch
{
protected override MethodBase GetTargetMethod()
{
return AccessTools.Method(typeof(RagfairScreen), nameof(RagfairScreen.Show));
}
[PatchPrefix]
public static void Prefix(RagfairScreen __instance, ISession session, DefaultUIButton ____addOfferButton, ref PreviousFilterButton __state)
{
// Create previous button
if (!Settings.EnableFleaHistory.Value)
{
return;
}
__state = ____addOfferButton.transform.parent.Find("PreviousFilterButton")?.GetComponent<PreviousFilterButton>();
if (__state == null)
{
var clone = UnityEngine.Object.Instantiate(____addOfferButton, ____addOfferButton.transform.parent, false);
clone.name = "PreviousFilterButton";
clone.transform.SetAsFirstSibling();
__state = clone.GetOrAddComponent<PreviousFilterButton>();
}
}
[PatchPostfix]
public static void Postfix(RagfairScreen __instance, ISession session, DefaultUIButton ____addOfferButton, PreviousFilterButton __state)
{
// Delete the upper right display options, since they aren't even implemented
var tabs = __instance.transform.Find("TopRightPanel/Tabs");
tabs?.gameObject.SetActive(false);
if (!Settings.EnableFleaHistory.Value)
{
return;
}
__state.Show(__instance, session.RagFair);
__instance.R().UI.AddDisposable(__state.Close);
// Resize the Add Offer button to use less extra space
var addOfferLayout = ____addOfferButton.GetComponent<LayoutElement>();
addOfferLayout.minWidth = -1;
addOfferLayout.preferredWidth = -1;
// Recenter the add offer text
var addOfferLabel = ____addOfferButton.transform.Find("SizeLabel");
addOfferLabel.localPosition = new Vector3(0f, 0f, 0f);
// Tighten up the spacing
var layoutGroup = PreviousFilterButton.Instance.transform.parent.GetComponent<HorizontalLayoutGroup>();
layoutGroup.spacing = 5f;
}
}
public class ChangedViewListTypePatch : ModulePatch public class ChangedViewListTypePatch : ModulePatch
{ {
protected override MethodBase GetTargetMethod() protected override MethodBase GetTargetMethod()
@@ -265,7 +321,7 @@ namespace UIFixes
[PatchPostfix] [PatchPostfix]
public static void Postfix(EViewListType type) public static void Postfix(EViewListType type)
{ {
PreviousButton?.gameObject.SetActive(type == EViewListType.AllOffers); PreviousFilterButton.Instance?.gameObject.SetActive(type == EViewListType.AllOffers);
} }
} }
@@ -307,21 +363,7 @@ namespace UIFixes
return; return;
} }
if (!String.IsNullOrEmpty(DelayedHandbookId)) PreviousFilterButton.Instance.OnOffersLoaded(__instance);
{
// Super important to clear DelayedHandbookId *before* calling method_10, or infinite loops can occur!
string newHandbookId = DelayedHandbookId;
DelayedHandbookId = string.Empty;
__instance.method_10(newHandbookId, false);
return;
}
// Restore scroll position now that the we're loaded
if (History.Any())
{
____scroller.SetScrollPosition(History.Peek().scrollPosition);
}
if (Settings.AutoExpandCategories.Value) if (Settings.AutoExpandCategories.Value)
{ {

5
R.cs
View File

@@ -363,18 +363,21 @@ namespace UIFixes
public TextMeshProUGUI Text { get { return (TextMeshProUGUI)TextField.GetValue(Value); } } public TextMeshProUGUI Text { get { return (TextMeshProUGUI)TextField.GetValue(Value); } }
} }
public class RagfairScreen(object value) : Wrapper(value) public class RagfairScreen(object value) : UIInputNode(value)
{ {
public static Type Type { get; private set; } public static Type Type { get; private set; }
private static FieldInfo OfferViewListField; private static FieldInfo OfferViewListField;
private static FieldInfo RagfairClassField;
public static void InitTypes() public static void InitTypes()
{ {
Type = typeof(EFT.UI.Ragfair.RagfairScreen); Type = typeof(EFT.UI.Ragfair.RagfairScreen);
OfferViewListField = AccessTools.Field(Type, "offerViewList_0"); OfferViewListField = AccessTools.Field(Type, "offerViewList_0");
RagfairClassField = AccessTools.GetDeclaredFields(Type).Single(f => f.FieldType == typeof(RagFairClass));
} }
public EFT.UI.Ragfair.OfferViewList OfferViewList { get { return (EFT.UI.Ragfair.OfferViewList)OfferViewListField.GetValue(Value); } } public EFT.UI.Ragfair.OfferViewList OfferViewList { get { return (EFT.UI.Ragfair.OfferViewList)OfferViewListField.GetValue(Value); } }
public RagFairClass RagfairClass { get { return (RagFairClass)RagfairClassField.GetValue(Value); } }
} }
public class OfferViewList(object value) : Wrapper(value) public class OfferViewList(object value) : Wrapper(value)