Decompiled source of GeneticVariantsPatch v0.4.0

GeneticVariantsPatch.dll

Decompiled 2 months ago
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GeneticsArtifact;
using On.RoR2;
using R2API.Utils;
using RoR2;
using UnityEngine;
using UnityEngine.Networking;
using VAPI;
using VAPI.Components;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("GeneticVariantsPatch")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("GeneticVariantsPatch")]
[assembly: AssemblyTitle("GeneticVariantsPatch")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace GeneticVariantsPatch;

[BepInPlugin("com.RicoValdezio.GeneticVariantsPatch", "GenVarPatch", "0.4.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
public class GeneticVariantsPatchPlugin : BaseUnityPlugin
{
	public const string ModVer = "0.4.0";

	public const string ModName = "GenVarPatch";

	public const string ModGuid = "com.RicoValdezio.GeneticVariantsPatch";

	public static GeneticVariantsPatchPlugin Instance;

	internal static ManualLogSource LogSource;

	public static ConfigEntry<bool> enableChanceTweaking;

	public static ConfigEntry<bool> enableGeneBlocking;

	public static ConfigEntry<float> maxGeneChanceInfluence;

	private void Awake()
	{
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_0051: Unknown result type (might be due to invalid IL or missing references)
		//IL_005b: Expected O, but got Unknown
		//IL_005b: Expected O, but got Unknown
		//IL_0070: Unknown result type (might be due to invalid IL or missing references)
		//IL_008f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0099: Expected O, but got Unknown
		//IL_0099: Expected O, but got Unknown
		//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cd: Expected O, but got Unknown
		//IL_00cd: Expected O, but got Unknown
		if ((Object)(object)Instance == (Object)null)
		{
			Instance = this;
		}
		LogSource = ((BaseUnityPlugin)Instance).Logger;
		enableChanceTweaking = ((BaseUnityPlugin)this).Config.Bind<bool>(new ConfigDefinition("General Settings", "Enable Variant Chance Tweaking"), true, new ConfigDescription("If true, the patch will allow the fitness algorithm to change the chance of variants spawning", (AcceptableValueBase)(object)new AcceptableValueList<bool>(new bool[2] { true, false }), Array.Empty<object>()));
		enableGeneBlocking = ((BaseUnityPlugin)this).Config.Bind<bool>(new ConfigDefinition("General Settings", "Enable Variant Gene Blocking"), true, new ConfigDescription("If true, the patch will prevent variants from receiving genetic bonus and from participating in the fitness algorithm", (AcceptableValueBase)(object)new AcceptableValueList<bool>(new bool[2] { true, false }), Array.Empty<object>()));
		maxGeneChanceInfluence = ((BaseUnityPlugin)this).Config.Bind<float>(new ConfigDefinition("General Settings", "Maximum Gene Chance Effect"), 0.5f, new ConfigDescription("The max difference percentage (as a decimal) that the algorithm can change the variant spawn rate - 0.5 is ±50%", (AcceptableValueBase)null, Array.Empty<object>()));
		GeneVariantDriver.RegisterHooks();
	}
}
public class GeneVariantBehaviour
{
	public BodyIndex bodyIndex;

	internal MasterGeneBehaviour masterGene;

	internal VariantDef variantDef;

	public string variantName;

	public float originalSpawnRate;

	public float fitness;

	public double totalScore;

	public void EvaluateFitness()
	{
		float num = masterGene.templateGenes[(GeneStat)0];
		float num2 = masterGene.templateGenes[(GeneStat)1];
		float num3 = masterGene.templateGenes[(GeneStat)2];
		float num4 = masterGene.templateGenes[(GeneStat)3];
		float num5 = Mathf.Min(num / variantDef.healthMultiplier, variantDef.healthMultiplier / num);
		float num6 = Mathf.Min(num2 / variantDef.moveSpeedMultiplier, variantDef.moveSpeedMultiplier / num2);
		float num7 = Mathf.Min(num3 / variantDef.attackSpeedMultiplier, variantDef.attackSpeedMultiplier / num3);
		float num8 = Mathf.Min(num4 / variantDef.damageMultiplier, variantDef.damageMultiplier / num4);
		totalScore = num5 + num6 + num7 + num8;
		fitness = (float)(2.0 / Math.Pow(Math.Cosh(totalScore - 4.0), 1.6));
		variantDef.spawnRate = Mathf.Clamp(originalSpawnRate * fitness, originalSpawnRate * (1f - GeneticVariantsPatchPlugin.maxGeneChanceInfluence.Value), originalSpawnRate * (1f + GeneticVariantsPatchPlugin.maxGeneChanceInfluence.Value));
	}

	public void ResetSpawnRate()
	{
		variantDef.spawnRate = originalSpawnRate;
	}
}
public class GeneVariantDriver : NetworkBehaviour
{
	public static GeneVariantDriver instance;

	internal static GeneVariantBehaviour[] geneVariantBehaviours;

	public static float timeSinceUpdate;

	internal static void RegisterHooks()
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: Expected O, but got Unknown
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0044: Expected O, but got Unknown
		Run.Start += new hook_Start(Run_Start);
		Run.onRunDestroyGlobal += Run_onRunDestroyGlobal;
		Stage.onServerStageBegin += Stage_onServerStageBegin;
		CharacterBody.RecalculateStats += new hook_RecalculateStats(CharacterBody_RecalculateStats);
	}

	private static void Run_Start(orig_Start orig, Run self)
	{
		//IL_006a: 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_0090: Unknown result type (might be due to invalid IL or missing references)
		//IL_0092: Unknown result type (might be due to invalid IL or missing references)
		orig.Invoke(self);
		timeSinceUpdate = 0f;
		if (NetworkServer.active)
		{
			instance = ((Component)self).gameObject.AddComponent<GeneVariantDriver>();
			VariantDef[] obj = (VariantDef[])typeof(VariantCatalog).GetField("_registeredVariants", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null);
			geneVariantBehaviours = new GeneVariantBehaviour[obj.Length];
			int num = 0;
			VariantDef[] array = obj;
			foreach (VariantDef val in array)
			{
				BodyIndex bodyIndex = BodyCatalog.FindBodyIndex(val.bodyName);
				geneVariantBehaviours[num] = new GeneVariantBehaviour
				{
					variantDef = val,
					originalSpawnRate = val.spawnRate,
					bodyIndex = bodyIndex,
					variantName = ((Object)val).name
				};
				num++;
			}
		}
	}

	private static void Run_onRunDestroyGlobal(Run obj)
	{
		if (NetworkServer.active)
		{
			GeneVariantBehaviour[] array = geneVariantBehaviours;
			for (int i = 0; i < array.Length; i++)
			{
				array[i].ResetSpawnRate();
			}
		}
	}

	private static void Stage_onServerStageBegin(Stage obj)
	{
		if (NetworkServer.active)
		{
			UpdateAllGeneVariants();
		}
	}

	private static void CharacterBody_RecalculateStats(orig_RecalculateStats orig, CharacterBody self)
	{
		if (NetworkServer.active && GeneticVariantsPatchPlugin.enableGeneBlocking.Value)
		{
			Inventory inventory = self.inventory;
			if (inventory != null && inventory.GetItemCount(GeneTokens.blockerDef) == 0)
			{
				BodyVariantManager component = ((Component)self).GetComponent<BodyVariantManager>();
				if (component != null && component.variantsInBody?.Count > 0)
				{
					self.inventory.GiveItem(GeneTokens.blockerDef, 1);
				}
			}
		}
		orig.Invoke(self);
	}

	private void Update()
	{
		if (NetworkServer.active)
		{
			timeSinceUpdate += Time.deltaTime;
			if (timeSinceUpdate >= 60f)
			{
				UpdateAllGeneVariants();
			}
		}
	}

	public static void UpdateAllGeneVariants()
	{
		if (!NetworkServer.active || !RunArtifactManager.instance.IsArtifactEnabled(ArtifactOfGenetics.artifactDef))
		{
			return;
		}
		GeneVariantBehaviour[] array = geneVariantBehaviours;
		foreach (GeneVariantBehaviour behaviour in array)
		{
			if (behaviour.masterGene == null && !GeneEngineDriver.masterGenes.Any((MasterGeneBehaviour master) => master.bodyIndex == behaviour.bodyIndex))
			{
				continue;
			}
			if (behaviour.masterGene == null)
			{
				behaviour.masterGene = GeneEngineDriver.masterGenes.First((MasterGeneBehaviour master) => master.bodyIndex == behaviour.bodyIndex);
			}
			if (GeneticVariantsPatchPlugin.enableChanceTweaking.Value)
			{
				behaviour.EvaluateFitness();
			}
		}
		timeSinceUpdate = 0f;
	}
}