RynthSuite/Tools/RynthCore.MonsterEditor/MainWindow.axaml.cs
BlankenshipG 7797901d69 feat: port SK test branch (2026.10.18.24) onto sk
Squash of test/hotbuild-2026-10-16 as merged onto aelrynth main 2026.10.17.1, matching the
2026.10.18.24 test installer. The test branch wins where both changed the same lines; sk-only
changes kept; duplicate features cut to the test branch's copy. RynthAi HandWrittenVersion stays
0.5.0-legacy-ui on sk.

Highlights: RynthOracle 0.6.0 (Legendary, Markers, Dynamic Stamps and Temple guardians merged into
the Quests list, RynthNav Go to), RynthILT 0.2.20 (ub-IT auto-chunk rules, rating level,
RynthILT.Data v3), RynthAi ub-IT /ub and VT/MT meta compatibility and the loot attribute checklist,
RynthNav destination alternatives. Changelog/2026-10-18-sk-port-test-branch-2026.10.18.24.md.
2026-10-18 16:22:40 -04:00

336 lines
13 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.Json;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Data.Converters;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using Avalonia.Platform.Storage;
using Avalonia.Threading;
using Avalonia.VisualTree;
namespace RynthCore.MonsterEditor;
public partial class MainWindow : Window
{
public ObservableCollection<MonsterRule> Rules { get; } = new();
public ObservableCollection<WeaponOption> WeaponOptions { get; } = new();
public string[] DmgOptions { get; } =
{ "Auto", "Slash", "Pierce", "Bludgeon", "Fire", "Cold", "Lightning", "Acid", "Nether" };
public string[] VulnOptions { get; } =
{ "None", "Slash", "Pierce", "Bludgeon", "Fire", "Cold", "Lightning", "Acid", "Nether" };
public string[] PetOptions { get; } =
{ "PAuto", "Slash", "Pierce", "Bludgeon", "Fire", "Cold", "Lightning", "Acid", "Nether" };
/// <summary>RynthAi KiteModes: Default follows the DEFAULT row; PullAway tags then runs clear.</summary>
public string[] KiteOptions { get; } =
{ "Default", "Off", "PullAway" };
private string _charFolder = string.Empty;
private string _monstersPath = string.Empty;
private bool _initialized;
// Set when the monsters file couldn't be read: saving would replace it with what's on screen.
private bool _loadFailed;
public MainWindow()
{
InitializeComponent();
DataContext = this;
Opened += MainWindow_Opened;
}
private void InitializeComponent() => AvaloniaXamlLoader.Load(this);
private async void MainWindow_Opened(object? sender, EventArgs e)
{
if (_initialized) return;
_initialized = true;
string[] args = Environment.GetCommandLineArgs();
if (args.Length > 1)
Open(args[1].Trim('"'));
else
await PromptOpenFileAsync();
}
// ── Open / Load ──────────────────────────────────────────────────────────
private async System.Threading.Tasks.Task PromptOpenFileAsync()
{
var sp = StorageProvider;
if (sp == null) return;
var pick = await sp.OpenFilePickerAsync(new FilePickerOpenOptions
{
Title = "Open a monster rules file (RynthAi > MonsterProfiles > character.json)",
AllowMultiple = false,
FileTypeFilter = new[]
{
new FilePickerFileType("Monster Rules") { Patterns = new[] { "*.json" } },
new FilePickerFileType("All Files") { Patterns = new[] { "*" } },
}
});
var file = pick.FirstOrDefault();
if (file == null)
{
SetStatus("No file opened.", error: true);
return;
}
string? path = file.TryGetLocalPath();
if (!string.IsNullOrEmpty(path))
Open(path);
}
/// <summary>Opens a monsters file, or a character's settings folder (what the plugin passes):
/// the folder maps to the file the plugin reads, MonsterProfiles\(server)\(character).json
/// (MonsterRulesFile.ResolveRulesPath). Before 2026-10-05 the editor always used
/// monsters.json in that folder, which the plugin now moves to MonsterProfiles at load, so the
/// editor opened empty and its saves were never read once the plugin's own file existed.</summary>
private void Open(string pathOrFolder)
{
_monstersPath = MonsterRulesFile.ResolveRulesPath(pathOrFolder);
_charFolder = Directory.Exists(pathOrFolder) ? pathOrFolder : MonsterRulesFile.CharFolderForRulesPath(_monstersPath);
string who = Path.GetFileNameWithoutExtension(_monstersPath);
if (who.Equals("monsters", StringComparison.OrdinalIgnoreCase)) who = Path.GetFileName(_charFolder);
Title = $"RynthAi — Monster Rules — {who}";
LoadWeaponOptions();
LoadRules();
}
private void LoadWeaponOptions()
{
WeaponOptions.Clear();
WeaponOptions.Add(new WeaponOption { Id = 0, DisplayName = "<AUTO>" });
try
{
string markerPath = Path.Combine(_charFolder, "active_profile.txt");
string profile = File.Exists(markerPath) ? File.ReadAllText(markerPath).Trim() : "Default";
string profilePath = Path.Combine(_charFolder, profile + ".json");
if (File.Exists(profilePath))
{
string json = File.ReadAllText(profilePath);
var settings = JsonSerializer.Deserialize(json, EditorJsonContext.Default.EditorSettings);
if (settings?.ItemRules != null)
{
foreach (var item in settings.ItemRules)
WeaponOptions.Add(new WeaponOption { Id = item.Id, DisplayName = item.Name });
}
}
}
catch { /* weapon dropdowns will just show <AUTO> */ }
IdToNameConverter.Instance.Options = WeaponOptions;
OptionFromIdConverter.Instance.Options = WeaponOptions;
}
private void LoadRules()
{
foreach (var r in Rules) r.PropertyChanged -= Rule_Changed;
Rules.Clear();
_loadFailed = false;
try
{
foreach (var r in MonsterRulesFile.Load(_monstersPath)) AddRule(r);
}
catch (Exception ex)
{
_loadFailed = true;
SetStatus($"Load error: {ex.Message} - not saving over {_monstersPath}; fix the file and reopen it.", error: true);
return;
}
EnsureDefault();
SetStatus(Rules.Count > 0
? $"Loaded {Rules.Count} rule(s) from {_monstersPath}"
: $"No monsters.json found — starting empty. Saving will create {_monstersPath}");
}
private void EnsureDefault()
{
for (int i = Rules.Count - 1; i >= 1; i--)
if (Rules[i].IsDefault) { Rules[i].PropertyChanged -= Rule_Changed; Rules.RemoveAt(i); }
if (Rules.Count > 0 && Rules[0].IsDefault)
return;
int existing = -1;
for (int i = 0; i < Rules.Count; i++)
if (Rules[i].IsDefault) { existing = i; break; }
if (existing > 0)
Rules.Move(existing, 0);
else
Rules.Insert(0, new MonsterRule { Name = "Default", Priority = 1 });
Rules[0].PropertyChanged -= Rule_Changed;
Rules[0].PropertyChanged += Rule_Changed;
}
private void AddRule(MonsterRule rule)
{
rule.PropertyChanged += Rule_Changed;
Rules.Add(rule);
}
// ── Save ─────────────────────────────────────────────────────────────────
private void Rule_Changed(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
=> Save();
private void MonsterGrid_CellEditEnding(object? sender, DataGridCellEditEndingEventArgs e)
{
Dispatcher.UIThread.Post(Save, DispatcherPriority.Background);
}
private void Save()
{
if (string.IsNullOrEmpty(_monstersPath) || _loadFailed) return;
try
{
MonsterRulesFile.Save(_monstersPath, Rules);
SetStatus($"Saved {Rules.Count} rule(s) — {DateTime.Now:HH:mm:ss}");
}
catch (Exception ex) { SetStatus($"Save error: {ex.Message}", error: true); }
}
// ── Row actions ──────────────────────────────────────────────────────────
private void AddMonster_Click(object? sender, RoutedEventArgs e)
{
var nameBox = this.FindControl<TextBox>("NewMonsterBox");
if (nameBox == null) return;
string name = (nameBox.Text ?? string.Empty).Trim();
if (string.IsNullOrWhiteSpace(name)) return;
var rule = new MonsterRule { Name = name };
AddRule(rule);
var grid = this.FindControl<DataGrid>("MonsterGrid");
if (grid != null)
{
grid.ScrollIntoView(rule, null);
grid.SelectedItem = rule;
}
nameBox.Text = string.Empty;
Save();
}
private void NewMonsterBox_KeyDown(object? sender, KeyEventArgs e)
{
if (e.Key == Key.Enter) AddMonster_Click(sender, e);
}
private void DeleteRow_Click(object? sender, RoutedEventArgs e)
{
if (sender is Button { Tag: MonsterRule rule } && !rule.IsDefault)
{
rule.PropertyChanged -= Rule_Changed;
Rules.Remove(rule);
Save();
}
}
// ── Helpers ──────────────────────────────────────────────────────────────
private void SetStatus(string msg, bool error = false)
{
var status = this.FindControl<TextBlock>("StatusText");
if (status == null) return;
status.Foreground = error
? new SolidColorBrush(Color.FromRgb(0xF3, 0x8B, 0xA8))
: new SolidColorBrush(Color.FromRgb(0xA6, 0xE3, 0xA1));
status.Text = msg;
}
}
// ── Converters ────────────────────────────────────────────────────────────────
/// <summary>int weapon/offhand id → display name (read-only).</summary>
public class IdToNameConverter : IValueConverter
{
public static readonly IdToNameConverter Instance = new();
public IReadOnlyList<WeaponOption> Options { get; set; } = Array.Empty<WeaponOption>();
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is int id)
{
if (id == 0) return "<AUTO>";
foreach (var opt in Options)
if (opt.Id == id) return opt.DisplayName;
return $"<ID:{id}>";
}
return "<AUTO>";
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
=> throw new NotSupportedException();
}
/// <summary>int id ↔ WeaponOption. Used for ComboBox SelectedItem on an int-backed property.</summary>
public class OptionFromIdConverter : IValueConverter
{
public static readonly OptionFromIdConverter Instance = new();
public IReadOnlyList<WeaponOption> Options { get; set; } = Array.Empty<WeaponOption>();
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is int id)
foreach (var opt in Options)
if (opt.Id == id) return opt;
return null;
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
=> value is WeaponOption opt ? opt.Id : 0;
}
/// <summary>IsDefault → yellow; else default Foreground colour. WPF DataTriggers have no Avalonia equivalent.</summary>
public class DefaultFgConverter : IValueConverter
{
public static readonly DefaultFgConverter Instance = new();
private static readonly IBrush DefaultYellow = new SolidColorBrush(Color.FromRgb(0xF9, 0xE2, 0xAF));
private static readonly IBrush Normal = new SolidColorBrush(Color.FromRgb(0xCD, 0xD6, 0xF4));
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
=> value is true ? DefaultYellow : Normal;
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
=> throw new NotSupportedException();
}
public class DefaultWeightConverter : IValueConverter
{
public static readonly DefaultWeightConverter Instance = new();
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
=> value is true ? FontWeight.SemiBold : FontWeight.Normal;
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
=> throw new NotSupportedException();
}
/// <summary>True → false, False → true (IsVisible on Delete button for the Default row).</summary>
public class InvertBoolConverter : IValueConverter
{
public static readonly InvertBoolConverter Instance = new();
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
=> value is not true;
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
=> value is not true;
}