-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
BetterUIWindow.cs
217 lines (191 loc) · 8.27 KB
/
BetterUIWindow.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using System.Collections.Generic;
using System.Text;
using RoR2;
using RoR2.UI;
using RoR2.UI.MainMenu;
using RoR2.UI.SkinControllers;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using System.Reflection;
using System.Collections;
namespace BetterUI
{
internal static class BetterUIWindow
{
static AssetBundle bundle;
static GameObject modPanelPrefab;
static GameObject modButtonPrefab;
static GameObject betterUIWindowPrefab;
internal static Transform menuParent;
static BetterUIWindow()
{
bundle = AssetBundle.LoadFromMemory(Properties.Resources.betteruiassets);
modPanelPrefab = bundle.LoadAsset<GameObject>($"Assets/ModPanel.prefab");
modButtonPrefab = bundle.LoadAsset<GameObject>($"Assets/ModButton.prefab");
betterUIWindowPrefab = bundle.LoadAsset<GameObject>($"Assets/BetterUIWindow.prefab");
}
class BetterUIDestroyer : MonoBehaviour{
internal GameObject monitoredGameObject;
void OnDisable(){
if(monitoredGameObject == null ||
Mathf.Abs(monitoredGameObject.transform.position.x) < 50 ||
Mathf.Abs(monitoredGameObject.transform.position.y) < 50 )
{
UnityEngine.Object.Destroy(BetterUIPlugin.instance);
Debug.LogError("It appears the BetterUI Button has been destroyed or moved off the screen, this is not supported and BetterUI has been disabled.");
}
}
}
internal static void Init()
{
BetterUIPlugin.Hooks.Add<BaseMainMenuScreen>(nameof(BaseMainMenuScreen.Awake), BaseMainMenuScreen_Awake);
ViewablesCatalog.Node node = new ViewablesCatalog.Node("BetterUI", true, null);
var buttonNode = new ViewablesCatalog.Node("Donate", false, node);
buttonNode.shouldShowUnviewed = (UserProfile userProfile) => !userProfile.HasViewedViewable(buttonNode.fullName);
ViewablesCatalog.AddNodeToRoot(node);
}
static void BaseMainMenuScreen_Awake(Action<BaseMainMenuScreen> orig, BaseMainMenuScreen self)
{
menuParent = self.transform;
var transform = self.transform.Find("SafeZone/GenericMenuButtonPanel");
var DescriptionGameObject = self.transform.Find("SafeZone/GenericMenuButtonPanel/JuicePanel/DescriptionPanel, Naked/ContentSizeFitter/DescriptionText");
if (transform != null || DescriptionGameObject != null)
{
var DescriptionController = DescriptionGameObject.GetComponent<LanguageTextMeshController>();
var modPanel = GameObject.Instantiate(modPanelPrefab, transform);
float bigifier = 0.1f;
modPanel.transform.position += new Vector3(Mathf.SmoothStep(0,250, bigifier),0,0);
foreach (var hgButton in modPanel.GetComponentsInChildren<HGButton>())
{
hgButton.transform.localScale *= 1 + bigifier;
var destroyer = self.gameObject.AddComponent<BetterUIDestroyer>();
destroyer.monitoredGameObject = hgButton.gameObject;
hgButton.hoverLanguageTextMeshController = DescriptionController;
}
}
orig(self);
}
}
public class BetterUIEventFunctions : MonoBehaviour
{
static Dictionary<GameObject, GameObject> spawnedGameObjects = new Dictionary<GameObject, GameObject>();
public void createWindow(GameObject prefab)
{
if (BetterUIWindow.menuParent != null)
{
var exists = spawnedGameObjects.TryGetValue(prefab, out var instance);
if (!exists || instance == null)
{
spawnedGameObjects[prefab] = GameObject.Instantiate(prefab, BetterUIWindow.menuParent);
Destroy(spawnedGameObjects[prefab].transform.Find("Window/UpperLeftButtonPanel/DonateButton")?.gameObject);
Destroy(spawnedGameObjects[prefab].transform.Find("Window/UpperLeftButtonPanel/DiscordButton")?.gameObject);
Destroy(spawnedGameObjects[prefab].transform.Find("Window/SocialPanel/TwitterButton")?.gameObject);
}
}
}
}
[ExecuteAlways]
internal class PrefabLoader : MonoBehaviour
{
public string prefabAddress;
private string loadedPrefab;
private Boolean loading = false;
GameObject instance;
void Start()
{
LoadPrefab();
}
void OnValidate()
{
LoadPrefab();
}
void LoadPrefab()
{
if (!string.IsNullOrEmpty(prefabAddress) && !loading)
{
loading = true;
Addressables.LoadAssetAsync<GameObject>(prefabAddress).Completed += PrefabLoaded;
}
}
private void PrefabLoaded(AsyncOperationHandle<GameObject> obj)
{
switch (obj.Status)
{
case AsyncOperationStatus.Succeeded:
if (loadedPrefab == prefabAddress) break;
if (instance != null) DestroyImmediate(instance);
var prefab = obj.Result;
instance = Instantiate(prefab);
SetRecursiveFlags(instance.transform);
instance.transform.SetParent(this.gameObject.transform, false);
loadedPrefab = prefabAddress;
loading = false;
break;
case AsyncOperationStatus.Failed:
if (instance != null) DestroyImmediate(instance);
Debug.LogError("Prefab load failed.");
loading = false;
break;
default:
// case AsyncOperationStatus.None:
break;
}
}
static void SetRecursiveFlags(Transform transform)
{
transform.gameObject.hideFlags |= HideFlags.DontSave;
foreach (Transform child in transform)
{
SetRecursiveFlags(child);
}
}
}
[ExecuteAlways]
internal class AddressableAssetLoader : MonoBehaviour
{
public Component component;
public string fieldName;
public string assetAddress;
private static readonly MethodInfo LoadAssetAsyncInfo = typeof(Addressables).GetMethod(nameof(Addressables.LoadAssetAsync), new[] { typeof(string) });
void LoadAsset(bool dontSave = false)
{
var typ = component.GetType();
var field = typ.GetField(fieldName, BindingFlags.Public | BindingFlags.Static | BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.NonPublic);
PropertyInfo property = null;
if (field == null)
{
property = typ.GetProperty(fieldName, BindingFlags.Public | BindingFlags.Static | BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.NonPublic);
if (property == null) return;
}
var meth = LoadAssetAsyncInfo.MakeGenericMethod(field?.FieldType ?? property.PropertyType);
var awaiter = meth.Invoke(null, new object[] { assetAddress });
var wait = awaiter.GetType().GetMethod("WaitForCompletion", BindingFlags.Instance | BindingFlags.Public);
var asset = wait.Invoke(awaiter, null);
var assetObject = (UnityEngine.Object)asset;
if (assetObject != null)
{
if (dontSave)
{
assetObject.hideFlags |= HideFlags.DontSave;
}
field?.SetValue(component, asset);
property?.SetValue(component, asset);
}
}
IEnumerator WaitAndLoadAsset()
{
yield return new WaitUntil(() => Addressables.InternalIdTransformFunc != null);
LoadAsset(true);
}
void Start()
{
LoadAsset();
}
void OnValidate()
{
if (gameObject.activeInHierarchy) StartCoroutine(WaitAndLoadAsset());
}
}
}