Skip to content

Commit

Permalink
Merge pull request #273 from SpaceWarpDev/1.6
Browse files Browse the repository at this point in the history
1.6 -> Main For Possible SW 1.6
  • Loading branch information
cheese3660 authored Dec 3, 2023
2 parents d0020c2 + 65cf427 commit d7f4545
Show file tree
Hide file tree
Showing 22 changed files with 545 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<SpaceWarpVersion>1.5.4</SpaceWarpVersion>
<SpaceWarpVersion>1.6.0</SpaceWarpVersion>
<TargetFramework>netstandard2.1</TargetFramework>
<RootNamespace>SpaceWarp</RootNamespace>
<LangVersion>11</LangVersion>
Expand Down
22 changes: 19 additions & 3 deletions SpaceWarp.Core/API/Configuration/BepInExConfigEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,23 @@ namespace SpaceWarp.API.Configuration;
public class BepInExConfigEntry : IConfigEntry
{
public readonly ConfigEntryBase EntryBase;

public BepInExConfigEntry(ConfigEntryBase entryBase)
public event Action<object, object> Callbacks;
public BepInExConfigEntry(ConfigEntryBase entryBase, IValueConstraint constraint = null)
{
EntryBase = entryBase;
Constraint = constraint;
}

public object Value
{
get => EntryBase.BoxedValue;
set => EntryBase.BoxedValue = value;
set
{
Callbacks?.Invoke(EntryBase.BoxedValue, value);
EntryBase.BoxedValue = value;
}
}

public Type ValueType => EntryBase.SettingType;

public T Get<T>() where T : class
Expand All @@ -38,8 +44,18 @@ public void Set<T>(T value)
throw new InvalidCastException($"Cannot cast {ValueType} to {typeof(T)}");
}

if (Constraint != null)
{
if (!Constraint.IsConstrained(value)) return;
}
Callbacks?.Invoke(EntryBase.BoxedValue, value);
EntryBase.BoxedValue = Convert.ChangeType(value, ValueType);
}

public string Description => EntryBase.Description.Description;
public IValueConstraint Constraint { get; }
public void RegisterCallback(Action<object, object> valueChangedCallback)
{
Callbacks += valueChangedCallback;
}
}
6 changes: 6 additions & 0 deletions SpaceWarp.Core/API/Configuration/BepInExConfigFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public IConfigEntry Bind<T>(string section, string key, T defaultValue = default
return new BepInExConfigEntry(AdaptedConfigFile.Bind(section, key, defaultValue, description));
}

public IConfigEntry Bind<T>(string section, string key, T defaultValue, string description, IValueConstraint valueConstraint)
{
return new BepInExConfigEntry(AdaptedConfigFile.Bind(new ConfigDefinition(section, key), defaultValue,
new ConfigDescription(description, valueConstraint.ToAcceptableValueBase())));
}

public IReadOnlyList<string> Sections => AdaptedConfigFile.Keys.Select(x => x.Section).Distinct().ToList();

public IReadOnlyList<string> this[string section] => AdaptedConfigFile.Keys.Where(x => x.Section == section)
Expand Down
17 changes: 16 additions & 1 deletion SpaceWarp.Core/API/Configuration/ConfigValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ public ConfigValue(IConfigEntry entry)
public T Value
{
get => (T)Entry.Value;
set => Entry.Value = value;
set
{
Entry.Value = value;
}
}

public void RegisterCallback(Action<T, T> callback)
{
// Callbacks += callback;
Entry.RegisterCallback(NewCallback);
return;

void NewCallback(object from, object to)
{
callback.Invoke((T)from, (T)to);
}
}
}
5 changes: 5 additions & 0 deletions SpaceWarp.Core/API/Configuration/EmptyConfigFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public IConfigEntry Bind<T>(string section, string key, T defaultValue = default
throw new System.NotImplementedException();
}

public IConfigEntry Bind<T>(string section, string key, T defaultValue, string description, IValueConstraint valueConstraint)
{
throw new System.NotImplementedException();
}

public IReadOnlyList<string> Sections => new List<string>();

public IReadOnlyList<string> this[string section] => throw new KeyNotFoundException($"{section}");
Expand Down
8 changes: 8 additions & 0 deletions SpaceWarp.Core/API/Configuration/IConfigEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ public interface IConfigEntry
public void Set<T>(T value);

public string Description { get; }

public IValueConstraint Constraint { get; }

/// <summary>
/// Called when setting the value on a config file
/// </summary>
/// <param name="valueChangedCallback">An action that takes te old value and the new value and calls a callback</param>
public void RegisterCallback(Action<object, object> valueChangedCallback);
}
5 changes: 3 additions & 2 deletions SpaceWarp.Core/API/Configuration/IConfigFile.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using JetBrains.Annotations;

namespace SpaceWarp.API.Configuration;
Expand All @@ -11,7 +12,7 @@ public interface IConfigFile
public IConfigEntry this[string section, string key] { get; }

public IConfigEntry Bind<T>(string section, string key, T defaultValue = default, string description = "");

public IReadOnlyList<string> Sections { get; }
public IReadOnlyList<string> this[string section] { get; }
}
9 changes: 9 additions & 0 deletions SpaceWarp.Core/API/Configuration/IValueConstraint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using BepInEx.Configuration;

namespace SpaceWarp.API.Configuration;

public interface IValueConstraint
{
public bool IsConstrained(object o);
public AcceptableValueBase ToAcceptableValueBase();
}
16 changes: 14 additions & 2 deletions SpaceWarp.Core/API/Configuration/JsonConfigEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ public class JsonConfigEntry : IConfigEntry
{
private readonly JsonConfigFile _configFile;
private object _value;
public event Action<object, object> Callbacks;


public JsonConfigEntry(JsonConfigFile configFile, Type type, string description, object value)
public JsonConfigEntry(JsonConfigFile configFile, Type type, string description, object value, IValueConstraint constraint = null)
{
_configFile = configFile;
_value = value;
Constraint = constraint;
Description = description;
ValueType = type;
}
Expand All @@ -23,6 +26,7 @@ public object Value
get => _value;
set
{
Callbacks?.Invoke(_value, value);
_value = value;
_configFile.Save();
}
Expand All @@ -45,9 +49,17 @@ public void Set<T>(T value)
{
throw new InvalidCastException($"Cannot cast {ValueType} to {typeof(T)}");
}

if (Constraint != null)
{
if (!Constraint.IsConstrained(value)) return;
}
Value = Convert.ChangeType(value, ValueType);
}

public string Description { get; }
public IValueConstraint Constraint { get; }
public void RegisterCallback(Action<object, object> valueChangedCallback)
{
Callbacks += valueChangedCallback;
}
}
16 changes: 8 additions & 8 deletions SpaceWarp.Core/API/Configuration/JsonConfigFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class JsonConfigFile : IConfigFile
{
[CanBeNull] private JObject _previousConfigObject;

private Dictionary<string, Dictionary<string, JsonConfigEntry>> _currentEntries = new();
internal Dictionary<string, Dictionary<string, JsonConfigEntry>> CurrentEntries = new();
private readonly string _file;

public JsonConfigFile(string file)
Expand All @@ -40,11 +40,11 @@ public JsonConfigFile(string file)

public void Save()
{
if (!_currentEntries.Any(value => value.Value.Count > 0)) return;
if (!CurrentEntries.Any(value => value.Value.Count > 0)) return;
var result = new StringBuilder();
result.AppendLine("{");
var hadPreviousSection = false;
foreach (var section in _currentEntries.Where(section => section.Value.Count > 0))
foreach (var section in CurrentEntries.Where(section => section.Value.Count > 0))
{
hadPreviousSection = DumpSection(hadPreviousSection, result, section);
}
Expand Down Expand Up @@ -128,15 +128,15 @@ private static bool DumpEntry(StringBuilder result, bool hadPreviousKey, KeyValu
return true;
}

public IConfigEntry this[string section, string key] => _currentEntries[section][key];
public IConfigEntry this[string section, string key] => CurrentEntries[section][key];

public IConfigEntry Bind<T>(string section, string key, T defaultValue = default, string description = "")
{
// So now we have to check if its already bound, and/or if the previous config object has it
if (!_currentEntries.TryGetValue(section, out var previousSection))
if (!CurrentEntries.TryGetValue(section, out var previousSection))
{
previousSection = new Dictionary<string, JsonConfigEntry>();
_currentEntries.Add(section,previousSection);
CurrentEntries.Add(section,previousSection);
}

if (previousSection.TryGetValue(key, out var result))
Expand Down Expand Up @@ -173,7 +173,7 @@ public IConfigEntry Bind<T>(string section, string key, T defaultValue = default
return previousSection[key];
}

public IReadOnlyList<string> Sections => _currentEntries.Keys.ToList();
public IReadOnlyList<string> Sections => CurrentEntries.Keys.ToList();

public IReadOnlyList<string> this[string section] => _currentEntries[section].Keys.ToList();
public IReadOnlyList<string> this[string section] => CurrentEntries[section].Keys.ToList();
}
47 changes: 47 additions & 0 deletions SpaceWarp.Core/API/Configuration/ListConstraint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Text;
using BepInEx.Configuration;
using JetBrains.Annotations;

namespace SpaceWarp.API.Configuration;

public class ListConstraint<T> : ValueConstraint<T> where T : IEquatable<T>
{
[PublicAPI]
public List<T> AcceptableValues;

public ListConstraint(IEnumerable<T> acceptableValues)
{
AcceptableValues = acceptableValues.ToList();
}

public ListConstraint(params T[] acceptableValues)
{
AcceptableValues = acceptableValues.ToList();
}

public override bool IsValid(T o) => AcceptableValues.Any(x => x.Equals(o));
public override AcceptableValueBase ToAcceptableValueBase()
{
return new AcceptableValueList<T>(AcceptableValues.ToArray());
}

public override string ToString()
{
StringBuilder sb = new();
sb.Append("[");
for (int i = 0; i < AcceptableValues.Count; i++)
{
if (i != 0)
{
sb.Append(", ");
}

sb.Append(AcceptableValues[i]);
}

sb.Append("]");
return sb.ToString();
}
}
30 changes: 30 additions & 0 deletions SpaceWarp.Core/API/Configuration/RangeConstraint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using BepInEx.Configuration;
using JetBrains.Annotations;

namespace SpaceWarp.API.Configuration;

public class RangeConstraint<T> : ValueConstraint<T> where T : IComparable<T>, IComparable
{
[PublicAPI]
public T Minimum;
[PublicAPI]
public T Maximum;

public RangeConstraint(T minimum, T maximum)
{
Minimum = minimum;
Maximum = maximum;
}

public override bool IsValid(T o) => Minimum.CompareTo(o) <= 0 && Maximum.CompareTo(o) >= 0;
public override AcceptableValueBase ToAcceptableValueBase()
{
return new AcceptableValueRange<T>(Minimum, Maximum);
}

public override string ToString()
{
return $"{Minimum} - {Maximum}";
}
}
19 changes: 19 additions & 0 deletions SpaceWarp.Core/API/Configuration/ValueConstraint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using BepInEx.Configuration;

namespace SpaceWarp.API.Configuration;

public abstract class ValueConstraint<T> : IValueConstraint
{
public abstract bool IsValid(T o);
public bool IsValid(object o)
{
return IsValid((T)o);
}

public bool IsConstrained(object o)
{
return IsValid((T)o);
}

public abstract AcceptableValueBase ToAcceptableValueBase();
}
Loading

0 comments on commit d7f4545

Please sign in to comment.