-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPlugin.cs
149 lines (138 loc) · 6.25 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
using BepInEx;
using System.IO;
using UnityEngine;
using HarmonyLib;
using USTManager.Data;
using USTManager.Patches;
using TMPro;
using UnityEngine.UI;
using USTManager.Misc;
using System.Linq;
using Newtonsoft.Json;
using BepInEx.Configuration;
namespace USTManager
{
[BepInPlugin("dev.zeddevstuff.ustmanager", "USTManager", "1.5.5")]
public class Plugin : BaseUnityPlugin
{
public static string UKPath, USTDir, LastUSTs;
public ConfigEntry<bool> UpdateBasedSwappingEnabled;
public static GameObject MenuEntryPrefab, SelectionScreenPrefab, SelectionScreenEntryPrefab, ConflictEntryPrefab, ConflictResolutionScreenPrefab, ToastPrefab;
private void Awake()
{
UpdateBasedSwappingEnabled = Config.Bind(
"Experimental",
"UpdateBasedSwappingEnabled",
false,
"Enable this to use Update based swapping on top of method patching. This should allow most sounds to be swapped. This method will probably hurt performance.");
instance = this;
Harmony.CreateAndPatchAll(typeof(AudioSourcePatches));
Harmony.CreateAndPatchAll(typeof(MainMenuPatches));
UKPath = new DirectoryInfo(Application.dataPath).Parent.FullName;
LastUSTs = Path.Combine(UKPath, "USTs","lastUSTs.json");
if(File.Exists(LastUSTs))
{
try
{
Manager.USTSave data = JsonConvert.DeserializeObject<Manager.USTSave>(File.ReadAllText(LastUSTs));
if(data != null)
{
USTSelectionScreen.InternalConfirm(data.Selected, data.UST);
}
}
catch(System.Exception e)
{
Debug.LogError(e);
}
}
DirectoryInfo ustDir = new(Path.Combine(UKPath, "USTs"));
USTDir = ustDir.FullName;
if(!ustDir.Exists) ustDir.Create();
AssetBundle bundle = AssetBundle.LoadFromMemory(Resources.Resource1.ust);
MenuEntryPrefab = bundle.LoadAsset<GameObject>("MenuEntry");
MenuEntryPrefab.AddComponent<HudOpenEffect>();
SelectionScreenEntryPrefab = bundle.LoadAsset<GameObject>("USTEntry");
USTEntry entry = SelectionScreenEntryPrefab.AddComponent<USTEntry>();
entry.Image = SelectionScreenEntryPrefab.transform.GetChild(0).GetComponent<Image>();
entry.Name = SelectionScreenEntryPrefab.transform.GetChild(1).GetComponent<TMP_Text>();
entry.Author = SelectionScreenEntryPrefab.transform.GetChild(2).GetComponent<TMP_Text>();
entry.IconButton = SelectionScreenEntryPrefab.transform.GetChild(0).GetComponent<Button>();
entry.Status = SelectionScreenEntryPrefab.transform.GetChild(0).GetChild(0).GetComponent<TMP_Text>();
SelectionScreenPrefab = bundle.LoadAsset<GameObject>("OptionMenu");
SelectionScreenPrefab?.AddComponent<USTSelectionScreen>();
ConflictEntryPrefab = bundle.LoadAsset<GameObject>("Conflict");
ConflictResolutionScreenPrefab = bundle.LoadAsset<GameObject>("ConflictResolutionScreen");
ConflictResolutionScreenPrefab?.AddComponent<ConflictResolutionScreen>();
CreateTemplate();
Logger.LogInfo($"Plugin {PluginInfo.PLUGIN_GUID} is loaded!");
}
System.Diagnostics.Stopwatch sw = new();
public void Update()
{
if(!UpdateBasedSwappingEnabled.Value) return;
AudioSource[] sources = /*UnityEngine.Resources.*/FindObjectsOfType<AudioSource>();
foreach(AudioSource source in sources)
{
if(source.GetComponent<USTTarget>()) continue;
else source.gameObject.AddComponent<USTTarget>();
}
}
public static USTSelectionScreen Screen;
public static void OpenMenu(Transform transform)
{
if(Screen == null)
{
Screen = Instantiate(SelectionScreenPrefab, transform).GetComponent<USTSelectionScreen>();
Screen.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, 0);
}
else
{
Screen.gameObject.SetActive(true);
}
}
private static Plugin instance;
public static void RunCoroutine(System.Collections.IEnumerator routine)
{
instance.StartCoroutine(routine);
}
public void CreateTemplate()
{
Directory.CreateDirectory(Path.Combine(UKPath, "USTs", "Template"));
File.WriteAllText(Path.Combine(UKPath, "USTs", "Template", "template.ust"), CustomUST.GetTemplateJson());
File.WriteAllText(Path.Combine(UKPath, "USTs", "Template", "readme.md"), StaticResources.Readme);
}
private void OnGUI()
{
if(!Manager.IsDebug) return;
AudioSource[] sources = FindObjectsOfType<AudioSource>()
.Where(s => s.isPlaying)
.Where(s => Manager.IsExtendedDebug ? true : s.spatialBlend < 1.0)
.ToArray();
void DrawColumn(System.Action<AudioSource> action)
{
using var _ = new GUILayout.VerticalScope();
foreach(AudioSource s in sources)
{
if(s is null) continue;
action(s);
}
}
GUIStyle style = GUI.skin.label;
style.fontSize = 24;
using var _ = new GUILayout.HorizontalScope();
style.normal.textColor = Color.green;
DrawColumn(s => GUILayout.Label(s.name ?? "null"));
GUILayout.Space(20);
style.normal.textColor = Color.white;
DrawColumn(s => GUILayout.Label(s.clip ? s.clip.name ?? "null" : "null"));
GUILayout.Space(20);
style.normal.textColor = Color.red;
DrawColumn(s =>
{
using var _ = new GUILayout.HorizontalScope();
GUILayout.Label(s.volume * 100 + "%");
GUILayout.HorizontalSlider(s.time, 0, s.clip ? s.clip.length : 0, GUILayout.Width(150));
});
}
}
}