Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor new core management style #262

Merged
merged 9 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions Natsurainko.FluentLauncher/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,17 @@
<ResourceDictionary x:Key="Light">
<SolidColorBrush x:Key="DeleteButtonBackground" Color="#BFCC0000" />
<SolidColorBrush x:Key="DeleteButtonBorder" Color="#BFCC0000" />
<x:Double x:Key="ExpanderSplitterOpacity">1</x:Double>
<Color x:Key="AcrylicTintColor">White</Color>
<SolidColorBrush x:Key="IconBorder" Color="#25000000" />
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
<SolidColorBrush x:Key="DeleteButtonBackground" Color="#3FFF0000" />
<SolidColorBrush x:Key="DeleteButtonBorder" Color="#59FF0000" />
<x:Double x:Key="ExpanderSplitterOpacity">0.1</x:Double>
<Color x:Key="AcrylicTintColor">Black</Color>
<SolidColorBrush x:Key="IconBorder" Color="#10FFFFFF" />
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>

<Style x:Key="ExpanderSplitter" TargetType="Border">
<Setter Property="Height" Value="1" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="BorderBrush" Value="{ThemeResource AppBarBorderThemeBrush}" />
<Setter Property="Opacity" Value="{ThemeResource ExpanderSplitterOpacity}" />
</Style>

<FontFamily x:Key="MinecraftTen">/Assets/MinecraftTen.ttf#Minecraft Ten</FontFamily>

<converters:AccountInfoConverter x:Key="AccountInfoConverter" />
Expand All @@ -45,11 +36,14 @@
<converters:GameIconConverter x:Key="GameIconConverter" />
<converters:GameInfoConverter x:Key="GameInfoConverter" />

<converters:ModInfoConverter x:Key="ModInfoConverter" />
<converters:SaveInfoConverter x:Key="SaveInfoConverter" />

<converters:ColorHexCodeConverter x:Key="ColorHexCodeConverter" />
<converters:InvertBoolConverter x:Key="InvertBoolConverter" />
<converters:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
<converters:ModItemTagConverter x:Key="ModItemTagConverter" />
<converters:ResourceAuthorsConverter x:Key="ResourceAuthorsConverter" />

<converters:RadioButtonSelectedIndexConverter x:Key="RadioButtonSelectedIndexConverter" />

<Style
Expand All @@ -60,7 +54,6 @@
<Setter Property="Margin" Value="1,30,0,6" />
</Style.Setters>
</Style>

<Style
x:Key="SettingsTitleSectionHeaderTextBlockStyle"
BasedOn="{StaticResource BodyStrongTextBlockStyle}"
Expand Down
27 changes: 27 additions & 0 deletions Natsurainko.FluentLauncher/Experimental/Saves/SaveInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;

#nullable disable
namespace Natsurainko.FluentLauncher.Experimental.Saves;

internal record SaveInfo
{
public string Folder { get; set; }

public string FolderName { get; set; }

public string LevelName { get; set; }

public string Version { get; set; }

public bool AllowCommands { get; set; }

public DateTime LastPlayed { get; set; }

public long Seed { get; set; }

public string IconFilePath { get; set; }

public int GameType { get; set; }
}


49 changes: 49 additions & 0 deletions Natsurainko.FluentLauncher/Experimental/Saves/SaveInfoParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using NbtToolkit.Binary;
using System;
using System.IO;
using System.Threading.Tasks;

namespace Natsurainko.FluentLauncher.Experimental.Saves;

internal static class SaveInfoParser
{
public static async Task<SaveInfo> ParseAsync(string saveFolder)
{
var saveInfo = new SaveInfo
{
FolderName = new DirectoryInfo(saveFolder).Name,
Folder = saveFolder
};

await Task.Run(() =>
{
var time = DateTime.Now;

using var fileStream = new FileStream(Path.Combine(saveFolder, "level.dat"), FileMode.Open, FileAccess.Read);
using var _nbtReader = new NbtReader(fileStream, NbtCompression.GZip, true);

var rootTag = _nbtReader.ReadRootTag();

var dataTagCompound = rootTag["Data"].AsTagCompound();

saveInfo.LevelName = dataTagCompound["LevelName"].AsString();
saveInfo.AllowCommands = dataTagCompound["allowCommands"].AsBool();
saveInfo.GameType = dataTagCompound["GameType"].AsInt();
saveInfo.Version = dataTagCompound["Version"].AsTagCompound()["Name"].AsString();

if (dataTagCompound.ContainsKey("WorldGenSettings"))
saveInfo.Seed = dataTagCompound["WorldGenSettings"].AsTagCompound()["seed"].AsLong();
else if (dataTagCompound.ContainsKey("RandomSeed"))
saveInfo.Seed = dataTagCompound["RandomSeed"].AsLong();

saveInfo.LastPlayed = DateTimeOffset.FromUnixTimeMilliseconds(dataTagCompound["LastPlayed"].AsLong()).ToLocalTime().DateTime;

if (File.Exists(Path.Combine(saveFolder, "icon.png")))
saveInfo.IconFilePath = Path.Combine(saveFolder, "icon.png");

var gap = DateTime.Now - time;
});

return saveInfo;
}
}
45 changes: 45 additions & 0 deletions Natsurainko.FluentLauncher/Experimental/Saves/SaveManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.IO;

namespace Natsurainko.FluentLauncher.Experimental.Saves;

internal class SaveManager
{
private readonly List<(FileInfo, Exception)> _errorLevelData = [];

public IReadOnlyList<(FileInfo, Exception)> ErrorLevelData { get; init; }

public string SavesFolder { get; private set; }

public SaveManager(string savesFolder)
{
SavesFolder = savesFolder;
ErrorLevelData = _errorLevelData;
}

public async IAsyncEnumerable<SaveInfo> EnumerateSavesAsync()
{
foreach (var dir in Directory.EnumerateDirectories(SavesFolder))
{
var levelDataFile = new FileInfo(Path.Combine(dir, "level.dat"));

if (!levelDataFile.Exists)
continue;

SaveInfo? saveInfo = default;

try
{
saveInfo = await SaveInfoParser.ParseAsync(dir);
}
catch (Exception ex)
{
_errorLevelData.Add((levelDataFile, ex));
}

if (saveInfo != null)
yield return saveInfo;
}
}
}
11 changes: 11 additions & 0 deletions Natsurainko.FluentLauncher/Experimental/Servers/ServerInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#nullable disable
namespace Natsurainko.FluentLauncher.Experimental.Servers;

internal record ServerInfo
{
public string Name { get; set; }

public string Address { get; set; }

public string Icon { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
using System.Text.Json;
using System.Text.Json.Serialization;

#nullable disable
namespace Natsurainko.FluentLauncher.Models.Launch;

internal partial class GameSpecialConfig : ObservableObject
internal partial class GameConfig : ObservableObject
{
[JsonIgnore]
public string FilePath { get; set; }
Expand Down
3 changes: 2 additions & 1 deletion Natsurainko.FluentLauncher/Natsurainko.FluentLauncher.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows10.0.22621.0</TargetFramework>
<TargetFramework>net8.0-windows10.0.22621.0</TargetFramework>
<LangVersion>12.0</LangVersion>
<TargetPlatformMinVersion>10.0.19041.0</TargetPlatformMinVersion>
<RootNamespace>Natsurainko.FluentLauncher</RootNamespace>
Expand Down Expand Up @@ -51,6 +51,7 @@
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.5.240428000" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.3233" />
<PackageReference Include="Microsoft.Xaml.Behaviors.WinUI.Managed" Version="2.0.9" />
<PackageReference Include="NbtToolkit" Version="0.1.2-beta" />
<PackageReference Include="PInvoke.User32" Version="0.7.124" />
<PackageReference Include="ReverseMarkdown" Version="3.25.0" />
<PackageReference Include="WindowsAPICodePack.Shell.CommonFileDialogs" Version="1.1.5" />
Expand Down
10 changes: 6 additions & 4 deletions Natsurainko.FluentLauncher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@

// Cores page
pages.WithPage<Views.Cores.CoresPage, ViewModels.Cores.CoresViewModel>("CoresPage");
pages.WithPage<Views.Cores.ManageNavigationPage, ViewModels.Cores.ManageNavigationViewModel>("CoresManageNavigationPage");
pages.WithPage<Views.Cores.Manage.CoreSettingsPage, ViewModels.Cores.Manage.CoreSettingsViewModel>("CoreSettingsPage");
pages.WithPage<Views.Cores.Manage.CoreModsPage, ViewModels.Cores.Manage.CoreModsViewModel>("CoreModsPage");
pages.WithPage<Views.Cores.Manage.CoreStatisticPage, ViewModels.Cores.Manage.CoreStatisticViewModel>("CoreStatisticPage");

pages.WithPage<Views.Cores.Manage.NavigationPage, ViewModels.Cores.Manage.NavigationViewModel>("CoreManage/Navigation");
pages.WithPage<Views.Cores.Manage.DefaultPage, ViewModels.Cores.Manage.DefaultViewModel>("CoreManage/Default");
pages.WithPage<Views.Cores.Manage.ConfigPage, ViewModels.Cores.Manage.ConfigViewModel>("CoreManage/Config");
pages.WithPage<Views.Cores.Manage.ModPage, ViewModels.Cores.Manage.ModViewModel>("CoreManage/Mod");
pages.WithPage<Views.Cores.Manage.SavePage, ViewModels.Cores.Manage.SaveViewModel>("CoreManage/Save");

// Activities page
pages.WithPage<Views.Activities.ActivitiesNavigationPage, ViewModels.Activities.ActivitiesNavigationViewModel>("ActivitiesNavigationPage");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ string GetTitle()
App.DispatcherQueue.SynchronousTryEnqueue(() => _gameService.RefreshGames());

var gameInfo = _gameService.Games.First(x => x.AbsoluteId.Equals(info.AbsoluteId));
var specialConfig = gameInfo.GetSpecialConfig();
var config = gameInfo.GetConfig();

specialConfig.EnableSpecialSetting = info.EnableIndependencyCore || !string.IsNullOrEmpty(info.NickName);
specialConfig.EnableIndependencyCore = info.EnableIndependencyCore;
specialConfig.NickName = info.NickName;
config.EnableSpecialSetting = info.EnableIndependencyCore || !string.IsNullOrEmpty(info.NickName);
config.EnableIndependencyCore = info.EnableIndependencyCore;
config.NickName = info.NickName;

@this.OnProgressChanged(1);

Expand Down
4 changes: 2 additions & 2 deletions Natsurainko.FluentLauncher/Services/Launch/GameLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public GameLocator(DirectoryInfo directory) : base(directory.FullName) { }

protected override string GetName(GameInfo gameInfo, VersionJsonEntity jsonEntity)
{
var specialConfig = gameInfo.GetSpecialConfig();
return string.IsNullOrEmpty(specialConfig.NickName) ? jsonEntity.Id : specialConfig.NickName;
var config = gameInfo.GetConfig();
return string.IsNullOrEmpty(config.NickName) ? jsonEntity.Id : config.NickName;
}
}
24 changes: 12 additions & 12 deletions Natsurainko.FluentLauncher/Services/Launch/LaunchService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ public override MinecraftSession CreateMinecraftSessionFromGameInfo(GameInfo gam
if (suitableJava == null)
throw new Exception(ResourceUtils.GetValue("Exceptions", "_NoSuitableJava").Replace("${version}", gameInfo.GetSuitableJavaVersion()));

var specialConfig = gameInfo.GetSpecialConfig(); // Game specific config
var launchAccount = GetLaunchAccount(specialConfig, _accountService)
var config = gameInfo.GetConfig(); // Game specific config
var launchAccount = GetLaunchAccount(config, _accountService)
?? throw new Exception(ResourceUtils.GetValue("Exceptions", "_NoAccount")); // Determine which account to use

Account Authenticate()
Expand All @@ -105,7 +105,7 @@ Account Authenticate()
else
{
_accountService.RefreshAccount(launchAccount).GetAwaiter().GetResult();
return GetLaunchAccount(specialConfig, _accountService);
return GetLaunchAccount(config, _accountService);
}
}
catch (Exception ex)
Expand All @@ -122,13 +122,13 @@ Account Authenticate()
{
Account = launchAccount,
GameInfo = gameInfo,
GameDirectory = GetGameDirectory(gameInfo, specialConfig),
GameDirectory = GetGameDirectory(gameInfo, config),
JavaPath = suitableJava,
MaxMemory = maxMemory,
MinMemory = minMemory,
UseDemoUser = _settingsService.EnableDemoUser,
ExtraGameParameters = GetExtraGameParameters(specialConfig),
ExtraVmParameters = GetExtraVmParameters(specialConfig, launchAccount),
ExtraGameParameters = GetExtraGameParameters(config),
ExtraVmParameters = GetExtraVmParameters(config, launchAccount),
CreateResourcesDownloader = (libs) => _downloadService.CreateResourcesDownloader
(gameInfo, libs)
};
Expand All @@ -138,7 +138,7 @@ Account Authenticate()

session.ProcessStarted += (s, e) =>
{
var title = GameWindowTitle(specialConfig);
var title = GameWindowTitle(config);
if (string.IsNullOrEmpty(title)) return;

Task.Run(async () =>
Expand Down Expand Up @@ -183,7 +183,7 @@ Account Authenticate()
return suits.First().Item1;
}

private string GetGameDirectory(GameInfo gameInfo, GameSpecialConfig specialConfig)
private string GetGameDirectory(GameInfo gameInfo, GameConfig specialConfig)
{
if (specialConfig.EnableSpecialSetting)
{
Expand All @@ -198,7 +198,7 @@ private string GetGameDirectory(GameInfo gameInfo, GameSpecialConfig specialConf
return gameInfo.MinecraftFolderPath;
}

private string? GameWindowTitle(GameSpecialConfig specialConfig)
private string? GameWindowTitle(GameConfig specialConfig)
{
if (specialConfig.EnableSpecialSetting)
{
Expand All @@ -214,7 +214,7 @@ private string GetGameDirectory(GameInfo gameInfo, GameSpecialConfig specialConf
return null;
}

public static Account GetLaunchAccount(GameSpecialConfig specialConfig, AccountService _accountService)
public static Account GetLaunchAccount(GameConfig specialConfig, AccountService _accountService)
{
if (specialConfig.EnableSpecialSetting && specialConfig.EnableTargetedAccount && specialConfig.Account != null)
{
Expand All @@ -241,7 +241,7 @@ public static Account GetLaunchAccount(GameSpecialConfig specialConfig, AccountS
return _accountService.ActiveAccount;
}

private IEnumerable<string> GetExtraVmParameters(GameSpecialConfig specialConfig, Account account)
private IEnumerable<string> GetExtraVmParameters(GameConfig specialConfig, Account account)
{
if (account is YggdrasilAccount yggdrasil)
{
Expand All @@ -259,7 +259,7 @@ private IEnumerable<string> GetExtraVmParameters(GameSpecialConfig specialConfig
yield return item;
}

private IEnumerable<string> GetExtraGameParameters(GameSpecialConfig specialConfig)
private IEnumerable<string> GetExtraGameParameters(GameConfig specialConfig)
{
if (specialConfig.EnableSpecialSetting)
{
Expand Down
Loading
Loading