From 25d6ec8c2925a1a874bafc0363b7e92d9048d14b Mon Sep 17 00:00:00 2001 From: Karl Essinger Date: Sun, 20 Mar 2022 17:38:19 +0100 Subject: [PATCH] Fixed bug breaking file reading in some circumstances --- SCPermissions/SCPermissions.cs | 71 ++++++++++++++++++++-------------- SCPermissions/players.yml | 2 +- 2 files changed, 42 insertions(+), 31 deletions(-) diff --git a/SCPermissions/SCPermissions.cs b/SCPermissions/SCPermissions.cs index e7fe7b3..50a088d 100644 --- a/SCPermissions/SCPermissions.cs +++ b/SCPermissions/SCPermissions.cs @@ -191,24 +191,25 @@ private void LoadConfig() } // Reads config contents into FileStream - FileStream stream = File.OpenRead(FileManager.GetAppFolder(true, !GetConfigBool("scperms_config_global")) + "SCPermissions/config.yml"); - - // Converts the FileStream into a YAML Dictionary object - IDeserializer deserializer = new DeserializerBuilder().Build(); - object yamlObject = deserializer.Deserialize(new StreamReader(stream)); - - // Converts the YAML Dictionary into JSON String - ISerializer serializer = new SerializerBuilder().JsonCompatible().Build(); - string jsonString = serializer.Serialize(yamlObject); - - JObject json = JObject.Parse(jsonString); - - permissions = (JObject)json.SelectToken("permissions"); - verbose = json.SelectToken("verbose").Value(); - debug = json.SelectToken("debug").Value(); - defaultRank = json.SelectToken("defaultRank").Value(); - - this.Info("Config \"" + FileManager.GetAppFolder(true, !GetConfigBool("scperms_config_global")) + "SCPermissions/config.yml\" loaded."); + using (FileStream stream = File.OpenRead(FileManager.GetAppFolder(true, !GetConfigBool("scperms_config_global")) + "SCPermissions/config.yml")) + { + // Converts the FileStream into a YAML Dictionary object + IDeserializer deserializer = new DeserializerBuilder().Build(); + object yamlObject = deserializer.Deserialize(new StreamReader(stream)); + + // Converts the YAML Dictionary into JSON String + ISerializer serializer = new SerializerBuilder().JsonCompatible().Build(); + string jsonString = serializer.Serialize(yamlObject); + + JObject json = JObject.Parse(jsonString); + + permissions = (JObject)json.SelectToken("permissions"); + verbose = json.SelectToken("verbose").Value(); + debug = json.SelectToken("debug").Value(); + defaultRank = json.SelectToken("defaultRank").Value(); + + this.Info("Config \"" + FileManager.GetAppFolder(true, !GetConfigBool("scperms_config_global")) + "SCPermissions/config.yml\" loaded."); + } } /// @@ -226,13 +227,14 @@ private void LoadPlayerData() } // Reads config contents into FileStream - FileStream stream = File.OpenRead(FileManager.GetAppFolder(true, !GetConfigBool("scperms_playerdata_global")) + "SCPermissions/playerdata.yml"); - - // Converts the FileStream into a YAML Dictionary object - IDeserializer deserializer = new DeserializerBuilder().Build(); - playerRankDict = deserializer.Deserialize>>(new StreamReader(stream)) ?? new Dictionary>(); - - this.Info("Player data \"" + FileManager.GetAppFolder(true, !GetConfigBool("scperms_playerdata_global")) + "SCPermissions/playerdata.yml\" loaded."); + using (FileStream stream = File.OpenRead(FileManager.GetAppFolder(true, !GetConfigBool("scperms_playerdata_global")) + "SCPermissions/playerdata.yml")) + { + // Converts the FileStream into a YAML Dictionary object + IDeserializer deserializer = new DeserializerBuilder().Build(); + playerRankDict = deserializer.Deserialize>>(new StreamReader(stream)) ?? new Dictionary>(); + + this.Info("Player data \"" + FileManager.GetAppFolder(true, !GetConfigBool("scperms_playerdata_global")) + "SCPermissions/playerdata.yml\" loaded."); + } } /// @@ -240,15 +242,23 @@ private void LoadPlayerData() /// private void SavePlayerData() { - StringBuilder builder = new StringBuilder(); - foreach (KeyValuePair> playerRanks in playerRankDict) + try { - if(playerRanks.Value.Count > 0) + StringBuilder builder = new StringBuilder(); + foreach (KeyValuePair> playerRanks in playerRankDict) { - builder.Append(playerRanks.Key + ": [ \"" + string.Join("\", \"", playerRanks.Value) + "\" ]\n"); + if (playerRanks.Value.Count > 0) + { + builder.Append(playerRanks.Key + ": [ \"" + string.Join("\", \"", playerRanks.Value) + "\" ]\n"); + } } + File.WriteAllText(FileManager.GetAppFolder(true, !GetConfigBool("scperms_playerdata_global")) + "SCPermissions/playerdata.yml", builder.ToString()); } - File.WriteAllText(FileManager.GetAppFolder(true, !GetConfigBool("scperms_playerdata_global")) + "SCPermissions/playerdata.yml", builder.ToString()); + catch (Exception e) + { + this.Error("Exception occured when trying to save player data: " + e); + } + } /// @@ -406,6 +416,7 @@ public void RefreshVanillaRank(Player player) { if(player == null) { + this.Debug("Not refreshing vanilla rank, player not online."); return; } diff --git a/SCPermissions/players.yml b/SCPermissions/players.yml index 25cbae8..0518051 100644 --- a/SCPermissions/players.yml +++ b/SCPermissions/players.yml @@ -1 +1 @@ -76561198022373616@steam: [ "restricted" ] \ No newline at end of file +76561198022373616@steam: [ "restricted" ] # Example \ No newline at end of file