SettingsWindowViewModel.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using ReactiveUI;
  2. using ReactiveUI.Validation.Extensions;
  3. using System;
  4. using System.IO;
  5. using System.Reflection;
  6. using VeloeMinecraftLauncher.MinecraftLauncher;
  7. using Avalonia.Controls;
  8. using System.Threading.Tasks;
  9. using System.Collections.ObjectModel;
  10. using Serilog.Events;
  11. namespace VeloeMinecraftLauncher.ViewModels;
  12. public class SettingsWindowViewModel : ViewModelBase
  13. {
  14. public SettingsWindowViewModel()
  15. {
  16. LauncherVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
  17. var _logger = Settings.logger;
  18. this.ValidationRule(
  19. viewModel => viewModel.MaxRam,
  20. value => { UInt32 result; bool resultb = UInt32.TryParse(value, out result); /*logger.Debug("Validator result: {0} {1}", value, resultb);*/ IsValid = resultb; return resultb; },
  21. "Not a number.");
  22. this.ValidationRule(
  23. viewModel => viewModel.MaxLog,
  24. value => { UInt32 result; bool resultb = UInt32.TryParse(value, out result); /*logger.Debug("Validator result: {0} {1}", value, resultb);*/ IsValid = resultb; return resultb; },
  25. "Not a number.");
  26. }
  27. private string _launcherVersion;
  28. private string _minecraftFolderPath = Settings.minecraftForlderPath;
  29. private string _javaPath = Settings.javaPath;
  30. private string _maxRam = Settings.maxRam.ToString();
  31. private bool _isValid;
  32. private LogEventLevel _fileLogEventLevel = Settings.fileLogEventLevel;
  33. private LogEventLevel _consoleLogEventLevel = Settings.consoleLogEventLevel;
  34. private string _maxLog = Settings.maxLog.ToString();
  35. private ObservableCollection<LogEventLevel> _logEventLevels = new() {LogEventLevel.Debug, LogEventLevel.Information, LogEventLevel.Warning, LogEventLevel.Error };
  36. public bool UseCustomJava
  37. {
  38. get => Settings.useCustomJava;
  39. set => this.RaiseAndSetIfChanged(ref Settings.useCustomJava, value);
  40. }
  41. public bool SetMaxRam
  42. {
  43. get => Settings.setMaxRam;
  44. set => this.RaiseAndSetIfChanged(ref Settings.setMaxRam, value);
  45. }
  46. public bool SetMinecraftFolder
  47. {
  48. get => Settings.setPath;
  49. set => this.RaiseAndSetIfChanged(ref Settings.setPath, value);
  50. }
  51. public bool CheckAssets
  52. {
  53. get => Settings.checkGameAssets;
  54. set => this.RaiseAndSetIfChanged(ref Settings.checkGameAssets, value);
  55. }
  56. public string JavaPath
  57. {
  58. get => _javaPath;
  59. set => this.RaiseAndSetIfChanged(ref _javaPath, value);
  60. }
  61. public string MaxRam
  62. {
  63. get => _maxRam;
  64. set => this.RaiseAndSetIfChanged(ref _maxRam, value);
  65. }
  66. public bool IsValid
  67. {
  68. get => _isValid;
  69. set => this.RaiseAndSetIfChanged(ref _isValid, value);
  70. }
  71. public string MinecraftFolderPath
  72. {
  73. get => _minecraftFolderPath;
  74. set => this.RaiseAndSetIfChanged(ref _minecraftFolderPath, value);
  75. }
  76. public string LauncherVersion
  77. {
  78. get => _launcherVersion;
  79. set => this.RaiseAndSetIfChanged(ref _launcherVersion, value);
  80. }
  81. public bool GameLogToLauncher
  82. {
  83. get => Settings.gameLogToLauncher;
  84. set => this.RaiseAndSetIfChanged(ref Settings.gameLogToLauncher, value);
  85. }
  86. public ObservableCollection<LogEventLevel> LogEventLevels
  87. {
  88. get => _logEventLevels;
  89. set => this.RaiseAndSetIfChanged(ref _logEventLevels, value);
  90. }
  91. public LogEventLevel FileLogEventLevel
  92. {
  93. get =>_fileLogEventLevel;
  94. set => this.RaiseAndSetIfChanged(ref _fileLogEventLevel, value);
  95. }
  96. public LogEventLevel ConsoleLogEventLevel
  97. {
  98. get => _consoleLogEventLevel;
  99. set => this.RaiseAndSetIfChanged(ref _consoleLogEventLevel, value);
  100. }
  101. public bool SetMaxLog
  102. {
  103. get => Settings.setMaxLog;
  104. set => this.RaiseAndSetIfChanged(ref Settings.setMaxLog, value);
  105. }
  106. public string MaxLog
  107. {
  108. get => _maxLog;
  109. set => this.RaiseAndSetIfChanged(ref _maxLog, value);
  110. }
  111. public void SaveSettings()
  112. {
  113. Settings.javaPath = _javaPath;
  114. Settings.minecraftForlderPath = _minecraftFolderPath;
  115. Settings.maxRam = UInt32.Parse(_maxRam);
  116. Settings.maxLog = UInt32.Parse(_maxLog);
  117. Settings.consoleLogEventLevel = _consoleLogEventLevel;
  118. Settings.fileLogEventLevel = _fileLogEventLevel;
  119. Settings.SaveSettings();
  120. }
  121. public void OpenMinecraftPathDialog(Window window)
  122. {
  123. Task.Run(() =>
  124. {
  125. OpenFolderDialog dialog = new OpenFolderDialog();
  126. var initPath = String.Empty;
  127. if (Settings.minecraftForlderPath is not null)
  128. if (Settings.minecraftForlderPath != String.Empty)
  129. initPath = Path.GetFullPath(Settings.minecraftForlderPath);
  130. dialog.Directory = initPath;
  131. var result = dialog.ShowAsync(window).Result;
  132. if (result is null)
  133. return;
  134. if (result == String.Empty)
  135. return;
  136. MinecraftFolderPath = result;
  137. });
  138. }
  139. public void OpenJavaPathDialog(Window window)
  140. {
  141. Task.Run(() =>
  142. {
  143. OpenFileDialog dialog = new OpenFileDialog();
  144. var initPath = String.Empty;
  145. if (Settings.javaPath is not null)
  146. if (Settings.javaPath != String.Empty)
  147. initPath = Path.GetFullPath(Settings.javaPath);
  148. dialog.AllowMultiple = false;
  149. dialog.Directory = initPath;
  150. var result = dialog.ShowAsync(window).Result;
  151. if (result is null)
  152. return;
  153. if (result[0] == String.Empty)
  154. return;
  155. JavaPath = result[0];
  156. });
  157. }
  158. }