Skip to content
This repository has been archived by the owner on Apr 7, 2023. It is now read-only.

Commit

Permalink
Fixed bug breaking file reading in some circumstances
Browse files Browse the repository at this point in the history
  • Loading branch information
KarlOfDuty committed Mar 20, 2022
1 parent 5ad279e commit 25d6ec8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 31 deletions.
71 changes: 41 additions & 30 deletions SCPermissions/SCPermissions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>();
debug = json.SelectToken("debug").Value<bool>();
defaultRank = json.SelectToken("defaultRank").Value<string>();

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<bool>();
debug = json.SelectToken("debug").Value<bool>();
defaultRank = json.SelectToken("defaultRank").Value<string>();

this.Info("Config \"" + FileManager.GetAppFolder(true, !GetConfigBool("scperms_config_global")) + "SCPermissions/config.yml\" loaded.");
}
}

/// <summary>
Expand All @@ -226,29 +227,38 @@ 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<Dictionary<string, HashSet<string>>>(new StreamReader(stream)) ?? new Dictionary<string, HashSet<string>>();

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<Dictionary<string, HashSet<string>>>(new StreamReader(stream)) ?? new Dictionary<string, HashSet<string>>();

this.Info("Player data \"" + FileManager.GetAppFolder(true, !GetConfigBool("scperms_playerdata_global")) + "SCPermissions/playerdata.yml\" loaded.");
}
}

/// <summary>
/// Saves player data to file
/// </summary>
private void SavePlayerData()
{
StringBuilder builder = new StringBuilder();
foreach (KeyValuePair<string, HashSet<string>> playerRanks in playerRankDict)
try
{
if(playerRanks.Value.Count > 0)
StringBuilder builder = new StringBuilder();
foreach (KeyValuePair<string, HashSet<string>> 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);
}

}

/// <summary>
Expand Down Expand Up @@ -406,6 +416,7 @@ public void RefreshVanillaRank(Player player)
{
if(player == null)
{
this.Debug("Not refreshing vanilla rank, player not online.");
return;
}

Expand Down
2 changes: 1 addition & 1 deletion SCPermissions/players.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
76561198022373616@steam: [ "restricted" ]
76561198022373616@steam: [ "restricted" ] # Example

0 comments on commit 25d6ec8

Please sign in to comment.