From 7a3079a4c8ebcd886605b91cefc7e3d8d5756f83 Mon Sep 17 00:00:00 2001 From: "waffle.lord" Date: Fri, 12 Jul 2024 17:29:43 -0400 Subject: [PATCH] add copy logs command --- .../Controllers/GameStarter.cs | 2 - .../Controllers/LogManager.cs | 10 +-- .../ViewModels/SettingsViewModel.cs | 76 ++++++++++++++++++- project/SPT.Launcher/Views/SettingsView.axaml | 23 +++--- 4 files changed, 91 insertions(+), 20 deletions(-) diff --git a/project/SPT.Launcher.Base/Controllers/GameStarter.cs b/project/SPT.Launcher.Base/Controllers/GameStarter.cs index 3c2e715..9903c14 100644 --- a/project/SPT.Launcher.Base/Controllers/GameStarter.cs +++ b/project/SPT.Launcher.Base/Controllers/GameStarter.cs @@ -300,7 +300,6 @@ namespace SPT.Launcher foreach (var value in key.GetValueNames()) { key.DeleteValue(value); - LogManager.Instance.Debug($"Removing reg key: {key.Name}"); } } catch (Exception ex) @@ -352,7 +351,6 @@ namespace SPT.Launcher { file.IsReadOnly = false; file.Delete(); - LogManager.Instance.Debug($" -> del file: {file.FullName}"); } // remove directory diff --git a/project/SPT.Launcher.Base/Controllers/LogManager.cs b/project/SPT.Launcher.Base/Controllers/LogManager.cs index 4f263b5..d615fe1 100644 --- a/project/SPT.Launcher.Base/Controllers/LogManager.cs +++ b/project/SPT.Launcher.Base/Controllers/LogManager.cs @@ -20,16 +20,16 @@ namespace SPT.Launcher.Controllers private static LogManager _instance; public static LogManager Instance => _instance ??= new LogManager(); private readonly string _filePath; - private readonly string _logFile; + public readonly string LogFile; private LogManager() { _filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "user", "logs"); - _logFile = Path.Combine(_filePath, "launcher.log"); + LogFile = Path.Combine(_filePath, "launcher.log"); - if (File.Exists(_logFile)) + if (File.Exists(LogFile)) { - File.Delete(_logFile); + File.Delete(LogFile); } Write($" ==== Launcher Started ===="); @@ -52,7 +52,7 @@ namespace SPT.Launcher.Controllers Directory.CreateDirectory(_filePath); } - File.AppendAllLines(_logFile, new[] { $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}]{GetDevModeTag()}{text}" }); + File.AppendAllLines(LogFile, new[] { $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}]{GetDevModeTag()}{text}" }); } public void Debug(string text) => Write($"[Debug] {text}"); diff --git a/project/SPT.Launcher/ViewModels/SettingsViewModel.cs b/project/SPT.Launcher/ViewModels/SettingsViewModel.cs index 54d8902..86f402a 100644 --- a/project/SPT.Launcher/ViewModels/SettingsViewModel.cs +++ b/project/SPT.Launcher/ViewModels/SettingsViewModel.cs @@ -7,12 +7,14 @@ using Avalonia; using Avalonia.Controls; using ReactiveUI; using System; +using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Reflection; using System.Threading.Tasks; using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Controls.Notifications; +using Avalonia.Input; using Avalonia.Platform.Storage; namespace SPT.Launcher.ViewModels @@ -36,6 +38,64 @@ namespace SPT.Launcher.ViewModels LauncherSettingsProvider.Instance.SaveSettings(); } + public async Task CopyLogsToClipboard() + { + LogManager.Instance.Info("[Settings] Copying logs to clipboard ..."); + + if (Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) + { + if (desktop.MainWindow?.Clipboard == null) + { + LogManager.Instance.Error("[Settings] Failed to get clipboard"); + return; + } + + var traceLogs = Directory.GetFiles(Path.Join(LauncherSettingsProvider.Instance.GamePath, "Logs"), $"{DateTime.Now:yyyy.MM.dd}_* traces.log", SearchOption.AllDirectories); + + var traceLog = traceLogs.Length > 0 ? traceLogs[0] : ""; + + var filesToCopy = new string[] + { + Path.Join(LauncherSettingsProvider.Instance.GamePath, @"\user\logs", $"server-{DateTime.Now:yyyy-MM-dd}.log"), + traceLog, + Path.Join(LauncherSettingsProvider.Instance.GamePath, @"BepInEx\LogOutput.log"), + Path.Join(LauncherSettingsProvider.Instance.GamePath, @"\user\profiles", $"{AccountManager.SelectedAccount.id}.json"), + LogManager.Instance.LogFile, + }; + + List files = new List(); + + foreach (var logPath in filesToCopy) + { + var file = await desktop.MainWindow.StorageProvider.TryGetFileFromPathAsync(logPath); + + if (file != null) + { + LogManager.Instance.Debug($"file to copy :: {logPath}"); + files.Add(file); + continue; + } + + LogManager.Instance.Warning($"failed to get file to copy :: {logPath}"); + } + + if (files.Count == 0) + { + LogManager.Instance.Warning("[Settings] Failed to copy log files"); + SendNotification("", LocalizationProvider.Instance.copy_failed); + } + + var data = new DataObject(); + + data.Set(DataFormats.Files, files.ToArray()); + + await desktop.MainWindow.Clipboard.SetDataObjectAsync(data); + + LogManager.Instance.Info($"[Settings] {files.Count} log/s copied to clipboard"); + SendNotification("", $"{files.Count} {LocalizationProvider.Instance.copied}"); + } + } + public void GoBackCommand() { if (Application.Current?.ApplicationLifetime is Avalonia.Controls.ApplicationLifetimes.IClassicDesktopStyleApplicationLifetime desktop) @@ -55,40 +115,47 @@ namespace SPT.Launcher.ViewModels public void CleanTempFilesCommand() { + LogManager.Instance.Info("[Settings] Clearing temp files ..."); bool filesCleared = gameStarter.CleanTempFiles(); if (filesCleared) { + LogManager.Instance.Info("[Settings] Temp files cleared"); SendNotification("", LocalizationProvider.Instance.clean_temp_files_succeeded, NotificationType.Success); } else { + LogManager.Instance.Info("[Settings] Temp files failed to clear"); SendNotification("", LocalizationProvider.Instance.clean_temp_files_failed, NotificationType.Error); } } public void RemoveRegistryKeysCommand() { + LogManager.Instance.Info("[Settings] Removing registry keys ..."); bool regKeysRemoved = gameStarter.RemoveRegistryKeys(); if (regKeysRemoved) { + LogManager.Instance.Info("[Settings] Registry keys removed"); SendNotification("", LocalizationProvider.Instance.remove_registry_keys_succeeded, Avalonia.Controls.Notifications.NotificationType.Success); } else { + LogManager.Instance.Info("[Settings] Registry keys failed to remove"); SendNotification("", LocalizationProvider.Instance.remove_registry_keys_failed, Avalonia.Controls.Notifications.NotificationType.Error); } } public async Task ResetGameSettingsCommand() { + LogManager.Instance.Info("[Settings] Reseting game settings ..."); string EFTSettingsFolder = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Battlestate Games", "Escape from Tarkov", "Settings"); string SPTSettingsFolder = Path.Join(LauncherSettingsProvider.Instance.GamePath, "user", "sptsettings"); if (!Directory.Exists(EFTSettingsFolder)) { - LogManager.Instance.Warning($"EFT settings folder not found, can't reset :: Path: {EFTSettingsFolder}"); + LogManager.Instance.Warning($"[Settings] EFT settings folder not found, can't reset :: Path: {EFTSettingsFolder}"); SendNotification("", LocalizationProvider.Instance.load_live_settings_failed, Avalonia.Controls.Notifications.NotificationType.Error); return; } @@ -114,12 +181,14 @@ namespace SPT.Launcher.ViewModels SendNotification("", LocalizationProvider.Instance.load_live_settings_failed, Avalonia.Controls.Notifications.NotificationType.Error); return; } - + + LogManager.Instance.Info("[Settings] Game settings reset to live settings"); SendNotification("", LocalizationProvider.Instance.load_live_settings_succeeded, Avalonia.Controls.Notifications.NotificationType.Success); } public async Task ClearGameSettingsCommand() { + LogManager.Instance.Info("[Settings] Clearing game settings ..."); var SPTSettingsDir = new DirectoryInfo(Path.Join(LauncherSettingsProvider.Instance.GamePath, "user", "sptsettings")); try @@ -134,7 +203,8 @@ namespace SPT.Launcher.ViewModels SendNotification("", LocalizationProvider.Instance.clear_game_settings_failed, Avalonia.Controls.Notifications.NotificationType.Error); return; } - + + LogManager.Instance.Info("[Settings] Game settings cleared"); SendNotification("", LocalizationProvider.Instance.clear_game_settings_succeeded, Avalonia.Controls.Notifications.NotificationType.Success); } diff --git a/project/SPT.Launcher/Views/SettingsView.axaml b/project/SPT.Launcher/Views/SettingsView.axaml index a955b9d..dd981c3 100644 --- a/project/SPT.Launcher/Views/SettingsView.axaml +++ b/project/SPT.Launcher/Views/SettingsView.axaml @@ -12,29 +12,29 @@