Skip to content

Commit

Permalink
Merge branch 'combat_log_line' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
quisquous committed Nov 2, 2022
2 parents 244ee2f + 5372392 commit e5d19f9
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 85 deletions.
108 changes: 43 additions & 65 deletions OverlayPlugin.Core/EventSources/EnmityEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
using RainbowMage.OverlayPlugin.MemoryProcessors.Aggro;
using RainbowMage.OverlayPlugin.MemoryProcessors.EnmityHud;
using RainbowMage.OverlayPlugin.MemoryProcessors.Target;
using static RainbowMage.OverlayPlugin.MemoryProcessors.InCombat.LineInCombat;

namespace RainbowMage.OverlayPlugin.EventSources
{
public class EnmityEventSource : EventSourceBase
{
private IInCombatMemory inCombatMemory;
private ICombatantMemory combatantMemory;
private ITargetMemory targetMemory;
private IEnmityMemory enmityMemory;
Expand All @@ -43,20 +43,16 @@ internal class InCombatDataObject
public bool inACTCombat = false;
public bool inGameCombat = false;
};
InCombatDataObject sentCombatData;

// Unlike "sentCombatData" which caches sent data, this variable caches each update.
private bool lastInGameCombat = false;
private const int endEncounterOutOfCombatDelayMs = 5000;
CancellationTokenSource endEncounterToken;

public BuiltinEventConfig Config { get; set; }

public event EventHandler<CombatStatusChangedArgs> CombatStatusChanged;
public event Action EnmityTick;

public EnmityEventSource(TinyIoCContainer container) : base(container)
{
inCombatMemory = container.Resolve<IInCombatMemory>();
combatantMemory = container.Resolve<ICombatantMemory>();
targetMemory = container.Resolve<ITargetMemory>();
enmityMemory = container.Resolve<IEnmityMemory>();
Expand All @@ -67,6 +63,12 @@ public EnmityEventSource(TinyIoCContainer container) : base(container)
EnmityTargetDataEvent, EnmityAggroListEvent, TargetableEnemiesEvent
});
RegisterCachedEventType(InCombatEvent);

var lineInCombat = container.Resolve<LineInCombat>();
lineInCombat.OnInCombatChanged += OnInCombatChanged;

EnmityTick += UpdateEnmity;
EnmityTick += lineInCombat.Update;
}

public override Control CreateConfigControl()
Expand All @@ -93,20 +95,24 @@ public override void SaveConfig(IPluginConfig config)
{
}

private void UpdateInCombat()
private void OnInCombatChanged(object sender, InCombatArgs args)
{
if (!inCombatMemory.IsValid())
return;
var combatData = new InCombatDataObject();
combatData.inACTCombat = args.InACTCombat;
combatData.inGameCombat = args.InGameCombat;
DispatchAndCacheEvent(JObject.FromObject(combatData));

// Handle optional "end encounter of combat" logic.
bool inGameCombat = inCombatMemory.GetInCombat();
if (inGameCombat != lastInGameCombat)
if (!args.InGameCombatChanged)
{
logger.Log(LogLevel.Debug, inGameCombat ? "Entered combat" : "Left combat");
return;
}

// Handle optional "end encounter of combat" logic.
bool inGameCombat = args.InGameCombat;
logger.Log(LogLevel.Debug, inGameCombat ? "Entered combat" : "Left combat");

// If we've transitioned to being out of combat, start a delayed task to end the ACT encounter.
if (Config.EndEncounterOutOfCombat && lastInGameCombat && !inGameCombat)
if (Config.EndEncounterOutOfCombat && !inGameCombat)
{
endEncounterToken = new CancellationTokenSource();
Task.Run(async delegate
Expand All @@ -125,24 +131,6 @@ private void UpdateInCombat()
endEncounterToken.Cancel();
endEncounterToken = null;
}
if (lastInGameCombat != inGameCombat)
{
CombatStatusChanged?.Invoke(this, new CombatStatusChangedArgs(inGameCombat));
}
lastInGameCombat = inGameCombat;

if (HasSubscriber(InCombatEvent))
{
bool inACTCombat = Advanced_Combat_Tracker.ActGlobals.oFormActMain.InCombat;
if (sentCombatData == null || sentCombatData.inACTCombat != inACTCombat || sentCombatData.inGameCombat != inGameCombat)
{
if (sentCombatData == null)
sentCombatData = new InCombatDataObject();
sentCombatData.inACTCombat = inACTCombat;
sentCombatData.inGameCombat = inGameCombat;
DispatchAndCacheEvent(JObject.FromObject(sentCombatData));
}
}
}

private void UpdateEnmity()
Expand All @@ -153,35 +141,30 @@ private void UpdateEnmity()
if (!targetData && !aggroList && !targetableEnemies)
return;

var combatants = combatantMemory.GetCombatantList();

combatants.RemoveAll((c) => c.Type != ObjectType.PC && c.Type != ObjectType.Monster);

if (targetData)
{
// See CreateTargetData() below
DispatchEvent(CreateTargetData(combatants));
}
if (aggroList)
{
DispatchEvent(CreateAggroList(combatants));
}
if (targetableEnemies)
{
DispatchEvent(CreateTargetableEnemyList(combatants));
}
}

protected override void Update()
{
try
{
#if TRACE
var stopwatch = new Stopwatch();
stopwatch.Start();
#endif
UpdateInCombat();
UpdateEnmity();

var combatants = combatantMemory.GetCombatantList();

combatants.RemoveAll((c) => c.Type != ObjectType.PC && c.Type != ObjectType.Monster);

if (targetData)
{
// See CreateTargetData() below
DispatchEvent(CreateTargetData(combatants));
}
if (aggroList)
{
DispatchEvent(CreateAggroList(combatants));
}
if (targetableEnemies)
{
DispatchEvent(CreateTargetableEnemyList(combatants));
}
#if TRACE
Log(LogLevel.Trace, "UpdateEnmity: {0}ms", stopwatch.ElapsedMilliseconds);
#endif
Expand All @@ -192,6 +175,11 @@ protected override void Update()
}
}

protected override void Update()
{
EnmityTick.Invoke();
}

[Serializable]
internal class EnmityTargetDataObject
{
Expand Down Expand Up @@ -321,16 +309,6 @@ public List<TargetableEnemyEntry> GetTargetableEnemyList(List<Combatant> combata
}
}

public class CombatStatusChangedArgs : EventArgs
{
public bool InCombat { get; private set; }

public CombatStatusChangedArgs(bool status)
{
InCombat = status;
}
}

[Serializable]
public class TargetableEnemyEntry
{
Expand Down
38 changes: 34 additions & 4 deletions OverlayPlugin.Core/EventSources/MiniParseEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,43 @@ private List<Dictionary<string, object>> GetCombatants(List<uint> ids, List<stri
return filteredCombatants;
}

private void StopACTCombat()
{
ActGlobals.oFormActMain.Invoke((Action)(() =>
{
ActGlobals.oFormActMain.EndCombat(true);
}));
}

private void LogLineHandler(bool isImport, LogLineEventArgs args)
{
if (isImport)
{
try
{
var line = args.originalLogLine.Split('|');

if (int.TryParse(line[0], out int lineTypeInt))
{
// If an imported log has split the encounter, also split it while importing.
// TODO: should we also consider the current user's wipe config option here for splitting,
// even if the original log writer did not have it set to true?
LogMessageType lineType = (LogMessageType)lineTypeInt;
if (lineType == LogMessageType.InCombat)
{
var inACTCombat = Convert.ToUInt32(line[2]);
if (inACTCombat == 0)
{
StopACTCombat();
}
}
}
}
catch
{
return;
}

lock (importedLogs)
{
importedLogs.Add(args.originalLogLine);
Expand Down Expand Up @@ -441,10 +474,7 @@ private void LogLineHandler(bool isImport, LogLineEventArgs args)
// When CN/KR is on 6.2, this can be removed.
if (line[3] == "40000010" || line[3] == "4000000F")
{
ActGlobals.oFormActMain.Invoke((Action)(() =>
{
ActGlobals.oFormActMain.EndCombat(true);
}));
StopACTCombat();
}
break;
}
Expand Down
13 changes: 12 additions & 1 deletion OverlayPlugin.Core/Integration/FFXIVRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ public enum LogMessageType
PacketDump,
Version,
Error,
Timer
Timer,
// OverlayPlugin lines
RegisterLogLine = 256,
MapEffect,
FateDirector,
CEDirector,
InCombat,
}

public enum GameRegion
Expand Down Expand Up @@ -462,5 +468,10 @@ public void RegisterProcessChangedHandler(Action<Process> handler)
}
}
}

public DateTime GetServerTimestamp()
{
return GetRepository()?.GetServerTimestamp() ?? DateTime.Now;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RainbowMage.OverlayPlugin.MemoryProcessors.InCombat;
using RainbowMage.OverlayPlugin.NetworkProcessors;
using RainbowMage.OverlayPlugin.Updater;

namespace RainbowMage.OverlayPlugin.NetworkProcessors
namespace RainbowMage.OverlayPlugin
{
using Opcodes = Dictionary<string, Dictionary<string, OpcodeConfigEntry>>;

Expand All @@ -17,6 +19,7 @@ public OverlayPluginLogLines(TinyIoCContainer container)
container.Register(new LineMapEffect(container));
container.Register(new LineFateControl(container));
container.Register(new LineCEDirector(container));
container.Register(new LineInCombat(container));
}
}

Expand Down
20 changes: 15 additions & 5 deletions OverlayPlugin.Core/Integration/UnstableNewLogLines.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
using Advanced_Combat_Tracker;
using Markdig.Helpers;
using RainbowMage.OverlayPlugin.EventSources;
using RainbowMage.OverlayPlugin.MemoryProcessors.InCombat;
using RainbowMage.OverlayPlugin.NetworkProcessors;
using static RainbowMage.OverlayPlugin.MemoryProcessors.InCombat.LineInCombat;

namespace RainbowMage.OverlayPlugin.Integration
{
Expand All @@ -22,6 +24,7 @@ public class UnstableNewLogLines
private string logPath = null;
private ConcurrentQueue<string> logQueue = null;
private Thread logThread = null;
private LineInCombat lineInCombat = null;

public UnstableNewLogLines(TinyIoCContainer container)
{
Expand All @@ -30,8 +33,10 @@ public UnstableNewLogLines(TinyIoCContainer container)
enmitySource = container.Resolve<EnmityEventSource>();
logger = container.Resolve<ILogger>();
logPath = Path.GetDirectoryName(ActGlobals.oFormActMain.LogFilePath) + "_OverlayPlugin.log";
var config = container.Resolve<BuiltinEventConfig>();
lineInCombat = container.Resolve<LineInCombat>();


var config = container.Resolve<BuiltinEventConfig>();
config.LogLinesChanged += (o, e) =>
{
if (config.LogLines)
Expand All @@ -53,7 +58,7 @@ public UnstableNewLogLines(TinyIoCContainer container)
public void Enable()
{
parser.OnOnlineStatusChanged += OnOnlineStatusChange;
enmitySource.CombatStatusChanged += OnCombatStatusChange;
lineInCombat.OnInCombatChanged += OnCombatStatusChange;

logThread = new Thread(new ThreadStart(WriteBackgroundLog));
logThread.IsBackground = true;
Expand All @@ -63,7 +68,7 @@ public void Enable()
public void Disable()
{
parser.OnOnlineStatusChanged -= OnOnlineStatusChange;
enmitySource.CombatStatusChanged -= OnCombatStatusChange;
lineInCombat.OnInCombatChanged -= OnCombatStatusChange;
logQueue?.Enqueue(null);
}

Expand Down Expand Up @@ -134,10 +139,15 @@ private void OnOnlineStatusChange(object sender, OnlineStatusChangedArgs ev)
}
}

private void OnCombatStatusChange(object sender, CombatStatusChangedArgs ev)
private void OnCombatStatusChange(object sender, InCombatArgs ev)
{
if (!ev.InGameCombatChanged)
{
return;
}

string msg;
if (ev.InCombat)
if (ev.InGameCombat)
{
msg = "Entered combat";
}
Expand Down
Loading

0 comments on commit e5d19f9

Please sign in to comment.