From ccb2a00a7113e3105242e88d9aade62610a1596a Mon Sep 17 00:00:00 2001 From: ZakFahey Date: Mon, 27 Jul 2020 10:05:28 -0700 Subject: [PATCH] Fix config file access errors when starting up multiple TShock servers at once Use case would be multi-server networks. This commit ensures that sscconfig.json is read with FileShare.Read enabled and stops writing to config.json and sscconfig.json on server load immediately after reading it if the file already exists, which is a no-op since neither of the config files have changed at that point. --- TShockAPI/FileTools.cs | 7 +++++-- TShockAPI/ServerSideCharacters/ServerSideConfig.cs | 3 ++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/TShockAPI/FileTools.cs b/TShockAPI/FileTools.cs index ba6a9272..314c7c5a 100644 --- a/TShockAPI/FileTools.cs +++ b/TShockAPI/FileTools.cs @@ -108,7 +108,10 @@ namespace TShockAPI TShock.Config = ConfigFile.Read(ConfigPath); // Add all the missing config properties in the json file } - TShock.Config.Write(ConfigPath); + else + { + TShock.Config.Write(ConfigPath); + } if (File.Exists(ServerSideCharacterConfigPath)) { @@ -127,8 +130,8 @@ namespace TShockAPI new NetItem(-16, 1, 0) } }; + TShock.ServerSideCharacterConfig.Write(ServerSideCharacterConfigPath); } - TShock.ServerSideCharacterConfig.Write(ServerSideCharacterConfigPath); } /// diff --git a/TShockAPI/ServerSideCharacters/ServerSideConfig.cs b/TShockAPI/ServerSideCharacters/ServerSideConfig.cs index 31d5caac..b1c6f6da 100644 --- a/TShockAPI/ServerSideCharacters/ServerSideConfig.cs +++ b/TShockAPI/ServerSideCharacters/ServerSideConfig.cs @@ -48,7 +48,8 @@ namespace TShockAPI.ServerSideCharacters public static ServerSideConfig Read(string path) { - using (var reader = new StreamReader(path)) + using (var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)) + using (var reader = new StreamReader(fileStream)) { string txt = reader.ReadToEnd(); var config = JsonConvert.DeserializeObject(txt);