From 0b91b3ee7f8cb266fa022533170cc62e6a43900c Mon Sep 17 00:00:00 2001 From: Masusder <59669685+Masusder@users.noreply.github.com> Date: Mon, 23 Nov 2026 16:27:34 +0100 Subject: [PATCH] Static mapping generation --- CUE4Parse | 2 +- FModel/App.xaml.cs | 30 ++-- FModel/Framework/CacheManager.cs | 2 +- FModel/Framework/FModelBridgeHost.cs | 2 +- FModel/Framework/StaticMappingGenerator.cs | 124 ++++++++++++++++ FModel/MainWindow.xaml | 18 +++ FModel/Settings/UserSettings.cs | 17 +++ FModel/ViewModels/ApplicationViewModel.cs | 10 +- FModel/ViewModels/AudioPlayerViewModel.cs | 6 +- FModel/ViewModels/BackupManagerViewModel.cs | 4 +- FModel/ViewModels/CUE4ParseViewModel.cs | 103 ++++++++----- FModel/ViewModels/Commands/LoadCommand.cs | 2 +- FModel/ViewModels/Commands/MenuCommand.cs | 5 + FModel/ViewModels/SettingsViewModel.cs | 65 ++++----- FModel/Views/ImageMerger.xaml.cs | 2 +- .../Resources/Controls/DropOverlay.xaml.cs | 8 +- FModel/Views/SettingsView.xaml | 135 +++++++++++++++--- FModel/Views/SettingsView.xaml.cs | 91 +++++++++++- 18 files changed, 498 insertions(+), 128 deletions(-) create mode 100644 FModel/Framework/StaticMappingGenerator.cs diff --git a/CUE4Parse b/CUE4Parse index 858ecc7d..c95dd9b5 160000 --- a/CUE4Parse +++ b/CUE4Parse @@ -1 +1 @@ -Subproject commit 858ecc7dae30b5a99e9bb2dbb77cb65c527c7f32 +Subproject commit c95dd9b587a6fb8cbf030d6b84d37cd0c7512b7d diff --git a/FModel/App.xaml.cs b/FModel/App.xaml.cs index 93c06aff..d4fdea25 100644 --- a/FModel/App.xaml.cs +++ b/FModel/App.xaml.cs @@ -27,6 +27,8 @@ namespace FModel; /// public partial class App { + public string LogFilePath { get; private set; } + [DllImport("kernel32.dll")] private static extern bool AttachConsole(int dwProcessId); @@ -74,47 +76,48 @@ public partial class App if (!Directory.Exists(UserSettings.Default.RawDataDirectory)) { createMe = true; - UserSettings.Default.RawDataDirectory = Path.Combine(UserSettings.Default.OutputDirectory, "Exports"); + UserSettings.Default.RawDataDirectory = UserSettings.Default.ExportsDirectory; } if (!Directory.Exists(UserSettings.Default.PropertiesDirectory)) { createMe = true; - UserSettings.Default.PropertiesDirectory = Path.Combine(UserSettings.Default.OutputDirectory, "Exports"); + UserSettings.Default.PropertiesDirectory = UserSettings.Default.ExportsDirectory; } if (!Directory.Exists(UserSettings.Default.TextureDirectory)) { createMe = true; - UserSettings.Default.TextureDirectory = Path.Combine(UserSettings.Default.OutputDirectory, "Exports"); + UserSettings.Default.TextureDirectory = UserSettings.Default.ExportsDirectory; } if (!Directory.Exists(UserSettings.Default.AudioDirectory)) { createMe = true; - UserSettings.Default.AudioDirectory = Path.Combine(UserSettings.Default.OutputDirectory, "Exports"); + UserSettings.Default.AudioDirectory = UserSettings.Default.ExportsDirectory; } if (!Directory.Exists(UserSettings.Default.CodeDirectory)) { createMe = true; - UserSettings.Default.CodeDirectory = Path.Combine(UserSettings.Default.OutputDirectory, "Exports"); + UserSettings.Default.CodeDirectory = UserSettings.Default.ExportsDirectory; } if (!Directory.Exists(UserSettings.Default.ModelDirectory)) { createMe = true; - UserSettings.Default.ModelDirectory = Path.Combine(UserSettings.Default.OutputDirectory, "Exports"); + UserSettings.Default.ModelDirectory = UserSettings.Default.ExportsDirectory; } Directory.CreateDirectory(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "FModel")); - Directory.CreateDirectory(Path.Combine(UserSettings.Default.OutputDirectory, "Backups")); - if (createMe) Directory.CreateDirectory(Path.Combine(UserSettings.Default.OutputDirectory, "Exports")); - Directory.CreateDirectory(Path.Combine(UserSettings.Default.OutputDirectory, "Logs")); + Directory.CreateDirectory(UserSettings.Default.BackupsDirectory); + if (createMe) Directory.CreateDirectory(UserSettings.Default.ExportsDirectory); + Directory.CreateDirectory(UserSettings.Default.LogsDirectory); + Directory.CreateDirectory(UserSettings.Default.MappingsDirectory); CacheManager.EnsureDirectories(); #if DEBUG - var filePath = Path.Combine(UserSettings.Default.OutputDirectory, "Logs", $"FModel-Debug-Log-{DateTime.Now:yyyy-MM-dd}.log"); + LogFilePath = Path.Combine(UserSettings.Default.LogsDirectory, $"FModel-Debug-Log-{DateTime.Now:yyyy-MM-dd}.log"); var viewerContexts = typeof(Snooper.Hosting.Bridge).Assembly.GetTypes() .Concat(typeof(EditorWindow).Assembly.GetTypes()) @@ -122,7 +125,7 @@ public partial class App .Append("Snooper") .ToHashSet(StringComparer.Ordinal); #else - var filePath = Path.Combine(UserSettings.Default.OutputDirectory, "Logs", $"FModel-Log-{DateTime.Now:yyyy-MM-dd}.log"); + LogFilePath = Path.Combine(UserSettings.Default.LogsDirectory, $"FModel-Log-{DateTime.Now:yyyy-MM-dd}.log"); #endif const string template1 = "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u3}] {SourceContext}: {Message:lj}{NewLine}{Exception}"; const string template2 = "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u3}] [{ClassName}] {ObjectPath}: {Message:lj}{NewLine}{Exception}"; @@ -142,14 +145,15 @@ public partial class App .WriteTo.Logger(lc => lc .Filter.ByExcluding(IsConversionLibrary) .WriteTo.Console(outputTemplate: template1, theme: AnsiConsoleTheme.Literate) - .WriteTo.File(outputTemplate: template1, path: filePath, shared: true)) + .WriteTo.File(outputTemplate: template1, path: LogFilePath, shared: true)) .WriteTo.Logger(lc => lc .Filter.ByIncludingOnly(IsConversionLibrary) .WriteTo.Console(outputTemplate: template2, theme: AnsiConsoleTheme.Literate) - .WriteTo.File(outputTemplate: template2, path: filePath, shared: true)) + .WriteTo.File(outputTemplate: template2, path: LogFilePath, shared: true)) .MinimumLevel.Override("CUE4Parse_Conversion", LogEventLevel.Verbose) .WriteTo.Sink(ImGuiSink.Instance) .CreateLogger(); + CUE4Parse.CUE4ParseLog.UseLogger(Log.Logger); CacheManager.MigrateLegacyFiles(); Log.Information("Version {Version} ({CommitId})", Constants.APP_VERSION, Constants.APP_COMMIT_ID); diff --git a/FModel/Framework/CacheManager.cs b/FModel/Framework/CacheManager.cs index 55b55478..a59566af 100644 --- a/FModel/Framework/CacheManager.cs +++ b/FModel/Framework/CacheManager.cs @@ -9,7 +9,7 @@ namespace FModel.Framework; public static class CacheManager { public static string DataDirectory => - Directory.CreateDirectory(Path.Combine(UserSettings.Default.OutputDirectory, ".data")).FullName; + Directory.CreateDirectory(UserSettings.Default.CacheDirectory).FullName; public static string ChunksDirectory => Directory.CreateDirectory(Path.Combine(DataDirectory, "chunks")).FullName; diff --git a/FModel/Framework/FModelBridgeHost.cs b/FModel/Framework/FModelBridgeHost.cs index 792c8c1b..d4b75a10 100644 --- a/FModel/Framework/FModelBridgeHost.cs +++ b/FModel/Framework/FModelBridgeHost.cs @@ -12,7 +12,7 @@ namespace FModel.Framework; public sealed class FModelBridgeHost : IBridgeHost { public string Name => "FModel"; - public string ImGuiIniPath => Path.Combine(UserSettings.Default.OutputDirectory, ".data", "snooper.ini"); + public string ImGuiIniPath => Path.Combine(UserSettings.Default.CacheDirectory, "snooper.ini"); public bool CanBrowseAssets => true; diff --git a/FModel/Framework/StaticMappingGenerator.cs b/FModel/Framework/StaticMappingGenerator.cs new file mode 100644 index 00000000..1bdb442a --- /dev/null +++ b/FModel/Framework/StaticMappingGenerator.cs @@ -0,0 +1,124 @@ +using System; +using System.Collections.Generic; +using System.IO; +using CUE4Parse.MappingsProvider.Usmap; +using CUE4Parse.UE4.Versions; +using Serilog; + +namespace FModel.Framework; + +public static class StaticMappingGenerator +{ + public static string Generate(string executable, string mappingsDirectory, EGame game, EUsmapCompressionMethod compression, string gameDisplayName = null) + { + if (game < EGame.GAME_UE4_18) + throw new NotSupportedException("Static mapping generation requires Unreal Engine 4.18 or later."); + + var directory = Directory.CreateDirectory(mappingsDirectory).FullName; + var pendingOutput = Path.Combine(directory, $"{Guid.NewGuid():N}.usmap"); + + try + { + if (!UsmapGenerator.Generate(executable, pendingOutput, compression)) + throw new InvalidOperationException("UsmapGen could not create the mapping."); + + using var stream = File.OpenRead(pendingOutput); + var engineVersion = new UsmapParser(stream, comparer: StringComparer.Ordinal).EngineVersion; + + var major = engineVersion?.Major ?? ((int) game >> 24); + var minor = engineVersion?.Minor ?? (((int) game >> 16) & 0xFF); + var suffix = compression == EUsmapCompressionMethod.None ? "" : $"_{compression}"; + var name = gameDisplayName == null ? null : string.Join("_", gameDisplayName.Split(Path.GetInvalidFileNameChars(), StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)); + if (string.IsNullOrEmpty(name) || string.Equals(name, "Unknown", StringComparison.OrdinalIgnoreCase)) + name = Path.GetFileNameWithoutExtension(executable); + + // Example: 501_GameName_22-11-2026_ZStandard.usmap (Engine version without using "." is intentional) + var fileName = FormattableString.Invariant($"{major}{minor:D2}_{name}_{DateTime.Today:dd-MM-yyyy}{suffix}.usmap"); + var output = Path.Combine(directory, fileName); + File.Move(pendingOutput, output, overwrite: true); + return output; + } + finally + { + try + { + File.Delete(pendingOutput); + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + Log.Warning(ex, "Could not remove temporary mapping {Path}", pendingOutput); + } + } + } + + public static string FindShippingExe(string archiveDirectory) + { + if (!Directory.Exists(archiveDirectory)) + throw new DirectoryNotFoundException("Static mapping generation requires a local archive directory."); + + var archive = new DirectoryInfo(archiveDirectory); + for (var directory = archive; directory.Parent != null; directory = directory.Parent) + { + if (Search(directory) is { } executable) + return executable; + } + + var options = new EnumerationOptions { AttributesToSkip = FileAttributes.ReparsePoint }; + string match = null; + foreach (var project in FindProjects(archive)) + { + if (Search(project) is not { } executable) + continue; + + if (match != null) + throw new FileNotFoundException($"Found multiple Win64 shipping executables under '{archiveDirectory}'."); + + match = executable; + } + + return match ?? throw new FileNotFoundException($"No Win64 shipping executable was found near '{archiveDirectory}'."); + + IEnumerable FindProjects(DirectoryInfo directory) + { + foreach (var child in directory.EnumerateDirectories("*", options)) + { + if (child.Name.Equals("Engine", StringComparison.OrdinalIgnoreCase)) + continue; + + if (Directory.Exists(Path.Combine(child.FullName, "Content", "Paks"))) + { + yield return child; + continue; + } + + foreach (var project in FindProjects(child)) + yield return project; + } + } + + static string Search(DirectoryInfo project) + { + var binaries = Path.Combine(project.FullName, "Binaries", "Win64"); + if (!Directory.Exists(binaries)) + return null; + + var preferred = Path.Combine(binaries, $"{project.Name}-Win64-Shipping.exe"); + if (File.Exists(preferred)) + return preferred; + + var projectExecutable = Path.Combine(binaries, $"{project.Name}.exe"); + if (File.Exists(projectExecutable)) + return projectExecutable; + + using var candidates = Directory.EnumerateFiles(binaries, "*-Shipping.exe").GetEnumerator(); + if (!candidates.MoveNext()) + return null; + + var executable = candidates.Current; + if (candidates.MoveNext()) + throw new FileNotFoundException($"Found multiple shipping executables in '{binaries}'."); + + return executable; + } + } +} diff --git a/FModel/MainWindow.xaml b/FModel/MainWindow.xaml index ac86a319..c79a0f23 100644 --- a/FModel/MainWindow.xaml +++ b/FModel/MainWindow.xaml @@ -692,6 +692,24 @@ + + + + + + + @@ -186,10 +274,10 @@