From cb799e877e5d8f7ff1f45b0fc2e5adffb8140751 Mon Sep 17 00:00:00 2001 From: miniduikboot Date: Mon, 30 May 2022 23:29:01 +0200 Subject: [PATCH] Improve regionInfo.json writing logic Since a few version AU writes the regionInfo.json file after loading it. This triggers the write observer, causing a positive feedback loop. 70155fe8a6fdf443a6699f4fc25e6642b249d933 took the first stab at stopping this positive feedback, but unfortunately it isn't adequate enough when 4+ clients are running at the same time, so this commit takes it a step further by not writing the data if it didn't change, stopping the loop. --- Reactor/Patches/RegionInfoWatcher.cs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/Reactor/Patches/RegionInfoWatcher.cs b/Reactor/Patches/RegionInfoWatcher.cs index 8d346d6..91d3f6c 100644 --- a/Reactor/Patches/RegionInfoWatcher.cs +++ b/Reactor/Patches/RegionInfoWatcher.cs @@ -57,12 +57,25 @@ public void Dispose() [HarmonyPatch(typeof(FileIO), nameof(FileIO.WriteAllText))] private static class WritePatch { - public static void Prefix(string path) + public static bool Prefix(string path, string contents) { + // Among Us' region loading code unfortunately contains a call + // to SaveServers, which will write out the region file. This + // will lead to a positive feedback loop when detecting writes, + // which is undesireable. So we check if the write makes a + // change to the file on disk and if it would write the same + // file again, stop AU from actually writing it. if (ServerManager.Instance && path == ServerManager.Instance.serverInfoFileJson) { - PluginSingleton.Instance.RegionInfoWatcher.IgnoreNext = true; + var currentContents = FileIO.ReadAllText(path); + var continueWrite = currentContents != contents; + Logger.Debug($"Continue serverInfoFile write? {continueWrite}"); + // If we will write, ignore the next change action from the observer. + PluginSingleton.Instance.RegionInfoWatcher.IgnoreNext = continueWrite; + return continueWrite; } + + return true; } } }