summaryrefslogtreecommitdiff
path: root/src/SMAPI
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2018-06-08 18:46:58 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2018-06-08 18:46:58 -0400
commit86a3f8dd460f329fad903770231016813e750168 (patch)
tree6db8cd9c2be1c507c8c79af02889d64ba08458ff /src/SMAPI
parent18906de0f4a31b70f21f386f65bd3fda3ec9fb13 (diff)
downloadSMAPI-86a3f8dd460f329fad903770231016813e750168.tar.gz
SMAPI-86a3f8dd460f329fad903770231016813e750168.tar.bz2
SMAPI-86a3f8dd460f329fad903770231016813e750168.zip
allow launching multiple instances without manually changing log path (#494)
Diffstat (limited to 'src/SMAPI')
-rw-r--r--src/SMAPI/Constants.cs7
-rw-r--r--src/SMAPI/Program.cs65
2 files changed, 53 insertions, 19 deletions
diff --git a/src/SMAPI/Constants.cs b/src/SMAPI/Constants.cs
index 786c1a39..867e01ea 100644
--- a/src/SMAPI/Constants.cs
+++ b/src/SMAPI/Constants.cs
@@ -82,8 +82,11 @@ namespace StardewModdingAPI
/// <summary>The file path for the SMAPI metadata file.</summary>
internal static string ApiMetadataPath => Path.Combine(Constants.ExecutionPath, $"{typeof(Program).Assembly.GetName().Name}.metadata.json");
- /// <summary>The file path to the log where the latest output should be saved.</summary>
- internal static string DefaultLogPath => Path.Combine(Constants.LogDir, "SMAPI-latest.txt");
+ /// <summary>The filename prefix for SMAPI log files.</summary>
+ internal static string LogNamePrefix { get; } = "SMAPI-latest";
+
+ /// <summary>The filename extension for SMAPI log files.</summary>
+ internal static string LogNameExtension { get; } = "txt";
/// <summary>A copy of the log leading up to the previous fatal crash, if any.</summary>
internal static string FatalCrashLog => Path.Combine(Constants.LogDir, "SMAPI-crash.txt");
diff --git a/src/SMAPI/Program.cs b/src/SMAPI/Program.cs
index cc59c0cd..570a3c55 100644
--- a/src/SMAPI/Program.cs
+++ b/src/SMAPI/Program.cs
@@ -117,30 +117,19 @@ namespace StardewModdingAPI
// get flags from arguments
bool writeToConsole = !args.Contains("--no-terminal");
- // get log path from arguments
- string logPath = null;
- {
- int pathIndex = Array.LastIndexOf(args, "--log-path") + 1;
- if (pathIndex >= 1 && args.Length >= pathIndex)
- {
- logPath = args[pathIndex];
- if (!Path.IsPathRooted(logPath))
- logPath = Path.Combine(Constants.LogDir, logPath);
- }
- }
- if (string.IsNullOrWhiteSpace(logPath))
- logPath = Constants.DefaultLogPath;
-
// load SMAPI
- using (Program program = new Program(writeToConsole, logPath))
+ using (Program program = new Program(writeToConsole))
program.RunInteractively();
}
/// <summary>Construct an instance.</summary>
/// <param name="writeToConsole">Whether to output log messages to the console.</param>
- /// <param name="logPath">The full file path to which to write log messages.</param>
- public Program(bool writeToConsole, string logPath)
+ public Program(bool writeToConsole)
{
+ // init log file
+ this.PurgeLogFiles();
+ string logPath = this.GetLogPath();
+
// init basics
this.Settings = JsonConvert.DeserializeObject<SConfig>(File.ReadAllText(Constants.ApiConfigPath));
this.LogFile = new LogFileManager(logPath);
@@ -1258,5 +1247,47 @@ namespace StardewModdingAPI
if (this.Settings.VerboseLogging)
this.Monitor.Log(message, LogLevel.Trace);
}
+
+ /// <summary>Get the absolute path to the next available log file.</summary>
+ private string GetLogPath()
+ {
+ // default path
+ {
+ FileInfo defaultFile = new FileInfo(Path.Combine(Constants.LogDir, $"{Constants.LogNamePrefix}.{Constants.LogNameExtension}"));
+ if (!defaultFile.Exists)
+ return defaultFile.FullName;
+ }
+
+ // get first disambiguated path
+ for (int i = 2; i < int.MaxValue; i++)
+ {
+ FileInfo file = new FileInfo(Path.Combine(Constants.LogDir, $"{Constants.LogNamePrefix}.player-{i}.{Constants.LogNameExtension}"));
+ if (!file.Exists)
+ return file.FullName;
+ }
+
+ // should never happen
+ throw new InvalidOperationException("Could not find an available log path.");
+ }
+
+ /// <summary>Delete all log files created by SMAPI.</summary>
+ private void PurgeLogFiles()
+ {
+ DirectoryInfo logsDir = new DirectoryInfo(Constants.LogDir);
+ foreach (FileInfo logFile in logsDir.EnumerateFiles("*.txt"))
+ {
+ if (logFile.Name.StartsWith(Constants.LogNamePrefix, StringComparison.InvariantCultureIgnoreCase))
+ {
+ try
+ {
+ FileUtilities.ForceDelete(logFile);
+ }
+ catch (Exception ex)
+ {
+ // leave file if it's locked
+ }
+ }
+ }
+ }
}
}