Skip to content

Commit

Permalink
Fix nullability warnings (#274)
Browse files Browse the repository at this point in the history
* Remove HttpUtils helpers

* Fix AddVmArgumentDialogViewModel

* Fix AuthenticationWizardDialogViewModel

* Fix DeleteInstanceDialogViewModel

* Fix ChooseAccountTypeViewModel

* Fix BrowserMicrosoftAuthViewModel

* Fix ChooseMicrosoftAuthMethodViewModel

* Fix ConfirmProfileViewModel

* Fix utils

* Fix non-exhaustive switch in TaskViewModel

* Fix nullability issues in GameService

* Fix nullability issues in AuthenticationWizardDialogViewModel

* Add EnumeratorCancellation to async enumerator in LaunchService

* Fix warnings in SkinPage

* Fix nullability issues in SettingsService

* Comment legacy MessageData class

* Update FluentCore

* Update FluentCore
  • Loading branch information
gaviny82 authored Sep 9, 2024
1 parent a14652c commit 9bf7ad0
Show file tree
Hide file tree
Showing 22 changed files with 133 additions and 111 deletions.
2 changes: 1 addition & 1 deletion Natsurainko.FluentCore
16 changes: 8 additions & 8 deletions Natsurainko.FluentLauncher/Models/UI/MessageData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

namespace Natsurainko.FluentLauncher.Models.UI;

public class MessageData
{
public string Message { get; set; }
//public class MessageData
//{
// public string Message { get; set; }

public string Title { get; set; }
// public string Title { get; set; }

public ButtonBase Button { get; set; }
// public ButtonBase Button { get; set; }

public InfoBarSeverity Severity { get; set; }
// public InfoBarSeverity Severity { get; set; }

public bool Removed { get; set; }
}
// public bool Removed { get; set; }
//}
12 changes: 6 additions & 6 deletions Natsurainko.FluentLauncher/Services/Launch/GameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public GameService(SettingsService settingsService)

public virtual void OnActiveInstanceChanged(MinecraftInstance? oldGame, MinecraftInstance? newGame)
{
_settingsService.ActiveInstanceId = newGame.InstanceId;
_settingsService.ActiveInstanceId = newGame?.InstanceId;
}

public virtual void ActivateMinecraftFolder(string? folder)
Expand All @@ -82,12 +82,12 @@ public virtual void ActivateMinecraftFolder(string? folder)
ActiveMinecraftFolder = folder;
}

public virtual void ActivateGame(MinecraftInstance? MinecraftInstance)
public virtual void ActivateGame(MinecraftInstance? instance)
{
if (MinecraftInstance != null && !_games.Contains(MinecraftInstance))
throw new ArgumentException("Not an game managed by GameService", nameof(MinecraftInstance));
if (instance != null && !_games.Contains(instance))
throw new ArgumentException("Not an game managed by GameService", nameof(instance));

ActiveGame = MinecraftInstance;
ActiveGame = instance;
}

public virtual void RefreshGames()
Expand Down Expand Up @@ -128,7 +128,7 @@ public void RemoveMinecraftFolder(string folder)

public void OnActiveMinecraftFolderChanged(string? oldFolder, string? newFolder)
{
_settingsService.ActiveMinecraftFolder = newFolder;
_settingsService.ActiveMinecraftFolder = newFolder ?? ""; // TODO: Make SettingsService.ActiveMinecraftFolder nullable

if (newFolder == null)
{
Expand Down
3 changes: 2 additions & 1 deletion Natsurainko.FluentLauncher/Services/Launch/LaunchService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Windows.ApplicationModel;
Expand Down Expand Up @@ -252,7 +253,7 @@ IEnumerable<string> GetExtraGameParameters()

cancellationToken.ThrowIfCancellationRequested();

async IAsyncEnumerable<string> GetExtraVmParameters(CancellationToken cancellationToken)
async IAsyncEnumerable<string> GetExtraVmParameters([EnumeratorCancellation] CancellationToken cancellationToken)
{
if (preCheckData.Account is YggdrasilAccount yggdrasil)
{
Expand Down
33 changes: 23 additions & 10 deletions Natsurainko.FluentLauncher/Services/Settings/SettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,30 @@ public SettingsService(ISettingsStorage storage) : base(storage)
Migrate();

// Init MinecraftFolders
string[] minecraftFolders = JsonSerializer.Deserialize<string[]>(appsettings.Values["MinecraftFolders"] as string ?? "null");
Array.ForEach(minecraftFolders ?? Array.Empty<string>(), MinecraftFolders.Add);
string? minecraftFoldersJson = appsettings.Values["MinecraftFolders"] as string;

string[] minecraftFolders;
if (minecraftFoldersJson is not null)
minecraftFolders = JsonSerializer.Deserialize<string[]>(minecraftFoldersJson) ?? [];
else
minecraftFolders = [];

Array.ForEach(minecraftFolders, MinecraftFolders.Add);
MinecraftFolders.CollectionChanged += (sender, e) =>
{
appsettings.Values["MinecraftFolders"] = JsonSerializer.Serialize(MinecraftFolders.ToArray());
};

// Init Javas
string[] javaRuntimes = JsonSerializer.Deserialize<string[]>(appsettings.Values["Javas"] as string ?? "null");
Array.ForEach(javaRuntimes ?? Array.Empty<string>(), Javas.Add);
string? javaRuntimesJson = appsettings.Values["Javas"] as string;

string[] javaRuntimes;
if (javaRuntimesJson is not null)
javaRuntimes = JsonSerializer.Deserialize<string[]>(javaRuntimesJson) ?? [];
else
javaRuntimes = [];

Array.ForEach(javaRuntimes, Javas.Add);
Javas.CollectionChanged += (sender, e) =>
{
appsettings.Values["Javas"] = JsonSerializer.Serialize(Javas.ToArray());
Expand All @@ -169,7 +183,6 @@ private void Migrate()
if (CurrentDownloadSource == "Mcbbs")
CurrentDownloadSource = "Bmclapi";

// ApplicationData.Current.LocalSettings.Values["SettingsVersion"] = 1u; // TODO: testing only, to be removed
if (SettingsVersion == 0u) // Version 0: Before Release 2.1.8.0
{
MigrateFrom_2_1_8_0();
Expand All @@ -188,9 +201,9 @@ private void Migrate()
SettingsVersion = 3u;
}

//if (SettingsVersion == 1) // Version 1: Release vNext
//if (SettingsVersion == N) // Version N: Release vNext
//{
// SettingsVersion = 2;
// SettingsVersion = N + 1;
//}
}

Expand Down Expand Up @@ -236,7 +249,7 @@ private static void MigrateFrom_2_1_8_0()
string accountSettingsPath = Path.Combine(accountSettingsDir, "accounts.json");
File.WriteAllText(accountSettingsPath, jsonNode.ToString());

//appsettings.Values.Remove("Accounts"); // TODO: Uncomment this after testing
appsettings.Values.Remove("Accounts");

// Migrate to storing the GUID of the active account in ApplicationData.Current.LocalSettings

Expand All @@ -247,11 +260,11 @@ private static void MigrateFrom_2_1_8_0()
return;

// Set new setting ActiveAccountUuid and remove the old one
if (Guid.TryParse(currentAccountJsonNode["Uuid"].GetValue<string>(), out Guid currentAccountUuid))
if (Guid.TryParse(currentAccountJsonNode["Uuid"]?.GetValue<string>(), out Guid currentAccountUuid))
{
appsettings.Values["ActiveAccountUuid"] = currentAccountUuid;
}
//appsettings.Values.Remove("CurrentAccount"); // TODO: Uncomment this after testing
appsettings.Values.Remove("CurrentAccount");
}

private static void MigrateFrom_2_3_0_0()
Expand Down
15 changes: 5 additions & 10 deletions Natsurainko.FluentLauncher/Utils/CommandParameterConverter.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
using System;

#nullable disable
namespace Natsurainko.FluentLauncher.Utils;

public static class CommandParameterConverter
{
public static (TSender sender, TArgs args) As<TSender, TArgs>(this object parameter)
{
var sender = parameter.GetType().GetField("Item1").GetValue(parameter);
var args = parameter.GetType().GetField("Item2").GetValue(parameter);
var sender = parameter.GetType().GetField("Item1")?.GetValue(parameter);
var args = parameter.GetType().GetField("Item2")?.GetValue(parameter);

return ((TSender)sender, (TArgs)args);
}
if (sender == null || args == null)
throw new InvalidCastException("Invalid parameter type.");

public static void As<TSender, TArgs>(this object parameter, Action<(TSender sender, TArgs args)> action)
{
var sender = parameter.GetType().GetField("Item1").GetValue(parameter);
var args = parameter.GetType().GetField("Item2").GetValue(parameter);
action(((TSender)sender, (TArgs)args));
return ((TSender)sender, (TArgs)args);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Microsoft.UI.Xaml.Controls;
using System;

#nullable disable
namespace Natsurainko.FluentLauncher.Utils.Extensions;

internal static class ControlExtensions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using System.Diagnostics;
using System.Management;

#nullable disable
namespace Natsurainko.FluentLauncher.Utils.Extensions;

internal static class ProcessExtensions
{
public static string GetCommandLine(this Process process)
public static string? GetCommandLine(this Process process)
{
string cmdLine = null;
string? cmdLine = null;
using (var searcher = new ManagementObjectSearcher(
$"SELECT CommandLine FROM Win32_Process WHERE ProcessId = {process.Id}"))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System;
using System.Threading.Tasks;

#nullable disable
namespace Natsurainko.FluentLauncher.Utils.Extensions;

internal static class StoryboardExtensions
Expand All @@ -14,7 +13,7 @@ public static Task BeginAsync(this Storyboard storyboard)

var taskCompletionSource = new TaskCompletionSource();

void onComplete(object s, object e)
void onComplete(object? s, object e)
{
//storyboard.Stop();
storyboard.Completed -= onComplete;
Expand Down
7 changes: 3 additions & 4 deletions Natsurainko.FluentLauncher/Utils/FileLockUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Diagnostics;
using System.Runtime.InteropServices;

#nullable disable
namespace Natsurainko.FluentLauncher.Utils;

internal class FileLockUtils
Expand Down Expand Up @@ -95,8 +94,8 @@ public struct RM_PROCESS_INFO

[DllImport("rstrtmgr.dll", CharSet = CharSet.Unicode)]
static extern int RmRegisterResources(uint pSessionHandle, uint nFiles, string[] rgsFilenames,
uint nApplications, [In] RM_UNIQUE_PROCESS[] rgApplications, uint nServices,
string[] rgsServiceNames);
uint nApplications, [In] RM_UNIQUE_PROCESS[]? rgApplications, uint nServices,
string[]? rgsServiceNames);

[DllImport("rstrtmgr.dll", CharSet = CharSet.Auto)]
static extern int RmStartSession(out uint pSessionHandle, int dwSessionFlags, string strSessionKey);
Expand All @@ -106,7 +105,7 @@ static extern int RmRegisterResources(uint pSessionHandle, uint nFiles, string[]

[DllImport("rstrtmgr.dll")]
static extern int RmGetList(uint dwSessionHandle, out uint pnProcInfoNeeded,
ref uint pnProcInfo, [In, Out] RM_PROCESS_INFO[] rgAffectedApps,
ref uint pnProcInfo, [In, Out] RM_PROCESS_INFO[]? rgAffectedApps,
ref uint lpdwRebootReasons);

private static List<Process> EnumerateProcesses(uint pnProcInfoNeeded, uint handle, uint lpdwRebootReasons)
Expand Down
3 changes: 1 addition & 2 deletions Natsurainko.FluentLauncher/Utils/LoggerColorLightLanguage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using ColorCode.Styling;
using System.Collections.Generic;

#nullable disable
namespace Natsurainko.FluentLauncher.Utils;

internal class LoggerColorLightLanguage : ILanguage
Expand Down Expand Up @@ -65,7 +64,7 @@ internal class LoggerColorLightLanguage : ILanguage

public string CssClassName => "logger";

public string FirstLinePattern => null;
public string? FirstLinePattern => null;

public IList<LanguageRule> Rules =>
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.ComponentModel;
using System.Web;

#nullable disable
namespace Natsurainko.FluentLauncher.ViewModels.AuthenticationWizard;

internal partial class BrowserMicrosoftAuthViewModel : WizardViewModelBase
Expand All @@ -17,16 +16,16 @@ internal partial class BrowserMicrosoftAuthViewModel : WizardViewModelBase

[ObservableProperty]
[NotifyPropertyChangedFor(nameof(CanNext))]
private string accessCode;
private string? accessCode;

[ObservableProperty]
private bool needRefresh;

[ObservableProperty]
private string description;
private string? description;

[ObservableProperty]
private string icon;
private string? icon;

[ObservableProperty]
private Uri source;
Expand All @@ -40,12 +39,11 @@ internal partial class BrowserMicrosoftAuthViewModel : WizardViewModelBase

private readonly AuthenticationService _authenticationService;

public BrowserMicrosoftAuthViewModel()
public BrowserMicrosoftAuthViewModel(AuthenticationService authService)
{
XamlPageType = typeof(BrowserMicrosoftAuthPage);

_authenticationService = App.GetService<AuthenticationService>();
_authenticationService = authService;

XamlPageType = typeof(BrowserMicrosoftAuthPage);
Source = new(AuthUrl);
}

Expand Down Expand Up @@ -91,7 +89,7 @@ private void ParseUrl()

public override WizardViewModelBase GetNextViewModel()
{
ConfirmProfileViewModel confirmProfileViewModel = default;
ConfirmProfileViewModel confirmProfileViewModel = null!;

confirmProfileViewModel = new ConfirmProfileViewModel(() =>
{
Expand All @@ -100,7 +98,7 @@ public override WizardViewModelBase GetNextViewModel()
App.DispatcherQueue.TryEnqueue(() => confirmProfileViewModel.LoadingProgressText = p.ToString());
});
return [_authenticationService.LoginMicrosoftAsync(AccessCode, progress).GetAwaiter().GetResult()];
return [_authenticationService.LoginMicrosoftAsync(AccessCode! /* guaranteed by CanNext */, progress).GetAwaiter().GetResult()];
});

return confirmProfileViewModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
using Natsurainko.FluentLauncher.ViewModels.Common;
using Natsurainko.FluentLauncher.Views.AuthenticationWizard;
using Nrk.FluentCore.Authentication;
using System;

#nullable disable
namespace Natsurainko.FluentLauncher.ViewModels.AuthenticationWizard;

internal partial class ChooseAccountTypeViewModel : WizardViewModelBase
Expand Down Expand Up @@ -35,7 +35,8 @@ public override WizardViewModelBase GetNextViewModel()
{
AccountType.Microsoft => new ChooseMicrosoftAuthMethodViewModel(_authService),
AccountType.Offline => new EnterOfflineProfileViewModel(),
AccountType.Yggdrasil => new EnterYggdrasilProfileViewModel()
AccountType.Yggdrasil => new EnterYggdrasilProfileViewModel(),
_ => throw new InvalidOperationException()
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
using Natsurainko.FluentLauncher.Services.Accounts;
using Natsurainko.FluentLauncher.ViewModels.Common;
using Natsurainko.FluentLauncher.Views.AuthenticationWizard;
using System;

#nullable disable
namespace Natsurainko.FluentLauncher.ViewModels.AuthenticationWizard;

internal partial class ChooseMicrosoftAuthMethodViewModel : WizardViewModelBase
Expand All @@ -30,8 +30,9 @@ public override WizardViewModelBase GetNextViewModel()
{
return SelectedMicrosoftAuthMethod switch
{
MicrosoftAuthMethod.BuiltInBrowser => new BrowserMicrosoftAuthViewModel(),
MicrosoftAuthMethod.DeviceFlowCode => new DeviceFlowMicrosoftAuthViewModel(_authService)
MicrosoftAuthMethod.BuiltInBrowser => new BrowserMicrosoftAuthViewModel(_authService),
MicrosoftAuthMethod.DeviceFlowCode => new DeviceFlowMicrosoftAuthViewModel(_authService),
_ => throw new InvalidOperationException()
};
}
}
Loading

0 comments on commit 9bf7ad0

Please sign in to comment.