SettingsWindowViewModel.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using ReactiveUI;
  2. using ReactiveUI.Validation.Extensions;
  3. using System;
  4. using System.IO;
  5. using System.Reflection;
  6. using VeloeMinecraftLauncher.Utils;
  7. using Avalonia.Controls;
  8. using System.Threading.Tasks;
  9. using System.Collections.ObjectModel;
  10. using Serilog.Events;
  11. using System.Linq;
  12. using ReactiveUI.Validation.Components;
  13. using Avalonia.Platform.Storage;
  14. namespace VeloeMinecraftLauncher.ViewModels;
  15. public class SettingsWindowViewModel : ViewModelBase
  16. {
  17. public SettingsWindowViewModel(Serilog.ILogger logger)
  18. {
  19. LauncherVersion = Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "unknown";
  20. _logger = logger;
  21. this.ValidationRule(
  22. viewModel => viewModel.MaxRam,
  23. value => { UInt32 result; bool resultb = UInt32.TryParse(value, out result); return resultb; },
  24. "Not a number.");
  25. var helper = this.ValidationRule(
  26. viewModel => viewModel.JavaPath,
  27. 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; },
  28. "Can't find java executable.");
  29. this.ValidationRule(
  30. viewModel => viewModel.MaxLog,
  31. value => { UInt32 result; bool resultb = UInt32.TryParse(value, out result); return resultb; },
  32. "Not a number.");
  33. }
  34. private string _launcherVersion = string.Empty;
  35. private string _minecraftFolderPath = Settings.minecraftForlderPath;
  36. private string _javaPath = Settings.javaPath;
  37. private string _maxRam = Settings.maxRam.ToString();
  38. private bool _isValid;
  39. private LogEventLevel _fileLogEventLevel = Settings.fileLogEventLevel;
  40. private LogEventLevel _consoleLogEventLevel = Settings.consoleLogEventLevel;
  41. private string _maxLog = Settings.maxLog.ToString();
  42. private ObservableCollection<LogEventLevel> _logEventLevels = new() {LogEventLevel.Debug, LogEventLevel.Information, LogEventLevel.Warning, LogEventLevel.Error };
  43. public bool UseCustomJava
  44. {
  45. get => Settings.useCustomJava;
  46. set
  47. {
  48. this.RaiseAndSetIfChanged(ref Settings.useCustomJava, value);
  49. this.RaisePropertyChanged(nameof(IsValid));
  50. }
  51. }
  52. public bool SetMaxRam
  53. {
  54. get => Settings.setMaxRam;
  55. set => this.RaiseAndSetIfChanged(ref Settings.setMaxRam, value);
  56. }
  57. public bool SetMinecraftFolder
  58. {
  59. get => Settings.setPath;
  60. set
  61. {
  62. this.RaiseAndSetIfChanged(ref Settings.setPath, value);
  63. this.RaisePropertyChanged(nameof(IsValid));
  64. }
  65. }
  66. public bool CheckAssets
  67. {
  68. get => Settings.checkGameAssets;
  69. set => this.RaiseAndSetIfChanged(ref Settings.checkGameAssets, value);
  70. }
  71. public string JavaPath
  72. {
  73. get => _javaPath;
  74. set
  75. {
  76. this.RaiseAndSetIfChanged(ref _javaPath, value);
  77. this.RaisePropertyChanged(nameof(IsValid));
  78. }
  79. }
  80. public string MaxRam
  81. {
  82. get => _maxRam;
  83. set
  84. {
  85. this.RaiseAndSetIfChanged(ref _maxRam, value);
  86. this.RaisePropertyChanged(nameof(IsValid));
  87. }
  88. }
  89. public bool IsValid
  90. {
  91. get
  92. {
  93. _isValid = true;
  94. if (!UseCustomJava)
  95. {
  96. var rules = ValidationContext.Validations.Where(r => !((BasePropertyValidation<SettingsWindowViewModel>)r).Properties.Contains(nameof(JavaPath)));
  97. foreach (var rule in rules)
  98. _isValid &= rule.IsValid;
  99. }
  100. else
  101. _isValid = ValidationContext.GetIsValid();
  102. return _isValid;
  103. }
  104. }
  105. public string MinecraftFolderPath
  106. {
  107. get => _minecraftFolderPath;
  108. set => this.RaiseAndSetIfChanged(ref _minecraftFolderPath, value);
  109. }
  110. public string LauncherVersion
  111. {
  112. get => _launcherVersion;
  113. set => this.RaiseAndSetIfChanged(ref _launcherVersion, value);
  114. }
  115. public bool GameLogToLauncher
  116. {
  117. get => Settings.gameLogToLauncher;
  118. set => this.RaiseAndSetIfChanged(ref Settings.gameLogToLauncher, value);
  119. }
  120. public ObservableCollection<LogEventLevel> LogEventLevels
  121. {
  122. get => _logEventLevels;
  123. set => this.RaiseAndSetIfChanged(ref _logEventLevels, value);
  124. }
  125. public LogEventLevel FileLogEventLevel
  126. {
  127. get =>_fileLogEventLevel;
  128. set => this.RaiseAndSetIfChanged(ref _fileLogEventLevel, value);
  129. }
  130. public LogEventLevel ConsoleLogEventLevel
  131. {
  132. get => _consoleLogEventLevel;
  133. set => this.RaiseAndSetIfChanged(ref _consoleLogEventLevel, value);
  134. }
  135. public bool SetMaxLog
  136. {
  137. get => Settings.setMaxLog;
  138. set => this.RaiseAndSetIfChanged(ref Settings.setMaxLog, value);
  139. }
  140. public string MaxLog
  141. {
  142. get => _maxLog;
  143. set => this.RaiseAndSetIfChanged(ref _maxLog, value);
  144. }
  145. public void SaveSettings()
  146. {
  147. Settings.javaPath = _javaPath;
  148. Settings.minecraftForlderPath = _minecraftFolderPath;
  149. Settings.maxRam = UInt32.Parse(_maxRam);
  150. Settings.maxLog = UInt32.Parse(_maxLog);
  151. Settings.consoleLogEventLevel = _consoleLogEventLevel;
  152. Settings.fileLogEventLevel = _fileLogEventLevel;
  153. Settings.SaveSettings();
  154. }
  155. public void OpenMinecraftPathDialog(object window)
  156. {
  157. Task.Run(async() =>
  158. {
  159. var storageProvider = ((Window)window).StorageProvider;
  160. var initPath = String.Empty;
  161. if (!string.IsNullOrEmpty(Settings.minecraftForlderPath))
  162. initPath = Path.GetFullPath(Settings.minecraftForlderPath);
  163. var folderPickerOpenOptions = new FolderPickerOpenOptions()
  164. {
  165. AllowMultiple = false,
  166. SuggestedStartLocation = await storageProvider.TryGetFolderFromPathAsync(initPath)
  167. };
  168. var result = await storageProvider.OpenFolderPickerAsync(folderPickerOpenOptions);
  169. if (result is null || result.Count != 1 || result.First().Path.AbsolutePath == String.Empty)
  170. return;
  171. MinecraftFolderPath = result.First().Path.AbsolutePath;
  172. });
  173. }
  174. public void OpenJavaPathDialog(object window)
  175. {
  176. Task.Run(async() =>
  177. {
  178. var storageProvider = ((Window)window).StorageProvider;
  179. var initPath = String.Empty;
  180. if (!string.IsNullOrEmpty(Settings.javaPath))
  181. initPath = Path.GetFullPath(Settings.javaPath);
  182. var folderPickerOpenOptions = new FolderPickerOpenOptions()
  183. {
  184. AllowMultiple = false,
  185. SuggestedStartLocation = await storageProvider.TryGetFolderFromPathAsync(initPath)
  186. };
  187. var result = await storageProvider.OpenFolderPickerAsync(folderPickerOpenOptions);
  188. if (result is null || result.Count != 1 || result.First().Path.AbsolutePath == String.Empty)
  189. return;
  190. JavaPath = result.First().Path.AbsolutePath;
  191. });
  192. }
  193. public void OpenLogFile()
  194. {
  195. if (Path.Exists(Settings.logFilePath?.Path))
  196. {
  197. System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo() { FileName = Settings.logFilePath.Path, UseShellExecute = true });
  198. }
  199. }
  200. }