Decompiled source of SeamstressTweaks v1.0.0

BepInEx/plugins/SeamstressTweaks.dll

Decompiled 15 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using RoR2;
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(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("SeamstressTweaks")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("SeamstressTweaks")]
[assembly: AssemblyTitle("SeamstressTweaks")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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]
	[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;
		}
	}
}
namespace SeamstressTweaks
{
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInPlugin("com.seamstresstweaks.gameplay", "SeamstressTweaks", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		private void Awake()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"SeamstressTweaks успешно загружен!");
			try
			{
				InstallCooldownPatch();
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Патчи Planar Manipulation установлены!");
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Ошибка установки патчей: " + ex));
			}
		}

		private void InstallCooldownPatch()
		{
			//IL_01af: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b6: Expected O, but got Unknown
			//IL_01cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01db: Expected O, but got Unknown
			//IL_01f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_0202: Expected O, but got Unknown
			//IL_021c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0229: Expected O, but got Unknown
			//IL_0243: Unknown result type (might be due to invalid IL or missing references)
			//IL_0250: Expected O, but got Unknown
			//IL_026a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0277: Expected O, but got Unknown
			//IL_0291: Unknown result type (might be due to invalid IL or missing references)
			//IL_029e: Expected O, but got Unknown
			//IL_02b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c5: Expected O, but got Unknown
			Assembly assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault((Assembly a) => a.GetName().Name == "SeamstressMod");
			if (assembly == null)
			{
				throw new Exception("Не найдена DLL SeamstressMod!");
			}
			Type type = FindType(assembly, "SeamstressTrackingSkillDef");
			Type type2 = FindType(assembly, "PlanarManipulation");
			if (type == null)
			{
				throw new Exception("Не найден SeamstressTrackingSkillDef!");
			}
			if (type2 == null)
			{
				throw new Exception("Не найден PlanarManipulation!");
			}
			MethodInfo methodInfo = AccessTools.Method(type, "CanExecute", new Type[1] { typeof(GenericSkill) }, (Type[])null);
			MethodInfo methodInfo2 = AccessTools.Method(type, "IsReady", new Type[1] { typeof(GenericSkill) }, (Type[])null);
			MethodInfo methodInfo3 = AccessTools.Method(typeof(GenericSkill), "DeductStock", new Type[1] { typeof(int) }, (Type[])null);
			MethodInfo methodInfo4 = AccessTools.Method(typeof(GenericSkill), "RestockSteplike", (Type[])null, (Type[])null);
			MethodInfo methodInfo5 = AccessTools.Method(typeof(GenericSkill), "RestockContinuous", (Type[])null, (Type[])null);
			MethodInfo methodInfo6 = AccessTools.Method(typeof(GenericSkill), "Reset", (Type[])null, (Type[])null);
			MethodInfo methodInfo7 = AccessTools.Method(type2, "OnExit", (Type[])null, (Type[])null);
			if (methodInfo == null || methodInfo2 == null || methodInfo3 == null || methodInfo4 == null || methodInfo5 == null || methodInfo6 == null || methodInfo7 == null)
			{
				throw new Exception("Не удалось найти необходимые методы для патча!");
			}
			Harmony val = new Harmony("com.seamstresstweaks.planarcooldown");
			val.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(CooldownPatch).GetMethod("SkillCheckPrefix")), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			val.Patch((MethodBase)methodInfo2, new HarmonyMethod(typeof(CooldownPatch).GetMethod("SkillCheckPrefix")), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			val.Patch((MethodBase)methodInfo3, (HarmonyMethod)null, new HarmonyMethod(typeof(CooldownPatch).GetMethod("OnDeductStockPostfix")), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			val.Patch((MethodBase)methodInfo4, (HarmonyMethod)null, new HarmonyMethod(typeof(CooldownPatch).GetMethod("OnRestockPostfix")), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			val.Patch((MethodBase)methodInfo5, (HarmonyMethod)null, new HarmonyMethod(typeof(CooldownPatch).GetMethod("OnRestockPostfix")), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			val.Patch((MethodBase)methodInfo6, (HarmonyMethod)null, new HarmonyMethod(typeof(CooldownPatch).GetMethod("OnResetPostfix")), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			val.Patch((MethodBase)methodInfo7, new HarmonyMethod(typeof(CooldownPatch).GetMethod("OnExitPrefix")), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}

		private static Type FindType(Assembly assembly, string name)
		{
			Type[] types = assembly.GetTypes();
			foreach (Type type in types)
			{
				if (type != null && type.Name == name)
				{
					return type;
				}
			}
			return null;
		}
	}
	public static class CooldownPatch
	{
		private static readonly Dictionary<GenericSkill, float> originalOverrides = new Dictionary<GenericSkill, float>();

		public static bool SkillCheckPrefix(GenericSkill skillSlot, ref bool __result)
		{
			if ((Object)(object)skillSlot == (Object)null)
			{
				return true;
			}
			if (skillSlot.stock <= 0)
			{
				__result = false;
				return false;
			}
			return true;
		}

		public static void OnDeductStockPostfix(GenericSkill __instance, int count)
		{
			if (!((Object)(object)__instance == (Object)null) && count > 0)
			{
				EntityStateMachine stateMachine = __instance.stateMachine;
				if (!((Object)(object)stateMachine == (Object)null) && stateMachine.state != null && !(((object)stateMachine.state).GetType().Name != "PlanarManipulation") && !originalOverrides.ContainsKey(__instance))
				{
					float telekinesisCooldown = GetTelekinesisCooldown();
					originalOverrides[__instance] = __instance.cooldownOverride;
					__instance.cooldownOverride = telekinesisCooldown;
					Debug.Log((object)("[SeamstressTweaks] Установлена перезарядка: " + telekinesisCooldown + " сек."));
				}
			}
		}

		public static void OnRestockPostfix(GenericSkill __instance)
		{
			RestoreOverrideIfFullyCharged(__instance);
		}

		public static void OnResetPostfix(GenericSkill __instance)
		{
			RestoreOverrideIfFullyCharged(__instance);
		}

		public static void OnExitPrefix(object __instance)
		{
			try
			{
				if (__instance == null)
				{
					return;
				}
				Type type = __instance.GetType();
				FieldInfo fieldInfo = FindField(type, "victimInitialized");
				FieldInfo fieldInfo2 = FindField(type, "victimBody");
				if (fieldInfo == null || fieldInfo2 == null || !(bool)fieldInfo.GetValue(__instance))
				{
					return;
				}
				object value = fieldInfo2.GetValue(__instance);
				Object val = (Object)((value is Object) ? value : null);
				if (val != null && val != (Object)null)
				{
					return;
				}
				object memberValue = GetMemberValue(__instance, "skillLocator");
				if (memberValue == null)
				{
					object memberValue2 = GetMemberValue(__instance, "characterBody");
					if (memberValue2 != null)
					{
						memberValue = GetMemberValue(memberValue2, "skillLocator");
					}
				}
				if (memberValue == null)
				{
					Debug.LogWarning((object)"[SeamstressTweaks] Не удалось получить skillLocator для резервного списания заряда.");
					return;
				}
				object memberValue3 = GetMemberValue(memberValue, "secondary");
				GenericSkill val2 = (GenericSkill)((memberValue3 is GenericSkill) ? memberValue3 : null);
				if ((Object)(object)val2 == (Object)null)
				{
					Debug.LogWarning((object)"[SeamstressTweaks] Не удалось получить secondary для резервного списания заряда.");
				}
				else if (val2.stock > 0)
				{
					Debug.LogWarning((object)"[SeamstressTweaks] Цель уничтожена до завершения Planar Manipulation. Выполняется резервное списание.");
					val2.DeductStock(1);
				}
			}
			catch (Exception ex)
			{
				Debug.LogError((object)("[SeamstressTweaks] Ошибка резервного списания заряда: " + ex));
			}
		}

		private static FieldInfo FindField(Type type, string name)
		{
			while (type != null)
			{
				FieldInfo fieldInfo = AccessTools.Field(type, name);
				if (fieldInfo != null)
				{
					return fieldInfo;
				}
				type = type.BaseType;
			}
			return null;
		}

		private static object GetMemberValue(object instance, string name)
		{
			if (instance == null)
			{
				return null;
			}
			Type type = instance.GetType();
			while (type != null)
			{
				PropertyInfo propertyInfo = AccessTools.Property(type, name);
				if (propertyInfo != null)
				{
					return propertyInfo.GetValue(instance, null);
				}
				FieldInfo fieldInfo = AccessTools.Field(type, name);
				if (fieldInfo != null)
				{
					return fieldInfo.GetValue(instance);
				}
				type = type.BaseType;
			}
			return null;
		}

		private static void RestoreOverrideIfFullyCharged(GenericSkill skill)
		{
			if (!((Object)(object)skill == (Object)null) && originalOverrides.TryGetValue(skill, out var value) && skill.stock >= skill.maxStock)
			{
				skill.cooldownOverride = value;
				originalOverrides.Remove(skill);
				Debug.Log((object)"[SeamstressTweaks] Все заряды восстановлены. Исходный cooldownOverride возвращён.");
			}
		}

		private static float GetTelekinesisCooldown()
		{
			try
			{
				Assembly assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault((Assembly a) => a.GetName().Name == "SeamstressMod");
				if (assembly != null)
				{
					Type type = assembly.GetTypes().FirstOrDefault((Type t) => t.Name == "SeamstressConfig");
					if (type != null)
					{
						FieldInfo fieldInfo = AccessTools.Field(type, "telekinesisCooldown");
						if (fieldInfo != null)
						{
							object value = fieldInfo.GetValue(null);
							if (value != null)
							{
								PropertyInfo propertyInfo = AccessTools.Property(value.GetType(), "Value");
								if (propertyInfo != null)
								{
									return Convert.ToSingle(propertyInfo.GetValue(value, null));
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				Debug.LogWarning((object)("[SeamstressTweaks] Ошибка чтения настройки cooldown: " + ex.Message));
			}
			Debug.LogWarning((object)"[SeamstressTweaks] Не удалось прочитать telekinesisCooldown. Используется значение 6 секунд.");
			return 6f;
		}
	}
}