Fix submarine tracker integration

This commit is contained in:
Liza 2024-06-18 21:58:29 +02:00
parent f2b51b38c2
commit 090d1f8dca
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">
<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<Version>0.17</Version>
<Version>0.18</Version>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>

View File

@ -50,7 +50,7 @@ internal sealed class InfluxPlugin : IDalamudPlugin
_pluginLog = pluginLog;
DalamudReflector dalamudReflector = new DalamudReflector(pluginInterface, framework, pluginLog);
_allaganToolsIpc = new AllaganToolsIpc(pluginInterface, chatGui, dalamudReflector, framework, _pluginLog);
_submarineTrackerIpc = new SubmarineTrackerIpc(dalamudReflector, chatGui, pluginLog);
_submarineTrackerIpc = new SubmarineTrackerIpc(dalamudReflector);
_localStatsCalculator =
new LocalStatsCalculator(pluginInterface, clientState, addonLifecycle, pluginLog, dataManager);
_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);
Type type = @delegate.GetType();
Name = (string)type.GetProperty("Name")!.GetValue(@delegate)!;
Level = (ushort)type.GetProperty("Rank")!.GetValue(@delegate)!;
FreeCompanyId = (ulong)type.GetField("FreeCompanyId")!.GetValue(@delegate)!;
Name = (string)type.GetField("Name")!.GetValue(@delegate)!;
Level = (ushort)type.GetField("Rank")!.GetValue(@delegate)!;
Build = new Build(type.GetProperty("Build")!.GetValue(@delegate)!);
ReturnTime = (DateTime)type.GetField("ReturnTime")!.GetValue(@delegate)!;
try
{
@ -28,8 +30,6 @@ internal sealed class Submarine
(uint predictedLevel, double _) = ((uint, double))type.GetMethod("PredictExpGrowth")!.Invoke(@delegate, Array.Empty<object?>())!;
PredictedLevel = (ushort)predictedLevel;
}
ReturnTime = (DateTime)type.GetField("ReturnTime")!.GetValue(@delegate)!;
}
catch (Exception)
{
@ -37,6 +37,7 @@ internal sealed class Submarine
}
}
public ulong FreeCompanyId { get; }
public string Name { get; }
public ushort Level { get; }
public ushort PredictedLevel { get; }

View File

@ -3,8 +3,8 @@ using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using Dalamud.Plugin;
using Dalamud.Plugin.Services;
using Influx.AllaganTools;
using LLib;
@ -13,14 +13,10 @@ namespace Influx.SubmarineTracker;
internal sealed class SubmarineTrackerIpc
{
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;
_chatGui = chatGui;
_pluginLog = pluginLog;
}
[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))
{
var submarineData = it.GetType().Assembly.GetType("SubmarineTracker.Data.Submarines");
var knownSubmarineData = submarineData!.GetField("KnownSubmarines")!;
return ((IEnumerable)knownSubmarineData.GetValue(null)!).Cast<object>()
.Select(x => new
{
OwnerId = (ulong)x.GetType().GetProperty("Key")!.GetValue(x)!,
FcWrapper = x.GetType().GetProperty("Value")!.GetValue(x)!
})
.Select(x => new
{
Owner = characters.FirstOrDefault(y => y.CharacterId == x.OwnerId),
Subs = new FcSubmarines(x.FcWrapper).Submarines,
})
.Where(x => x.Owner != null)
.Select(x => new
{
x.Subs,
Fc = characters.FirstOrDefault(y => y.CharacterId == x.Owner!.FreeCompanyId)
})
var databaseCache = it.GetType()
.GetField("DatabaseCache", BindingFlags.Static | BindingFlags.Public)!
.GetValue(null)!;
var getSubmarines = databaseCache.GetType()
.GetMethod("GetSubmarines", [])!;
var knownSubmarineData = ((IEnumerable)getSubmarines.Invoke(databaseCache, [])!).Cast<object>();
return knownSubmarineData
.Select(x => new Submarine(x))
.GroupBy(x => x.FreeCompanyId)
.Select(x => new SubmarineInfo(
characters.SingleOrDefault(y =>
y.CharacterType == CharacterType.FreeCompanyChest && y.CharacterId == x.Key),
x.ToList()
))
.Where(x => x.Fc != null)
.Select(x => new SubmarineInfo(x.Fc!, 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;
});
.ToDictionary(x => x.Fc!, x => x.Subs);
}
else
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())
{
}
private static SubmarineStats Convert(Character fc, int index, Submarine y)
private static SubmarineStats Convert(Character? fc, int index, Submarine y)
{
return new SubmarineStats
{
Id = index,
Name = y.Name,
WorldId = fc.WorldId,
WorldId = fc?.WorldId ?? 0,
Level = y.Level,
PredictedLevel = y.PredictedLevel,
Hull = y.Build.HullIdentifier,