/
CharacterApi.Hooks.KK.cs
232 lines (202 loc) · 10.4 KB
/
CharacterApi.Hooks.KK.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
using ADV;
using ChaCustom;
using HarmonyLib;
using KKAPI.Maker;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
namespace KKAPI.Chara
{
public static partial class CharacterApi
{
private static class Hooks
{
public static void InitHooks()
{
var i = Harmony.CreateAndPatchAll(typeof(Hooks));
// Fuzzy argument lengths are needed for darkness compatibility
var target = typeof(ChaControl).GetMethods().Single(info => info.Name == nameof(ChaControl.Initialize) && info.GetParameters().Length >= 5);
i.Patch(target, null, new HarmonyMethod(typeof(Hooks), nameof(ChaControl_InitializePostHook)));
var target2 = typeof(ChaControl).GetMethods().Single(info => info.Name == nameof(ChaControl.ReloadAsync) && info.GetParameters().Length >= 5);
i.Patch(target2, null, new HarmonyMethod(typeof(Hooks), nameof(ReloadAsyncPostHook)));
#if KKS
var target2a = typeof(ChaControl).GetMethods().Single(info => info.Name == nameof(ChaControl.ReloadNoAsync) && info.GetParameters().Length >= 4);
i.Patch(target2a, null, new HarmonyMethod(typeof(Hooks), nameof(ReloadNoAsyncPostHook)));
#endif
var target3 = typeof(ChaFile).GetMethods().Single(info => info.Name == nameof(ChaFile.CopyAll));
i.Patch(target3, null, new HarmonyMethod(typeof(Hooks), nameof(ChaFile_CopyChaFileHook)));
#if KK //todo || KKS
// Find the ADV character Start lambda to hook for extended data copying. The inner type names change between versions so try them all.
var transpiler = new HarmonyMethod(typeof(Hooks), nameof(FixEventSceneLambdaTpl));
var lambdaOuter = AccessTools.Inner(typeof(FixEventSceneEx), "<Start>c__AnonStorey1");
foreach (var nestedType in lambdaOuter.GetNestedTypes(AccessTools.all))
{
if (!nestedType.IsClass) continue;
var lambdaMethod = AccessTools.Method(nestedType, "<>m__0");
if (lambdaMethod != null)
i.Patch(lambdaMethod, null, null, transpiler);
}
#endif
}
public static void ChaControl_InitializePostHook(ChaControl __instance)
{
KoikatuAPI.Logger.LogDebug($"Character card load: {GetLogName(__instance)} {(MakerAPI.CharaListIsLoading ? "inside CharaList" : string.Empty)}");
ChaControls.Add(__instance);
if (!MakerAPI.CharaListIsLoading)
CreateOrAddBehaviours(__instance);
}
[HarmonyPostfix]
[HarmonyPatch(typeof(ChaControl), nameof(ChaControl.OnDestroy))]
public static void ChaControl_OnDestroyHook(ChaControl __instance)
{
ChaControls.Remove(__instance);
}
/// <summary>
/// Needed for saving in class maker, rest is handled by ExtendedSave.CardBeingSaved
/// </summary>
[HarmonyPrefix]
[HarmonyPatch(typeof(CvsExit), nameof(CvsExit.ExitSceneRestoreStatus), new[]
{
typeof(string)
})]
public static void CvsExit_ExitSceneRestoreStatus(string strInput, CvsExit __instance)
{
OnCardBeingSaved(Singleton<CustomBase>.Instance.chaCtrl.chaFile);
}
/// <summary>
/// Copy extended data when moving between class roster and main game data, and in free h
/// (the character data gets transferred to predefined slots instead of creating new characters)
/// </summary>
public static void ChaFile_CopyChaFileHook(ChaFile __instance, ChaFile _chafile)
{
OnCopyChaFile(__instance, _chafile);
}
private static void OnCopyChaFile(ChaFile destination, ChaFile source)
{
foreach (var handler in _registeredHandlers)
{
if (handler.Value.ExtendedDataCopier == null)
continue;
try
{
handler.Value.ExtendedDataCopier(destination, source);
}
catch (Exception e)
{
KoikatuAPI.Logger.LogError(e);
}
}
}
#if KK || KKS
/// <summary>
/// Fix extended data being lost in ADV by copying it over when chara data is copied
/// </summary>
public static IEnumerable<CodeInstruction> FixEventSceneLambdaTpl(MethodBase original, IEnumerable<CodeInstruction> instructions)
{
/* 1 is the source ChaFileControl to copy from, 2 is CharaData that has the destination ChaFile. Below code after which we hook in
60 00B8 ldloc.2
61 00B9 callvirt instance class ChaFileControl SaveData/CharaData::get_charFile()
62 00BE ldloc.1
63 00BF ldfld uint8[] ChaFile::pngData
64 00C4 stfld uint8[] ChaFile::pngData
*/
var il = instructions.ToList();
var target = AccessTools.Field(typeof(ChaFile), nameof(ChaFile.pngData));
if (target == null) throw new ArgumentNullException(nameof(target));
// This can get called on lambdas that are not the one we need to patch, only one of them uses Stfld on ChaFile.pngData
var targetIndex = il.FindIndex(instruction => instruction.opcode == OpCodes.Stfld && instruction.operand is FieldInfo fi && fi == target);
if (targetIndex > 0)
{
il.InsertRange(
targetIndex + 1, new[]
{
// Target
new CodeInstruction(OpCodes.Ldloc_2),
new CodeInstruction(OpCodes.Callvirt, AccessTools.Property(typeof(SaveData.CharaData), nameof(SaveData.CharaData.charFile)).GetGetMethod()),
// Source
new CodeInstruction(OpCodes.Ldloc_1),
// Call the data copy
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(Hooks), nameof(OnCopyChaFile))),
});
if (KoikatuAPI.EnableDebugLogging)
KoikatuAPI.Logger.LogDebug("FixEventSceneLambdaTpl success on " + original.FullDescription());
}
return il;
}
/// <summary>
/// Needed to update controllers after ADV scene finishes loading since characters get loaded async so other events fire too early
/// </summary>
[HarmonyPostfix]
[HarmonyPatch(typeof(CharaData), nameof(CharaData.Initialize))]
public static void CharaData_InitializePost(CharaData __instance)
{
// Only run reload if the character was newly created
if (__instance.isADVCreateChara)
ReloadChara(__instance.chaCtrl);
}
/// <summary>
/// Update controllers after selecting live mode character
/// </summary>
[HarmonyPostfix]
[HarmonyPatch(typeof(LiveCharaSelectSprite), "Start")]
public static void LiveCharaSelectSprite_StartPostHook(LiveCharaSelectSprite __instance)
{
__instance.btnIdolBack?.onClick.AddListener(() => __instance.StartCoroutine(DelayedReloadChara(__instance.heroine.chaCtrl)));
}
/// <summary>
/// Force reload when going to next day in school
/// It's needed because after 1st day since loading the characters are reset but not reloaded, and can cause issues
/// </summary>
[HarmonyPrefix]
#if KKS
// For some reason both of these methods are copies of each other and are called from the same place (one or the other)
// todo this needs a check when full KKS game comes out
[HarmonyPatch(typeof(ActionScene), nameof(ActionScene.NPCLoadAll), new Type[0])]
[HarmonyPatch(typeof(ActionScene), nameof(ActionScene.NPCLoadAll), typeof(bool))]
#else
[HarmonyPatch(typeof(ActionScene), nameof(ActionScene.NPCLoadAll))]
#endif
public static void ActionScene_NPCLoadAllPreHook(ActionScene __instance)
{
__instance.StartCoroutine(DelayedReloadChara(null));
}
#endif
/// <summary>
/// Needed for some edge cases, replacing characters in scene maker in EC
/// </summary>
public static void ReloadAsyncPostHook(bool noChangeClothes, bool noChangeHead, bool noChangeHair, bool noChangeBody, ChaControl __instance, ref IEnumerator __result)
{
if (noChangeClothes || noChangeHead || noChangeHair || noChangeBody) return;
if (IsCurrentlyReloading(__instance)) return;
var original = __result;
__result = new[] { original, DelayedReloadChara(__instance) }.GetEnumerator();
}
public static void ReloadNoAsyncPostHook(bool noChangeClothes, bool noChangeHead, bool noChangeHair, bool noChangeBody, ChaControl __instance)
{
if (noChangeClothes || noChangeHead || noChangeHair || noChangeBody) return;
// Avoid double reloads in maker
if (MakerAPI.InsideMaker) return;
ReloadChara(__instance);
}
/// <summary>
/// Prevents firing coordinate load events when the coordinate window is populating
/// </summary>
public static bool ClothesFileControlLoading;
[HarmonyPrefix]
[HarmonyPatch(typeof(clothesFileControl), "Initialize")]
public static void clothesFileControl_InitializePreHook()
{
ClothesFileControlLoading = true;
}
[HarmonyPostfix]
[HarmonyPatch(typeof(clothesFileControl), "Initialize")]
public static void clothesFileControl_InitializePostHook()
{
ClothesFileControlLoading = false;
}
}
}
}