Skip to content

Commit

Permalink
Fix MarketBoardItemRequest & Universalis upload (#1888)
Browse files Browse the repository at this point in the history
  • Loading branch information
Infiziert90 authored Jul 3, 2024
1 parent b546dd0 commit a4c234c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,9 @@ private MarketBoardItemRequest()
{
}

/// <summary>
/// Gets the catalog ID.
/// </summary>
public uint CatalogId { get; private set; }

/// <summary>
/// Gets the request status. Nonzero statuses are errors.
/// Known values: default=0; rate limited=0x70000003
/// </summary>
public uint Status { get; private set; }

Expand All @@ -32,44 +28,33 @@ private MarketBoardItemRequest()
/// <summary>
/// Gets the amount to arrive.
/// </summary>
public byte AmountToArrive { get; private set; }
public uint AmountToArrive { get; private set; }

/// <summary>
/// Gets the offered item listings.
/// </summary>
public List<MarketBoardCurrentOfferings.MarketBoardItemListing> Listings { get; } = new();
public List<MarketBoardCurrentOfferings.MarketBoardItemListing> Listings { get; } = [];

/// <summary>
/// Gets the historical item listings.
/// </summary>
public List<MarketBoardHistory.MarketBoardHistoryListing> History { get; } = new();

/// <summary>
/// Gets or sets the listing request ID.
/// </summary>
public int ListingsRequestId { get; set; } = -1;

/// <summary>
/// Gets a value indicating whether the upload is complete.
/// </summary>
public bool IsDone => this.Listings.Count == this.AmountToArrive && this.History.Count != 0;
public List<MarketBoardHistory.MarketBoardHistoryListing> History { get; } = [];

/// <summary>
/// Read a packet off the wire.
/// </summary>
/// <param name="dataPtr">Packet data.</param>
/// <returns>An object representing the data read.</returns>
public static unsafe MarketBoardItemRequest Read(IntPtr dataPtr)
public static unsafe MarketBoardItemRequest Read(nint dataPtr)
{
using var stream = new UnmanagedMemoryStream((byte*)dataPtr.ToPointer(), 1544);
using var reader = new BinaryReader(stream);

var output = new MarketBoardItemRequest();

output.CatalogId = reader.ReadUInt32();
output.Status = reader.ReadUInt32();
stream.Position += 0x3;
output.AmountToArrive = reader.ReadByte();
var output = new MarketBoardItemRequest
{
Status = reader.ReadUInt32(),
AmountToArrive = reader.ReadUInt32(),
};

return output;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -47,9 +48,9 @@ public async Task Upload(MarketBoardItemRequest request)
{
WorldId = clientState.LocalPlayer?.CurrentWorld.Id ?? 0,
UploaderId = uploader.ToString(),
ItemId = request.CatalogId,
Listings = new List<UniversalisItemListingsEntry>(),
Sales = new List<UniversalisHistoryEntry>(),
ItemId = request.Listings.FirstOrDefault()?.CatalogId ?? 0,
Listings = [],
Sales = [],
};

foreach (var marketBoardItemListing in request.Listings)
Expand Down Expand Up @@ -105,7 +106,7 @@ public async Task Upload(MarketBoardItemRequest request)

// ====================================================================================

Log.Verbose("Universalis data upload for item#{CatalogId} completed", request.CatalogId);
Log.Verbose("Universalis data upload for item#{CatalogId} completed", request.Listings.FirstOrDefault()?.CatalogId ?? 0);
}

/// <inheritdoc/>
Expand Down
32 changes: 12 additions & 20 deletions Dalamud/Game/Network/Internal/NetworkHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ private unsafe nint CfPopDetour(nint packetData)
}
catch (Exception ex)
{
Log.Error(ex, "CfPopDetour threw an exception");
Log.Error(ex, "CfPopDetour threw an exception");
}

return result;
Expand Down Expand Up @@ -350,7 +350,7 @@ IObservable<MarketBoardCurrentOfferings> UntilBatchEnd(MarketBoardItemRequest re
}

return offeringsObservable
.Where(offerings => offerings.InternalItemListings.All(l => l.CatalogId == request.CatalogId))
.Where(offerings => offerings.InternalItemListings.All(l => l.CatalogId != 0))
.Skip(totalPackets - 1)
.Do(LogEndObserved);
}
Expand Down Expand Up @@ -387,7 +387,7 @@ void LogHistoryObserved(MarketBoardHistory history)
IObservable<MarketBoardHistory> UntilBatchEnd(MarketBoardItemRequest request)
{
return historyObservable
.Where(history => history.CatalogId == request.CatalogId)
.Where(history => history.CatalogId != 0)
.Take(1);
}

Expand All @@ -411,10 +411,7 @@ private IDisposable HandleMarketBoardItemRequest()
{
void LogStartObserved(MarketBoardItemRequest request)
{
Log.Verbose(
"Observed start of request for item#{CatalogId} with {NumListings} expected listings",
request.CatalogId,
request.AmountToArrive);
Log.Verbose("Observed start of request for item with {NumListings} expected listings", request.AmountToArrive);
}

var startObservable = this.MbItemRequestObservable
Expand Down Expand Up @@ -442,23 +439,18 @@ private void UploadMarketBoardData(
ICollection<MarketBoardHistory.MarketBoardHistoryListing> sales,
ICollection<MarketBoardCurrentOfferings.MarketBoardItemListing> listings)
{
var catalogId = listings.FirstOrDefault()?.CatalogId ?? 0;
if (listings.Count != request.AmountToArrive)
{
Log.Error(
"Wrong number of Market Board listings received for request: {ListingsCount} != {RequestAmountToArrive} item#{RequestCatalogId}",
listings.Count, request.AmountToArrive, request.CatalogId);
return;
}

if (listings.Any(listing => listing.CatalogId != request.CatalogId))
{
Log.Error("Received listings with mismatched item IDs for item#{RequestCatalogId}", request.CatalogId);
listings.Count, request.AmountToArrive, catalogId);
return;
}

Log.Verbose(
"Market Board request resolved, starting upload: item#{CatalogId} listings#{ListingsObserved} sales#{SalesObserved}",
request.CatalogId,
catalogId,
listings.Count,
sales.Count);

Expand Down Expand Up @@ -545,7 +537,7 @@ private nint MarketPurchasePacketDetour(nint a1, nint packetData)
{
Log.Error(ex, "MarketPurchasePacketHandler threw an exception");
}

return this.mbPurchaseHook.OriginalDisposeSafe(a1, packetData);
}

Expand All @@ -559,7 +551,7 @@ private nint MarketHistoryPacketDetour(nint a1, nint packetData, uint a3, char a
{
Log.Error(ex, "MarketHistoryPacketDetour threw an exception");
}

return this.mbHistoryHook.OriginalDisposeSafe(a1, packetData, a3, a4);
}

Expand Down Expand Up @@ -589,7 +581,7 @@ private nint MarketItemRequestStartDetour(nint a1, nint packetRef)
{
Log.Error(ex, "MarketItemRequestStartDetour threw an exception");
}

return this.mbItemRequestStartHook.OriginalDisposeSafe(a1, packetRef);
}

Expand All @@ -603,7 +595,7 @@ private byte MarketBoardOfferingsDetour(nint a1, nint packetRef)
{
Log.Error(ex, "MarketBoardOfferingsDetour threw an exception");
}

return this.mbOfferingsHook.OriginalDisposeSafe(a1, packetRef);
}

Expand All @@ -617,7 +609,7 @@ private byte MarketBoardSendPurchaseRequestDetour(InfoProxyItemSearch* infoProxy
{
Log.Error(ex, "MarketBoardSendPurchaseRequestDetour threw an exception");
}

return this.mbSendPurchaseRequestHook.OriginalDisposeSafe(infoProxyItemSearch);
}
}

0 comments on commit a4c234c

Please sign in to comment.