Skip to content

Commit

Permalink
change to deferring logger, fix unavailable pilots being allowed to drop
Browse files Browse the repository at this point in the history
  • Loading branch information
ajkroeg committed Mar 17, 2022
1 parent 6f0be77 commit 7d999fc
Show file tree
Hide file tree
Showing 16 changed files with 176 additions and 169 deletions.
6 changes: 3 additions & 3 deletions Framework/PilotInjuryData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ internal void SerializeInjuryState()
var injuryState = sim.CompanyTags.FirstOrDefault(( x) => x.StartsWith(injuryStateTag));
sim.CompanyTags.Remove(injuryState);
injuryState = $"{injuryStateTag}{JsonConvert.SerializeObject(HolderInstance.pilotInjuriesMap)}";
ModInit.modLog.LogMessage($"Serialized injuryState and adding to company tags. State was {injuryState}");
ModInit.modLog?.Info?.Write($"Serialized injuryState and adding to company tags. State was {injuryState}");
sim.CompanyTags.Add(injuryState);
}

Expand All @@ -92,12 +92,12 @@ internal void DeserializeInjuryState()
var InjuryStateCTag = sim.CompanyTags.FirstOrDefault((x) => x.StartsWith(injuryStateTag));
var injuryState = InjuryStateCTag?.Substring(injuryStateTag.Length);
HolderInstance.pilotInjuriesMap = JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(injuryState);
ModInit.modLog.LogMessage($"Deserializing injuryState and removing from company tags. State was {injuryState}");
ModInit.modLog?.Info?.Write($"Deserializing injuryState and removing from company tags. State was {injuryState}");
sim.CompanyTags.Remove(InjuryStateCTag);
}
else
{
ModInit.modLog.LogMessage($"No injuryState to deserialize. Hopefully this is the first time you're running TBAS!");
ModInit.modLog?.Info?.Write($"No injuryState to deserialize. Hopefully this is the first time you're running TBAS!");
}
}
}
Expand Down
116 changes: 58 additions & 58 deletions Framework/PilotInjuryManager.cs

Large diffs are not rendered by default.

14 changes: 10 additions & 4 deletions ModInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
using Harmony;
using System.Reflection;
using BattleTech;
using IRBTModUtils.Logging;
using TisButAScratch.Framework;

namespace TisButAScratch
{
public static class ModInit
{
internal static Logger modLog;
internal static DeferringLogger modLog;
private static string modDir;


Expand All @@ -20,7 +21,7 @@ public static class ModInit
public static void Init(string directory, string settingsJSON)
{
modDir = directory;
modLog = new Logger(modDir, "TBAS", true);
Exception settingsException = null;
try
{
using (StreamReader reader = new StreamReader($"{modDir}/settings.json"))
Expand All @@ -32,11 +33,16 @@ public static void Init(string directory, string settingsJSON)
}
catch (Exception ex)
{
Logger.LogException(ex);
settingsException =ex;
ModInit.modSettings = new Settings();
}
modLog = new DeferringLogger(modDir, "TBAS", false, modSettings.enableTrace);
//HarmonyInstance.DEBUG = true;
ModInit.modLog.LogMessage($"Initializing TisButAScratch - Version {typeof(Settings).Assembly.GetName().Version}");
if (settingsException != null)
{
ModInit.modLog?.Error?.Write($"EXCEPTION while reading settings file! Error was: {settingsException}");
}
ModInit.modLog?.Info?.Write($"Initializing TisButAScratch - Version {typeof(Settings).Assembly.GetName().Version}");
PilotInjuryManager.ManagerInstance.Initialize();
PilotInjuryHolder.HolderInstance.Initialize();
var harmony = HarmonyInstance.Create(HarmonyPackage);
Expand Down
29 changes: 14 additions & 15 deletions Patches/BleedingOut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static void Prefix(AbstractActor __instance, string sourceID, int stackIt
var pKey = p.FetchGUID();
if (__instance.GetStaticUnitTags().Contains(ModInit.modSettings.disableTBASTag) || (ModInit.modSettings.disableTBASTroopers && __instance.UnitIsTrooperSquad()))
{
ModInit.modLog.LogMessage(
ModInit.modLog?.Info?.Write(
$"[OnActivationEnd] {__instance.GetPilot().Callsign}_{pKey} has {ModInit.modSettings.disableTBASTag} or disableTBASTroopers {ModInit.modSettings.disableTBASTroopers}, not processing TBAS injuries.");
return;
}
Expand All @@ -57,7 +57,7 @@ public static void Prefix(AbstractActor __instance, string sourceID, int stackIt
}
}

ModInit.modLog.LogMessage(
ModInit.modLog?.Trace?.Write(
$"Actor {p.Callsign} {pKey} ending turn.");
var effects = __instance.Combat.EffectManager.GetAllEffectsTargeting(__instance);
if (effects.Count == 0) return;
Expand All @@ -66,7 +66,7 @@ public static void Prefix(AbstractActor __instance, string sourceID, int stackIt
{
if (effect.EffectData?.Description?.Id == null)
{
ModInit.modLog.LogMessage(
ModInit.modLog?.Trace?.Write(
$"Effect {effect?.EffectData} had null description");
continue;
}
Expand All @@ -82,23 +82,23 @@ public static void Prefix(AbstractActor __instance, string sourceID, int stackIt
var baseRate = p.GetBleedingRate();
var multi = p.GetBleedingRateMulti();
var bleedRate = baseRate * multi;
ModInit.modLog.LogMessage(
ModInit.modLog?.Info?.Write(
$"OnActivationEnd: {p.Callsign}_{pKey} bleeding out at rate of {bleedRate}/activation from base {baseRate} * multi {multi}!");
var bloodBank = p.GetBloodBank();
var bloodCap = p.GetBloodCapacity();
if (bloodBank > bloodCap)
{
ModInit.modLog.LogMessage(
ModInit.modLog?.Info?.Write(
$"OnActivationEnd: {p.Callsign}_{pKey} bloodbank {bloodBank} > blood capacity {bloodCap}. Setting bloodbank to blood capacity before bleeding continues.");
p.SetBloodBank(bloodCap);
bloodBank = p.GetBloodBank();
}

ModInit.modLog.LogMessage(
ModInit.modLog?.Info?.Write(
$"{p.Callsign}_{pKey}: Current bloodBank at {bloodBank}!");
var newbloodBank = bloodBank - bleedRate;
p.SetBloodBank(newbloodBank);
ModInit.modLog.LogMessage(
ModInit.modLog?.Info?.Write(
$"{p.Callsign}_{pKey}: BloodBank set to {p.GetBloodBank()}");

if (newbloodBank <= 0)
Expand All @@ -107,7 +107,7 @@ public static void Prefix(AbstractActor __instance, string sourceID, int stackIt

p.StatCollection.ModifyStat<bool>("TBAS_Injuries", 0, "BledOut",
StatCollection.StatOperation.Set, true);
ModInit.modLog.LogMessage(
ModInit.modLog?.Info?.Write(
$"{p.Callsign}_{pKey} has bled out!");

__instance.FlagForDeath("Bled Out", DeathMethod.PilotKilled, DamageType.Unknown, 1, 1, p.FetchGUID(), false);
Expand Down Expand Up @@ -161,7 +161,7 @@ public static void Postfix(CombatHUD __instance, AbstractActor actor)
{
if (effect.EffectData?.Description?.Id == null)
{
ModInit.modLog.LogMessage(
ModInit.modLog?.Trace?.Write(
$"Effect {effect?.EffectData} had null description");
continue;
}
Expand All @@ -177,11 +177,11 @@ public static void Postfix(CombatHUD __instance, AbstractActor actor)
var baseRate = p.GetBleedingRate();
var multi = p.GetBleedingRateMulti();
var bleedRate = baseRate * multi;
ModInit.modLog.LogMessage(
ModInit.modLog?.Trace?.Write(
$"OnActorSelected: {p.Callsign}_{pKey} bleeding out at rate of {bleedRate}/activation from base {baseRate} * multi {multi}!");
var durationInfo = Mathf.CeilToInt(p.GetBloodBank() / (bleedRate) -1);

ModInit.modLog.LogMessage(
ModInit.modLog?.Trace?.Write(
$"At OnActorSelected: Found bleeding effect(s) for {actor.GetPilot().Callsign}, processing time to bleedout for display: {durationInfo} activations remain");

var eject = "";
Expand Down Expand Up @@ -231,8 +231,7 @@ public static void Postfix(CombatHUDStatusPanel __instance, ref Text __result, E
if (!(theInstance.DisplayedCombatant is AbstractActor actor)) return;
if (effect?.Description?.Id == null)
{
ModInit.modLog.LogTrace(
$"Effect {effect} had null description");
ModInit.modLog?.Trace?.Write($"Effect {effect} had null description");
return;
}
var p = actor.GetPilot();
Expand Down Expand Up @@ -260,11 +259,11 @@ public static void Postfix(CombatHUDStatusPanel __instance, ref Text __result, E
var baseRate = p.GetBleedingRate();
var multi = p.GetBleedingRateMulti();
var bleedRate = baseRate * multi;
ModInit.modLog.LogTrace(
ModInit.modLog?.Trace?.Write(
$"ProcessDetailString: {p.Callsign}_{pKey} bleeding out at rate of {bleedRate}/activation from base {baseRate} * multi {multi}!");
var durationInfo = Mathf.CeilToInt(p.GetBloodBank() / (bleedRate) - 1);

ModInit.modLog.LogTrace(
ModInit.modLog?.Trace?.Write(
$"At ProcessDetailString: Found bleeding effect(s) for {actor.GetPilot().Callsign}, processing time to bleedout for display: {durationInfo} activations remain");
var tgtEffect = effectsList.FirstOrDefault(x => x.EffectData == effect);
if (tgtEffect == null) return;
Expand Down
6 changes: 3 additions & 3 deletions Patches/FloatyPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> inst
if (instruction.opcode == OpCodes.Ldstr && "{0}: PILOT INJURED".Equals((string)instruction.operand, StringComparison.InvariantCultureIgnoreCase))
{
injuryStrIdx = i;
ModInit.modLog.LogMessage($"Found PILOT INJURED instruction at idx: {i}");
ModInit.modLog?.Info?.Write($"Found PILOT INJURED instruction at idx: {i}");
}
else if (instruction.opcode == OpCodes.Callvirt && (MethodInfo)instruction.operand == clearNeedsInjuryMI)
{
clearInjuryIdx = i;
ModInit.modLog.LogMessage($"Found Pilot.ClearNeedsInjury instruction at idx: {i}");
ModInit.modLog?.Info?.Write($"Found Pilot.ClearNeedsInjury instruction at idx: {i}");
}
}

Expand Down Expand Up @@ -64,7 +64,7 @@ static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> inst
if (instruction.opcode == OpCodes.Ldstr && "KNOCKDOWN: PILOT INJURED".Equals((string)instruction.operand, StringComparison.InvariantCultureIgnoreCase))
{
targetIdx = i;
ModInit.modLog.LogMessage($"KNOCKDOWN: PILOT INJURED instruction at idx: {i}");
ModInit.modLog?.Info?.Write($"KNOCKDOWN: PILOT INJURED instruction at idx: {i}");
}
}
codes.RemoveRange(targetIdx - 4, 12);
Expand Down
Loading

0 comments on commit 7d999fc

Please sign in to comment.