From a70461254490c635905a650226a108ff8943059e Mon Sep 17 00:00:00 2001 From: LongerWarrior Date: Thu, 30 Jan 2025 22:40:11 +0200 Subject: [PATCH] SerilogEnricher --- CUE4Parse | 2 +- FModel/App.xaml.cs | 19 +++++--- FModel/Framework/SerilogEnricher.cs | 63 +++++++++++++++++++++++++ FModel/MainWindow.xaml.cs | 16 +++++-- FModel/Services/DiscordService.cs | 2 +- FModel/ViewModels/CUE4ParseViewModel.cs | 8 +--- 6 files changed, 92 insertions(+), 18 deletions(-) create mode 100644 FModel/Framework/SerilogEnricher.cs diff --git a/CUE4Parse b/CUE4Parse index e45f8547..94dbeb7c 160000 --- a/CUE4Parse +++ b/CUE4Parse @@ -1 +1 @@ -Subproject commit e45f85472248e38b35fae893fa1bf69851aa90eb +Subproject commit 94dbeb7cd3232b6719120219ae7d2a63968e9036 diff --git a/FModel/App.xaml.cs b/FModel/App.xaml.cs index aca3c9ec..a38dd3bd 100644 --- a/FModel/App.xaml.cs +++ b/FModel/App.xaml.cs @@ -104,15 +104,20 @@ public partial class App Directory.CreateDirectory(Path.Combine(UserSettings.Default.OutputDirectory, "Logs")); Directory.CreateDirectory(Path.Combine(UserSettings.Default.OutputDirectory, ".data")); + const string template = "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u3}] {Enriched}: {Message:lj}{NewLine}{Exception}"; + Log.Logger = new LoggerConfiguration() #if DEBUG - Log.Logger = new LoggerConfiguration().WriteTo.Console(theme: AnsiConsoleTheme.Literate).WriteTo.File( - Path.Combine(UserSettings.Default.OutputDirectory, "Logs", $"FModel-Debug-Log-{DateTime.Now:yyyy-MM-dd}.txt"), - outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [FModel] [{Level:u3}] {Message:lj}{NewLine}{Exception}").CreateLogger(); + .Enrich.With() + .MinimumLevel.Verbose() + .WriteTo.Console(outputTemplate: template, theme: AnsiConsoleTheme.Literate) + .WriteTo.File(outputTemplate: template, + path: Path.Combine(UserSettings.Default.OutputDirectory, "Logs", $"FModel-Debug-Log-{DateTime.Now:yyyy-MM-dd}.txt")) #else - Log.Logger = new LoggerConfiguration().WriteTo.Console(theme: AnsiConsoleTheme.Literate).WriteTo.File( - Path.Combine(UserSettings.Default.OutputDirectory, "Logs", $"FModel-Log-{DateTime.Now:yyyy-MM-dd}.txt"), - outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [FModel] [{Level:u3}] {Message:lj}{NewLine}{Exception}").CreateLogger(); + .Enrich.With() + .WriteTo.File(outputTemplate: template, + path: Path.Combine(UserSettings.Default.OutputDirectory, "Logs", $"FModel-Log-{DateTime.Now:yyyy-MM-dd}.txt")) #endif + .CreateLogger(); Log.Information("Version {Version} ({CommitId})", Constants.APP_VERSION, Constants.APP_COMMIT_ID); Log.Information("{OS}", GetOperatingSystemProductName()); @@ -183,4 +188,4 @@ public partial class App return rk.GetValue(name, null) as string; return string.Empty; } -} \ No newline at end of file +} diff --git a/FModel/Framework/SerilogEnricher.cs b/FModel/Framework/SerilogEnricher.cs new file mode 100644 index 00000000..2aadc28c --- /dev/null +++ b/FModel/Framework/SerilogEnricher.cs @@ -0,0 +1,63 @@ +using System; +using System.Diagnostics; +using System.Reflection; +using Serilog.Core; +using Serilog.Events; + +namespace FModel.Framework; + +public abstract class SerilogEnricher : ILogEventEnricher +{ + public abstract void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory); + + protected bool TryGetCaller(out MethodBase method) + { + method = null; + + var serilogAssembly = typeof(Serilog.Log).Assembly; + var stack = new StackTrace(3); + + foreach (var frame in stack.GetFrames()) + { + var m = frame.GetMethod(); + if (m?.DeclaringType is null) continue; + + if (m.DeclaringType.Assembly != serilogAssembly) + { + method = m; + break; + } + } + + return method != null; + } +} + +public class SourceEnricher : SerilogEnricher +{ + public override void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) + { + var source = "N/A"; + if (logEvent.Properties.TryGetValue("SourceContext", out var sourceContext)) + { + source = sourceContext.ToString()[1..^1]; + } + else if (TryGetCaller(out var method)) + { + source = method.DeclaringType?.Namespace; + } + + logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("Enriched", source.Split('.')[0])); + } +} + +public class CallerEnricher : SerilogEnricher +{ + public override void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) + { + if (TryGetCaller(out var method)) + { + logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("Enriched", $"{method.DeclaringType?.FullName}.{method.Name}")); + } + } +} diff --git a/FModel/MainWindow.xaml.cs b/FModel/MainWindow.xaml.cs index 1fe3623a..c92895e8 100644 --- a/FModel/MainWindow.xaml.cs +++ b/FModel/MainWindow.xaml.cs @@ -85,10 +85,10 @@ public partial class MainWindow #if DEBUG // await _threadWorkerView.Begin(cancellationToken => // _applicationView.CUE4Parse.Extract(cancellationToken, - // "FortniteGame/Content/Athena/Apollo/Maps/UI/Apollo_Terrain_Minimap.uasset")); + // "MyProject/Content/FirstPerson/Meshes/FirstPersonProjectileMesh.uasset")); // await _threadWorkerView.Begin(cancellationToken => // _applicationView.CUE4Parse.Extract(cancellationToken, - // "FortniteGame/Content/Environments/Helios/Props/GlacierHotel/GlacierHotel_Globe_A/Meshes/SM_GlacierHotel_Globe_A.uasset")); + // "RED/Content/Chara/ABA/Costume01/Animation/Charaselect/body/stand_body01.uasset")); #endif } @@ -208,7 +208,7 @@ public partial class MainWindow await _threadWorkerView.Begin(cancellationToken => { _applicationView.CUE4Parse.TextureFolder(cancellationToken, folder); }); FLogger.Append(ELog.Information, () => { - FLogger.Text("Successfully saved ", Constants.WHITE); + FLogger.Text("Successfully saved textures from ", Constants.WHITE); FLogger.Link(folder.PathAtThisPoint, UserSettings.Default.TextureDirectory, true); }); } @@ -219,6 +219,11 @@ public partial class MainWindow if (AssetsFolderName.SelectedItem is TreeItem folder) { await _threadWorkerView.Begin(cancellationToken => { _applicationView.CUE4Parse.ModelFolder(cancellationToken, folder); }); + FLogger.Append(ELog.Information, () => + { + FLogger.Text("Successfully saved models from ", Constants.WHITE); + FLogger.Link(folder.PathAtThisPoint, UserSettings.Default.ModelDirectory, true); + }); } } @@ -227,6 +232,11 @@ public partial class MainWindow if (AssetsFolderName.SelectedItem is TreeItem folder) { await _threadWorkerView.Begin(cancellationToken => { _applicationView.CUE4Parse.AnimationFolder(cancellationToken, folder); }); + FLogger.Append(ELog.Information, () => + { + FLogger.Text("Successfully saved animations from ", Constants.WHITE); + FLogger.Link(folder.PathAtThisPoint, UserSettings.Default.ModelDirectory, true); + }); } } diff --git a/FModel/Services/DiscordService.cs b/FModel/Services/DiscordService.cs index c5b36d28..91ae7627 100644 --- a/FModel/Services/DiscordService.cs +++ b/FModel/Services/DiscordService.cs @@ -22,7 +22,7 @@ namespace FModel.Services private readonly Assets _staticAssets = new() { - LargeImageKey = "official_logo", SmallImageKey = "verified", SmallImageText = $"v{Constants.APP_VERSION}" + LargeImageKey = "official_logo", SmallImageKey = "verified", SmallImageText = $"v{Constants.APP_VERSION} ({Constants.APP_SHORT_COMMIT_ID})" }; private readonly Button[] _buttons = diff --git a/FModel/ViewModels/CUE4ParseViewModel.cs b/FModel/ViewModels/CUE4ParseViewModel.cs index edbe2876..0ee16cb5 100644 --- a/FModel/ViewModels/CUE4ParseViewModel.cs +++ b/FModel/ViewModels/CUE4ParseViewModel.cs @@ -912,9 +912,7 @@ public class CUE4ParseViewModel : ViewModel SnooperViewer.Run(); return true; } - case UAnimSequence when isNone && ModelIsWaitingAnimation: - case UAnimMontage when isNone && ModelIsWaitingAnimation: - case UAnimComposite when isNone && ModelIsWaitingAnimation: + case UAnimSequenceBase when isNone && ModelIsWaitingAnimation: { SnooperViewer.Renderer.Animate(pointer.Object); SnooperViewer.Run(); @@ -924,9 +922,7 @@ public class CUE4ParseViewModel : ViewModel case USkeletalMesh when HasFlag(bulk, EBulkType.Meshes): case USkeleton when UserSettings.Default.SaveSkeletonAsMesh && HasFlag(bulk, EBulkType.Meshes): // case UMaterialInstance when HasFlag(bulk, EBulkType.Materials): // read the fucking json - case UAnimSequence when HasFlag(bulk, EBulkType.Animations): - case UAnimMontage when HasFlag(bulk, EBulkType.Animations): - case UAnimComposite when HasFlag(bulk, EBulkType.Animations): + case UAnimSequenceBase when HasFlag(bulk, EBulkType.Animations): { SaveExport(pointer.Object.Value, updateUi); return true;