Skip to content

Commit

Permalink
Error handling for wrong sideloader version
Browse files Browse the repository at this point in the history
  • Loading branch information
DeathWeasel1337 committed Apr 12, 2019
1 parent f9911da commit 34aaaea
Showing 1 changed file with 46 additions and 26 deletions.
72 changes: 46 additions & 26 deletions KK_GUIDMigration/KK_GUIDMigration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,40 @@ public class KK_GUIDMigration : BaseUnityPlugin
{
public const string GUID = "com.deathweasel.bepinex.guidmigration";
public const string PluginName = "GUID Migration";
public const string Version = "1.2";
public const string Version = "1.4";
private static List<MigrationInfo> MigrationInfoList = new List<MigrationInfo>();
private static readonly string GUIDMigrationFilePath = Path.Combine(Paths.PluginPath, "KK_GUIDMigration.csv");

void Main()
{
//Don't even bother if there's no mods directory
if (Directory.Exists(Path.Combine(Paths.GameRootPath, "mods")) && Directory.Exists(Paths.PluginPath))
if (!Directory.Exists(Path.Combine(Paths.GameRootPath, "mods")) || !Directory.Exists(Paths.PluginPath))
{
//Only do migration if there's a .csv file and it has stuff in it
if (File.Exists(GUIDMigrationFilePath))
{
GenerateMigrationInfo();
if (MigrationInfoList.Count > 0)
{
var harmony = HarmonyInstance.Create(GUID);
harmony.PatchAll(typeof(KK_GUIDMigration));
}
else
{
Logger.Log(LogLevel.Error | LogLevel.Message, "KK_GUIDMigration was not loaded due to missing or empty KK_GUIDMigration.csv file.");
}
}
Logger.Log(LogLevel.Warning, "KK_GUIDMigration was not loaded due to missing mods folder.");
return;
}

//Only do migration if there's a .csv file and it has stuff in it
if (!File.Exists(GUIDMigrationFilePath))
{
Logger.Log(LogLevel.Error | LogLevel.Message, "KK_GUIDMigration was not loaded due to missing KK_GUIDMigration.csv file.");
return;
}

GenerateMigrationInfo();
if (MigrationInfoList.Count == 0)
{
Logger.Log(LogLevel.Error | LogLevel.Message, "KK_GUIDMigration was not loaded due to empty KK_GUIDMigration.csv file.");
return;
}

var harmony = HarmonyInstance.Create(GUID);
harmony.PatchAll(typeof(KK_GUIDMigration));
}
/// <summary>
/// Look through all the GUIDs, compare it to the MigrationInfoList, and do migration when necessary
/// </summary>
private static IEnumerable<ResolveInfo> MigrateGUID(IEnumerable<ResolveInfo> extInfo, string characterName)
private static IEnumerable<ResolveInfo> MigrateGUID(IEnumerable<ResolveInfo> extInfo, string characterName = "")
{
List<ResolveInfo> extInfoNew = new List<ResolveInfo>();
bool DidBlankGUIDMessage = false;
Expand All @@ -64,7 +69,10 @@ private static IEnumerable<ResolveInfo> MigrateGUID(IEnumerable<ResolveInfo> ext
//Don't add empty GUID to the new list, this way CompatibilityResolve will treat it as a hard mod and attempt to find a match
if (!DidBlankGUIDMessage) //No need to spam it for every single thing
{
Logger.Log(LogLevel.Warning | LogLevel.Message, $"[{characterName}] Blank GUID detected, attempting Compatibility Resolve");
if (characterName == "")
Logger.Log(LogLevel.Warning | LogLevel.Message, $"Blank GUID detected, attempting Compatibility Resolve");
else
Logger.Log(LogLevel.Warning | LogLevel.Message, $"[{characterName}] Blank GUID detected, attempting Compatibility Resolve");
DidBlankGUIDMessage = true;
}
}
Expand Down Expand Up @@ -216,17 +224,29 @@ public MigrationInfo(string property, string oldGUID, int oldID, string newGUID,
}
}

[HarmonyPrefix]
[HarmonyPatch(typeof(Hooks), "IterateCardPrefixes")]
[HarmonyPrefix, HarmonyPatch(typeof(Hooks), "IterateCardPrefixes")]
public static void IterateCardPrefixesPrefix(ref IEnumerable<ResolveInfo> extInfo, ChaFile file)
{
extInfo = MigrateGUID(extInfo, file.parameter.fullname.Trim());
try
{
extInfo = MigrateGUID(extInfo, file.parameter.fullname.Trim());
}
catch
{
Logger.Log(LogLevel.Error | LogLevel.Message, $"GUID migration failed. Please update KK_GUIDMigration and/or BepisPlugins.");
}
}

[HarmonyPrefix]
[HarmonyPatch(typeof(Hooks), "IterateCoordinatePrefixes")]
public static void IterateCoordinatePrefixesPrefix(ref IEnumerable<ResolveInfo> extInfo, ChaFileCoordinate coordinate) {
extInfo = MigrateGUID(extInfo, coordinate.coordinateName.Trim());
[HarmonyPrefix, HarmonyPatch(typeof(Hooks), "IterateCoordinatePrefixes")]
public static void IterateCoordinatePrefixesPrefix(ref IEnumerable<ResolveInfo> extInfo, ChaFileCoordinate coordinate)
{
try
{
extInfo = MigrateGUID(extInfo);
}
catch
{
Logger.Log(LogLevel.Error | LogLevel.Message, $"GUID migration failed. Please update KK_GUIDMigration and/or BepisPlugins.");
}
}
}
}

0 comments on commit 34aaaea

Please sign in to comment.