Skip to content

Commit

Permalink
Better interface splitting
Browse files Browse the repository at this point in the history
  • Loading branch information
Aragas committed Nov 30, 2023
1 parent 82ffefe commit 90a60a7
Show file tree
Hide file tree
Showing 26 changed files with 318 additions and 222 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Bannerlord.LauncherManager.External;
using Bannerlord.LauncherManager.External.UI;
using Bannerlord.LauncherManager.Models;

using BUTR.NativeAOT.Shared;
Expand All @@ -10,12 +10,12 @@

namespace Bannerlord.LauncherManager.Native.Adapters;

internal sealed unsafe class DialogUIProvider : IDialogUIProvider
internal sealed unsafe class DialogProvider : IDialogProvider
{
private readonly param_ptr* _pOwner;
private readonly N_SendDialogDelegate _sendDialog;

public DialogUIProvider(param_ptr* pOwner, N_SendDialogDelegate sendDialog)
public DialogProvider(param_ptr* pOwner, N_SendDialogDelegate sendDialog)
{
_pOwner = pOwner;
_sendDialog = sendDialog;
Expand All @@ -31,9 +31,9 @@ private void SendDialogNative(ReadOnlySpan<char> type, ReadOnlySpan<char> title,
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
static void Callback(param_ptr* p_ptr, param_string* p_result)
{
Logger.LogInput($"{nameof(SendDialog)}_{nameof(Callback)}");
Logger.LogInput($"{nameof(SendDialogNative)}_{nameof(Callback)}");
var result = new string(param_string.ToSpan(p_result));
Logger.LogOutput(result, $"{nameof(SendDialog)}_{nameof(Callback)}");
Logger.LogOutput(result, $"{nameof(SendDialogNative)}_{nameof(Callback)}");

var handle = GCHandle.FromIntPtr(new IntPtr(p_ptr));
try
Expand All @@ -43,7 +43,7 @@ static void Callback(param_ptr* p_ptr, param_string* p_result)
}
catch (Exception e)
{
Logger.LogException(e, $"{nameof(SendDialog)}_{nameof(Callback)}");
Logger.LogException(e, $"{nameof(SendDialogNative)}_{nameof(Callback)}");
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using Bannerlord.LauncherManager.External;
using Bannerlord.LauncherManager.Models;

using BUTR.NativeAOT.Shared;

using System;
using System.Collections.Generic;

namespace Bannerlord.LauncherManager.Native.Adapters;

internal sealed unsafe class LauncherStateProvider : ILauncherStateProvider
{
private readonly param_ptr* _pOwner;
private readonly N_SetGameParametersDelegate _setGameParameters;
private readonly N_GetOptions _getOptions;
private readonly N_GetState _getState;

public LauncherStateProvider(
param_ptr* pOwner,
N_SetGameParametersDelegate setGameParameters,
N_GetOptions getOptions,
N_GetState getState)
{
_pOwner = pOwner;
_setGameParameters = setGameParameters;
_getOptions = getOptions;
_getState = getState;
}

public void SetGameParameters(string executable, IReadOnlyList<string> gameParameters) => SetGameParametersNative(executable, gameParameters);
public LauncherOptions GetOptions() => GetOptionsNative();
public LauncherState GetState() => GetStateNative();

private void SetGameParametersNative(ReadOnlySpan<char> executable, IReadOnlyList<string> gameParameters)
{
Logger.LogInput();

fixed (char* pExecutable = executable)
fixed (char* pGameParameters = BUTR.NativeAOT.Shared.Utils.SerializeJson(gameParameters, Bindings.CustomSourceGenerationContext.IReadOnlyListString))
{
Logger.LogPinned(pExecutable, pGameParameters);

using var result = SafeStructMallocHandle.Create(_setGameParameters(_pOwner, (param_string*) pExecutable, (param_json*) pGameParameters), true);
result.ValueAsVoid();
}

Logger.LogOutput();
}

private LauncherOptions GetOptionsNative()
{
Logger.LogInput();

using var result = SafeStructMallocHandle.Create(_getOptions(_pOwner), true);
if (result.IsNull) return LauncherOptions.Empty;

var returnResult = result.ValueAsJson(Bindings.CustomSourceGenerationContext.LauncherOptions)!;
Logger.LogOutput(returnResult);
return returnResult;
}

private LauncherState GetStateNative()
{
Logger.LogInput();

using var result = SafeStructMallocHandle.Create(_getState(_pOwner), true);
if (result.IsNull) return LauncherState.Empty;

var returnResult = result.ValueAsJson(Bindings.CustomSourceGenerationContext.LauncherState)!;
Logger.LogOutput(returnResult);
return returnResult;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

namespace Bannerlord.LauncherManager.Native.Adapters;

internal sealed unsafe class LoadOrderProvider : ILoadOrderProvider
internal sealed unsafe class LoadOrderPersistenceProvider : ILoadOrderPersistenceProvider
{
private readonly param_ptr* _pOwner;
private readonly N_GetLoadOrderDelegate _getLoadOrder;
private readonly N_SetLoadOrderDelegate _setLoadOrder;

public LoadOrderProvider(param_ptr* pOwner, N_GetLoadOrderDelegate getLoadOrder, N_SetLoadOrderDelegate setLoadOrder)
public LoadOrderPersistenceProvider(param_ptr* pOwner, N_GetLoadOrderDelegate getLoadOrder, N_SetLoadOrderDelegate setLoadOrder)
{
_pOwner = pOwner;
_getLoadOrder = getLoadOrder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,65 +1,38 @@
using Bannerlord.LauncherManager.External;
using Bannerlord.LauncherManager.External.UI;
using Bannerlord.LauncherManager.Models;
using Bannerlord.LauncherManager.Native.Models;

using BUTR.NativeAOT.Shared;

using System;
using System.Collections.Generic;
using System.Linq;

namespace Bannerlord.LauncherManager.Native.Adapters;

internal sealed unsafe class LauncherProvider : ILauncherProvider
internal sealed unsafe class LoadOrderStateProvider : ILoadOrderStateProvider
{
private readonly param_ptr* _pOwner;
private readonly N_SetGameParametersDelegate _setGameParameters;
private readonly N_GetAllModuleViewModels _getAllModuleViewModels;
private readonly N_GetModuleViewModels _getModuleViewModels;
private readonly N_SetModuleViewModels _setModuleViewModels;
private readonly N_GetOptions _getOptions;
private readonly N_GetState _getState;

public LauncherProvider(
public LoadOrderStateProvider(
param_ptr* pOwner,
N_SetGameParametersDelegate setGameParameters,
N_GetAllModuleViewModels getAllModuleViewModels,
N_GetModuleViewModels getModuleViewModels,
N_SetModuleViewModels setModuleViewModels,
N_GetOptions getOptions,
N_GetState getState)
N_SetModuleViewModels setModuleViewModels)
{
_pOwner = pOwner;
_setGameParameters = setGameParameters;
_getAllModuleViewModels = getAllModuleViewModels;
_getModuleViewModels = getModuleViewModels;
_setModuleViewModels = setModuleViewModels;
_getOptions = getOptions;
_getState = getState;
}

public void SetGameParameters(string executable, IReadOnlyList<string> gameParameters) => SetGameParametersNative(executable, gameParameters);
public IModuleViewModel[]? GetAllModuleViewModels() => GetAllModuleViewModelsNative();
public IModuleViewModel[]? GetModuleViewModels() => GetModuleViewModelsNative();
public void SetModuleViewModels(IReadOnlyList<IModuleViewModel> moduleViewModels) => SetModuleViewModelsNative(moduleViewModels);
public LauncherOptions GetOptions() => GetOptionsNative();
public LauncherState GetState() => GetStateNative();

private void SetGameParametersNative(ReadOnlySpan<char> executable, IReadOnlyList<string> gameParameters)
{
Logger.LogInput();

fixed (char* pExecutable = executable)
fixed (char* pGameParameters = BUTR.NativeAOT.Shared.Utils.SerializeJson(gameParameters, Bindings.CustomSourceGenerationContext.IReadOnlyListString))
{
Logger.LogPinned(pExecutable, pGameParameters);

using var result = SafeStructMallocHandle.Create(_setGameParameters(_pOwner, (param_string*) pExecutable, (param_json*) pGameParameters), true);
result.ValueAsVoid();
}

Logger.LogOutput();
}

private IModuleViewModel[]? GetAllModuleViewModelsNative()
{
Expand Down Expand Up @@ -98,28 +71,4 @@ private void SetModuleViewModelsNative(IReadOnlyList<IModuleViewModel> moduleVie
}
Logger.LogOutput();
}

private LauncherOptions GetOptionsNative()
{
Logger.LogInput();

using var result = SafeStructMallocHandle.Create(_getOptions(_pOwner), true);
if (result.IsNull) return LauncherOptions.Empty;

var returnResult = result.ValueAsJson(Bindings.CustomSourceGenerationContext.LauncherOptions)!;
Logger.LogOutput(returnResult);
return returnResult;
}

private LauncherState GetStateNative()
{
Logger.LogInput();

using var result = SafeStructMallocHandle.Create(_getState(_pOwner), true);
if (result.IsNull) return LauncherState.Empty;

var returnResult = result.ValueAsJson(Bindings.CustomSourceGenerationContext.LauncherState)!;
Logger.LogOutput(returnResult);
return returnResult;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Bannerlord.LauncherManager.External;
using Bannerlord.LauncherManager.External.UI;
using Bannerlord.LauncherManager.Models;

using BUTR.NativeAOT.Shared;
Expand All @@ -7,12 +7,12 @@

namespace Bannerlord.LauncherManager.Native.Adapters;

internal sealed unsafe class NotificationUIProvider : INotificationUIProvider
internal sealed unsafe class NotificationProvider : INotificationProvider
{
private readonly param_ptr* _pOwner;
private readonly N_SendNotificationDelegate _sendNotification;

public NotificationUIProvider(param_ptr* pOwner, N_SendNotificationDelegate sendNotification)
public NotificationProvider(param_ptr* pOwner, N_SendNotificationDelegate sendNotification)
{
_pOwner = pOwner;
_sendNotification = sendNotification;
Expand Down
8 changes: 0 additions & 8 deletions src/Bannerlord.LauncherManager.Native/AllocationException.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static unsafe partial class Bindings
try
{
var launcherManager = new LauncherManagerHandlerNative(p_owner,
dialogUIProvider: new DialogUIProvider(p_owner,
dialogProvider: new DialogProvider(p_owner,
Marshal.GetDelegateForFunctionPointer<N_SendDialogDelegate>(new IntPtr(p_send_dialog))
),
fileSystemProvider: new FileSystemProvider(p_owner,
Expand All @@ -51,20 +51,22 @@ public static unsafe partial class Bindings
gameInfoProvider: new GameInfoProvider(p_owner,
Marshal.GetDelegateForFunctionPointer<N_GetInstallPathDelegate>(new IntPtr(p_get_install_path))
),
launcherUProvider:new LauncherProvider(p_owner,
launcherStateUProvider: new LauncherStateProvider(p_owner,
Marshal.GetDelegateForFunctionPointer<N_SetGameParametersDelegate>(new IntPtr(p_set_game_parameters)),
Marshal.GetDelegateForFunctionPointer<N_GetAllModuleViewModels>(new IntPtr(p_get_all_module_view_models)),
Marshal.GetDelegateForFunctionPointer<N_GetModuleViewModels>(new IntPtr(p_get_module_view_models)),
Marshal.GetDelegateForFunctionPointer<N_SetModuleViewModels>(new IntPtr(p_set_module_view_models)),
Marshal.GetDelegateForFunctionPointer<N_GetOptions>(new IntPtr(p_get_options)),
Marshal.GetDelegateForFunctionPointer<N_GetState>(new IntPtr(p_get_state))
),
loadOrderProvider: new LoadOrderProvider(p_owner,
loadOrderPersistenceProvider: new LoadOrderPersistenceProvider(p_owner,
Marshal.GetDelegateForFunctionPointer<N_GetLoadOrderDelegate>(new IntPtr(p_load_load_order)),
Marshal.GetDelegateForFunctionPointer<N_SetLoadOrderDelegate>(new IntPtr(p_save_load_order))
),
notificationUIProvider: new NotificationUIProvider(p_owner,
notificationProvider: new NotificationProvider(p_owner,
Marshal.GetDelegateForFunctionPointer<N_SendNotificationDelegate>(new IntPtr(p_send_notification))
),
loadOrderStateProvider: new LoadOrderStateProvider(p_owner,
Marshal.GetDelegateForFunctionPointer<N_GetAllModuleViewModels>(new IntPtr(p_get_all_module_view_models)),
Marshal.GetDelegateForFunctionPointer<N_GetModuleViewModels>(new IntPtr(p_get_module_view_models)),
Marshal.GetDelegateForFunctionPointer<N_SetModuleViewModels>(new IntPtr(p_set_module_view_models))
)
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Bannerlord.LauncherManager.External;
using Bannerlord.LauncherManager.External.UI;
using Bannerlord.LauncherManager.Localization;
using Bannerlord.LauncherManager.Models;

Expand Down Expand Up @@ -26,8 +27,15 @@ internal sealed unsafe class LauncherManagerHandlerNative : LauncherManagerHandl
public param_ptr* OwnerPtr { get; }
public VoidPtr* HandlePtr { get; }

public LauncherManagerHandlerNative(param_ptr* pOwner, ILauncherProvider launcherUProvider, IGameInfoProvider gameInfoProvider, ILoadOrderProvider loadOrderProvider, IFileSystemProvider fileSystemProvider, IDialogUIProvider dialogUIProvider, INotificationUIProvider notificationUIProvider) :
base(launcherUProvider, gameInfoProvider, loadOrderProvider, fileSystemProvider, dialogUIProvider, notificationUIProvider)
public LauncherManagerHandlerNative(param_ptr* pOwner,
ILauncherStateProvider launcherStateUProvider,
IGameInfoProvider gameInfoProvider,
ILoadOrderPersistenceProvider loadOrderPersistenceProvider,
IFileSystemProvider fileSystemProvider,
IDialogProvider dialogProvider,
INotificationProvider notificationProvider,
ILoadOrderStateProvider loadOrderStateProvider) :
base(launcherStateUProvider, gameInfoProvider, loadOrderPersistenceProvider, fileSystemProvider, dialogProvider, notificationProvider, loadOrderStateProvider)
{
OwnerPtr = pOwner;
HandlePtr = (VoidPtr*) GCHandle.ToIntPtr(GCHandle.Alloc(this, GCHandleType.Normal)).ToPointer();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Bannerlord.LauncherManager.Models;

using System;
using System.Collections.Generic;

namespace Bannerlord.LauncherManager.External;

public sealed class CallbackLauncherStateProvider : ILauncherStateProvider
{
private readonly Action<string, IReadOnlyList<string>> _setGameParameters;

private readonly Func<LauncherOptions> _getOptions;
private readonly Func<LauncherState> _getState;

public CallbackLauncherStateProvider(Action<string, IReadOnlyList<string>> setGameParameters, Func<LauncherOptions> getOptions, Func<LauncherState> getState)
{
_setGameParameters = setGameParameters;
_getOptions = getOptions;
_getState = getState;
}

public void SetGameParameters(string executable, IReadOnlyList<string> gameParameters) => _setGameParameters(executable, gameParameters);
public LauncherOptions GetOptions() => _getOptions();
public LauncherState GetState() => _getState();
}
Loading

0 comments on commit 90a60a7

Please sign in to comment.