Settings.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using Serilog.Events;
  2. using System;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text.Json;
  6. using VeloeMinecraftLauncher.Utils.Logger;
  7. namespace VeloeMinecraftLauncher.Utils;
  8. internal static class Settings
  9. {
  10. //public static readonly string JavaPath = "C:\\Program Files\\Microsoft\\jdk-11.0.12.7-hotspot\\bin\\java.exe";
  11. public static string javaPath = string.Empty;
  12. public static string minecraftForlderPath = Directory.GetCurrentDirectory() + '/';
  13. public static UInt32 maxRam = 2048;
  14. public static bool useCustomJava = false;
  15. public static bool setMaxRam = false;
  16. public static bool setPath = true;
  17. public static bool checkGameAssets = false;
  18. public static bool gameLogToLauncher = false;
  19. public static string username = string.Empty;
  20. public static string lastChosenVersion = string.Empty;
  21. public static Serilog.ILogger logger;
  22. public static CaptureFilePathHook logFilePath;
  23. public static Serilog.ILogger avaloniaLogger;
  24. public static bool setMaxLog = false;
  25. public static UInt32 maxLog = 1024;
  26. public static LogEventLevel consoleLogEventLevel = LogEventLevel.Debug;
  27. public static LogEventLevel fileLogEventLevel = LogEventLevel.Debug;
  28. public static void UpdateSettings(SettingsSerializable settings)
  29. {
  30. if (settings is null)
  31. return;
  32. javaPath = settings.JavaPath;
  33. minecraftForlderPath = settings.MinecraftForlderPath;
  34. maxRam = settings.MaxRam;
  35. useCustomJava = settings.UseCustomJava;
  36. setMaxRam = settings.SetMaxRam;
  37. setPath = settings.SetPath;
  38. checkGameAssets = settings.CheckGameAssets;
  39. gameLogToLauncher = settings.GameLogToLauncher;
  40. lastChosenVersion = settings.LastChosenVersion;
  41. username = settings.Username;
  42. setMaxLog = settings.SetMaxLog;
  43. maxLog = settings.MaxLog;
  44. consoleLogEventLevel = settings.ConsoleLogEventLevel;
  45. fileLogEventLevel = settings.FileLogEventLevel;
  46. }
  47. public static void LoadSettings()
  48. {
  49. if (!File.Exists($"settings.json"))
  50. {
  51. var file = File.Create($"settings.json");
  52. file.Close();
  53. file.Dispose();
  54. }
  55. var settings = File.ReadAllText($"settings.json");
  56. if (settings != String.Empty)
  57. {
  58. try
  59. {
  60. var settingsSerializable = JsonSerializer.Deserialize<SettingsSerializable>(settings, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
  61. if (settingsSerializable is not null)
  62. Settings.UpdateSettings(settingsSerializable);
  63. }
  64. catch (JsonException ex)
  65. {
  66. //TODO log
  67. }
  68. }
  69. }
  70. public static void SaveSettings()
  71. {
  72. if(minecraftForlderPath == string.Empty)
  73. minecraftForlderPath = Directory.GetCurrentDirectory();
  74. if(minecraftForlderPath.Last() is not ('/' or '\\'))
  75. minecraftForlderPath += "/";
  76. if (!Directory.Exists(minecraftForlderPath))
  77. Directory.CreateDirectory(minecraftForlderPath);
  78. var settings = JsonSerializer.Serialize(new SettingsSerializable());
  79. File.WriteAllText($"settings.json", settings);
  80. }
  81. }
  82. public class SettingsSerializable
  83. {
  84. public string JavaPath {get; set;}
  85. public string MinecraftForlderPath { get; set;}
  86. public UInt32 MaxRam { get; set; }
  87. public bool UseCustomJava { get; set; }
  88. public bool SetMaxRam { get; set; }
  89. public bool SetPath { get; set; }
  90. public bool CheckGameAssets { get; set; }
  91. public bool GameLogToLauncher { get; set; }
  92. public string Username { get; set; }
  93. public string LastChosenVersion { get; set; }
  94. public bool SetMaxLog { get; set; }
  95. public UInt32 MaxLog { get; set; }
  96. public LogEventLevel ConsoleLogEventLevel { get; set; }
  97. public LogEventLevel FileLogEventLevel { get; set; }
  98. public SettingsSerializable()
  99. {
  100. JavaPath = Settings.javaPath;
  101. MinecraftForlderPath= Settings.minecraftForlderPath;
  102. MaxRam= Settings.maxRam;
  103. UseCustomJava= Settings.useCustomJava;
  104. SetMaxRam= Settings.setMaxRam;
  105. SetPath = Settings.setPath;
  106. CheckGameAssets= Settings.checkGameAssets;
  107. GameLogToLauncher= Settings.gameLogToLauncher;
  108. Username = Settings.username;
  109. LastChosenVersion= Settings.lastChosenVersion;
  110. SetMaxLog= Settings.setMaxLog;
  111. MaxLog = Settings.maxLog;
  112. ConsoleLogEventLevel= Settings.consoleLogEventLevel;
  113. FileLogEventLevel= Settings.fileLogEventLevel;
  114. }
  115. }