-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugin.cs
571 lines (493 loc) · 23.5 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
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
using System.Collections.Generic;
namespace HaremPOV
{
[BepInPlugin(GUID, PluginName, Version)]
public class Plugin : BaseUnityPlugin
{
public const string GUID = "frostedashe.hm.harempov";
public const string PluginName = "HaremPOV";
public const string Version = "1.0.0";
private const string SECTION_GENERAL = "General";
private const string SECTION_HOTKEYS = "Keyboard shortcuts";
private const float FOV_DEFAULT = 70.0f;
private const float FOV_MIN = 20.0f;
private const float FOV_MAX = 120.0f;
private static ConfigEntry<float> DefaultFOV { get; set; }
private static ConfigEntry<float> MouseSensitivity { get; set; }
private static ConfigEntry<float> CameraSmoothing { get; set; }
private static ConfigEntry<bool> IncludeMalePOV { get; set; }
private static ConfigEntry<bool> IncludeFemalePOV { get; set; }
private static ConfigEntry<bool> UseCameraSmoothing { get; set; }
private static ConfigEntry<bool> UseMouseLookMode { get; set; }
private static ConfigEntry<KeyboardShortcut> POVHotkey { get; set; }
private static ConfigEntry<KeyboardShortcut> PrevCharHotkey { get; set; }
private static ConfigEntry<KeyboardShortcut> NextCharHotkey { get; set; }
private static ConfigEntry<KeyboardShortcut> ResetPOVHotkey { get; set; }
private static ConfigEntry<KeyboardShortcut> CameraSmoothingHotkey { get; set; }
private static ConfigEntry<KeyboardShortcut> MouseLookModeHotkey { get; set; }
private static ConfigEntry<KeyboardShortcut> CameraRollHotkey { get; set; }
private Harmony harmony;
private static ManualLogSource Log;
private static bool isPOVEnabled = false;
private static bool inSettingsScreen = false;
private static HScene hScene = null;
private static GameObject mainHPosition = null;
private static float mainHRotationBackup;
private static List<HScene.HInfo.Human> characters;
private static HScene.HInfo.Human currentChar;
private static GameObject currentCharHead;
private static int currentCharIndex = 0;
private static bool isBlueMan = false;
private static Vector3 cameraPositionBackup;
private static Quaternion cameraRotationBackup;
private static Dictionary<HScene.HInfo.Human, Quaternion> povHeadRotations;
private static Dictionary<HScene.HInfo.Human, float> povFOVBackups;
private static float cameraFovBackup;
private static float cameraNearClipBackup;
private static GameObject currentFemaleHairFront;
private static GameObject currentFemaleHairBack;
private static GameObject currentFemaleHairSide;
private static GameObject currentFemaleEyeline;
private static GameObject currentFemaleHairAcc;
private static GameObject currentFemaleEarrings;
private static float povCameraNearClipOffset = -0.075f;
private static Vector3 malePovCameraOffset = new Vector3(0, 0.05f, 0);
private static Vector3 femalePovCameraOffset = new Vector3(0, 0.065f, 0);
private static Vector3 povCameraOffset = malePovCameraOffset;
private bool wasPOVenabled = false;
private void Awake()
{
Log = base.Logger;
DefaultFOV = Config.Bind(SECTION_GENERAL, "Default FOV", FOV_DEFAULT, new ConfigDescription("Default Field of View in POV mode.", new AcceptableValueRange<float>(20f, 120f)));
MouseSensitivity = Config.Bind(SECTION_GENERAL, "Mouse Sensitivity", 3.0f, new ConfigDescription("Mouse look sensitivity.", new AcceptableValueRange<float>(0.1f, 5f)));
CameraSmoothing = Config.Bind(SECTION_GENERAL, "POV Camera Smoothing", 0.7f, new ConfigDescription("Amount of camera smoothing to apply in POV mode.", new AcceptableValueRange<float>(0.0f, 0.85f)));
UseMouseLookMode = Config.Bind(SECTION_GENERAL, "Use Mouse Look Mode", false, new ConfigDescription("Whether to use Mouse Look Mode, where the mouse moves the camera without having to click and drag."));
UseCameraSmoothing = Config.Bind(SECTION_GENERAL, "Use Camera Smoothing", false, new ConfigDescription("Whether to apply camera smoothing in POV mode."));
IncludeMalePOV = Config.Bind(SECTION_GENERAL, "Include Male POV", true, new ConfigDescription("Whether to include male characters when entering or switching POV."));
IncludeFemalePOV = Config.Bind(SECTION_GENERAL, "Include Female POV", true, new ConfigDescription("Whether to include female characters when entering or switching POV."));
POVHotkey = Config.Bind(SECTION_HOTKEYS, "Toggle POV", new KeyboardShortcut(KeyCode.F), new ConfigDescription("Enable or disable POV mode."));
PrevCharHotkey = Config.Bind(SECTION_HOTKEYS, "POV Previous Character", new KeyboardShortcut(KeyCode.C), new ConfigDescription("Switch to previous character's perspective when in POV mode."));
NextCharHotkey = Config.Bind(SECTION_HOTKEYS, "POV Next Character", new KeyboardShortcut(KeyCode.V), new ConfigDescription("Switch to next character's perspective when in POV mode."));
ResetPOVHotkey = Config.Bind(SECTION_HOTKEYS, "POV Reset", new KeyboardShortcut(KeyCode.LeftControl), new ConfigDescription("Reset camera rotation and FOV when in POV mode."));
CameraSmoothingHotkey = Config.Bind(SECTION_HOTKEYS, "Toggle Camera Smoothing", new KeyboardShortcut(KeyCode.G), new ConfigDescription("Enable or disable camera smoothing when in POV mode."));
MouseLookModeHotkey = Config.Bind(SECTION_HOTKEYS, "Toggle Mouse Look Mode", new KeyboardShortcut(KeyCode.Mouse2), new ConfigDescription("Enable or disable Mouse Look Mode."));
CameraRollHotkey = Config.Bind(SECTION_HOTKEYS, "Camera Roll Key", new KeyboardShortcut(KeyCode.LeftShift), new ConfigDescription("The camera will roll instead of looking around while this key is held down."));
characters = new List<HScene.HInfo.Human>();
povHeadRotations = new Dictionary<HScene.HInfo.Human, Quaternion>();
povFOVBackups = new Dictionary<HScene.HInfo.Human, float>();
OnHSceneStart();
IncludeMalePOV.SettingChanged +=
(object sender, System.EventArgs e) =>
{
FindCharacters();
};
IncludeFemalePOV.SettingChanged +=
(object sender, System.EventArgs e) =>
{
FindCharacters();
};
harmony = Harmony.CreateAndPatchAll(GetType());
}
private void OnDestroy()
{
harmony.UnpatchSelf();
if(isPOVEnabled)
{
if((currentChar != null) && (currentCharHead != null))
{
DisablePOV();
}
}
}
private void Update()
{
if((hScene == null) || inSettingsScreen)
{
return;
}
wasPOVenabled = isPOVEnabled;
if(POVHotkey.Value.IsDown())
{
if(!isPOVEnabled)
{
EnablePOV();
}
else
{
DisablePOV();
}
}
if(MouseLookModeHotkey.Value.IsDown())
{
if(isPOVEnabled)
{
UseMouseLookMode.Value = !UseMouseLookMode.Value;
if(UseMouseLookMode.Value)
{
Screen.lockCursor = true;
}
else
{
Screen.lockCursor = false;
}
}
else
{
EnablePOV();
if(isPOVEnabled)
{
UseMouseLookMode.Value = true;
Screen.lockCursor = true;
}
}
}
if(isPOVEnabled && UseMouseLookMode.Value && !Screen.lockCursor)
{
Screen.lockCursor = true;
}
if(mainHPosition.transform.rotation.eulerAngles.y != mainHRotationBackup)
{
UpdatePOVRotationBackups();
}
mainHRotationBackup = mainHPosition.transform.rotation.eulerAngles.y;
// if(isPOVEnabled != wasPOVenabled)
// {
// Log.LogMessage("POV toggled");
// }
if(isPOVEnabled)
{
UpdatePOVCamera();
}
}
private static void EnablePOV()
{
if(characters.Count == 0)
{
Log.LogMessage("Unable to enter POV mode: No available targets.");
return;
}
currentChar = characters[currentCharIndex];
if(!currentChar.isFemale)
{
isBlueMan = ((CharMale)(currentChar.chara)).GetBlueMan();
if(isBlueMan)
{
((CharMale)(currentChar.chara)).ChangeBlueMan(false);
}
}
currentCharHead = Traverse.Create(currentChar.chara).Field("objHead").GetValue<GameObject>();
cameraNearClipBackup = Camera.main.nearClipPlane;
cameraPositionBackup = Camera.main.transform.position;
cameraRotationBackup = Camera.main.transform.rotation;
cameraFovBackup = Camera.main.fieldOfView;
if(povFOVBackups.ContainsKey(currentChar))
{
Camera.main.fieldOfView = povFOVBackups[currentChar];
}
else
{
Camera.main.fieldOfView = DefaultFOV.Value;
}
povCameraOffset = malePovCameraOffset;
if(povHeadRotations.ContainsKey(currentChar))
{
currentCharHead.transform.rotation = povHeadRotations[currentChar];
}
Camera.main.transform.position = currentCharHead.transform.position;
Camera.main.transform.Translate(povCameraOffset);
Camera.main.transform.rotation = currentCharHead.transform.rotation;
Camera.main.nearClipPlane = cameraNearClipBackup + povCameraNearClipOffset;
SetObjectRenderersEnabled(currentCharHead, false);
if(currentChar.isFemale)
{
povCameraOffset = femalePovCameraOffset;
Traverse female = Traverse.Create(currentChar.female);
currentFemaleEyeline = female.Field("objEyeline").GetValue<GameObject>();
currentFemaleHairFront = female.Field("objHairF").GetValue<GameObject>();
currentFemaleHairBack = female.Field("objHairB").GetValue<GameObject>();
currentFemaleHairSide = female.Field("objHairS").GetValue<GameObject>();
// currentFemaleHairO = female.Field("objHairO").GetValue<GameObject>();
currentFemaleHairAcc = FindChildGameObject(currentChar.female.GetTopObj(), "cf_acs_cap");
currentFemaleEarrings = FindChildGameObject(currentChar.female.GetTopObj(), "cf_acs_earrings");
SetObjectRenderersEnabled(currentFemaleEyeline, false);
SetObjectRenderersEnabled(currentFemaleHairFront, false);
SetObjectRenderersEnabled(currentFemaleHairBack, false);
SetObjectRenderersEnabled(currentFemaleHairSide, false);
SetObjectRenderersEnabled(currentFemaleHairAcc, false);
SetObjectRenderersEnabled(currentFemaleEarrings, false);
}
isPOVEnabled = true;
}
private static void DisablePOV()
{
povFOVBackups[currentChar] = Camera.main.fieldOfView;
Camera.main.fieldOfView = cameraFovBackup;
Camera.main.nearClipPlane = cameraNearClipBackup;
Camera.main.transform.position = cameraPositionBackup;
Camera.main.transform.rotation = cameraRotationBackup;
povHeadRotations[currentChar] = currentCharHead.transform.rotation;
currentCharHead.transform.rotation = new Quaternion();
SetObjectRenderersEnabled(currentCharHead, true);
if(currentChar.isFemale)
{
SetObjectRenderersEnabled(currentFemaleEyeline, true);
SetObjectRenderersEnabled(currentFemaleHairFront, true);
SetObjectRenderersEnabled(currentFemaleHairBack, true);
SetObjectRenderersEnabled(currentFemaleHairSide, true);
SetObjectRenderersEnabled(currentFemaleHairAcc, true);
SetObjectRenderersEnabled(currentFemaleEarrings, true);
}
else if(isBlueMan)
{
((CharMale)(currentChar.chara)).ChangeBlueMan(true);
}
if(UseMouseLookMode.Value)
{
Screen.lockCursor = false;
}
isPOVEnabled = false;
}
private void UpdatePOVCamera()
{
if(characters.Count > 1)
{
if(NextCharHotkey.Value.IsDown() || ((Input.GetAxis("Mouse ScrollWheel") < 0) && UseMouseLookMode.Value))
{
DisablePOV();
currentCharIndex++;
if(currentCharIndex > characters.Count - 1)
{
currentCharIndex = 0;
}
EnablePOV();
}
else if(PrevCharHotkey.Value.IsDown() || ((Input.GetAxis("Mouse ScrollWheel") > 0) && UseMouseLookMode.Value))
{
DisablePOV();
currentCharIndex--;
if(currentCharIndex < 0)
{
currentCharIndex = characters.Count - 1;
}
EnablePOV();
}
}
if(CameraSmoothingHotkey.Value.IsDown())
{
UseCameraSmoothing.Value = !UseCameraSmoothing.Value;
}
if(ResetPOVHotkey.Value.IsDown() || (!Input.GetMouseButton(1) && Input.GetMouseButtonDown(0) && UseMouseLookMode.Value))
{
currentCharHead.transform.rotation = new Quaternion();
Camera.main.fieldOfView = DefaultFOV.Value;
}
Vector3 povHeadRotation = new Vector3();
float x = Input.GetAxis("Mouse X") * MouseSensitivity.Value;
float y = Input.GetAxis("Mouse Y") * MouseSensitivity.Value;
bool shouldLookAround = Input.GetMouseButton(0) || UseMouseLookMode.Value;
bool shouldChangeFOV = Input.GetMouseButton(1);
bool shouldRollCamera = (Input.GetKey(CameraRollHotkey.Value.MainKey) && (Input.GetMouseButton(0) || UseMouseLookMode.Value)) || (Input.GetMouseButton(0) && Input.GetMouseButton(1));
if(shouldRollCamera)
{
povHeadRotation = new Vector3(0f, 0f, -x);
}
else if(shouldChangeFOV)
{
Camera.main.fieldOfView += x;
Camera.main.fieldOfView = Mathf.Clamp(Camera.main.fieldOfView, FOV_MIN, FOV_MAX);
}
else if(shouldLookAround)
{
povHeadRotation = new Vector3(-y, x, 0f);
}
currentCharHead.transform.Rotate(povHeadRotation);
float hScale = mainHPosition.transform.localScale.x;
if(UseCameraSmoothing.Value)
{
float camSmoothing = (1.0f - CameraSmoothing.Value) * 10 * Time.deltaTime;
Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, currentCharHead.transform.position + (currentCharHead.transform.rotation * (povCameraOffset * hScale)), camSmoothing);
Camera.main.transform.rotation = Quaternion.Slerp(Camera.main.transform.rotation, currentCharHead.transform.rotation, camSmoothing);
}
else
{
Camera.main.transform.position = currentCharHead.transform.position;
Camera.main.transform.Translate((povCameraOffset * hScale));
Camera.main.transform.rotation = currentCharHead.transform.rotation;
}
}
private static void FindCharacters()
{
if(hScene == null)
{
return;
}
HScene.HInfo info = Traverse.Create(hScene).Field("info").GetValue<HScene.HInfo>();
List<HScene.HInfo.Human> males = new List<HScene.HInfo.Human>();
List<HScene.HInfo.Human> females = new List<HScene.HInfo.Human>();
characters.Clear();
foreach(HScene.HInfo.Human human in info.humanList)
{
if(human.isFemale)
{
females.Add(human);
}
else
{
males.Add(human);
}
}
if(IncludeMalePOV.Value)
{
foreach (HScene.HInfo.Human male in males)
{
characters.Add(male);
}
}
if(IncludeFemalePOV.Value)
{
foreach (HScene.HInfo.Human female in females)
{
characters.Add(female);
}
}
if(currentChar != null)
{
bool shouldResetCurrentChar = (!currentChar.isFemale && !IncludeMalePOV.Value) || (currentChar.isFemale && !IncludeFemalePOV.Value);
if(shouldResetCurrentChar)
{
currentCharIndex = 0;
if(isPOVEnabled)
{
DisablePOV();
EnablePOV();
}
}
}
}
private static void UpdatePOVRotationBackups()
{
float hRotation = mainHPosition.transform.rotation.eulerAngles.y - mainHRotationBackup;
Dictionary<HScene.HInfo.Human, Quaternion>.KeyCollection keys = povHeadRotations.Keys;
HScene.HInfo.Human[] savedChars = new HScene.HInfo.Human[keys.Count];
int i = 0;
foreach(HScene.HInfo.Human human in keys)
{
savedChars[i] = human;
i++;
}
for(i = 0; i < savedChars.Length; i++)
{
Vector3 currentRot = povHeadRotations[savedChars[i]].eulerAngles;
currentRot.y += hRotation;
povHeadRotations[savedChars[i]] = Quaternion.Euler(currentRot);
}
}
private static void SetObjectRenderersEnabled(GameObject go, bool enabled)
{
Renderer[] headRenderers = go.GetComponentsInChildren<Renderer>();
foreach(Renderer renderer in headRenderers)
{
renderer.enabled = enabled;
}
}
private static GameObject FindChildGameObject(GameObject parent, string name)
{
for (int i = 0; i < parent.transform.childCount; i++)
{
GameObject child = parent.transform.GetChild(i).gameObject;
if(child.name == name)
{
return child;
}
GameObject result = FindChildGameObject(child, name);
if(result)
{
return result;
}
}
return null;
}
[HarmonyPostfix, HarmonyPatch(typeof(HScene), "Start")]
private static void OnHSceneStart()
{
hScene = FindObjectOfType<HScene>();
if(hScene)
{
FindCharacters();
mainHPosition = Traverse.Create(hScene).Field("mainPosition").GetValue<GameObject>();
}
}
[HarmonyPostfix, HarmonyPatch(typeof(CheckScene), "JudgeProc")]
private static void OnHSceneEnd(CheckScene __instance, bool __result)
{
if(__result && __instance.IsYes && (BaseScene.overlapSceneType == BaseScene.Type.SceneEnd))
{
if(isPOVEnabled)
{
DisablePOV();
}
povHeadRotations.Clear();
povFOVBackups.Clear();
characters.Clear();
}
}
[HarmonyPrefix, HarmonyPatch(typeof(CameraControl), "LateUpdate")]
private static bool OnBaseCameraUpdate()
{
return !isPOVEnabled;
}
[HarmonyPostfix, HarmonyPatch(typeof(HScene), "ChangeAnimeMenue")]
private static void OnAnimationChange()
{
if(isPOVEnabled)
{
DisablePOV();
povHeadRotations.Clear();
povFOVBackups.Clear();
EnablePOV();
}
else
{
povHeadRotations.Clear();
povFOVBackups.Clear();
}
}
[HarmonyPostfix, HarmonyPatch(typeof(ConfigScene), "ChangeBefore")]
private static void OnConfigScreenEnter()
{
Screen.lockCursor = false;
inSettingsScreen = true;
}
[HarmonyPostfix, HarmonyPatch(typeof(ConfigScene), "ChangeAfter")]
private static void OnConfigScreenExit()
{
inSettingsScreen = false;
}
[HarmonyPostfix, HarmonyPatch(typeof(HSceneMenu), "SubMenu")]
private static void OnMapPointChangeAfter(HSceneMenu __instance)
{
if(!isPOVEnabled)
{
return;
}
HSceneMenu.E_SUB_MENU subMenu = Traverse.Create(__instance).Field("subMenu").GetValue<HSceneMenu.E_SUB_MENU>();
if((subMenu == HSceneMenu.E_SUB_MENU.POINT) || (subMenu == HSceneMenu.E_SUB_MENU.MAP))
{
YSUI_Select selCtrl = Traverse.Create(__instance).Field("selCtrl").GetValue<YSUI_Select>();
HSceneMenu.E_COL_ID selectIdFromGroup = (HSceneMenu.E_COL_ID)selCtrl.GetSelectIdFromGroup(4);
Dictionary<HSceneMenu.E_COL_ID, HSceneMenu.E_SP_ID> colSubPair = Traverse.Create(__instance).Field("colSubPair").GetValue<Dictionary<HSceneMenu.E_COL_ID, HSceneMenu.E_SP_ID>>();
if(colSubPair.ContainsKey(selectIdFromGroup))
{
Camera.main.transform.position = currentCharHead.transform.position;
Camera.main.transform.Translate(povCameraOffset);
Camera.main.transform.rotation = currentCharHead.transform.rotation;
}
}
}
}
}