From 11dbf83faff426408a5b886d3dd6357c49b76bc6 Mon Sep 17 00:00:00 2001 From: Westin Miller Date: Mon, 30 Oct 2017 13:43:35 -0700 Subject: [PATCH] NLog works again --- NLog.config | 2 + Torch/Managers/KeenLogPatch.cs | 2 +- Torch/Patches/GameAnalyticsPatch.cs | 57 +++++++++++++++++++++++++++++ Torch/Torch.csproj | 1 + Torch/TorchBase.cs | 2 + Torch/Utils/TorchLauncher.cs | 2 +- 6 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 Torch/Patches/GameAnalyticsPatch.cs diff --git a/NLog.config b/NLog.config index 1025b68..2b900e3 100644 --- a/NLog.config +++ b/NLog.config @@ -5,6 +5,7 @@ + @@ -15,6 +16,7 @@ + diff --git a/Torch/Managers/KeenLogPatch.cs b/Torch/Managers/KeenLogPatch.cs index c44e7a8..dfa9c2a 100644 --- a/Torch/Managers/KeenLogPatch.cs +++ b/Torch/Managers/KeenLogPatch.cs @@ -13,7 +13,7 @@ using VRage.Utils; namespace Torch.Managers { -// [PatchShim] + [PatchShim] internal static class KeenLogPatch { private static readonly Logger _log = LogManager.GetLogger("Keen"); diff --git a/Torch/Patches/GameAnalyticsPatch.cs b/Torch/Patches/GameAnalyticsPatch.cs new file mode 100644 index 0000000..66bcf72 --- /dev/null +++ b/Torch/Patches/GameAnalyticsPatch.cs @@ -0,0 +1,57 @@ +using System; +using System.IO; +using System.Reflection; +using System.Runtime.CompilerServices; +using NLog; +using NLog.Config; +using Torch.Managers.PatchManager; + +namespace Torch.Patches +{ + [PatchShim] + public static class GameAnalyticsPatch + { + private static readonly Logger _log = LogManager.GetCurrentClassLogger(); + private static Action _setLogger; + + public static void Patch(PatchContext ctx) + { + Type type = Type.GetType("GameAnalyticsSDK.Net.Logging.GALogger, GameAnalytics.Mono"); + if (type == null) + return; + FieldInfo loggerField = type.GetField("logger", + BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public); + if (loggerField == null) + { + _log.Warn("GALogger logger field is unknown. Logging may not function."); + return; + } + RuntimeHelpers.RunClassConstructor(type.TypeHandle); + _setLogger = loggerField?.CreateSetter(); + FixLogging(); + + ConstructorInfo ctor = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[0], new ParameterModifier[0]); + if (ctor == null) + { + _log.Warn("GALogger constructor is unknown. Logging may not function."); + return; + } + ctx.GetPattern(ctor).Prefixes.Add(typeof(GameAnalyticsPatch).GetMethod(nameof(PatchLogger), + BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)); + } + + private static void FixLogging() + { + _setLogger(LogManager.GetLogger("GameAnalytics")); + if (!(LogManager.Configuration is XmlLoggingConfiguration)) + LogManager.Configuration = new XmlLoggingConfiguration(Path.Combine( + Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) ?? Environment.CurrentDirectory, "NLog.config")); + } + + private static bool PatchLogger() + { + FixLogging(); + return false; + } + } +} diff --git a/Torch/Torch.csproj b/Torch/Torch.csproj index 4994736..ab4a85f 100644 --- a/Torch/Torch.csproj +++ b/Torch/Torch.csproj @@ -190,6 +190,7 @@ + diff --git a/Torch/TorchBase.cs b/Torch/TorchBase.cs index 882fd65..e000fd7 100644 --- a/Torch/TorchBase.cs +++ b/Torch/TorchBase.cs @@ -50,6 +50,8 @@ namespace Torch ReflectedManager.Process(typeof(TorchBase).Assembly); ReflectedManager.Process(typeof(ITorchBase).Assembly); PatchManager.AddPatchShim(typeof(GameStatePatchShim)); + PatchManager.AddPatchShim(typeof(GameAnalyticsPatch)); + PatchManager.AddPatchShim(typeof(KeenLogPatch)); PatchManager.CommitInternal(); RegisterCoreAssembly(typeof(ITorchBase).Assembly); RegisterCoreAssembly(typeof(TorchBase).Assembly); diff --git a/Torch/Utils/TorchLauncher.cs b/Torch/Utils/TorchLauncher.cs index fbe4e31..f368548 100644 --- a/Torch/Utils/TorchLauncher.cs +++ b/Torch/Utils/TorchLauncher.cs @@ -31,7 +31,7 @@ namespace Torch.Utils allPaths.Add(other.ToLower().Replace('/', '\\')); var pathPrefix = StringUtils.CommonPrefix(allPaths); #pragma warning disable 618 - AppDomain.CurrentDomain.AppendPrivatePath(String.Join(Path.PathSeparator.ToString(), allPaths)); + AppDomain.CurrentDomain.AppendPrivatePath(string.Join(Path.PathSeparator.ToString(), allPaths)); #pragma warning restore 618 AppDomain.CurrentDomain.SetData(TorchKey, true); AppDomain.CurrentDomain.ExecuteAssemblyByName(entryPoint, args);