Decompiled source of P1 Rhythm Rework v1.0.1

P1RhythmRework.dll

Decompiled a year ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using P1.System.Sound;
using P1RhythmRework;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("P1RhythmRework")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Patapon 1 Rhythm Rework")]
[assembly: AssemblyTitle("P1RhythmRework")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
[HarmonyPatch(typeof(AttackCommand), "initialize")]
public static class ReadSettings
{
	public static object systemData;

	public static void Postfix()
	{
		systemData = Plugin.GetValue(SingletonMonobehaviour<SaveDataManager>.Instance, "SystemSaveData");
		CommandTiming.inputTiming = Math.Clamp((int)Plugin.GetValue(systemData, "GameInputTimingP1"), -5, 5);
		CommandTiming.difficulty = (int)Plugin.GetValue(systemData, "GameLevelP1");
	}
}
[HarmonyPatch(typeof(AttackCommand), "createAttack")]
public static class CommandTiming
{
	public static int inputTiming = 0;

	public static int difficulty = 0;

	public static int forceDifficulty = 0;

	public static float[] rhythmThresholds = new float[4] { 0.01f, 0.5f, 0.72f, 0.9f };

	public static void Postfix(AttackCommand __instance, int hit, bool allHalf, uint beatCount, uint halfCount, ref Attack __result)
	{
		if (__result != null)
		{
			Plugin.SetValue(__result, "level", GetLevel(__instance, allHalf));
		}
	}

	public static int GetLevel(AttackCommand __instance, bool allHalf)
	{
		float accuracy = GetAccuracy(__instance, allHalf);
		int num = ((forceDifficulty == 0) ? difficulty : forceDifficulty);
		float inverted = 1f - Mathf.Pow(accuracy, 1f + (float)(num * 2));
		int num2 = Array.FindIndex(rhythmThresholds, (float threshold) => inverted < threshold);
		return (num2 == -1) ? 1 : (5 - num2);
	}

	public static float GetAccuracy(AttackCommand __instance, bool allHalf)
	{
		object value = Plugin.GetValue(__instance, "beatTimer_");
		BeatTimer val = (BeatTimer)((value is BeatTimer) ? value : null);
		if (val == null)
		{
			return 0f;
		}
		int num = ((!allHalf) ? 1 : 0) + 1;
		bool flag = false;
		float num2 = ((num == 1) ? val.getRateToHalfLine(ref flag) : val.getRateToBeatLine(ref flag));
		float num3 = (float)inputTiming / 15f;
		if (flag)
		{
			num2 = 0f - num2;
		}
		num2 += num3;
		if (num2 < 0f)
		{
			num2 = 0f - num2;
		}
		if (num2 > 1f)
		{
			num2 = 1f - (num2 - 1f);
		}
		return 1f - num2;
	}
}
namespace P1RhythmRework
{
	[BepInPlugin("com.nextrg.p1rhythmrework", "Patapon 1 Rhythm Rework", "1.0.1")]
	public class Plugin : BasePlugin
	{
		public static ConfigEntry<int> forceDifficulty;

		internal static ManualLogSource Log;

		public static readonly string guid = "com.nextrg.p1rhythmrework";

		public static ConfigFile config;

		private FileSystemWatcher _watcher;

		public override void Load()
		{
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Expected O, but got Unknown
			Log = ((BasePlugin)this).Log;
			config = ((BasePlugin)this).Config;
			Log.LogInfo((object)"Loaded");
			forceDifficulty = ((BasePlugin)this).Config.Bind<int>("General", "Difficulty Override", 0, "0 will use the Game Levels setting, 1 will force easy, 2 medium and 3 hard");
			UpdateVariables();
			CheckConfigChanges();
			Harmony val = new Harmony(guid);
			val.PatchAll();
		}

		public void CheckConfigChanges()
		{
			_watcher = new FileSystemWatcher(Path.GetDirectoryName(config.ConfigFilePath))
			{
				Filter = Path.GetFileName(config.ConfigFilePath),
				NotifyFilter = (NotifyFilters.Size | NotifyFilters.LastWrite),
				IncludeSubdirectories = false
			};
			_watcher.Changed += delegate
			{
				RefreshConfig();
			};
			_watcher.EnableRaisingEvents = true;
		}

		public static void UpdateVariables()
		{
			CommandTiming.forceDifficulty = Math.Clamp(forceDifficulty.Value, 0, 3);
		}

		public static void RefreshConfig()
		{
			try
			{
				config.Reload();
			}
			catch (Exception)
			{
			}
			UpdateVariables();
		}

		public static void SetValue(object instance, string propertyName, object value)
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Expected O, but got Unknown
			PropertyInfo property = instance.GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (property == null)
			{
				ManualLogSource log = Log;
				bool flag = default(bool);
				BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(26, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Property '");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(propertyName);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("' not found in ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(instance.GetType().Name);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("!");
				}
				log.LogError(val);
			}
			else
			{
				property.SetValue(instance, value);
			}
		}

		public static object GetValue(object instance, string propertyName)
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Expected O, but got Unknown
			PropertyInfo property = instance.GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (property == null)
			{
				ManualLogSource log = Log;
				bool flag = default(bool);
				BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(26, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Property '");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(propertyName);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("' not found in ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(instance.GetType().Name);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("!");
				}
				log.LogError(val);
				return null;
			}
			return property.GetValue(instance);
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "P1RhythmRework";

		public const string PLUGIN_NAME = "Patapon 1 Rhythm Rework";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}