// Made by MarC0 / ManlyMarco
// Copyright 2018 GNU General Public License v3.0
using BepInEx;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using BepInEx.Configuration;
namespace ConfigurationManager
{
///
/// Class representing all data about a setting collected by ConfigurationManager.
///
public abstract class SettingEntryBase
{
///
/// List of values this setting can take
///
public object[] AcceptableValues { get; protected set; }
///
/// Range of the values this setting can take
///
public KeyValuePair AcceptableValueRange { get; protected set; }
///
/// Should the setting be shown as a percentage (only applies to value range settings)
///
public bool? ShowRangeAsPercent { get; protected set; }
///
/// Custom setting draw action
///
public Action CustomDrawer { get; private set; }
///
/// Show this setting in the settings screen at all? If false, don't show.
///
public bool? Browsable { get; protected set; }
///
/// Category the setting is under. Null to be directly under the plugin.
///
public string Category { get; protected set; }
///
/// If set, a "Default" button will be shown next to the setting to allow resetting to default.
///
public object DefaultValue { get; protected set; }
///
/// Optional description shown when hovering over the setting
///
public string Description { get; protected internal set; }
///
/// Name of the setting
///
public virtual string DispName { get; protected internal set; }
///
/// Plugin this setting belongs to
///
public BepInPlugin PluginInfo { get; protected internal set; }
///
/// Only allow showing of the value. False whenever possible by default.
///
public bool? ReadOnly { get; protected set; }
///
/// Type of the variable
///
public abstract Type SettingType { get; }
///
/// Instance of the plugin that owns this setting
///
public BaseUnityPlugin PluginInstance { get; private set; }
///
/// Instance of the that holds this setting.
/// Null if setting is not in a ConfigWrapper.
///
public object Wrapper { get; internal set; }
///
/// Is this setting advanced
///
public bool? IsAdvanced { get; internal set; }
///
/// Get the value of this setting
///
public abstract object Get();
///
/// Set the value of this setting
///
public void Set(object newVal)
{
if (ReadOnly != true)
SetValue(newVal);
}
protected abstract void SetValue(object newVal);
///
/// Custom converter from setting type to string for the textbox
///
public Func ObjToStr { get; internal set; }
///
/// Custom converter from string to setting type for the textbox
///
public Func StrToObj { get; internal set; }
protected void SetFromAttributes(object[] attribs, BaseUnityPlugin pluginInstance)
{
PluginInstance = pluginInstance;
PluginInfo = pluginInstance?.Info.Metadata;
if (attribs == null || attribs.Length == 0) return;
DispName = attribs.OfType().FirstOrDefault()?.DisplayName;
Category = attribs.OfType().FirstOrDefault()?.Category;
Description = attribs.OfType().FirstOrDefault()?.Description;
DefaultValue = attribs.OfType().FirstOrDefault()?.Value;
var acc = attribs.OfType().FirstOrDefault();
if (acc is AcceptableValueListAttribute accList)
AcceptableValues = accList.GetAcceptableValues(pluginInstance);
else if (acc is AcceptableValueRangeAttribute accRange)
{
AcceptableValueRange = new KeyValuePair(accRange.MinValue, accRange.MaxValue);
ShowRangeAsPercent = accRange.ShowAsPercentage;
}
var customSettingDrawAttribute = attribs.OfType().FirstOrDefault();
if (customSettingDrawAttribute != null) CustomDrawer = x => customSettingDrawAttribute.Run(x.PluginInstance);
else CustomDrawer = attribs.OfType>().FirstOrDefault();
bool HasStringValue(string val)
{
return attribs.OfType().Contains(val, StringComparer.OrdinalIgnoreCase);
}
if (HasStringValue("ReadOnly")) ReadOnly = true;
else ReadOnly = attribs.OfType().FirstOrDefault()?.IsReadOnly;
if (HasStringValue("Browsable")) Browsable = true;
else if (HasStringValue("Unbrowsable") || HasStringValue("Hidden")) Browsable = false;
else Browsable = attribs.OfType().FirstOrDefault()?.Browsable;
if (HasStringValue("Advanced")) IsAdvanced = true;
else IsAdvanced = attribs.OfType().FirstOrDefault()?.IsAdvanced;
}
}
}