-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerSkills.cs
More file actions
621 lines (531 loc) · 23.2 KB
/
PlayerSkills.cs
File metadata and controls
621 lines (531 loc) · 23.2 KB
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using HarmonyLib;
using UnityEngine;
using Object = UnityEngine.Object;
namespace DarkwoodCustomizer
{
internal class PlayerSkillsPatch
{
public static bool RefreshSkills = true;
private static bool _skillsAlreadyApplied;
// Complete dictionary of all skills from the wiki with correct game object names
private static readonly Dictionary<string, SkillInfo> AllSkills = new()
{
// Positive Skills
{ "EagleEye", new SkillInfo("farSight", "Passive", "Positive I") },
{ "Moth", new SkillInfo("moth", "Active", "Positive I") },
{ "Navigator", new SkillInfo("navigator", "Active", "Positive I") },
{ "MushroomHealing", new SkillInfo("mushroomHealing", "Passive", "Positive I") },
{ "AcidBlood", new SkillInfo("acidBlood", "Passive", "Positive II") },
{ "ThirdEye", new SkillInfo("thirdEye", "Active", "Positive II") },
{ "Runner", new SkillInfo("runner", "Active", "Positive II") },
{ "CarefulStep", new SkillInfo("carefulStep", "Passive", "Positive III") },
{ "Appetite", new SkillInfo("panda", "Passive", "Positive III") },
{ "Scream", new SkillInfo("scaryFace", "Active", "Positive III") },
{ "Adrenaline", new SkillInfo("strong", "Passive", "Positive IV") },
{ "Vitality", new SkillInfo("moreHealth", "Passive", "Positive IV") },
{ "Chameleon", new SkillInfo("ninja", "Active", "Positive IV") },
// Negative Skills
{ "Shadows", new SkillInfo("nightShadows", "Passive", "Negative I") },
{ "PoisonVulnerability", new SkillInfo("poisonVulnerability", "Passive", "Negative II") },
{ "Fearful", new SkillInfo("narrowerFOV", "Passive", "Negative II") },
{ "WeakLungs", new SkillInfo("lessStamina", "Passive", "Negative III") },
{ "Weakness", new SkillInfo("weak", "Passive", "Negative III") },
{ "WeakRegeneration", new SkillInfo("weakHealthRegeneration", "Passive", "Negative IV") },
{ "ShakyHands", new SkillInfo("shakyHands", "Passive", "Negative IV") }
};
// Skills that are in the game files but not mapped (for logging)
private static readonly List<string> UnmappedGameObjectSkills =
[
"badNavigator",
"clumsy",
"cook",
"dodge",
"electrician",
"enemyOfTheForest",
"flatfoot",
"friendOfTheForest",
"hotbarPlus",
"inventoryPlus",
"lessHealth",
"moreStamina",
"perception1",
"randomBleeding",
"slow",
"specialAttack",
"vigilant",
"widerFOV"
];
// Reverse mapping for easier lookup
private static readonly Dictionary<string, string> GameObjectToConfigKey = new();
static PlayerSkillsPatch()
{
// Build reverse mapping
foreach (var kvp in AllSkills.Where(kvp => !string.IsNullOrEmpty(kvp.Value.GameObjectName) && !GameObjectToConfigKey.ContainsKey(kvp.Value.GameObjectName)))
{
GameObjectToConfigKey[kvp.Value.GameObjectName] = kvp.Key;
}
}
// Main Patch Methods - Apply All Skill States
[HarmonyPatch(typeof(PlayerSkills), "initialize")]
[HarmonyPostfix]
// ReSharper disable once InconsistentNaming
private static void InitializeSkills(PlayerSkills __instance, bool resetTimesUsed = true)
{
if (!Plugin.PlayerSkillsModification.Value) return;
// Prevent running multiple times
if (_skillsAlreadyApplied) return;
ApplyAllSkillStates(__instance);
RefreshSkills = false;
_skillsAlreadyApplied = true;
}
[HarmonyPatch(typeof(PlayerSkills), "Start")]
[HarmonyPostfix]
// ReSharper disable once InconsistentNaming
private static void StartSkills(PlayerSkills __instance)
{
if (!Plugin.PlayerSkillsModification.Value) return;
// Reset the flag when starting a new game
_skillsAlreadyApplied = false;
}
// Apply All Skill States
private static void ApplyAllSkillStates(PlayerSkills skills)
{
if (!Plugin.PlayerSkillsModification.Value) return;
// Log all skills found in Resources
LogAllAvailableSkills();
// Clear and rebuild all skill lists
RebuildSkillLists(skills);
// Update UI and visual effects
if (Player.Instance != null)
{
Player.Instance.updateHealthBar();
Singleton<UI>.Instance.refreshLives();
}
// Log final skill states
LogFinalSkillStates(skills);
}
private static void LogAllAvailableSkills()
{
var allSkills = Resources.LoadAll("Skills", typeof(GameObject));
Plugin.Log.LogInfo("[Skills] All skills found in Resources:");
foreach (var skillObj in allSkills)
{
if (skillObj is not GameObject skillGameObject) continue;
var skill = skillGameObject.GetComponent<PlayerSkill>();
if (skill == null) continue;
var configKey = GetConfigKeyFromGameObjectName(skillGameObject.name);
var status = string.IsNullOrEmpty(configKey) ? "UNMAPPED" : "MAPPED";
Plugin.Log.LogInfo($" - {skillGameObject.name}: " +
$"Manual={skill.isActivatedManually}, " +
$"Status={status}, " +
$"Config={configKey ?? "N/A"}");
}
}
private static void RebuildSkillLists(PlayerSkills skills)
{
// Get all available skills from the game
var allSkills = Resources.LoadAll("Skills", typeof(GameObject));
// Track which skills we've already processed
var processedGameObjects = new HashSet<string>();
// Clear lists if empty
if (skills.skills.Count == 0)
{
skills.skills.Clear();
skills.manuallyActivatedSkills.Clear();
skills.automaticActivatedSkills.Clear();
skills.availableSkills.Clear();
}
// Add skills from Resources
foreach (var skillObj in allSkills)
{
if (skillObj is not GameObject skillGameObject) continue;
var skill = skillGameObject.GetComponent<PlayerSkill>();
if (skill == null) continue;
var gameObjectName = skillGameObject.name;
// Skip if already processed
if (!processedGameObjects.Add(gameObjectName))
continue;
var configKey = GetConfigKeyFromGameObjectName(gameObjectName);
if (string.IsNullOrEmpty(configKey))
{
// Skill not in our config map, skip it
continue;
}
// Check if skill should be enabled
var shouldBeEnabled = ShouldSkillBeEnabled(configKey);
if (shouldBeEnabled)
{
// Create a new instance of the skill
var skillInstance = CreateSkillInstance(skillGameObject, gameObjectName);
if (skillInstance == null) continue;
AddSkillToLists(skills, skillInstance);
// Update boolean fields for skill effects
UpdateSkillBooleanField(skills, configKey, true);
}
else
{
// Skill is not enabled, set boolean field to false
UpdateSkillBooleanField(skills, configKey, false);
}
}
// Update player stats for skills that affect them
UpdatePlayerStats(skills);
}
private static PlayerSkill CreateSkillInstance(GameObject skillPrefab, string skillName)
{
// Create a new instance
var skillInstance = Object.Instantiate(skillPrefab);
skillInstance.name = skillName;
skillInstance.SetActive(true);
return skillInstance.GetComponent<PlayerSkill>();
}
private static void AddSkillToLists(PlayerSkills skills, PlayerSkill skill)
{
if (skill == null) return;
var skillName = skill.gameObject.name;
// Check if skill already exists
if (skills.skills.Any(s => s != null && s.gameObject != null && s.gameObject.name == skillName))
{
if (Plugin.LogDebug.Value)
{
Plugin.Log.LogInfo($"[Skills] Skipping duplicate skill: {skillName}");
}
return;
}
// Add to main skills list
skills.skills.Add(skill);
// Add to available skills
skills.availableSkills.Add(skill);
// Add to appropriate activation list
if (skill.isActivatedManually)
{
skills.manuallyActivatedSkills.Add(skill);
}
else
{
skills.automaticActivatedSkills.Add(skill);
}
// Initialize the skill
skill.chosen = true;
skill.initialized = true;
skill.timesUsed = 0;
}
private static string GetConfigKeyFromGameObjectName(string gameObjectName)
{
return GameObjectToConfigKey.TryGetValue(gameObjectName, out var configKey) ? configKey : null;
}
private static bool ShouldSkillBeEnabled(string configKey)
{
return GetEnableConfigValue(configKey);
}
private static bool GetEnableConfigValue(string configKey)
{
if (!AllSkills.ContainsKey(configKey))
return false;
return configKey switch
{
// Positive Skills
"EagleEye" => Plugin.PlayerSkillEagleEye.Value,
"Moth" => Plugin.PlayerSkillMoth.Value,
"Navigator" => Plugin.PlayerSkillNavigator.Value,
"MushroomHealing" => Plugin.PlayerSkillMushroomHealing.Value,
"AcidBlood" => Plugin.PlayerSkillAcidBlood.Value,
"ThirdEye" => Plugin.PlayerSkillThirdEye.Value,
"Runner" => Plugin.PlayerSkillRunner.Value,
"CarefulStep" => Plugin.PlayerSkillCarefulStep.Value,
"Appetite" => Plugin.PlayerSkillAppetite.Value,
"Scream" => Plugin.PlayerSkillScream.Value,
"Adrenaline" => Plugin.PlayerSkillAdrenaline.Value,
"Vitality" => Plugin.PlayerSkillVitality.Value,
"Chameleon" => Plugin.PlayerSkillChameleon.Value,
// Negative Skills
"Shadows" => Plugin.PlayerSkillShadows.Value,
"PoisonVulnerability" => Plugin.PlayerSkillPoisonVulnerability.Value,
"Fearful" => Plugin.PlayerSkillFearful.Value,
"WeakLungs" => Plugin.PlayerSkillWeakLungs.Value,
"Weakness" => Plugin.PlayerSkillWeakness.Value,
"WeakRegeneration" => Plugin.PlayerSkillWeakRegeneration.Value,
"ShakyHands" => Plugin.PlayerSkillShakyHands.Value,
_ => false
};
}
private static void UpdateSkillBooleanField(PlayerSkills skills, string configKey, bool value)
{
if (!AllSkills.TryGetValue(configKey, out var skillInfo))
return;
var fieldName = skillInfo.GameObjectName;
// Special cases for skills with different field names
switch (fieldName)
{
case "nightShadows":
fieldName = "NightShadows";
break;
case "farSight":
fieldName = "farsight";
break;
case "moreHealth":
fieldName = "moreHealth1";
break;
case "lessStamina":
fieldName = "lessStamina1";
break;
case "strong":
fieldName = "strong";
break;
case "narrowerFOV":
fieldName = "narrowerFOV";
break;
case "panda":
case "scaryFace":
case "ninja":
// These skills don't have boolean fields in PlayerSkills
return;
}
// Use reflection to set the boolean field
var field = typeof(PlayerSkills).GetField(fieldName,
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
if (field != null && field.FieldType == typeof(bool))
{
field.SetValue(skills, value);
}
else if (Plugin.LogDebug.Value)
{
Plugin.Log.LogInfo($"[Skills] No boolean field found for {configKey} -> {fieldName}");
}
}
private static void UpdatePlayerStats(PlayerSkills skills)
{
if (Player.Instance == null) return;
try
{
// Update boolean properties for skills that have them
if (Plugin.PlayerSkillEagleEye.Value)
{
skills.Farsight = true;
}
if (Plugin.PlayerSkillVitality.Value)
{
skills.MoreHealth1 = true;
}
if (Plugin.PlayerSkillThirdEye.Value)
{
skills.ThirdEye = true;
}
if (Plugin.PlayerSkillRunner.Value)
{
skills.Runner = true;
}
if (Plugin.PlayerSkillCarefulStep.Value)
{
skills.CarefulStep = true;
}
if (Plugin.PlayerSkillShakyHands.Value)
{
skills.ShakyHands = true;
}
if (Plugin.PlayerSkillPoisonVulnerability.Value)
{
skills.PoisonVulnerability = true;
}
if (Plugin.PlayerSkillWeakRegeneration.Value)
{
skills.WeakHealthRegeneration = true;
}
if (Plugin.PlayerSkillShadows.Value)
{
// Set NightShadows field directly
var nightShadowsField = typeof(PlayerSkills).GetField("NightShadows",
BindingFlags.Public | BindingFlags.Instance);
if (nightShadowsField != null)
{
nightShadowsField.SetValue(skills, true);
}
}
if (Plugin.PlayerSkillAdrenaline.Value)
{
skills.Strong = true;
}
if (Plugin.PlayerSkillFearful.Value)
{
// Try to set narrowerFOV field
var narrowerFOVField = typeof(PlayerSkills).GetField("narrowerFOV",
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
if (narrowerFOVField != null)
{
narrowerFOVField.SetValue(skills, true);
}
}
// Note: Appetite (panda) might not have a boolean property to set
}
catch (Exception e)
{
Plugin.Log.LogError($"[Skills] Error updating player stats: {e}");
}
}
private static void LogFinalSkillStates(PlayerSkills skills)
{
Plugin.LogDivider();
Plugin.Log.LogInfo("[Skills] FINAL SKILL STATES");
Plugin.Log.LogInfo($"Total Skills: {skills.skills.Count} ({skills.manuallyActivatedSkills.Count} Manual, {skills.automaticActivatedSkills.Count} Auto)");
// Track all known skills and their status
var enabledActive = new List<string>();
var enabledPassive = new List<string>();
var disabledSkills = new List<string>();
var missingSkills = new List<string>();
// Also track which game objects were found for each config
var foundGameObjects = new Dictionary<string, string>();
foreach (var skill in skills.skills)
{
if (skill == null) continue;
var configKey = GetConfigKeyFromGameObjectName(skill.gameObject.name);
if (!string.IsNullOrEmpty(configKey))
{
foundGameObjects[configKey] = skill.gameObject.name;
}
}
foreach (var kvp in AllSkills)
{
var configKey = kvp.Key;
var skillInfo = kvp.Value;
var shouldBeEnabled = ShouldSkillBeEnabled(configKey);
var isFound = foundGameObjects.ContainsKey(configKey);
var displayName = $"{configKey} ({skillInfo.GameObjectName})";
var type = skillInfo.Type;
if (shouldBeEnabled)
{
if (isFound)
{
if (type == "Active")
enabledActive.Add(displayName);
else
enabledPassive.Add(displayName);
}
else
{
missingSkills.Add($"{configKey} - GameObject '{skillInfo.GameObjectName}' not found");
}
}
else
{
disabledSkills.Add(displayName);
}
}
// Log enabled active skills
Plugin.Log.LogInfo("");
Plugin.Log.LogInfo($"ENABLED ACTIVE SKILLS ({enabledActive.Count}):");
if (enabledActive.Count > 0)
{
foreach (var skill in enabledActive.OrderBy(s => s))
{
Plugin.Log.LogInfo($" • {skill}");
}
}
else
{
Plugin.Log.LogInfo(" None");
}
// Log enabled passive skills
Plugin.Log.LogInfo("");
Plugin.Log.LogInfo($"ENABLED PASSIVE SKILLS ({enabledPassive.Count}):");
if (enabledPassive.Count > 0)
{
foreach (var skill in enabledPassive.OrderBy(s => s))
{
Plugin.Log.LogInfo($" • {skill}");
}
}
else
{
Plugin.Log.LogInfo(" None");
}
// Log disabled skills
Plugin.Log.LogInfo("");
Plugin.Log.LogInfo($"DISABLED SKILLS ({disabledSkills.Count}):");
if (disabledSkills.Count > 0)
{
foreach (var skill in disabledSkills.OrderBy(s => s))
{
Plugin.Log.LogInfo($" • {skill}");
}
}
else
{
Plugin.Log.LogInfo(" None");
}
// Log missing skills (enabled but not found)
if (missingSkills.Count > 0)
{
Plugin.Log.LogInfo("");
Plugin.Log.LogInfo($"MISSING SKILLS ({missingSkills.Count}):");
foreach (var skill in missingSkills.OrderBy(s => s))
{
Plugin.Log.LogWarning($" ⚠ {skill}");
}
}
// Log actual skills in lists for debugging
if (Plugin.LogDebug.Value)
{
Plugin.Log.LogInfo("");
Plugin.Log.LogInfo("[Skills] Actual Skills in Lists:");
Plugin.Log.LogInfo($"Total: {skills.skills.Count}");
Plugin.Log.LogInfo($"Manual: {skills.manuallyActivatedSkills.Count}");
Plugin.Log.LogInfo($"Auto: {skills.automaticActivatedSkills.Count}");
foreach (var skill in skills.skills)
{
if (skill != null)
{
var configKey = GetConfigKeyFromGameObjectName(skill.gameObject.name);
Plugin.Log.LogInfo($" {skill.gameObject.name} -> {configKey ?? "UNKNOWN"}");
}
}
}
Plugin.LogDivider();
}
// Skill Activation Patches
[HarmonyPatch(typeof(PlayerSkills), "activateSkill")]
[HarmonyPrefix]
// ReSharper disable once InconsistentNaming
private static bool OverrideSkillActivation(PlayerSkills __instance, int skillId)
{
if (!Plugin.PlayerSkillsModification.Value) return true;
if (skillId < 0 || skillId >= __instance.manuallyActivatedSkills.Count)
return false;
var skill = __instance.manuallyActivatedSkills[skillId];
if (skill == null) return false;
var skillName = skill.gameObject.name;
var configKey = GetConfigKeyFromGameObjectName(skillName);
if (string.IsNullOrEmpty(configKey))
return false;
var skillEnabled = GetEnableConfigValue(configKey);
if (!skillEnabled)
{
if (Plugin.LogDebug.Value)
{
Plugin.Log.LogInfo($"[Skills] Blocked activation of non-enabled skill: {skillName}");
}
return false;
}
return true;
}
// Helper method to refresh skills when config changes
public static void RefreshAllSkills()
{
_skillsAlreadyApplied = false;
if (Player.Instance != null && Player.Instance.skills != null)
{
ApplyAllSkillStates(Player.Instance.skills);
}
}
// Helper Class
private class SkillInfo(string gameObjectName, string type, string tier)
{
public string GameObjectName { get; } = gameObjectName;
public string Type { get; } = type; // "Active" or "Passive"
public string Tier { get; } = tier;
}
}
}