SettingsWindowViewModel.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. using Avalonia.Media;
  12. using Avalonia;
  13. using Egorozh.ColorPicker;
  14. using VeloeMinecraftLauncher.Views;
  15. namespace VeloeMinecraftLauncher.ViewModels
  16. {
  17. public class SettingsWindowViewModel : ViewModelBase
  18. {
  19. public SettingsWindowViewModel()
  20. {
  21. LauncherVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
  22. var logger = Settings.logger;
  23. this.ValidationRule(
  24. viewModel => viewModel.MaxRam,
  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. this.ValidationRule(
  28. viewModel => viewModel.MaxLog,
  29. value => { UInt32 result; bool resultb = UInt32.TryParse(value, out result); /*logger.Debug("Validator result: {0} {1}", value, resultb);*/ IsValid = resultb; return resultb; },
  30. "Not a number.");
  31. }
  32. private string launcherVersion;
  33. private string minecraftFolderPath = Settings.MinecraftForlderPath;
  34. private string javaPath = Settings.JavaPath;
  35. private string maxRam = Settings.MaxRam.ToString();
  36. private bool isValid;
  37. private LogEventLevel fileLogEventLevel = Settings.fileLogEventLevel;
  38. private LogEventLevel consoleLogEventLevel = Settings.consoleLogEventLevel;
  39. private string maxLog = Settings.MaxLog.ToString();
  40. private ObservableCollection<LogEventLevel> logEventLevels = new() {LogEventLevel.Debug, LogEventLevel.Information, LogEventLevel.Warning, LogEventLevel.Error };
  41. private ColorPickerControl ColorPicker = null;
  42. public bool UseCustomJava
  43. {
  44. get => Settings.useCustomJava;
  45. set
  46. {
  47. this.RaiseAndSetIfChanged(ref Settings.useCustomJava, value);
  48. }
  49. }
  50. public bool SetMaxRam
  51. {
  52. get => Settings.setMaxRam;
  53. set
  54. {
  55. this.RaiseAndSetIfChanged(ref Settings.setMaxRam, value);
  56. }
  57. }
  58. public bool SetMinecraftFolder
  59. {
  60. get => Settings.setPath;
  61. set
  62. {
  63. this.RaiseAndSetIfChanged(ref Settings.setPath, value);
  64. }
  65. }
  66. public bool CheckAssets
  67. {
  68. get => Settings.checkGameAssets;
  69. set
  70. {
  71. this.RaiseAndSetIfChanged(ref Settings.checkGameAssets, value);
  72. }
  73. }
  74. public string JavaPath
  75. {
  76. get => javaPath;
  77. set
  78. {
  79. this.RaiseAndSetIfChanged(ref javaPath, value);
  80. }
  81. }
  82. public string MaxRam
  83. {
  84. get => maxRam.ToString();
  85. set
  86. {
  87. this.RaiseAndSetIfChanged(ref maxRam, value);
  88. }
  89. }
  90. public bool IsValid
  91. {
  92. get => isValid;
  93. set => this.RaiseAndSetIfChanged(ref isValid, value);
  94. }
  95. public string MinecraftFolderPath
  96. {
  97. get => minecraftFolderPath;
  98. set
  99. {
  100. this.RaiseAndSetIfChanged(ref minecraftFolderPath, value);
  101. }
  102. }
  103. public string LauncherVersion
  104. {
  105. get => launcherVersion;
  106. set
  107. {
  108. this.RaiseAndSetIfChanged(ref launcherVersion, value);
  109. }
  110. }
  111. public bool GameLogToLauncher
  112. {
  113. get => Settings.gameLogToLauncher;
  114. set
  115. {
  116. this.RaiseAndSetIfChanged(ref Settings.gameLogToLauncher, value);
  117. }
  118. }
  119. public ObservableCollection<LogEventLevel> LogEventLevels
  120. {
  121. get => logEventLevels;
  122. set => this.RaiseAndSetIfChanged(ref logEventLevels, value);
  123. }
  124. public LogEventLevel FileLogEventLevel
  125. {
  126. get =>fileLogEventLevel;
  127. set => this.RaiseAndSetIfChanged(ref fileLogEventLevel, value);
  128. }
  129. public LogEventLevel ConsoleLogEventLevel
  130. {
  131. get => consoleLogEventLevel;
  132. set => this.RaiseAndSetIfChanged(ref consoleLogEventLevel, value);
  133. }
  134. public bool SetMaxLog
  135. {
  136. get => Settings.setMaxLog;
  137. set => this.RaiseAndSetIfChanged(ref Settings.setMaxLog, value);
  138. }
  139. public string MaxLog
  140. {
  141. get => maxLog;
  142. set => this.RaiseAndSetIfChanged(ref maxLog, value);
  143. }
  144. public void SaveSettings()
  145. {
  146. Settings.JavaPath = javaPath;
  147. Settings.MinecraftForlderPath = minecraftFolderPath;
  148. Settings.MaxRam = UInt32.Parse(maxRam);
  149. Settings.MaxLog = UInt32.Parse(maxLog);
  150. Settings.consoleLogEventLevel = consoleLogEventLevel;
  151. Settings.fileLogEventLevel = fileLogEventLevel;
  152. Settings.SaveSettings();
  153. }
  154. public void OpenMinecraftPathDialog(Window window)
  155. {
  156. Task.Run(() =>
  157. {
  158. OpenFolderDialog dialog = new OpenFolderDialog();
  159. var initPath = String.Empty;
  160. if (Settings.MinecraftForlderPath is not null)
  161. if (Settings.MinecraftForlderPath != String.Empty)
  162. initPath = Path.GetFullPath(Settings.MinecraftForlderPath);
  163. dialog.Directory = initPath;
  164. var result = dialog.ShowAsync(window).Result;
  165. if (result is null)
  166. return;
  167. if (result == String.Empty)
  168. return;
  169. MinecraftFolderPath = result;
  170. });
  171. }
  172. public void OpenJavaPathDialog(Window window)
  173. {
  174. Task.Run(() =>
  175. {
  176. OpenFolderDialog dialog = new OpenFolderDialog();
  177. var initPath = String.Empty;
  178. if (Settings.JavaPath is not null)
  179. if (Settings.JavaPath != String.Empty)
  180. initPath = Path.GetFullPath(Settings.JavaPath);
  181. dialog.Directory = initPath;
  182. var result = dialog.ShowAsync(window).Result;
  183. if (result is null)
  184. return;
  185. if (result == String.Empty)
  186. return;
  187. JavaPath = result;
  188. });
  189. }
  190. public void SetColor(Window window)
  191. {
  192. if (ColorPicker == null)
  193. ColorPicker = window.FindControl<ColorPickerControl>("ColorPicker");
  194. InterfaceColor = Color.FromRgb(ColorPicker.Color.R, ColorPicker.Color.G, ColorPicker.Color.B);
  195. MaterialOpacity = float.Parse(ColorPicker.Color.A.ToString())/byte.MaxValue;
  196. if (ColorPicker.Color.R >= 200 && ColorPicker.Color.G >= 200 && ColorPicker.Color.B >= 200 || MaterialOpacity <= 0.1)
  197. {
  198. Application.Current.Styles[0] = App.FluentLight;
  199. Application.Current.Styles[2] = App.FluentColorPickerThemeLight;
  200. Application.Current.Styles[1] = App.DataGridFluent;
  201. }
  202. else
  203. {
  204. Application.Current.Styles[0] = App.FluentDark;
  205. Application.Current.Styles[2] = App.FluentColorPickerThemeDark;
  206. Application.Current.Styles[1] = App.DataGridFluent;
  207. }
  208. ((ViewModelBase)((MainWindow) window.Owner).DataContext).RaisePropertyChanged(nameof(InterfaceColor));
  209. ((ViewModelBase)((MainWindow) window.Owner).DataContext).RaisePropertyChanged(nameof(MaterialOpacity));
  210. }
  211. }
  212. }