Browse Source

Changed config to take PluginMetadata

pull/1/head
DaNike 5 years ago
parent
commit
ba5ddaac75
3 changed files with 37 additions and 34 deletions
  1. +33
    -13
      IPA.Loader/Config/Config.cs
  2. +2
    -0
      IPA.Loader/Config/ConfigProviders/JsonConfigProvider.cs
  3. +2
    -21
      IPA.Loader/Config/ModPrefs.cs

+ 33
- 13
IPA.Loader/Config/Config.cs View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using IPA.Config.ConfigProviders; using IPA.Config.ConfigProviders;
@ -65,6 +66,29 @@ namespace IPA.Config
} }
} }
/// <inheritdoc />
/// <summary>
/// Specifies a preferred config name, instead of using the plugin's name.
/// </summary>
public class NameAttribute : Attribute
{
/// <summary>
/// The name to use for the config.
/// </summary>
// ReSharper disable once UnusedAutoPropertyAccessor.Global
public string Name { get; private set; }
/// <inheritdoc />
/// <summary>
/// Constructs the attribute with a specific name.
/// </summary>
/// <param name="name">the name to use for the config.</param>
public NameAttribute(string name)
{
Name = name;
}
}
private static readonly Dictionary<string, Type> registeredProviders = new Dictionary<string, Type>(); private static readonly Dictionary<string, Type> registeredProviders = new Dictionary<string, Type>();
/// <summary> /// <summary>
@ -96,36 +120,32 @@ namespace IPA.Config
/// <summary> /// <summary>
/// Gets an <see cref="IConfigProvider"/> using the specified list pf preferred config types. /// Gets an <see cref="IConfigProvider"/> using the specified list pf preferred config types.
/// </summary> /// </summary>
/// <param name="filename">the name of the file to associate it with</param>
/// <param name="configName">the name of the mod for this config</param>
/// <param name="extensions">the preferred config types to try to get</param> /// <param name="extensions">the preferred config types to try to get</param>
/// <returns>an <see cref="IConfigProvider"/> of the requested type, or of type JSON.</returns> /// <returns>an <see cref="IConfigProvider"/> of the requested type, or of type JSON.</returns>
public static IConfigProvider GetProviderFor(string filename, params string[] extensions)
public static IConfigProvider GetProviderFor(string configName, params string[] extensions)
{ {
var chosenExt = extensions.FirstOrDefault(s => registeredProviders.ContainsKey(s)) ?? "json"; var chosenExt = extensions.FirstOrDefault(s => registeredProviders.ContainsKey(s)) ?? "json";
var type = registeredProviders[chosenExt]; var type = registeredProviders[chosenExt];
var provider = Activator.CreateInstance(type) as IConfigProvider; var provider = Activator.CreateInstance(type) as IConfigProvider;
if (provider != null) if (provider != null)
{ {
provider.Filename = filename;
provider.Filename = Path.Combine(BeatSaber.UserDataPath, configName);
configProviders.Add(provider.LastModified, provider); configProviders.Add(provider.LastModified, provider);
} }
return provider; return provider;
} }
/// <summary>
/// Gets an <see cref="IConfigProvider"/> using the specified list pf preferred config types.
/// </summary>
/// <param name="filename">the name of the file to associate it with</param>
/// <param name="info">the parameter info to try and get info for</param>
/// <returns>an <see cref="IConfigProvider"/> of the requested type, or of type JSON.</returns>
public static IConfigProvider GetProviderFor(string filename, ParameterInfo info)
internal static IConfigProvider GetProviderFor(string modName, ParameterInfo info)
{ {
var prefs = new string[0]; var prefs = new string[0];
if (info.GetCustomAttribute(typeof(PreferAttribute)) is PreferAttribute prefer)
if (info.GetCustomAttribute<PreferAttribute>() is PreferAttribute prefer)
prefs = prefer.PreferenceOrder; prefs = prefer.PreferenceOrder;
if (info.GetCustomAttribute<NameAttribute>() is NameAttribute name)
modName = name.Name;
return GetProviderFor(filename, prefs);
return GetProviderFor(modName, prefs);
} }
private static Dictionary<IConfigProvider, Action> linkedProviders = private static Dictionary<IConfigProvider, Action> linkedProviders =


+ 2
- 0
IPA.Loader/Config/ConfigProviders/JsonConfigProvider.cs View File

@ -103,6 +103,8 @@ namespace IPA.Config.ConfigProviders
public void Save() public void Save()
{ {
Logger.config.Debug($"Saving file {Filename}.json"); Logger.config.Debug($"Saving file {Filename}.json");
if (!Directory.Exists(Path.GetDirectoryName(Filename)))
Directory.CreateDirectory(Path.GetDirectoryName(Filename) ?? throw new InvalidOperationException());
File.WriteAllText(Filename + ".json", JsonConvert.SerializeObject(jsonObj, Formatting.Indented)); File.WriteAllText(Filename + ".json", JsonConvert.SerializeObject(jsonObj, Formatting.Indented));
HasChanged = false; HasChanged = false;


+ 2
- 21
IPA.Loader/Config/ModPrefs.cs View File

@ -1,8 +1,7 @@
using System; using System;
using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Linq;
using IPA.Loader;
namespace IPA.Config namespace IPA.Config
{ {
@ -93,19 +92,15 @@ namespace IPA.Config
private static ModPrefs _staticInstance; private static ModPrefs _staticInstance;
private static IModPrefs StaticInstance => _staticInstance ?? (_staticInstance = new ModPrefs()); private static IModPrefs StaticInstance => _staticInstance ?? (_staticInstance = new ModPrefs());
// ReSharper disable once IdentifierTypo
internal static Dictionary<IBeatSaberPlugin, ModPrefs> ModPrefss { get; set; } = new Dictionary<IBeatSaberPlugin, ModPrefs>();
private readonly IniFile _instance; private readonly IniFile _instance;
/// <summary> /// <summary>
/// Constructs a ModPrefs object for the provide plugin. /// Constructs a ModPrefs object for the provide plugin.
/// </summary> /// </summary>
/// <param name="plugin">the plugin to get the preferences file for</param> /// <param name="plugin">the plugin to get the preferences file for</param>
public ModPrefs(IBeatSaberPlugin plugin) {
public ModPrefs(PluginLoader.PluginMetadata plugin) {
_instance = new IniFile(Path.Combine(Environment.CurrentDirectory, "UserData", "ModPrefs", _instance = new IniFile(Path.Combine(Environment.CurrentDirectory, "UserData", "ModPrefs",
$"{plugin.Name}.ini")); $"{plugin.Name}.ini"));
ModPrefss.Add(plugin, this);
} }
private ModPrefs() private ModPrefs()
@ -263,18 +258,4 @@ namespace IPA.Config
public static void SetBool(string section, string name, bool value) public static void SetBool(string section, string name, bool value)
=> StaticInstance.SetBool(section, name, value); => StaticInstance.SetBool(section, name, value);
} }
/// <summary>
/// An extension class for IBeatSaberPlugins.
/// </summary>
public static class ModPrefsExtensions {
/// <summary>
/// Gets the ModPrefs object for the provided plugin.
/// </summary>
/// <param name="plugin">the plugin wanting the prefrences</param>
/// <returns>the ModPrefs object</returns>
public static IModPrefs GetModPrefs(this IBeatSaberPlugin plugin) {
return ModPrefs.ModPrefss.First(o => o.Key == plugin).Value;
}
}
} }

Loading…
Cancel
Save