From 8af406160635786ce07266c46ae08a5a19bf435d Mon Sep 17 00:00:00 2001 From: CoderCow Date: Sat, 29 Jun 2013 10:57:13 +0200 Subject: [PATCH 1/5] Added header to PaginationTools.cs. --- TShockAPI/PaginationTools.cs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/TShockAPI/PaginationTools.cs b/TShockAPI/PaginationTools.cs index f7ab193c..07ae22fd 100644 --- a/TShockAPI/PaginationTools.cs +++ b/TShockAPI/PaginationTools.cs @@ -1,4 +1,21 @@ -using System; +/* +TShock, a server mod for Terraria +Copyright (C) 2011-2012 The TShock Team + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ +using System; using System.Collections; using System.Collections.Generic; using System.Linq; From 863082dfc3c708adf80c9632ada219c899fe1cbc Mon Sep 17 00:00:00 2001 From: CoderCow Date: Sat, 29 Jun 2013 12:15:32 +0200 Subject: [PATCH 2/5] Added "-logpath" command line parameter. Added "LogPath" config setting. --- TShockAPI/ConfigFile.cs | 2 + TShockAPI/TShock.cs | 92 +++++++++++++++++++++++++++-------------- 2 files changed, 63 insertions(+), 31 deletions(-) diff --git a/TShockAPI/ConfigFile.cs b/TShockAPI/ConfigFile.cs index d1e800ad..9b5b40c6 100644 --- a/TShockAPI/ConfigFile.cs +++ b/TShockAPI/ConfigFile.cs @@ -249,6 +249,8 @@ namespace TShockAPI [Description("Allows stacks in chests to be beyond the stack limit")] public bool IgnoreChestStacksOnLoad = false; + [Description("The path of the directory where logs should be written into.")] public string LogPath = "tshock"; + /// /// Reads a configuration file from a given path /// diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 3a8908fb..cd85d530 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -40,13 +40,15 @@ namespace TShockAPI [APIVersion(1, 13)] public class TShock : TerrariaPlugin { - private const string LogFormatDefault = "yyyy-MM-dd_HH-mm-ss"; - private static string LogFormat = LogFormatDefault; - private static bool LogClear = false; public static readonly Version VersionNum = Assembly.GetExecutingAssembly().GetName().Version; public static readonly string VersionCodename = "Welcome to the future."; public static string SavePath = "tshock"; + private const string LogFormatDefault = "yyyy-MM-dd_HH-mm-ss"; + private static string LogFormat = LogFormatDefault; + private const string LogPathDefault = "tshock"; + private static string LogPath = LogPathDefault; + private static bool LogClear = false; public static TSPlayer[] Players = new TSPlayer[Main.maxPlayers]; public static BanManager Bans; @@ -112,35 +114,57 @@ namespace TShockAPI [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")] public override void Initialize() { - HandleCommandLine(Environment.GetCommandLineArgs()); - - if (!Directory.Exists(SavePath)) - Directory.CreateDirectory(SavePath); - - DateTime now = DateTime.Now; - string logFilename; try { - logFilename = Path.Combine(SavePath, now.ToString(LogFormat)+".log"); - } - catch(Exception) - { - // Problem with the log format use the default - logFilename = Path.Combine(SavePath, now.ToString(LogFormatDefault) + ".log"); - } + HandleCommandLine(Environment.GetCommandLineArgs()); + + if (Version.Major >= 4) + getTShockAscii(); + + if (!Directory.Exists(SavePath)) + Directory.CreateDirectory(SavePath); + + ConfigFile.ConfigRead += OnConfigRead; + FileTools.SetupConfig(); + + DateTime now = DateTime.Now; + string logFilename; + string logPathSetupWarning = null; + // Log path was not already set by the command line parameter? + if (LogPath == LogPathDefault) + LogPath = Config.LogPath; + try + { + logFilename = Path.Combine(LogPath, now.ToString(LogFormat)+".log"); + if (!Directory.Exists(LogPath)) + Directory.CreateDirectory(LogPath); + } + catch(Exception ex) + { + logPathSetupWarning = "Could not apply the given log path / log format, defaults will be used. Exception details:\n" + ex; + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(logPathSetupWarning); + Console.ForegroundColor = ConsoleColor.Gray; + // Problem with the log path or format use the default + logFilename = Path.Combine(LogPathDefault, now.ToString(LogFormatDefault) + ".log"); + } #if DEBUG - Log.Initialize(logFilename, LogLevel.All, false); + Log.Initialize(logFilename, LogLevel.All, false); #else - Log.Initialize(logFilename, LogLevel.All & ~LogLevel.Debug, LogClear); + Log.Initialize(logFilename, LogLevel.All & ~LogLevel.Debug, LogClear); #endif - AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; - - if (Version.Major >= 4) - { - getTShockAscii(); - } + if (logPathSetupWarning != null) + Log.Warn(logPathSetupWarning); + AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; + } + catch(Exception ex) + { + // Will be handled by the server api and written to its crashlog.txt. + throw new Exception("Fatal TShock initialization exception. See inner exception for details.", ex); + } + // Further exceptions are written to TShock's log from now on. try { if (File.Exists(Path.Combine(SavePath, "tshock.pid"))) @@ -151,9 +175,6 @@ namespace TShockAPI } File.WriteAllText(Path.Combine(SavePath, "tshock.pid"), Process.GetCurrentProcess().Id.ToString(CultureInfo.InvariantCulture)); - ConfigFile.ConfigRead += OnConfigRead; - FileTools.SetupConfig(); - HandleCommandLinePostConfigLoad(Environment.GetCommandLineArgs()); if (Config.StorageType.ToLower() == "sqlite") @@ -446,9 +467,13 @@ namespace TShockAPI } break; - case "-dump": - ConfigFile.DumpDescriptions(); - Permissions.DumpDescriptions(); + case "-logpath": + path = parms[++i]; + if (path.IndexOfAny(Path.GetInvalidPathChars()) == -1) + { + LogPath = path; + Log.ConsoleInfo("Log path has been set to " + path); + } break; case "-logformat": @@ -458,6 +483,11 @@ namespace TShockAPI case "-logclear": bool.TryParse(parms[++i], out LogClear); break; + + case "-dump": + ConfigFile.DumpDescriptions(); + Permissions.DumpDescriptions(); + break; } } } From 251456c8002ac2456cefc5433cd1bb5d1e5251cb Mon Sep 17 00:00:00 2001 From: CoderCow Date: Sat, 29 Jun 2013 14:00:24 +0200 Subject: [PATCH 3/5] Fixed PaginationTools brackets. --- TShockAPI/PaginationTools.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/TShockAPI/PaginationTools.cs b/TShockAPI/PaginationTools.cs index 07ae22fd..47ac654d 100644 --- a/TShockAPI/PaginationTools.cs +++ b/TShockAPI/PaginationTools.cs @@ -207,12 +207,14 @@ namespace TShockAPI { string termString; if (termFormatter != null) { - try { + try + { termString = termFormatter(term); if (termString == null) continue; - } catch (Exception ex) + } + catch (Exception ex) { throw new ArgumentException( "The method represented by termFormatter has thrown an exception. See inner exception for details.", ex); @@ -226,9 +228,8 @@ namespace TShockAPI { bool goesOnNextLine = (lineBuilder.Length + termString.Length > maxCharsPerLine); if (!goesOnNextLine) { - if (lineBuilder.Length > 0) { + if (lineBuilder.Length > 0) lineBuilder.Append(separator); - } lineBuilder.Append(termString); } else From 8a7bdf669771e5cff2d5b9a0eff9e204858a6ce5 Mon Sep 17 00:00:00 2001 From: CoderCow Date: Sat, 29 Jun 2013 14:00:52 +0200 Subject: [PATCH 4/5] Fixed /login error message. --- TShockAPI/Commands.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index abbe3680..bad2ba8f 100644 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -407,7 +407,7 @@ namespace TShockAPI } else { - args.Player.SendErrorMessage(String.Format("Syntax: /login{0} ", TShock.Config.AllowLoginAnyUsername ? " " : " [username]")); + args.Player.SendErrorMessage(String.Format("Syntax: /login{0} ", TShock.Config.AllowLoginAnyUsername ? " [username]" : " ")); args.Player.SendErrorMessage("If you forgot your password, there is no way to recover it."); return; } From 93c76f6d538ecb0da61dfa7798a775fab6743cd9 Mon Sep 17 00:00:00 2001 From: Scavenger3 Date: Sun, 30 Jun 2013 19:20:07 +1000 Subject: [PATCH 5/5] Added world size to world command. --- TShockAPI/Commands.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index bad2ba8f..bba2edec 100644 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -742,6 +742,7 @@ namespace TShockAPI public static void WorldInfo(CommandArgs args) { args.Player.SendInfoMessage("World name: " + Main.worldName); + args.Player.SendInfoMessage("World size: {0}x{1}", Main.maxTilesX, Main.maxTilesY); args.Player.SendInfoMessage("World ID: " + Main.worldID); }