Fix submarine tracker integration

This commit is contained in:
Liza 2024-06-18 21:58:29 +02:00
parent f2b51b38c2
commit cd1d14e0cc
Signed by: liza
GPG Key ID: 7199F8D727D55F67
5 changed files with 28 additions and 69 deletions

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework> <TargetFramework>net8.0-windows</TargetFramework>
<Version>0.17</Version> <Version>0.18</Version>
<LangVersion>12</LangVersion> <LangVersion>12</LangVersion>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>

View File

@ -50,7 +50,7 @@ internal sealed class InfluxPlugin : IDalamudPlugin
_pluginLog = pluginLog; _pluginLog = pluginLog;
DalamudReflector dalamudReflector = new DalamudReflector(pluginInterface, framework, pluginLog); DalamudReflector dalamudReflector = new DalamudReflector(pluginInterface, framework, pluginLog);
_allaganToolsIpc = new AllaganToolsIpc(pluginInterface, chatGui, dalamudReflector, framework, _pluginLog); _allaganToolsIpc = new AllaganToolsIpc(pluginInterface, chatGui, dalamudReflector, framework, _pluginLog);
_submarineTrackerIpc = new SubmarineTrackerIpc(dalamudReflector, chatGui, pluginLog); _submarineTrackerIpc = new SubmarineTrackerIpc(dalamudReflector);
_localStatsCalculator = _localStatsCalculator =
new LocalStatsCalculator(pluginInterface, clientState, addonLifecycle, pluginLog, dataManager); new LocalStatsCalculator(pluginInterface, clientState, addonLifecycle, pluginLog, dataManager);
_fcStatsCalculator = new FcStatsCalculator(this, pluginInterface, clientState, addonLifecycle, gameGui, _fcStatsCalculator = new FcStatsCalculator(this, pluginInterface, clientState, addonLifecycle, gameGui,

View File

@ -1,21 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Influx.SubmarineTracker;
internal sealed class FcSubmarines
{
public FcSubmarines(object @delegate)
{
ArgumentNullException.ThrowIfNull(@delegate);
Submarines = ((IEnumerable)@delegate.GetType().GetField("Submarines")!.GetValue(@delegate)!)
.Cast<object>()
.Select(x => new Submarine(x))
.ToList()
.AsReadOnly();
}
public IList<Submarine> Submarines { get; }
}

View File

@ -8,9 +8,11 @@ internal sealed class Submarine
{ {
ArgumentNullException.ThrowIfNull(@delegate); ArgumentNullException.ThrowIfNull(@delegate);
Type type = @delegate.GetType(); Type type = @delegate.GetType();
Name = (string)type.GetProperty("Name")!.GetValue(@delegate)!; FreeCompanyId = (ulong)type.GetField("FreeCompanyId")!.GetValue(@delegate)!;
Level = (ushort)type.GetProperty("Rank")!.GetValue(@delegate)!; Name = (string)type.GetField("Name")!.GetValue(@delegate)!;
Level = (ushort)type.GetField("Rank")!.GetValue(@delegate)!;
Build = new Build(type.GetProperty("Build")!.GetValue(@delegate)!); Build = new Build(type.GetProperty("Build")!.GetValue(@delegate)!);
ReturnTime = (DateTime)type.GetField("ReturnTime")!.GetValue(@delegate)!;
try try
{ {
@ -28,8 +30,6 @@ internal sealed class Submarine
(uint predictedLevel, double _) = ((uint, double))type.GetMethod("PredictExpGrowth")!.Invoke(@delegate, Array.Empty<object?>())!; (uint predictedLevel, double _) = ((uint, double))type.GetMethod("PredictExpGrowth")!.Invoke(@delegate, Array.Empty<object?>())!;
PredictedLevel = (ushort)predictedLevel; PredictedLevel = (ushort)predictedLevel;
} }
ReturnTime = (DateTime)type.GetField("ReturnTime")!.GetValue(@delegate)!;
} }
catch (Exception) catch (Exception)
{ {
@ -37,6 +37,7 @@ internal sealed class Submarine
} }
} }
public ulong FreeCompanyId { get; }
public string Name { get; } public string Name { get; }
public ushort Level { get; } public ushort Level { get; }
public ushort PredictedLevel { get; } public ushort PredictedLevel { get; }

View File

@ -3,8 +3,8 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
using System.Reflection;
using Dalamud.Plugin; using Dalamud.Plugin;
using Dalamud.Plugin.Services;
using Influx.AllaganTools; using Influx.AllaganTools;
using LLib; using LLib;
@ -13,14 +13,10 @@ namespace Influx.SubmarineTracker;
internal sealed class SubmarineTrackerIpc internal sealed class SubmarineTrackerIpc
{ {
private readonly DalamudReflector _dalamudReflector; private readonly DalamudReflector _dalamudReflector;
private readonly IChatGui _chatGui;
private readonly IPluginLog _pluginLog;
public SubmarineTrackerIpc(DalamudReflector dalamudReflector, IChatGui chatGui, IPluginLog pluginLog) public SubmarineTrackerIpc(DalamudReflector dalamudReflector)
{ {
_dalamudReflector = dalamudReflector; _dalamudReflector = dalamudReflector;
_chatGui = chatGui;
_pluginLog = pluginLog;
} }
[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")]
@ -28,58 +24,41 @@ internal sealed class SubmarineTrackerIpc
{ {
if (_dalamudReflector.TryGetDalamudPlugin("Submarine Tracker", out IDalamudPlugin? it, false, true)) if (_dalamudReflector.TryGetDalamudPlugin("Submarine Tracker", out IDalamudPlugin? it, false, true))
{ {
var submarineData = it.GetType().Assembly.GetType("SubmarineTracker.Data.Submarines"); var databaseCache = it.GetType()
var knownSubmarineData = submarineData!.GetField("KnownSubmarines")!; .GetField("DatabaseCache", BindingFlags.Static | BindingFlags.Public)!
return ((IEnumerable)knownSubmarineData.GetValue(null)!).Cast<object>() .GetValue(null)!;
.Select(x => new var getSubmarines = databaseCache.GetType()
{ .GetMethod("GetSubmarines", [])!;
OwnerId = (ulong)x.GetType().GetProperty("Key")!.GetValue(x)!, var knownSubmarineData = ((IEnumerable)getSubmarines.Invoke(databaseCache, [])!).Cast<object>();
FcWrapper = x.GetType().GetProperty("Value")!.GetValue(x)! return knownSubmarineData
}) .Select(x => new Submarine(x))
.Select(x => new .GroupBy(x => x.FreeCompanyId)
{ .Select(x => new SubmarineInfo(
Owner = characters.FirstOrDefault(y => y.CharacterId == x.OwnerId), characters.SingleOrDefault(y =>
Subs = new FcSubmarines(x.FcWrapper).Submarines, y.CharacterType == CharacterType.FreeCompanyChest && y.CharacterId == x.Key),
}) x.ToList()
.Where(x => x.Owner != null) ))
.Select(x => new
{
x.Subs,
Fc = characters.FirstOrDefault(y => y.CharacterId == x.Owner!.FreeCompanyId)
})
.Where(x => x.Fc != null) .Where(x => x.Fc != null)
.Select(x => new SubmarineInfo(x.Fc!, x.Subs)) .ToDictionary(x => x.Fc!, x => x.Subs);
.GroupBy(x => x.Fc)
.ToDictionary(x => x.Key, x =>
{
if (x.Count() != 1)
{
_chatGui.PrintError($"[Influx] Unable to collect data, FC '{x.Key.Name}' is included in statistics through multiple characters/owners.");
var characterNames = characters.Where(y => y.FreeCompanyId == x.Key.CharacterId).Select(y => y.Name).ToList();
throw new InvalidOperationException($"Unable to collect FC data for FC '{x.Key}'{Environment.NewLine}Multiple characters include the same FC ({string.Join(", ", characterNames)}), only one of them should have 'Include Free Company Statistics' set");
}
return x.Single().Subs;
});
} }
else else
return new Dictionary<Character, List<SubmarineStats>>(); return new Dictionary<Character, List<SubmarineStats>>();
} }
private sealed record SubmarineInfo(Character Fc, List<SubmarineStats> Subs) private sealed record SubmarineInfo(Character? Fc, List<SubmarineStats> Subs)
{ {
public SubmarineInfo(Character fc, IList<Submarine> subs) public SubmarineInfo(Character? fc, List<Submarine> subs)
: this(fc, subs.Select(x => Convert(fc, subs.IndexOf(x), x)).ToList()) : this(fc, subs.Select(x => Convert(fc, subs.IndexOf(x), x)).ToList())
{ {
} }
private static SubmarineStats Convert(Character fc, int index, Submarine y) private static SubmarineStats Convert(Character? fc, int index, Submarine y)
{ {
return new SubmarineStats return new SubmarineStats
{ {
Id = index, Id = index,
Name = y.Name, Name = y.Name,
WorldId = fc.WorldId, WorldId = fc?.WorldId ?? 0,
Level = y.Level, Level = y.Level,
PredictedLevel = y.PredictedLevel, PredictedLevel = y.PredictedLevel,
Hull = y.Build.HullIdentifier, Hull = y.Build.HullIdentifier,