From 8df7e0c298bc6c7b4a14a72f57094c47d52f5dff Mon Sep 17 00:00:00 2001 From: DogooFalchion Date: Sun, 23 Oct 2016 14:37:16 -0400 Subject: [PATCH] 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 ///