Decompiled source of CommandoBuff v7.0.0

CommandoBuff.dll

Decompiled 3 weeks ago
using System;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using EntityStates;
using EntityStates.Commando;
using EntityStates.Commando.CommandoWeapon;
using EntityStates.Huntress;
using On.EntityStates.Commando.CommandoWeapon;
using On.EntityStates.Huntress;
using On.RoR2;
using RoR2;
using RoR2.Skills;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace CommandoBuff;

[BepInPlugin("com.dsh.commandobuff", "Commando Buff", "7.0.0")]
public class CommandoBuffPlugin : BaseUnityPlugin
{
	public static CommandoBuffPlugin Inst;

	private ConfigEntry<float> cfgPrimary;

	private ConfigEntry<float> cfgBarrageDamage;

	private ConfigEntry<float> cfgBlinkCooldown;

	private int pulverizeBuff = -1;

	private int debuffCycle = 0;

	private static string cachedDodgeSound;

	private static FieldInfo FieldOf(Type t, string name)
	{
		return t.GetField(name, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
	}

	private static void SetFloat(Type t, string name, float v, object inst)
	{
		FieldInfo fieldInfo = FieldOf(t, name);
		if (fieldInfo == null)
		{
			((BaseUnityPlugin)Inst).Logger.LogWarning((object)("field not found: " + t.Name + "." + name));
		}
		else if (fieldInfo.IsStatic)
		{
			fieldInfo.SetValue(null, v);
		}
		else if (inst != null)
		{
			fieldInfo.SetValue(inst, v);
		}
	}

	private static string Pct(float v)
	{
		return (v * 100f).ToString("0.#", CultureInfo.InvariantCulture);
	}

	private void Awake()
	{
		//IL_008f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0094: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
		//IL_0104: Unknown result type (might be due to invalid IL or missing references)
		//IL_010e: Expected O, but got Unknown
		//IL_0116: Unknown result type (might be due to invalid IL or missing references)
		//IL_0120: Expected O, but got Unknown
		//IL_0128: Unknown result type (might be due to invalid IL or missing references)
		//IL_0132: Expected O, but got Unknown
		//IL_0141: Unknown result type (might be due to invalid IL or missing references)
		//IL_014b: Expected O, but got Unknown
		//IL_0166: Unknown result type (might be due to invalid IL or missing references)
		//IL_0170: Expected O, but got Unknown
		Inst = this;
		cfgPrimary = ((BaseUnityPlugin)this).Config.Bind<float>("Commando", "Primary damage coefficient", 1.2f, "Double Tap damage (vanilla 1.0)");
		cfgBarrageDamage = ((BaseUnityPlugin)this).Config.Bind<float>("Commando", "Suppressive Fire damage coefficient", 2.5f, "Per bullet (vanilla 1.0)");
		cfgBlinkCooldown = ((BaseUnityPlugin)this).Config.Bind<float>("Commando", "Blink cooldown", 5f, "Seconds");
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Commando Buff v7.0.0 loaded");
		try
		{
			uint num = default(uint);
			AKRESULT val = AkSoundEngine.LoadBank("char_Huntress", ref num);
			((BaseUnityPlugin)this).Logger.LogInfo((object)string.Concat("LoadBank char_Huntress = ", val, " (id ", num, ")"));
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)("bank load failed: " + ex.Message));
		}
		FirePistol2.OnEnter += (hook_OnEnter)delegate(orig_OnEnter orig, FirePistol2 self)
		{
			SetFloat(typeof(FirePistol2), "damageCoefficient", cfgPrimary.Value, self);
			orig.Invoke(self);
		};
		FireBarrage.OnEnter += (hook_OnEnter)delegate(orig_OnEnter orig, FireBarrage self)
		{
			SetFloat(typeof(FireBarrage), "damageCoefficient", cfgBarrageDamage.Value, self);
			orig.Invoke(self);
		};
		BulletAttack.Fire += (hook_Fire)delegate(orig_Fire orig, BulletAttack self)
		{
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Expected O, but got Unknown
			HitCallback val2 = null;
			HitCallback original = self.hitCallback;
			if (IsFiringBarrage(self.owner))
			{
				if (val2 == null)
				{
					val2 = (HitCallback)delegate(BulletAttack ba, ref BulletHit hitInfo)
					{
						bool result = true;
						try
						{
							if (original != null)
							{
								result = original.Invoke(ba, ref hitInfo);
							}
							ApplyRandomDebuff(hitInfo);
						}
						catch (Exception ex2)
						{
							((BaseUnityPlugin)Inst).Logger.LogWarning((object)("debuff err: " + ex2.Message));
						}
						return result;
					};
				}
				self.hitCallback = val2;
			}
			try
			{
				orig.Invoke(self);
			}
			finally
			{
				self.hitCallback = original;
			}
		};
		BlinkState.GetBlinkVector += (hook_GetBlinkVector)delegate(orig_GetBlinkVector orig, BlinkState self)
		{
			//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_0096: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				CharacterBody val2 = (((Object)(object)((EntityState)self).outer != (Object)null) ? ((Component)((EntityState)self).outer).GetComponent<CharacterBody>() : null);
				if ((Object)(object)val2 != (Object)null && val2.bodyIndex == BodyCatalog.FindBodyIndex("CommandoBody"))
				{
					Vector3 moveVector = val2.inputBank.moveVector;
					if (((Vector3)(ref moveVector)).sqrMagnitude > 0.01f)
					{
						Vector3 val3 = orig.Invoke(self);
						float num2 = ((Vector3)(ref val3)).magnitude;
						if (num2 < 0.1f)
						{
							num2 = 10f;
						}
						return ((Vector3)(ref moveVector)).normalized * num2;
					}
				}
			}
			catch (Exception ex2)
			{
				((BaseUnityPlugin)Inst).Logger.LogWarning((object)("blink dir err: " + ex2.Message));
			}
			return orig.Invoke(self);
		};
		BlinkState.OnEnter += (hook_OnEnter)delegate(orig_OnEnter orig, BlinkState self)
		{
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_01be: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c5: Expected O, but got Unknown
			//IL_01d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01df: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e4: Unknown result type (might be due to invalid IL or missing references)
			orig.Invoke(self);
			try
			{
				CharacterBody val2 = (((Object)(object)((EntityState)self).outer != (Object)null) ? ((Component)((EntityState)self).outer).GetComponent<CharacterBody>() : null);
				if (!((Object)(object)val2 == (Object)null) && val2.bodyIndex == BodyCatalog.FindBodyIndex("CommandoBody"))
				{
					string beginSoundString = self.beginSoundString;
					string endSoundString = self.endSoundString;
					((BaseUnityPlugin)Inst).Logger.LogInfo((object)("blink sounds: begin='" + beginSoundString + "' end='" + endSoundString + "' prefab=" + (((Object)(object)BlinkState.blinkPrefab == (Object)null) ? "null" : ((Object)BlinkState.blinkPrefab).name)));
					if (!string.IsNullOrEmpty(beginSoundString))
					{
						uint num2 = Util.PlaySound(beginSoundString, ((Component)((EntityState)self).outer).gameObject);
						((BaseUnityPlugin)Inst).Logger.LogInfo((object)("played sound '" + beginSoundString + "' id=" + num2));
						if (num2 == 0)
						{
							string commandoDodgeSound = GetCommandoDodgeSound();
							if (!string.IsNullOrEmpty(commandoDodgeSound))
							{
								uint num3 = Util.PlaySound(commandoDodgeSound, ((Component)((EntityState)self).outer).gameObject);
								((BaseUnityPlugin)Inst).Logger.LogInfo((object)("fallback sound '" + commandoDodgeSound + "' id=" + num3));
							}
						}
					}
					GameObject blinkPrefab = BlinkState.blinkPrefab;
					if ((Object)(object)blinkPrefab != (Object)null)
					{
						EffectData val3 = new EffectData();
						val3.origin = ((Component)((EntityState)self).outer).transform.position;
						val3.rotation = Quaternion.identity;
						val3.scale = 1f;
						EffectManager.SpawnEffect(blinkPrefab, val3, true);
						((BaseUnityPlugin)Inst).Logger.LogInfo((object)("spawned blink effect " + ((Object)blinkPrefab).name));
					}
				}
			}
			catch (Exception ex2)
			{
				((BaseUnityPlugin)Inst).Logger.LogWarning((object)("blink sound err: " + ex2.Message));
			}
		};
		RoR2Application.onLoad = (Action)Delegate.Combine(RoR2Application.onLoad, (Action)delegate
		{
			try
			{
				FindPulverizeBuff();
			}
			catch (Exception ex2)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("buff lookup failed: " + ex2.Message));
			}
			try
			{
				UpdateSkillText();
			}
			catch (Exception ex2)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("text update failed: " + ex2.Message));
			}
			try
			{
				AddBlinkSkill();
			}
			catch (Exception ex2)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("blink add failed: " + ex2.Message));
			}
		});
	}

	private static string GetCommandoDodgeSound()
	{
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Expected O, but got Unknown
		if (cachedDodgeSound != null)
		{
			return cachedDodgeSound;
		}
		try
		{
			DodgeState val = new DodgeState();
			EntityStateCatalog.InitializeStateFields((EntityState)(object)val);
			cachedDodgeSound = DodgeState.dodgeSoundString;
			((BaseUnityPlugin)Inst).Logger.LogInfo((object)("commando dodge sound = '" + cachedDodgeSound + "'"));
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)Inst).Logger.LogWarning((object)("dodge sound lookup failed: " + ex.Message));
			cachedDodgeSound = "";
		}
		return cachedDodgeSound;
	}

	private static bool IsFiringBarrage(GameObject owner)
	{
		if ((Object)(object)owner == (Object)null)
		{
			return false;
		}
		EntityStateMachine[] components = owner.GetComponents<EntityStateMachine>();
		for (int i = 0; i < components.Length; i++)
		{
			if (!((Object)(object)components[i] == (Object)null) && components[i].state != null)
			{
				Type type = ((object)components[i].state).GetType();
				if (type == typeof(FireBarrage))
				{
					return true;
				}
				if (type == typeof(PrepBarrage))
				{
					return true;
				}
			}
		}
		return false;
	}

	private void FindPulverizeBuff()
	{
		for (int i = 0; i < 256; i++)
		{
			BuffDef buffDef = BuffCatalog.GetBuffDef((BuffIndex)i);
			if (!((Object)(object)buffDef == (Object)null) && ((Object)buffDef).name != null && ((Object)buffDef).name.IndexOf("Pulverize", StringComparison.OrdinalIgnoreCase) >= 0)
			{
				pulverizeBuff = i;
				((BaseUnityPlugin)this).Logger.LogInfo((object)("pulverize buff = " + ((Object)buffDef).name + " (" + i + ")"));
				return;
			}
		}
		((BaseUnityPlugin)this).Logger.LogWarning((object)"pulverize buff not found");
	}

	private void ApplyRandomDebuff(BulletHit hitInfo)
	{
		//IL_0068: Unknown result type (might be due to invalid IL or missing references)
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		HurtBox val = hitInfo.hitHurtBox;
		if ((Object)(object)val == (Object)null)
		{
			val = hitInfo.hurtBox;
		}
		if ((Object)(object)val == (Object)null)
		{
			return;
		}
		HealthComponent healthComponent = val.healthComponent;
		if ((Object)(object)healthComponent == (Object)null || (Object)(object)healthComponent.body == (Object)null || healthComponent.body.bodyIndex == BodyCatalog.FindBodyIndex("CommandoBody"))
		{
			return;
		}
		int num = debuffCycle % 3;
		debuffCycle++;
		GameObject gameObject = ((Component)healthComponent.body).gameObject;
		switch (num)
		{
		case 0:
			if (pulverizeBuff >= 0)
			{
				healthComponent.body.AddBuff((BuffIndex)pulverizeBuff);
			}
			break;
		case 1:
			DotController.InflictDot(gameObject, ((Component)Inst).gameObject, (DotIndex)1, 5f, 1f, (uint?)null);
			break;
		default:
			DotController.InflictDot(gameObject, ((Component)Inst).gameObject, (DotIndex)0, 3f, 1f, (uint?)null);
			break;
		}
	}

	private static SkillDef GetSlotDefault(GenericSkill slot)
	{
		if ((Object)(object)slot == (Object)null)
		{
			return null;
		}
		SkillFamily skillFamily = slot.skillFamily;
		if ((Object)(object)skillFamily == (Object)null || skillFamily.variants == null || skillFamily.variants.Length == 0)
		{
			return null;
		}
		int num = (int)skillFamily.defaultVariantIndex;
		if (num < 0 || num >= skillFamily.variants.Length)
		{
			num = 0;
		}
		return skillFamily.variants[num].skillDef;
	}

	private void UpdateSkillText()
	{
		GameObject val = BodyCatalog.FindBodyPrefab("CommandoBody");
		if ((Object)(object)val == (Object)null)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"CommandoBody missing for text update");
			return;
		}
		SkillLocator component = val.GetComponent<SkillLocator>();
		if ((Object)(object)component == (Object)null)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"SkillLocator missing");
			return;
		}
		SkillDef slotDefault = GetSlotDefault(component.primary);
		if ((Object)(object)slotDefault != (Object)null)
		{
			slotDefault.skillNameToken = "双重射击";
			slotDefault.skillDescriptionToken = "快速射击,造成 " + Pct(cfgPrimary.Value) + "% 伤害。";
			((BaseUnityPlugin)this).Logger.LogInfo((object)("primary text set on " + slotDefault.skillName + ": " + slotDefault.skillDescriptionToken));
		}
		else
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"primary skilldef null");
		}
		SkillDef slotDefault2 = GetSlotDefault(component.special);
		if ((Object)(object)slotDefault2 != (Object)null)
		{
			slotDefault2.skillNameToken = "压制射击";
			slotDefault2.skillDescriptionToken = "连续射击,每发造成 " + Pct(cfgBarrageDamage.Value) + "% 伤害。命中时依次附加:破甲、燃烧、流血。";
			((BaseUnityPlugin)this).Logger.LogInfo((object)("special text set on " + slotDefault2.skillName + ": " + slotDefault2.skillDescriptionToken));
		}
		else
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"special skilldef null");
		}
	}

	private void AddBlinkSkill()
	{
		GameObject val = BodyCatalog.FindBodyPrefab("CommandoBody");
		GameObject val2 = BodyCatalog.FindBodyPrefab("HuntressBody");
		if ((Object)(object)val == (Object)null || (Object)(object)val2 == (Object)null)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"body prefab missing");
			return;
		}
		SkillLocator component = val.GetComponent<SkillLocator>();
		SkillLocator component2 = val2.GetComponent<SkillLocator>();
		if ((Object)(object)component == (Object)null || (Object)(object)component.utility == (Object)null)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"commando locator missing");
			return;
		}
		if ((Object)(object)component2 == (Object)null || (Object)(object)component2.utility == (Object)null)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"huntress locator missing");
			return;
		}
		SkillDef slotDefault = GetSlotDefault(component2.utility);
		if ((Object)(object)slotDefault == (Object)null)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"huntress default utility skilldef null");
			return;
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)("huntress utility = " + slotDefault.skillName + " state=" + ((((SerializableEntityStateType)(ref slotDefault.activationState)).stateType == null) ? "null" : ((SerializableEntityStateType)(ref slotDefault.activationState)).stateType.Name) + " icon=" + (((Object)(object)slotDefault.icon == (Object)null) ? "null" : ((Object)slotDefault.icon).name)));
		SkillDef val3 = Object.Instantiate<SkillDef>(slotDefault);
		val3.baseRechargeInterval = cfgBlinkCooldown.Value;
		val3.baseMaxStock = 1;
		val3.skillName = "CMDBLINK";
		val3.skillNameToken = "闪现";
		val3.skillDescriptionToken = "向视角方向瞬移一段距离,冷却 " + cfgBlinkCooldown.Value.ToString("0.#", CultureInfo.InvariantCulture) + " 秒。";
		val3.canceledFromSprinting = false;
		val3.cancelSprintingOnActivation = false;
		SkillFamily skillFamily = component.utility.skillFamily;
		if ((Object)(object)skillFamily == (Object)null || skillFamily.variants == null)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"commando family missing");
			return;
		}
		SkillDef slotDefault2 = GetSlotDefault(component.utility);
		int num = -1;
		for (int i = 0; i < skillFamily.variants.Length; i++)
		{
			SkillDef skillDef = skillFamily.variants[i].skillDef;
			if (!((Object)(object)skillDef == (Object)null))
			{
				if ((Object)(object)slotDefault2 != (Object)null && (Object)(object)skillDef == (Object)(object)slotDefault2)
				{
					num = i;
					break;
				}
				if (((SerializableEntityStateType)(ref skillDef.activationState)).stateType == typeof(DodgeState))
				{
					num = i;
					break;
				}
			}
		}
		if (num < 0)
		{
			num = (int)skillFamily.defaultVariantIndex;
		}
		if (num < 0 || num >= skillFamily.variants.Length)
		{
			num = 0;
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)("replacing utility variant #" + num + " (was " + (((Object)(object)skillFamily.variants[num].skillDef == (Object)null) ? "null" : skillFamily.variants[num].skillDef.skillName) + ")"));
		skillFamily.variants[num].skillDef = val3;
		((BaseUnityPlugin)this).Logger.LogInfo((object)("blink installed into utility slot, cooldown " + cfgBlinkCooldown.Value + "s, icon " + (((Object)(object)val3.icon == (Object)null) ? "null" : ((Object)val3.icon).name)));
	}
}