From 6242a54603ed7be077e33fd75cb434bbcafb6b00 Mon Sep 17 00:00:00 2001 From: DogooFalchion Date: Sat, 22 Oct 2016 17:37:00 -0400 Subject: [PATCH 1/6] Update references to MOTD to point to the variable, instead of having magic strings. --- TShockAPI/Commands.cs | 2 +- TShockAPI/FileTools.cs | 2 +- TShockAPI/Rest/RestManager.cs | 2 +- TShockAPI/TShock.cs | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index 2fcfa3c7..a7dfef83 100755 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -4806,7 +4806,7 @@ namespace TShockAPI private static void Motd(CommandArgs args) { - TShock.Utils.ShowFileToUser(args.Player, "motd.txt"); + TShock.Utils.ShowFileToUser(args.Player, FileTools.MotdPath); } private static void Rules(CommandArgs args) diff --git a/TShockAPI/FileTools.cs b/TShockAPI/FileTools.cs index a85c269f..159f6a71 100644 --- a/TShockAPI/FileTools.cs +++ b/TShockAPI/FileTools.cs @@ -40,7 +40,7 @@ namespace TShockAPI /// internal static string MotdPath { - get { return Path.Combine(TShock.SavePath, "motd.txt"); } + get { return Path.Combine(TShock.SavePath, FileTools.MotdPath); } } /// diff --git a/TShockAPI/Rest/RestManager.cs b/TShockAPI/Rest/RestManager.cs index d74aa6c5..2bbc5e24 100644 --- a/TShockAPI/Rest/RestManager.cs +++ b/TShockAPI/Rest/RestManager.cs @@ -280,7 +280,7 @@ namespace TShockAPI [Token] private object ServerMotd(RestRequestArgs args) { - string motdFilePath = Path.Combine(TShock.SavePath, "motd.txt"); + string motdFilePath = Path.Combine(TShock.SavePath, FileTools.MotdPath); if (!File.Exists(motdFilePath)) return this.RestError("The motd.txt was not found.", "500"); diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 2d3c4026..3ca45e51 100755 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -1173,7 +1173,7 @@ namespace TShockAPI { if (args.Handled) return; - + if (!Config.AllowCrimsonCreep && (args.Type == TileID.Dirt || args.Type == TileID.FleshWeeds || TileID.Sets.Crimson[args.Type])) { @@ -1577,7 +1577,7 @@ namespace TShockAPI if (Config.DisplayIPToAdmins) Utils.SendLogs(string.Format("{0} has joined. IP: {1}", player.Name, player.IP), Color.Blue); - Utils.ShowFileToUser(player, "motd.txt"); + Utils.ShowFileToUser(player, FileTools.MotdPath); string pvpMode = Config.PvPMode.ToLowerInvariant(); if (pvpMode == "always") From 998bf71b96e14a5bb420cf8fd37f03a5502ec046 Mon Sep 17 00:00:00 2001 From: DogooFalchion Date: Sat, 22 Oct 2016 18:55:42 -0400 Subject: [PATCH 2/6] Don't reference yourself. --- TShockAPI/FileTools.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TShockAPI/FileTools.cs b/TShockAPI/FileTools.cs index 159f6a71..a85c269f 100644 --- a/TShockAPI/FileTools.cs +++ b/TShockAPI/FileTools.cs @@ -40,7 +40,7 @@ namespace TShockAPI /// internal static string MotdPath { - get { return Path.Combine(TShock.SavePath, FileTools.MotdPath); } + get { return Path.Combine(TShock.SavePath, "motd.txt"); } } /// From 31794b6a2713c667bd3d72421f22c176dcd54783 Mon Sep 17 00:00:00 2001 From: DogooFalchion Date: Sat, 22 Oct 2016 19:01:27 -0400 Subject: [PATCH 3/6] ShowFileToUser shouldn't assume the file is in the TShock folder. Use the file path variables where we can. Use smart text instead of removing color parsing. --- TShockAPI/Commands.cs | 2 +- TShockAPI/Utils.cs | 39 +++++++++++++++++++++++++++++++-------- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index a7dfef83..138892e8 100755 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -4811,7 +4811,7 @@ namespace TShockAPI private static void Rules(CommandArgs args) { - TShock.Utils.ShowFileToUser(args.Player, "rules.txt"); + TShock.Utils.ShowFileToUser(args.Player, FileTools.RulesPath); } private static void Whisper(CommandArgs args) diff --git a/TShockAPI/Utils.cs b/TShockAPI/Utils.cs index 6af27068..d6fc4a3b 100644 --- a/TShockAPI/Utils.cs +++ b/TShockAPI/Utils.cs @@ -50,7 +50,8 @@ namespace TShockAPI /// instance - an instance of the utils class private static readonly Utils instance = new Utils(); - private Regex byteRegex = new Regex("%\\s*(?\\d{1,3})\\s*,\\s*(?\\d{1,3})\\s*,\\s*(?\\d{1,3})\\s*%"); + /// This regex will look for the old MotD format for colors and replace them with the new chat format. + private Regex motdColorRegex = new Regex(@"\%\s*(?\d{1,3})\s*,\s*(?\d{1,3})\s*,\s*(?\d{1,3})\s*\%(?((?!\%\s*\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}\s*\%).)*)"); /// Utils - Creates a utilities object. private Utils() {} @@ -193,7 +194,7 @@ namespace TShockAPI TSPlayer.Server.SendMessage(log, color); foreach (TSPlayer player in TShock.Players) { - if (player != null && player != excludedPlayer && player.Active && player.HasPermission(Permissions.logs) && + if (player != null && player != excludedPlayer && player.Active && player.HasPermission(Permissions.logs) && player.DisplayLogs && TShock.Config.DisableSpewLogs == false) player.SendMessage(log, color); } @@ -502,7 +503,7 @@ namespace TShockAPI } return found; } - + /// /// Gets a prefix by ID or name /// @@ -557,7 +558,7 @@ namespace TShockAPI } /// - /// Stops the server after kicking all players with a reason message, and optionally saving the world then attempts to + /// Stops the server after kicking all players with a reason message, and optionally saving the world then attempts to /// restart it. /// /// bool perform a world save before stop (default: true) @@ -683,7 +684,7 @@ namespace TShockAPI { TShock.Bans.RemoveBan(ban.IP, false, false, false); } - + return true; } @@ -699,7 +700,7 @@ namespace TShockAPI { string foo = ""; bool containsOldFormat = false; - using (var tr = new StreamReader(Path.Combine(TShock.SavePath, file))) + using (var tr = new StreamReader(file)) { while ((foo = tr.ReadLine()) != null) { @@ -710,18 +711,40 @@ namespace TShockAPI foo = foo.Replace("%map%", (TShock.Config.UseServerName ? TShock.Config.ServerName : Main.worldName)); foo = foo.Replace("%players%", String.Join(",", GetPlayers(false))); - if (byteRegex.IsMatch(foo) && !containsOldFormat) + + string newFoo = ReplaceDeprecatedColorCodes(foo); + if (newFoo != foo && !containsOldFormat) { TShock.Log.ConsoleInfo($"You are using an old color format in file {file}."); TShock.Log.ConsoleInfo("To send coloured text please use Terraria's inbuilt format of: [c/#hex:text]."); TShock.Log.ConsoleInfo("For example: [c/ff00aa:This is a message!]."); containsOldFormat = true; } + foo = newFoo; + player.SendMessage(foo, Color.White); } } } + /// + /// Returns a string with deprecated %###,###,###% formats replaced with the new chat format colors. + /// + /// The input string + /// A replaced version of the input with the new chat color format. + private string ReplaceDeprecatedColorCodes(string input) + { + String tempString = input; + Match match = null; + + while ((match = motdColorRegex.Match(tempString)).Success) + { + tempString = tempString.Replace(match.Groups[0].Value, String.Format("[c/{0:X2}{1:X2}{2:X2}:{3}]", Int32.Parse(match.Groups["r"].Value), Int32.Parse(match.Groups["g"].Value), Int32.Parse(match.Groups["b"].Value), match.Groups["text"])); + } + + return tempString; + } + /// /// Returns a Group from the name of the group /// @@ -874,7 +897,7 @@ namespace TShockAPI int num; if (!int.TryParse(sb.ToString(), out num)) return false; - + sb.Clear(); switch (str[i]) { From a2003f130b4d3a09649c47cb59bc1c63057751c5 Mon Sep 17 00:00:00 2001 From: DogooFalchion Date: Sat, 22 Oct 2016 19:03:32 -0400 Subject: [PATCH 4/6] Do not append Tshock to the MotD path. --- TShockAPI/Rest/RestManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TShockAPI/Rest/RestManager.cs b/TShockAPI/Rest/RestManager.cs index 2bbc5e24..9c669a38 100644 --- a/TShockAPI/Rest/RestManager.cs +++ b/TShockAPI/Rest/RestManager.cs @@ -280,7 +280,7 @@ namespace TShockAPI [Token] private object ServerMotd(RestRequestArgs args) { - string motdFilePath = Path.Combine(TShock.SavePath, FileTools.MotdPath); + string motdFilePath = FileTools.MotdPath; if (!File.Exists(motdFilePath)) return this.RestError("The motd.txt was not found.", "500"); From fcca88c2d6504c7dfb26ec11273cc2e01b28153d Mon Sep 17 00:00:00 2001 From: DogooFalchion Date: Sun, 23 Oct 2016 14:01:36 -0400 Subject: [PATCH 5/6] Add support for start of line colors and terraria chat tags being nested. --- TShockAPI/Utils.cs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/TShockAPI/Utils.cs b/TShockAPI/Utils.cs index d6fc4a3b..c1869181 100644 --- a/TShockAPI/Utils.cs +++ b/TShockAPI/Utils.cs @@ -51,7 +51,10 @@ namespace TShockAPI private static readonly Utils instance = new Utils(); /// This regex will look for the old MotD format for colors and replace them with the new chat format. - private Regex motdColorRegex = new Regex(@"\%\s*(?\d{1,3})\s*,\s*(?\d{1,3})\s*,\s*(?\d{1,3})\s*\%(?((?!\%\s*\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}\s*\%).)*)"); + private Regex motdColorRegex = new Regex(@"\%\s*(?\d{1,3})\s*,\s*(?\d{1,3})\s*,\s*(?\d{1,3})\s*\%(?((?!(\%\s*\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}\s*\%)|(\[[a-zA-Z]/[^:]+:[^\]]*\])).)*)"); + + /// Matches the start of a line with our legacy color format + private Regex startOfLineColorRegex = new Regex(@"^\%\s*(?\d{1,3})\s*,\s*(?\d{1,3})\s*,\s*(?\d{1,3})\s*\%"); /// Utils - Creates a utilities object. private Utils() {} @@ -702,8 +705,10 @@ namespace TShockAPI bool containsOldFormat = false; using (var tr = new StreamReader(file)) { + Color lineColor; while ((foo = tr.ReadLine()) != null) { + lineColor = Color.White; if (string.IsNullOrWhiteSpace(foo)) { continue; @@ -712,6 +717,14 @@ namespace TShockAPI foo = foo.Replace("%map%", (TShock.Config.UseServerName ? TShock.Config.ServerName : Main.worldName)); foo = foo.Replace("%players%", String.Join(",", GetPlayers(false))); + var legacyColorMatch = startOfLineColorRegex.Match(foo); + if (legacyColorMatch.Success) + { + lineColor = new Color(Int32.Parse(legacyColorMatch.Groups["r"].Value), + Int32.Parse(legacyColorMatch.Groups["g"].Value), + Int32.Parse(legacyColorMatch.Groups["b"].Value)); + foo = foo.Replace(legacyColorMatch.Groups[0].Value, ""); + } string newFoo = ReplaceDeprecatedColorCodes(foo); if (newFoo != foo && !containsOldFormat) { @@ -722,7 +735,7 @@ namespace TShockAPI } foo = newFoo; - player.SendMessage(foo, Color.White); + player.SendMessage(foo, lineColor); } } } From 8df7e0c298bc6c7b4a14a72f57094c47d52f5dff Mon Sep 17 00:00:00 2001 From: DogooFalchion Date: Sun, 23 Oct 2016 14:37:16 -0400 Subject: [PATCH 6/6] Backup old MotD and convert it to using Terraria chat tags automatically, then write it back out to the MotD file. --- TShockAPI/TShock.cs | 2 ++ TShockAPI/Utils.cs | 57 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 3ca45e51..24636c7f 100755 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -837,6 +837,8 @@ namespace TShockAPI ComputeMaxStyles(); FixChestStacks(); + Utils.UpgradeMotD(); + if (Config.UseServerName) { Main.worldName = Config.ServerName; diff --git a/TShockAPI/Utils.cs b/TShockAPI/Utils.cs index c1869181..3aa665ff 100644 --- a/TShockAPI/Utils.cs +++ b/TShockAPI/Utils.cs @@ -725,8 +725,10 @@ namespace TShockAPI Int32.Parse(legacyColorMatch.Groups["b"].Value)); foo = foo.Replace(legacyColorMatch.Groups[0].Value, ""); } - string newFoo = ReplaceDeprecatedColorCodes(foo); - if (newFoo != foo && !containsOldFormat) + + bool upgraded = false; + string newFoo = ReplaceDeprecatedColorCodes(foo, out upgraded); + if (upgraded && !containsOldFormat) { TShock.Log.ConsoleInfo($"You are using an old color format in file {file}."); TShock.Log.ConsoleInfo("To send coloured text please use Terraria's inbuilt format of: [c/#hex:text]."); @@ -744,20 +746,69 @@ namespace TShockAPI /// Returns a string with deprecated %###,###,###% formats replaced with the new chat format colors. /// /// The input string + /// An out parameter that denotes if this line of text was upgraded. /// A replaced version of the input with the new chat color format. - private string ReplaceDeprecatedColorCodes(string input) + private string ReplaceDeprecatedColorCodes(string input, out bool upgradedFormat) { String tempString = input; Match match = null; + bool uFormat = false; while ((match = motdColorRegex.Match(tempString)).Success) { + uFormat = true; tempString = tempString.Replace(match.Groups[0].Value, String.Format("[c/{0:X2}{1:X2}{2:X2}:{3}]", Int32.Parse(match.Groups["r"].Value), Int32.Parse(match.Groups["g"].Value), Int32.Parse(match.Groups["b"].Value), match.Groups["text"])); } + upgradedFormat = uFormat; return tempString; } + /// + /// Upgrades a legacy MotD file to the new terraria chat tags version. + /// + public void UpgradeMotD() + { + string foo = ""; + StringBuilder motd = new StringBuilder(); + bool informedOwner = false; + using (var tr = new StreamReader(FileTools.MotdPath)) + { + Color lineColor; + while ((foo = tr.ReadLine()) != null) + { + lineColor = Color.White; + var legacyColorMatch = startOfLineColorRegex.Match(foo); + if (legacyColorMatch.Success) + { + lineColor = new Color(Int32.Parse(legacyColorMatch.Groups["r"].Value), + Int32.Parse(legacyColorMatch.Groups["g"].Value), + Int32.Parse(legacyColorMatch.Groups["b"].Value)); + foo = foo.Replace(legacyColorMatch.Groups[0].Value, ""); + } + + bool upgraded = false; + string newFoo = ReplaceDeprecatedColorCodes(foo, out upgraded); + if (!informedOwner && upgraded) + { + informedOwner = true; + TShock.Log.ConsoleInfo("We have upgraded your MotD to the new format. A backup has been created."); + } + + if (lineColor != Color.White) + motd.Append(String.Format("%{0:d3},{1:d3},{2:d3}%", lineColor.R, lineColor.G, lineColor.B)); + + motd.AppendLine(newFoo); + } + } + + if (informedOwner) + { + File.Copy(FileTools.MotdPath, String.Format("{0}_{1}.backup", FileTools.MotdPath, DateTime.Now.ToString("ddMMMyy_hhmmss"))); + File.WriteAllText(FileTools.MotdPath, motd.ToString()); + } + } + /// /// Returns a Group from the name of the group ///