using System; using System.Collections.Generic; using System.Globalization; using System.IO; namespace RynthCore.Plugin.RynthAi.LegacyUi; /// /// The file side of the Loaded files drawer's profile buttons (Save As, Rename, Move to Deleted) and /// their /ra verbs, for settings profiles, navs, metas and loot profiles (2026-11-11): the name /// typed checked as a file name (' rules), whether it would replace /// another file, and the Deleted folder. Nothing is ever deleted: a profile removed from its picker /// is moved into a "Deleted" folder beside it, and a file replaced by Save As has its old copy /// kept there first. The pickers list only their own folder, so nothing in Deleted shows in them; /// moving a file back out of Deleted (and dropping the time from its name) brings it back. /// Pure file work, so the offline tests cover it. /// internal static class ProfileFileOps { /// The folder, inside each profile folder, that removed and replaced profiles go to. public const string DeletedFolderName = "Deleted"; /// /// The name typed, tidied (whitespace, quotes and one of dropped) /// into . Null when it is safe as a file name, else why not. /// public static string? CleanName(string? raw, IReadOnlyList extensions, out string name) { name = NewProfileFiles.Tidy(raw, extensions, out _); return NewProfileFiles.Validate(name); } /// The same file (full paths, case ignored)? False when either is empty or not a path. public static bool SamePath(string? a, string? b) { if (string.IsNullOrWhiteSpace(a) || string.IsNullOrWhiteSpace(b)) return false; try { return string.Equals(Path.GetFullPath(a.Trim()), Path.GetFullPath(b.Trim()), StringComparison.OrdinalIgnoreCase); } catch { return false; } } /// /// Saving to would replace another file: it exists and it isn't /// (saving a profile onto its own file is a plain save). /// public static bool WouldReplace(string target, string? current) => File.Exists(target) && !SamePath(target, current); /// <folder of path>\Deleted. public static string DeletedFolderFor(string path) => Path.Combine(Path.GetDirectoryName(Path.GetFullPath(path)) ?? ".", DeletedFolderName); /// /// Where goes in the Deleted folder: its name with the time before the /// extension ("farm.nav" -> Deleted\farm.2026-11-11_153012.nav), and " (2)", " (3)" ... when /// that is taken, so an earlier copy is never replaced. /// public static string DeletedPathFor(string path, DateTime now) { string folder = DeletedFolderFor(path); string ext = Path.GetExtension(path); string stem = Path.GetFileNameWithoutExtension(path); string stamp = now.ToString("yyyy-MM-dd_HHmmss", CultureInfo.InvariantCulture); string candidate = Path.Combine(folder, $"{stem}.{stamp}{ext}"); for (int n = 2; File.Exists(candidate) || Directory.Exists(candidate); n++) candidate = Path.Combine(folder, $"{stem}.{stamp} ({n}){ext}"); return candidate; } /// /// Moves into its Deleted folder (made when missing). Never deletes: /// on any failure the file stays where it was. is where it went. /// public static bool MoveToDeleted(string path, DateTime now, out string moved, out string error) { moved = string.Empty; error = string.Empty; try { if (!File.Exists(path)) { error = $"{Path.GetFileName(path)} isn't there any more"; return false; } string target = DeletedPathFor(path, now); Directory.CreateDirectory(Path.GetDirectoryName(target)!); File.Move(path, target, overwrite: false); moved = target; return true; } catch (Exception ex) { error = ex.Message; return false; } } /// /// Keeps a copy of in its Deleted folder before it is replaced (Save As /// over another profile). Nothing to keep (no such file) is a success with an empty /// . /// public static bool CopyToDeleted(string path, DateTime now, out string copy, out string error) { copy = string.Empty; error = string.Empty; try { if (!File.Exists(path)) return true; string target = DeletedPathFor(path, now); Directory.CreateDirectory(Path.GetDirectoryName(target)!); File.Copy(path, target, overwrite: false); copy = target; return true; } catch (Exception ex) { error = ex.Message; return false; } } /// /// Renames to (same folder). Refused when /// is another file that exists (a change of case only is allowed). /// public static bool Rename(string from, string to, out string error) { error = string.Empty; try { if (!File.Exists(from)) { error = $"{Path.GetFileName(from)} isn't there any more"; return false; } if (string.Equals(Path.GetFullPath(from), Path.GetFullPath(to), StringComparison.Ordinal)) { error = "that is already its name"; return false; } if (File.Exists(to) && !SamePath(from, to)) { error = $"{Path.GetFileName(to)} already exists"; return false; } if (SamePath(from, to)) { // Only the case changes: Windows sees one file, so go through a temporary name. string tmp = from + "." + Environment.ProcessId + ".rename"; File.Move(from, tmp); try { File.Move(tmp, to); } catch { try { File.Move(tmp, from); } catch { } throw; } } else { File.Move(from, to, overwrite: false); } return true; } catch (Exception ex) { error = ex.Message; return false; } } }