Skip to content

Commit

Permalink
Added JSON serialization/deserialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
RealityMachina committed May 24, 2018
1 parent c06b02f commit 3816057
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 13 deletions.
52 changes: 45 additions & 7 deletions PunchinOut/BasicPanic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using Newtonsoft.Json;
using System.Collections.Generic;
using PunchinOut;
using BattleTech.Serialization;
using System.IO;

namespace BasicPanic
{
Expand All @@ -25,6 +25,7 @@ public static void Prefix(AttackStackSequence __instance, MessageCenterMessage m
if (attackCompleteMessage == null || attackCompleteMessage.stackItemUID != __instance.SequenceGUID)
return;


if (__instance.directorSequences[0].target is Mech)
{
mech = __instance.directorSequences[0].target as Mech;
Expand All @@ -33,6 +34,8 @@ public static void Prefix(AttackStackSequence __instance, MessageCenterMessage m

}

Holder.Serialize();

if (PanicHelpers.IsPanicking(mech) && BasicPanic.RollForEjectionResult(mech, attackCompleteMessage.attackSequence))
{
mech.EjectPilot(mech.GUID, attackCompleteMessage.stackItemUID, DeathMethod.PilotEjection, false);
Expand Down Expand Up @@ -140,6 +143,8 @@ public static void Prefix(AbstractActor __instance)
__instance.StatCollection.ModifyStat<float>("Panic Turn: Panicking Defence!", -1, "ToHitThisActor", StatCollection.StatOperation.Float_Add, BasicPanic.Settings.PanickedToHitModifier, -1, true);
}
}

Holder.Serialize();
}
}

Expand Down Expand Up @@ -197,7 +202,7 @@ public static int GetTrackedPilotIndex(Mech mech)

if(Holder.TrackedPilots == null)
{
Holder.TrackedPilots = new List<PanicTracker>();
Holder.Deserialize();
}

for (int i = 0; i < Holder.TrackedPilots.Count; i++)
Expand Down Expand Up @@ -233,6 +238,8 @@ public static bool ShouldPanic(Mech mech, AttackDirector.AttackSequence attackSe
return false;
}



int PanicRoll = 0;

Pilot pilot = mech.GetPilot();
Expand Down Expand Up @@ -476,17 +483,47 @@ internal class ModSettings
public float WeaponlessModifier = 15;
public float AloneModifier = 20;
}
[SerializableContract("Holder")]

public static class Holder
{
[SerializableMember(SerializationTarget.SaveGame)]
public static List<PanicTracker> TrackedPilots;

public static string JsonPath;
public static string ModDirectory;
public static void Reset()
{
TrackedPilots = new List<PanicTracker>();
}


public static void Serialize()
{
try
{
if (TrackedPilots != null)
{
File.WriteAllText(JsonPath, JsonConvert.SerializeObject(TrackedPilots));
}
}
catch (Exception)
{
return;
}
}

public static void Deserialize()
{
// read all text, then deserialize into an object
List<PanicTracker> panicTrackers = JsonConvert.DeserializeObject<List<PanicTracker>>(File.ReadAllText(JsonPath));

if (panicTrackers == null)
{
TrackedPilots = new List<PanicTracker>();
}
else
{
TrackedPilots = panicTrackers;
}

}
}

public static class BasicPanic
Expand All @@ -497,7 +534,8 @@ public static void Init(string modDir, string modSettings)
{
var harmony = HarmonyInstance.Create("io.github.RealityMachina.BasicPanic");
harmony.PatchAll(Assembly.GetExecutingAssembly());

Holder.ModDirectory = Path.Combine(Path.GetDirectoryName(VersionManifestUtilities.MANIFEST_FILEPATH), @"..\..\..\Mods\BasicPanicSystem");
Holder.JsonPath = Path.Combine(Holder.ModDirectory, "BasicPanicSystem.json");
try
{
Settings = JsonConvert.DeserializeObject<ModSettings>(modSettings);
Expand Down
17 changes: 11 additions & 6 deletions PunchinOut/panicTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Linq;
using System.Text;
using BattleTech;
using BattleTech.Serialization;

namespace PunchinOut
{
Expand All @@ -16,24 +15,30 @@ public enum PanicStatus
Panicked
}

[SerializableContract("Holder")]

public class PanicTracker
{
[SerializableMember(SerializationTarget.SaveGame)]

public PanicStatus pilotStatus;

[SerializableMember(SerializationTarget.SaveGame)]

public string trackedMech;

[SerializableMember(SerializationTarget.SaveGame)]

public bool ChangedRecently;

public PanicTracker()
{
//do nothing here, if this is called, then JSON is deserializing us
}
public PanicTracker(Mech mech)
{



trackedMech = mech.GUID;
pilotStatus = PanicStatus.Normal;
ChangedRecently = false;
}
}

}

0 comments on commit 3816057

Please sign in to comment.