SettingsWindowViewModel.cs 4.6 KB

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