-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin.cs
184 lines (160 loc) · 6.14 KB
/
Plugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using System;
using HarmonyLib;
using UnityEngine;
using BinderSearch.Helpers;
namespace BinderSearch
{
[BepInPlugin(MyPluginInfo.PLUGIN_GUID, MyPluginInfo.PLUGIN_NAME, MyPluginInfo.PLUGIN_VERSION)]
[BepInProcess("Card Shop Simulator.exe")]
public class Plugin : BaseUnityPlugin
{
internal static new ManualLogSource Logger;
private readonly Harmony harmony = new Harmony(MyPluginInfo.PLUGIN_GUID);
public static ConfigEntry<KeyboardShortcut> SearchHotkey;
public static bool activeGame = false;
private static int currentPage = 1;
// UI Components
private SimpleTextEntry textEntry;
private bool uiInitialized = false;
private static GameObject searchUIObject;
// Static instance for patches to access
public static Plugin Instance { get; private set; }
private void Update()
{
if (SearchHotkey.Value.IsDown())
{
Logger.LogInfo("Search hotkey pressed");
//Logger.LogInfo($"activeGame state: {activeGame}");
var binderUI = UnityEngine.Object.FindObjectOfType<CollectionBinderUI>();
if (binderUI == null || !binderUI.m_ScreenGrp.activeSelf)
{
return;
}
TriggerSearch();
}
}
private void Awake()
{
Instance = this; // Set instance for patches to access
Logger = base.Logger;
Logger.LogInfo($"Plugin Binder Search is loaded!");
SearchHotkey = Config.Bind(
"Hotkey Settings",
"Search Hotkey",
new KeyboardShortcut(KeyCode.T),
"Press this key to activate the binder search feature when the binder is open."
);
}
private void Start()
{
SetupSearchUI();
}
private void SetupSearchUI()
{
try
{
if (uiInitialized && textEntry != null)
{
return;
}
// Clean up any existing UI
if (searchUIObject != null)
{
Destroy(searchUIObject);
}
// Create UI at root level
searchUIObject = new GameObject("BinderSearchUI");
DontDestroyOnLoad(searchUIObject);
// Add SimpleTextEntry component
textEntry = searchUIObject.AddComponent<SimpleTextEntry>();
if (textEntry == null)
{
throw new Exception("Failed to add SimpleTextEntry component");
}
// Subscribe to the text changed event
textEntry.OnTextChanged += OnSearchValueChanged;
uiInitialized = true;
}
catch (Exception ex)
{
Logger.LogError($"Error setting up search UI: {ex.Message}\nStack trace: {ex.StackTrace}");
uiInitialized = false;
textEntry = null;
}
}
private void OnDestroy()
{
if (textEntry != null)
{
textEntry.OnTextChanged -= OnSearchValueChanged;
}
if (searchUIObject != null)
{
Destroy(searchUIObject);
}
uiInitialized = false;
textEntry = null;
}
private void TriggerSearch()
{
try
{
Logger.LogInfo("TriggerSearch called");
// Get current page
var binderUI = UnityEngine.Object.FindObjectOfType<CollectionBinderUI>();
if (binderUI == null)
{
Logger.LogWarning("CollectionBinderUI not found!");
return;
}
var pageText = binderUI.m_PageText.text;
var parts = pageText.Split('/');
if (parts.Length > 0)
{
int.TryParse(parts[0].Trim(), out currentPage);
Logger.LogInfo($"Parsed current page: {currentPage}");
}
textEntry.ShowEntryPanel();
}
catch (Exception ex)
{
Logger.LogError($"Error triggering search: {ex.Message}\nStack trace: {ex.StackTrace}");
}
}
private void OnSearchValueChanged(string value)
{
//Logger.LogInfo($"Search value changed to: {value}");
NavigateToPage(value);
}
private void NavigateToPage(string value)
{
if (string.IsNullOrEmpty(value)) return;
if (int.TryParse(value, out int targetPage))
{
var binderUI = UnityEngine.Object.FindObjectOfType<CollectionBinderUI>();
if (binderUI != null && binderUI.m_CollectionAlbum != null)
{
// Get max page
var pageText = binderUI.m_PageText.text;
var parts = pageText.Split('/');
if (parts.Length > 1)
{
if (int.TryParse(parts[1].Trim(), out int maxPage))
{
Logger.LogInfo($"Navigating from page {currentPage} to {targetPage} (max: {maxPage})");
// Ensure target page is within bounds
if (targetPage < 1) targetPage = 1;
if (targetPage > maxPage) targetPage = maxPage;
binderUI.SetCurrentPage(targetPage);
BinderNavigationHelper.NavigateToPage(binderUI.m_CollectionAlbum, targetPage, Logger);
Logger.LogInfo($"Navigation complete - now on page {currentPage}");
}
}
}
}
}
}
}