Skip to content

Commit

Permalink
Fixed settings saving and did some renaming
Browse files Browse the repository at this point in the history
Added UIVersion property
Added other overrides to WrapperSettings
  • Loading branch information
Aragas committed Apr 16, 2020
1 parent 28b706a commit 662910b
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 17 deletions.
4 changes: 2 additions & 2 deletions GUI/v1a/ViewModels/ModSettingsScreenVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,15 @@ private void ExecuteRevert()
tuple =>
{
//Do action
tuple.SettingsInstance = SettingsDatabase.ResetSettingsInstance(SelectedMod.SettingsInstance);
tuple.SettingsInstance = SettingsDatabase.ResetSettings(SelectedMod.SettingsInstance.Id);
tuple.VM.RefreshValues();
ExecuteSelect(null);
ExecuteSelect(tuple.VM);
},
tuple =>
{
//Undo action
SettingsDatabase.OverrideSettingsWithId(tuple.SettingsInstance, tuple.SettingsInstance.Id);
SettingsDatabase.OverrideSettings(tuple.SettingsInstance);
tuple.VM.SettingsInstance = tuple.SettingsInstance;
tuple.VM.RefreshValues();
if (SelectedMod == tuple.VM)
Expand Down
6 changes: 5 additions & 1 deletion GUI/v1a/ViewModels/ModSettingsVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ public SettingsBase SettingsInstance
set => ModSettingsDefinition.SettingsInstance = value;
}
public UndoRedoStack URS { get; } = new UndoRedoStack();

/// <summary>
/// XSLT?
/// </summary>
[DataSourceProperty]
public int UIVersion => SettingsInstance.UIVersion;
[DataSourceProperty]
public string ModName => SettingsInstance.ModName;
[DataSourceProperty]
Expand Down
4 changes: 2 additions & 2 deletions Interfaces/ISettingsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface ISettingsProvider
bool RegisterSettings(SettingsBase settingsClass);
SettingsBase? GetSettings(string uniqueId);
void SaveSettings(SettingsBase settingsInstance);
bool OverrideSettingsWithId(SettingsBase settings, string Id);
SettingsBase ResetSettingsInstance(SettingsBase settingsInstance);
bool OverrideSettings(SettingsBase settings);
SettingsBase ResetSettings(string id);
}
}
2 changes: 1 addition & 1 deletion MBOptionScreen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<PackageId>Bannerlord.MBOptionScreen</PackageId>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Version>1.1.0</Version>
<Version>1.1.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions Settings/AttributeSettingsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace MBOptionScreen.Settings
{
public abstract class AttributeSettings<T> : SettingsBase<T> where T : SettingsBase, new()
{
protected override char SubGroupDelimiter => '/';

public override List<SettingPropertyGroupDefinition> GetSettingPropertyGroups()
{
var groups = new List<SettingPropertyGroupDefinition>();
Expand Down
1 change: 1 addition & 0 deletions Settings/SettingsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public abstract class SettingsBase
public abstract string Id { get; set; }
public abstract string ModuleFolderName { get; }
public abstract string ModName { get; }
public virtual int UIVersion => 1;
public virtual string SubFolder => "";
protected virtual char SubGroupDelimiter => '/';

Expand Down
4 changes: 2 additions & 2 deletions Settings/SettingsDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ internal static class SettingsDatabase

public static void SaveSettings(SettingsBase settingsInstance) => SettingsStorage.SaveSettings(settingsInstance);

public static bool OverrideSettingsWithId(SettingsBase settings, string Id) => SettingsStorage.OverrideSettingsWithId(settings, Id);
public static bool OverrideSettings(SettingsBase settings) => SettingsStorage.OverrideSettings(settings);

public static SettingsBase ResetSettingsInstance(SettingsBase settingsInstance) => SettingsStorage.ResetSettingsInstance(settingsInstance);
public static SettingsBase ResetSettings(string id) => SettingsStorage.ResetSettings(id);
}
}
19 changes: 10 additions & 9 deletions Settings/SimpleJsonSettingsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ public bool RegisterSettings(SettingsBase settingsInstance)

LoadedSettings.Add(settingsInstance.Id, settingsInstance);

var path = Path.Combine(_defaultRootFolder, settingsInstance.ModuleFolderName,
$"{settingsInstance.Id}.json");
var path = Path.Combine(_defaultRootFolder, settingsInstance.ModuleFolderName, settingsInstance.SubFolder ?? "", $"{settingsInstance.Id}.json");
var file = new FileInfo(path);
if (file.Exists)
{
Expand Down Expand Up @@ -88,7 +87,7 @@ public void SaveSettings(SettingsBase settingsInstance)
if (settingsInstance == null || !LoadedSettings.ContainsKey(settingsInstance.Id))
return;

var path = Path.Combine(_defaultRootFolder, settingsInstance.ModuleFolderName, $"{settingsInstance.Id}.json");
var path = Path.Combine(_defaultRootFolder, settingsInstance.ModuleFolderName, settingsInstance.SubFolder ?? "", $"{settingsInstance.Id}.json");
var file = new FileInfo(path);

var content = JsonConvert.SerializeObject(settingsInstance, _jsonSerializerSettings);
Expand All @@ -97,22 +96,24 @@ public void SaveSettings(SettingsBase settingsInstance)
writer.Write(content);
}

public bool OverrideSettingsWithId(SettingsBase newSettingsInstance, string id)
public bool OverrideSettings(SettingsBase newSettingsInstance)
{
if (newSettingsInstance == null || !LoadedSettings.ContainsKey(newSettingsInstance.Id))
return false;

LoadedSettings[id] = newSettingsInstance;
LoadedSettings[newSettingsInstance.Id] = newSettingsInstance;
SaveSettings(newSettingsInstance);
return true;
}

public SettingsBase ResetSettingsInstance(SettingsBase settingsInstance)
public SettingsBase ResetSettings(string id)
{
if (settingsInstance == null || !LoadedSettings.ContainsKey(settingsInstance.Id))
if (!LoadedSettings.ContainsKey(id))
return null;

var defaultSettingsInstance = (SettingsBase) Activator.CreateInstance(settingsInstance.GetType());
LoadedSettings[settingsInstance.Id] = defaultSettingsInstance;
var defaultSettingsInstance = (SettingsBase) Activator.CreateInstance(LoadedSettings[id].GetType());
LoadedSettings[id] = defaultSettingsInstance;
SaveSettings(defaultSettingsInstance);
return defaultSettingsInstance;
}

Expand Down
9 changes: 9 additions & 0 deletions Settings/Wrapper/WrapperSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ internal class WrapperSettings : SettingsBase
private PropertyInfo IdProperty { get; }
private PropertyInfo ModuleFolderNameProperty { get; }
private PropertyInfo ModNameProperty { get; }
private PropertyInfo UIVersionProperty { get; }
private PropertyInfo SubFolderProperty { get; }
private PropertyInfo SubGroupDelimiterProperty { get; }


public override string Id { get => (string) IdProperty.GetValue(_object); set => IdProperty.SetValue(_object, value); }
public override string ModuleFolderName => (string) ModuleFolderNameProperty.GetValue(_object);
public override string ModName => (string) ModNameProperty.GetValue(_object);
public override int UIVersion => UIVersionProperty.GetValue(_object) as int? ?? 1;
public override string SubFolder => SubFolderProperty.GetValue(_object) as string ?? "";
protected override char SubGroupDelimiter => SubGroupDelimiterProperty.GetValue(_object) as char? ?? '/';

public WrapperSettings(object @object)
{
Expand All @@ -31,6 +37,9 @@ public WrapperSettings(object @object)
type.GetProperty("ID", BindingFlags.Instance | BindingFlags.Public);
ModuleFolderNameProperty = type.GetProperty("ModuleFolderName", BindingFlags.Instance | BindingFlags.Public);
ModNameProperty = type.GetProperty("ModName", BindingFlags.Instance | BindingFlags.Public);
UIVersionProperty = type.GetProperty("UIVersion", BindingFlags.Instance | BindingFlags.Public);
SubFolderProperty = type.GetProperty("SubFolder", BindingFlags.Instance | BindingFlags.Public);
SubGroupDelimiterProperty = type.GetProperty("SubGroupDelimiter", BindingFlags.Instance | BindingFlags.Public);
}

public override List<SettingPropertyGroupDefinition> GetSettingPropertyGroups()
Expand Down

0 comments on commit 662910b

Please sign in to comment.