-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFastSaveDelete.cs
65 lines (58 loc) · 2.07 KB
/
FastSaveDelete.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
using System;
using System.Reflection;
using BattleTech.Save.SaveGameStructure;
using BattleTech.UI;
using Harmony;
using UnityEngine;
namespace FastSaveDelete
{
public class FastSaveDelete
{
public static void Init(string directory, string settingsJSON)
{
var harmony = HarmonyInstance.Create("com.joelmeador.FastSaveDelete");
harmony.PatchAll(Assembly.GetExecutingAssembly());
}
}
[HarmonyPatch(typeof(SGLoadSavedGameScreen), "DeleteSave")]
public static class SGLoadSavedGameScreenDeleteSavePatch
{
static bool Prefix(
ref SlotModel slot,
SGLoadSavedGameScreen __instance)
{
SlotModel mostRecentSave = __instance.saveStructure.MostRecentCampaignSave;
// normal behavior for the most recent save.
if (slot == mostRecentSave) return true;
ActuallyDeleteSave(__instance, slot);
return false;
}
private static void ActuallyDeleteSave(SGLoadSavedGameScreen screen, SlotModel slot)
{
try
{
screen.saveStructure.Delete(slot);
}
catch (Exception ex)
{
Debug.LogError($"Exception caught: {ex.Message}");
}
ReflectionHelper.InvokePrivateMethode(screen, "BringToFront", new object[] { }, new Type[] { });
}
}
public static class ReflectionHelper
{
public static MethodInfo GetPrivateMethodInfo(object instance, string methodname, Type[] types)
{
Type type = instance.GetType();
MethodInfo methodInfo = type.GetMethod(methodname, BindingFlags.NonPublic | BindingFlags.Instance, null,
types, null);
return methodInfo;
}
public static object InvokePrivateMethode(object instance, string methodname, object[] parameters, Type[] types)
{
MethodInfo methodInfo = GetPrivateMethodInfo(instance, methodname, types);
return methodInfo.Invoke(instance, parameters);
}
}
}