1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
|
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using StardewModdingAPI.Framework.Commands;
using StardewModdingAPI.Framework.Models;
using StardewModdingAPI.Framework.ModLoading;
using StardewModdingAPI.Internal;
using StardewModdingAPI.Internal.ConsoleWriting;
using StardewModdingAPI.Toolkit.Framework.ModData;
using StardewModdingAPI.Toolkit.Utilities;
namespace StardewModdingAPI.Framework.Logging
{
/// <summary>Manages the SMAPI console window and log file.</summary>
internal class LogManager : IDisposable
{
/*********
** Fields
*********/
/// <summary>The log file to which to write messages.</summary>
private readonly LogFileManager LogFile;
/// <summary>Prefixing a low-level message with this character indicates that the console interceptor should write the string without intercepting it. (The character itself is not written.)</summary>
private readonly char IgnoreChar = '\u200B';
/// <summary>Get a named monitor instance.</summary>
private readonly Func<string, Monitor> GetMonitorImpl;
/// <summary>Regex patterns which match console non-error messages to suppress from the console and log.</summary>
private readonly Regex[] SuppressConsolePatterns =
{
new Regex(@"^TextBox\.Selected is now '(?:True|False)'\.$", RegexOptions.Compiled | RegexOptions.CultureInvariant),
new Regex(@"^loadPreferences\(\); begin", RegexOptions.Compiled | RegexOptions.CultureInvariant),
new Regex(@"^savePreferences\(\); async=", RegexOptions.Compiled | RegexOptions.CultureInvariant),
new Regex(@"^DebugOutput:\s+(?:added cricket|dismount tile|Ping|playerPos)", RegexOptions.Compiled | RegexOptions.CultureInvariant),
new Regex(@"^Ignoring keys: ", RegexOptions.Compiled | RegexOptions.CultureInvariant)
};
/// <summary>Regex patterns which match console messages to show a more friendly error for.</summary>
private readonly ReplaceLogPattern[] ReplaceConsolePatterns =
{
// Steam not loaded
new ReplaceLogPattern(
search: new Regex(@"^System\.InvalidOperationException: Steamworks is not initialized\.[\s\S]+$", RegexOptions.Compiled | RegexOptions.CultureInvariant),
replacement:
#if SMAPI_FOR_WINDOWS
"Oops! Steam achievements won't work because Steam isn't loaded. See 'Launch SMAPI through Steam or GOG Galaxy' in the install guide for more info: https://smapi.io/install.",
#else
"Oops! Steam achievements won't work because Steam isn't loaded. You can launch the game through Steam to fix that.",
#endif
logLevel: LogLevel.Error
),
// save file not found error
new ReplaceLogPattern(
search: new Regex(@"^System\.IO\.FileNotFoundException: [^\n]+\n[^:]+: '[^\n]+[/\\]Saves[/\\]([^'\r\n]+)[/\\]([^'\r\n]+)'[\s\S]+LoadGameMenu\.FindSaveGames[\s\S]+$", RegexOptions.Compiled | RegexOptions.CultureInvariant),
replacement: "The game can't find the '$2' file for your '$1' save. See https://stardewvalleywiki.com/Saves#Troubleshooting for help.",
logLevel: LogLevel.Error
)
};
/*********
** Accessors
*********/
/// <summary>The core logger and monitor for SMAPI.</summary>
public Monitor Monitor { get; }
/// <summary>The core logger and monitor on behalf of the game.</summary>
public Monitor MonitorForGame { get; }
/*********
** Public methods
*********/
/****
** Initialization
****/
/// <summary>Construct an instance.</summary>
/// <param name="logPath">The log file path to write.</param>
/// <param name="colorConfig">The colors to use for text written to the SMAPI console.</param>
/// <param name="writeToConsole">Whether to output log messages to the console.</param>
/// <param name="isVerbose">Whether verbose logging is enabled. This enables more detailed diagnostic messages than are normally needed.</param>
/// <param name="isDeveloperMode">Whether to enable full console output for developers.</param>
/// <param name="getScreenIdForLog">Get the screen ID that should be logged to distinguish between players in split-screen mode, if any.</param>
public LogManager(string logPath, ColorSchemeConfig colorConfig, bool writeToConsole, bool isVerbose, bool isDeveloperMode, Func<int?> getScreenIdForLog)
{
// init construction logic
this.GetMonitorImpl = name => new Monitor(name, this.IgnoreChar, this.LogFile, colorConfig, isVerbose, getScreenIdForLog)
{
WriteToConsole = writeToConsole,
ShowTraceInConsole = isDeveloperMode,
ShowFullStampInConsole = isDeveloperMode
};
// init fields
this.LogFile = new LogFileManager(logPath);
this.Monitor = this.GetMonitor("SMAPI");
this.MonitorForGame = this.GetMonitor("game");
// redirect direct console output
var output = new InterceptingTextWriter(Console.Out, this.IgnoreChar);
if (writeToConsole)
output.OnMessageIntercepted += message => this.HandleConsoleMessage(this.MonitorForGame, message);
Console.SetOut(output);
// enable Unicode handling on Windows
// (the terminal defaults to UTF-8 on Linux/macOS)
#if SMAPI_FOR_WINDOWS
Console.InputEncoding = Encoding.Unicode;
Console.OutputEncoding = Encoding.Unicode;
#endif
}
/// <summary>Get a monitor instance derived from SMAPI's current settings.</summary>
/// <param name="name">The name of the module which will log messages with this instance.</param>
public Monitor GetMonitor(string name)
{
return this.GetMonitorImpl(name);
}
/// <summary>Set the title of the SMAPI console window.</summary>
/// <param name="title">The new window title.</param>
public void SetConsoleTitle(string title)
{
Console.Title = title;
}
/****
** Console input
****/
/// <summary>Run a loop handling console input.</summary>
[SuppressMessage("ReSharper", "FunctionNeverReturns", Justification = "The thread is aborted when the game exits.")]
public void RunConsoleInputLoop(CommandManager commandManager, Action reloadTranslations, Action<string> handleInput, Func<bool> continueWhile)
{
// prepare console
this.Monitor.Log("Type 'help' for help, or 'help <cmd>' for a command's usage", LogLevel.Info);
commandManager
.Add(new HelpCommand(commandManager), this.Monitor)
.Add(new HarmonySummaryCommand(), this.Monitor)
.Add(new ReloadI18nCommand(reloadTranslations), this.Monitor);
// start handling command line input
Thread inputThread = new Thread(() =>
{
while (true)
{
// get input
string input = Console.ReadLine();
if (string.IsNullOrWhiteSpace(input))
continue;
// handle command
this.Monitor.LogUserInput(input);
handleInput(input);
}
});
inputThread.Start();
// keep console thread alive while the game is running
while (continueWhile())
Thread.Sleep(1000 / 10);
}
/// <summary>Show a 'press any key to exit' message, and exit when they press a key.</summary>
public void PressAnyKeyToExit()
{
this.Monitor.Log("Game has ended. Press any key to exit.", LogLevel.Info);
this.PressAnyKeyToExit(showMessage: false);
}
/// <summary>Show a 'press any key to exit' message, and exit when they press a key.</summary>
/// <param name="showMessage">Whether to print a 'press any key to exit' message to the console.</param>
public void PressAnyKeyToExit(bool showMessage)
{
if (showMessage)
this.Monitor.Log("Game has ended. Press any key to exit.");
Thread.Sleep(100);
Console.ReadKey();
Environment.Exit(0);
}
/****
** Crash/update handling
****/
/// <summary>Create a crash marker and duplicate the log into the crash log.</summary>
public void WriteCrashLog()
{
try
{
File.WriteAllText(Constants.FatalCrashMarker, string.Empty);
File.Copy(this.LogFile.Path, Constants.FatalCrashLog, overwrite: true);
}
catch (Exception ex)
{
this.Monitor.Log($"SMAPI failed trying to track the crash details: {ex.GetLogSummary()}", LogLevel.Error);
}
}
/// <summary>Write an update alert marker file.</summary>
/// <param name="version">The new version found.</param>
/// <param name="url">The download URL for the update.</param>
public void WriteUpdateMarker(string version, string url)
{
File.WriteAllText(Constants.UpdateMarker, $"{version}|{url}");
}
/// <summary>Check whether SMAPI crashed or detected an update during the last session, and display them in the SMAPI console.</summary>
public void HandleMarkerFiles()
{
// show update alert
if (File.Exists(Constants.UpdateMarker))
{
string[] rawUpdateFound = File.ReadAllText(Constants.UpdateMarker).Split(new[] { '|' }, 2);
if (SemanticVersion.TryParse(rawUpdateFound[0], out ISemanticVersion updateFound))
{
if (Constants.ApiVersion.IsPrerelease() && updateFound.IsNewerThan(Constants.ApiVersion))
{
string url = rawUpdateFound.Length > 1
? rawUpdateFound[1]
: Constants.HomePageUrl;
this.Monitor.Log("A new version of SMAPI was detected last time you played.", LogLevel.Error);
this.Monitor.Log($"You can update to {updateFound}: {url}.", LogLevel.Error);
this.Monitor.Log("Press any key to continue playing anyway. (This only appears when using a SMAPI beta.)", LogLevel.Info);
Console.ReadKey();
}
}
File.Delete(Constants.UpdateMarker);
}
// show details if game crashed during last session
if (File.Exists(Constants.FatalCrashMarker))
{
this.Monitor.Log("The game crashed last time you played. If it happens repeatedly, see 'get help' on https://smapi.io.", LogLevel.Error);
this.Monitor.Log("If you ask for help, make sure to share your SMAPI log: https://smapi.io/log.", LogLevel.Error);
this.Monitor.Log("Press any key to delete the crash data and continue playing.", LogLevel.Info);
Console.ReadKey();
File.Delete(Constants.FatalCrashLog);
File.Delete(Constants.FatalCrashMarker);
}
}
/// <summary>Log a fatal exception which prevents SMAPI from launching.</summary>
/// <param name="exception">The exception details.</param>
public void LogFatalLaunchError(Exception exception)
{
switch (exception)
{
// audio crash
case InvalidOperationException ex when ex.Source == "Microsoft.Xna.Framework.Xact" && ex.StackTrace.Contains("Microsoft.Xna.Framework.Audio.AudioEngine..ctor"):
this.Monitor.Log("The game couldn't load audio. Do you have speakers or headphones plugged in?", LogLevel.Error);
this.Monitor.Log($"Technical details: {ex.GetLogSummary()}");
break;
// missing content folder exception
case FileNotFoundException ex when ex.Message == "Couldn't find file 'C:\\Program Files (x86)\\Steam\\SteamApps\\common\\Stardew Valley\\Content\\XACT\\FarmerSounds.xgs'.": // path in error is hardcoded regardless of install path
this.Monitor.Log("The game can't find its Content\\XACT\\FarmerSounds.xgs file. You can usually fix this by resetting your content files (see https://smapi.io/troubleshoot#reset-content ), or by uninstalling and reinstalling the game.", LogLevel.Error);
this.Monitor.Log($"Technical details: {ex.GetLogSummary()}");
break;
// path too long exception
case PathTooLongException _:
{
string[] affectedPaths = PathUtilities.GetTooLongPaths(Constants.ModsPath).ToArray();
string message = affectedPaths.Any()
? $"SMAPI can't launch because some of your mod files exceed the maximum path length on {Constants.Platform}.\nIf you need help fixing this error, see https://smapi.io/help\n\nAffected paths:\n {string.Join("\n ", affectedPaths)}"
: $"The game failed to launch: {exception.GetLogSummary()}";
this.MonitorForGame.Log(message, LogLevel.Error);
}
break;
// generic exception
default:
this.MonitorForGame.Log($"The game failed to launch: {exception.GetLogSummary()}", LogLevel.Error);
break;
}
}
/****
** General log output
****/
/// <summary>Log the initial header with general SMAPI and system details.</summary>
/// <param name="modsPath">The path from which mods will be loaded.</param>
/// <param name="customSettings">The custom SMAPI settings.</param>
public void LogIntro(string modsPath, IDictionary<string, object> customSettings)
{
// log platform & patches
{
this.Monitor.Log($"SMAPI {Constants.ApiVersion} with Stardew Valley {Constants.GameVersion} on {EnvironmentUtility.GetFriendlyPlatformName(Constants.Platform)}", LogLevel.Info);
string[] patchLabels = this.GetPatchLabels().ToArray();
if (patchLabels.Any())
this.Monitor.Log($"Detected custom version: {string.Join(", ", patchLabels)}", LogLevel.Info);
}
// log basic info
this.Monitor.Log($"Mods go here: {modsPath}", LogLevel.Info);
if (modsPath != Constants.DefaultModsPath)
this.Monitor.Log("(Using custom --mods-path argument.)");
this.Monitor.Log($"Log started at {DateTime.UtcNow:s} UTC");
// log custom settings
if (customSettings.Any())
this.Monitor.Log($"Loaded with custom settings: {string.Join(", ", customSettings.OrderBy(p => p.Key).Select(p => $"{p.Key}: {p.Value}"))}");
}
/// <summary>Log details for settings that don't match the default.</summary>
/// <param name="settings">The settings to log.</param>
public void LogSettingsHeader(SConfig settings)
{
// developer mode
if (settings.DeveloperMode)
this.Monitor.Log("You enabled developer mode, so the console will be much more verbose. You can disable it by installing the non-developer version of SMAPI.", LogLevel.Info);
// warnings
if (!settings.CheckForUpdates)
this.Monitor.Log("You disabled update checks, so you won't be notified of new SMAPI or mod updates. Running an old version of SMAPI is not recommended. You can undo this by reinstalling SMAPI.", LogLevel.Warn);
if (!settings.RewriteMods)
this.Monitor.Log("You disabled rewriting broken mods, so many older mods may fail to load. You can undo this by reinstalling SMAPI.", LogLevel.Info);
if (!this.Monitor.WriteToConsole)
this.Monitor.Log("Writing to the terminal is disabled because the --no-terminal argument was received. This usually means launching the terminal failed.", LogLevel.Warn);
// verbose logging
this.Monitor.VerboseLog("Verbose logging enabled.");
}
/// <summary>Log info about loaded mods.</summary>
/// <param name="loaded">The full list of loaded content packs and mods.</param>
/// <param name="loadedContentPacks">The loaded content packs.</param>
/// <param name="loadedMods">The loaded mods.</param>
/// <param name="skippedMods">The mods which could not be loaded.</param>
/// <param name="logParanoidWarnings">Whether to log issues for mods which directly use potentially sensitive .NET APIs like file or shell access.</param>
public void LogModInfo(IModMetadata[] loaded, IModMetadata[] loadedContentPacks, IModMetadata[] loadedMods, IModMetadata[] skippedMods, bool logParanoidWarnings)
{
// log loaded mods
this.Monitor.Log($"Loaded {loadedMods.Length} mods" + (loadedMods.Length > 0 ? ":" : "."), LogLevel.Info);
foreach (IModMetadata metadata in loadedMods.OrderBy(p => p.DisplayName))
{
IManifest manifest = metadata.Manifest;
this.Monitor.Log(
$" {metadata.DisplayName} {manifest.Version}"
+ (!string.IsNullOrWhiteSpace(manifest.Author) ? $" by {manifest.Author}" : "")
+ (!string.IsNullOrWhiteSpace(manifest.Description) ? $" | {manifest.Description}" : ""),
LogLevel.Info
);
}
this.Monitor.Newline();
// log loaded content packs
if (loadedContentPacks.Any())
{
string GetModDisplayName(string id) => loadedMods.FirstOrDefault(p => p.HasID(id))?.DisplayName;
this.Monitor.Log($"Loaded {loadedContentPacks.Length} content packs:", LogLevel.Info);
foreach (IModMetadata metadata in loadedContentPacks.OrderBy(p => p.DisplayName))
{
IManifest manifest = metadata.Manifest;
this.Monitor.Log(
$" {metadata.DisplayName} {manifest.Version}"
+ (!string.IsNullOrWhiteSpace(manifest.Author) ? $" by {manifest.Author}" : "")
+ $" | for {GetModDisplayName(metadata.Manifest.ContentPackFor.UniqueID)}"
+ (!string.IsNullOrWhiteSpace(manifest.Description) ? $" | {manifest.Description}" : ""),
LogLevel.Info
);
}
this.Monitor.Newline();
}
// log mod warnings
this.LogModWarnings(loaded, skippedMods, logParanoidWarnings);
}
/// <inheritdoc />
public void Dispose()
{
this.LogFile.Dispose();
}
/*********
** Protected methods
*********/
/// <summary>Redirect messages logged directly to the console to the given monitor.</summary>
/// <param name="gameMonitor">The monitor with which to log messages as the game.</param>
/// <param name="message">The message to log.</param>
private void HandleConsoleMessage(IMonitor gameMonitor, string message)
{
// detect exception
LogLevel level = message.Contains("Exception") ? LogLevel.Error : LogLevel.Trace;
// ignore suppressed message
if (level != LogLevel.Error && this.SuppressConsolePatterns.Any(p => p.IsMatch(message)))
return;
// show friendly error if applicable
foreach (ReplaceLogPattern entry in this.ReplaceConsolePatterns)
{
string newMessage = entry.Search.Replace(message, entry.Replacement);
if (message != newMessage)
{
gameMonitor.Log(newMessage, entry.LogLevel);
gameMonitor.Log(message);
return;
}
}
// forward to monitor
gameMonitor.Log(message, level);
}
/// <summary>Get human-readable labels to log for detected SMAPI and Stardew Valley customizations.</summary>
private IEnumerable<string> GetPatchLabels()
{
// custom game framework
if (EarlyConstants.IsWindows64BitHack)
yield return $"running 64-bit SMAPI with {Constants.GameFramework}";
else if ((Constants.GameFramework == GameFramework.Xna) != (Constants.Platform == Platform.Windows))
yield return $"running {Constants.GameFramework}";
// patched by Stardew64Installer
if (Constants.IsPatchedByStardew64Installer(out ISemanticVersion patchedByVersion))
yield return $"patched by Stardew64Installer {patchedByVersion}";
}
/// <summary>Write a summary of mod warnings to the console and log.</summary>
/// <param name="mods">The loaded mods.</param>
/// <param name="skippedMods">The mods which could not be loaded.</param>
/// <param name="logParanoidWarnings">Whether to log issues for mods which directly use potentially sensitive .NET APIs like file or shell access.</param>
private void LogModWarnings(IEnumerable<IModMetadata> mods, IModMetadata[] skippedMods, bool logParanoidWarnings)
{
// get mods with warnings
IModMetadata[] modsWithWarnings = mods.Where(p => p.Warnings != ModWarning.None).ToArray();
if (!modsWithWarnings.Any() && !skippedMods.Any())
return;
// log intro
{
int count = modsWithWarnings.Length + skippedMods.Length;
this.Monitor.Log($"Found {count} mod{(count == 1 ? "" : "s")} with warnings:", LogLevel.Info);
}
// log skipped mods
if (skippedMods.Any())
{
var loggedDuplicateIds = new HashSet<string>();
this.Monitor.Log(" Skipped mods", LogLevel.Error);
this.Monitor.Log(" " + "".PadRight(50, '-'), LogLevel.Error);
this.Monitor.Log(" These mods could not be added to your game.", LogLevel.Error);
this.Monitor.Newline();
foreach (var list in this.GroupFailedModsByPriority(skippedMods))
{
if (list.Any())
{
foreach (IModMetadata mod in list.OrderBy(p => p.DisplayName))
{
string message = $" - {mod.DisplayName}{(" " + mod.Manifest?.Version?.ToString()).TrimEnd()} because {mod.Error}";
// duplicate mod: log first one only, don't show redundant version
if (mod.FailReason == ModFailReason.Duplicate && mod.HasManifest())
{
if (loggedDuplicateIds.Add(mod.Manifest.UniqueID))
continue; // already logged
message = $" - {mod.DisplayName} because {mod.Error}";
}
// log message
this.Monitor.Log(message, LogLevel.Error);
if (mod.ErrorDetails != null)
this.Monitor.Log($" ({mod.ErrorDetails})");
}
this.Monitor.Newline();
}
}
}
// log warnings
if (modsWithWarnings.Any())
{
// broken code
this.LogModWarningGroup(modsWithWarnings, ModWarning.BrokenCodeLoaded, LogLevel.Error, "Broken mods",
"These mods have broken code, but you configured SMAPI to load them anyway. This may cause bugs,",
"errors, or crashes in-game."
);
// changes serializer
this.LogModWarningGroup(modsWithWarnings, ModWarning.ChangesSaveSerializer, LogLevel.Warn, "Changed save serializer",
"These mods change the save serializer. They may corrupt your save files, or make them unusable if",
"you uninstall these mods."
);
// patched game code
this.LogModWarningGroup(modsWithWarnings, ModWarning.PatchesGame, LogLevel.Info, "Patched game code",
"These mods directly change the game code. They're more likely to cause errors or bugs in-game; if",
"your game has issues, try removing these first. Otherwise you can ignore this warning."
);
// unvalidated update tick
this.LogModWarningGroup(modsWithWarnings, ModWarning.UsesUnvalidatedUpdateTick, LogLevel.Info, "Bypassed safety checks",
"These mods bypass SMAPI's normal safety checks, so they're more likely to cause errors or save",
"corruption. If your game has issues, try removing these first."
);
// paranoid warnings
if (logParanoidWarnings)
{
this.LogModWarningGroup(
modsWithWarnings,
match: mod => mod.HasWarnings(ModWarning.AccessesConsole, ModWarning.AccessesFilesystem, ModWarning.AccessesShell),
level: LogLevel.Debug,
heading: "Direct system access",
blurb: new[]
{
"You enabled paranoid warnings and these mods directly access the filesystem, shells/processes, or",
"SMAPI console. (This is usually legitimate and innocent usage; this warning is only useful for",
"further investigation.)"
},
modLabel: mod =>
{
List<string> labels = new List<string>();
if (mod.HasWarnings(ModWarning.AccessesConsole))
labels.Add("console");
if (mod.HasWarnings(ModWarning.AccessesFilesystem))
labels.Add("files");
if (mod.HasWarnings(ModWarning.AccessesShell))
labels.Add("shells/processes");
return $"{mod.DisplayName} ({string.Join(", ", labels)})";
}
);
}
// no update keys
this.LogModWarningGroup(modsWithWarnings, ModWarning.NoUpdateKeys, LogLevel.Debug, "No update keys",
"These mods have no update keys in their manifest. SMAPI may not notify you about updates for these",
"mods. Consider notifying the mod authors about this problem."
);
// not crossplatform
this.LogModWarningGroup(modsWithWarnings, ModWarning.UsesDynamic, LogLevel.Debug, "Not crossplatform",
"These mods use the 'dynamic' keyword, and won't work on Linux/macOS."
);
}
}
/// <summary>Group failed mods by the priority players should update them, where mods in earlier groups are more likely to fix multiple mods.</summary>
/// <param name="failedMods">The failed mods to group.</param>
private IEnumerable<IList<IModMetadata>> GroupFailedModsByPriority(IList<IModMetadata> failedMods)
{
var failedOthers = failedMods.ToList();
var skippedModIds = new HashSet<string>(from mod in failedMods where mod.HasID() select mod.Manifest.UniqueID, StringComparer.OrdinalIgnoreCase);
// group B: dependencies which failed
var failedOtherDependencies = new List<IModMetadata>();
{
// get failed dependency IDs
var skippedDependencyIds = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (IModMetadata mod in failedMods)
{
foreach (string requiredId in skippedModIds.Intersect(mod.GetRequiredModIds()))
skippedDependencyIds.Add(requiredId);
}
// group matching mods
this.FilterThrough(
fromList: failedOthers,
toList: failedOtherDependencies,
match: mod => mod.HasID() && skippedDependencyIds.Contains(mod.Manifest.UniqueID)
);
}
// group A: failed root dependencies which other dependencies need
var failedRootDependencies = new List<IModMetadata>();
{
var skippedDependencyIds = new HashSet<string>(failedOtherDependencies.Select(p => p.Manifest.UniqueID));
this.FilterThrough(
fromList: failedOtherDependencies,
toList: failedRootDependencies,
match: mod =>
{
// has no failed dependency
foreach (string requiredId in mod.GetRequiredModIds())
{
if (skippedDependencyIds.Contains(requiredId))
return false;
}
// another dependency depends on this mod
bool isDependedOn = false;
foreach (IModMetadata other in failedOtherDependencies)
{
if (other.HasRequiredModId(mod.Manifest.UniqueID, includeOptional: false))
{
isDependedOn = true;
break;
}
}
return isDependedOn;
}
);
}
// return groups
return new[]
{
failedRootDependencies,
failedOtherDependencies,
failedOthers
};
}
/// <summary>Filter matching items from one list and add them to the other.</summary>
/// <typeparam name="TItem">The list item type.</typeparam>
/// <param name="fromList">The list to filter.</param>
/// <param name="toList">The list to which to add filtered items.</param>
/// <param name="match">Matches items to filter through.</param>
private void FilterThrough<TItem>(IList<TItem> fromList, IList<TItem> toList, Func<TItem, bool> match)
{
for (int i = 0; i < fromList.Count; i++)
{
TItem item = fromList[i];
if (match(item))
{
toList.Add(item);
fromList.RemoveAt(i);
i--;
}
}
}
/// <summary>Write a mod warning group to the console and log.</summary>
/// <param name="mods">The mods to search.</param>
/// <param name="match">Matches mods to include in the warning group.</param>
/// <param name="level">The log level for the logged messages.</param>
/// <param name="heading">A brief heading label for the group.</param>
/// <param name="blurb">A detailed explanation of the warning, split into lines.</param>
/// <param name="modLabel">Formats the mod label, or <c>null</c> to use the <see cref="IModMetadata.DisplayName"/>.</param>
private void LogModWarningGroup(IModMetadata[] mods, Func<IModMetadata, bool> match, LogLevel level, string heading, string[] blurb, Func<IModMetadata, string> modLabel = null)
{
// get matching mods
string[] modLabels = mods
.Where(match)
.Select(mod => modLabel?.Invoke(mod) ?? mod.DisplayName)
.OrderBy(p => p)
.ToArray();
if (!modLabels.Any())
return;
// log header/blurb
this.Monitor.Log(" " + heading, level);
this.Monitor.Log(" " + "".PadRight(50, '-'), level);
foreach (string line in blurb)
this.Monitor.Log(" " + line, level);
this.Monitor.Newline();
// log mod list
foreach (string label in modLabels)
this.Monitor.Log($" - {label}", level);
this.Monitor.Newline();
}
/// <summary>Write a mod warning group to the console and log.</summary>
/// <param name="mods">The mods to search.</param>
/// <param name="warning">The mod warning to match.</param>
/// <param name="level">The log level for the logged messages.</param>
/// <param name="heading">A brief heading label for the group.</param>
/// <param name="blurb">A detailed explanation of the warning, split into lines.</param>
private void LogModWarningGroup(IModMetadata[] mods, ModWarning warning, LogLevel level, string heading, params string[] blurb)
{
this.LogModWarningGroup(mods, mod => mod.HasWarnings(warning), level, heading, blurb);
}
/*********
** Protected types
*********/
/// <summary>A console log pattern to replace with a different message.</summary>
private class ReplaceLogPattern
{
/*********
** Accessors
*********/
/// <summary>The regex pattern matching the portion of the message to replace.</summary>
public Regex Search { get; }
/// <summary>The replacement string.</summary>
public string Replacement { get; }
/// <summary>The log level for the new message.</summary>
public LogLevel LogLevel { get; }
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="search">The regex pattern matching the portion of the message to replace.</param>
/// <param name="replacement">The replacement string.</param>
/// <param name="logLevel">The log level for the new message.</param>
public ReplaceLogPattern(Regex search, string replacement, LogLevel logLevel)
{
this.Search = search;
this.Replacement = replacement;
this.LogLevel = logLevel;
}
}
}
}
|