static SolutionFileFixer() { AssemblyReloadEvents.beforeAssemblyReload += OnBeforeAssemblyReload; SolutionFileWatcher.solutionFileChanged += OnSolutionFileChanged; SolutionFileWatcher.csprojFileChanged += OnCsprojFileChanged; SolutionFileWatcher.Start(); FixSolutionFile(); }
private static void FixSolutionFile(string solutionPath) { var solutionName = Path.GetFileNameWithoutExtension(solutionPath); var fixPattern = $"( = \"{solutionName}\",) \"(.*)[.]csproj\""; var fixRegex = new Regex(fixPattern); var fixCount = 0; var lines = File.ReadAllLines(solutionPath); for (int i = 0, n = lines.Length; i < n; ++i) { var line = lines[i]; if (line.StartsWith("Project(\"{") && fixRegex.IsMatch(line)) { var matches = fixRegex.Matches(line); foreach (Match match in matches) { var csprojName = match.Groups[2].Value; if (csprojName == solutionName) { continue; } var oldSubstring = match.Groups[1].Value; var newSubstring = $" = \"{csprojName}\","; if (newSubstring == oldSubstring) { continue; } fixCount += 1; lines[i] = line.Replace(oldSubstring, newSubstring); } } } if (fixCount > 0) { var writeTime = File.GetLastWriteTime(solutionPath); SolutionFileWatcher.Pause(); var text = string.Join("\n", lines); File.WriteAllText(solutionPath, text); File.SetLastWriteTime(solutionPath, writeTime); SolutionFileWatcher.Resume(); UnityEngine.Debug.Log($"fixed solution file: {solutionPath}"); } }
//---------------------------------------------------------------------- private static void OnBeforeAssemblyReload() { SolutionFileWatcher.Stop(); }