Skip to content

Commit e6f9ea9

Browse files
authored
Feature settings additions (#6)
* added path to modpack in UserSettings.cs. Implemented saving and retrieving settings * Added a FileUser service to read from and write to Json Files. Registerd FileUser with DI. * renamed textbox in settingview for the modpack path. updated the textbox with value from the settings.
1 parent 03bd3fb commit e6f9ea9

File tree

7 files changed

+182
-102
lines changed

7 files changed

+182
-102
lines changed

src/HyperMC/Program.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Hypermc.Services;
12
using Hypermc.Settings;
23
using Hypermc.UI.Views;
34
using Microsoft.Extensions.DependencyInjection;
@@ -16,10 +17,10 @@ static class Program
1617
/// The main entry point for the application.
1718
/// </summary>
1819
[STAThread]
19-
static void Main()
20+
static async Task Main()
2021
{
2122
Provider = ConfigureService();
22-
Provider.GetRequiredService<IUserSettings>().Initialize();
23+
await Provider.GetRequiredService<IUserSettings>().Initialize();
2324

2425
Application.SetHighDpiMode(HighDpiMode.SystemAware);
2526
Application.EnableVisualStyles();
@@ -34,7 +35,8 @@ private static IServiceProvider ConfigureService()
3435
services.AddSingleton<HyperMcView>()
3536
.AddSingleton<IUserSettings, UserSettings>();
3637

37-
services.AddTransient<SettingView>();
38+
services.AddTransient<SettingView>()
39+
.AddTransient<IFileUesr, FileUser>();
3840

3941
services.AddForgeClient();
4042

src/HyperMC/Services/FileUser.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Text.Json;
7+
using System.Threading.Tasks;
8+
9+
namespace Hypermc.Services
10+
{
11+
public class FileUser : IFileUesr
12+
{
13+
public async Task<T> ReadFile<T>(string file)
14+
{
15+
using var stream = new FileStream(file, FileMode.Open, FileAccess.Read);
16+
var settings = await JsonSerializer.DeserializeAsync<T>(stream);
17+
return settings;
18+
}
19+
20+
public async Task WriteToFile<T>(T data, string file)
21+
{
22+
string settingsToSave = JsonSerializer.Serialize(data);
23+
await File.WriteAllTextAsync(file, settingsToSave);
24+
}
25+
}
26+
}

src/HyperMC/Services/IFileUesr.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Hypermc.Services
8+
{
9+
public interface IFileUesr
10+
{
11+
Task WriteToFile<T>(T data, string file);
12+
Task<T> ReadFile<T>(string file);
13+
}
14+
}

src/HyperMC/Settings/IUserSettings.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ namespace Hypermc.Settings
99
public interface IUserSettings
1010
{
1111
string MinecraftPath { get; set; }
12+
string ModPacksPath { get; set; }
1213

13-
void Initialize();
14+
Task Initialize();
1415
}
1516
}
Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,56 @@
1-
using System;
1+
using Hypermc.Services;
2+
using System;
23
using System.Collections.Generic;
34
using System.IO;
45
using System.Linq;
56
using System.Text;
7+
using System.Text.Json;
68
using System.Threading.Tasks;
79

810
namespace Hypermc.Settings
911
{
1012
public class UserSettings : IUserSettings
1113
{
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;
1317

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()
1633
{
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);
1854
}
1955
}
2056
}

src/HyperMC/UI/Views/SettingView.Designer.cs

Lines changed: 93 additions & 93 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/HyperMC/UI/Views/SettingView.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public SettingView(IUserSettings settings)
2525
InitializeComponent();
2626
_settings = settings;
2727
game_path.Text = settings.MinecraftPath;
28+
modPackPath.Text = settings.ModPacksPath;
2829
}
2930

3031
public event IView.ViewMessageCallback OnMessage;

0 commit comments

Comments
 (0)