Skip to content

Commit

Permalink
Merge branch 'feature/apiX' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
fmauNeko committed Jun 30, 2024
2 parents 2c4362c + be538af commit 4015d87
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 130 deletions.
8 changes: 4 additions & 4 deletions MarketBoardPlugin/GUI/MarketBoardConfigWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override void Draw()
this.Checkbox("No Gil Sales Tax", "Toggles whether the Gil Sales Tax is included", this.Plugin.Config.NoGilSalesTax, (v) =>
{
this.Plugin.Config.NoGilSalesTax = v;
MBPlugin.PluginInterface.SavePluginConfig(this.Plugin.Config);
this.Plugin.PluginInterface.SavePluginConfig(this.Plugin.Config);
this.Plugin.ResetMarketData();
});

Expand All @@ -56,7 +56,7 @@ public override void Draw()
if (this.Plugin.Config.MarketBufferSize != marketBufferSize)
{
this.Plugin.Config.MarketBufferSize = marketBufferSize;
MBPlugin.PluginInterface.SavePluginConfig(this.Plugin.Config);
this.Plugin.PluginInterface.SavePluginConfig(this.Plugin.Config);
}

var itemRefreshTimeout = this.Plugin.Config.ItemRefreshTimeout;
Expand All @@ -65,15 +65,15 @@ public override void Draw()
if (this.Plugin.Config.ItemRefreshTimeout != itemRefreshTimeout)
{
this.Plugin.Config.ItemRefreshTimeout = itemRefreshTimeout;
MBPlugin.PluginInterface.SavePluginConfig(this.Plugin.Config);
this.Plugin.PluginInterface.SavePluginConfig(this.Plugin.Config);
}
}

private void Checkbox(string label, string description, bool oldValue, Action<bool> setter)
{
if (ImGuiUtil.Checkbox(label, description, oldValue, setter))
{
MBPlugin.PluginInterface.SavePluginConfig(this.Plugin.Config);
this.Plugin.PluginInterface.SavePluginConfig(this.Plugin.Config);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion MarketBoardPlugin/GUI/MarketBoardShoppingListWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private void Checkbox(string label, string description, bool oldValue, Action<bo
{
if (ImGuiUtil.Checkbox(label, description, oldValue, setter))
{
MBPlugin.PluginInterface.SavePluginConfig(this.Plugin.Config);
this.Plugin.PluginInterface.SavePluginConfig(this.Plugin.Config);
}
}
}
Expand Down
91 changes: 46 additions & 45 deletions MarketBoardPlugin/GUI/MarketBoardWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ namespace MarketBoardPlugin.GUI
using System.Threading.Tasks;
using Dalamud.Game.Text;
using Dalamud.Interface;
using Dalamud.Interface.Components;
using Dalamud.Interface.Internal;
using Dalamud.Interface.ManagedFontAtlas;
using Dalamud.Interface.Utility.Raii;
using Dalamud.Interface.Textures;
using Dalamud.Interface.Textures.TextureWraps;
using Dalamud.Interface.Windowing;
using Dalamud.Plugin.Services;
using ImGuiNET;
Expand Down Expand Up @@ -83,8 +82,6 @@ public class MarketBoardWindow : Window, IDisposable

private ClassJob selectedClassJob;

private IDalamudTextureWrap selectedItemIcon;

private bool hQOnly;

private ulong playerId;
Expand Down Expand Up @@ -116,6 +113,7 @@ public class MarketBoardWindow : Window, IDisposable
public MarketBoardWindow(MBPlugin plugin)
: base("Market Board")
{
this.plugin = plugin ?? throw new ArgumentNullException(nameof(plugin));
this.Flags = ImGuiWindowFlags.NoScrollbar;
this.Size = new Vector2(800, 600);
this.SizeCondition = ImGuiCond.FirstUseEver;
Expand All @@ -126,8 +124,8 @@ public MarketBoardWindow(MBPlugin plugin)
};

this.marketBuffer = new List<MarketDataResponse>();
this.items = MBPlugin.Data.GetExcelSheet<Item>();
this.classJobs = MBPlugin.Data.GetExcelSheet<ClassJob>()?
this.items = plugin.DataManager.GetExcelSheet<Item>();
this.classJobs = plugin.DataManager.GetExcelSheet<ClassJob>()?
.Where(cj => cj.RowId != 0)
.OrderBy(cj =>
{
Expand All @@ -141,13 +139,12 @@ public MarketBoardWindow(MBPlugin plugin)
_ => 4,
};
}).ToList();
this.plugin = plugin ?? throw new ArgumentNullException(nameof(plugin));
this.sortedCategoriesAndItems = this.SortCategoriesAndItems();

MBPlugin.Framework.Update += this.HandleFrameworkUpdateEvent;
MBPlugin.GameGui.HoveredItemChanged += this.HandleHoveredItemChange;
this.plugin.Framework.Update += this.HandleFrameworkUpdateEvent;
this.plugin.GameGui.HoveredItemChanged += this.HandleHoveredItemChange;

this.defaultFontHandle = MBPlugin.PluginInterface.UiBuilder.FontAtlas.NewDelegateFontHandle(e =>
this.defaultFontHandle = this.plugin.PluginInterface.UiBuilder.FontAtlas.NewDelegateFontHandle(e =>
e.OnPreBuild(toolkit =>
toolkit.AddFontFromStream(
this.GetType().Assembly.GetManifestResourceStream("MarketBoardPlugin.Resources.NotoSans-Medium-NNBSP.otf"),
Expand All @@ -160,11 +157,9 @@ public MarketBoardWindow(MBPlugin plugin)
false,
"NNBSP")));

this.titleFontHandle = MBPlugin.PluginInterface.UiBuilder.FontAtlas.NewDelegateFontHandle(e =>
this.titleFontHandle = this.plugin.PluginInterface.UiBuilder.FontAtlas.NewDelegateFontHandle(e =>
e.OnPreBuild(toolkit =>
toolkit.AddDalamudDefaultFont(MBPlugin.PluginInterface.UiBuilder.DefaultFontSpec.SizePx * 1.5f)));

MBPlugin.PluginInterface.UiBuilder.RebuildFonts();
toolkit.AddDalamudDefaultFont(this.plugin.PluginInterface.UiBuilder.DefaultFontSpec.SizePx * 1.5f)));

var imPlotStylePtr = ImPlot.GetStyle();

Expand Down Expand Up @@ -347,7 +342,7 @@ void SelectClassJob(ClassJob classJob)
{
ImGui.Text("History");
ImGui.Separator();
var sheet = MBPlugin.Data.Excel.GetSheet<Item>();
var sheet = this.plugin.DataManager.Excel.GetSheet<Item>();
foreach (var id in this.plugin.Config.History.ToArray())
{
var item = sheet.GetRow(id);
Expand All @@ -366,7 +361,7 @@ void SelectClassJob(ClassJob classJob)
{
ImGui.Text("Favorites");
ImGui.Separator();
var sheet = MBPlugin.Data.Excel.GetSheet<Item>();
var sheet = this.plugin.DataManager.Excel.GetSheet<Item>();
foreach (var id in this.plugin.Config.Favorites.ToArray())
{
var item = sheet.GetRow(id);
Expand Down Expand Up @@ -477,7 +472,7 @@ void SelectClassJob(ClassJob classJob)
else
{
this.progressPosition = 0;
var itemId = MBPlugin.GameGui.HoveredItem;
var itemId = this.plugin.GameGui.HoveredItem;
this.ChangeSelectedItem(Convert.ToUInt32(itemId % 500000));
this.itemBeingHovered = 0;
}
Expand Down Expand Up @@ -505,18 +500,26 @@ void SelectClassJob(ClassJob classJob)

if (this.selectedItem?.RowId > 0)
{
if (this.selectedItemIcon != null)
var couldGetIcon = this.plugin.TextureProvider.GetFromGameIcon(new GameIconLookup
{
if (ImGui.ImageButton(this.selectedItemIcon.ImGuiHandle, new Vector2(40, 40)))
IconId = this.selectedItem.Icon,
}).TryGetWrap(out var selectedItemIcon, out _);

using (selectedItemIcon)
{
if (couldGetIcon)
{
ImGui.LogToClipboard();
ImGui.LogText(this.selectedItem.Name);
ImGui.LogFinish();
if (ImGui.ImageButton(selectedItemIcon.ImGuiHandle, new Vector2(40, 40)))
{
ImGui.LogToClipboard();
ImGui.LogText(this.selectedItem.Name);
ImGui.LogFinish();
}
}
else
{
ImGui.SetCursorPos(new Vector2(40, 40));
}
}
else
{
ImGui.SetCursorPos(new Vector2(40, 40));
}

this.titleFontHandle.Push();
Expand Down Expand Up @@ -848,8 +851,7 @@ internal void ChangeSelectedItem(uint itemId, bool noHistory = false)
this.selectedItem = this.items.Single(i => i.RowId == itemId);

var iconId = this.selectedItem.Icon;
this.selectedItemIcon?.Dispose();
this.selectedItemIcon = MBPlugin.TextureProvider.GetIcon(iconId);
this.plugin.Log.Verbose("Selected item: {0} ({1}), icon: {2}", this.selectedItem.Name, this.selectedItem.RowId, iconId);

this.RefreshMarketData();
if (!noHistory)
Expand All @@ -861,7 +863,7 @@ internal void ChangeSelectedItem(uint itemId, bool noHistory = false)
this.plugin.Config.History.RemoveRange(100, this.plugin.Config.History.Count - 100);
}

MBPlugin.PluginInterface.SavePluginConfig(this.plugin.Config);
this.plugin.PluginInterface.SavePluginConfig(this.plugin.Config);
}
}

Expand All @@ -878,9 +880,8 @@ protected virtual void Dispose(bool disposing)

if (disposing)
{
MBPlugin.Framework.Update -= this.HandleFrameworkUpdateEvent;
MBPlugin.GameGui.HoveredItemChanged -= this.HandleHoveredItemChange;
this.selectedItemIcon?.Dispose();
this.plugin.Framework.Update -= this.HandleFrameworkUpdateEvent;
this.plugin.GameGui.HoveredItemChanged -= this.HandleHoveredItemChange;
this.defaultFontHandle?.Dispose();
this.titleFontHandle?.Dispose();
}
Expand Down Expand Up @@ -919,13 +920,13 @@ private static string ConvertItemNameToSortableFormat(string itemName)
/// Function used for debug purposes : Log every attributes of an Item.
/// </summary>
/// <param name="itm"> Item class to show in logs.</param>
private static void LogItemInfo(Item itm)
private void LogItemInfo(Item itm)
{
foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(itm))
{
string name = descriptor.Name;
object value = descriptor.GetValue(itm);
MBPlugin.Log.Information("{0}={1}", name, value);
this.plugin.Log.Information("{0}={1}", name, value);
}
}

Expand Down Expand Up @@ -972,7 +973,7 @@ private Dictionary<ItemSearchCategory, List<Item>> SortCategoriesAndItems()
{
try
{
var itemSearchCategories = MBPlugin.Data.GetExcelSheet<ItemSearchCategory>();
var itemSearchCategories = this.plugin.DataManager.GetExcelSheet<ItemSearchCategory>();

var sortedCategories = itemSearchCategories.Where(c => c.Category > 0).OrderBy(c => c.Category).ThenBy(c => c.Order);

Expand All @@ -992,23 +993,23 @@ private Dictionary<ItemSearchCategory, List<Item>> SortCategoriesAndItems()
}
catch (Exception ex)
{
MBPlugin.Log.Error(ex, $"Error loading category list.");
this.plugin.Log.Error(ex, $"Error loading category list.");
return null;
}
}

private void HandleFrameworkUpdateEvent(IFramework framework)
{
if (MBPlugin.ClientState.LocalContentId != 0 && this.playerId != MBPlugin.ClientState.LocalContentId)
if (this.plugin.ClientState.LocalContentId != 0 && this.playerId != this.plugin.ClientState.LocalContentId)
{
var localPlayer = MBPlugin.ClientState.LocalPlayer;
var localPlayer = this.plugin.ClientState.LocalPlayer;
if (localPlayer == null)
{
return;
}

var currentDc = localPlayer.CurrentWorld.GameData.DataCenter;
var dcWorlds = MBPlugin.Data.GetExcelSheet<World>()
var dcWorlds = this.plugin.DataManager.GetExcelSheet<World>()
.Where(w => w.DataCenter.Row == currentDc.Row && w.IsPublic)
.OrderBy(w => w.Name.ToString())
.Select(w =>
Expand Down Expand Up @@ -1052,11 +1053,11 @@ private void HandleFrameworkUpdateEvent(IFramework framework)

if (this.worldList.Count > 1)
{
this.playerId = MBPlugin.ClientState.LocalContentId;
this.playerId = this.plugin.ClientState.LocalContentId;
}
}

if (MBPlugin.ClientState.LocalContentId == 0)
if (this.plugin.ClientState.LocalContentId == 0)
{
this.playerId = 0;
}
Expand All @@ -1077,7 +1078,7 @@ private void HandleHoveredItemChange(object sender, ulong itemId)
return;
}

var item = MBPlugin.Data.Excel.GetSheet<Item>().GetRow((uint)itemId % 500000);
var item = this.plugin.DataManager.Excel.GetSheet<Item>().GetRow((uint)itemId % 500000);

if (item != null && this.enumerableCategoriesAndItems != null && this.sortedCategoriesAndItems.Any(i => i.Value != null && i.Value.Any(k => k.ToString() == item.ToString())))
{
Expand Down Expand Up @@ -1119,7 +1120,7 @@ private void RefreshMarketData()
try
{
this.marketData = await UniversalisClient
this.marketData = await this.plugin.UniversalisClient
.GetMarketData(
this.selectedItem.RowId,
this.worldList[this.selectedWorld].Item1,
Expand All @@ -1129,7 +1130,7 @@ private void RefreshMarketData()
}
catch (Exception)
{
MBPlugin.Log.Warning($"Failed to fetch market data for item {this.selectedItem.RowId} from Universalis.");
this.plugin.Log.Warning($"Failed to fetch market data for item {this.selectedItem.RowId} from Universalis.");
this.marketData = null;
}
Expand Down
26 changes: 16 additions & 10 deletions MarketBoardPlugin/Helpers/UniversalisClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,23 @@ namespace MarketBoardPlugin.Helpers
/// <summary>
/// Universalis API Client.
/// </summary>
public static class UniversalisClient
/// <remarks>
/// Initializes a new instance of the <see cref="UniversalisClient"/> class.
/// </remarks>
/// <param name="plugin">The plugin instance.</param>
public class UniversalisClient(MBPlugin plugin)
{
private readonly MBPlugin plugin = plugin ?? throw new ArgumentNullException(nameof(plugin));

/// <summary>
/// Gets market data of an item for a specific world.
/// Retrieves market data for a specific item from the Universalis API.
/// </summary>
/// <param name="itemId">The item ID.</param>
/// <param name="worldName">The world's name.</param>
/// <param name="historyCount">Number of entries to fetch from the history.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/>.</param>
/// <returns>The market data.</returns>
public static async Task<MarketDataResponse> GetMarketData(uint itemId, string worldName, int historyCount, CancellationToken cancellationToken)
/// <param name="itemId">The ID of the item to retrieve market data for.</param>
/// <param name="worldName">The name of the world to retrieve market data from.</param>
/// <param name="historyCount">The number of historical entries to retrieve.</param>
/// <param name="cancellationToken">A cancellation token to cancel the operation.</param>
/// <returns>A <see cref="MarketDataResponse"/> object containing the retrieved market data, or null if the operation fails.</returns>
public async Task<MarketDataResponse> GetMarketData(uint itemId, string worldName, int historyCount, CancellationToken cancellationToken)
{
var uriBuilder = new UriBuilder($"https://universalis.app/api/{worldName}/{itemId}?entries={historyCount}");

Expand All @@ -44,7 +50,7 @@ public static async Task<MarketDataResponse> GetMarketData(uint itemId, string w
}
catch (HttpRequestException)
{
MBPlugin.Log.Warning($"Failed to fetch market data for item {itemId} on world {worldName}.");
this.plugin.Log.Warning($"Fai /// to fetch market data for item {itemId} on world {worldName}.");
return null;
}

Expand All @@ -60,7 +66,7 @@ public static async Task<MarketDataResponse> GetMarketData(uint itemId, string w
}
catch (JsonException)
{
MBPlugin.Log.Warning($"Failed to parse market data for item {itemId} on world {worldName}.");
this.plugin.Log.Warning($"Failed to parse market data for item {itemId} on world {worldName}.");
return null;
}

Expand Down
Loading

0 comments on commit 4015d87

Please sign in to comment.