using System; using System.Collections.Generic; using System.IO; using UnityEngine; namespace Amanotes { [Serializable] internal class DevModeConfig { private static readonly string configDir = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".amanotes"); private static readonly string configPath = Path.Combine(configDir, "dev-paths.json"); public List entries = new List(); public List ignoredPackages = new List(); public List searchPaths = new List(); [Serializable] internal class DevPathEntry { public string packageName; public string localPath; } internal static DevModeConfig Load() { if (!File.Exists(configPath)) return new DevModeConfig(); var json = File.ReadAllText(configPath); if (string.IsNullOrEmpty(json)) return new DevModeConfig(); var config = JsonUtility.FromJson(json); return config ?? new DevModeConfig(); } internal void Save() { if (!Directory.Exists(configDir)) Directory.CreateDirectory(configDir); File.WriteAllText(configPath, JsonUtility.ToJson(this, true)); } internal string GetLocalPath(string packageName) { if (string.IsNullOrEmpty(packageName)) return null; for (int i = 0; i < entries.Count; i++) { if (entries[i].packageName == packageName) return entries[i].localPath; } return null; } internal void SetLocalPath(string packageName, string localPath) { for (int i = 0; i < entries.Count; i++) { if (entries[i].packageName != packageName) continue; entries[i].localPath = localPath; LearnSearchPath(localPath); Save(); return; } entries.Add(new DevPathEntry { packageName = packageName, localPath = localPath }); LearnSearchPath(localPath); Save(); } internal void RemoveLocalPath(string packageName) { for (int i = entries.Count - 1; i >= 0; i--) { if (entries[i].packageName != packageName) continue; entries.RemoveAt(i); Save(); return; } } internal bool IsIgnored(string packageName) { return ignoredPackages.Contains(packageName); } internal void SetIgnored(string packageName, bool ignored) { if (ignored) { if (!ignoredPackages.Contains(packageName)) ignoredPackages.Add(packageName); } else { ignoredPackages.Remove(packageName); } Save(); } /// /// Records the parent folder of a local path as a search root for future auto-discovery. /// Normalizes the path to avoid duplicates from trailing separators or relative forms. /// private void LearnSearchPath(string localPath) { if (string.IsNullOrEmpty(localPath)) return; var parent = Path.GetDirectoryName(localPath); if (string.IsNullOrEmpty(parent)) return; parent = NormalizePath(parent); for (int i = 0; i < searchPaths.Count; i++) { if (NormalizePath(searchPaths[i]) == parent) return; } searchPaths.Add(parent); } /// /// Returns a canonical absolute path with trailing separators removed. /// private static string NormalizePath(string path) { return Path.GetFullPath(path).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); } } }