diff --git a/UncreatedWarfare/Events/Patches/InteractableBedReceiveClaimRequest.cs b/UncreatedWarfare/Events/Patches/InteractableBedReceiveClaimRequest.cs index 2c72468b..742ebdd3 100644 --- a/UncreatedWarfare/Events/Patches/InteractableBedReceiveClaimRequest.cs +++ b/UncreatedWarfare/Events/Patches/InteractableBedReceiveClaimRequest.cs @@ -1,9 +1,6 @@ -using Autofac; -using DanielWillett.ReflectionTools; +using DanielWillett.ReflectionTools; using DanielWillett.ReflectionTools.Formatting; -using SDG.Unturned; using System.Reflection; -using Uncreated.Warfare.Components; using Uncreated.Warfare.Events.Models.Barricades; using Uncreated.Warfare.Patches; using Uncreated.Warfare.Players; @@ -24,16 +21,15 @@ void IHarmonyPatch.Patch(ILogger logger) if (_target != null) { Patcher.Patch(_target, prefix: Accessor.GetMethod(Prefix)); - logger.LogDebug("Patched {0} for bedroll claim requested.", Accessor.Formatter.Format(_target)); + logger.LogDebug("Patched {0} for bedroll claim requested.", _target); return; } logger.LogError("Failed to find method: {0}.", - Accessor.Formatter.Format(new MethodDefinition(nameof(InteractableBed.ReceiveClaimRequest)) + new MethodDefinition(nameof(InteractableBed.ReceiveClaimRequest)) .DeclaredIn(isStatic: false) .WithParameter("newText") .ReturningVoid() - ) ); } @@ -43,7 +39,7 @@ void IHarmonyPatch.Unpatch(ILogger logger) return; Patcher.Unpatch(_target, Accessor.GetMethod(Prefix)); - logger.LogDebug("Unpatched {0} for sign text updated event.", Accessor.Formatter.Format(_target)); + logger.LogDebug("Unpatched {0} for sign text updated event.", _target); _target = null; } diff --git a/UncreatedWarfare/Logging/WarfareLoggingExtensions.cs b/UncreatedWarfare/Logging/WarfareLoggingExtensions.cs index a99244ec..5d5515ca 100644 --- a/UncreatedWarfare/Logging/WarfareLoggingExtensions.cs +++ b/UncreatedWarfare/Logging/WarfareLoggingExtensions.cs @@ -1,5 +1,6 @@ using System; using System.Diagnostics; +using Uncreated.Warfare; using Uncreated.Warfare.Logging.Formatting; // ReSharper disable once CheckNamespace @@ -2274,53 +2275,61 @@ public static void Log(this ILogger logger, LogLevel logLevel, string message, p logger.Log(logLevel, 0, null, message, args); } -// /// -// /// Formats and writes a log message at the specified log level. -// /// -// /// The to write to. -// /// Entry will be written on this level. -// /// The event id associated with the log. -// /// Format string of the log message. -// /// An object array that contains zero or more objects to format. -// [StringFormatMethod(nameof(message))] -// public static void Log(this ILogger logger, LogLevel logLevel, EventId eventId, string message, params object?[]? args) -// { -// logger.Log(logLevel, eventId, null, message, args); -// } - -// /// -// /// Formats and writes a log message at the specified log level. -// /// -// /// The to write to. -// /// Entry will be written on this level. -// /// The exception to log. -// /// Format string of the log message. -// /// An object array that contains zero or more objects to format. -// [StringFormatMethod(nameof(message))] -// public static void Log(this ILogger logger, LogLevel logLevel, Exception? exception, string message, params object?[]? args) -// { -// logger.Log(logLevel, 0, exception, message, args); -// } - -// /// -// /// Formats and writes a log message at the specified log level. -// /// -// /// The to write to. -// /// Entry will be written on this level. -// /// The event id associated with the log. -// /// The exception to log. -// /// Format string of the log message. -// /// An object array that contains zero or more objects to format. -// [StringFormatMethod(nameof(message))] -// public static void Log(this ILogger logger, LogLevel logLevel, EventId eventId, Exception? exception, string message, params object?[]? args) -// { -// if (logger == null) -// { -// throw new ArgumentNullException(nameof(logger)); -// } - -// logger.Log(logLevel, eventId, new WarfareFormattedLogValues(message, args ?? Array.Empty()), exception, MessageFormatter); -// } + /// + /// Formats and writes a log message at the specified log level. + /// + /// The to write to. + /// Entry will be written on this level. + /// The event id associated with the log. + /// Format string of the log message. + /// An object array that contains zero or more objects to format. + [StringFormatMethod(nameof(message))] + public static void Log(this ILogger logger, LogLevel logLevel, EventId eventId, string message, params object?[]? args) + { + logger.Log(logLevel, eventId, null, message, args); + } + + /// + /// Formats and writes a log message at the specified log level. + /// + /// The to write to. + /// Entry will be written on this level. + /// The exception to log. + /// Format string of the log message. + /// An object array that contains zero or more objects to format. + [StringFormatMethod(nameof(message))] + public static void Log(this ILogger logger, LogLevel logLevel, Exception? exception, string message, params object?[]? args) + { + logger.Log(logLevel, 0, exception, message, args); + } + + /// + /// Formats and writes a log message at the specified log level. + /// + /// The to write to. + /// Entry will be written on this level. + /// The event id associated with the log. + /// The exception to log. + /// Format string of the log message. + /// An object array that contains zero or more objects to format. + [StringFormatMethod(nameof(message))] + public static void Log(this ILogger logger, LogLevel logLevel, EventId eventId, Exception? exception, string message, params object?[]? args) + { + if (logger == null) + { + throw new ArgumentNullException(nameof(logger)); + } + + args ??= Array.Empty(); + if (WarfareModule.IsActive) + { + logger.Log(logLevel, eventId, new WarfareFormattedLogValues(message, args), exception, MessageFormatter); + } + else + { + LoggerExtensions.Log(logger, logLevel, eventId, exception, message, args); + } + } /// /// Formats and writes a log message at the specified log level. @@ -2376,7 +2385,14 @@ public static void Log(this ILogger logger, LogLevel logLevel, EventId eventId, throw new ArgumentNullException(nameof(logger)); } - logger.Log(logLevel, eventId, new WarfareFormattedLogValues(message, Array.Empty()), exception, MessageFormatter); + if (WarfareModule.IsActive) + { + logger.Log(logLevel, eventId, new WarfareFormattedLogValues(message, Array.Empty()), exception, MessageFormatter); + } + else + { + LoggerExtensions.Log(logger, logLevel, eventId, exception, message, Array.Empty()); + } } /// @@ -2433,7 +2449,14 @@ public static void Log(this ILogger logger, LogLevel logLevel, EventId eventId, throw new ArgumentNullException(nameof(logger)); } - logger.Log(logLevel, eventId, new WarfareFormattedLogValues(message, arg1), exception, MessageFormatter); + if (WarfareModule.IsActive) + { + logger.Log(logLevel, eventId, new WarfareFormattedLogValues(message, arg1), exception, MessageFormatter); + } + else + { + LoggerExtensions.Log(logger, logLevel, eventId, exception, message, [ arg1 ]); + } } /// @@ -2490,7 +2513,14 @@ public static void Log(this ILogger logger, LogLevel logLevel, EventId eventId, throw new ArgumentNullException(nameof(logger)); } - logger.Log(logLevel, eventId, new WarfareFormattedLogValues(message, arg1, arg2), exception, MessageFormatter); + if (WarfareModule.IsActive) + { + logger.Log(logLevel, eventId, new WarfareFormattedLogValues(message, arg1, arg2), exception, MessageFormatter); + } + else + { + LoggerExtensions.Log(logger, logLevel, eventId, exception, message, [ arg1, arg2 ]); + } } /// @@ -2547,7 +2577,14 @@ public static void Log(this ILogger logger, LogLevel logLevel, EventId eventId, throw new ArgumentNullException(nameof(logger)); } - logger.Log(logLevel, eventId, new WarfareFormattedLogValues(message, arg1, arg2, arg3), exception, MessageFormatter); + if (WarfareModule.IsActive) + { + logger.Log(logLevel, eventId, new WarfareFormattedLogValues(message, arg1, arg2, arg3), exception, MessageFormatter); + } + else + { + LoggerExtensions.Log(logger, logLevel, eventId, exception, message, [ arg1, arg2, arg3 ]); + } } /// @@ -2604,7 +2641,15 @@ public static void Log(this ILogger logger, LogLevel logLevel, EventId eventId, throw new ArgumentNullException(nameof(logger)); } - logger.Log(logLevel, eventId, new WarfareFormattedLogValues(message, arg1, arg2, arg3, arg4), exception, MessageFormatter); + + if (WarfareModule.IsActive) + { + logger.Log(logLevel, eventId, new WarfareFormattedLogValues(message, arg1, arg2, arg3, arg4), exception, MessageFormatter); + } + else + { + LoggerExtensions.Log(logger, logLevel, eventId, exception, message, [ arg1, arg2, arg3, arg4 ]); + } } private static readonly Func MessageFormatter = MessageFormatterMtd; diff --git a/UncreatedWarfare/UncreatedWarfare.csproj b/UncreatedWarfare/UncreatedWarfare.csproj index 002e412c..64823e5c 100644 --- a/UncreatedWarfare/UncreatedWarfare.csproj +++ b/UncreatedWarfare/UncreatedWarfare.csproj @@ -8,7 +8,7 @@ 4.0.0 - 002 + 003 prerelease$(VersionBuild) @@ -137,9 +137,9 @@ - + - + diff --git a/UncreatedWarfare/WarfareModule.cs b/UncreatedWarfare/WarfareModule.cs index 78de58ec..428230d5 100644 --- a/UncreatedWarfare/WarfareModule.cs +++ b/UncreatedWarfare/WarfareModule.cs @@ -86,6 +86,11 @@ public sealed class WarfareModule : IModuleNexus /// Do not use unless in a patch. public static WarfareModule Singleton { get; private set; } + /// + /// If Uncreated.Warfare is loaded as a module instead of as a library. + /// + public static bool IsActive { get; private set; } + #nullable restore private bool _unloadedHostedServices = true; @@ -147,6 +152,8 @@ public CancellationToken UnloadToken void IModuleNexus.initialize() { + IsActive = true; + AppDomain.CurrentDomain.AssemblyResolve += HandleAssemblyResolve; // will setup the main thread in GameThread before asserting