Skip to content

Commit

Permalink
Map KK outfits to KKS outfit slots, expose coordinateMapping dictiona…
Browse files Browse the repository at this point in the history
…ry in CardImportEvent
  • Loading branch information
DeathWeasel1337 committed Sep 3, 2021
1 parent 0b8a64e commit fa9b665
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 17 deletions.
6 changes: 3 additions & 3 deletions src/KKS_ExtensibleSaveFormat/KKS.ExtendedSave.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ namespace ExtensibleSaveFormat
public partial class ExtendedSave
{
/// <summary> ImportEventHandler </summary>
public delegate void ImportEventHandler(Dictionary<string, PluginData> importedExtendedData);
public delegate void ImportEventHandler(Dictionary<string, PluginData> importedExtendedData, Dictionary<int, int?> coordinateMapping);

/// <summary>
/// Contains all extended data read from the KK card. Key is data GUID.
/// Convert your data and write it back to the dictionary to get it saved.
/// </summary>
public static event ImportEventHandler CardBeingImported;

private static void CardImportEvent(Dictionary<string, PluginData> data)
private static void CardImportEvent(Dictionary<string, PluginData> data, Dictionary<int, int?> coordinateMapping)
{
if (CardBeingImported != null)
{
Expand All @@ -24,7 +24,7 @@ private static void CardImportEvent(Dictionary<string, PluginData> data)
var handler = (ImportEventHandler)entry;
try
{
handler.Invoke(data);
handler.Invoke(data, coordinateMapping);
}
catch (Exception ex)
{
Expand Down
70 changes: 67 additions & 3 deletions src/KKS_ExtensibleSaveFormat/KKS.ExtendedSave.Hooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,76 @@ public partial class ExtendedSave
{
internal static partial class Hooks
{
private static readonly bool MoreOutfitsInstalled;
private static Dictionary<int, int?> CoordinateMapping = new Dictionary<int, int?>();

static Hooks()
{
MoreOutfitsInstalled = Type.GetType($"KK_Plugins.MoreOutfits.Plugin, KKS_MoreOutfits") != null;
}

#region Import KK Chara

private static void KKChaFileLoadFileHook(ChaFile file, BlockHeader header, BinaryReader reader)
{
var info = header.SearchInfo(Marker);
CoordinateMapping = new Dictionary<int, int?>();

// Remap coordinates from the KK index to the matching KKS index
ChaFileCoordinate[] newCoordinates = new ChaFileCoordinate[file.coordinate.Length];
for (int i = 0; i < file.coordinate.Length; i++)
{
int index = i;
int? newIndex = i;

switch (i)
{
case 0:
newIndex = 4;
break;
case 1:
newIndex = 3;
break;
case 2:
newIndex = 5;
break;
case 3:
newIndex = 1;
break;
case 4:
//Get rid of these outfits without MoreOutfits installed since they wouldn't be usable
if (MoreOutfitsInstalled)
newIndex = 6;
else
newIndex = null;
break;
case 5:
if (MoreOutfitsInstalled)
newIndex = 0;
else
newIndex = null;
break;
case 6:
if (MoreOutfitsInstalled)
newIndex = 2;
else
newIndex = null;
break;
default:
//Carry over extra outfits if MoreOutfits is installed, otherwise drop them
if (MoreOutfitsInstalled)
newIndex = index;
else
newIndex = null;
break;
}

CoordinateMapping[index] = newIndex;
if (newIndex != null)
newCoordinates[(int)newIndex] = file.coordinate[index];
}
file.coordinate = newCoordinates;

var info = header.SearchInfo(Marker);
if (info != null && info.version == DataVersion.ToString())
{
var originalPosition = reader.BaseStream.Position;
Expand All @@ -34,7 +98,7 @@ private static void KKChaFileLoadFileHook(ChaFile file, BlockHeader header, Bina
var dictionary = MessagePackDeserialize<Dictionary<string, PluginData>>(data);
if (dictionary != null)
{
CardImportEvent(dictionary);
CardImportEvent(dictionary, CoordinateMapping);
internalCharaDictionary.Set(file, dictionary);
}
}
Expand Down Expand Up @@ -101,7 +165,7 @@ private static void KKChaFileLoadFilePostHook(ChaFile __instance, bool __result,

if (dictionary != null)
{
CardImportEvent(dictionary);
CardImportEvent(dictionary, CoordinateMapping);
internalCharaDictionary.Set(__instance, dictionary);
}
}
Expand Down
29 changes: 18 additions & 11 deletions src/KKS_Sideloader/UniversalAutoResolver/KKS.UAR.Hooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,36 @@ internal static partial class Hooks
[HarmonyPostfix, HarmonyPatch(typeof(ConvertChaFileScene), nameof(ConvertChaFileScene.OnDestroy))]
private static void ConvertChaFileSceneEnd() => DoingImport = false;

internal static void ExtendedCardImport(Dictionary<string, PluginData> importedExtendedData)
internal static void ExtendedCardImport(Dictionary<string, PluginData> importedExtendedData, Dictionary<int, int?> coordinateMapping)
{
if (importedExtendedData.TryGetValue(UARExtID, out var pluginData))
{
if (pluginData != null && pluginData.data.ContainsKey("info"))
{
var tmpExtInfo = (object[])pluginData.data["info"];
var extInfo = tmpExtInfo.Select(x => ResolveInfo.Deserialize((byte[])x)).ToList();
List<ResolveInfo> extInfoToRemove = new List<ResolveInfo>();

for (int i = 0; i < extInfo.Count;)
foreach (var info in extInfo)
{
if (extInfo[i].Property.StartsWith("outfit0"))
if (info.Property.StartsWith("outfit"))
{
i++;
var propertySplit = info.Property.Split('.');
int index = int.Parse(propertySplit[0].Replace("outfit", ""));
if (coordinateMapping.TryGetValue(index, out int? newIndex) && newIndex != null)
{
propertySplit[0] = $"outfit{newIndex}";
info.Property = string.Join(".", propertySplit);
}
else
{
//Remove all the excess outfits
extInfoToRemove.Add(info);
}
}
else if (extInfo[i].Property.StartsWith("outfit"))
{
//Remove all the excess outfits
extInfo.RemoveAt(i);
}
else
i++;
}
foreach (var infoToRemove in extInfoToRemove)
extInfo.Remove(infoToRemove);

importedExtendedData[UARExtID] = new PluginData
{
Expand Down

0 comments on commit fa9b665

Please sign in to comment.