Dalamud/Dalamud/Game/Marketboard/MarketBoard.cs
Haselnussbomber c93f04f0e4
Code cleanup (#2439)
* Use new Lock objects

* Fix CA1513: Use ObjectDisposedException.ThrowIf

* Fix CA1860: Avoid using 'Enumerable.Any()' extension method

* Fix IDE0028: Use collection initializers or expressions

* Fix CA2263: Prefer generic overload when type is known

* Fix CA1862: Use the 'StringComparison' method overloads to perform case-insensitive string comparisons

* Fix IDE0270: Null check can be simplified

* Fix IDE0280: Use 'nameof'

* Fix IDE0009: Add '.this'

* Fix IDE0007: Use 'var' instead of explicit type

* Fix IDE0062: Make local function static

* Fix CA1859: Use concrete types when possible for improved performance

* Fix IDE0066: Use switch expression

Only applied to where it doesn't look horrendous.

* Use is over switch

* Fix CA1847: Use String.Contains(char) instead of String.Contains(string) with single characters

* Fix SYSLIB1045: Use 'GeneratedRegexAttribute' to generate the regular expression implementation at compile-time.

* Fix CA1866: Use 'string.EndsWith(char)' instead of 'string.EndsWith(string)' when you have a string with a single char

* Fix IDE0057: Substring can be simplified

* Fix IDE0059: Remove unnecessary value assignment

* Fix CA1510: Use ArgumentNullException throw helper

* Fix IDE0300: Use collection expression for array

* Fix IDE0250: Struct can be made 'readonly'

* Fix IDE0018: Inline variable declaration

* Fix CA1850: Prefer static HashData method over ComputeHash

* Fi CA1872: Prefer 'Convert.ToHexString' and 'Convert.ToHexStringLower' over call chains based on 'BitConverter.ToString'

* Update ModuleLog instantiations

* Organize usings
2026-01-06 08:36:55 -08:00

232 lines
7.1 KiB
C#

using Dalamud.Game.Network.Internal;
using Dalamud.Game.Network.Structures;
using Dalamud.IoC;
using Dalamud.IoC.Internal;
using Dalamud.Logging.Internal;
using Dalamud.Plugin.Internal.Types;
using Dalamud.Plugin.Services;
using static Dalamud.Plugin.Services.IMarketBoard;
namespace Dalamud.Game.MarketBoard;
/// <summary>
/// This class provides access to market board events.
/// </summary>
[ServiceManager.EarlyLoadedService]
internal class MarketBoard : IInternalDisposableService, IMarketBoard
{
[ServiceManager.ServiceDependency]
private readonly NetworkHandlers networkHandlers = Service<NetworkHandlers>.Get();
/// <summary>
/// Initializes a new instance of the <see cref="MarketBoard"/> class.
/// </summary>
[ServiceManager.ServiceConstructor]
public MarketBoard()
{
this.networkHandlers.MbHistoryObservable.Subscribe(this.OnMbHistory);
this.networkHandlers.MbPurchaseObservable.Subscribe(this.OnPurchase);
this.networkHandlers.MbOfferingsObservable.Subscribe(this.OnOfferings);
this.networkHandlers.MbPurchaseSentObservable.Subscribe(this.OnPurchaseSent);
this.networkHandlers.MbTaxesObservable.Subscribe(this.OnTaxRates);
}
/// <inheritdoc/>
public event HistoryReceivedDelegate? HistoryReceived;
/// <inheritdoc/>
public event ItemPurchasedDelegate? ItemPurchased;
/// <inheritdoc/>
public event OfferingsReceivedDelegate? OfferingsReceived;
/// <inheritdoc/>
public event PurchaseRequestedDelegate? PurchaseRequested;
/// <inheritdoc/>
public event TaxRatesReceivedDelegate? TaxRatesReceived;
/// <inheritdoc/>
public void DisposeService()
{
this.HistoryReceived = null;
this.ItemPurchased = null;
this.OfferingsReceived = null;
this.PurchaseRequested = null;
this.TaxRatesReceived = null;
}
private void OnMbHistory(MarketBoardHistory marketBoardHistory)
{
this.HistoryReceived?.Invoke(marketBoardHistory);
}
private void OnPurchase(MarketBoardPurchase marketBoardHistory)
{
this.ItemPurchased?.Invoke(marketBoardHistory);
}
private void OnOfferings(MarketBoardCurrentOfferings currentOfferings)
{
this.OfferingsReceived?.Invoke(currentOfferings);
}
private void OnPurchaseSent(MarketBoardPurchaseHandler purchaseHandler)
{
this.PurchaseRequested?.Invoke(purchaseHandler);
}
private void OnTaxRates(MarketTaxRates taxRates)
{
this.TaxRatesReceived?.Invoke(taxRates);
}
}
/// <summary>
/// Plugin scoped version of MarketBoard.
/// </summary>
[PluginInterface]
[ServiceManager.ScopedService]
#pragma warning disable SA1015
[ResolveVia<IMarketBoard>]
#pragma warning restore SA1015
internal class MarketBoardPluginScoped : IInternalDisposableService, IMarketBoard
{
private static readonly ModuleLog Log = ModuleLog.Create<MarketBoardPluginScoped>();
[ServiceManager.ServiceDependency]
private readonly MarketBoard marketBoardService = Service<MarketBoard>.Get();
private readonly string owningPluginName;
/// <summary>
/// Initializes a new instance of the <see cref="MarketBoardPluginScoped"/> class.
/// </summary>
/// <param name="plugin">The plugin owning this service.</param>
internal MarketBoardPluginScoped(LocalPlugin? plugin)
{
this.marketBoardService.HistoryReceived += this.OnHistoryReceived;
this.marketBoardService.ItemPurchased += this.OnItemPurchased;
this.marketBoardService.OfferingsReceived += this.OnOfferingsReceived;
this.marketBoardService.PurchaseRequested += this.OnPurchaseRequested;
this.marketBoardService.TaxRatesReceived += this.OnTaxRatesReceived;
this.owningPluginName = plugin?.InternalName ?? "DalamudInternal";
}
/// <inheritdoc/>
public event HistoryReceivedDelegate? HistoryReceived;
/// <inheritdoc/>
public event ItemPurchasedDelegate? ItemPurchased;
/// <inheritdoc/>
public event OfferingsReceivedDelegate? OfferingsReceived;
/// <inheritdoc/>
public event PurchaseRequestedDelegate? PurchaseRequested;
/// <inheritdoc/>
public event TaxRatesReceivedDelegate? TaxRatesReceived;
/// <inheritdoc/>
void IInternalDisposableService.DisposeService()
{
this.marketBoardService.HistoryReceived -= this.OnHistoryReceived;
this.marketBoardService.ItemPurchased -= this.OnItemPurchased;
this.marketBoardService.OfferingsReceived -= this.OnOfferingsReceived;
this.marketBoardService.PurchaseRequested -= this.OnPurchaseRequested;
this.marketBoardService.TaxRatesReceived -= this.OnTaxRatesReceived;
this.HistoryReceived = null;
this.ItemPurchased = null;
this.OfferingsReceived = null;
this.PurchaseRequested = null;
this.TaxRatesReceived = null;
}
private void OnHistoryReceived(IMarketBoardHistory history)
{
foreach (var action in Delegate.EnumerateInvocationList(this.HistoryReceived))
{
try
{
action.Invoke(history);
}
catch (Exception ex)
{
this.LogInvocationError(ex, nameof(this.HistoryReceived));
}
}
}
private void OnItemPurchased(IMarketBoardPurchase purchase)
{
foreach (var action in Delegate.EnumerateInvocationList(this.ItemPurchased))
{
try
{
action.Invoke(purchase);
}
catch (Exception ex)
{
this.LogInvocationError(ex, nameof(this.ItemPurchased));
}
}
}
private void OnOfferingsReceived(IMarketBoardCurrentOfferings currentOfferings)
{
foreach (var action in Delegate.EnumerateInvocationList(this.OfferingsReceived))
{
try
{
action.Invoke(currentOfferings);
}
catch (Exception ex)
{
this.LogInvocationError(ex, nameof(this.OfferingsReceived));
}
}
}
private void OnPurchaseRequested(IMarketBoardPurchaseHandler purchaseHandler)
{
foreach (var action in Delegate.EnumerateInvocationList(this.PurchaseRequested))
{
try
{
action.Invoke(purchaseHandler);
}
catch (Exception ex)
{
this.LogInvocationError(ex, nameof(this.PurchaseRequested));
}
}
}
private void OnTaxRatesReceived(IMarketTaxRates taxRates)
{
foreach (var action in Delegate.EnumerateInvocationList(this.TaxRatesReceived))
{
try
{
action.Invoke(taxRates);
}
catch (Exception ex)
{
this.LogInvocationError(ex, nameof(this.TaxRatesReceived));
}
}
}
private void LogInvocationError(Exception ex, string delegateName)
{
Log.Error(
ex,
"An error occured while invoking event `{evName}` for {plugin}",
delegateName,
this.owningPluginName);
}
}