-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigManager.cs
More file actions
184 lines (165 loc) · 6.02 KB
/
ConfigManager.cs
File metadata and controls
184 lines (165 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
namespace JustLauncher;
public static class ConfigManager
{
private static readonly string LauncherDir = PlatformManager.GetLauncherDirectory();
private static readonly string AccountsPath = Path.Combine(LauncherDir, "launcher_accounts.json");
private static readonly string InstallationsPath = Path.Combine(LauncherDir, "launcher_installations.json");
private static readonly string SettingsPath = Path.Combine(LauncherDir, "launcher_settings.json");
static ConfigManager()
{
if (!Directory.Exists(LauncherDir)) Directory.CreateDirectory(LauncherDir);
MigrateOldConfigFiles();
}
private static void MigrateOldConfigFiles()
{
string oldDir = PlatformManager.GetMinecraftDirectory();
var filesToMigrate = new[]
{
"launcher_accounts.json",
"launcher_installations.json",
"launcher_settings.json"
};
foreach (var fileName in filesToMigrate)
{
string oldPath = Path.Combine(oldDir, fileName);
string newPath = Path.Combine(LauncherDir, fileName);
if (File.Exists(oldPath) && !File.Exists(newPath))
{
try
{
File.Move(oldPath, newPath);
}
catch
{
}
}
}
}
public static async Task<AccountsConfig> LoadAccountsAsync()
{
if (!File.Exists(AccountsPath)) return new AccountsConfig();
try
{
string json = await File.ReadAllTextAsync(AccountsPath);
return JsonSerializer.Deserialize<AccountsConfig>(json) ?? new AccountsConfig();
}
catch (Exception ex)
{
ConsoleService.Instance.Log($"[Config] Error loading accounts: {ex.Message}");
return new AccountsConfig();
}
}
public static async Task SaveAccountsAsync(AccountsConfig config)
{
try
{
string json = JsonSerializer.Serialize(config, new JsonSerializerOptions { WriteIndented = true });
await File.WriteAllTextAsync(AccountsPath, json);
}
catch (Exception ex)
{
ConsoleService.Instance.Log($"[Config] Error saving accounts: {ex.Message}");
throw;
}
}
public static async Task<InstallationsConfig> LoadInstallationsAsync()
{
if (!File.Exists(InstallationsPath)) return new InstallationsConfig();
try
{
string json = await File.ReadAllTextAsync(InstallationsPath);
return JsonSerializer.Deserialize<InstallationsConfig>(json) ?? new InstallationsConfig();
}
catch (Exception ex)
{
ConsoleService.Instance.Log($"[Config] Error loading installations: {ex.Message}");
return new InstallationsConfig();
}
}
public static async Task SaveInstallationsAsync(InstallationsConfig config)
{
try
{
string json = JsonSerializer.Serialize(config, new JsonSerializerOptions { WriteIndented = true });
await File.WriteAllTextAsync(InstallationsPath, json);
}
catch (Exception ex)
{
ConsoleService.Instance.Log($"[Config] Error saving installations: {ex.Message}");
throw;
}
}
public static async Task<LauncherSettings> LoadSettingsAsync()
{
if (!File.Exists(SettingsPath)) return new LauncherSettings();
try
{
string json = await File.ReadAllTextAsync(SettingsPath);
return JsonSerializer.Deserialize<LauncherSettings>(json) ?? new LauncherSettings();
}
catch (Exception ex)
{
ConsoleService.Instance.Log($"[Config] Error loading settings: {ex.Message}");
return new LauncherSettings();
}
}
public static async Task SaveSettingsAsync(LauncherSettings settings)
{
try
{
string json = JsonSerializer.Serialize(settings, new JsonSerializerOptions { WriteIndented = true });
await File.WriteAllTextAsync(SettingsPath, json);
}
catch (Exception ex)
{
ConsoleService.Instance.Log($"[Config] Error saving settings: {ex.Message}");
throw;
}
}
public static LauncherSettings LoadSettings()
{
if (!File.Exists(SettingsPath)) return new LauncherSettings();
try
{
string json = File.ReadAllText(SettingsPath);
return JsonSerializer.Deserialize<LauncherSettings>(json) ?? new LauncherSettings();
}
catch (Exception ex)
{
ConsoleService.Instance.Log($"[Config] Error loading settings: {ex.Message}");
return new LauncherSettings();
}
}
public static void SaveSettings(LauncherSettings settings)
{
try
{
string json = JsonSerializer.Serialize(settings, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(SettingsPath, json);
}
catch (Exception ex)
{
ConsoleService.Instance.Log($"[Config] Error saving settings: {ex.Message}");
}
}
}
public class LauncherSettings
{
public string Language { get; set; } = "en";
public string JavaPath { get; set; } = string.Empty;
public double MemoryAllocationGb { get; set; } = 2.0;
public bool CloseOnLaunch { get; set; } = false;
public bool UseSeparateGameDir { get; set; } = true;
public string Theme { get; set; } = "System";
public bool DarkMode { get; set; } = true;
public bool IsSakiEnabled { get; set; } = false;
public string SakiSkin { get; set; } = "Steve";
public bool CheckForUpdatesOnStartup { get; set; } = true;
public DateTime LastUpdateCheck { get; set; } = DateTime.MinValue;
public string SkippedVersion { get; set; } = string.Empty;
public bool IsFirstRun { get; set; } = true;
}