using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using RynthCore.Install;
namespace RynthCore.Plugin.RynthAi;
///
/// A buff profile: the spells buffing keeps up, in cast order. Built in the Spells window
/// (/ra spells) and chosen under Loaded Files (Buffs). Without one, buffing uses its
/// built-in list.
///
public sealed class BuffProfile
{
public int Schema = BuffProfileStore.CurrentSchema;
public string Name = "";
public string Notes = "";
public List Entries = new();
}
/// One spell line in a .
public sealed class BuffProfileEntry
{
/// The spell picked in the Spells window (shown in the profile list).
public int SpellId;
public string Name = "";
///
/// Base name buffing resolves to the best tier the character knows ("Strength Self");
/// see .
///
public string Key = "";
/// ACE spell family: the fallback when the base name has no tier ladder.
public int FamilyId;
public string School = "";
public string Target = "";
public int Level;
public bool Enabled = true;
}
///
/// Buff profile files: <SuiteDir>\RynthAi\BuffProfiles\<name>.json, shared by
/// every character like the Nav, Loot and Meta folders.
///
internal static class BuffProfileStore
{
public const int CurrentSchema = 1;
public const string FileExtension = ".json";
/// Tests point the folder at a temp directory.
internal static string? FolderOverride;
public static string Folder => FolderOverride ?? Path.Combine(RynthInstallPaths.RynthAiDir, "BuffProfiles");
/// Full path for a profile name (sanitised for Windows file names).
public static string PathFor(string profileName) =>
Path.Combine(Folder, SanitizeFileName(profileName) + FileExtension);
/// Profile file names (with extension) in the folder, sorted.
public static List ListFileNames()
{
var list = new List();
try
{
if (!Directory.Exists(Folder)) return list;
foreach (string f in Directory.GetFiles(Folder, "*" + FileExtension))
list.Add(Path.GetFileName(f));
list.Sort(StringComparer.OrdinalIgnoreCase);
}
catch { /* folder unreadable: no profiles */ }
return list;
}
public static bool TryLoad(string path, out BuffProfile profile, out string error)
{
profile = new BuffProfile();
error = "";
try
{
if (string.IsNullOrWhiteSpace(path) || !File.Exists(path)) { error = "file not found"; return false; }
var p = JsonSerializer.Deserialize(File.ReadAllText(path), RynthAiJsonContext.Default.BuffProfile);
if (p == null) { error = "empty file"; return false; }
if (p.Schema > CurrentSchema) { error = $"made by a newer RynthAi (schema {p.Schema})"; return false; }
Normalize(p, Path.GetFileNameWithoutExtension(path));
profile = p;
return true;
}
catch (Exception ex)
{
error = ex.Message;
return false;
}
}
/// Writes the profile (temp file + move, so a crash never leaves half a file).
public static void Save(BuffProfile profile, string path)
{
Normalize(profile, Path.GetFileNameWithoutExtension(path));
profile.Schema = CurrentSchema;
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
string tmp = path + ".tmp";
File.WriteAllText(tmp, JsonSerializer.Serialize(profile, RynthAiJsonContext.Default.BuffProfile));
File.Move(tmp, path, overwrite: true);
}
///
/// The Spells window's Delete: the profile is moved to BuffProfiles\Deleted (never deleted;
/// LegacyUi/ProfileFileOps.cs), so it leaves the list and can be moved back.
///
public static bool Delete(string path) =>
RynthCore.Plugin.RynthAi.LegacyUi.ProfileFileOps.MoveToDeleted(path, RynthClock.Now, out _, out _);
/// An entry for a catalog spell, with its buffing key worked out.
public static BuffProfileEntry EntryFor(SpellCatalogEntry e) => new()
{
SpellId = e.Id,
Name = e.Name,
Key = SpellCatalog.BuffKeyFor(e),
FamilyId = e.FamilyId,
School = e.School,
Target = e.Target,
Level = e.Level,
Enabled = true,
};
/// True when an entry with the same buffing key is already in the profile.
public static bool ContainsKey(BuffProfile p, string key)
{
foreach (var e in p.Entries)
if (e.Key.Equals(key, StringComparison.OrdinalIgnoreCase)) return true;
return false;
}
///
/// Catalog spell for one of buffing's built-in base names ("Strength Self", "Blade Bane",
/// "Blood Drinker Self"), used to start a profile from the built-in list. Null when the
/// catalog has no tier ladder for it.
///
public static SpellCatalogEntry? CatalogSpellForBaseName(string baseName)
{
string[] candidates =
{
baseName + " I", "Aura of " + baseName + " I", baseName + " II", "Aura of " + baseName + " II",
baseName, "Incantation of " + baseName,
};
foreach (string c in candidates)
{
var e = SpellCatalog.GetByName(c);
if (e != null) return e;
}
return null;
}
/// Repairs nulls, blank keys and the name after loading or before saving.
public static void Normalize(BuffProfile p, string fallbackName)
{
p.Name = string.IsNullOrWhiteSpace(p.Name) ? fallbackName : p.Name.Trim();
p.Notes ??= "";
p.Entries ??= new List();
p.Entries.RemoveAll(e => e == null || (e.SpellId == 0 && string.IsNullOrWhiteSpace(e.Key)));
foreach (var e in p.Entries)
{
e.Name ??= "";
e.School ??= "";
e.Target ??= "";
e.Key ??= "";
// Hand-edited files may hold only a spell id: fill the rest from the catalog.
var c = e.SpellId != 0 ? SpellCatalog.Get(e.SpellId) : null;
if (c != null)
{
if (e.Name.Length == 0) e.Name = c.Name;
if (e.Key.Length == 0) e.Key = SpellCatalog.BuffKeyFor(c);
if (e.FamilyId == 0) e.FamilyId = c.FamilyId;
if (e.School.Length == 0) e.School = c.School;
if (e.Target.Length == 0) e.Target = c.Target;
if (e.Level == 0) e.Level = c.Level;
}
if (e.Key.Length == 0) e.Key = SpellCatalog.StripTier(e.Name);
}
}
/// Strips characters Windows forbids in file names; "Default" when nothing is left.
public static string SanitizeFileName(string name)
{
string t = (name ?? "").Trim();
foreach (char c in Path.GetInvalidFileNameChars()) t = t.Replace(c, '_');
if (t.Length > 80) t = t.Substring(0, 80);
return t.Length == 0 ? "Default" : t;
}
}