Settings.cs 4.4 KB

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