diff --git a/IPA.Injector/Backups/BackupUnit.cs b/IPA.Injector/Backups/BackupUnit.cs index e3e9ee6d..73e18439 100644 --- a/IPA.Injector/Backups/BackupUnit.cs +++ b/IPA.Injector/Backups/BackupUnit.cs @@ -1,4 +1,5 @@ -using System; +using IPA.Utilities; +using System; using System.Collections.Generic; using System.IO; @@ -16,7 +17,7 @@ namespace IPA.Injector.Backups private readonly FileInfo _manifestFile; private const string ManifestFileName = "$manifest$.txt"; - public BackupUnit(string dir) : this(dir, DateTime.Now.ToString("yyyy-MM-dd_h-mm-ss")) + public BackupUnit(string dir) : this(dir, Utils.CurrentTime().ToString("yyyy-MM-dd_h-mm-ss")) { } diff --git a/IPA.Loader/Config/Config.cs b/IPA.Loader/Config/Config.cs index d7c1a6af..126c2d50 100644 --- a/IPA.Loader/Config/Config.cs +++ b/IPA.Loader/Config/Config.cs @@ -212,7 +212,7 @@ namespace IPA.Config try { provider.Item2.Save(); - provider.Item1.Value = DateTime.Now; + provider.Item1.Value = Utils.CurrentTime(); } catch (Exception e) { diff --git a/IPA.Loader/Logging/Printers/GlobalLogFilePrinter.cs b/IPA.Loader/Logging/Printers/GlobalLogFilePrinter.cs index 30c27acf..12c08bd3 100644 --- a/IPA.Loader/Logging/Printers/GlobalLogFilePrinter.cs +++ b/IPA.Loader/Logging/Printers/GlobalLogFilePrinter.cs @@ -1,4 +1,5 @@ -using System; +using IPA.Utilities; +using System; using System.IO; namespace IPA.Logging.Printers @@ -35,7 +36,7 @@ namespace IPA.Logging.Printers { var logsDir = new DirectoryInfo("Logs"); logsDir.Create(); - var finfo = new FileInfo(Path.Combine(logsDir.FullName, $"{DateTime.Now:yyyy.MM.dd.HH.mm.ss}.log")); + var finfo = new FileInfo(Path.Combine(logsDir.FullName, $"{Utils.CurrentTime():yyyy.MM.dd.HH.mm.ss}.log")); return finfo; } } diff --git a/IPA.Loader/Logging/Printers/PluginLogFilePrinter.cs b/IPA.Loader/Logging/Printers/PluginLogFilePrinter.cs index f4b61bea..c50fae74 100644 --- a/IPA.Loader/Logging/Printers/PluginLogFilePrinter.cs +++ b/IPA.Loader/Logging/Printers/PluginLogFilePrinter.cs @@ -1,4 +1,5 @@ -using System; +using IPA.Utilities; +using System; using System.IO; namespace IPA.Logging.Printers @@ -24,7 +25,7 @@ namespace IPA.Logging.Printers { var logsDir = new DirectoryInfo(Path.Combine("Logs", name)); logsDir.Create(); - var finfo = new FileInfo(Path.Combine(logsDir.FullName, $"{DateTime.Now:yyyy.MM.dd.HH.mm.ss}.log")); + var finfo = new FileInfo(Path.Combine(logsDir.FullName, $"{Utils.CurrentTime():yyyy.MM.dd.HH.mm.ss}.log")); return finfo; } diff --git a/IPA.Loader/Logging/Printers/PluginSubLogPrinter.cs b/IPA.Loader/Logging/Printers/PluginSubLogPrinter.cs index 09a6d0ea..2b0e06dc 100644 --- a/IPA.Loader/Logging/Printers/PluginSubLogPrinter.cs +++ b/IPA.Loader/Logging/Printers/PluginSubLogPrinter.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using IPA.Utilities; namespace IPA.Logging.Printers { @@ -25,7 +26,7 @@ namespace IPA.Logging.Printers { var logsDir = new DirectoryInfo(Path.Combine("Logs", mainName, name)); logsDir.Create(); - var finfo = new FileInfo(Path.Combine(logsDir.FullName, $"{DateTime.Now:yyyy.MM.dd.HH.mm.ss}.log")); + var finfo = new FileInfo(Path.Combine(logsDir.FullName, $"{Utils.CurrentTime():yyyy.MM.dd.HH.mm.ss}.log")); return finfo; } diff --git a/IPA.Loader/Logging/StandardLogger.cs b/IPA.Loader/Logging/StandardLogger.cs index e0b4b12f..ea12a922 100644 --- a/IPA.Loader/Logging/StandardLogger.cs +++ b/IPA.Loader/Logging/StandardLogger.cs @@ -1,5 +1,6 @@ using IPA.Config; using IPA.Logging.Printers; +using IPA.Utilities; using System; using System.Collections.Concurrent; using System.Collections.Generic; @@ -194,7 +195,7 @@ namespace IPA.Logging Level = level, Message = message, Logger = this, - Time = DateTime.Now + Time = Utils.CurrentTime() }); } @@ -285,7 +286,7 @@ namespace IPA.Logging } // update last use time and print - printer.LastUse = DateTime.Now; + printer.LastUse = Utils.CurrentTime(); printer.Print(msg.Level, msg.Time, msg.Logger.logName, msg.Message); } } @@ -328,14 +329,14 @@ namespace IPA.Logging Level = Level.Warning, Logger = loggerLogger, Message = $"{loggerLogger.logName.ToUpper()}: Messages omitted to improve performance", - Time = DateTime.Now + Time = Utils.CurrentTime() }); // resume log calls logWaitEvent.Set(); } - var now = DateTime.Now; + var now = Utils.CurrentTime(); var copy = new List(started); foreach (var printer in copy) { diff --git a/IPA.Loader/Utilities/Utils.cs b/IPA.Loader/Utilities/Utils.cs index b442e65b..a9147d86 100644 --- a/IPA.Loader/Utilities/Utils.cs +++ b/IPA.Loader/Utilities/Utils.cs @@ -129,5 +129,42 @@ namespace IPA.Utilities CopyAll(diSourceSubDir, nextTargetSubDir, appendFileName, onCopyException); } } + + /// + /// Whether you can safely use without Mono throwing a fit. + /// + /// if you can use safely, otherwise + public static bool CanUseDateTimeNowSafely { get; private set; } = true; + private static bool DateTimeSafetyUnknown = true; + private static ulong UnsafeAdvanceTicks = 1; + + /// + /// Gets the current if supported, otherwise, if Mono would throw a fit, + /// returns plus some value, such that each time it is called + /// the value will be greater than the previous result. Not suitable for timing. + /// + /// the current if supported, otherwise some indeterminant increasing value. + public static DateTime CurrentTime() + { + if (DateTimeSafetyUnknown) + { + DateTime time = DateTime.MinValue; + try + { + time = DateTime.Now; + } + catch (TimeZoneNotFoundException) + { // Mono did a fucky wucky and we need to avoid this call + CanUseDateTimeNowSafely = false; + } + DateTimeSafetyUnknown = false; + return time; + } + else + { + if (CanUseDateTimeNowSafely) return DateTime.Now; + else return DateTime.MinValue.AddTicks((long)UnsafeAdvanceTicks++); // return MinValue as a fallback + } + } } }