Decompiled source of DrifterRandomizer v1.0.1

DrifterRandomizer/DrifterRandomizer.dll

Decompiled 2 months ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using Microsoft.CodeAnalysis;
using RoR2;
using RoR2.Projectile;
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("DrifterRandomProjectiles")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("DrifterRandomProjectiles")]
[assembly: AssemblyTitle("DrifterRandomProjectiles")]
[assembly: AssemblyVersion("1.0.0.0")]
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;
		}
	}
}
namespace DrifterRandomizer
{
	[BepInPlugin("com.Spig.DrifterRandomizer", "Drifter Randomizer", "1.0.0")]
	public class DrifterRandomizer : BaseUnityPlugin
	{
		public void Awake()
		{
			RoR2Application.onLoad = (Action)Delegate.Combine(RoR2Application.onLoad, new Action(PatchDrifterPrefab));
		}

		private void PatchDrifterPrefab()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Invalid comparison between Unknown and I4
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			BodyIndex val = BodyCatalog.FindBodyIndex("DrifterBody");
			if ((int)val != -1)
			{
				GameObject bodyPrefab = BodyCatalog.GetBodyPrefab(val);
				if (!Object.op_Implicit((Object)(object)bodyPrefab.GetComponent<DrifterChaosHandler>()))
				{
					bodyPrefab.AddComponent<DrifterChaosHandler>();
				}
			}
		}
	}
	public class DrifterChaosHandler : MonoBehaviour
	{
		[CompilerGenerated]
		private sealed class <InjectRoutine>d__3 : IEnumerator<object>, IEnumerator, IDisposable
		{
			private int <>1__state;

			private object <>2__current;

			public DrifterChaosHandler <>4__this;

			private Component <cleanupController>5__1;

			private BindingFlags <flags>5__2;

			private FieldInfo <selectionField>5__3;

			private object <selection>5__4;

			object IEnumerator<object>.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			object IEnumerator.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			[DebuggerHidden]
			public <InjectRoutine>d__3(int <>1__state)
			{
				this.<>1__state = <>1__state;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				<cleanupController>5__1 = null;
				<selectionField>5__3 = null;
				<selection>5__4 = null;
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				switch (<>1__state)
				{
				default:
					return false;
				case 0:
					<>1__state = -1;
					<>2__current = null;
					<>1__state = 1;
					return true;
				case 1:
					<>1__state = -1;
					<>2__current = null;
					<>1__state = 2;
					return true;
				case 2:
					<>1__state = -1;
					if (<>4__this.hasInjected)
					{
						return false;
					}
					<cleanupController>5__1 = ((Component)<>4__this).GetComponent("DrifterCleanupController");
					if (!Object.op_Implicit((Object)(object)<cleanupController>5__1))
					{
						return false;
					}
					<flags>5__2 = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
					<selectionField>5__3 = ((object)<cleanupController>5__1).GetType().GetField("projectileCandidateSelection", <flags>5__2);
					<selection>5__4 = <selectionField>5__3?.GetValue(<cleanupController>5__1);
					if (<selection>5__4 == null)
					{
						return false;
					}
					<>4__this.InjectChaos(<selection>5__4);
					<>4__this.hasInjected = true;
					return false;
				}
			}

			bool IEnumerator.MoveNext()
			{
				//ILSpy generated this explicit interface implementation from .override directive in MoveNext
				return this.MoveNext();
			}

			[DebuggerHidden]
			void IEnumerator.Reset()
			{
				throw new NotSupportedException();
			}
		}

		private const float ChaosWeight = 100f;

		private bool hasInjected = false;

		public void Start()
		{
			((MonoBehaviour)this).StartCoroutine(InjectRoutine());
		}

		[IteratorStateMachine(typeof(<InjectRoutine>d__3))]
		private IEnumerator InjectRoutine()
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <InjectRoutine>d__3(0)
			{
				<>4__this = this
			};
		}

		private void InjectChaos(object selection)
		{
			Type type = selection.GetType();
			Type type2 = type.GetGenericArguments()[0];
			MethodInfo method = type.GetMethod("AddChoice", new Type[2]
			{
				type2,
				typeof(float)
			});
			MethodInfo method2 = type.GetMethod("GetChoice", new Type[1] { typeof(int) });
			PropertyInfo property = type.GetProperty("Count");
			if (method == null || property == null || (int)property.GetValue(selection) == 0)
			{
				return;
			}
			object obj = method2.Invoke(selection, new object[1] { 0 });
			object value = obj.GetType().GetField("value").GetValue(obj);
			FieldInfo fieldInfo = AnalyzeCandidateFields(type2, value);
			if (fieldInfo == null)
			{
				return;
			}
			IEnumerable<GameObject> allProjectiles = GetAllProjectiles();
			foreach (GameObject item in allProjectiles)
			{
				if (Object.op_Implicit((Object)(object)item) && Object.op_Implicit((Object)(object)item.GetComponent<ProjectileController>()))
				{
					object obj2 = Activator.CreateInstance(type2);
					FieldInfo[] fields = type2.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
					foreach (FieldInfo fieldInfo2 in fields)
					{
						fieldInfo2.SetValue(obj2, fieldInfo2.GetValue(value));
					}
					fieldInfo.SetValue(obj2, item);
					try
					{
						method.Invoke(selection, new object[2] { obj2, 100f });
					}
					catch
					{
					}
				}
			}
		}

		private FieldInfo AnalyzeCandidateFields(Type type, object instance)
		{
			FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			foreach (FieldInfo fieldInfo in fields)
			{
				if (fieldInfo.FieldType == typeof(GameObject))
				{
					object? value = fieldInfo.GetValue(instance);
					GameObject val = (GameObject)((value is GameObject) ? value : null);
					if (Object.op_Implicit((Object)(object)val) && Object.op_Implicit((Object)(object)val.GetComponent<ProjectileController>()))
					{
						return fieldInfo;
					}
				}
			}
			return type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).FirstOrDefault((FieldInfo f) => f.FieldType == typeof(GameObject));
		}

		private IEnumerable<GameObject> GetAllProjectiles()
		{
			Type type = typeof(RoR2Application).Assembly.GetType("RoR2.ContentManagement.ContentManager");
			if (type != null)
			{
				MemberInfo memberInfo = (MemberInfo)(((object)type.GetProperty("projectilePrefabs", BindingFlags.Static | BindingFlags.Public)) ?? ((object)type.GetField("projectilePrefabs", BindingFlags.Static | BindingFlags.Public)));
				if (memberInfo != null)
				{
					object obj = ((memberInfo is PropertyInfo propertyInfo) ? propertyInfo.GetValue(null) : ((FieldInfo)memberInfo).GetValue(null));
					if (obj is IEnumerable<GameObject> result)
					{
						return result;
					}
					if (obj is GameObject[] result2)
					{
						return result2;
					}
				}
			}
			Type typeFromHandle = typeof(ProjectileCatalog);
			FieldInfo fieldInfo = typeFromHandle.GetField("projectilePrefabs", BindingFlags.Static | BindingFlags.Public) ?? typeFromHandle.GetField("projectilePrefabArray", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
			if (fieldInfo != null)
			{
				object value = fieldInfo.GetValue(null);
				if (value is IEnumerable<GameObject> result3)
				{
					return result3;
				}
				if (value is GameObject[] result4)
				{
					return result4;
				}
			}
			return Enumerable.Empty<GameObject>();
		}
	}
}