-
Notifications
You must be signed in to change notification settings - Fork 5
/
CMJS.cs
201 lines (176 loc) · 6.95 KB
/
CMJS.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using HarmonyLib;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
[Plugin("CM JS")]
public class CMJS
{
private NotesContainer notesContainer;
private ObstaclesContainer wallsContainer;
private EventsContainer eventsContainer;
private CustomEventsContainer customEventsContainer;
private BPMChangesContainer bpmChangesContainer;
private List<Check> checks = new List<Check>()
{
new VisionBlocks(),
new StackedNotes()
};
private CheckResult errors;
private UI ui;
private AudioTimeSyncController atsc;
private int index = 0;
private bool movedAfterRun = false;
[Init]
private void Init()
{
SceneManager.sceneLoaded += SceneLoaded;
string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
foreach (string file in Directory.GetFiles(assemblyFolder, "*.js"))
{
checks.Add(new ExternalJS(Path.GetFileName(file)));
}
ui = new UI(this, checks);
try
{
JintPatch.DoPatching();
}
catch (HarmonyException e)
{
Debug.LogError("Failed to patch Jint during CM-JS init");
Debug.LogException(e);
Debug.LogException(e.InnerException);
}
}
private void SceneLoaded(Scene arg0, LoadSceneMode arg1)
{
if (arg0.buildIndex == 3) // Mapper scene
{
notesContainer = UnityEngine.Object.FindObjectOfType<NotesContainer>();
wallsContainer = UnityEngine.Object.FindObjectOfType<ObstaclesContainer>();
eventsContainer = UnityEngine.Object.FindObjectOfType<EventsContainer>();
customEventsContainer = UnityEngine.Object.FindObjectOfType<CustomEventsContainer>();
bpmChangesContainer = UnityEngine.Object.FindObjectOfType<BPMChangesContainer>();
var mapEditorUI = UnityEngine.Object.FindObjectOfType<MapEditorUI>();
atsc = BeatmapObjectContainerCollection.GetCollectionForType(BeatmapObject.ObjectType.Note).AudioTimeSyncController;
// Add button to UI
ui.AddButton(mapEditorUI);
}
}
public void CheckErrors(Check check)
{
var allNotes = notesContainer.LoadedObjects.Cast<BeatmapNote>().OrderBy(it => it.Time).ToList();
var allWalls = wallsContainer.LoadedObjects.Cast<BeatmapObstacle>().OrderBy(it => it.Time).ToList();
var allEvents = eventsContainer.LoadedObjects.Cast<MapEvent>().OrderBy(it => it.Time).ToList();
var allCustomEvents = customEventsContainer.LoadedObjects.Cast<BeatmapCustomEvent>().OrderBy(it => it.Time).ToList();
var allBpmChanges = bpmChangesContainer.LoadedObjects.Cast<BeatmapBPMChange>().OrderBy(it => it.Time).ToList();
if (errors != null)
{
// Remove error outline from old errors
foreach (var block in errors.all)
{
if (BeatmapObjectContainerCollection.GetCollectionForType(BeatmapObject.ObjectType.Note).LoadedContainers.TryGetValue(block.note, out BeatmapObjectContainer container))
{
container.OutlineVisible = SelectionController.IsObjectSelected(container.ObjectData);
container.SetOutlineColor(SelectionController.SelectedColor, false);
}
}
}
try
{
var vals = ui.paramTexts.Select((it, idx) =>
{
switch (it)
{
case UITextInput textInput:
return check.Params[idx].Parse(textInput.InputField.text);
case UIDropdown dropdown:
return check.Params[idx].Parse(dropdown.Dropdown.value.ToString());
case Toggle toggle:
return check.Params[idx].Parse(toggle.isOn.ToString());
default:
return new ParamValue<string>(null); // IDK
}
}).ToArray();
errors = check.PerformCheck(allNotes, allEvents, allWalls, allCustomEvents, allBpmChanges, vals).Commit();
// Highlight blocks in loaded containers in case we don't scrub far enough with MoveToTimeInBeats to load them
foreach (var block in errors.errors)
{
if (BeatmapObjectContainerCollection.GetCollectionForType(BeatmapObject.ObjectType.Note).LoadedContainers.TryGetValue(block.note, out BeatmapObjectContainer container))
{
container.SetOutlineColor(Color.red);
}
}
foreach (var block in errors.warnings)
{
if (BeatmapObjectContainerCollection.GetCollectionForType(BeatmapObject.ObjectType.Note).LoadedContainers.TryGetValue(block.note, out BeatmapObjectContainer container))
{
container.SetOutlineColor(Color.yellow);
}
}
index = 0;
movedAfterRun = false;
if (errors == null || errors.all.Count < 1)
{
ui.problemInfoText.text = "No problems found";
}
else
{
ui.problemInfoText.text = $"{errors.all.Count} problems found";
}
ui.problemInfoText.fontSize = 12;
ui.problemInfoText.GetComponent<RectTransform>().sizeDelta = new Vector2(190, 50);
//NextBlock(0);
}
catch (Exception e) { Debug.LogError(e.Message + e.StackTrace); }
}
public void NextBlock(int offset = 1)
{
if (!movedAfterRun)
{
movedAfterRun = true;
if (offset > 0) offset = 0;
}
if (errors == null || errors.all.Count < 1)
{
return;
}
index = (index + offset) % errors.all.Count;
if (index < 0)
{
index += errors.all.Count;
}
float? time = errors.all[index]?.note.Time;
if (time != null)
{
atsc.MoveToTimeInBeats(time ?? 0);
}
if (ui.problemInfoText != null)
{
ui.problemInfoText.text = errors.all[index]?.reason ?? "...";
ui.problemInfoText.fontSize = 12;
ui.problemInfoText.GetComponent<RectTransform>().sizeDelta = new Vector2(190, 50);
}
}
[ObjectLoaded]
private void ObjectLoaded(BeatmapObjectContainer container)
{
if (container.ObjectData == null || errors == null) return;
if (errors.errors.Any(it => it.note.Equals(container.ObjectData)))
{
container.SetOutlineColor(Color.red);
}
else if (errors.warnings.Any(it => it.note.Equals(container.ObjectData)))
{
container.SetOutlineColor(Color.yellow);
}
}
[Exit]
private void Exit()
{
}
}