Browse Source

Revert "Removed ModPrefs (finally, holy crap)"

This reverts commit 3e8388ef6a.
4.0.0-beta
Anairkoen Schno 5 years ago
parent
commit
bc8e407633
6 changed files with 371 additions and 0 deletions
  1. +100
    -0
      IPA.Loader/Config/IniFile.cs
  2. +266
    -0
      IPA.Loader/Config/ModPrefs.cs
  3. +2
    -0
      IPA.Loader/IPA.Loader.csproj
  4. +3
    -0
      IPA.Loader/Loader/PluginInitInjector.cs
  5. BIN
      Refs/Assembly-CSharp.dll
  6. BIN
      Refs/UnityEngine.CoreModule.Net4.dll

+ 100
- 0
IPA.Loader/Config/IniFile.cs View File

@ -0,0 +1,100 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace IPA.Config
{
/// <summary>
/// Create a New INI file to store or load data
/// </summary>
[Obsolete("Jesus, this uses old 16-bit system calls!")]
internal class IniFile
{
[DllImport("KERNEL32.DLL", EntryPoint = "GetPrivateProfileStringW",
SetLastError = true,
CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
private static extern int GetPrivateProfileString(
string lpSection,
string lpKey,
string lpDefault,
StringBuilder lpReturnString,
int nSize,
string lpFileName);
[DllImport("KERNEL32.DLL", EntryPoint = "WritePrivateProfileStringW",
SetLastError = true,
CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
private static extern int WritePrivateProfileString(
string lpSection,
string lpKey,
string lpValue,
string lpFileName);
/*private string _path = "";
public string Path
{
get
{
return _path;
}
set
{
if (!File.Exists(value))
File.WriteAllText(value, "", Encoding.Unicode);
_path = value;
}
}*/
private FileInfo _iniFileInfo;
public FileInfo IniFileInfo {
get => _iniFileInfo;
set {
_iniFileInfo = value;
if (_iniFileInfo.Exists) return;
_iniFileInfo.Directory?.Create();
_iniFileInfo.Create();
}
}
/// <summary>
/// INIFile Constructor.
/// </summary>
/// <PARAM name="iniPath"></PARAM>
public IniFile(string iniPath)
{
IniFileInfo = new FileInfo(iniPath);
//this.Path = INIPath;
}
/// <summary>
/// Write Data to the INI File
/// </summary>
/// <PARAM name="section"></PARAM>
/// Section name
/// <PARAM name="key"></PARAM>
/// Key Name
/// <PARAM name="value"></PARAM>
/// Value Name
public void IniWriteValue(string section, string key, string value)
{
WritePrivateProfileString(section, key, value, IniFileInfo.FullName);
}
/// <summary>
/// Read Data Value From the Ini File
/// </summary>
/// <PARAM name="section"></PARAM>
/// <PARAM name="key"></PARAM>
/// <returns></returns>
public string IniReadValue(string section, string key)
{
const int maxChars = 1023;
StringBuilder result = new StringBuilder(maxChars);
GetPrivateProfileString(section, key, "", result, maxChars, IniFileInfo.FullName);
return result.ToString();
}
}
}

+ 266
- 0
IPA.Loader/Config/ModPrefs.cs View File

@ -0,0 +1,266 @@
using System;
using System.Globalization;
using System.IO;
using IPA.Loader;
#if NET3
using Path = Net3_Proxy.Path;
#endif
namespace IPA.Config
{
/// <summary>
/// Allows to get and set preferences for your mod.
/// </summary>
[Obsolete("Uses IniFile, which uses 16 bit system calls. Use BS Utils INI system for now.")]
public interface IModPrefs
{
/// <summary>
/// Gets a string from the ini.
/// </summary>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="defaultValue">Value that should be used when no value is found.</param>
/// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
/// <returns></returns>
string GetString(string section, string name, string defaultValue = "", bool autoSave = false);
/// <summary>
/// Gets an int from the ini.
/// </summary>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="defaultValue">Value that should be used when no value is found.</param>
/// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
/// <returns></returns>
int GetInt(string section, string name, int defaultValue = 0, bool autoSave = false);
/// <summary>
/// Gets a float from the ini.
/// </summary>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="defaultValue">Value that should be used when no value is found.</param>
/// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
/// <returns></returns>
float GetFloat(string section, string name, float defaultValue = 0f, bool autoSave = false);
/// <summary>
/// Gets a bool from the ini.
/// </summary>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="defaultValue">Value that should be used when no value is found.</param>
/// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
/// <returns></returns>
bool GetBool(string section, string name, bool defaultValue = false, bool autoSave = false);
/// <summary>
/// Checks whether or not a key exists in the ini.
/// </summary>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <returns></returns>
bool HasKey(string section, string name);
/// <summary>
/// Sets a float in the ini.
/// </summary>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="value">Value that should be written.</param>
void SetFloat(string section, string name, float value);
/// <summary>
/// Sets an int in the ini.
/// </summary>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="value">Value that should be written.</param>
void SetInt(string section, string name, int value);
/// <summary>
/// Sets a string in the ini.
/// </summary>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="value">Value that should be written.</param>
void SetString(string section, string name, string value);
/// <summary>
/// Sets a bool in the ini.
/// </summary>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="value">Value that should be written.</param>
void SetBool(string section, string name, bool value);
}
/// <inheritdoc />
/// <summary>
/// Allows to get and set preferences for your mod.
/// </summary>
[Obsolete("Uses IniFile, which uses 16 bit system calls. Use BS Utils INI system for now.")]
public class ModPrefs : IModPrefs
{
private static ModPrefs _staticInstance;
private static IModPrefs StaticInstance => _staticInstance ?? (_staticInstance = new ModPrefs());
private readonly IniFile _instance;
/// <summary>
/// Constructs a ModPrefs object for the provide plugin.
/// </summary>
/// <param name="plugin">the plugin to get the preferences file for</param>
public ModPrefs(PluginLoader.PluginMetadata plugin) {
_instance = new IniFile(Path.Combine(Environment.CurrentDirectory, "UserData", "ModPrefs",
$"{plugin.Name}.ini"));
}
private ModPrefs()
{
_instance = new IniFile(Path.Combine(Environment.CurrentDirectory, "UserData", "modprefs.ini"));
}
string IModPrefs.GetString(string section, string name, string defaultValue, bool autoSave)
{
var value = _instance.IniReadValue(section, name);
if (value != "")
return value;
else if (autoSave)
(this as IModPrefs).SetString(section, name, defaultValue);
return defaultValue;
}
/// <summary>
/// Gets a string from the ini.
/// </summary>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="defaultValue">Value that should be used when no value is found.</param>
/// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
/// <returns></returns>
public static string GetString(string section, string name, string defaultValue = "", bool autoSave = false)
=> StaticInstance.GetString(section, name, defaultValue, autoSave);
int IModPrefs.GetInt(string section, string name, int defaultValue, bool autoSave)
{
if (int.TryParse(_instance.IniReadValue(section, name), out var value))
return value;
else if (autoSave)
(this as IModPrefs).SetInt(section, name, defaultValue);
return defaultValue;
}
/// <summary>
/// Gets an int from the ini.
/// </summary>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="defaultValue">Value that should be used when no value is found.</param>
/// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
/// <returns></returns>
public static int GetInt(string section, string name, int defaultValue = 0, bool autoSave = false)
=> StaticInstance.GetInt(section, name, defaultValue, autoSave);
float IModPrefs.GetFloat(string section, string name, float defaultValue, bool autoSave)
{
if (float.TryParse(_instance.IniReadValue(section, name), out var value))
return value;
else if (autoSave)
(this as IModPrefs).SetFloat(section, name, defaultValue);
return defaultValue;
}
/// <summary>
/// Gets a float from the ini.
/// </summary>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="defaultValue">Value that should be used when no value is found.</param>
/// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
/// <returns></returns>
public static float GetFloat(string section, string name, float defaultValue = 0f, bool autoSave = false)
=> StaticInstance.GetFloat(section, name, defaultValue, autoSave);
bool IModPrefs.GetBool(string section, string name, bool defaultValue, bool autoSave)
{
string sVal = GetString(section, name, null);
if (sVal == "1" || sVal == "0")
{
return sVal == "1";
}
else if (autoSave)
{
(this as IModPrefs).SetBool(section, name, defaultValue);
}
return defaultValue;
}
/// <summary>
/// Gets a bool from the ini.
/// </summary>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="defaultValue">Value that should be used when no value is found.</param>
/// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
/// <returns></returns>
public static bool GetBool(string section, string name, bool defaultValue = false, bool autoSave = false)
=> StaticInstance.GetBool(section, name, defaultValue, autoSave);
bool IModPrefs.HasKey(string section, string name)
{
return _instance.IniReadValue(section, name) != null;
}
/// <summary>
/// Checks whether or not a key exists in the ini.
/// </summary>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <returns></returns>
public static bool HasKey(string section, string name) => StaticInstance.HasKey(section, name);
void IModPrefs.SetFloat(string section, string name, float value)
{
_instance.IniWriteValue(section, name, value.ToString(CultureInfo.InvariantCulture));
}
/// <summary>
/// Sets a float in the ini.
/// </summary>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="value">Value that should be written.</param>
public static void SetFloat(string section, string name, float value)
=> StaticInstance.SetFloat(section, name, value);
void IModPrefs.SetInt(string section, string name, int value)
{
_instance.IniWriteValue(section, name, value.ToString());
}
/// <summary>
/// Sets an int in the ini.
/// </summary>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="value">Value that should be written.</param>
public static void SetInt(string section, string name, int value)
=> StaticInstance.SetInt(section, name, value);
void IModPrefs.SetString(string section, string name, string value)
{
_instance.IniWriteValue(section, name, value);
}
/// <summary>
/// Sets a string in the ini.
/// </summary>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="value">Value that should be written.</param>
public static void SetString(string section, string name, string value)
=> StaticInstance.SetString(section, name, value);
void IModPrefs.SetBool(string section, string name, bool value)
{
_instance.IniWriteValue(section, name, value ? "1" : "0");
}
/// <summary>
/// Sets a bool in the ini.
/// </summary>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="value">Value that should be written.</param>
public static void SetBool(string section, string name, bool value)
=> StaticInstance.SetBool(section, name, value);
}
}

+ 2
- 0
IPA.Loader/IPA.Loader.csproj View File

@ -112,10 +112,12 @@
<Compile Include="PluginInterfaces\BeatSaber\IEnhancedPlugin.cs" />
<Compile Include="PluginInterfaces\BeatSaber\IPlugin.cs" />
<Compile Include="PluginInterfaces\IGenericEnhancedPlugin.cs" />
<Compile Include="Config\IniFile.cs" />
<Compile Include="PluginInterfaces\IPA\IEnhancedPlugin.cs" />
<Compile Include="PluginInterfaces\IPA\IPlugin.cs" />
<Compile Include="Logging\Logger.cs" />
<Compile Include="Logging\LogPrinter.cs" />
<Compile Include="Config\ModPrefs.cs" />
<Compile Include="JsonConverters\SemverRangeConverter.cs" />
<Compile Include="JsonConverters\SemverVersionConverter.cs" />
<Compile Include="Utilities\BeatSaber.cs" />


+ 3
- 0
IPA.Loader/Loader/PluginInitInjector.cs View File

@ -64,6 +64,9 @@ namespace IPA.Loader
private static readonly List<TypedInjector> injectors = new List<TypedInjector>
{
new TypedInjector(typeof(Logger), (prev, param, meta) => prev ?? new StandardLogger(meta.Name)),
#pragma warning disable CS0618 // Type or member is obsolete
new TypedInjector(typeof(IModPrefs), (prev, param, meta) => prev ?? new ModPrefs(meta)),
#pragma warning restore CS0618 // Type or member is obsolete
new TypedInjector(typeof(PluginLoader.PluginMetadata), (prev, param, meta) => prev ?? meta),
new TypedInjector(typeof(IConfigProvider), (prev, param, meta) =>
{


BIN
Refs/Assembly-CSharp.dll View File


BIN
Refs/UnityEngine.CoreModule.Net4.dll View File


Loading…
Cancel
Save