|
1 | | -using System; |
| 1 | +using Hypermc.Services; |
| 2 | +using System; |
2 | 3 | using System.Collections.Generic; |
3 | 4 | using System.IO; |
4 | 5 | using System.Linq; |
5 | 6 | using System.Text; |
| 7 | +using System.Text.Json; |
6 | 8 | using System.Threading.Tasks; |
7 | 9 |
|
8 | 10 | namespace Hypermc.Settings |
9 | 11 | { |
10 | 12 | public class UserSettings : IUserSettings |
11 | 13 | { |
12 | | - public string MinecraftPath { get; set; } = $@"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\.minecraft"; |
| 14 | + private readonly string _appPath; |
| 15 | + private readonly string _settingsFile; |
| 16 | + private readonly IFileUesr _fileUesr; |
13 | 17 |
|
14 | | - // TODO: intialize settings by getting the users saved settings for this application |
15 | | - public void Initialize() |
| 18 | + public string MinecraftPath { get; set; } |
| 19 | + public string ModPacksPath { get; set; } |
| 20 | + |
| 21 | + public UserSettings(IFileUesr fileUesr) |
| 22 | + { |
| 23 | + // TODO: possibly move the file names to the appsettings. |
| 24 | + _appPath = $@"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\.hypermc"; |
| 25 | + _settingsFile = $@"{_appPath}\settings.json"; |
| 26 | + _fileUesr = fileUesr; |
| 27 | + |
| 28 | + MinecraftPath = $@"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\.minecraft"; |
| 29 | + ModPacksPath = $@"{MinecraftPath}\ModPacks"; |
| 30 | + } |
| 31 | + |
| 32 | + public async Task Initialize() |
16 | 33 | { |
17 | | - |
| 34 | + if (!Directory.Exists(_appPath)) |
| 35 | + { |
| 36 | + Directory.CreateDirectory(_appPath); |
| 37 | + } |
| 38 | + |
| 39 | + if (File.Exists(_settingsFile)) |
| 40 | + { |
| 41 | + var settings = await _fileUesr.ReadFile<UserSettings>(_settingsFile); |
| 42 | + |
| 43 | + MinecraftPath = settings.MinecraftPath; |
| 44 | + ModPacksPath = settings.ModPacksPath; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public async Task UpdateSettings(string mcPath, string modPath) |
| 49 | + { |
| 50 | + MinecraftPath = mcPath; |
| 51 | + ModPacksPath = modPath; |
| 52 | + |
| 53 | + await _fileUesr.WriteToFile(this, _settingsFile); |
18 | 54 | } |
19 | 55 | } |
20 | 56 | } |
0 commit comments