123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- using ReactiveUI;
- using ReactiveUI.Validation.Extensions;
- using System;
- using System.IO;
- using System.Reflection;
- using VeloeMinecraftLauncher.Utils;
- using Avalonia.Controls;
- using System.Threading.Tasks;
- using System.Collections.ObjectModel;
- using Serilog.Events;
- using System.Linq;
- using ReactiveUI.Validation.Components;
- using Avalonia.Platform.Storage;
- namespace VeloeMinecraftLauncher.ViewModels;
- public class SettingsWindowViewModel : ViewModelBase
- {
- public SettingsWindowViewModel(Serilog.ILogger logger)
- {
- LauncherVersion = Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "unknown";
- _logger = logger;
- this.ValidationRule(
- viewModel => viewModel.MaxRam,
- value => { UInt32 result; bool resultb = UInt32.TryParse(value, out result); return resultb; },
- "Not a number.");
- var helper = this.ValidationRule(
- viewModel => viewModel.JavaPath,
- value => { if (value is null) return false; string path = Path.Combine(value , "bin/java"); if (OperatingSystem.IsWindows()) path += ".exe"; bool resultb = File.Exists(path); return resultb; },
- "Can't find java executable.");
- this.ValidationRule(
- viewModel => viewModel.MaxLog,
- value => { UInt32 result; bool resultb = UInt32.TryParse(value, out result); return resultb; },
- "Not a number.");
- }
- private string _launcherVersion = string.Empty;
- private string _minecraftFolderPath = Settings.minecraftForlderPath;
- private string _javaPath = Settings.javaPath;
- private string _maxRam = Settings.maxRam.ToString();
- private bool _isValid;
- private LogEventLevel _fileLogEventLevel = Settings.fileLogEventLevel;
- private LogEventLevel _consoleLogEventLevel = Settings.consoleLogEventLevel;
- private string _maxLog = Settings.maxLog.ToString();
-
- private ObservableCollection<LogEventLevel> _logEventLevels = new() {LogEventLevel.Debug, LogEventLevel.Information, LogEventLevel.Warning, LogEventLevel.Error };
- public bool UseCustomJava
- {
- get => Settings.useCustomJava;
- set
- {
- this.RaiseAndSetIfChanged(ref Settings.useCustomJava, value);
- this.RaisePropertyChanged(nameof(IsValid));
- }
- }
- public bool SetMaxRam
- {
- get => Settings.setMaxRam;
- set => this.RaiseAndSetIfChanged(ref Settings.setMaxRam, value);
- }
- public bool SetMinecraftFolder
- {
- get => Settings.setPath;
- set
- {
- this.RaiseAndSetIfChanged(ref Settings.setPath, value);
- this.RaisePropertyChanged(nameof(IsValid));
- }
- }
- public bool CheckAssets
- {
- get => Settings.checkGameAssets;
- set => this.RaiseAndSetIfChanged(ref Settings.checkGameAssets, value);
- }
- public string JavaPath
- {
- get => _javaPath;
- set
- {
- this.RaiseAndSetIfChanged(ref _javaPath, value);
- this.RaisePropertyChanged(nameof(IsValid));
- }
- }
- public string MaxRam
- {
- get => _maxRam;
- set
- {
- this.RaiseAndSetIfChanged(ref _maxRam, value);
- this.RaisePropertyChanged(nameof(IsValid));
- }
- }
- public bool IsValid
- {
- get
- {
- _isValid = true;
- if (!UseCustomJava)
- {
- var rules = ValidationContext.Validations.Where(r => !((BasePropertyValidation<SettingsWindowViewModel>)r).Properties.Contains(nameof(JavaPath)));
- foreach (var rule in rules)
- _isValid &= rule.IsValid;
- }
- else
- _isValid = ValidationContext.GetIsValid();
- return _isValid;
- }
- }
- public string MinecraftFolderPath
- {
- get => _minecraftFolderPath;
- set => this.RaiseAndSetIfChanged(ref _minecraftFolderPath, value);
- }
- public string LauncherVersion
- {
- get => _launcherVersion;
- set => this.RaiseAndSetIfChanged(ref _launcherVersion, value);
- }
- public bool GameLogToLauncher
- {
- get => Settings.gameLogToLauncher;
- set => this.RaiseAndSetIfChanged(ref Settings.gameLogToLauncher, value);
- }
- public ObservableCollection<LogEventLevel> LogEventLevels
- {
- get => _logEventLevels;
- set => this.RaiseAndSetIfChanged(ref _logEventLevels, value);
- }
- public LogEventLevel FileLogEventLevel
- {
- get =>_fileLogEventLevel;
- set => this.RaiseAndSetIfChanged(ref _fileLogEventLevel, value);
- }
- public LogEventLevel ConsoleLogEventLevel
- {
- get => _consoleLogEventLevel;
- set => this.RaiseAndSetIfChanged(ref _consoleLogEventLevel, value);
- }
- public bool SetMaxLog
- {
- get => Settings.setMaxLog;
- set => this.RaiseAndSetIfChanged(ref Settings.setMaxLog, value);
- }
- public string MaxLog
- {
- get => _maxLog;
- set => this.RaiseAndSetIfChanged(ref _maxLog, value);
- }
- public void SaveSettings()
- {
- Settings.javaPath = _javaPath;
- Settings.minecraftForlderPath = _minecraftFolderPath;
- Settings.maxRam = UInt32.Parse(_maxRam);
- Settings.maxLog = UInt32.Parse(_maxLog);
- Settings.consoleLogEventLevel = _consoleLogEventLevel;
- Settings.fileLogEventLevel = _fileLogEventLevel;
- Settings.SaveSettings();
- }
- public void OpenMinecraftPathDialog(object window)
- {
- Task.Run(async() =>
- {
- var storageProvider = ((Window)window).StorageProvider;
- var initPath = String.Empty;
- if (!string.IsNullOrEmpty(Settings.minecraftForlderPath))
- initPath = Path.GetFullPath(Settings.minecraftForlderPath);
- var folderPickerOpenOptions = new FolderPickerOpenOptions()
- {
- AllowMultiple = false,
- SuggestedStartLocation = await storageProvider.TryGetFolderFromPathAsync(initPath)
- };
- var result = await storageProvider.OpenFolderPickerAsync(folderPickerOpenOptions);
- if (result is null || result.Count != 1 || result.First().Path.AbsolutePath == String.Empty)
- return;
- MinecraftFolderPath = result.First().Path.AbsolutePath;
- });
- }
- public void OpenJavaPathDialog(object window)
- {
- Task.Run(async() =>
- {
- var storageProvider = ((Window)window).StorageProvider;
- var initPath = String.Empty;
- if (!string.IsNullOrEmpty(Settings.javaPath))
- initPath = Path.GetFullPath(Settings.javaPath);
- var folderPickerOpenOptions = new FolderPickerOpenOptions()
- {
- AllowMultiple = false,
- SuggestedStartLocation = await storageProvider.TryGetFolderFromPathAsync(initPath)
- };
- var result = await storageProvider.OpenFolderPickerAsync(folderPickerOpenOptions);
- if (result is null || result.Count != 1 || result.First().Path.AbsolutePath == String.Empty)
- return;
- JavaPath = result.First().Path.AbsolutePath;
- });
- }
- public void OpenLogFile()
- {
- if (Path.Exists(Settings.logFilePath.Path))
- {
- System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo() { FileName = Settings.logFilePath.Path, UseShellExecute = true });
- }
- }
- }
|