forked from nalulululuna/NalulunaModifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NalulunaModifierController.cs
422 lines (361 loc) · 16.6 KB
/
NalulunaModifierController.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
using BS_Utils.Gameplay;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
using Xft;
namespace NalulunaModifier
{
public class NalulunaModifierController : MonoBehaviour
{
enum AvatarType { None, VMCAvatar, CustomAvatar }
public static NalulunaModifierController instance { get; private set; }
private AudioTimeSyncController _audioTimeSyncController;
private AudioSource _audioSource;
private PlayerController _playerController;
private Transform _rightSaberTransform;
private Transform _leftSaberTransform;
private SaberClashChecker _saberClashChecker;
private SaberBurnMarkArea _saberBurnMarkArea;
private SaberBurnMarkSparkles _saberBurnMarkSparkles;
private BeatmapObjectSpawnController _beatmapObjectSpawnController;
private GameEnergyCounter _gameEnergyCounter;
private GamePause _gamePause;
private Transform _footL;
private Transform _footR;
private AvatarType _avatarType;
private bool _init;
private float _originalTimeScale;
private void Awake()
{
if (instance != null)
{
Logger.log?.Warn($"Instance of {this.GetType().Name} already exists, destroying.");
GameObject.DestroyImmediate(this);
return;
}
GameObject.DontDestroyOnLoad(this); // Don't destroy this object on scene changes
instance = this;
Logger.log?.Debug($"{name}: Awake()");
SceneManager.activeSceneChanged += OnActiveSceneChanged;
}
public void OnActiveSceneChanged(Scene oldScene, Scene newScene)
{
_init = false;
if (newScene.name == "GameCore")
{
Config.Read();
if (Config.parabola || Config.noBlue || Config.noRed || Config.redToBlue || Config.blueToRed || Config.centering ||
Config.feet || Config.noDirection || Config.flatNotes || Config.superhot || Config.vacuum)
{
ScoreSubmission.DisableSubmission(Plugin.Name);
}
StartCoroutine(OnGameCoreCoroutine());
}
}
public void OnPauseResume()
{
UpdateSaberActive();
}
private void UpdateSaberActive()
{
if (!(Config.blueToRed || !(Config.noRed || Config.redToBlue)))
{
_playerController.leftSaber.gameObject.SetActive(false);
}
if (!(Config.redToBlue || !(Config.noBlue || Config.blueToRed)))
{
_playerController.rightSaber.gameObject.SetActive(false);
}
if (Config.vacuum)
{
_playerController.leftSaber.gameObject.SetActive(false);
}
}
private void EnumChildren(GameObject gameObject, string indent)
{
Logger.log.Debug(indent + gameObject.name);
foreach (Transform transform in gameObject.GetComponentInChildren<Transform>())
{
EnumChildren(transform.gameObject, indent + " ");
}
}
private IEnumerator EnumAllObjectsCoroutine()
{
foreach (GameObject g in Array.FindAll(FindObjectsOfType<GameObject>(), x => x.transform.parent == null))
{
EnumChildren(g, "");
yield return null;
}
}
private IEnumerator OnGameCoreCoroutine()
{
if (_audioTimeSyncController == null)
{
_audioTimeSyncController = Resources.FindObjectsOfTypeAll<AudioTimeSyncController>().FirstOrDefault();
_audioSource = _audioTimeSyncController.GetPrivateField<AudioSource>("_audioSource");
}
_originalTimeScale = _audioTimeSyncController.timeScale;
if (_gamePause == null)
_gamePause = Resources.FindObjectsOfTypeAll<GamePause>().FirstOrDefault();
_gamePause.didResumeEvent += OnPauseResume;
// wait for CustomSaber mod
yield return new WaitUntil(() => Resources.FindObjectsOfTypeAll<Saber>().Any());
yield return new WaitForSecondsRealtime(0.1f);
if (_playerController == null)
_playerController = Resources.FindObjectsOfTypeAll<PlayerController>().FirstOrDefault();
if (_rightSaberTransform == null)
_rightSaberTransform = _playerController.rightSaber.transform;
if (_leftSaberTransform == null)
_leftSaberTransform = _playerController.leftSaber.transform;
if (_saberClashChecker == null)
_saberClashChecker = Resources.FindObjectsOfTypeAll<SaberClashChecker>().FirstOrDefault();
if (_saberBurnMarkArea == null)
_saberBurnMarkArea = Resources.FindObjectsOfTypeAll<SaberBurnMarkArea>().FirstOrDefault();
if (_saberBurnMarkSparkles == null)
_saberBurnMarkSparkles = Resources.FindObjectsOfTypeAll<SaberBurnMarkSparkles>().FirstOrDefault();
// need some wait to GetNoteOffset
if (Config.centering)
{
if (_beatmapObjectSpawnController == null)
_beatmapObjectSpawnController = Resources.FindObjectsOfTypeAll<BeatmapObjectSpawnController>().FirstOrDefault();
BeatmapObjectSpawnMovementData beatmapObjectSpawnMovementData = _beatmapObjectSpawnController.GetPrivateField<BeatmapObjectSpawnMovementData>("_beatmapObjectSpawnMovementData");
Vector3 leftBase = beatmapObjectSpawnMovementData.GetNoteOffset(0, NoteLineLayer.Base);
Vector3 rightTop = beatmapObjectSpawnMovementData.GetNoteOffset(3, NoteLineLayer.Top);
NoteJumpManualUpdate.center = (leftBase + rightTop) / 2;
//Logger.log.Debug($"leftBase={leftBase.x}, {leftBase.y}, {leftBase.z}");
//Logger.log.Debug($"rightTop={rightTop.x}, {rightTop.y}, {rightTop.z}");
}
if (Config.hideSabers)
{
SetSaberVisible(_playerController.rightSaber, false);
SetSaberVisible(_playerController.leftSaber, false);
SetTrailWidth(0f);
_saberClashChecker.enabled = false;
_saberBurnMarkArea.enabled = false;
_saberBurnMarkSparkles.enabled = false;
}
if (Config.hideSaberEffects)
{
SetTrailWidth(0f);
_saberClashChecker.enabled = false;
_saberBurnMarkArea.enabled = false;
_saberBurnMarkSparkles.enabled = false;
}
if (Config.boxing)
{
SetTrailWidth(0.05f);
}
if (Config.headbang)
{
SetTrailWidth(0f);
}
if (Config.vacuum)
{
if (_gameEnergyCounter == null)
_gameEnergyCounter = Resources.FindObjectsOfTypeAll<GameEnergyCounter>().FirstOrDefault();
_gameEnergyCounter.SetPrivateField("_hitBombEnergyDrain", 0f);
}
if (GameObject.Find("vacuum_saber_right"))
{
SetTrailWidth(0f);
}
if (Config.feetAvatar)
{
StartCoroutine(FindAvatarCoroutine());
}
UpdateSaberActive();
_init = true;
}
private IEnumerator FindAvatarCoroutine()
{
// need some wait
for (int i = 0; i < 3; i++)
{
Logger.log.Debug("VRM Search");
GameObject vrm = GameObject.Find("VMCReceiver/VRM");
if (vrm == null)
{
Logger.log.Debug("VRM Search2");
vrm = GameObject.Find("VRM");
}
if (vrm != null)
{
Logger.log.Debug("VRM Found");
_footL = vrm.GetComponent<Animator>()?.GetBoneTransform(HumanBodyBones.LeftFoot)?.transform;
_footR = vrm.GetComponent<Animator>()?.GetBoneTransform(HumanBodyBones.RightFoot)?.transform;
if (_footL != null && _footR != null)
{
_avatarType = AvatarType.VMCAvatar;
}
break;
}
else
{
Logger.log.Debug("VRM NotFound");
Logger.log.Debug("CustomAvatar Search");
GameObject customAvatar = GameObject.Find("_CustomAvatar(Clone)");
if (customAvatar != null)
{
Logger.log.Debug("CustomAvatar Found");
_footL = customAvatar.GetComponentInChildren<Animator>()?.GetBoneTransform(HumanBodyBones.LeftFoot)?.transform;
_footR = customAvatar.GetComponentInChildren<Animator>()?.GetBoneTransform(HumanBodyBones.RightFoot)?.transform;
if (_footL != null && _footR != null)
{
_avatarType = AvatarType.CustomAvatar;
}
break;
}
else
{
Logger.log.Debug("CustomAvatar NotFound");
}
}
yield return new WaitForSecondsRealtime(0.1f);
}
if (_footL != null && _footR != null)
{
Logger.log.Debug($"FootL: {_footL.position.x}, {_footL.position.y}, {_footL.position.z}");
Logger.log.Debug($"FootR: {_footR.position.x}, {_footR.position.y}, {_footR.position.z}");
SetTrailWidth(0.25f);
SetSaberVisible(_playerController.rightSaber, false);
SetSaberVisible(_playerController.leftSaber, false);
}
else
{
Logger.log.Debug($"Foot NotFound");
_avatarType = AvatarType.None;
// for debug
//StartCoroutine(EnumAllObjectsCoroutine());
}
}
private void Start()
{
Logger.log?.Debug($"{name}: Start()");
}
private Vector3 _prevHeadPos;
private Vector3 _prevLeftHandlePos;
private Vector3 _prevRightHandlePos;
private void Update()
{
if (!_init)
{
return;
}
if (Config.boxing)
{
_rightSaberTransform.Translate(0, 0, -0.23f, Space.Self);
_leftSaberTransform.Translate(0, 0, -0.23f, Space.Self);
_rightSaberTransform.localScale = new Vector3(4, 4, 0.25f);
_leftSaberTransform.localScale = new Vector3(4, 4, 0.25f);
}
if (Config.headbang)
{
_rightSaberTransform.rotation = _playerController.headRot;
_leftSaberTransform.rotation = _playerController.headRot;
_rightSaberTransform.Rotate(new Vector3(270, 0, 0));
_leftSaberTransform.Rotate(new Vector3(270, 0, 0));
_rightSaberTransform.position = _playerController.headPos;
_leftSaberTransform.position = _playerController.headPos;
_rightSaberTransform.Translate(0.05f, 0, -0.2f, Space.Self);
_leftSaberTransform.Translate(-0.05f, 0, -0.2f, Space.Self);
_rightSaberTransform.localScale = new Vector3(2, 2, 0.5f);
_leftSaberTransform.localScale = new Vector3(2, 2, 0.5f);
}
if (Config.feetAvatar)
{
if ((_footR != null) && (_footL != null))
{
_rightSaberTransform.rotation = _footR.rotation;
_leftSaberTransform.rotation = _footL.rotation;
if (_avatarType == AvatarType.VMCAvatar)
{
_rightSaberTransform.position = _footR.position + new Vector3(Config.vmcAvatarFootPosX, Config.vmcAvatarFootPosY, Config.vmcAvatarFootPosZ);
_leftSaberTransform.position = _footL.position + new Vector3(-1 * Config.vmcAvatarFootPosX, Config.vmcAvatarFootPosY, Config.vmcAvatarFootPosZ);
_rightSaberTransform.Rotate(new Vector3(Config.vmcAvatarFootRotX, Config.vmcAvatarFootRotY, Config.vmcAvatarFootRotZ));
_leftSaberTransform.Rotate(new Vector3(Config.vmcAvatarFootRotX, -1 * Config.vmcAvatarFootRotY, -1 * Config.vmcAvatarFootRotZ));
}
else if (_avatarType == AvatarType.CustomAvatar)
{
_rightSaberTransform.position = _footR.position + new Vector3(Config.customAvatarFootPosX, Config.customAvatarFootPosY, Config.customAvatarFootPosZ);
_leftSaberTransform.position = _footL.position + new Vector3(-1 * Config.customAvatarFootPosX, Config.customAvatarFootPosY, Config.customAvatarFootPosZ);
_rightSaberTransform.Rotate(new Vector3(Config.customAvatarFootRotX, Config.customAvatarFootRotY, Config.customAvatarFootRotZ));
_leftSaberTransform.Rotate(new Vector3(Config.customAvatarFootRotX, -1 * Config.customAvatarFootRotY, -1 * Config.customAvatarFootRotZ));
}
_rightSaberTransform.localScale = new Vector3(2, 2, 0.25f);
_leftSaberTransform.localScale = new Vector3(2, 2, 0.25f);
}
}
if (Config.superhot)
{
float distance = 0;// = Vector3.Distance(_playerController.headPos, prevHeadPos);
distance = Mathf.Max(distance, Vector3.Distance(_playerController.leftSaber.handlePos, _prevLeftHandlePos));
distance = Mathf.Max(distance, Vector3.Distance(_playerController.rightSaber.handlePos, _prevRightHandlePos));
distance = Mathf.Clamp01(distance * 50f);
SetTimeScale(distance);
_prevHeadPos = _playerController.headPos;
_prevLeftHandlePos = _playerController.leftSaber.handlePos;
_prevRightHandlePos = _playerController.rightSaber.handlePos;
}
}
private void OnDestroy()
{
SceneManager.activeSceneChanged -= OnActiveSceneChanged;
Logger.log?.Debug($"{name}: OnDestroy()");
instance = null;
}
private void SetSaberVisible(Saber saber, bool active)
{
// not hide Skinned Mesh Renderer
// (There is a customsaber(shoes) for FeetSaber using this loophole.)
IEnumerable<MeshFilter> meshFilters = saber.transform.GetComponentsInChildren<MeshFilter>();
foreach (MeshFilter meshFilter in meshFilters)
{
meshFilter.gameObject.SetActive(active);
MeshFilter filter = meshFilter.GetComponentInChildren<MeshFilter>();
if (filter != null)
{
filter.gameObject.SetActive(active);
}
}
/* hide Skinned Mesh Renderer
IEnumerable<Renderer> renders = saber.transform.GetComponentsInChildren<Renderer>();
foreach (Renderer render in renders)
{
render.gameObject.SetActive(active);
Renderer r = render.GetComponentInChildren<Renderer>();
if (r != null)
{
r.gameObject.SetActive(active);
}
}
*/
}
private void SetTrailWidth(float trailWidth)
{
IEnumerable<XWeaponTrail> trails = Resources.FindObjectsOfTypeAll<XWeaponTrail>();
foreach (XWeaponTrail trail in trails)
{
trail.SetPrivateField("_trailWidth", trailWidth);
}
}
private IEnumerator SetTimeScaleCoroutine(float timeScale)
{
float amount = 0;
float currentTimeScale = _audioTimeSyncController.timeScale;
while (amount < 1)
{
SetTimeScale(Mathf.Lerp(currentTimeScale, timeScale, amount));
amount += 0.1f;
yield return new WaitForFixedUpdate();
}
}
private void SetTimeScale(float timeScale)
{
_audioTimeSyncController.SetPrivateField("_timeScale", timeScale);
_audioSource.pitch = timeScale;
}
}
}