Use new dropbox IPC
This commit is contained in:
parent
bb11f13512
commit
83e2ea2776
@ -1,15 +1,13 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Dalamud.Game.Command;
|
||||
using Dalamud.Game.Text.SeStringHandling;
|
||||
using Dalamud.Plugin;
|
||||
using Dalamud.Plugin.Ipc;
|
||||
using Dalamud.Plugin.Services;
|
||||
using FFXIVClientStructs.FFXIV.Client.Game;
|
||||
using LLib;
|
||||
|
||||
namespace KitchenSink.Commands;
|
||||
|
||||
@ -29,12 +27,12 @@ internal sealed class DropboxQueue : IDisposable
|
||||
private readonly IChatGui _chatGui;
|
||||
private readonly DropboxApi _dropboxApi;
|
||||
|
||||
public DropboxQueue(DalamudReflector reflector, ICommandManager commandManager, IChatGui chatGui,
|
||||
public DropboxQueue(DalamudPluginInterface pluginInterface, ICommandManager commandManager, IChatGui chatGui,
|
||||
IPluginLog pluginLog)
|
||||
{
|
||||
_commandManager = commandManager;
|
||||
_chatGui = chatGui;
|
||||
_dropboxApi = new DropboxApi(reflector, pluginLog);
|
||||
_dropboxApi = new DropboxApi(pluginInterface, pluginLog);
|
||||
|
||||
_commandManager.AddHandler("/dbq", new CommandInfo(Queue)
|
||||
{
|
||||
@ -65,7 +63,7 @@ internal sealed class DropboxQueue : IDisposable
|
||||
BuildRequest(parts.Length == 2 ? parts[1] : string.Empty);
|
||||
break;
|
||||
case "clear":
|
||||
_dropboxApi.ClearQueue();
|
||||
//_dropboxApi.ClearQueue();
|
||||
break;
|
||||
default:
|
||||
AddToQueue(arguments);
|
||||
@ -135,6 +133,8 @@ internal sealed class DropboxQueue : IDisposable
|
||||
itemCount.HighQualityQuantity - highQualityQuantity);
|
||||
}
|
||||
}
|
||||
|
||||
_dropboxApi.BeginTrade();
|
||||
}
|
||||
|
||||
private IReadOnlyList<NeededItem>? ParseArguments(string arguments)
|
||||
@ -232,72 +232,30 @@ internal sealed class DropboxQueue : IDisposable
|
||||
|
||||
private sealed class DropboxApi
|
||||
{
|
||||
private readonly DalamudReflector _reflector;
|
||||
private readonly IPluginLog _pluginLog;
|
||||
private readonly ICallGateSubscriber<object> _beginTradingQueue;
|
||||
private readonly ICallGateSubscriber<uint,bool,int> _getItemQuantity;
|
||||
private readonly ICallGateSubscriber<uint,bool,int, object> _setItemQuantity;
|
||||
|
||||
public DropboxApi(DalamudReflector reflector, IPluginLog pluginLog)
|
||||
public DropboxApi(DalamudPluginInterface pluginInterface, IPluginLog pluginLog)
|
||||
{
|
||||
_reflector = reflector;
|
||||
_pluginLog = pluginLog;
|
||||
|
||||
_beginTradingQueue = pluginInterface.GetIpcSubscriber<object>("Dropbox.BeginTradingQueue");
|
||||
_getItemQuantity = pluginInterface.GetIpcSubscriber<uint, bool, int>("Dropbox.GetItemQuantity");
|
||||
_setItemQuantity = pluginInterface.GetIpcSubscriber<uint, bool, int, object>("Dropbox.SetItemQuantity");
|
||||
}
|
||||
|
||||
[SuppressMessage("Performance", "CA2000", Justification = "Should not dispose other plugin")]
|
||||
public void BeginTrade() => _beginTradingQueue.InvokeAction();
|
||||
|
||||
public void EnqueueItem(uint itemId, bool hq, int quantity)
|
||||
{
|
||||
_pluginLog.Verbose($"Preparing to queue {itemId}, {hq}, {quantity}");
|
||||
if (quantity < 0)
|
||||
return;
|
||||
|
||||
if (!TryGetItemQuantities(out IDalamudPlugin? dropboxPlugin, out IDictionary? itemQuantities))
|
||||
throw new InvalidOperationException("Could not retrieve item quantities");
|
||||
|
||||
uint[] tradeableItemIds = (uint[])dropboxPlugin.GetType()
|
||||
.GetField("TradeableItems", BindingFlags.Public | BindingFlags.Instance)!
|
||||
.GetValue(dropboxPlugin)!;
|
||||
if (!tradeableItemIds.Contains(itemId))
|
||||
{
|
||||
_pluginLog.Warning($"Item {itemId} is untradable");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
var itemDescriptorType = itemQuantities.GetType().GetGenericArguments()[0];
|
||||
var itemDescriptor = Activator.CreateInstance(itemDescriptorType, args: [itemId, hq])!;
|
||||
|
||||
var queuedQuantity = itemQuantities[itemDescriptor];
|
||||
_pluginLog.Verbose($"Retrieved quantity: {queuedQuantity}");
|
||||
if (queuedQuantity == null)
|
||||
return;
|
||||
|
||||
var boxType = queuedQuantity.GetType();
|
||||
var valueField = boxType.GetField("Value", BindingFlags.Public | BindingFlags.Instance)!;
|
||||
|
||||
_pluginLog.Information($"Adding {itemDescriptor} to queue");
|
||||
valueField.SetValue(queuedQuantity, quantity);
|
||||
}
|
||||
|
||||
public void ClearQueue()
|
||||
{
|
||||
if (!TryGetItemQuantities(out IDalamudPlugin? _, out IDictionary? itemQuantities))
|
||||
throw new InvalidOperationException("Could not retrieve item quantities");
|
||||
|
||||
itemQuantities.Clear();
|
||||
}
|
||||
|
||||
private bool TryGetItemQuantities([NotNullWhen(true)] out IDalamudPlugin? dropboxPlugin,
|
||||
[NotNullWhen(true)] out IDictionary? itemQuantities)
|
||||
{
|
||||
if (!_reflector.TryGetDalamudPlugin("Dropbox", out dropboxPlugin))
|
||||
{
|
||||
itemQuantities = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
var itemQueueUiType = dropboxPlugin.GetType().Assembly.GetType("Dropbox.ItemQueueUI")!;
|
||||
itemQuantities =
|
||||
(IDictionary?)itemQueueUiType.GetField("ItemQuantities", BindingFlags.Public | BindingFlags.Static)!
|
||||
.GetValue(null);
|
||||
return itemQuantities != null;
|
||||
int currentQuantity = _getItemQuantity.InvokeFunc(itemId, hq);
|
||||
_setItemQuantity.InvokeAction(itemId, hq, quantity + currentQuantity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0-windows</TargetFramework>
|
||||
<Version>0.4</Version>
|
||||
<Version>0.5</Version>
|
||||
<LangVersion>12</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
|
||||
|
@ -4,7 +4,6 @@ using Dalamud.Plugin;
|
||||
using Dalamud.Plugin.Services;
|
||||
using ECommons;
|
||||
using KitchenSink.Commands;
|
||||
using LLib;
|
||||
|
||||
namespace KitchenSink;
|
||||
|
||||
@ -18,15 +17,13 @@ internal sealed class KitchenSinkPlugin : IDalamudPlugin
|
||||
|
||||
public KitchenSinkPlugin(DalamudPluginInterface pluginInterface, ICommandManager commandManager,
|
||||
IClientState clientState, IChatGui chatGui, INotificationManager notificationManager, IDtrBar dtrBar,
|
||||
ICondition condition, IPluginLog pluginLog, IFramework framework)
|
||||
ICondition condition, IPluginLog pluginLog)
|
||||
{
|
||||
DalamudReflector reflector = new DalamudReflector(pluginInterface, framework, pluginLog);
|
||||
|
||||
ECommonsMain.Init(pluginInterface, this);
|
||||
_autoRetainerApi = new AutoRetainerApi();
|
||||
_characterSwitch = new CharacterSwitch(_autoRetainerApi, commandManager, clientState, chatGui,
|
||||
notificationManager, dtrBar, condition, pluginLog);
|
||||
_dropboxQueue = new DropboxQueue(reflector, commandManager, chatGui, pluginLog);
|
||||
_dropboxQueue = new DropboxQueue(pluginInterface, commandManager, chatGui, pluginLog);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
Loading…
Reference in New Issue
Block a user