using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using Newtonsoft.Json.Linq; using XFEWarframe.Loadouts; namespace XFEWarframe.Desktop.Editing; public sealed record EquipmentTarget(string AccountId, string AccountName, string Category, string Item, string? Section = null); public sealed record RelatedEquipment(string Type, string Name, string Category, string Key, bool Owned, string Description); public sealed record EquipmentRevision(string Version, string Permissions, bool Enabled); public sealed record EquipmentTab(string Key, string Title, IReadOnlyList Actions); public sealed class EquipmentAction(EditorAction source) { public string Key => source.Key; public string Label => source.Label; public string Description => source.Description; public IReadOnlyList Fields { get; } = source.Fields.OfType().Select(x => new EditorField(x)).ToArray(); public bool Changed => Fields.Any(x => x.Changed); } /// One detail editor for both desktop roles. Transports supply only actions authorized by the server. public sealed class EquipmentEditorModel : ObservableObject { private readonly Func> read; private readonly Func> save; private readonly Func> revision; private readonly Func> confirm; private readonly Action notify; private readonly Action? navigate; private readonly EditorChangeMonitor changes; private EquipmentTarget? target; private string? version, permissionVersion; private int generation; private bool busy, conflict; private string title = UiText.Translate("装备详情"), subtitle = "", summary = "", message = ""; private IReadOnlyList tabs = []; public IReadOnlyList Related { get; private set; } = []; public ModLoadout? Modding { get; private set; } public string Title { get => title; private set => SetProperty(ref title, value); } public string Subtitle { get => subtitle; private set => SetProperty(ref subtitle, value); } public string Summary { get => summary; private set => SetProperty(ref summary, value); } public string Message { get => message; private set { SetProperty(ref message, value); OnPropertyChanged(nameof(HasMessage)); } } public bool HasMessage => Message.Length > 0; public string SelectedTabKey { get; set; } = "basic"; public IReadOnlyList Tabs { get => tabs; private set => SetProperty(ref tabs, value); } public IEnumerable Actions => Tabs.SelectMany(x => x.Actions); public bool HasChanges => Actions.Any(x => x.Changed); public bool HasConflict { get => conflict; private set { SetProperty(ref conflict, value); SaveCommand.NotifyCanExecuteChanged(); OpenRelatedCommand.NotifyCanExecuteChanged(); } } public bool IsBusy { get => busy; private set { SetProperty(ref busy, value); OnPropertyChanged(nameof(IsIdle)); SaveCommand.NotifyCanExecuteChanged(); OpenRelatedCommand.NotifyCanExecuteChanged(); ResetCommand.NotifyCanExecuteChanged(); RefreshCommand.NotifyCanExecuteChanged(); } } public bool IsIdle => !IsBusy; public IAsyncRelayCommand SaveCommand { get; } public IRelayCommand ResetCommand { get; } public IAsyncRelayCommand RefreshCommand { get; } public IAsyncRelayCommand OpenRelatedCommand { get; } public EquipmentEditorModel(Func> read, Func> save, Func> revision, Func> confirm, Action notify, Action? navigate = null) { this.read = read; this.save = save; this.revision = revision; this.confirm = confirm; this.notify = notify; this.navigate = navigate; changes = new EditorChangeMonitor(CheckChangesAsync); SaveCommand = new AsyncRelayCommand(SaveAsync, action => !IsBusy && !HasConflict && version is not null && action is not null && Actions.Contains(action)); ResetCommand = new RelayCommand(Reset, _ => !IsBusy); RefreshCommand = new AsyncRelayCommand(() => RunAsync(async () => { if (target is null || !await CanDiscardAsync()) return; await ReadAsync(false); }), () => !IsBusy); OpenRelatedCommand = new AsyncRelayCommand(OpenRelatedAsync, item => !IsBusy && !HasConflict && version is not null && item is not null && Related.Contains(item)); } public void Attach() => changes.Start(); public void Detach() { changes.Stop(); generation++; } public Task LoadAsync(EquipmentTarget? next) => RunAsync(async () => { generation++; target = next; version = permissionVersion = null; Tabs = []; Related = []; Modding = null; HasConflict = false; Message = ""; SelectedTabKey = next?.Section ?? "basic"; Title = UiText.Translate("装备详情"); Summary = ""; Subtitle = next is null ? "" : next.AccountName + " · " + InventoryNavigation.Label(next.Category); if (next is null) { Message = UiText.Translate("请从库存列表选择一件装备。"); return; } await ReadAsync(false); }); private async Task ReadAsync(bool preserveDrafts, string? savedKey = null) { var selected = target!; var observed = generation; var pageGeneration = changes.Generation; var data = await read(selected); var payload = await Task.Run(() => { var modding = (data["modding"] as JObject)?.ToObject(); var actions = ((JArray?)data["actions"] ?? []).OfType().Where(x => modding is null || !x.Value("key")!.StartsWith("exalted.mods.", StringComparison.Ordinal)).Select(x => new EquipmentAction(new EditorAction(x.Value("key")!, x.Value("label")!, x.Value("description") ?? "", (JArray)x["fields"]!, ""))).ToArray(); return (Modding: modding, Actions: actions); }); var prepared = payload.Actions; if (selected != target || observed != generation || pageGeneration != changes.Generation) throw new OperationCanceledException(); if (data.Value("selectedKey") != selected.Item) { Tabs = []; Related = []; Modding = null; version = null; HasConflict = true; Message = UiText.Translate("此物品已不存在,请返回库存列表。"); return; } if (preserveDrafts) foreach (var action in prepared.Where(x => x.Key != savedKey)) if (Actions.FirstOrDefault(x => x.Key == action.Key) is { } previous) foreach (var field in action.Fields) if (previous.Fields.FirstOrDefault(x => x.Key == field.Key) is { Changed: true } draft) { field.Value = draft.Value.DeepClone(); field.HasUnresolvedChoice = draft.HasUnresolvedChoice; field.ChoiceQuery = draft.ChoiceQuery; } version = data.Value("version"); permissionVersion = data.Value("permissionsRevision") ?? permissionVersion; Modding = payload.Modding; Title = data.Value("selectedName") ?? Title; Summary = data.Value("summary") ?? ""; HasConflict = false; Related = ((JArray?)data["relatedEquipment"] ?? []).OfType().Select(x => new RelatedEquipment( x.Value("type")!, x.Value("name")!, x.Value("category")!, x.Value("key") ?? "", x.Value("owned"), x.Value("description") ?? "")).ToArray(); var displayed = Modding is null ? prepared : prepared.Where(x => !x.Key.StartsWith("exalted.mods.", StringComparison.Ordinal)).ToArray(); var groups = displayed.GroupBy(x => Group(x.Key)).OrderBy(x => x.Key.Order) .Select(x => new EquipmentTab(x.Key.Key, UiText.Translate(x.Key.Title), x.ToArray())).ToList(); if (Related.Count > 0 && !groups.Any(x => x.Key == "exalted")) groups.Add(new EquipmentTab("exalted", UiText.Translate("显赫武器"), [])); Tabs = groups; Message = prepared.Length == 0 && Related.Count == 0 ? UiText.Translate("管理员未允许编辑此物品。") : ""; SaveCommand.NotifyCanExecuteChanged(); OpenRelatedCommand.NotifyCanExecuteChanged(); } private static (int Order, string Key, string Title) Group(string action) => action switch { "rename" or "experience" or "max" or "forma" or "mod.rank" or "mature" => (0, "basic", "基础属性"), "features" => (1, "features", "装备特性"), var key when key.StartsWith("shards.", StringComparison.Ordinal) => (2, "shards", "源力石"), "umbra" or "umbra.remove" => (3, "umbra", "Umbra 回响"), "invigoration" or "invigoration.remove" => (4, "invigoration", "活力注入"), var key when key.StartsWith("ability.multiple.", StringComparison.Ordinal) => (5, "experimental-abilities", "多技能实验"), var key when key.StartsWith("ability.", StringComparison.Ordinal) => (5, "abilities", "移植技能"), "exalted.prepare" => (6, "exalted", "显赫武器"), var key when key.StartsWith("loadout.mods.", StringComparison.Ordinal) || key.StartsWith("exalted.mods.", StringComparison.Ordinal) => (1, "mods", "MOD 配装"), "parts" or "valence" => (6, "parts", "部件与元素"), "remove" => (99, "manage", "物品管理"), _ => (7, "other", "其他编辑") }; private async Task OpenRelatedAsync(RelatedEquipment? item) { EquipmentTarget? destination = null; await RunAsync(async () => { if (item is null || target is null || version is null || HasConflict || !Related.Contains(item) || !await CanDiscardAsync()) return; if (Actions.Any(x => x.Key == "exalted.prepare")) { await save(target, new JObject { ["version"] = version, ["category"] = target.Category, ["item"] = target.Item, ["action"] = "exalted.prepare", ["values"] = new JObject { ["type"] = item.Type } }); await ReadAsync(false); } var current = Related.FirstOrDefault(x => x.Type == item.Type); if (current is not { Owned: true } || current.Key.Length == 0) throw new InvalidOperationException(UiText.Translate("此显赫武器尚未建立配装,或编辑权限已关闭。")); foreach (var action in Actions.Where(x => x.Changed)) Reset(action); destination = new(target.AccountId, target.AccountName, current.Category, current.Key, "mods"); }); // Native navigation rejects transitions while the source page is saving. if (destination is not null) navigate?.Invoke(destination); } private Task SaveAsync(EquipmentAction? action) => RunAsync(async () => { if (action is null || target is null || version is null || HasConflict || !Actions.Contains(action)) return; var values = new JObject(action.Fields.Select(x => new JProperty(x.Key, x.Serialize()))); if ((action.Key is "remove" or "shards.remove" or "umbra.remove" or "invigoration.remove") && !await confirm(action.Label, target.AccountName + " · " + Title + "\n" + action.Description)) return; await save(target, new JObject { ["version"] = version, ["category"] = target.Category, ["item"] = target.Item, ["action"] = action.Key, ["values"] = values }); await ReadAsync(true, action.Key); notify(UiText.Translate("存档已保存")); }); private void Reset(EquipmentAction? action) { if (action is null || !Actions.Contains(action)) return; foreach (var field in action.Fields) field.Reset(); OnPropertyChanged(nameof(Tabs)); } public Task CanDiscardAsync() => HasChanges ? confirm(UiText.Translate("放弃未保存的修改?"), UiText.Translate("当前装备有未保存的修改。继续后将丢弃这些输入。")) : Task.FromResult(true); public async Task CheckChangesAsync() { if (!changes.Active || IsBusy || target is null) return; var observed = version; var selected = target; var pageGeneration = changes.Generation; try { var current = await revision(selected); if (!changes.Active || IsBusy || target != selected || observed != version || pageGeneration != changes.Generation) return; if (!current.Enabled) { Revoke(); return; } if (version is null) { await RunAsync(() => ReadAsync(false)); return; } if (permissionVersion is not null && permissionVersion != current.Permissions) { var previousVersion = version; var existingConflict = HasConflict; await RunAsync(() => ReadAsync(true)); // Rebuild from the server's newly filtered action set. // The save can change after the revision request but before the actual read completes. // ReadAsync advances the permission version only on success so a failed refresh is retried. if (HasChanges && (existingConflict || current.Version != previousVersion || version != previousVersion)) { HasConflict = true; Message = UiText.Translate("存档已在其他位置更新,输入已保留。请刷新后再保存。"); } return; } permissionVersion = current.Permissions; if (current.Version == version) return; if (HasChanges) { HasConflict = true; Message = UiText.Translate("存档已在其他位置更新,输入已保留。请刷新后再保存。"); } else await RunAsync(() => ReadAsync(false)); } catch (OperationCanceledException) { } catch { if (changes.Active && target == selected) Message = UiText.Translate("连接中断,当前输入已保留。"); } } public void Revoke(bool forgetTarget = false) { generation++; Tabs = []; Related = []; Modding = null; version = null; HasConflict = true; if (forgetTarget) target = null; Message = UiText.Translate("编辑权限已关闭或账号已退出,请返回库存列表。"); } private async Task RunAsync(Func operation) { if (IsBusy) return; IsBusy = true; Message = ""; try { await operation(); } catch (OperationCanceledException) { } catch (Exception ex) { Message = ex.Message; } finally { IsBusy = false; } } }