-
Notifications
You must be signed in to change notification settings - Fork 3
/
SaveHandling.cs
171 lines (161 loc) · 7.02 KB
/
SaveHandling.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
using System.IO;
using System.Linq;
using BattleTech;
using BattleTech.Save.Test;
using Harmony;
using Newtonsoft.Json;
using UnityEngine;
using BattleTech.Save;
using System;
using System.Collections.Generic;
namespace Pilot_Quirks
{
public static class SaveHandling
{
//This patch is useful for setting up the Sim after the career or campaign is initially started.
[HarmonyPatch(typeof(SimGameState), "_OnAttachUXComplete")]
public static class SimGameState__OnAttachUXComplete_Patch
{
public static void Postfix()
{
//var sim = UnityGameInstance.BattleTechGame.Simulation;
//if ()
//{
//}
//else
//{
// LogDebug("_OnAttachUXComplete Postfix");
//}
}
}
//On game load.
[HarmonyPatch(typeof(SimGameState), "Rehydrate")]
public static class SimGameState_Rehydrate_Patch
{
static void Postfix(SimGameState __instance, GameInstanceSave gameInstanceSave)
{
bool NewPQ = true;
foreach (string tag in __instance.CompanyTags)
{
if (tag.StartsWith("PilotQuirksSave{"))
NewPQ = false;
}
if (!NewPQ)
{
DeserializePilotQuirks();
CheckForDuplicateTattoos();
}
}
}
internal static void DeserializePilotQuirks()
{
var sim = UnityGameInstance.BattleTechGame.Simulation;
bool OldSave = true;
if (sim.CompanyTags.First(x => x.StartsWith("PilotQuirksSave{")) != null)
MechBonding.PilotsAndMechs = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, int>>>(sim.CompanyTags.First(x => x.StartsWith("PilotQuirksSave{")).Substring(15));
bool SecondPQS2 = sim.CompanyTags.Any(x => x.StartsWith("PilotQuirksSave2"));
if (SecondPQS2 && sim.CompanyTags.First(x => x.StartsWith("PilotQuirksSave2")) != null)
{
MechBonding.PQ_GUID = JsonConvert.DeserializeObject<int>(sim.CompanyTags.First(x => x.StartsWith("PilotQuirksSave2")).Substring(16));
OldSave = false;
}
if (OldSave && Pre_Control.settings.MechBonding)
{
UpdateSavedGame();
}
}
//This is necessary to correct previous saves to the new save dictionary
internal static void UpdateSavedGame()
{
Dictionary<string, Dictionary<string, int>> OldDictionaryHolder = new Dictionary<string, Dictionary<string, int>>(MechBonding.PilotsAndMechs);
MechBonding.PilotsAndMechs.Clear();
Dictionary<string, string> PilotTransfer = new Dictionary<string, string>();
var sim = UnityGameInstance.BattleTechGame.Simulation;
PilotTransfer.Add(sim.Commander.pilotDef.Description.Id, "PQ_Pilot_GUID_" + MechBonding.PQ_GUID);
sim.Commander.pilotDef.PilotTags.Add("PQ_Pilot_GUID_" + MechBonding.PQ_GUID);
MechBonding.PQ_GUID++;
foreach (PilotDef pilotdef in sim.CurSystem.AvailablePilots)
{
PilotTransfer.Add(pilotdef.Description.Id, "PQ_Pilot_GUID_" + MechBonding.PQ_GUID);
pilotdef.PilotTags.Add("PQ_Pilot_GUID_" + MechBonding.PQ_GUID);
MechBonding.PQ_GUID++;
}
foreach (Pilot hiredpilot in sim.PilotRoster)
{
PilotTransfer.Add(hiredpilot.pilotDef.Description.Id, "PQ_Pilot_GUID_" + MechBonding.PQ_GUID);
hiredpilot.pilotDef.PilotTags.Add("PQ_Pilot_GUID_" + MechBonding.PQ_GUID);
MechBonding.PQ_GUID++;
}
foreach (Pilot deadpilot in sim.Graveyard)
{
PilotTransfer.Add(deadpilot.pilotDef.Description.Id, "PQ_Pilot_GUID_" + MechBonding.PQ_GUID);
deadpilot.pilotDef.PilotTags.Add("PQ_Pilot_GUID_" + MechBonding.PQ_GUID);
MechBonding.PQ_GUID++;
}
foreach (string OldPilot in OldDictionaryHolder.Keys)
{
if (PilotTransfer.Keys.Contains(OldPilot))
MechBonding.PilotsAndMechs.Add(PilotTransfer[OldPilot], OldDictionaryHolder[OldPilot]);
}
}
//What happens on game save.
[HarmonyPatch(typeof(SimGameState), "Dehydrate")]
public static class SimGameState_Dehydrate_Patch
{
public static void Prefix()
{
SerializePilotQuirks();
}
}
internal static void SerializePilotQuirks()
{
var sim = UnityGameInstance.BattleTechGame.Simulation;
sim.CompanyTags.Where(tag => tag.StartsWith("PilotQuirks")).Do(x => sim.CompanyTags.Remove(x));
sim.CompanyTags.Add("PilotQuirksSave" + JsonConvert.SerializeObject(Pilot_Quirks.MechBonding.PilotsAndMechs));
sim.CompanyTags.Add("PilotQuirksSave2" + JsonConvert.SerializeObject(MechBonding.PQ_GUID));
}
internal static void CheckForDuplicateTattoos()
{
List<string> PilotTattoos = new List<string>();
var sim = UnityGameInstance.BattleTechGame.Simulation;
foreach (Pilot hiredpilot in sim.PilotRoster)
{
foreach (string tag in hiredpilot.pilotDef.PilotTags)
{
if (tag.StartsWith("PQ_Pilot_GUID_"))
{
if (!PilotTattoos.Contains(tag))
PilotTattoos.Add(tag);
else
{
hiredpilot.pilotDef.PilotTags.Remove(tag);
string newTag = "PQ_Pilot_GUID_" + MechBonding.PQ_GUID;
hiredpilot.pilotDef.PilotTags.Add(newTag);
MechBonding.PQ_GUID++;
MechBonding.PilotsAndMechs.Add(newTag, MechBonding.PilotsAndMechs[tag]);
}
}
}
}
foreach (Pilot deadpilot in sim.Graveyard)
{
foreach (string tag in deadpilot.pilotDef.PilotTags)
{
if (tag.StartsWith("PQ_Pilot_GUID_"))
{
if (!PilotTattoos.Contains(tag))
PilotTattoos.Add(tag);
else
{
deadpilot.pilotDef.PilotTags.Remove(tag);
string newTag = "PQ_Pilot_GUID_" + MechBonding.PQ_GUID;
deadpilot.pilotDef.PilotTags.Add(newTag);
MechBonding.PQ_GUID++;
MechBonding.PilotsAndMechs.Add(newTag, MechBonding.PilotsAndMechs[tag]);
}
}
}
}
}
}
}