Skip to content

Commit

Permalink
Harden rampId hooks; Fix crash if system.xml is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
ManlyMarco committed Sep 13, 2021
1 parent deb8c97 commit 8842f58
Show file tree
Hide file tree
Showing 2 changed files with 197 additions and 99 deletions.
151 changes: 101 additions & 50 deletions src/KKS_Sideloader/UniversalAutoResolver/KKS.UAR.Hooks.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using BepInEx.Logging;
using System;
using BepInEx.Logging;
using ExtensibleSaveFormat;
using HarmonyLib;
using Illusion.Elements.Xml;
using Studio;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Config;

namespace Sideloader.AutoResolver
{
Expand Down Expand Up @@ -79,88 +82,136 @@ internal static void ExtendedCardImport(Dictionary<string, PluginData> importedE
}
}

#region rampID GUID

private const string ConfigFilePath = "UserData/config/system.xml";

[HarmonyPrefix, HarmonyPatch(typeof(Control), nameof(Control.Write))]
private static void XMLWritePrefix(Control __instance, ref int __state)
{
__state = -1;
foreach (Data data in __instance.Datas)
if (data is Config.GraphicSystem graphicSystem)
if (graphicSystem.rampId >= BaseSlotID)
try
{
__state = -1;
foreach (Data data in __instance.Datas)
{
if (data is GraphicSystem graphicSystem)
{
ResolveInfo RampResolveInfo = LoadedResolutionInfo.FirstOrDefault(x => x.Property == "Ramp" && x.LocalSlot == graphicSystem.rampId);
if (RampResolveInfo == null)
{
//ID is a sideloader ID but no resolve info found, set it to the default
__state = 1;
graphicSystem.rampId = 1;
}
else
if (graphicSystem.rampId >= BaseSlotID)
{
//Switch out the resolved ID for the original
__state = graphicSystem.rampId;
graphicSystem.rampId = RampResolveInfo.Slot;
ResolveInfo RampResolveInfo = LoadedResolutionInfo.FirstOrDefault(x => x.Property == "Ramp" && x.LocalSlot == graphicSystem.rampId);
if (RampResolveInfo == null)
{
//ID is a sideloader ID but no resolve info found, set it to the default
__state = 1;
graphicSystem.rampId = 1;
}
else
{
//Switch out the resolved ID for the original
__state = graphicSystem.rampId;
graphicSystem.rampId = RampResolveInfo.Slot;
}
}
}
}
}
catch (Exception e)
{
__state = -1;
UnityEngine.Debug.LogException(e);
}
}

[HarmonyPostfix, HarmonyPatch(typeof(Control), nameof(Control.Write))]
private static void XMLWritePostfix(Control __instance, ref int __state)
{
int rampId = __state;
if (rampId >= BaseSlotID)
foreach (Data data in __instance.Datas)
if (data is Config.GraphicSystem graphicSystem)
try
{
int rampId = __state;
if (rampId >= BaseSlotID)
{
foreach (Data data in __instance.Datas)
{
ResolveInfo RampResolveInfo = LoadedResolutionInfo.FirstOrDefault(x => x.Property == "Ramp" && x.LocalSlot == rampId);
if (RampResolveInfo != null)
if (data is GraphicSystem graphicSystem)
{
//Restore the resolved ID
graphicSystem.rampId = RampResolveInfo.LocalSlot;

var xmlDoc = XDocument.Load("UserData/config/system.xml");
xmlDoc.Element("System").Element("Graphic").Element("rampId").AddAfterSelf(new XElement("rampGUID", RampResolveInfo.GUID));
xmlDoc.Save("UserData/config/system.xml");
ResolveInfo RampResolveInfo = LoadedResolutionInfo.FirstOrDefault(x => x.Property == "Ramp" && x.LocalSlot == rampId);
if (RampResolveInfo != null)
{
//Restore the resolved ID
graphicSystem.rampId = RampResolveInfo.LocalSlot;

var xmlDoc = XDocument.Load(ConfigFilePath);
xmlDoc.Element("System").Element("Graphic").Element("rampId").AddAfterSelf(new XElement("rampGUID", RampResolveInfo.GUID));
xmlDoc.Save(ConfigFilePath);
}
}
}
}
}
catch (Exception e)
{
UnityEngine.Debug.LogException(e);
}
}

[HarmonyPostfix, HarmonyPatch(typeof(Control), nameof(Control.Read))]
private static void XMLReadPostfix(Control __instance)
{
foreach (Data data in __instance.Datas)
if (data is Config.GraphicSystem graphicSystem)
if (graphicSystem.rampId >= BaseSlotID) //Saved with a resolved ID, reset it to default
graphicSystem.rampId = 1;
else
try
{
foreach (Data data in __instance.Datas)
{
if (data is GraphicSystem graphicSystem)
{
var xmlDoc = XDocument.Load("UserData/config/system.xml");
string rampGUID = xmlDoc.Element("System").Element("Graphic").Element("rampGUID")?.Value;
if (!rampGUID.IsNullOrWhiteSpace())
if (graphicSystem.rampId >= BaseSlotID) //Saved with a resolved ID, reset it to default
graphicSystem.rampId = 1;
else if (File.Exists(ConfigFilePath))
{
ResolveInfo RampResolveInfo = LoadedResolutionInfo.FirstOrDefault(x => x.Property == "Ramp" && x.GUID == rampGUID && x.Slot == graphicSystem.rampId);
if (RampResolveInfo == null) //Missing mod, reset ID to default
graphicSystem.rampId = 1;
else //Restore the resolved ID
graphicSystem.rampId = RampResolveInfo.LocalSlot;
var xmlDoc = XDocument.Load(ConfigFilePath);
string rampGUID = xmlDoc.Element("System").Element("Graphic").Element("rampGUID")?.Value;
if (!rampGUID.IsNullOrWhiteSpace())
{
ResolveInfo RampResolveInfo = LoadedResolutionInfo.FirstOrDefault(x => x.Property == "Ramp" && x.GUID == rampGUID && x.Slot == graphicSystem.rampId);
if (RampResolveInfo == null) //Missing mod, reset ID to default
graphicSystem.rampId = 1;
else //Restore the resolved ID
graphicSystem.rampId = RampResolveInfo.LocalSlot;
}
}
}
}
}
catch (Exception e)
{
UnityEngine.Debug.LogException(e);
}
}
//Studio
[HarmonyPostfix, HarmonyPatch(typeof(SceneInfo), nameof(SceneInfo.Init))]
private static void SceneInfoInit(SceneInfo __instance)
{
var xmlDoc = XDocument.Load("UserData/config/system.xml");
string rampGUID = xmlDoc.Element("System").Element("Graphic").Element("rampGUID")?.Value;
string rampIDXML = xmlDoc.Element("System").Element("Graphic").Element("rampId")?.Value;
if (!rampGUID.IsNullOrWhiteSpace() && !rampIDXML.IsNullOrWhiteSpace() && int.TryParse(rampIDXML, out int rampID))
try
{
ResolveInfo RampResolveInfo = LoadedResolutionInfo.FirstOrDefault(x => x.Property == "Ramp" && x.GUID == rampGUID && x.Slot == rampID);
if (RampResolveInfo == null) //Missing mod, reset ID to default
__instance.rampG = 1;
else //Restore the resolved ID
__instance.rampG = RampResolveInfo.LocalSlot;
if (!File.Exists(ConfigFilePath)) return;
var xmlDoc = XDocument.Load(ConfigFilePath);
string rampGUID = xmlDoc.Element("System").Element("Graphic").Element("rampGUID")?.Value;
string rampIDXML = xmlDoc.Element("System").Element("Graphic").Element("rampId")?.Value;
if (!rampGUID.IsNullOrWhiteSpace() && !rampIDXML.IsNullOrWhiteSpace() && int.TryParse(rampIDXML, out int rampID))
{
ResolveInfo RampResolveInfo = LoadedResolutionInfo.FirstOrDefault(x => x.Property == "Ramp" && x.GUID == rampGUID && x.Slot == rampID);
if (RampResolveInfo == null) //Missing mod, reset ID to default
__instance.rampG = 1;
else //Restore the resolved ID
__instance.rampG = RampResolveInfo.LocalSlot;
}
}
catch (Exception e)
{
UnityEngine.Debug.LogException(e);
}
}

#endregion
}
}
}

0 comments on commit 8842f58

Please sign in to comment.