From 48f591b572f032c88db357c24b46a08c61e5123a Mon Sep 17 00:00:00 2001 From: Mira Date: Sat, 4 Jan 2025 19:21:46 +0100 Subject: [PATCH] fix bis fetch Signed-off-by: Mira --- .../Connectors/BaseConnector.cs | 28 ++- .../Connectors/ConnectorPool.cs | 3 +- .../Connectors/EtroConnector.cs | 16 +- .../Connectors/LodestoneConnector.cs | 8 +- .../Connectors/XivGearAppConnector.cs | 31 ++-- .../Localization/GeneralLoc.Designer.cs | 54 ++++++ .../Localization/GeneralLoc.resx | 18 ++ HimbeertoniRaidTool/Modules/Core/ChangeLog.cs | 9 + .../Modules/LootMaster/LootMasterModule.cs | 19 +- .../Services/ExamineGearDataProvider.cs | 6 +- HimbeertoniRaidTool/UI/EditWindows.cs | 169 ++++++++---------- HimbeertoniRaidTool/UI/ImGuiHelper.cs | 9 +- HimbeertoniRaidTool/UI/ItemSelection.cs | 2 +- 13 files changed, 230 insertions(+), 142 deletions(-) diff --git a/HimbeertoniRaidTool/Connectors/BaseConnector.cs b/HimbeertoniRaidTool/Connectors/BaseConnector.cs index 1ff366b..604841e 100644 --- a/HimbeertoniRaidTool/Connectors/BaseConnector.cs +++ b/HimbeertoniRaidTool/Connectors/BaseConnector.cs @@ -12,14 +12,38 @@ public interface IReadOnlyGearConnector public bool BelongsToThisService(string url); public string GetId(string url); public string GetWebUrl(string id); - public IList GetNames(string id); - public IReadOnlyDictionary GetBiSList(Job job); + public IList GetPossibilities(string id); + public IList GetBiSList(Job job); internal HrtUiMessage UpdateAllSets(bool updateAll, int maxAgeInDays); public void RequestGearSetUpdate(GearSet set, Action? messageCallback = null, string taskName = "Gearset Update"); public HrtUiMessage UpdateGearSet(GearSet set); } +public readonly struct ExternalBiSDefinition +{ + public readonly GearSetManager Service = GearSetManager.Unknown; + public readonly string Id = string.Empty; + public readonly int Idx = 0; + public readonly string Name = string.Empty; + + public ExternalBiSDefinition(GearSetManager service, string id, int idx, string name) + { + Service = service; + Id = id; + Idx = idx; + Name = name; + } + + public GearSet ToGearSet() => new(Service, Name) + { + ExternalId = Id, + ExternalIdx = Idx, + }; + + public bool Equals(GearSet? set) => Service == set?.ManagedBy && Id == set.ExternalId && set.ExternalIdx == Idx; +} + internal abstract class WebConnector { private readonly ConcurrentDictionary _cachedRequests; diff --git a/HimbeertoniRaidTool/Connectors/ConnectorPool.cs b/HimbeertoniRaidTool/Connectors/ConnectorPool.cs index a566bc8..b2d18b0 100644 --- a/HimbeertoniRaidTool/Connectors/ConnectorPool.cs +++ b/HimbeertoniRaidTool/Connectors/ConnectorPool.cs @@ -33,8 +33,7 @@ public bool TryGetConnector(GearSetManager type, [NotNullWhen(true)] out IReadOn _ => null, }; - public (GearSetManager Service, string Id) GetDefaultBiS(Job job) => - (GearSetManager.Etro, _etroConnector.GetDefaultBiS(job)); + public ExternalBiSDefinition GetDefaultBiS(Job job) => _etroConnector.GetDefaultBiS(job); public void Dispose() => LodestoneConnector.Dispose(); } \ No newline at end of file diff --git a/HimbeertoniRaidTool/Connectors/EtroConnector.cs b/HimbeertoniRaidTool/Connectors/EtroConnector.cs index 717a4ba..3ec2d31 100644 --- a/HimbeertoniRaidTool/Connectors/EtroConnector.cs +++ b/HimbeertoniRaidTool/Connectors/EtroConnector.cs @@ -19,7 +19,7 @@ internal sealed class EtroConnector : WebConnector, IReadOnlyGearConnector private const string GEARSET_WEB_BASE_URL = WEB_BASE_URL + "gearset/"; private const string RELIC_API_BASE_URL = API_BASE_URL + "relic/"; private const string BIS_API_BASE_URL = GEARSET_API_BASE_URL + "bis/"; - private readonly Dictionary> _bisCache = []; + private readonly Dictionary> _bisCache = []; private readonly Dictionary _foodLookup = []; private readonly TaskManager _taskManager; private readonly HrtDataManager _hrtDataManager; @@ -30,7 +30,7 @@ internal EtroConnector(HrtDataManager hrtDataManager, TaskManager tm, ILogger lo _taskManager = tm; foreach (var job in Enum.GetValues()) { - _bisCache.Add(job, new Dictionary()); + _bisCache.Add(job, []); } _taskManager.RegisterTask(new HrtTask(FillBisList, msg => @@ -59,9 +59,9 @@ internal EtroConnector(HrtDataManager hrtDataManager, TaskManager tm, ILogger lo MissingMemberHandling = MissingMemberHandling.Ignore, ReferenceLoopHandling = ReferenceLoopHandling.Ignore, }; - public IReadOnlyDictionary GetBiSList(Job job) => _bisCache[job]; - public string GetDefaultBiS(Job job) => _bisCache[job].Keys.FirstOrDefault(""); - public IList GetNames(string id) + public IList GetBiSList(Job job) => _bisCache[job]; + public ExternalBiSDefinition GetDefaultBiS(Job job) => _bisCache[job].FirstOrDefault(new ExternalBiSDefinition()); + public IList GetPossibilities(string id) { if (id.Equals("")) return []; var httpResponse = MakeWebRequest(GEARSET_API_BASE_URL + id); @@ -69,7 +69,7 @@ public IList GetNames(string id) var readTask = httpResponse.Content.ReadAsStringAsync(); readTask.Wait(); var etroSet = JsonConvert.DeserializeObject(readTask.Result, JsonSettings); - return etroSet?.name is null ? [] : [etroSet.name]; + return etroSet?.name is null ? [] : [new ExternalBiSDefinition(GearSetManager.Etro, id, 0, etroSet.name)]; } private HrtUiMessage FillBisList() { @@ -82,13 +82,13 @@ private HrtUiMessage FillBisList() foreach (var set in sets) { if (set.id != null) - _bisCache[set.job][set.id] = set.name ?? set.id; + _bisCache[set.job].Add(new ExternalBiSDefinition(GearSetManager.Etro, set.id, 0, set.name ?? set.id)); } return new HrtUiMessage(GeneralLoc.EtroConnector_FillBisList_Success, HrtUiMessageType.Success); } public bool BelongsToThisService(string url) => url.StartsWith(WEB_BASE_URL); - public string GetId(string url) => url[GEARSET_WEB_BASE_URL.Length..]; + public string GetId(string url) => BelongsToThisService(url) ? url[GEARSET_WEB_BASE_URL.Length..] : url; public string GetWebUrl(string id) => GEARSET_WEB_BASE_URL + id; public void RequestGearSetUpdate(GearSet set, Action? messageCallback = null, string taskName = "Etro Update") diff --git a/HimbeertoniRaidTool/Connectors/LodestoneConnector.cs b/HimbeertoniRaidTool/Connectors/LodestoneConnector.cs index 4e1128e..cacb5c3 100644 --- a/HimbeertoniRaidTool/Connectors/LodestoneConnector.cs +++ b/HimbeertoniRaidTool/Connectors/LodestoneConnector.cs @@ -95,6 +95,10 @@ private async Task UpdateCharacterAsync(Player p) FillItem(newGearset.Ring1, GearSetSlot.Ring1); FillItem(newGearset.Ring2, GearSetSlot.Ring2); + return new HrtUiMessage( + string.Format(GeneralLoc.LodestoneConnector_msg_Success, p.MainChar.Name, classToChange.Job), + HrtUiMessageType.Success); + void FillItem(GearEntry? gearPiece, GearSetSlot slot) { if (gearPiece == null) @@ -136,10 +140,6 @@ void FillItem(GearEntry? gearPiece, GearSetSlot slot) classToChange.CurGear[slot].AddMateria(new MateriaItem(materiaCategory, materiaLevel)); } } - - return new HrtUiMessage( - string.Format(GeneralLoc.LodestoneConnector_msg_Success, p.MainChar.Name, classToChange.Job), - HrtUiMessageType.Success); } catch (Exception e) { diff --git a/HimbeertoniRaidTool/Connectors/XivGearAppConnector.cs b/HimbeertoniRaidTool/Connectors/XivGearAppConnector.cs index 57d2c6b..340a212 100644 --- a/HimbeertoniRaidTool/Connectors/XivGearAppConnector.cs +++ b/HimbeertoniRaidTool/Connectors/XivGearAppConnector.cs @@ -30,32 +30,33 @@ internal class XivGearAppConnector(HrtDataManager hrtDataManager, TaskManager ta }; - public bool BelongsToThisService(string url) => url.StartsWith(WEB_BASE_URL) || url.StartsWith(API_BASE_URL); - public string GetId(string url) => HttpUtility.UrlDecode(url).Split('|')[^1]; - public string GetWebUrl(string id) => $"{GEAR_WEB_BASE_URL}{id}"; - public IReadOnlyDictionary GetBiSList(Job job) => new Dictionary(); - public bool IsSheet(string id) + public bool BelongsToThisService(string url) { - var httpResponse = MakeWebRequest(GEAR_API_BASE_URL + id); - if (httpResponse is null || !httpResponse.IsSuccessStatusCode) return false; - var readTask = httpResponse.Content.ReadAsStringAsync(); - readTask.Wait(); - return IsSheetInternal(readTask.Result); + url = HttpUtility.UrlDecode(url); + return url.StartsWith(WEB_BASE_URL) || url.StartsWith(API_BASE_URL); } + public string GetId(string url) => HttpUtility.UrlDecode(url).Split('|')[^1]; + public string GetWebUrl(string id) => $"{GEAR_WEB_BASE_URL}{id}"; + public IList GetBiSList(Job job) => []; private static bool IsSheetInternal(string content) => JsonConvert.DeserializeObject(content)?.sets?.Count > 0; - public IList GetNames(string id) + public IList GetPossibilities(string id) { + Logger.Debug($"Getting possibilities for {id}"); var httpResponse = MakeWebRequest(GEAR_API_BASE_URL + id); if (httpResponse is null || !httpResponse.IsSuccessStatusCode) return []; var readTask = httpResponse.Content.ReadAsStringAsync(); readTask.Wait(); var sheet = JsonConvert.DeserializeObject(readTask.Result); - if (sheet?.sets != null) - return sheet.sets?.ConvertAll(set => set.name ?? "") ?? []; - var set = JsonConvert.DeserializeObject(readTask.Result); - return set is null ? [] : [set.name ?? ""]; + if (sheet?.sets == null) + { + var set = JsonConvert.DeserializeObject(readTask.Result); + return set is null ? [] : [new ExternalBiSDefinition(GearSetManager.XivGear, id, 0, set.name ?? "")]; + } + int idx = 0; + return sheet.sets.Select(set => new ExternalBiSDefinition(GearSetManager.XivGear, id, idx++, set.name ?? "")) + .ToList(); } public void RequestGearSetUpdate(GearSet set, Action? messageCallback = null, diff --git a/HimbeertoniRaidTool/Localization/GeneralLoc.Designer.cs b/HimbeertoniRaidTool/Localization/GeneralLoc.Designer.cs index 1aa0be2..da6afe8 100644 --- a/HimbeertoniRaidTool/Localization/GeneralLoc.Designer.cs +++ b/HimbeertoniRaidTool/Localization/GeneralLoc.Designer.cs @@ -460,6 +460,24 @@ internal static string EditGearSetUi_btn_tt_selectItem { } } + /// + /// Looks up a localized string similar to Supply own set. + /// + internal static string EditGearSetUi_hdg_ExtCustom { + get { + return ResourceManager.GetString("EditGearSetUi_hdg_ExtCustom", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to ID/Url. + /// + internal static string EditGearSetUi_input_ExtId { + get { + return ResourceManager.GetString("EditGearSetUi_input_ExtId", resourceCulture); + } + } + /// /// Looks up a localized string similar to Source. /// @@ -487,6 +505,33 @@ internal static string EditGearSetUi_text_getEtroBis { } } + /// + /// Looks up a localized string similar to Invalid Id / Url. + /// + internal static string EditGearSetUi_text_InvalidId { + get { + return ResourceManager.GetString("EditGearSetUi_text_InvalidId", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Replace with:. + /// + internal static string EditGearSetUi_text_Replace { + get { + return ResourceManager.GetString("EditGearSetUi_text_Replace", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Auto Detect. + /// + internal static string EditGearSetUi_txt_AutoDetect { + get { + return ResourceManager.GetString("EditGearSetUi_txt_AutoDetect", resourceCulture); + } + } + /// /// Looks up a localized string similar to Gear set is meant for Job:. /// @@ -514,6 +559,15 @@ internal static string EditGearSetUi_txt_lastUpdate { } } + /// + /// Looks up a localized string similar to Loading.... + /// + internal static string EditGearSetUi_txt_Loading { + get { + return ResourceManager.GetString("EditGearSetUi_txt_Loading", resourceCulture); + } + } + /// /// Looks up a localized string similar to Source. /// diff --git a/HimbeertoniRaidTool/Localization/GeneralLoc.resx b/HimbeertoniRaidTool/Localization/GeneralLoc.resx index 3349ecb..93a72f6 100644 --- a/HimbeertoniRaidTool/Localization/GeneralLoc.resx +++ b/HimbeertoniRaidTool/Localization/GeneralLoc.resx @@ -480,4 +480,22 @@ You need to update to use this plugin! Food + + Loading... + + + Invalid Id / Url + + + ID/Url + + + Supply own set + + + Auto Detect + + + Replace with: + \ No newline at end of file diff --git a/HimbeertoniRaidTool/Modules/Core/ChangeLog.cs b/HimbeertoniRaidTool/Modules/Core/ChangeLog.cs index dec5807..7409721 100644 --- a/HimbeertoniRaidTool/Modules/Core/ChangeLog.cs +++ b/HimbeertoniRaidTool/Modules/Core/ChangeLog.cs @@ -8,6 +8,15 @@ public class ChangeLog { public static readonly IReadOnlyList Entries = new List { + new(new Version(1, 8, 0, 2)) + { + MinorFeatures = + { + new ChangeLogEntry(UserInterface, "Improved usability of BiS section in gear edit"), + new ChangeLogEntry( + Bugfix, "2nd,3rd,... set from XivGear.app sheets are now fetched correctly, not always the 1st"), + }, + }, new(new Version(1, 8, 0, 1)) { MinorFeatures = diff --git a/HimbeertoniRaidTool/Modules/LootMaster/LootMasterModule.cs b/HimbeertoniRaidTool/Modules/LootMaster/LootMasterModule.cs index 30b5d55..9639f5b 100644 --- a/HimbeertoniRaidTool/Modules/LootMaster/LootMasterModule.cs +++ b/HimbeertoniRaidTool/Modules/LootMaster/LootMasterModule.cs @@ -180,21 +180,16 @@ private bool FillCharacter(ref Character destination, IPlayerCharacter source) { curClass.Level = source.Level; var gearDb = Services.HrtDataManager.GearDb; - if (!gearDb.Search( - entry => entry?.ExternalId - == Services.ConnectorPool.GetDefaultBiS(curClass.Job).Id, - out var etroSet)) + var defaultBis = Services.ConnectorPool.GetDefaultBiS(curClass.Job); + if (!gearDb.Search(defaultBis.Equals, out var bisSet)) { - var defaultBis = Services.ConnectorPool.GetDefaultBiS(curClass.Job); - etroSet = new GearSet(defaultBis.Service) - { - ExternalId = defaultBis.Id, - }; - gearDb.TryAdd(etroSet); + + bisSet = defaultBis.ToGearSet(); + gearDb.TryAdd(bisSet); if (Services.ConnectorPool.TryGetConnector(defaultBis.Service, out var connector)) - connector.RequestGearSetUpdate(etroSet, HandleMessage); + connector.RequestGearSetUpdate(bisSet, HandleMessage); } - curClass.CurBis = etroSet; + curClass.CurBis = bisSet; gearDb.TryAdd(curClass.CurGear); } Services.HrtDataManager.Save(); diff --git a/HimbeertoniRaidTool/Services/ExamineGearDataProvider.cs b/HimbeertoniRaidTool/Services/ExamineGearDataProvider.cs index ff13cb3..475ec6f 100644 --- a/HimbeertoniRaidTool/Services/ExamineGearDataProvider.cs +++ b/HimbeertoniRaidTool/Services/ExamineGearDataProvider.cs @@ -128,13 +128,11 @@ private void GetItemInfos() targetClass = targetChar.AddClass(targetJob); var defaultBiS = _connectorPool.GetDefaultBiS(targetJob); - if (_hrtDataManager.GearDb.Search( - entry => entry?.ManagedBy == defaultBiS.Service && entry.ExternalId == defaultBiS.Id, - out var existingBis)) + if (_hrtDataManager.GearDb.Search(defaultBiS.Equals, out var existingBis)) targetClass.CurBis = existingBis; else { - (targetClass.CurBis.ManagedBy, targetClass.CurBis.ExternalId) = defaultBiS; + targetClass.CurBis = defaultBiS.ToGearSet(); if (_hrtDataManager.GearDb.TryAdd(targetClass.CurBis) && _connectorPool.TryGetConnector(defaultBiS.Service, out var connector)) connector.RequestGearSetUpdate(targetClass.CurBis); diff --git a/HimbeertoniRaidTool/UI/EditWindows.cs b/HimbeertoniRaidTool/UI/EditWindows.cs index fdd1dbd..611eea9 100644 --- a/HimbeertoniRaidTool/UI/EditWindows.cs +++ b/HimbeertoniRaidTool/UI/EditWindows.cs @@ -1,4 +1,5 @@ -using System.Numerics; +using System.Collections.Concurrent; +using System.Numerics; using Dalamud.Interface; using Dalamud.Interface.Utility.Raii; using Dalamud.Utility; @@ -9,7 +10,6 @@ using ImGuiNET; using Lumina.Excel.Sheets; using Action = System.Action; -using EnumExtensions = HimbeertoniRaidTool.Common.Data.EnumExtensions; namespace HimbeertoniRaidTool.Plugin.UI; @@ -371,7 +371,11 @@ protected override void DrawContent() Factory.DataManager.GearDb.TryAdd(newClass.CurGear); Factory.DataManager.GearDb.TryAdd(newClass.CurBis); } - (newClass.CurBis.ManagedBy, newClass.CurBis.ExternalId) = Factory.ConnectorPool.GetDefaultBiS(_newJob); + var newBis = Factory.ConnectorPool.GetDefaultBiS(_newJob); + newClass.CurBis.ManagedBy = newBis.Service; + newClass.CurBis.ExternalId = newBis.Id; + newClass.CurBis.ExternalIdx = newBis.Idx; + newClass.CurBis.Name = newBis.Name; } return; @@ -428,11 +432,13 @@ private class EditGearSetWindow : EditWindow { private readonly Job? _providedJob; private Job _job; - private string _externalIdInput = ""; - private GearSetManager _curSetManager; - private IList _externalGearSetList = []; - private string _loadedExternalId = ""; - private bool _backgroundTaskBusy = false; + private string _curSetId = ""; + private GearSetManager? _selectedService; + private GearSetManager? _detectedService; + private GearSetManager? CurService => _selectedService ?? _detectedService; + private readonly ConcurrentDictionary<(GearSetManager? service, string id), IList> + _externalGearSetCache = []; + private readonly ConcurrentDictionary<(GearSetManager? service, string id), bool> _currentlyLoading = []; internal EditGearSetWindow(EditWindowFactory factory, GearSet original, Action? onSave = null, Action? onCancel = null, Action? onDelete = null, Job? job = null) : base(factory, original, onSave, onCancel, onDelete) @@ -440,7 +446,6 @@ internal EditGearSetWindow(EditWindowFactory factory, GearSet original, Action()) + { + if (!Factory.ConnectorPool.TryGetConnector(serviceType, out var connector)) continue; + if (!connector.BelongsToThisService(_curSetId) && CurService == null) continue; + if (CurService.HasValue && CurService.Value != serviceType) continue; + _detectedService = serviceType; + _selectedService = null; + _curSetId = connector.GetId(_curSetId); + if (_externalGearSetCache.ContainsKey((CurService, _curSetId))) return; + if (_currentlyLoading.ContainsKey((CurService, _curSetId))) return; + if (_currentlyLoading.TryAdd((CurService, _curSetId), true)) + Factory.TaskManager.RegisterTask(new HrtTask>( + () => connector.GetPossibilities(_curSetId), + list => + { + _externalGearSetCache.TryAdd( + (CurService, _curSetId), list); + _currentlyLoading.Remove( + (CurService, _curSetId), out _); + }, "GetSetNames" + )); + return; + } + } + protected override void DrawContent() { ImGui.Columns(2, "##Naming", false); @@ -485,8 +520,8 @@ private void DrawGeneralSection() { using var table = ImRaii.Table("##GeneralTable", 2); if (!table) return; - ImGui.TableSetupColumn("##Label", ImGuiTableColumnFlags.WidthStretch, 1); - ImGui.TableSetupColumn("##Input", ImGuiTableColumnFlags.WidthStretch, 5); + ImGui.TableSetupColumn("##Label", ImGuiTableColumnFlags.WidthStretch, 2); + ImGui.TableSetupColumn("##Input", ImGuiTableColumnFlags.WidthStretch, 6); ImGui.TableNextColumn(); ImGui.BeginDisabled(DataCopy.IsManagedExternally); ImGui.Text(GeneralLoc.CommonTerms_Name); @@ -591,6 +626,7 @@ private void DrawGearEditSection() return; void DrawSlot(GearSetSlot slot) { + ImGui.BeginDisabled(ChildIsOpen); ImGui.TableNextColumn(); UiSystem.Helpers.DrawGearEdit(this, slot, DataCopy[slot], i => @@ -610,116 +646,65 @@ private void DrawReplaceSection() foreach (var service in Enum.GetValues()) { if (!Factory.ConnectorPool.TryGetConnector(service, out var connector)) continue; - //Etro curated BiS + //curated BiS var bisSets = connector.GetBiSList(_job); if (!bisSets.Any()) continue; ImGui.Text(string.Format(GeneralLoc.EditGearSetUi_text_getCuratedBis, service.FriendlyName())); - foreach ((string externalId, string name) in bisSets) + + foreach (var biSDefinition in bisSets) { ImGui.SameLine(); - if (ImGuiHelper.Button($"{name}##BIS#{externalId}", $"{externalId}")) - { - if (Factory.DataManager.GearDb.Search( - gearSet => gearSet?.ExternalId == externalId && gearSet.ManagedBy == service, - out var newSet)) - { - ReplaceOriginal(newSet); - continue; - } - ReplaceOriginal(new GearSet(service, name) - { - ExternalId = externalId, - }); - connector.RequestGearSetUpdate(DataCopy); - } + DrawReplaceButton(biSDefinition); } } - ImGui.Text("Replace with: "); - ImGui.SameLine(); + ImGui.Text(GeneralLoc.EditGearSetUi_hdg_ExtCustom); ImGui.SetNextItemWidth(100 * ScaleFactor); - ImGuiHelper.Combo("##manager", ref _curSetManager, EnumExtensions.FriendlyName, - val => val != GearSetManager.Hrt); + ImGuiHelper.Combo("##manager", ref _selectedService, + val => val.HasValue ? val.Value.FriendlyName() : GeneralLoc.EditGearSetUi_txt_AutoDetect, + val => Factory.ConnectorPool.HasConnector(val)); ImGui.SetNextItemWidth(200 * ScaleFactor); ImGui.SameLine(); - if (ImGui.InputText("ID/Url", ref _externalIdInput, 255)) - { - foreach (var serviceType in Enum.GetValues()) - { - if (!Factory.ConnectorPool.TryGetConnector(serviceType, out var connector)) continue; - if (!connector.BelongsToThisService(_externalIdInput)) continue; - _curSetManager = serviceType; - _externalIdInput = connector.GetId(_externalIdInput); - } - } - if (_backgroundTaskBusy) + ImGui.InputText(GeneralLoc.EditGearSetUi_input_ExtId, ref _curSetId, 255); + LoadSet(); + if (_currentlyLoading.ContainsKey((CurService, _curSetId))) { - ImGui.Text("Loading... "); - ImGui.SameLine(); - } - else if (_externalIdInput != _loadedExternalId) - { - if (Factory.ConnectorPool.TryGetConnector(_curSetManager, out var connector)) - { - _backgroundTaskBusy = true; - _externalGearSetList = []; - DataCopy.ExternalIdx = 0; - _loadedExternalId = _externalIdInput; - Factory.TaskManager.RegisterTask(new HrtTask>( - () => connector.GetNames(_externalIdInput), - list => - { - _externalGearSetList = list; - _backgroundTaskBusy = false; - }, "GetSetNames" - )); - } + ImGui.Text(GeneralLoc.EditGearSetUi_txt_Loading); } - else if (_externalGearSetList.Count > 0 && Factory.ConnectorPool.HasConnector(_curSetManager)) + else { - if (_externalGearSetList.Count > 1) + if (_externalGearSetCache.TryGetValue((CurService, _curSetId), out var bisList) && bisList.Any()) { - ImGui.SetNextItemWidth(200 * ScaleFactor); - int idx = 0; - if (ImGui.BeginCombo("##idx", _externalGearSetList[DataCopy.ExternalIdx])) + ImGui.Text(GeneralLoc.EditGearSetUi_text_Replace); + foreach (var biSDefinition in bisList) { - foreach (string name in _externalGearSetList) - { - if (ImGui.Selectable(name)) - DataCopy.ExternalIdx = idx; - idx++; - } - - ImGui.EndCombo(); + ImGui.SameLine(); + DrawReplaceButton(biSDefinition); } } else { - ImGui.Text(_externalGearSetList[0]); + ImGui.Text(GeneralLoc.EditGearSetUi_text_InvalidId); } - ImGui.SameLine(); - } - else - { - ImGui.Text("No set"); - ImGui.SameLine(); + } - if (ImGuiHelper.Button(GeneralLoc.EditGearSetUi_btn_GetExternal, - GeneralLoc.EditGearSetUi_btn_tt_GetExternal, !_backgroundTaskBusy)) + return; + void DrawReplaceButton(ExternalBiSDefinition biSDefinition) { - if (Factory.DataManager.GearDb.Search( - gearSet => gearSet?.ManagedBy == _curSetManager && gearSet.ExternalId == _externalIdInput, - out var newSet)) + if (!ImGuiHelper.Button($"{biSDefinition.Name}##BIS#{biSDefinition.Id}#{biSDefinition.Idx}", + $"{biSDefinition.Id} ({biSDefinition.Idx}) <{biSDefinition.Service.FriendlyName()}>")) + return; + if (Factory.DataManager.GearDb.Search(biSDefinition.Equals, out var newSet)) { ReplaceOriginal(newSet); return; } - ReplaceOriginal(new GearSet(_curSetManager) + ReplaceOriginal(new GearSet(biSDefinition.Service) { - ExternalId = _externalIdInput, - ExternalIdx = DataCopy.ExternalIdx, + ExternalId = biSDefinition.Id, + ExternalIdx = biSDefinition.Idx, }); - if (Factory.ConnectorPool.TryGetConnector(_curSetManager, out var connector)) + if (Factory.ConnectorPool.TryGetConnector(biSDefinition.Service, out var connector)) connector.RequestGearSetUpdate(DataCopy); } } diff --git a/HimbeertoniRaidTool/UI/ImGuiHelper.cs b/HimbeertoniRaidTool/UI/ImGuiHelper.cs index 4fd95da..e0f93ba 100644 --- a/HimbeertoniRaidTool/UI/ImGuiHelper.cs +++ b/HimbeertoniRaidTool/UI/ImGuiHelper.cs @@ -187,7 +187,7 @@ public static bool Combo(string label, ref T value, Func? toName = { T? value2 = value; Func? toNameInternal = toName is null ? null : t => t.HasValue ? toName(t.Value) : ""; - bool result = Combo(label, ref value2, toNameInternal, select); + bool result = Combo(label, ref value2, toNameInternal, select, false); if (result && value2.HasValue) value = value2.Value; return result; @@ -195,13 +195,18 @@ public static bool Combo(string label, ref T value, Func? toName = public static bool Combo(string label, ref T? value, Func? toName = null, - Func? select = null) where T : struct, Enum + Func? select = null, bool allowNull = true) where T : struct, Enum { string[] names = Enum.GetNames(typeof(T)); toName ??= t => names[t.HasValue ? Array.IndexOf(Enum.GetValues(typeof(T)), t) : 0]; select ??= _ => true; bool result = false; if (!ImGui.BeginCombo(label, toName(value))) return result; + if (allowNull && ImGui.Selectable(toName(null))) + { + value = null; + result = true; + } foreach (var choice in Enum.GetValues()) { if (!select(choice) || !ImGui.Selectable(toName(choice))) continue; diff --git a/HimbeertoniRaidTool/UI/ItemSelection.cs b/HimbeertoniRaidTool/UI/ItemSelection.cs index 80450e5..d52e14c 100644 --- a/HimbeertoniRaidTool/UI/ItemSelection.cs +++ b/HimbeertoniRaidTool/UI/ItemSelection.cs @@ -157,7 +157,7 @@ protected override void DrawItemSelection() //Draw selection bar ImGui.SetNextItemWidth(65f * ScaleFactor); ImGui.BeginDisabled(_lockJob); - if (ImGuiHelper.Combo("##job", ref _job)) + if (ImGuiHelper.Combo("##job", ref _job, job => job.HasValue ? job.Value.ToString() : "-")) ReevaluateItems(); ImGui.EndDisabled(); ImGui.SameLine();