From 3ac52091ea2cef67b8a410d817232093c584473f Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Fri, 29 Dec 2017 08:40:44 -0700 Subject: [PATCH] Move SendFileToUser to TSP.SendFileTextAsMessage. This is not a great method, but it's actually the only method in TShock that interpolates the %map% and %players% variables and it used in at least three places in the codebase. Since it's already so specialized, it's not worth changing it to take an actual File object, in my humble opinion. This also clarifies what the method does and what makes it special, as opposed to being fairly generic. --- CHANGELOG.md | 1 + TShockAPI/Commands.cs | 4 ++-- TShockAPI/TSPlayer.cs | 37 +++++++++++++++++++++++++++++++++++++ TShockAPI/TShock.cs | 2 +- TShockAPI/Utils.cs | 38 -------------------------------------- 5 files changed, 41 insertions(+), 41 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b51cfcdd..5fdfb994 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -76,6 +76,7 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * Moved `Utils.SendMultipleMatchError()` to `TSPlayer.SendMultipleMatchError`. (@hakusaro) * Removed `Utils.GetPlayers()`. Iterate over the TSPlayers on the server and make your own list. * Removed `Utils.HasBanExpired()` and replaced with `Bans.RemoveBanIfExpired()`. (@hakusaro) +* Removed `Utils.SendFileToUser()` and replaced with `TSPlayer.SendFileTextAsMessage()`. (@hakusaro) ## TShock 4.3.25 * Fixed a critical exploit in the Terraria protocol that could cause massive unpreventable world corruption as well as a number of other problems. Thanks to @bartico6 for reporting. Fixed by the efforts of @QuiCM, @hakusaro, and tips in the right directioon from @bartico6. diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index d7cdcf96..e6b7feca 100644 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -4979,12 +4979,12 @@ namespace TShockAPI private static void Motd(CommandArgs args) { - TShock.Utils.ShowFileToUser(args.Player, FileTools.MotdPath); + args.Player.SendFileTextAsMessage(FileTools.MotdPath); } private static void Rules(CommandArgs args) { - TShock.Utils.ShowFileToUser(args.Player, FileTools.RulesPath); + args.Player.SendFileTextAsMessage(FileTools.RulesPath); } private static void Whisper(CommandArgs args) diff --git a/TShockAPI/TSPlayer.cs b/TShockAPI/TSPlayer.cs index 24578de6..c71141e7 100644 --- a/TShockAPI/TSPlayer.cs +++ b/TShockAPI/TSPlayer.cs @@ -1458,6 +1458,43 @@ namespace TShockAPI SendDataFromPlayer(PacketTypes.SmartTextMessage, ply, msg, red, green, blue, -1); } + /// + /// Sends the text of a given file to the player. Replacement of %map% and %players% if in the file. + /// + /// Filename relative to + public void SendFileTextAsMessage(string file) + { + string foo = ""; + bool containsOldFormat = false; + using (var tr = new StreamReader(file)) + { + Color lineColor; + while ((foo = tr.ReadLine()) != null) + { + lineColor = Color.White; + if (string.IsNullOrWhiteSpace(foo)) + { + continue; + } + + var players = new List(); + + foreach (TSPlayer ply in TShock.Players) + { + if (ply != null && ply.Active) + { + players.Add(ply.Name); + } + } + + foo = foo.Replace("%map%", (TShock.Config.UseServerName ? TShock.Config.ServerName : Main.worldName)); + foo = foo.Replace("%players%", String.Join(",", players)); + + SendMessage(foo, lineColor); + } + } + } + /// /// Wounds the player with the given damage. /// diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 9549b6fb..26594077 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -1637,7 +1637,7 @@ namespace TShockAPI if (Config.DisplayIPToAdmins) Utils.SendLogs(string.Format("{0} has joined. IP: {1}", player.Name, player.IP), Color.Blue); - Utils.ShowFileToUser(player, FileTools.MotdPath); + player.SendFileTextAsMessage(FileTools.MotdPath); string pvpMode = Config.PvPMode.ToLowerInvariant(); if (pvpMode == "always") diff --git a/TShockAPI/Utils.cs b/TShockAPI/Utils.cs index 778d211c..2907abb8 100644 --- a/TShockAPI/Utils.cs +++ b/TShockAPI/Utils.cs @@ -479,44 +479,6 @@ namespace TShockAPI TShock.TileBans.UpdateBans(); } - /// - /// Shows a file to the user. - /// - /// Player the file contents will be sent to - /// Filename relative to - public void ShowFileToUser(TSPlayer player, string file) - { - string foo = ""; - bool containsOldFormat = false; - using (var tr = new StreamReader(file)) - { - Color lineColor; - while ((foo = tr.ReadLine()) != null) - { - lineColor = Color.White; - if (string.IsNullOrWhiteSpace(foo)) - { - continue; - } - - var players = new List(); - - foreach (TSPlayer ply in TShock.Players) - { - if (ply != null && ply.Active) - { - players.Add(ply.Name); - } - } - - foo = foo.Replace("%map%", (TShock.Config.UseServerName ? TShock.Config.ServerName : Main.worldName)); - foo = foo.Replace("%players%", String.Join(",", players)); - - player.SendMessage(foo, lineColor); - } - } - } - /// /// Returns a Group from the name of the group ///