Skip to content
This repository has been archived by the owner on Feb 15, 2023. It is now read-only.

Commit

Permalink
Mono port
Browse files Browse the repository at this point in the history
  • Loading branch information
Raoul1808 committed Jan 28, 2022
1 parent c703ba6 commit fddcca5
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 121 deletions.
10 changes: 10 additions & 0 deletions Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
using HarmonyLib;
using System.Reflection;

namespace QualityOfSpeen
{
public static class Extensions
{
public static void PatchAll<T>(this Harmony harmony) => harmony.PatchAll(typeof(T));

// Source: https://stackoverflow.com/a/46488844
public static T GetFieldValue<T>(this object obj, string name)
{
// Set the flags so that private and public fields from instances will be found
var bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
var field = obj.GetType().GetField(name, bindingFlags);
return (T)field?.GetValue(obj);
}
}
}
91 changes: 54 additions & 37 deletions Features/DiscordRPCFix.cs
Original file line number Diff line number Diff line change
@@ -1,74 +1,91 @@
using HarmonyLib;
using System.Reflection;
using UnityEngine;

namespace QualityOfSpeen.Features
{
public class DiscordRPCFix
{
public static bool UpdateDiscord;
public static bool SecretMode;
public static bool UpdateDiscord = Main.ModConfig.GetValueOrDefaultTo("Discord", "EnableRichPresence", true);
public static bool SecretMode = Main.ModConfig.GetValueOrDefaultTo("Discord", "StartInSecretMode", false);

private static void Awake()
[HarmonyPatch]
class SpinDiscordPatch
{
UpdateDiscord = Main.ModConfig.GetValueOrDefaultTo("Discord", "EnableRichPresence", true);
SecretMode = Main.ModConfig.GetValueOrDefaultTo("Discord", "StartInSecretMode", false);
}

[HarmonyPatch(typeof(SpinDiscord), nameof(SpinDiscord.UpdateActivityPresence))]
[HarmonyPrefix]
private static bool UpdateActivityPresencePrefix(ref string state, ref string details, ref string coverArt, ref string trackArtist, ref string trackTitle, ref string trackLabel, ref long endTime)
{
if (!UpdateDiscord) return false;
static MethodBase TargetMethod()
{
var type = AccessTools.TypeByName("SpinDiscord");
return AccessTools.Method(type, "UpdateActivityPresence");
}

if (Main.InGameState == InGameState.Editor)
static bool Prefix(ref string state, ref string details, ref string coverArt, ref string trackArtist, ref string trackTitle, ref string trackLabel, ref long endTime)
{
if (SecretMode)
//Main.Logger.LogMessage("Activity Update");
if (!UpdateDiscord) return false;


if (Main.InGameState == InGameState.Editor)
{
details = "Editing <SECRET>";
state = "Secret Mode enabled!";
trackArtist = "Secret";
trackTitle = "Secret";
trackLabel = "Secret";
endTime = 0;
if (SecretMode)
{
details = "Editing <SECRET>";
state = "Secret Mode enabled!";
trackArtist = "Secret";
trackTitle = "Secret";
trackLabel = "Secret";
endTime = 0;
}
else
{
details = "Editing " + trackTitle;
endTime = 0;
}
}
else

if (Main.IsDead)
{
details = "Editing " + trackTitle;
state = "Failed";
endTime = 0;
}
}

if (Main.IsDead)
{
state = "Failed";
endTime = 0;
}
if (Main.InGameState == InGameState.CustomLevelSelectMenu)
{
state = "Picking Custom Track";
endTime = 0;
}

if (Main.InGameState == InGameState.CustomLevelSelectMenu)
{
state = "Picking Custom Track";
endTime = 0;
return true;
}
}

return true;
[HarmonyPatch(typeof(Track), "UpdateRichPresence")]
[HarmonyPrefix]
private static bool TrackUpdateRichPresencePrefix()
{
return UpdateDiscord;
}

[HarmonyPatch(typeof(Track), nameof(Track.Update))]
[HarmonyPostfix]
private static void TrackUpdatePostfix()
{
if (Input.GetKeyDown(KeyCode.F5))
if (Input.GetKeyDown(KeyCode.F4))
{
SecretMode = !SecretMode;
Main.Logger.LogWarning("Secret Mode " + (SecretMode ? "Enabled" : "Disabled"));
NotificationSystemGUI.AddMessage("Secret Mode " + (SecretMode ? "Enabled" : "Disabled"));
}
if (Input.GetKeyDown(KeyCode.F3))
{
UpdateDiscord = !UpdateDiscord;
NotificationSystemGUI.AddMessage("Discord Rich Presence Toggled " + (UpdateDiscord ? "On" : "Off"));
}
}

[HarmonyPatch(typeof(XDCustomLevelSelectMenu), nameof(XDCustomLevelSelectMenu.Awake))]
[HarmonyPatch(typeof(XDCustomLevelSelectMenu), "Awake")]
[HarmonyPostfix]
private static void XDCustomLevelSelectMenuAwakePostfix()
{
Main.Logger.LogWarning("Reminder: Secret Mode is currently " + (SecretMode ? "Enabled" : "Disabled"));
NotificationSystemGUI.AddMessage("Reminder: Secret Mode is currently " + (SecretMode ? "Enabled" : "Disabled"));
}
}
}
6 changes: 5 additions & 1 deletion Features/InstantRestartKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ public class InstantRestartKey
{
public static bool currentKeyState = false, previousKeyState = false;

private static XDInput inputRef;

[HarmonyPatch(typeof(Track), nameof(Track.Update))]
[HarmonyPostfix]
private static void RestartKey()
{
if (inputRef == null)
inputRef = XDInputModule.Instance.GetFieldValue<XDInput>("xdInput");
previousKeyState = currentKeyState;
currentKeyState = XDInputModule.Instance.xdInput.GetButtonDown(InputMapping.SpinCommands.RestartSong);
currentKeyState = inputRef.GetButtonDown(InputMapping.SpinCommands.RestartSong);
if (currentKeyState && !previousKeyState && Main.InGameState == InGameState.Playing && !Main.IsDead && !Main.HasWon && !Main.IsRestarting)
{
Track.Instance.RestartTrack();
Expand Down
26 changes: 0 additions & 26 deletions Features/NoIntro.cs

This file was deleted.

95 changes: 79 additions & 16 deletions IniFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ public class IniFile
{
private Dictionary<string, Dictionary<string, string>> iniContent;
private string FilePath;
public static IFormatProvider Culture;
public IFormatProvider Culture;

/// <summary>
/// Creates a new Ini file handler for the file at the given path. /!\ THE FILE MUST EXIST FIRST. IT WILL NOT BE CREATED AUTOMATICALLY IF MISSING
/// </summary>
/// <param name="filePath">The path to the file. Make sure the given path is valid!</param>
public IniFile(string filePath)
{
FilePath = filePath;
Expand All @@ -21,9 +25,14 @@ public IniFile(string filePath)
ParseFile(fileContent);
}

public Dictionary<string, string> this[string setting]
/// <summary>
/// Get a section as a string-string dictionary
/// </summary>
/// <param name="section">The section to get</param>
/// <returns>The section as a string-string dictionary</returns>
public Dictionary<string, string> this[string section]
{
get { return iniContent[setting]; }
get { return iniContent[section]; }
}

private void ParseFile(string[] content)
Expand Down Expand Up @@ -53,7 +62,7 @@ private void ParseFile(string[] content)
private string MakeString()
{
StringBuilder sb = new StringBuilder();

foreach (KeyValuePair<string, Dictionary<string, string>> pair in iniContent)
{
sb.AppendLine("[" + pair.Key + "]");
Expand All @@ -67,20 +76,39 @@ private string MakeString()
return sb.ToString();
}

public void AddSetting(string section, string settingName, string defaultValue)
{
if (!iniContent.ContainsKey(section)) iniContent.Add(section, new Dictionary<string, string>());
iniContent[section].Add(settingName, defaultValue);
}

/// <summary>
/// Saves the file
/// </summary>
public void SaveFile() => SaveFile(FilePath);

/// <summary>
/// Saves the file at a certain path
/// </summary>
/// <param name="filePath">The path to save the file to</param>
public void SaveFile(string filePath)
{
File.WriteAllText(filePath, MakeString());
}

public T GetValueOrDefaultTo<T>(string section, string setting, T defaultValue)
/// <summary>
/// Reloads the file if any external changes were made
/// </summary>
public void ReloadFile()
{
iniContent = new Dictionary<string, Dictionary<string, string>>();
ParseFile(File.ReadAllLines(FilePath));
}

/// <summary>
/// Safely get a value from the config file. It will automatically be cast to the requested type. If an error occurs or the config entry is missing, it can be created automatically.
/// </summary>
/// <typeparam name="T">The type to cast the config entry to. Currently supported: string, bool, int, float, double, decimal</typeparam>
/// <param name="section">The config section to get the value from</param>
/// <param name="setting">The config entry to get the value from</param>
/// <param name="defaultValue">The value returned if the entry was not found or a cast isn't supported. In the first case, this value can be used to make a new config entry</param>
/// <param name="setIfDoesntExist">If set to true, the config file will be updated with the new value. Default: true</param>
/// <returns>The setting you're looking for, or defaultValue if the value is not found</returns>
public T GetValueOrDefaultTo<T>(string section, string setting, T defaultValue, bool setIfDoesntExist = true, bool saveIfDoesntExist = true)
{
try
{
Expand All @@ -96,6 +124,12 @@ public T GetValueOrDefaultTo<T>(string section, string setting, T defaultValue)
case TypeCode.Int32:
return (T)(object)int.Parse(value);

case TypeCode.Single:
return (T)(object)float.Parse(value, Culture);

case TypeCode.Double:
return (T)(object)double.Parse(value, Culture);

case TypeCode.Decimal:
return (T)(object)decimal.Parse(value, Culture);

Expand All @@ -105,13 +139,42 @@ public T GetValueOrDefaultTo<T>(string section, string setting, T defaultValue)
}
catch
{
if (!iniContent.ContainsKey(section))
iniContent.Add(section, new Dictionary<string, string>());
if (!iniContent[section].ContainsKey(setting))
iniContent[section].Add(setting, defaultValue.ToString());
SaveFile();
if (setIfDoesntExist)
SetValue(section, setting, defaultValue, saveIfDoesntExist);
return defaultValue;
}
}

/// <summary>
/// Sets a value in the config
/// </summary>
/// <typeparam name="T">The type of the value</typeparam>
/// <param name="section">The section to save the value to</param>
/// <param name="setting">The config entry to save the value to</param>
/// <param name="value">The value to save</param>
/// <param name="immediatelySave">If set to true, immediately save the config file afterwards.</param>
public void SetValue<T>(string section, string setting, T value, bool immediatelySave = true)
{
if (!iniContent.ContainsKey(section))
iniContent.Add(section, new Dictionary<string, string>());
if (!iniContent[section].ContainsKey(setting))
iniContent[section].Add(setting, value.ToString());
if (immediatelySave) SaveFile();
}

/// <summary>
/// Checks if a section exists
/// </summary>
/// <param name="section">The section to check</param>
/// <returns>True if the section exists, False otherwise</returns>
public bool Exists(string section) => iniContent.ContainsKey(section);

/// <summary>
/// Checks if a setting exists
/// </summary>
/// <param name="section">The section to check</param>
/// <param name="setting">The setting to check</param>
/// <returns>True if both the section and the setting exist, False otherwise</returns>
public bool Exists(string section, string setting) => iniContent.ContainsKey(section) && iniContent[section].ContainsKey(setting);
}
}
Loading

0 comments on commit fddcca5

Please sign in to comment.