Skip to content

Commit

Permalink
Fixed Dropdown related issues
Browse files Browse the repository at this point in the history
Fixed test settings RequireRestart
Fixed v1 override of GetSettings
Changed ApplicationContainerProvider and SynchronizationProvider to use their singleton ability instead of relying on some game's list
Minor re branding
  • Loading branch information
Aragas committed Apr 27, 2020
1 parent c1a148d commit 11d44b3
Show file tree
Hide file tree
Showing 37 changed files with 447 additions and 313 deletions.
4 changes: 3 additions & 1 deletion MBOptionScreen/Actions/ComplexAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ namespace MBOptionScreen.Actions
{
public sealed class ComplexAction<T> : IAction
{
public Ref Context { get; }
public Ref? Context { get; }
public object Value { get; }
public object Original { get; }
public Action<T> DoFunction { get; }
public Action<T> UndoFunction { get; }

public ComplexAction(T value, Action<T> doFunction, Action<T> undoFunction)
{
Value = value!;
Original = value!;
DoFunction = doFunction;
UndoFunction = undoFunction;
}
Expand Down
3 changes: 2 additions & 1 deletion MBOptionScreen/Actions/IAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
{
public interface IAction
{
Ref Context { get; }
Ref? Context { get; }
object Original { get; }
object Value { get; }
void DoAction();
void UndoAction();
Expand Down
4 changes: 2 additions & 2 deletions MBOptionScreen/Actions/Ref.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public class Ref
{
private readonly Func<object> _getter;
private readonly Action<object> _setter;
private readonly PropertyInfo _propInfo;
private readonly object _instance;
public readonly PropertyInfo _propInfo;
public readonly object _instance;

public object Value
{
Expand Down
23 changes: 23 additions & 0 deletions MBOptionScreen/Actions/SetDropdownAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using MBOptionScreen.Data;

using TaleWorlds.Core.ViewModelCollection;

namespace MBOptionScreen.Actions
{
public sealed class SetDropdownAction : IAction
{
public Ref? Context { get; }
public object Value { get; }
public object Original { get; }

public SetDropdownAction(Ref context, SelectorVM<SelectorItemVM> value)
{
Context = context;
Value = value.SelectedIndex;
Original = (Context.Value as IDropdownProvider)!.SelectedIndex;
}

public void DoAction() => (Context!.Value as IDropdownProvider)!.SelectedIndex = (int) Value;
public void UndoAction() => (Context!.Value as IDropdownProvider)!.SelectedIndex = (int) Original;
}
}
11 changes: 5 additions & 6 deletions MBOptionScreen/Actions/SetFloatSettingProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@ namespace MBOptionScreen.Actions
{
public sealed class SetFloatSettingProperty : IAction
{
private readonly float _originalValue;

public Ref Context { get; }
public Ref? Context { get; }
public object Value { get; }
public object Original { get; }
private ISettingPropertyFloatValue SettingProperty { get; }

public SetFloatSettingProperty(ISettingPropertyFloatValue settingProperty, float value)
{
Value = value;
SettingProperty = settingProperty;
_originalValue = SettingProperty.FloatValue;
Original = SettingProperty.FloatValue;
}

public void DoAction() => SettingProperty.FloatValue = (float)Value;
public void UndoAction() => SettingProperty.FloatValue = _originalValue;
public void DoAction() => SettingProperty.FloatValue = (float) Value;
public void UndoAction() => SettingProperty.FloatValue = (float) Original;
}
}
11 changes: 5 additions & 6 deletions MBOptionScreen/Actions/SetIntSettingProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@ namespace MBOptionScreen.Actions
{
public sealed class SetIntSettingProperty : IAction
{
private readonly int _originalValue;

public Ref Context { get; }
public Ref? Context { get; }
public object Value { get; }
public object Original { get; }
private ISettingPropertyIntValue SettingProperty { get; }

public SetIntSettingProperty(ISettingPropertyIntValue settingProperty, int value)
{
Value = value;
SettingProperty = settingProperty;
_originalValue = SettingProperty.IntValue;
Original = SettingProperty.IntValue;
}

public void DoAction() => SettingProperty.IntValue = (int)Value;
public void UndoAction() => SettingProperty.IntValue = _originalValue;
public void DoAction() => SettingProperty.IntValue = (int) Value;
public void UndoAction() => SettingProperty.IntValue = (int) Original;
}
}
19 changes: 19 additions & 0 deletions MBOptionScreen/Actions/SetStringAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace MBOptionScreen.Actions
{
public sealed class SetStringAction : IAction
{
public Ref? Context { get; }
public object Value { get; }
public object Original { get; }

public SetStringAction(Ref context, string value)
{
Context = context;
Value = value!;
Original = Context.Value;
}

public void DoAction() => Context!.Value = Value;
public void UndoAction() => Context!.Value = Original;
}
}
9 changes: 4 additions & 5 deletions MBOptionScreen/Actions/SetStringSettingProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@ namespace MBOptionScreen.Actions
{
public sealed class SetStringSettingProperty : IAction
{
private readonly string _originalValue;

public Ref Context { get; }
public Ref? Context { get; }
public object Value { get; }
public object Original { get; }
private ISettingPropertyStringValue SettingProperty { get; }

public SetStringSettingProperty(ISettingPropertyStringValue settingProperty, string value)
{
Value = value;
SettingProperty = settingProperty;
_originalValue = SettingProperty.StringValue;
Original = SettingProperty.StringValue;
}

public void DoAction() => SettingProperty.StringValue = (string) Value;
public void UndoAction() => SettingProperty.StringValue = _originalValue;
public void UndoAction() => SettingProperty.StringValue = (string) Original;
}
}
19 changes: 0 additions & 19 deletions MBOptionScreen/Actions/SetValueAction.cs

This file was deleted.

21 changes: 21 additions & 0 deletions MBOptionScreen/Actions/SetValueTypeAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace MBOptionScreen.Actions
{

public sealed class SetValueTypeAction<T> : IAction
where T : struct
{
public Ref? Context { get; }
public object Value { get; }
public object Original { get; }

public SetValueTypeAction(Ref context, T value)
{
Context = context;
Value = value!;
Original = Context.Value;
}

public void DoAction() => Context!.Value = Value;
public void UndoAction() => Context!.Value = Original;
}
}
6 changes: 6 additions & 0 deletions MBOptionScreen/Actions/UndoRedoStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public void Do(IAction action)
RedoStack.Clear();
}

public void DoWithoutDo(IAction action)
{
UndoStack.Push(action);
RedoStack.Clear();
}

/// <summary>
/// Calls the Undo function for the top item in the UndoStack. If there is nothing in the stack, does nothing.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,113 +1,42 @@
using HarmonyLib;
using MBOptionScreen.Attributes;

using MBOptionScreen.Attributes;

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

using TaleWorlds.Localization;
using TaleWorlds.MountAndBlade;

using Module = TaleWorlds.MountAndBlade.Module;
using System.Collections.Concurrent;

namespace MBOptionScreen.ApplicationContainer
{
[Version("e1.0.0", 200)]
[Version("e1.0.1", 200)]
[Version("e1.0.2", 200)]
[Version("e1.0.3", 200)]
[Version("e1.0.4", 200)]
[Version("e1.0.5", 200)]
[Version("e1.0.6", 200)]
[Version("e1.0.7", 200)]
[Version("e1.0.8", 200)]
[Version("e1.0.9", 200)]
[Version("e1.0.10", 200)]
[Version("e1.0.11", 200)]
[Version("e1.1.0", 200)]
[Version("e1.2.0", 200)]
[Version("e1.0.0", 201)]
[Version("e1.0.1", 201)]
[Version("e1.0.2", 201)]
[Version("e1.0.3", 201)]
[Version("e1.0.4", 201)]
[Version("e1.0.5", 201)]
[Version("e1.0.6", 201)]
[Version("e1.0.7", 201)]
[Version("e1.0.8", 201)]
[Version("e1.0.9", 201)]
[Version("e1.0.10", 201)]
[Version("e1.0.11", 201)]
[Version("e1.1.0", 201)]
[Version("e1.2.0", 201)]
[Version("e1.2.1", 201)]
[Version("e1.3.0", 201)]
internal sealed class DefaultApplicationContainerProvider : IApplicationContainerProvider
{
private static readonly AccessTools.FieldRef<Module, IList> _initialStateOptions =
AccessTools.FieldRefAccess<Module, IList>("_initialStateOptions");

private readonly List<Container> _containers = new List<Container>();
private static readonly ConcurrentDictionary<string, object> _containers = new ConcurrentDictionary<string, object>();

public object Get(string name)
{
if (Module.CurrentModule.GetInitialStateOptionWithId(name) is object obj)
{
if (!(obj is Container container))
{
var wrapper = new ContainerWrapper(obj);
if (!wrapper.IsCorrect)
return default;
container = wrapper;
}

return container.Value;
}
return default;
return _containers.TryGetValue(name, out var value) ? value : default;
}

public void Set(string name, object value)
{
if (Module.CurrentModule.GetInitialStateOptionWithId(name) is object obj)
{
if (!(obj is Container container))
{
var wrapper = new ContainerWrapper(obj);
if (!wrapper.IsCorrect)
return;
container = wrapper;
}

container.Value = value;
}
else
{
var container = new Container(name, value);
Module.CurrentModule.AddInitialStateOption(container);
_containers.Add(container);
}
_containers.AddOrUpdate(name, value, (n, v) => v);
}

public void Clear()
{
var list = _initialStateOptions(Module.CurrentModule);
foreach (var container in _containers)
list.Remove(container);
_containers.Clear();
}

private class Container : InitialStateOption
{
public object Value { get; internal set; }

public Container(string name, object value) : base(name, new TextObject(""), 1, () => { }, true)
{
Value = value;
}
}
private class ContainerWrapper : Container, IWrapper
{
private static string? GetName(object @object)
{
var propInfo = @object.GetType().GetProperty("Id");
return propInfo?.GetValue(@object) as string;
}
private static object? GetValue(object @object)
{
var propInfo = @object.GetType().GetProperty("Value");
return propInfo?.GetValue(@object) as object;
}

public bool IsCorrect { get; }

public ContainerWrapper(object @object) : base(GetName(@object) ?? "ERROR", GetValue(@object))
{
IsCorrect = Id != "ERROR" && @object.GetType().GetProperty("Value") != null;
}
}
}
}
2 changes: 1 addition & 1 deletion MBOptionScreen/Bannerlord.MBOptionScreen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<Copyright>Copyright © 2020</Copyright>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>Bannerlord.MBOptionScreen</PackageId>
<Version>2.0.3</Version>
<Version>2.0.7</Version>
<PackageProjectUrl>https://github.com/Aragas/Bannerlord.MBOptionScreen</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryUrl>https://github.com/Aragas/Bannerlord.MBOptionScreen</RepositoryUrl>
Expand Down
Loading

0 comments on commit 11d44b3

Please sign in to comment.