123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- using Serilog.Events;
- using System;
- using System.IO;
- using System.Linq;
- using System.Text.Json;
- using VeloeMinecraftLauncher.Utils.Logger;
- namespace VeloeMinecraftLauncher.Utils;
- internal static class Settings
- {
- //public static readonly string JavaPath = "C:\\Program Files\\Microsoft\\jdk-11.0.12.7-hotspot\\bin\\java.exe";
- public static string javaPath = string.Empty;
- public static string minecraftForlderPath = Directory.GetCurrentDirectory() + '/';
- public static UInt32 maxRam = 2048;
- public static bool useCustomJava = false;
- public static bool setMaxRam = false;
- public static bool setPath = true;
- public static bool checkGameAssets = false;
- public static bool gameLogToLauncher = false;
- public static string username = string.Empty;
- public static string lastChosenVersion = string.Empty;
- public static Serilog.ILogger logger;
- public static CaptureFilePathHook logFilePath;
- public static Serilog.ILogger avaloniaLogger;
- public static bool setMaxLog = false;
- public static UInt32 maxLog = 1024;
- public static LogEventLevel consoleLogEventLevel = LogEventLevel.Debug;
- public static LogEventLevel fileLogEventLevel = LogEventLevel.Debug;
- public static void UpdateSettings(SettingsSerializable settings)
- {
- if (settings is null)
- return;
- javaPath = settings.JavaPath;
- minecraftForlderPath = settings.MinecraftForlderPath;
- maxRam = settings.MaxRam;
- useCustomJava = settings.UseCustomJava;
- setMaxRam = settings.SetMaxRam;
- setPath = settings.SetPath;
- checkGameAssets = settings.CheckGameAssets;
- gameLogToLauncher = settings.GameLogToLauncher;
- lastChosenVersion = settings.LastChosenVersion;
- username = settings.Username;
- setMaxLog = settings.SetMaxLog;
- maxLog = settings.MaxLog;
- consoleLogEventLevel = settings.ConsoleLogEventLevel;
- fileLogEventLevel = settings.FileLogEventLevel;
- }
- public static void LoadSettings()
- {
- if (!File.Exists($"settings.json"))
- {
- var file = File.Create($"settings.json");
- file.Close();
- file.Dispose();
- }
- var settings = File.ReadAllText($"settings.json");
- if (settings != String.Empty)
- {
- try
- {
- var settingsSerializable = JsonSerializer.Deserialize<SettingsSerializable>(settings, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
- if (settingsSerializable is not null)
- Settings.UpdateSettings(settingsSerializable);
- }
- catch (JsonException ex)
- {
- //TODO log
- }
- }
- }
- public static void SaveSettings()
- {
- if(minecraftForlderPath == string.Empty)
- minecraftForlderPath = Directory.GetCurrentDirectory();
- if(minecraftForlderPath.Last() is not ('/' or '\\'))
- minecraftForlderPath += "/";
- if (!Directory.Exists(minecraftForlderPath))
- Directory.CreateDirectory(minecraftForlderPath);
- var settings = JsonSerializer.Serialize(new SettingsSerializable());
- File.WriteAllText($"settings.json", settings);
- }
- }
- public class SettingsSerializable
- {
- public string JavaPath {get; set;}
- public string MinecraftForlderPath { get; set;}
- public UInt32 MaxRam { get; set; }
- public bool UseCustomJava { get; set; }
- public bool SetMaxRam { get; set; }
- public bool SetPath { get; set; }
- public bool CheckGameAssets { get; set; }
- public bool GameLogToLauncher { get; set; }
- public string Username { get; set; }
- public string LastChosenVersion { get; set; }
- public bool SetMaxLog { get; set; }
- public UInt32 MaxLog { get; set; }
- public LogEventLevel ConsoleLogEventLevel { get; set; }
- public LogEventLevel FileLogEventLevel { get; set; }
- public SettingsSerializable()
- {
- JavaPath = Settings.javaPath;
- MinecraftForlderPath= Settings.minecraftForlderPath;
- MaxRam= Settings.maxRam;
- UseCustomJava= Settings.useCustomJava;
- SetMaxRam= Settings.setMaxRam;
- SetPath = Settings.setPath;
- CheckGameAssets= Settings.checkGameAssets;
- GameLogToLauncher= Settings.gameLogToLauncher;
- Username = Settings.username;
- LastChosenVersion= Settings.lastChosenVersion;
- SetMaxLog= Settings.setMaxLog;
- MaxLog = Settings.maxLog;
- ConsoleLogEventLevel= Settings.consoleLogEventLevel;
- FileLogEventLevel= Settings.fileLogEventLevel;
- }
- }
|