feat(client): switch to hashes for primary keys

This commit is contained in:
Anna 2023-10-07 04:32:54 -04:00
parent b19aa5247c
commit 03c67fa26c
Signed by: anna
GPG Key ID: D0943384CD9F87D1
6 changed files with 123 additions and 14 deletions

10
client/Configuration.cs Normal file
View File

@ -0,0 +1,10 @@
using Dalamud.Configuration;
namespace PlayerMap;
[Serializable]
public class Configuration : IPluginConfiguration {
public int Version { get; set; } = 1;
public int UpdateFrequency { get; set; } = 5;
}

20
client/OnDispose.cs Normal file
View File

@ -0,0 +1,20 @@
namespace PlayerMap;
internal class OnDispose : IDisposable {
private Action Action { get; }
private bool _disposed;
internal OnDispose(Action action) {
this.Action = action;
}
public void Dispose() {
if (this._disposed) {
return;
}
this._disposed = true;
this.Action();
}
}

View File

@ -31,10 +31,6 @@
<HintPath>$(DalamudLibPath)\Dalamud.dll</HintPath>
<Private>false</Private>
</Reference>
<Reference Include="FFXIVClientStructs">
<HintPath>$(DalamudLibPath)\FFXIVClientStructs.dll</HintPath>
<Private>false</Private>
</Reference>
<Reference Include="ImGui.NET">
<HintPath>$(DalamudLibPath)\ImGui.NET.dll</HintPath>
<Private>false</Private>
@ -54,6 +50,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Blake3" Version="0.6.1" />
<PackageReference Include="DalamudPackager" Version="2.1.12"/>
<PackageReference Include="MessagePack" Version="2.5.129"/>
</ItemGroup>

View File

@ -1,6 +1,8 @@
using System.Diagnostics;
using System.IO.Compression;
using System.Net.Http.Headers;
using System.Text;
using Blake3;
using Dalamud.Game.ClientState.Objects.Enums;
using Dalamud.Game.ClientState.Objects.SubKinds;
using Dalamud.IoC;
@ -14,6 +16,9 @@ public class Plugin : IDalamudPlugin {
[PluginService]
internal static IPluginLog Log { get; private set; }
[PluginService]
internal DalamudPluginInterface Interface { get; init; }
[PluginService]
internal IFramework Framework { get; init; }
@ -23,20 +28,35 @@ public class Plugin : IDalamudPlugin {
[PluginService]
internal IClientState ClientState { get; init; }
internal Configuration Config { get; }
internal PluginUi Ui { get; }
private HttpClient Http { get; } = new();
private Stopwatch Stopwatch { get; } = new();
private Blake3HashAlgorithm Hasher { get; } = new();
public Plugin() {
this.Hasher.Initialize();
this.Framework!.Update += this.FrameworkUpdate;
this.Config = this.Interface!.GetPluginConfig() as Configuration ?? new Configuration();
this.Ui = new PluginUi(this);
this.Stopwatch.Start();
}
public void Dispose() {
this.Ui.Dispose();
this.Framework.Update -= this.FrameworkUpdate;
this.Hasher.Dispose();
}
internal void SaveConfig() {
this.Interface.SavePluginConfig(this.Config);
}
private void FrameworkUpdate(IFramework framework) {
if (this.Stopwatch.Elapsed < TimeSpan.FromSeconds(5)) {
if (this.Stopwatch.Elapsed < TimeSpan.FromSeconds(5) || this.ClientState.LocalPlayer is not { } localPlayer) {
return;
}
@ -46,17 +66,25 @@ public class Plugin : IDalamudPlugin {
var players = this.ObjectTable
.Where(obj => obj.ObjectKind == ObjectKind.Player && obj is PlayerCharacter)
.Cast<PlayerCharacter>()
.Where(player => player.IsValid() && player.Level > 0)
.Where(
player => player.IsValid()
&& player.Level > 0
&& player.HomeWorld.Id != 65535
&& player.Name.TextValue.Trim().Length > 0
)
.Select(player => {
var customizeHex = string.Join("", player.Customize.Select(x => x.ToString("x2")));
var key = $"don't be creepy-{player.Name.TextValue.Trim()}-{player.HomeWorld.Id}-{customizeHex}";
var hash = this.Hasher.ComputeHash(Encoding.UTF8.GetBytes(key));
var pos = player.Position;
return new PlayerInfo(
player.Name.TextValue,
hash,
player.HomeWorld.Id,
pos.X,
pos.Y,
pos.Z,
player.Rotation,
player.Customize.ToList(),
player.Customize,
player.Level,
player.ClassJob.Id,
player.CompanyTag.TextValue,
@ -66,7 +94,7 @@ public class Plugin : IDalamudPlugin {
})
.ToList();
if (players.Count == 0 || this.ClientState.LocalPlayer is not { } player) {
if (players.Count == 0) {
Log.Verbose("no players to upload");
return;
}
@ -75,7 +103,7 @@ public class Plugin : IDalamudPlugin {
// shuffle so first player isn't always local player
players.Shuffle();
var update = new Update(territory, player.CurrentWorld.Id, players);
var update = new Update(territory, localPlayer.CurrentWorld.Id, players);
var msgpack = MessagePackSerializer.Serialize(update, MessagePackSerializerOptions.Standard);
using var mem = new MemoryStream();
@ -126,13 +154,13 @@ public struct Update(uint territory, uint world, List<PlayerInfo> players) {
[Serializable]
[MessagePackObject]
public struct PlayerInfo(
string name,
byte[] hash,
uint world,
float x,
float y,
float z,
float w,
List<byte> customize,
byte[] customize,
byte level,
uint job,
string freeCompany,
@ -140,7 +168,7 @@ public struct PlayerInfo(
uint maxHp
) {
[Key(0)]
public readonly string Name = name;
public readonly byte[] Hash = hash;
[Key(1)]
public readonly uint World = world;
@ -158,7 +186,7 @@ public struct PlayerInfo(
public readonly float W = w;
[Key(6)]
public readonly List<byte> Customize = customize;
public readonly byte[] Customize = customize;
[Key(7)]
public readonly byte Level = level;

48
client/PluginUi.cs Normal file
View File

@ -0,0 +1,48 @@
using ImGuiNET;
namespace PlayerMap;
internal class PluginUi : IDisposable {
private Plugin Plugin { get; }
private bool _visible;
internal PluginUi(Plugin plugin) {
this.Plugin = plugin;
this.Plugin.Interface.UiBuilder.Draw += this.Draw;
this.Plugin.Interface.UiBuilder.OpenConfigUi += this.OpenConfig;
}
public void Dispose() {
this.Plugin.Interface.UiBuilder.OpenConfigUi -= this.OpenConfig;
this.Plugin.Interface.UiBuilder.Draw -= this.Draw;
}
private void OpenConfig() {
this._visible = true;
}
private void Draw() {
if (!this._visible) {
return;
}
using var end = new OnDispose(ImGui.End);
if (!ImGui.Begin("Player Map settings", ref this._visible, ImGuiWindowFlags.AlwaysAutoResize)) {
return;
}
var anyChanged = false;
var freq = this.Plugin.Config.UpdateFrequency;
if (ImGui.DragInt("Upload frequency (secs)", ref freq, 0.05f, 1, 60)) {
anyChanged = true;
this.Plugin.Config.UpdateFrequency = freq;
}
if (anyChanged) {
this.Plugin.SaveConfig();
}
}
}

View File

@ -2,6 +2,12 @@
"version": 1,
"dependencies": {
"net7.0-windows7.0": {
"Blake3": {
"type": "Direct",
"requested": "[0.6.1, )",
"resolved": "0.6.1",
"contentHash": "YlEweiQX1iMXh9HPpoRzSKeLMuKPnMJJWTdqnP1NJV0XwwDwO7WrwLlj60pGk0vJWbeZLZOh06U70+2z0DQCsQ=="
},
"DalamudPackager": {
"type": "Direct",
"requested": "[2.1.12, )",