2024-05-21 20:15:19 +01:00
using SPT.Launcher.Controllers ;
using SPT.Launcher.Helpers ;
using SPT.Launcher.Models ;
using SPT.Launcher.Models.Launcher ;
2023-03-03 19:25:33 +00:00
using Avalonia ;
using ReactiveUI ;
using System ;
2024-07-12 17:29:43 -04:00
using System.Collections.Generic ;
2023-03-03 19:25:33 +00:00
using System.Diagnostics ;
using System.IO ;
using System.Reflection ;
using System.Threading.Tasks ;
2024-07-12 09:38:09 -04:00
using Avalonia.Controls.ApplicationLifetimes ;
2024-06-04 19:10:21 -04:00
using Avalonia.Controls.Notifications ;
2024-07-12 17:29:43 -04:00
using Avalonia.Input ;
2024-07-12 09:38:09 -04:00
using Avalonia.Platform.Storage ;
2023-03-03 19:25:33 +00:00
2024-05-21 20:15:19 +01:00
namespace SPT.Launcher.ViewModels
2023-03-03 19:25:33 +00:00
{
public class SettingsViewModel : ViewModelBase
{
public LocaleCollection Locales { get ; set ; } = new LocaleCollection ( ) ;
private GameStarter gameStarter = new GameStarter ( new GameStarterFrontend ( ) ) ;
public SettingsViewModel ( IScreen Host ) : base ( Host )
{
if ( Application . Current ? . ApplicationLifetime is Avalonia . Controls . ApplicationLifetimes . IClassicDesktopStyleApplicationLifetime desktop )
{
desktop . MainWindow . Closing + = MainWindow_Closing ;
}
}
private void MainWindow_Closing ( object? sender , System . ComponentModel . CancelEventArgs e )
{
LauncherSettingsProvider . Instance . SaveSettings ( ) ;
}
2024-07-12 17:29:43 -04:00
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 ;
}
2024-08-07 13:21:57 +00:00
var filesToCopy = new List < string > { LogManager . Instance . LogFile } ;
2024-07-12 17:29:43 -04:00
2024-08-07 13:21:57 +00:00
var serverLog = Path . Join ( LauncherSettingsProvider . Instance . GamePath , @"\user\logs" ,
$"server-{DateTime.Now:yyyy-MM-dd}.log" ) ;
var bepinexLog = Path . Join ( LauncherSettingsProvider . Instance . GamePath , @"BepInEx\LogOutput.log" ) ;
2024-07-12 17:29:43 -04:00
2024-08-07 13:21:57 +00:00
if ( AccountManager . SelectedAccount ? . id ! = null )
2024-07-12 17:29:43 -04:00
{
2024-08-07 13:21:57 +00:00
filesToCopy . Add ( Path . Join ( LauncherSettingsProvider . Instance . GamePath , @"\user\profiles" ,
$"{AccountManager.SelectedAccount.id}.json" ) ) ;
}
if ( File . Exists ( serverLog ) )
{
filesToCopy . Add ( serverLog ) ;
}
if ( File . Exists ( bepinexLog ) )
{
filesToCopy . Add ( bepinexLog ) ;
}
2024-07-12 17:29:43 -04:00
2024-08-07 13:21:57 +00:00
var logsPath = Path . Join ( LauncherSettingsProvider . Instance . GamePath , "Logs" ) ;
if ( Directory . Exists ( logsPath ) )
{
var traceLogs = Directory . GetFiles ( logsPath , $"{DateTime.Now:yyyy.MM.dd}_* traces.log" ,
SearchOption . AllDirectories ) ;
var log = traceLogs . Length > 0 ? traceLogs [ 0 ] : "" ;
if ( ! string . IsNullOrWhiteSpace ( log ) )
{
filesToCopy . Add ( log ) ;
}
}
2024-07-12 17:29:43 -04:00
List < IStorageFile > files = new List < IStorageFile > ( ) ;
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}" ) ;
}
}
2023-03-03 19:25:33 +00:00
public void GoBackCommand ( )
{
if ( Application . Current ? . ApplicationLifetime is Avalonia . Controls . ApplicationLifetimes . IClassicDesktopStyleApplicationLifetime desktop )
{
desktop . MainWindow . Closing - = MainWindow_Closing ;
}
LauncherSettingsProvider . Instance . AllowSettings = true ;
2024-06-04 19:10:21 -04:00
if ( ! LauncherSettingsProvider . Instance . SaveSettings ( ) )
{
SendNotification ( "" , LocalizationProvider . Instance . failed_to_save_settings , NotificationType . Error ) ;
}
2023-03-03 19:25:33 +00:00
NavigateBack ( ) ;
}
public void CleanTempFilesCommand ( )
{
2024-07-12 17:29:43 -04:00
LogManager . Instance . Info ( "[Settings] Clearing temp files ..." ) ;
2023-03-03 19:25:33 +00:00
bool filesCleared = gameStarter . CleanTempFiles ( ) ;
if ( filesCleared )
{
2024-07-12 17:29:43 -04:00
LogManager . Instance . Info ( "[Settings] Temp files cleared" ) ;
2024-06-04 19:10:21 -04:00
SendNotification ( "" , LocalizationProvider . Instance . clean_temp_files_succeeded , NotificationType . Success ) ;
2023-03-03 19:25:33 +00:00
}
else
{
2024-07-12 17:29:43 -04:00
LogManager . Instance . Info ( "[Settings] Temp files failed to clear" ) ;
2024-06-04 19:10:21 -04:00
SendNotification ( "" , LocalizationProvider . Instance . clean_temp_files_failed , NotificationType . Error ) ;
2023-03-03 19:25:33 +00:00
}
}
2023-08-22 19:42:14 -04:00
public async Task ResetGameSettingsCommand ( )
{
2024-07-12 17:29:43 -04:00
LogManager . Instance . Info ( "[Settings] Reseting game settings ..." ) ;
2023-08-22 20:06:12 -04:00
string EFTSettingsFolder = Path . Join ( Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) , "Battlestate Games" , "Escape from Tarkov" , "Settings" ) ;
2023-08-22 19:42:14 -04:00
string SPTSettingsFolder = Path . Join ( LauncherSettingsProvider . Instance . GamePath , "user" , "sptsettings" ) ;
if ( ! Directory . Exists ( EFTSettingsFolder ) )
{
2024-07-12 17:29:43 -04:00
LogManager . Instance . Warning ( $"[Settings] EFT settings folder not found, can't reset :: Path: {EFTSettingsFolder}" ) ;
2023-08-23 18:24:56 -04:00
SendNotification ( "" , LocalizationProvider . Instance . load_live_settings_failed , Avalonia . Controls . Notifications . NotificationType . Error ) ;
2023-08-22 19:42:14 -04:00
return ;
}
try
{
Directory . CreateDirectory ( SPTSettingsFolder ) ;
foreach ( string dirPath in Directory . GetDirectories ( EFTSettingsFolder , "*" , SearchOption . AllDirectories ) )
{
Directory . CreateDirectory ( dirPath . Replace ( EFTSettingsFolder , SPTSettingsFolder ) ) ;
}
//Copy all the files & Replaces any files with the same name
foreach ( string newPath in Directory . GetFiles ( EFTSettingsFolder , "*.*" , SearchOption . AllDirectories ) )
{
File . Copy ( newPath , newPath . Replace ( EFTSettingsFolder , SPTSettingsFolder ) , true ) ;
}
}
catch ( Exception ex )
{
LogManager . Instance . Exception ( ex ) ;
2023-08-23 18:24:56 -04:00
SendNotification ( "" , LocalizationProvider . Instance . load_live_settings_failed , Avalonia . Controls . Notifications . NotificationType . Error ) ;
2023-08-22 19:42:14 -04:00
return ;
}
2024-07-12 17:29:43 -04:00
LogManager . Instance . Info ( "[Settings] Game settings reset to live settings" ) ;
2023-08-23 18:24:56 -04:00
SendNotification ( "" , LocalizationProvider . Instance . load_live_settings_succeeded , Avalonia . Controls . Notifications . NotificationType . Success ) ;
2023-08-22 19:42:14 -04:00
}
2023-03-03 19:25:33 +00:00
public async Task ClearGameSettingsCommand ( )
{
2024-07-12 17:29:43 -04:00
LogManager . Instance . Info ( "[Settings] Clearing game settings ..." ) ;
2023-08-22 18:54:54 -04:00
var SPTSettingsDir = new DirectoryInfo ( Path . Join ( LauncherSettingsProvider . Instance . GamePath , "user" , "sptsettings" ) ) ;
2023-03-03 19:25:33 +00:00
2023-08-22 18:54:54 -04:00
try
2023-03-03 19:25:33 +00:00
{
2023-08-22 18:54:54 -04:00
SPTSettingsDir . Delete ( true ) ;
2023-03-03 19:25:33 +00:00
2023-08-22 18:54:54 -04:00
Directory . CreateDirectory ( SPTSettingsDir . FullName ) ;
}
catch ( Exception ex )
2023-03-03 19:25:33 +00:00
{
2023-08-22 18:54:54 -04:00
LogManager . Instance . Exception ( ex ) ;
2023-08-22 19:42:14 -04:00
SendNotification ( "" , LocalizationProvider . Instance . clear_game_settings_failed , Avalonia . Controls . Notifications . NotificationType . Error ) ;
2023-08-22 18:54:54 -04:00
return ;
2023-03-03 19:25:33 +00:00
}
2024-07-12 17:29:43 -04:00
LogManager . Instance . Info ( "[Settings] Game settings cleared" ) ;
2023-03-03 19:25:33 +00:00
SendNotification ( "" , LocalizationProvider . Instance . clear_game_settings_succeeded , Avalonia . Controls . Notifications . NotificationType . Success ) ;
}
public void OpenGameFolderCommand ( )
{
Process . Start ( new ProcessStartInfo
{
FileName = Path . EndsInDirectorySeparator ( LauncherSettingsProvider . Instance . GamePath ) ? LauncherSettingsProvider . Instance . GamePath : LauncherSettingsProvider . Instance . GamePath + Path . DirectorySeparatorChar ,
UseShellExecute = true ,
Verb = "open"
} ) ;
}
public async Task SelectGameFolderCommand ( )
{
2024-07-12 09:38:09 -04:00
if ( Application . Current . ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop )
2023-03-03 19:25:33 +00:00
{
2024-07-12 09:38:09 -04:00
var startPath = await desktop . MainWindow . StorageProvider . TryGetFolderFromPathAsync ( Assembly . GetExecutingAssembly ( ) . Location ) ;
var dir = await desktop . MainWindow . StorageProvider . OpenFolderPickerAsync ( new FolderPickerOpenOptions ( )
{
Title = "Select your SPT folder" ,
SuggestedStartLocation = startPath
} ) ;
2023-03-03 19:25:33 +00:00
2024-07-12 09:38:09 -04:00
if ( dir = = null | | dir . Count = = 0 )
2023-03-03 19:25:33 +00:00
{
2024-07-12 09:38:09 -04:00
return ;
2023-03-03 19:25:33 +00:00
}
2024-07-12 09:38:09 -04:00
LauncherSettingsProvider . Instance . GamePath = dir [ 0 ] . Path . LocalPath ;
2023-03-03 19:25:33 +00:00
}
}
}
}