Skip to content

Commit

Permalink
Start of fixing all SpaceWarp warnings, removes all unused imports an…
Browse files Browse the repository at this point in the history
…d adds doc comments to SpaceWarp.Core.API.Assets, SpaceWarp.Core.API.Configuration, SpaceWarp.Core.API.Loading and SpaceWarp.Core.API.Lua namespaces
  • Loading branch information
jan-bures committed Dec 26, 2023
1 parent 9472362 commit 8fb94a1
Show file tree
Hide file tree
Showing 91 changed files with 441 additions and 303 deletions.
8 changes: 4 additions & 4 deletions src/SpaceWarp.Core/API/Assets/AssetManager.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using JetBrains.Annotations;
using JetBrains.Annotations;
using SpaceWarp.API.Lua;
using UnityEngine;
using Logger = BepInEx.Logging.Logger;

namespace SpaceWarp.API.Assets;

/// <summary>
/// Manages all mod assets loaded by SpaceWarp
/// </summary>
[SpaceWarpLuaAPI("Assets")]
[PublicAPI]
public static class AssetManager
Expand Down
32 changes: 29 additions & 3 deletions src/SpaceWarp.Core/API/Configuration/BepInExConfigEntry.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
using System;
using BepInEx.Configuration;
using BepInEx.Configuration;
using JetBrains.Annotations;

namespace SpaceWarp.API.Configuration;

/// <summary>
/// A wrapper around a BepInEx <see cref="ConfigEntryBase"/> to make it compatible with the <see cref="IConfigEntry"/>
/// interface.
/// </summary>
[PublicAPI]
public class BepInExConfigEntry : IConfigEntry
{
/// <summary>
/// The underlying <see cref="ConfigEntryBase"/> that this class wraps.
/// </summary>
public readonly ConfigEntryBase EntryBase;
public event Action<object, object> Callbacks;

/// <summary>
/// The callbacks that are invoked when the value of this entry changes.
/// </summary>
public event Action<object, object> Callbacks;

/// <summary>
/// Creates a new <see cref="BepInExConfigEntry"/> from the given <see cref="ConfigEntryBase"/> and optional
/// <see cref="IValueConstraint"/>.
/// </summary>
/// <param name="entryBase">The <see cref="ConfigEntryBase"/> to wrap.</param>
/// <param name="constraint">The <see cref="IValueConstraint"/> to use.</param>
public BepInExConfigEntry(ConfigEntryBase entryBase, IValueConstraint constraint = null)
{
EntryBase = entryBase;
Constraint = constraint;
}

/// <inheritdoc />
public object Value
{
get => EntryBase.BoxedValue;
Expand All @@ -25,8 +43,10 @@ public object Value
}
}

/// <inheritdoc />
public Type ValueType => EntryBase.SettingType;

/// <inheritdoc />
public T Get<T>() where T : class
{
if (!typeof(T).IsAssignableFrom(ValueType))
Expand All @@ -37,6 +57,7 @@ public T Get<T>() where T : class
return Value as T;
}

/// <inheritdoc />
public void Set<T>(T value)
{
if (!ValueType.IsAssignableFrom(typeof(T)))
Expand All @@ -52,8 +73,13 @@ public void Set<T>(T value)
EntryBase.BoxedValue = Convert.ChangeType(value, ValueType);
}

/// <inheritdoc />
public string Description => EntryBase.Description.Description;

/// <inheritdoc />
public IValueConstraint Constraint { get; }

/// <inheritdoc />
public void RegisterCallback(Action<object, object> valueChangedCallback)
{
Callbacks += valueChangedCallback;
Expand Down
37 changes: 33 additions & 4 deletions src/SpaceWarp.Core/API/Configuration/BepInExConfigFile.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,69 @@
using System.Collections.Generic;
using BepInEx.Configuration;
using BepInEx.Configuration;
using JetBrains.Annotations;

namespace SpaceWarp.API.Configuration;

/// <summary>
/// A wrapper around BepInEx's <see cref="ConfigFile" /> to make it implement <see cref="IConfigFile" />.
/// </summary>
[PublicAPI]
public class BepInExConfigFile : IConfigFile
{

/// <summary>
/// The underlying <see cref="ConfigFile" /> instance.
/// </summary>
public readonly ConfigFile AdaptedConfigFile;

/// <summary>
/// Creates a new <see cref="BepInExConfigFile" /> instance.
/// </summary>
/// <param name="adaptedConfigFile">The <see cref="ConfigFile" /> instance to wrap.</param>
public BepInExConfigFile(ConfigFile adaptedConfigFile)
{
AdaptedConfigFile = adaptedConfigFile;
}

/// <inheritdoc />
public void Save()
{
AdaptedConfigFile.Save();
}

/// <inheritdoc />
public IConfigEntry this[string section, string key] => new BepInExConfigEntry(AdaptedConfigFile[section, key]);

/// <inheritdoc />
public IConfigEntry Bind<T>(string section, string key, T defaultValue = default, string description = "")
{
return new BepInExConfigEntry(AdaptedConfigFile.Bind(section, key, defaultValue, description));
}

public IConfigEntry Bind<T>(string section, string key, T defaultValue, string description, IValueConstraint valueConstraint)
/// <summary>
/// Binds a new config entry to the given section and key with the given default value and description.
/// </summary>
/// <param name="section">Section to bind the entry to.</param>
/// <param name="key">Key to bind the entry to.</param>
/// <param name="defaultValue">Default value of the entry.</param>
/// <param name="description">Description of the entry.</param>
/// <param name="valueConstraint">Value constraint of the entry.</param>
/// <typeparam name="T">Type of the entry.</typeparam>
/// <returns>The newly bound config entry.</returns>
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())));
}

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

/// <inheritdoc />
public IReadOnlyList<string> this[string section] => AdaptedConfigFile.Keys.Where(x => x.Section == section)
.Select(x => x.Key).ToList();
}
29 changes: 23 additions & 6 deletions src/SpaceWarp.Core/API/Configuration/ConfigValue.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
using System;
using JetBrains.Annotations;
using JetBrains.Annotations;

namespace SpaceWarp.API.Configuration;

/// <summary>
/// A wrapper around <see cref="IConfigEntry"/> that provides type safety.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
[PublicAPI]
public class ConfigValue<T>
{
/// <summary>
/// The underlying <see cref="IConfigEntry"/>.
/// </summary>
public IConfigEntry Entry;

/// <summary>
/// Creates a new <see cref="ConfigValue{T}"/> from an <see cref="IConfigEntry"/>.
/// </summary>
/// <param name="entry">The entry to wrap.</param>
/// <exception cref="ArgumentException">
/// If the type of <paramref name="entry"/> does not match <typeparamref name="T"/>.
/// </exception>
public ConfigValue(IConfigEntry entry)
{
Entry = entry;
Expand All @@ -17,15 +30,19 @@ public ConfigValue(IConfigEntry entry)
}
}

/// <summary>
/// The value of the entry.
/// </summary>
public T Value
{
get => (T)Entry.Value;
set
{
Entry.Value = value;
}
set => Entry.Value = value;
}

/// <summary>
/// Registers a callback that will be invoked when the value changes.
/// </summary>
/// <param name="callback">The callback to invoke.</param>
public void RegisterCallback(Action<T, T> callback)
{
// Callbacks += callback;
Expand Down
34 changes: 29 additions & 5 deletions src/SpaceWarp.Core/API/Configuration/EmptyConfigFile.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,52 @@
using System.Collections.Generic;
using JetBrains.Annotations;
using JetBrains.Annotations;

namespace SpaceWarp.API.Configuration;

/// <summary>
/// An empty config file that does not save anything.
/// </summary>
[PublicAPI]
public class EmptyConfigFile : IConfigFile
{
/// <inheritdoc />
public void Save()
{
}

/// <inheritdoc />
public IConfigEntry this[string section, string key] => throw new KeyNotFoundException($"{section}/{key}");

/// <inheritdoc />
public IConfigEntry Bind<T>(string section, string key, T defaultValue = default, string description = "")
{
throw new System.NotImplementedException();
throw new NotImplementedException();
}

public IConfigEntry Bind<T>(string section, string key, T defaultValue, string description, IValueConstraint valueConstraint)
/// <summary>
/// Binds a config entry to a section and key.
/// </summary>
/// <param name="section">Section to bind to.</param>
/// <param name="key">Key to bind to.</param>
/// <param name="defaultValue">Default value to use if no value is found.</param>
/// <param name="description">Description of the config entry.</param>
/// <param name="valueConstraint">Constraint to use for the value.</param>
/// <typeparam name="T">Type of the value.</typeparam>
/// <returns>The config entry.</returns>
/// <exception cref="NotImplementedException">Always thrown.</exception>
public IConfigEntry Bind<T>(
string section,
string key,
T defaultValue,
string description,
IValueConstraint valueConstraint
)
{
throw new System.NotImplementedException();
throw new NotImplementedException();
}

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

/// <inheritdoc />
public IReadOnlyList<string> this[string section] => throw new KeyNotFoundException($"{section}");
}
39 changes: 34 additions & 5 deletions src/SpaceWarp.Core/API/Configuration/IConfigEntry.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,52 @@
using System;
using JetBrains.Annotations;
using JetBrains.Annotations;

namespace SpaceWarp.API.Configuration;

/// <summary>
/// Represents a config entry
/// </summary>
[PublicAPI]
public interface IConfigEntry
{
/// <summary>
/// The value of the config entry
/// </summary>
public object Value { get; set; }

/// <summary>
/// The type of the value of the config entry
/// </summary>
public Type ValueType { get; }

/// <summary>
/// Gets the value of the config entry as a specific type
/// </summary>
/// <typeparam name="T">The type to cast to</typeparam>
/// <returns>The value as the specified type</returns>
public T Get<T>() where T : class;

/// <summary>
/// Sets the value of the config entry
/// </summary>
/// <param name="value">The value to set</param>
/// <typeparam name="T">The type of the value</typeparam>
public void Set<T>(T value);

/// <summary>
/// The description of the config entry
/// </summary>
public string Description { get; }

/// <summary>
/// The value constraint of the config entry
/// </summary>
public IValueConstraint Constraint { get; }

/// <summary>
/// Called when setting the value on a config file
/// Registers a callback to be 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>
/// <param name="valueChangedCallback">
/// An action that takes the old value and the new value and calls a callback
/// </param>
public void RegisterCallback(Action<object, object> valueChangedCallback);
}
34 changes: 30 additions & 4 deletions src/SpaceWarp.Core/API/Configuration/IConfigFile.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,44 @@
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using JetBrains.Annotations;

namespace SpaceWarp.API.Configuration;

/// <summary>
/// Represents a configuration file.
/// </summary>
[PublicAPI]
public interface IConfigFile
{
/// <summary>
/// Saves the configuration file.
/// </summary>
public void Save();

/// <summary>
/// Gets the <see cref="IConfigEntry" /> with the specified section and key.
/// </summary>
/// <param name="section">Section of the entry.</param>
/// <param name="key">Key of the entry.</param>
public IConfigEntry this[string section, string key] { get; }

/// <summary>
/// Binds a new <see cref="IConfigEntry" /> to the specified section and key.
/// </summary>
/// <param name="section">Section of the entry.</param>
/// <param name="key">Key of the entry.</param>
/// <param name="defaultValue">Default value of the entry.</param>
/// <param name="description">Description of the entry.</param>
/// <typeparam name="T">Type of the entry.</typeparam>
/// <returns>The bound <see cref="IConfigEntry" />.</returns>
public IConfigEntry Bind<T>(string section, string key, T defaultValue = default, string description = "");


/// <summary>
/// A list of all sections in the configuration file.
/// </summary>
public IReadOnlyList<string> Sections { get; }

/// <summary>
/// A list of all keys in the specified section.
/// </summary>
/// <param name="section"></param>
public IReadOnlyList<string> this[string section] { get; }
}
Loading

0 comments on commit 8fb94a1

Please sign in to comment.