Skip to content

Commit

Permalink
修复下载源修改、修改 Java 选择算法
Browse files Browse the repository at this point in the history
  • Loading branch information
natsurainko committed Sep 17, 2024
1 parent 56020c5 commit 92e4fa3
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 32 deletions.
1 change: 0 additions & 1 deletion Natsurainko.FluentLauncher/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using System.Diagnostics;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;

namespace Natsurainko.FluentLauncher;

Expand Down
2 changes: 1 addition & 1 deletion Natsurainko.FluentLauncher/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<Identity
Name="26553XcubeStudio.Natsurianko.FluentLauncher"
Publisher="CN=053EFB0E-6705-4A11-94B9-980C4C9E0047"
Version="2.3.0.0" />
Version="2.3.1.0" />

<Properties>
<DisplayName>Natsurianko.FluentLauncher</DisplayName>
Expand Down
18 changes: 15 additions & 3 deletions Natsurainko.FluentLauncher/Services/Launch/LaunchService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Nrk.FluentCore.Experimental.Launch;
using Nrk.FluentCore.GameManagement;
using Nrk.FluentCore.GameManagement.Dependencies;
using Nrk.FluentCore.GameManagement.Installer;
using Nrk.FluentCore.GameManagement.Instances;
using Nrk.FluentCore.Launch;
using Nrk.FluentCore.Utils;
Expand Down Expand Up @@ -154,11 +155,22 @@ async Task<PreCheckData> PreCheckLaunchNeeds(
if (_settingsService.EnableAutoJava)
{
var targetJavaVersion = instance.GetSuitableJavaVersion();

var javaInfos = _settingsService.Javas.Select(JavaUtils.GetJavaInfo).ToArray();
var possiblyAvailableJavas = targetJavaVersion == null

JavaInfo[] possiblyAvailableJavas;
bool isForgeOrNeoForge = false;

if (instance is ModifiedMinecraftInstance modifiedMinecraftInstance)
{
var loaders = modifiedMinecraftInstance.ModLoaders.Select(x => x.Type);
isForgeOrNeoForge = loaders.Contains(ModLoaderType.Forge) || loaders.Contains(ModLoaderType.NeoForge);
}

possiblyAvailableJavas = targetJavaVersion == null
? javaInfos
: javaInfos.Where(x => x.Version.Major.ToString().Equals(targetJavaVersion)).ToArray();
: isForgeOrNeoForge
? javaInfos.Where(x => x.Version.Major.ToString().Equals(targetJavaVersion)).ToArray()
: javaInfos.Where(x => x.Version.Major >= int.Parse(targetJavaVersion)).ToArray();

if (possiblyAvailableJavas.Length == 0)
throw new Exception($"No suitable version of Java found to start this game, version {targetJavaVersion} is required");
Expand Down
31 changes: 19 additions & 12 deletions Natsurainko.FluentLauncher/Services/Network/DownloadService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Natsurainko.FluentLauncher.Models.UI;
using Natsurainko.FluentLauncher.Services.Launch;
using Natsurainko.FluentLauncher.Services.Network.Data;
using Natsurainko.FluentLauncher.Services.Settings;
using Natsurainko.FluentLauncher.ViewModels.Common;
Expand All @@ -20,19 +19,26 @@ namespace Natsurainko.FluentLauncher.Services.Network;
internal partial class DownloadService
{
private readonly SettingsService _settingsService;
private readonly GameService _gameService;
private readonly MultipartDownloader _downloader = new(HttpUtils.HttpClient, 1024 * 1024, 8, 64);
private MultipartDownloader _downloader;

public IDownloader Downloader { get => _downloader; }

public ObservableCollection<TaskViewModel> DownloadTasks { get; } = [];

public DownloadService(SettingsService settingsService, GameService gameService)
public DownloadService(SettingsService settingsService)
{
_settingsService = settingsService;
_gameService = gameService;
_downloader = new(HttpUtils.HttpClient, 1024 * 1024, 8, _settingsService.MaxDownloadThreads,
_settingsService.CurrentDownloadSource == "Bmclapi" ? DownloadMirrors.BmclApi : null);

// TODO: 注册下载设置变化事件
_settingsService.MaxDownloadThreadsChanged += (_,_) => UpdateDownloader();
_settingsService.CurrentDownloadSourceChanged += (_, _) => UpdateDownloader();
}

void UpdateDownloader()
{
_downloader = new(HttpUtils.HttpClient, 1024 * 1024, 8, _settingsService.MaxDownloadThreads,
_settingsService.CurrentDownloadSource == "Bmclapi" ? DownloadMirrors.BmclApi : null);
}

public void DownloadResourceFile(GameResourceFile file, string filePath)
Expand Down Expand Up @@ -80,14 +86,15 @@ IInstanceInstaller GetInstanceInstaller(

ModLoaderType modLoaderType = instanceInstallConfig.PrimaryLoader.Type;
object selectedInstallData = instanceInstallConfig.PrimaryLoader.SelectedInstallData;
IDownloadMirror? downloadMirror = _settingsService.CurrentDownloadSource == "Bmclapi" ? DownloadMirrors.BmclApi : null;

installationStageViews = GetInstallationViewModel(modLoaderType, out var vanillaStagesViewModel, out var stagesViewModel);

IInstanceInstaller installer = modLoaderType switch
{
ModLoaderType.Forge => new ForgeInstanceInstaller()
{
//DownloadMirror = DownloadMirrors.BmclApi,
DownloadMirror = downloadMirror,
McVersionManifestItem = versionManifestItem,
MinecraftFolder = minecraftFolder,
CheckAllDependencies = true,
Expand All @@ -100,7 +107,7 @@ IInstanceInstaller GetInstanceInstaller(
},
ModLoaderType.NeoForge => new ForgeInstanceInstaller()
{
//DownloadMirror = DownloadMirrors.BmclApi,
DownloadMirror = downloadMirror,
McVersionManifestItem = versionManifestItem,
MinecraftFolder = minecraftFolder,
CheckAllDependencies = true,
Expand All @@ -113,7 +120,7 @@ IInstanceInstaller GetInstanceInstaller(
},
ModLoaderType.OptiFine => new OptiFineInstanceInstaller()
{
DownloadMirror = DownloadMirrors.BmclApi,
DownloadMirror = downloadMirror,
McVersionManifestItem = versionManifestItem,
MinecraftFolder = minecraftFolder,
CheckAllDependencies = true,
Expand All @@ -125,7 +132,7 @@ IInstanceInstaller GetInstanceInstaller(
},
ModLoaderType.Fabric => new FabricInstanceInstaller()
{
DownloadMirror = DownloadMirrors.BmclApi,
DownloadMirror = downloadMirror,
McVersionManifestItem = versionManifestItem,
MinecraftFolder = minecraftFolder,
CheckAllDependencies = true,
Expand All @@ -136,7 +143,7 @@ IInstanceInstaller GetInstanceInstaller(
},
ModLoaderType.Quilt => new QuiltInstanceInstaller()
{
DownloadMirror = DownloadMirrors.BmclApi,
DownloadMirror = downloadMirror,
McVersionManifestItem = versionManifestItem,
MinecraftFolder = minecraftFolder,
CheckAllDependencies = true,
Expand All @@ -156,7 +163,7 @@ List<InstallationStageViewModel> GetInstallationViewModel(
out InstallationViewModel<VanillaInstallationStage> vanillaStagesViewModel,
out object stagesViewModel)
{
List<InstallationStageViewModel> stageViewModels = new();
List<InstallationStageViewModel> stageViewModels = [];
vanillaStagesViewModel = new();

if (modLoaderType == ModLoaderType.Quilt)
Expand Down
17 changes: 5 additions & 12 deletions Natsurainko.FluentLauncher/Services/Settings/SettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,25 @@
using FluentLauncher.Infra.Settings.Converters;
using Natsurainko.FluentLauncher.Services.Storage;
using Natsurainko.FluentLauncher.Utils;
using Nrk.FluentCore.Management;
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using Windows.Storage;

namespace Natsurainko.FluentLauncher.Services.Settings;

public partial class SettingsService : SettingsContainer
{
#region New IFluentCoreSettingsService Property

public ObservableCollection<string> MinecraftFolders { get; private set; } = new();
public ObservableCollection<string> Javas { get; private set; } = new();
public ObservableCollection<string> MinecraftFolders { get; private set; } = [];
public ObservableCollection<string> Javas { get; private set; } = [];

[SettingItem(Default = "", Converter = typeof(JsonStringConverter<string>))]
public partial string ActiveMinecraftFolder { get; set; }

//[SettingItem(typeof(GameInfo), "ActiveGameInfo", Converter = typeof(JsonStringConverter<GameInfo>))]
[SettingItem]
[SettingItem] //[SettingItem(typeof(GameInfo), "ActiveGameInfo", Converter = typeof(JsonStringConverter<GameInfo>))]
public partial string? ActiveInstanceId { get; set; }

[SettingItem(Default = "", Converter = typeof(JsonStringConverter<string>))]
Expand All @@ -34,8 +29,6 @@ public partial class SettingsService : SettingsContainer
[SettingItem(Default = 1024, Converter = typeof(JsonStringConverter<int>))]
public partial int JavaMemory { get; set; }

#endregion

[SettingItem(Default = true, Converter = typeof(JsonStringConverter<bool>))]
public partial bool EnableAutoMemory { get; set; }

Expand Down Expand Up @@ -159,7 +152,7 @@ public SettingsService(ISettingsStorage storage) : base(storage)
Array.ForEach(minecraftFolders, MinecraftFolders.Add);
MinecraftFolders.CollectionChanged += (sender, e) =>
{
appsettings.Values["MinecraftFolders"] = JsonSerializer.Serialize(MinecraftFolders.ToArray(), FLSerializerContext.Default.StringArray);
appsettings.Values["MinecraftFolders"] = JsonSerializer.Serialize([.. MinecraftFolders], FLSerializerContext.Default.StringArray);
};

// Init Javas
Expand All @@ -174,7 +167,7 @@ public SettingsService(ISettingsStorage storage) : base(storage)
Array.ForEach(javaRuntimes, Javas.Add);
Javas.CollectionChanged += (sender, e) =>
{
appsettings.Values["Javas"] = JsonSerializer.Serialize(Javas.ToArray(), FLSerializerContext.Default.StringArray);
appsettings.Values["Javas"] = JsonSerializer.Serialize([.. Javas], FLSerializerContext.Default.StringArray);
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using CommunityToolkit.Mvvm.DependencyInjection;
using System;
using System;
using System.Diagnostics;
using System.Management;
using System.Runtime.CompilerServices;

namespace Natsurainko.FluentLauncher.Utils.Extensions;

Expand Down

0 comments on commit 92e4fa3

Please sign in to comment.