namespace WiiCompiled.Setup.Common; /// /// A portable installation is a self-contained directory tree the user can move or carry on removable media: /// /// <root>\portable.txt marker; its contents are irrelevant /// <root>\Install\ the installation directory /// <root>\UserData\ runtime user state (Config.toml, NAND, Cache, Logs) /// /// The runtime finds the same root independently (runtime/include/runtime_config.h, /// PortableRootDirectory); this class must keep the same marker name, layout, and depth bound. /// Shared by both installers - Linux's CLI has no --portable flag, so it only ever calls /// // (always missing, /// since it never creates a marker file), not . /// public static class PortableRoot { public const string MarkerFileName = "portable.txt"; public const string UserDataDirectoryName = "UserData"; public const string InstallDirectoryName = "Install"; /// /// Parents searched above the start directory. Kept small (installs sit 2 levels below root, /// <root>\Install\Base\game.exe) so an unrelated marker far up a drive can't capture it. /// public const int MaximumSearchDepth = 4; /// /// The portable root belongs to, or null otherwise. Callers pass the /// installation directory (not the running executable's location), since a downloaded setup runs from Downloads. /// public static string? TryFind(string startDirectory) { if (string.IsNullOrWhiteSpace(startDirectory)) return null; string current; try { // Normalize before trimming: "C:\" trimmed to "C:" is the current directory on that // drive, not the drive root. current = Normalize(startDirectory); } catch (Exception ex) when (ex is ArgumentException or NotSupportedException or PathTooLongException) { return null; } for (var level = 0; level <= MaximumSearchDepth; level++) { if (File.Exists(Path.Combine(current, MarkerFileName))) return current; var parent = Path.GetDirectoryName(current); if (string.IsNullOrEmpty(parent) || parent.Equals(current, StringComparison.Ordinal)) break; current = parent; } return null; } public static string UserDataDirectory(string root) => Path.Combine(root, UserDataDirectoryName); /// Whether is or lives inside it. public static bool Contains(string root, string candidate) => FileSystemUtilities.PathContains(root, candidate); /// /// Establishes the root an install is about to publish into: the marker and the user-state /// directory must exist before anything resolves a configuration path, because that resolution is /// what decides whether the installation writes portable or machine-wide settings. /// public static string Create(string root) { var full = Normalize(root); EnsureUsableRoot(full); Directory.CreateDirectory(full); Directory.CreateDirectory(UserDataDirectory(full)); var marker = Path.Combine(full, MarkerFileName); if (!File.Exists(marker)) { File.WriteAllText(marker, "WiiCompiled portable installation." + Environment.NewLine + "This marker makes the runtime keep Config.toml, NAND, Cache, and Logs in UserData\\ " + "beside it instead of in %LOCALAPPDATA%." + Environment.NewLine + "Delete it to make this installation use per-user application data again." + Environment.NewLine); } return full; } /// /// A portable root is deleted wholesale by the user, so like an install directory it may never be a /// drive root or well-known system location; both share . /// public static void EnsureUsableRoot(string root) => FileSystemUtilities.EnsureUsableLocation(root, "A portable installation root"); private static string Normalize(string path) => FileSystemUtilities.NormalizePath(path); }