Skip to content

Commit

Permalink
Added ModuleProviderType to specify the origin of the module
Browse files Browse the repository at this point in the history
Renamed ModuleInfoExtendedWithPath to ModuleInfoExtendedWithMetadata
Fixed the LoadOrder sorter
Formatting
  • Loading branch information
Aragas committed Apr 19, 2024
1 parent 033db78 commit 433ed7c
Show file tree
Hide file tree
Showing 50 changed files with 202 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal partial class BUTRLanguageData
{
public string StringId { get; }
public string Title { get; private set; } = string.Empty;
public string[] SupportedIsoCodes { get; private set; } = Array.Empty<string>();
public string[] SupportedIsoCodes { get; private set; } = [];
public bool IsUnderDevelopment { get; private set; } = true;
public bool IsValid { get; private set; }

Expand Down
2 changes: 1 addition & 1 deletion src/Bannerlord.LauncherManager.Models/IModuleViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public interface IModuleViewModel
{
ModuleInfoExtendedWithPath ModuleInfoExtended { get; }
ModuleInfoExtendedWithMetadata ModuleInfoExtended { get; }
bool IsValid { get; }
bool IsSelected { get; set; }
bool IsDisabled { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@

namespace Bannerlord.LauncherManager.Models;

public record ModuleInfoExtendedWithPath : ModuleInfoExtended
public record ModuleInfoExtendedWithMetadata : ModuleInfoExtended
{
public ModuleProviderType ModuleProviderType { get; set; }
public string Path { get; set; } = string.Empty;

public ModuleInfoExtendedWithPath() { }
public ModuleInfoExtendedWithPath(string path, string id, string name, bool isOfficial, ApplicationVersion version, bool isSingleplayerModule, bool isMultiplayerModule,
public ModuleInfoExtendedWithMetadata() { }
public ModuleInfoExtendedWithMetadata(ModuleProviderType moduleProviderType, string path, string id, string name, bool isOfficial, ApplicationVersion version, bool isSingleplayerModule, bool isMultiplayerModule,
IReadOnlyList<SubModuleInfoExtended> subModules, IReadOnlyList<DependentModule> dependentModules, IReadOnlyList<DependentModule> modulesToLoadAfterThis,
IReadOnlyList<DependentModule> incompatibleModules, IReadOnlyList<DependentModuleMetadata> dependentModuleMetadatas, string url)
{
ModuleProviderType = moduleProviderType;
Path = path;
Id = id;
Name = name;
Expand All @@ -27,8 +29,9 @@ public ModuleInfoExtendedWithPath(string path, string id, string name, bool isOf
DependentModuleMetadatas = dependentModuleMetadatas;
Url = url;
}
public ModuleInfoExtendedWithPath(ModuleInfoExtended module, string path) : base(module)
public ModuleInfoExtendedWithMetadata(ModuleInfoExtended module, ModuleProviderType moduleProviderType, string path) : base(module)
{
ModuleProviderType = moduleProviderType;
Path = path;
}
}
7 changes: 7 additions & 0 deletions src/Bannerlord.LauncherManager.Models/ModuleProviderType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Bannerlord.LauncherManager.Models;

public enum ModuleProviderType
{
Default = 0,
Steam = 1,
}
2 changes: 1 addition & 1 deletion src/Bannerlord.LauncherManager.Models/SaveMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public SaveMetadata(string name, TWSaveMetadata metadata) : base(metadata.List)
}

public string[] GetModules() =>
TryGetValue("Modules", out var text) ? text.Split(';') : Array.Empty<string>();
TryGetValue("Modules", out var text) ? text.Split(';') : [];

public ApplicationVersion GetModuleVersion(string moduleName) =>
TryGetValue($"Module_{moduleName}", out var versionRaw) ? ApplicationVersion.TryParse(versionRaw, out var versionVar) ? versionVar : ApplicationVersion.Empty : ApplicationVersion.Empty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,20 @@ namespace Bannerlord::ModuleManager
const auto result = bmm_get_module_info_with_path(sourceCopy.get(), pathCopy.get());
return ThrowOrReturnJson(env, result);
}
Value GetModuleInfoWithMetadata(const CallbackInfo &info)
{
const auto env = info.Env();
const auto source = info[0].As<String>();
const auto moduleProvider = info[1].As<String>();
const auto path = info[2].As<String>();

const auto sourceCopy = CopyWithFree(source.Utf16Value());
const auto moduleProviderCopy = CopyWithFree(moduleProvider.Utf16Value());
const auto pathCopy = CopyWithFree(path.Utf16Value());

const auto result = bmm_get_module_info_with_metadata(sourceCopy.get(), moduleProviderCopy.get(), pathCopy.get());
return ThrowOrReturnJson(env, result);
}
Value GetSubModuleInfo(const CallbackInfo &info)
{
const auto env = info.Env();
Expand Down Expand Up @@ -366,6 +380,7 @@ namespace Bannerlord::ModuleManager

exports.Set("getModuleInfo", Function::New(env, GetModuleInfo));
exports.Set("getModuleInfoWithPath", Function::New(env, GetModuleInfoWithPath));
exports.Set("getModuleInfoWithMetadata", Function::New(env, GetModuleInfoWithMetadata));
exports.Set("getSubModuleInfo", Function::New(env, GetSubModuleInfo));

exports.Set("parseApplicationVersion", Function::New(env, ParseApplicationVersion));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ export interface ModuleInfoExtended {
export interface ModuleInfoExtendedWithPath extends ModuleInfoExtended {
path: string;
}
export interface ModuleInfoExtendedWithMetadata extends ModuleInfoExtendedWithPath {
moduleProviderType: ModuleProviderType;
}
export enum ModuleProviderType {
Default = 'Default',
Steam = 'Steam',
}
export interface ApplicationVersion {
applicationVersionType: ApplicationVersionType;
major: number;
Expand All @@ -28,7 +35,7 @@ export enum ApplicationVersionType {
EarlyAccess = 'EarlyAccess',
Release = 'Release',
Development = 'Development',
Invalid = 'Invalid'
Invalid = 'Invalid',
}
export interface SubModuleInfoExtended {
name: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Bannerlord.LauncherManager.External.UI;
using Bannerlord.LauncherManager.External.UI;
using Bannerlord.LauncherManager.Models;

using BUTR.NativeAOT.Shared;
Expand All @@ -25,7 +25,7 @@ public void SendDialog(DialogType type, string title, string message, IReadOnlyL
{
SendDialogNative(type.ToStringFast().ToLowerInvariant(), title, message, filters, onResult);
}

private void SendDialogNative(ReadOnlySpan<char> type, ReadOnlySpan<char> title, ReadOnlySpan<char> message, IReadOnlyList<DialogFileFilter> filters, Action<string> onResult)
{
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Bannerlord.LauncherManager.External;
using Bannerlord.LauncherManager.External;

using BUTR.NativeAOT.Shared;

Expand All @@ -13,7 +13,7 @@ internal sealed unsafe class FileSystemProvider : IFileSystemProvider
private readonly N_WriteFileContentDelegate _writeFileContent;
private readonly N_ReadDirectoryFileList _readDirectoryFileList;
private readonly N_ReadDirectoryList _readDirectoryList;

public FileSystemProvider(
param_ptr* pOwner,
N_ReadFileContentDelegate readFileContent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public GameInfoProvider(param_ptr* pOwner, N_GetInstallPathDelegate getInstallPa
_pOwner = pOwner;
_getInstallPath = getInstallPath;
}

public string GetInstallPath() => GetInstallPathNative();

private string GetInstallPathNative()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public LauncherStateProvider(
_getOptions = getOptions;
_getState = getState;
}

public void SetGameParameters(string executable, IReadOnlyList<string> gameParameters) => SetGameParametersNative(executable, gameParameters);
public LauncherOptions GetOptions() => GetOptionsNative();
public LauncherState GetState() => GetStateNative();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Bannerlord.LauncherManager.External;
using Bannerlord.LauncherManager.External;
using Bannerlord.LauncherManager.Models;

using BUTR.NativeAOT.Shared;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ public LoadOrderStateProvider(
_getModuleViewModels = getModuleViewModels;
_setModuleViewModels = setModuleViewModels;
}

public IModuleViewModel[]? GetAllModuleViewModels() => GetAllModuleViewModelsNative();
public IModuleViewModel[]? GetModuleViewModels() => GetModuleViewModelsNative();
public void SetModuleViewModels(IReadOnlyList<IModuleViewModel> moduleViewModels) => SetModuleViewModelsNative(moduleViewModels);


private IModuleViewModel[]? GetAllModuleViewModelsNative()
{
Logger.LogInput();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Bannerlord.LauncherManager.External.UI;
using Bannerlord.LauncherManager.External.UI;
using Bannerlord.LauncherManager.Models;

using BUTR.NativeAOT.Shared;
Expand Down
4 changes: 2 additions & 2 deletions src/Bannerlord.LauncherManager.Native/Bindings.Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ public override void Write(Utf8JsonWriter writer, IInstallInstruction value, Jso
new JsonStringEnumConverter<GameStore>(),
new JsonStringEnumConverter<InstallInstructionType>(),
new JsonStringEnumConverter<NotificationType>(),

new JsonStringEnumConverter<ApplicationVersionType>(),
new JsonStringEnumConverter<ModuleIssueType>(),
new JsonStringEnumConverter<LoadType>(),
}
};

internal static readonly SourceGenerationContext CustomSourceGenerationContext = new(_options);
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static unsafe partial class Bindings
Marshal.GetDelegateForFunctionPointer<N_SetModuleViewModels>(new IntPtr(p_set_module_view_models))
)
);

Logger.LogOutput();
return return_value_ptr.AsValue(launcherManager.HandlePtr, false);
}
Expand Down Expand Up @@ -180,7 +180,7 @@ public static unsafe partial class Bindings
return return_value_json.AsError(BUTR.NativeAOT.Shared.Utils.Copy("Handler is null or wrong!", false), false);

var files = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_files, CustomSourceGenerationContext.StringArray);
var moduleInfos = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_module_infos, CustomSourceGenerationContext.ModuleInfoExtendedWithPathArray);
var moduleInfos = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_module_infos, CustomSourceGenerationContext.ModuleInfoExtendedWithMetadataArray);

var result = handler.InstallModuleContent(files, moduleInfos);
Logger.LogOutput(result);
Expand Down Expand Up @@ -336,7 +336,7 @@ public static unsafe partial class Bindings
var result = handler.GetModules().ToArray();

Logger.LogOutput(result);
return return_value_json.AsValue(result, CustomSourceGenerationContext.ModuleInfoExtendedWithPathArray, false);
return return_value_json.AsValue(result, CustomSourceGenerationContext.ModuleInfoExtendedWithMetadataArray, false);
}
catch (Exception e)
{
Expand Down Expand Up @@ -507,7 +507,7 @@ public static unsafe partial class Bindings

var moduleViewModel = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_module_view_model, CustomSourceGenerationContext.ModuleViewModel);

var modules = handler.GetModuleViewModels() ?? Array.Empty<IModuleViewModel>();
var modules = handler.GetModuleViewModels() ?? [];
var lookup = modules.ToDictionary(x => x.ModuleInfoExtended.Id, x => x);
SortHelper.ToggleModuleSelection(modules, lookup, moduleViewModel);

Expand Down Expand Up @@ -549,7 +549,7 @@ public static unsafe partial class Bindings
}
}


[UnmanagedCallersOnly(EntryPoint = "ve_get_save_files", CallConvs = [typeof(CallConvCdecl)])]
public static return_value_json* GetSaveFiles(param_ptr* p_handle)
{
Expand Down Expand Up @@ -742,7 +742,7 @@ public record OrderByLoadOrderResult(bool Result, IReadOnlyList<string>? Issues,
return return_value_void.AsException(e, false);
}
}


[UnmanagedCallersOnly(EntryPoint = "ve_set_game_store", CallConvs = [typeof(CallConvCdecl)])]
public static return_value_void* SetGameStore(param_ptr* p_handle, param_string* p_game_store)
Expand All @@ -767,7 +767,7 @@ public record OrderByLoadOrderResult(bool Result, IReadOnlyList<string>? Issues,
return return_value_void.AsException(e, false);
}
}

[UnmanagedCallersOnly(EntryPoint = "ve_get_game_platform", CallConvs = [typeof(CallConvCdecl)])]
public static return_value_string* GetGamePlatform(param_ptr* p_handle)
{
Expand Down
23 changes: 22 additions & 1 deletion src/Bannerlord.LauncherManager.Native/Bindings.ModuleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,28 @@ public static unsafe partial class Bindings
doc.LoadXml(new string(param_string.ToSpan(p_xml_content)));

var module = ModuleInfoExtended.FromXml(doc);
var result = new ModuleInfoExtendedWithPath(module, new string(param_string.ToSpan(p_path)));
var result = new ModuleInfoExtendedWithMetadata(module, ModuleProviderType.Default, new string(param_string.ToSpan(p_path)));

Logger.LogOutput(result);
return return_value_json.AsValue(result, CustomSourceGenerationContext.ModuleInfoExtended, false);
}
catch (Exception e)
{
Logger.LogException(e);
return return_value_json.AsException(e, false);
}
}
[UnmanagedCallersOnly(EntryPoint = "bmm_get_module_info_with_metadata", CallConvs = [typeof(CallConvCdecl)]), IsNotConst<IsPtrConst>]
public static return_value_json* GetModuleInfoWithMetadata([IsConst<IsPtrConst>] param_string* p_xml_content, [IsConst<IsPtrConst>] param_string* p_module_provider, [IsConst<IsPtrConst>] param_string* p_path)
{
Logger.LogInput(p_xml_content, p_path);
try
{
var doc = new XmlDocument();
doc.LoadXml(new string(param_string.ToSpan(p_xml_content)));

var module = ModuleInfoExtended.FromXml(doc);
var result = new ModuleInfoExtendedWithMetadata(module, Enum.TryParse<ModuleProviderType>(new string(param_string.ToSpan(p_path)), out var e) ? e : ModuleProviderType.Default, new string(param_string.ToSpan(p_path)));

Logger.LogOutput(result);
return return_value_json.AsValue(result, CustomSourceGenerationContext.ModuleInfoExtended, false);
Expand Down
2 changes: 1 addition & 1 deletion src/Bannerlord.LauncherManager.Native/CallbackStorage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;

namespace Bannerlord.LauncherManager.Native;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ public void Dispose()
public new void SetGameParameterLoadOrder(LoadOrder loadOrder) => base.SetGameParameterLoadOrder(loadOrder);


public new IReadOnlyList<ModuleInfoExtendedWithPath> GetModules() => base.GetModules();
public new IReadOnlyList<ModuleInfoExtendedWithMetadata> GetModules() => base.GetModules();

public new IModuleViewModel[]? GetModuleViewModels() => base.GetModuleViewModels();

public new void ShowHint(BUTRTextObject message) => base.ShowHint(message);
public new void ShowHint(string message) => base.ShowHint(message);

public new void SendDialog(DialogType type, string title, string message, IReadOnlyList<DialogFileFilter> filters, Action<string> onResult) => base.SendDialog(type, title, message, filters, onResult);

public override SaveMetadata? GetSaveMetadata(string fileName, ReadOnlySpan<byte> data)
Expand All @@ -95,7 +95,7 @@ public override SaveMetadata[] GetSaveFiles()
{
IEnumerable<SaveMetadata> GetSaveFilesInternal()
{
foreach (var filePath in ReadDirectoryFileList(SavePath) ?? Array.Empty<string>())
foreach (var filePath in ReadDirectoryFileList(SavePath) ?? [])
{
if (ReadFileContent(filePath, 0, 4) is not { } lengthData) continue;
var length = BitConverter.ToInt32(lengthData, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Bannerlord.LauncherManager.Native.Models;

public sealed record ModuleViewModel : IModuleViewModel
{
public required ModuleInfoExtendedWithPath ModuleInfoExtended { get; init; }
public required ModuleInfoExtendedWithMetadata ModuleInfoExtended { get; init; }
public required bool IsValid { get; init; }
public bool IsSelected { get; set; }
public bool IsDisabled { get; set; }
Expand All @@ -16,7 +16,7 @@ public sealed record ModuleViewModel : IModuleViewModel
public ModuleViewModel() { }

[SetsRequiredMembers, JsonConstructor]
public ModuleViewModel(ModuleInfoExtendedWithPath moduleInfoExtended, bool isValid)
public ModuleViewModel(ModuleInfoExtendedWithMetadata moduleInfoExtended, bool isValid)
{
ModuleInfoExtended = moduleInfoExtended;
IsValid = isValid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ namespace Bannerlord.LauncherManager.Native;
[JsonSerializable(typeof(SubModuleInfoExtended))]
[JsonSerializable(typeof(ModuleInfoExtended))]
[JsonSerializable(typeof(ModuleInfoExtended[]))]
[JsonSerializable(typeof(ModuleInfoExtendedWithPath))]
[JsonSerializable(typeof(ModuleInfoExtendedWithPath[]))]
[JsonSerializable(typeof(ModuleInfoExtendedWithMetadata))]
[JsonSerializable(typeof(ModuleInfoExtendedWithMetadata[]))]
[JsonSerializable(typeof(ModuleSorterOptions))]
[JsonSerializable(typeof(ModuleIssue[]))]
[JsonSerializable(typeof(DependentModuleMetadata[]))]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;

namespace Bannerlord.LauncherManager.External;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;

namespace Bannerlord.LauncherManager.External;

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

using System;
using System.Collections.Generic;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Bannerlord.LauncherManager.Models;
using Bannerlord.LauncherManager.Models;

using System;

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

using System;
using System.Collections.Generic;
Expand Down
Loading

0 comments on commit 433ed7c

Please sign in to comment.