Skip to content

Commit dd0fe26

Browse files
Merge pull request #30 from mstew20/Windows
Saving modpack data
2 parents 1349c02 + 7602d29 commit dd0fe26

File tree

12 files changed

+252
-114
lines changed

12 files changed

+252
-114
lines changed

src/HyperMC.CurseForge/ForgeClient.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,17 @@ public async Task<MinecraftVersion[]> GetMinecraftVersions()
6464
{
6565
return await _forgeClient.Minecraft.RetrieveGameVersions();
6666
}
67+
68+
public async Task<Stream> GetImageFromURL(string url)
69+
{
70+
var result = await _forgeClient.HttpClient.GetAsync(url);
71+
72+
if (result.IsSuccessStatusCode)
73+
{
74+
return await result.Content.ReadAsStreamAsync();
75+
}
76+
77+
return null;
78+
}
6779
}
6880
}

src/HyperMC.CurseForge/IForgeClient.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
using ForgedCurse.Enumeration;
22
using ForgedCurse.Json;
3+
using System.IO;
34
using System.Threading.Tasks;
45

56
namespace HyperMC.CurseForge
67
{
78
public interface IForgeClient
89
{
910
Task DownloadMod(Addon mod, string filePath);
11+
Task<Stream> GetImageFromURL(string url);
1012
Task<MinecraftVersion[]> GetMinecraftVersions();
1113
Task<Addon[]> SearchForMod(string modName = "", string version = "", int amount = 10, int offset = 0, MinecraftCategory category = MinecraftCategory.All, MinecraftSection seciton = MinecraftSection.Mod, AddonSorting sorting = AddonSorting.Featured);
1214
}

src/HyperMC/Data/ModpackData.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@ namespace Hypermc.Data
1010
public class ModpackData
1111
{
1212
public string Name { get; set; }
13-
public Image Thumbnail { get; }
13+
public string Thumbnail { get; }
1414
public string Path { get; }
1515

16-
public ModpackData(string name, Image thumbnail, string path)
16+
public ModpackData()
17+
{
18+
19+
}
20+
21+
public ModpackData(string name, string thumbnail, string path)
1722
{
1823
Name = name;
1924
Thumbnail = thumbnail;

src/HyperMC/HyperMcView.cs

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using System.Threading.Tasks;
1212
using System.Windows.Forms;
1313
using Hypermc.Data;
14+
using Hypermc.Services;
15+
using Hypermc.Settings;
1416
using Hypermc.UI.Dialogs;
1517
using Hypermc.UI.UserControls;
1618
using Hypermc.UI.Views;
@@ -25,20 +27,32 @@ public partial class HyperMcView : Form, IViewHost
2527

2628
private readonly IForgeClient _forgeClient;
2729
private readonly IServiceProvider _provider;
30+
private readonly IFileManager _fileManager;
31+
private readonly IUserSettings _settings;
2832

29-
public HyperMcView(IForgeClient forgeClient, IServiceProvider provider)
33+
public HyperMcView(IForgeClient forgeClient, IServiceProvider provider, IFileManager fileManager, IUserSettings settings)
3034
{
3135
InitializeComponent();
3236

3337
_forgeClient = forgeClient;
3438
_provider = provider;
39+
_fileManager = fileManager;
40+
_settings = settings;
3541
_modpacks = new();
3642
_modpacks.CollectionChanged += ModpacksUpdated;
3743
}
3844

39-
private void HyperMcView_Load(object sender, EventArgs e)
45+
private async void HyperMcView_Load(object sender, EventArgs e)
4046
{
4147
SetView(new ControlView(pnl_MainArea));
48+
var mods = await _fileManager.ReadFile<ModpackData[]>($@"{_settings.ModPacksPath}\packs.json");
49+
if (mods != null)
50+
{
51+
foreach (var mod in mods)
52+
{
53+
_modpacks.Add(mod);
54+
}
55+
}
4256
}
4357

4458
#region Default View
@@ -61,7 +75,7 @@ private void Hbtn_CreateModpack_Click(object sender, EventArgs e)
6175

6276
private ObservableCollection<ModpackData> _modpacks;
6377

64-
private void ModpacksUpdated(object? sender, NotifyCollectionChangedEventArgs e)
78+
private async void ModpacksUpdated(object? sender, NotifyCollectionChangedEventArgs e)
6579
{
6680
switch (e.Action)
6781
{
@@ -112,13 +126,25 @@ private void ModpacksUpdated(object? sender, NotifyCollectionChangedEventArgs e)
112126
}
113127

114128
SortModpacks();
129+
await _fileManager.WriteToFile(_modpacks.ToArray(), $@"{_settings.ModPacksPath}\packs.json");
115130
}
116131

117-
private static ModpackBox CreateModpackBox(ModpackData data)
132+
private ModpackBox CreateModpackBox(ModpackData data)
118133
{
134+
Image thumbnail;
135+
if (string.IsNullOrWhiteSpace(data.Thumbnail))
136+
{
137+
thumbnail = Properties.Resources.DefaultModpackImage;
138+
}
139+
else
140+
{
141+
// May need to be changed depending on how the image will be set
142+
thumbnail = Image.FromStream(_forgeClient.GetImageFromURL(data.Thumbnail).GetAwaiter().GetResult());
143+
}
144+
119145
return new()
120146
{
121-
Thumbnail = data.Thumbnail,
147+
Thumbnail = thumbnail,
122148
SizeMode = PictureBoxSizeMode.StretchImage,
123149
Name = data.Name,
124150
Tag = data.Path
@@ -202,9 +228,12 @@ public void SetView(IView view, object? data = null)
202228
{
203229
if (_view is not null)
204230
{
205-
_view.HideView(Utils.PopChildControls(pnl_MainArea));
206-
_view.OnMessage -= View_OnMessage;
207-
_viewPrev = _view;
231+
if (_view.GetType() != view.GetType())
232+
{
233+
_view.HideView(Utils.PopChildControls(pnl_MainArea));
234+
_view.OnMessage -= View_OnMessage;
235+
_viewPrev = _view;
236+
}
208237
}
209238

210239
view.OnMessage += View_OnMessage;

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<IFileManager, FileManager>();
3840

3941
services.AddForgeClient();
4042

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 FileManager : IFileManager
12+
{
13+
public async Task<T> ReadFile<T>(string file)
14+
{
15+
if (File.Exists(file))
16+
{
17+
using var stream = new FileStream(file, FileMode.Open, FileAccess.Read);
18+
var settings = await JsonSerializer.DeserializeAsync<T>(stream);
19+
return settings;
20+
}
21+
22+
return default(T);
23+
}
24+
25+
public async Task WriteToFile<T>(T data, string file)
26+
{
27+
string settingsToSave = JsonSerializer.Serialize(data);
28+
await File.WriteAllTextAsync(file, settingsToSave);
29+
}
30+
}
31+
}
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 IFileManager
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: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,61 @@
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 IFileManager _fileManager;
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(IFileManager fileManager)
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+
_fileManager = fileManager;
27+
28+
MinecraftPath = $@"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\.minecraft";
29+
ModPacksPath = $@"{MinecraftPath}\ModPacks";
30+
}
31+
32+
public async Task Initialize()
33+
{
34+
if (!Directory.Exists(_appPath))
35+
{
36+
Directory.CreateDirectory(_appPath);
37+
}
38+
39+
if (!Directory.Exists(ModPacksPath))
40+
{
41+
Directory.CreateDirectory(ModPacksPath);
42+
}
43+
44+
var settings = await _fileManager.ReadFile<UserSettings>(_settingsFile);
45+
46+
if (settings != null)
47+
{
48+
MinecraftPath = settings.MinecraftPath;
49+
ModPacksPath = settings.ModPacksPath;
50+
}
51+
}
52+
53+
public async Task UpdateSettings(string mcPath, string modPath)
1654
{
17-
55+
MinecraftPath = mcPath;
56+
ModPacksPath = modPath;
57+
58+
await _fileManager.WriteToFile(this, _settingsFile);
1859
}
1960
}
2061
}

src/HyperMC/UI/Dialogs/CreateModpackDialog.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ private void Hbtn_Ok_Click(object sender, EventArgs e)
6464
return;
6565
}
6666

67-
Data = new ModpackData(txb_Name.Text, Properties.Resources.DefaultModpackImage, null);
68-
DataForge = new ModpackData(cmbx_FmlVersion.Text, Properties.Resources.DefaultModpackImage, null);
67+
Data = new ModpackData(txb_Name.Text, "", null);
68+
DataForge = new ModpackData(cmbx_FmlVersion.Text, "", null);
6969
DialogResult = DialogResult.OK;
7070
Close();
7171
}

0 commit comments

Comments
 (0)