Permalink
Browse files

possibly temp hacky fix for (temp)banning issues

added a check to prevent simultanious reading/writing of the bans.json file. also removed the retarded code that made it refresh 3 times whenever a player joined/has joined.
  • Loading branch information...
TomGrobbe committed Aug 30, 2018
1 parent c39c854 commit 24957c6956354dd331c35ecc0c35896903efdd22
Showing with 55 additions and 29 deletions.
  1. +2 −2 vMenu/MainMenu.cs
  2. +50 −24 vMenuServer/BanManager.cs
  3. +3 −3 vMenuServer/MainServer.cs
@@ -380,7 +380,7 @@ private async Task OnTick()
// Request the permissions data from the server.
TriggerServerEvent("vMenu:RequestPermissions", PlayerId());
TriggerServerEvent("vMenu:RequestBanList", PlayerId());
//TriggerServerEvent("vMenu:RequestBanList", PlayerId());
// Wait until the data is received and the player's name is loaded correctly.
while (!permissionsSetupDone || !optionsSetupDone
@@ -609,7 +609,7 @@ private void CreateSubmenus()
}
if (Cf.IsAllowed(Permission.OPUnban))
{
TriggerServerEvent("vMenu:RequestBanList", PlayerId());
//TriggerServerEvent("vMenu:RequestBanList", PlayerId());
BannedPlayersMenu = new BannedPlayers();
UIMenu menu = BannedPlayersMenu.GetMenu();
UIMenuItem button = new UIMenuItem("Banned Players", "View and manage all banned players in this menu.");
@@ -13,6 +13,8 @@ namespace vMenuServer
{
public class BanManager : BaseScript
{
private static bool readingOrWritingToBanFile = false;
/// <summary>
/// Struct used to store bans.
/// </summary>
@@ -41,28 +43,36 @@ public BanManager()
EventHandlers.Add("vMenu:RequestPlayerUnban", new Action<Player, string>(RemoveBanRecord));
EventHandlers.Add("vMenu:RequestBanList", new Action<Player>(SendBanList));
BannedPlayersList = GetBanList();
//BannedPlayersList = await GetBanList();
}
/// <summary>
/// Sends the banlist (as json string) to the client.
/// </summary>
/// <param name="source"></param>
private void SendBanList([FromSource] Player source)
private async void SendBanList([FromSource] Player source)
{
BannedPlayersList = await GetBanList();
Log("Updating player with new banlist.\n");
source.TriggerEvent("vMenu:SetBanList", JsonConvert.SerializeObject(GetBanList()).ToString());
string data = JsonConvert.SerializeObject(BannedPlayersList).ToString();
//Debug.Write(data + "\n");
source.TriggerEvent("vMenu:SetBanList", data);
}
/// <summary>
/// Gets the ban list from the bans.json file.
/// </summary>
/// <returns></returns>
public static List<BanRecord> GetBanList()
public static async Task<List<BanRecord>> GetBanList()
{
while (readingOrWritingToBanFile)
{
await Delay(0);
}
readingOrWritingToBanFile = true;
var banList = new List<BanRecord>();
string bansJson = LoadResourceFile(GetCurrentResourceName(), "bans.json");
if (bansJson != null && bansJson != "")
if (bansJson != null && bansJson != "" && !string.IsNullOrEmpty(bansJson))
{
dynamic banRecords = JsonConvert.DeserializeObject(bansJson);
if (banRecords != null)
@@ -73,6 +83,7 @@ public static List<BanRecord> GetBanList()
}
}
}
readingOrWritingToBanFile = false;
return banList;
}
@@ -125,8 +136,9 @@ private static BanRecord JsonToBanRecord(dynamic br)
/// <param name="source"></param>
/// <param name="playerName"></param>
/// <param name="kickCallback"></param>
private void CheckForBans([FromSource]Player source, string playerName, CallbackDelegate kickCallback)
private async void CheckForBans([FromSource]Player source, string playerName, CallbackDelegate kickCallback)
{
BannedPlayersList = await GetBanList();
foreach (BanRecord ban in BannedPlayersList)
{
foreach (string identifier in source.Identifiers)
@@ -152,7 +164,7 @@ private void CheckForBans([FromSource]Player source, string playerName, Callback
}
else
{
if (RemoveBan(ban))
if (await RemoveBan(ban))
{
BanLog($"The following ban record has been removed (player unbanned). " +
$"The player has been unbanned because their ban duration expired. [Player: {ban.playerName} " +
@@ -177,7 +189,7 @@ private void CheckForBans([FromSource]Player source, string playerName, Callback
/// <param name="source">The player who triggered the event.</param>
/// <param name="targetPlayer">The player that needs to be banned.</param>
/// <param name="banReason">The reason why the player is getting banned.</param>
private void BanPlayer([FromSource] Player source, int targetPlayer, string banReason)
private async void BanPlayer([FromSource] Player source, int targetPlayer, string banReason)
{
if (IsPlayerAceAllowed(source.Handle, "vMenu.OnlinePlayers.PermBan") || IsPlayerAceAllowed(source.Handle, "vMenu.Everything") || IsPlayerAceAllowed(source.Handle, "vMenu.OnlinePlayers.All"))
{
@@ -194,13 +206,13 @@ private void BanPlayer([FromSource] Player source, int targetPlayer, string banR
identifiers = target.Identifiers.ToList<string>(),
playerName = GetSafePlayerName(target.Name)
};
if (AddBan(ban))
if (await AddBan(ban))
{
BanLog($"A new ban record has been added. Player: {ban.playerName} was banned by {ban.bannedBy} " +
$"for {ban.banReason} until {ban.bannedUntil} (forever).");
TriggerEvent("vMenu:BanSuccessful", JsonConvert.SerializeObject(ban).ToString());
BannedPlayersList = GetBanList();
target.Drop($"You have been permanently banned from this server. Banned by: {ban.bannedBy}. Ban reason: {ban.banReason}");
TriggerEvent("vMenu:BanSuccessful", JsonConvert.SerializeObject(ban).ToString());
BannedPlayersList = await GetBanList();
}
else
{
@@ -230,7 +242,7 @@ private void BanPlayer([FromSource] Player source, int targetPlayer, string banR
/// <param name="targetPlayer">Player who needs to be banned.</param>
/// <param name="banDurationHours">Ban duration in hours.</param>
/// <param name="banReason">Reason for the ban.</param>
private void BanPlayer([FromSource] Player source, int? targetPlayer, double banDurationHours, string banReason)
private async void BanPlayer([FromSource] Player source, int? targetPlayer, double banDurationHours, string banReason)
{
if (IsPlayerAceAllowed(source.Handle, "vMenu.OnlinePlayers.TempBan") || IsPlayerAceAllowed(source.Handle, "vMenu.Everything") ||
IsPlayerAceAllowed(source.Handle, "vMenu.OnlinePlayers.All"))
@@ -253,7 +265,7 @@ private void BanPlayer([FromSource] Player source, int? targetPlayer, double ban
};
Log("Record created.");
if (AddBan(ban))
if (await AddBan(ban))
{
Log("banning was successfull.");
BanLog($"A new ban record has been added. Player: {ban.playerName} was banned by " +
@@ -265,7 +277,7 @@ private void BanPlayer([FromSource] Player source, int? targetPlayer, double ban
{
Log("Saving of new ban failed. Reason: unknown. Maybe the file is broken?");
}
BannedPlayersList = GetBanList();
BannedPlayersList = await GetBanList();
string timeRemaining = GetRemainingTimeMessage(ban.bannedUntil.Subtract(DateTime.Now));
target.Drop($"You are banned from this server. Ban time remaining: {timeRemaining}. Banned by: {ban.bannedBy}. Ban reason: {ban.banReason}");
@@ -323,11 +335,11 @@ private string GetRemainingTimeMessage(TimeSpan remainingTime)
/// </summary>
/// <param name="ban"></param>
/// <returns></returns>
private static bool AddBan(BanRecord ban)
private static async Task<bool> AddBan(BanRecord ban)
{
Log("Refreshing banned players list.");
BannedPlayersList = GetBanList();
BannedPlayersList = await GetBanList();
var found = false;
foreach (BanRecord b in BannedPlayersList)
{
@@ -349,17 +361,24 @@ private static bool AddBan(BanRecord ban)
BannedPlayersList.Add(ban);
var output = JsonConvert.SerializeObject(BannedPlayersList);
return SaveResourceFile(GetCurrentResourceName(), "bans.json", output, output.Length);
while (readingOrWritingToBanFile)
{
await Delay(0);
}
readingOrWritingToBanFile = true;
bool successful = SaveResourceFile(GetCurrentResourceName(), "bans.json", output, output.Length);
readingOrWritingToBanFile = false;
return successful;
}
/// <summary>
/// Removes a ban record from the banned players list.
/// </summary>
/// <param name="record"></param>
/// <returns></returns>
public static bool RemoveBan(BanRecord record)
public static async Task<bool> RemoveBan(BanRecord record)
{
BannedPlayersList = GetBanList();
BannedPlayersList = await GetBanList();
List<int> itemsToRemove = new List<int>();
foreach (BanRecord ban in BannedPlayersList)
{
@@ -390,23 +409,30 @@ public static bool RemoveBan(BanRecord record)
}
}
var output = JsonConvert.SerializeObject(BannedPlayersList);
return SaveResourceFile(GetCurrentResourceName(), "bans.json", output, output.Length);
while (readingOrWritingToBanFile)
{
await Delay(0);
}
readingOrWritingToBanFile = true;
bool result = SaveResourceFile(GetCurrentResourceName(), "bans.json", output, output.Length);
readingOrWritingToBanFile = false;
return result;
}
/// <summary>
/// Removes a ban record.
/// </summary>
/// <param name="source"></param>
/// <param name="banRecordJsonString"></param>
private void RemoveBanRecord([FromSource]Player source, string banRecordJsonString)
private async void RemoveBanRecord([FromSource]Player source, string banRecordJsonString)
{
if (source != null && !string.IsNullOrEmpty(source.Name) && source.Name.ToLower() != "**invalid**" && source.Name.ToLower() != "** invalid **")
{
if (IsPlayerAceAllowed(source.Handle, "vMenu.OnlinePlayers.Unban"))
{
dynamic obj = JsonConvert.DeserializeObject(banRecordJsonString);
BanRecord ban = JsonToBanRecord(obj);
if (RemoveBan(ban))
if (await RemoveBan(ban))
{
BanLog($"The following ban record has been removed (player unbanned). " +
$"[Player: {ban.playerName} was banned by {ban.bannedBy} for {ban.banReason} until {ban.bannedUntil}.]");
@@ -430,7 +456,7 @@ private void RemoveBanRecord([FromSource]Player source, string banRecordJsonStri
/// Someone trying to trigger fake server events? Well, goodbye idiots.
/// </summary>
/// <param name="source"></param>
public static void BanCheater(Player source)
public static async void BanCheater(Player source)
{
var ban = new BanRecord()
{
@@ -441,7 +467,7 @@ public static void BanCheater(Player source)
playerName = GetSafePlayerName(source.Name)
};
if (AddBan(ban))
if (await AddBan(ban))
{
TriggerEvent("vMenu:BanCheaterSuccessful", JsonConvert.SerializeObject(ban).ToString());
BanLog($"A cheater has been banned. {JsonConvert.SerializeObject(ban).ToString()}");
@@ -315,7 +315,7 @@ public class MainServer : BaseScript
/// </summary>
public MainServer()
{
RegisterCommand("vmenuserver", new Action<int, List<object>, string>((int source, List<object> args, string rawCommand) =>
RegisterCommand("vmenuserver", new Action<int, List<object>, string>(async (int source, List<object> args, string rawCommand) =>
{
if (args != null)
{
@@ -341,11 +341,11 @@ public MainServer()
string name = args[1].ToString().Trim();
name = name.Replace("\"", "");
name = BanManager.GetSafePlayerName(name);
var bans = BanManager.GetBanList();
var bans = await BanManager.GetBanList();
var banRecord = bans.Find(b => { return b.playerName == name; });
if (banRecord.playerName != null)
{
if (BanManager.RemoveBan(banRecord))
if (await BanManager.RemoveBan(banRecord))
{
Debug.WriteLine("Player has been successfully unbanned.");
}

0 comments on commit 24957c6

Please sign in to comment.