SettingsWindowViewModel.cs 6.2 KB

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