-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModClass.cs
588 lines (566 loc) · 23.6 KB
/
ModClass.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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
using ButtplugKnight;
using GoodVibes;
using MagicUI.Core;
using Modding;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using UnityEngine;
namespace ButtplugMod
{
public class ButtplugMod : Mod, IMenuMod, ITogglableMod
{
private LayoutRoot? layout;
internal static ButtplugMod Instance;
static PlayerData player => PlayerData.instance;
const int retryAttempts = 10;
private string address = "localhost";
private int port = 12345;
private int secondsPerHit = 5;
private float baseVibeRate = 0.5f;
private bool doubleOnOverlap = true;
private bool scaleWithDamage = true;
private bool randomSurprises = true;
private bool buzzOnHeal = true;
private bool buzzOnDamage = true;
private int buzzOnStrike = 0; //off, not when full, always
private bool punctuateHits = false;
private bool vulnerableWhileVibing = false;
//UI options
private bool displayPercentage = false;
private bool displayTimeRemaining = false;
/*adding a new setting? Make sure to;
* add it to the menu options (ensuring you change the loader and saver)
* add it to the save settings method
* add it to the load settings method
*/
bool vulnerable => vulnerableWhileVibing && vibing;
static string hkData = "hollow_knight_Data";
string modPath => $@"{Environment.CurrentDirectory}\{hkData}\Managed\Mods\ButtplugKnight";
string logPath => $@"{modPath}\VibeLog.txt";
string settingsPath => $@"{modPath}\Settings.txt";
bool vibing => timeToReset > 0;
float currentPower;
float timeToReset;
float punctuateTimer;
PlugManager plug;
private void UpdateTextDisplay()
{
string text = "";
if (displayPercentage) text += $"{plug._currentPower*100:f0}%\n";
if (displayTimeRemaining && vibing) text += $"{timeToReset:f1}";
try
{
VibeUI.textUI.Text = text;
}
catch (NullReferenceException e)
{
Log($"Text UI not properly initialised: {e.Message}");
}
}
new public string GetName() => "Buttplug Knight";
public override string GetVersion() => "v1.2.6";
void LoadSettings()
{
try
{
string[] settingsRaw = File.ReadAllLines(settingsPath);
Dictionary<string, string> settings = new Dictionary<string, string>();
foreach (string setting in settingsRaw)
{
string[] parts = setting.Split('=');
settings.Add(parts[0], parts[1]);
}
secondsPerHit = int.Parse(settings[nameof(secondsPerHit)]);
baseVibeRate = float.Parse(settings[nameof(baseVibeRate)]);
doubleOnOverlap = bool.Parse(settings[nameof(doubleOnOverlap)]);
scaleWithDamage = bool.Parse(settings[nameof(scaleWithDamage)]);
randomSurprises = bool.Parse(settings[nameof(randomSurprises)]);
buzzOnHeal = bool.Parse(settings[nameof(buzzOnHeal)]);
buzzOnDamage = bool.Parse(settings[nameof(buzzOnDamage)]);
buzzOnStrike = int.Parse(settings[nameof(buzzOnStrike)]);
punctuateHits = bool.Parse(settings[nameof(punctuateHits)]);
vulnerableWhileVibing = bool.Parse(settings[nameof(vulnerableWhileVibing)]);
displayPercentage = bool.Parse(settings[nameof(displayPercentage)]);
displayTimeRemaining = bool.Parse(settings[nameof(displayTimeRemaining)]);
address = settings[nameof(address)];
port = int.Parse(settings[nameof(port)]);
}
catch (FileNotFoundException ex)
{
Log("No settings file found; using default settings.");
}
catch (Exception ex)
{
Log($"Failed to load settings. {ex.GetType()}; {ex.Message}");
}
}
void SaveSettings()
{
try
{
List<string> settings = new()
{
$"{nameof(secondsPerHit)}={secondsPerHit}",
$"{nameof(baseVibeRate)}={baseVibeRate}",
$"{nameof(doubleOnOverlap)}={doubleOnOverlap}",
$"{nameof(scaleWithDamage)}={scaleWithDamage}",
$"{nameof(randomSurprises)}={randomSurprises}",
$"{nameof(buzzOnHeal)}={buzzOnHeal}",
$"{nameof(buzzOnDamage)}={buzzOnDamage}",
$"{nameof(buzzOnStrike)}={buzzOnStrike}",
$"{nameof(punctuateHits)}={punctuateHits}",
$"{nameof(vulnerableWhileVibing)}={vulnerableWhileVibing}",
$"{nameof(displayPercentage)}={displayPercentage}",
$"{nameof(displayTimeRemaining)}={displayTimeRemaining}",
$"{nameof(address)}={address}",
$"{nameof(port)}={port}"
};
File.WriteAllLines(settingsPath, settings.ToArray());
}
catch (Exception ex)
{
Log($"Failed to save settings. {ex.GetType()}; {ex.Message}");
}
}
public override void Initialize(Dictionary<string, Dictionary<string, GameObject>> preloadedObjects)
{
Log("Initializing");
Instance = this;
currentPower = 0;
timeToReset = 1;
punctuateTimer = 0;
ModHooks.HeroUpdateHook += OnHeroUpdate;
ModHooks.BeforeAddHealthHook += BeforeHealthAdd;
ModHooks.AfterTakeDamageHook += OnHeroDamaged;
On.HeroController.Awake += OnSaveOpened;
ModHooks.SoulGainHook += OnSoulGain;
Regex pattern = new(@"[hH]ollow[\s_][kK]night[\s_][dD]ata");
foreach (string directory in Directory.EnumerateDirectories(Environment.CurrentDirectory))
{
Match match = pattern.Match(directory);
if (match.Success)
{
hkData = match.Value;
break;
}
}
LoadSettings();
if (!File.Exists(logPath)) File.Create(logPath).Close();
else if (plug == null) File.WriteAllLines(logPath, new string[0]);
if (plug == null) PlugSetup();
Log("Initialized");
}
private void OnSaveOpened(On.HeroController.orig_Awake orig, HeroController self)
{
if (layout == null)
{
layout = new(true, "Persistent layout");
layout.RenderDebugLayoutBounds = false;
// comment out examples as needed to see only the specific ones you want
VibeUI.Setup(layout);
}
orig(self);
}
private int OnSoulGain(int arg)
{
if (buzzOnStrike == 0) return arg;
if (buzzOnStrike == 1 && player.GetInt(nameof(player.MPCharge)) == player.GetInt(nameof(player.maxMP)))
{
if (player.GetInt(nameof(player.MPReserve)) == player.GetInt(nameof(player.MPReserveMax))) return arg;
if (BossSequenceController.BoundSoul) return arg;
}
DoGoodVibes(arg / 50f);
return arg;
}
private int BeforeHealthAdd(int arg)
{
if (buzzOnHeal) DoGoodVibes(arg * 0.2f);
return arg;
}
async void PlugSetup()
{
try
{
plug = new PlugManager()
{
Address = address,
Port = port,
RetryAmount = retryAttempts
};
plug.LogMessage += LogVibe;
await plug.Initialize();
Log($"Plug initialized, logging to {logPath}");
}
catch (Exception e)
{
Log($"Failed to initialize vibrator - {e.GetType()}: {e.Message}");
}
}
static System.Random rng = new System.Random();
private void OnHeroUpdate()
{
if (displayPercentage || displayTimeRemaining) UpdateTextDisplay();
if (randomSurprises && rng.Next(1, 1_000_000) == 69)
{
timeToReset += 10;
plug?.SetPowerLevel(1);
LogVibe("Random surprise triggered! Enjoy 10 seconds of max power :)");
}
if (punctuateHits && punctuateTimer > 0)
{
punctuateTimer -= Time.deltaTime;
if (punctuateTimer <= 0 && currentPower < 1)
{
plug?.SetPowerLevel(currentPower);
}
}
else if (vibing)
{
timeToReset -= Time.deltaTime;
if (!vibing)
{
plug?.SetPowerLevel(0);
currentPower = 0;
timeToReset = 0;
}
}
}
private int OnHeroDamaged(int hazardType, int damageAmount)
{
if (!buzzOnDamage) return damageAmount;
if (hazardType == 0) return damageAmount;
if (vulnerable) damageAmount *= 2;
DoGoodVibes(damageAmount);
if (punctuateHits)
{
if (currentPower < 1)
{
plug?.SetPowerLevel(1);
LogVibe("Hit punctuated by half a second of max power.");
}
punctuateTimer = 0.5f;
}
return damageAmount;
}
private void DoGoodVibes(float amount)
{
bool healTriggered = amount < 1;
string message = "";
//if took more than 1 damage
if (amount > 1)
{
if (!scaleWithDamage) amount = 1; //scale it to 1 if we're not doing that
else if (!vulnerable || amount > 2) message = "(heavy) "; //else mark it as a heavy hit
//you always take 2x damage while vulnerable.
//A heavy hit is defined by one that's normally 2+ damage.
}
//calculate new power level
float newPower = baseVibeRate * amount;
if (vulnerable && !healTriggered) newPower /= 2; //vulnerable is only supposed to effect the game, not the vibe
if (!healTriggered && player.GetBool(nameof(player.overcharmed)))
{
newPower *= 2; //for some reason the game doesn't report double damage from overcharming?
message = "(overcharmed) " + message;
}
if (doubleOnOverlap) newPower += currentPower;
else if (newPower < currentPower) newPower = currentPower;
newPower = Mathf.Clamp01(newPower);
//logging type of damage
if (healTriggered) message += $"Small vibe source ({amount})";
else message += $"Took {amount} damage"; //just for log message
if (vulnerable) message = "(vuln. 2x) " + message;
if (vibing && doubleOnOverlap)
{
message = "(overlap) " + message;
if (!vulnerable) amount *= 2; //apply doubling after so it only effects timer (since vibration is additive it's already accounted for)
}
//by default, dying in radiant mode triggers the game to deal 9999 damage to you
//this equates to almost 14 hours of vibe at default settings. I think it's fair to lower it.
if (amount > 10)
{
LogVibe($"Oof, radiant death? That'd normally be {secondsPerHit * amount} seconds. Lowering that a bit.");
amount = 10;
}
float seconds = secondsPerHit * amount;
timeToReset += seconds;
LogVibe($"{message}. Vibing at {100*newPower}% for {seconds} seconds. {timeToReset:f1}s left.");
if (currentPower == newPower) return;
currentPower = newPower;
//if we're punctuating hits, we don't need to update the power here, as it'll be done after.
//unless, of course, it's not caused by a hit (heal triggered) or it's already at 100%.
if (!punctuateHits || currentPower == 1 || healTriggered) plug?.SetPowerLevel(currentPower);
}
public void LogVibe(string s)
{
File.AppendAllText(logPath, $"[{DateTime.Now}] {s}\n");
}
public bool ToggleButtonInsideMenu => false;
public List<IMenuMod.MenuEntry> GetMenuData(IMenuMod.MenuEntry? toggleButtonEntry)
{
return new List<IMenuMod.MenuEntry>
{
new IMenuMod.MenuEntry {
Name = "Intensity",
Description = "The vibration level from 1 damage (50% recommended)",
Values = new string[] {
"5%",
"10%",
"20%",
"30%",
"40%",
"50%",
"75%",
"100%"
},
Saver = opt => {baseVibeRate = opt switch {
0 => 0.05f,
1 => 0.1f,
2 => 0.2f,
3 => 0.3f,
4 => 0.4f,
5 => 0.5f,
6 => 0.75f,
7 => 1f,
// This should never be called
_ => throw new InvalidOperationException()
}; SaveSettings(); },
Loader = () => baseVibeRate switch {
0.05f => 0,
0.1f => 1,
0.2f => 2,
0.3f => 3,
0.4f => 4,
0.5f => 5,
0.75f => 6,
1f => 7,
_ => 5
}
}, //Intensity
new IMenuMod.MenuEntry {
Name = "Seconds per hit",
Description = "The vibration duration from 1 damage",
Values = new string[] {
"1",
"2",
"3",
"4",
"5",
"10",
"20",
"69"
},
Saver = opt => {secondsPerHit = opt switch {
0 => 1,
1 => 2,
2 => 3,
3 => 4,
4 => 5,
5 => 10,
6 => 20,
7 => 69,
// This should never be called
_ => throw new InvalidOperationException()
}; SaveSettings(); },
Loader = () => secondsPerHit switch {
1 => 0,
2 => 1,
3 => 2,
4 => 3,
5 => 4,
10 => 5,
20 => 6,
69 => 7,
_ => 4
}
}, //seconds per hit
new IMenuMod.MenuEntry {
Name = "Buzz on damage",
Description = "Taking damage causes vibrations - the point of the mod.",
Values = new string[] {
"On",
"Off"
},
Saver = opt => {buzzOnDamage = opt switch {
0 => true,
1 => false,
// This should never be called
_ => throw new InvalidOperationException()
}; SaveSettings(); },
Loader = () => buzzOnDamage switch {
true => 0,
false => 1,
}
}, //buzz on damage
new IMenuMod.MenuEntry {
Name = "Buzz when healing",
Description = "Healing will cause a small short vibration",
Values = new string[] {
"On",
"Off"
},
Saver = opt => {buzzOnHeal = opt switch {
0 => true,
1 => false,
// This should never be called
_ => throw new InvalidOperationException()
}; SaveSettings(); },
Loader = () => buzzOnHeal switch {
true => 0,
false => 1,
}
}, //buzz when healing
new IMenuMod.MenuEntry {
Name = "Buzz on soul gain",
Description = "Extracting white fluids from enemies will cause a small vibration",
Values = new string[] {
"Off",
"Not when full",
"Always"
},
Saver = opt => {buzzOnStrike = opt; SaveSettings(); },
Loader = () => buzzOnStrike
}, //buzz on soul gain
new IMenuMod.MenuEntry {
Name = "Scale with damage",
Description = "Heavier hits will cause longer vibrations",
Values = new string[] {
"On",
"Off"
},
Saver = opt => {scaleWithDamage = opt switch {
0 => true,
1 => false,
// This should never be called
_ => throw new InvalidOperationException()
}; SaveSettings(); },
Loader = () => scaleWithDamage switch {
true => 0,
false => 1,
}
}, //scale with damage
new IMenuMod.MenuEntry {
Name = "Double on overlap",
Description = "Vibration commands can stack",
Values = new string[] {
"On",
"Off"
},
Saver = opt => {doubleOnOverlap = opt switch {
0 => true,
1 => false,
// This should never be called
_ => throw new InvalidOperationException()
}; SaveSettings(); },
Loader = () => doubleOnOverlap switch {
true => 0,
false => 1,
}
}, //double on overlap
new IMenuMod.MenuEntry {
Name = "Random surprises",
Description = "Every frame has a 1 in 1,000,000 chance of fun surprises",
Values = new string[] {
"On",
"Off"
},
Saver = opt => {randomSurprises = opt switch {
0 => true,
1 => false,
// This should never be called
_ => throw new InvalidOperationException()
}; SaveSettings(); },
Loader = () => randomSurprises switch {
true => 0,
false => 1,
}
}, //random surprises
new IMenuMod.MenuEntry {
Name = "Vulnerable vibing",
Description = "While vibing, the knight takes double damage.",
Values = new string[] {
"On",
"Off"
},
Saver = opt => {vulnerableWhileVibing = opt switch {
0 => true,
1 => false,
// This should never be called
_ => throw new InvalidOperationException()
}; SaveSettings(); },
Loader = () => vulnerableWhileVibing switch {
true => 0,
false => 1,
}
}, //Vulnerable when vibing
new IMenuMod.MenuEntry {
Name = "Punctuate hits",
Description = "Hits cause half a second of max power vibe, followed by regular",
Values = new string[] {
"On",
"Off"
},
Saver = opt => {punctuateHits = opt switch {
0 => true,
1 => false,
// This should never be called
_ => throw new InvalidOperationException()
}; SaveSettings(); },
Loader = () => punctuateHits switch {
true => 0,
false => 1,
}
}, //punctuate hits
new IMenuMod.MenuEntry {
Name = "Display Intensity",
Description = "Display the intensity percentage in the top right on the UI",
Values = new string[] {
"On",
"Off"
},
Saver = opt => {displayPercentage = opt switch {
0 => true,
1 => false,
// This should never be called
_ => throw new InvalidOperationException()
}; SaveSettings(); },
Loader = () => displayPercentage switch {
true => 0,
false => 1,
}
}, //Display Intensity
new IMenuMod.MenuEntry {
Name = "Display Timer",
Description = "Display the amount of time left before the vibrator turns off",
Values = new string[] {
"On",
"Off"
},
Saver = opt => {displayTimeRemaining = opt switch {
0 => true,
1 => false,
// This should never be called
_ => throw new InvalidOperationException()
}; SaveSettings(); },
Loader = () => displayTimeRemaining switch {
true => 0,
false => 1,
}
} //Display Intensity
//max line length |----------------------------------------------------------------|
};
}
public void Unload()
{
plug?.SetPowerLevel(0);
ModHooks.HeroUpdateHook -= OnHeroUpdate;
ModHooks.BeforeAddHealthHook -= BeforeHealthAdd;
ModHooks.AfterTakeDamageHook -= OnHeroDamaged;
ModHooks.SoulGainHook -= OnSoulGain;
On.HeroController.Awake -= OnSaveOpened;
}
}
}