Decompiled source of ArtifactOfPandemonium v1.0.0

plugins/ArtifactOfPandemonium/ArtifactOfPandemonium.dll

Decompiled 6 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using On.RoR2;
using R2API;
using RoR2;
using UnityEngine;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("ArtifactOfPandemonium")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+180840df69953a38787aa2115cabb0ae71d94281")]
[assembly: AssemblyProduct("ArtifactOfPandemonium")]
[assembly: AssemblyTitle("ArtifactOfPandemonium")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace ArtifactOfPandemonium
{
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInPlugin("AuthorName.ArtifactOfPandemonium", "ArtifactOfPandemonium", "1.0.0")]
	public class ArtifactOfPandemoniumPlugin : BaseUnityPlugin
	{
		public const string PluginGUID = "AuthorName.ArtifactOfPandemonium";

		public const string PluginAuthor = "AuthorName";

		public const string PluginName = "ArtifactOfPandemonium";

		public const string PluginVersion = "1.0.0";

		private const int MinimumOtherArtifacts = 2;

		private const int GuaranteedEnableCount = 2;

		private const float InitialEnableChance = 0.35f;

		private const float CreditBonusMultiplier = 1.25f;

		private const float MoveSpeedBonusMultiplier = 1.1f;

		private const float MoneyBonusMultiplier = 1.1f;

		private const float TruePandemoniumFixedTime = 99999f;

		private static ArtifactDef artifactDef;

		private static BuffDef blessingBuffDef;

		public void Awake()
		{
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_0144: Unknown result type (might be due to invalid IL or missing references)
			//IL_014e: Expected O, but got Unknown
			Log.Init(((BaseUnityPlugin)this).Logger);
			artifactDef = ScriptableObject.CreateInstance<ArtifactDef>();
			artifactDef.cachedName = "ARTIFACT_PANDEMONIUM";
			artifactDef.nameToken = "ARTIFACT_PANDEMONIUM_NAME";
			artifactDef.descriptionToken = "ARTIFACT_PANDEMONIUM_DESC";
			LanguageAPI.Add("ARTIFACT_PANDEMONIUM_NAME", "Artifact of Pandemonium");
			LanguageAPI.Add("ARTIFACT_PANDEMONIUM_DESC", "Randomly selects a number of other unlocked artifacts to be enabled. Players are granted Petrichor V's Blessing.");
			artifactDef.smallIconSelectedSprite = LoadSprite("ArtifactOfPandemonium.Resources.icon_selected.png");
			artifactDef.smallIconDeselectedSprite = LoadSprite("ArtifactOfPandemonium.Resources.icon_deselected.png");
			ContentAddition.AddArtifactDef(artifactDef);
			blessingBuffDef = ScriptableObject.CreateInstance<BuffDef>();
			((Object)blessingBuffDef).name = "PETRICHOR_V_BLESSING";
			blessingBuffDef.buffColor = new Color(0.4f, 0.75f, 1f);
			blessingBuffDef.canStack = false;
			blessingBuffDef.isDebuff = false;
			blessingBuffDef.isHidden = false;
			blessingBuffDef.iconSprite = LoadSprite("ArtifactOfPandemonium.Resources.icon_selected.png");
			ContentAddition.AddBuffDef(blessingBuffDef);
			Run.onRunStartGlobal += OnRunStart;
			DirectorAPI.StageSettingsActions += OnStageSettings;
			CharacterBody.onBodyStartGlobal += OnBodyStartGlobal;
			CharacterMaster.GiveMoney += new hook_GiveMoney(OnGiveMoney);
			Log.Info("ArtifactOfPandemonium loaded.");
		}

		private void OnRunStart(Run run)
		{
			if (NetworkServer.active && IsBlessingActive())
			{
				RunArtifactManager.instance.SetArtifactEnabledServer(Artifacts.Sacrifice, false);
				LocalUser firstLocalUser = LocalUserManager.GetFirstLocalUser();
				UserProfile profile = ((firstLocalUser != null) ? firstLocalUser.userProfile : null);
				List<ArtifactDef> list = ArtifactCatalog.artifactDefs.Where((ArtifactDef a) => (Object)(object)a != (Object)(object)artifactDef && (Object)(object)a != (Object)(object)Artifacts.Sacrifice && IsUnlocked(a, profile)).ToList();
				if (list.Count < 2)
				{
					UnleashTruePandemonium(run);
				}
				else
				{
					RandomizeArtifacts(list);
				}
			}
		}

		private static bool IsUnlocked(ArtifactDef artifact, UserProfile profile)
		{
			if ((Object)(object)artifact.unlockableDef == (Object)null)
			{
				return true;
			}
			if (profile != null)
			{
				return profile.HasUnlockable(artifact.unlockableDef);
			}
			return false;
		}

		private void RandomizeArtifacts(List<ArtifactDef> artifacts)
		{
			for (int num = artifacts.Count - 1; num > 0; num--)
			{
				int num2 = Random.Range(0, num + 1);
				int index = num;
				int index2 = num2;
				ArtifactDef value = artifacts[num2];
				ArtifactDef value2 = artifacts[num];
				artifacts[index] = value;
				artifacts[index2] = value2;
			}
			int num3 = Mathf.Min(2, artifacts.Count);
			for (int i = 0; i < num3; i++)
			{
				RunArtifactManager.instance.SetArtifactEnabledServer(artifacts[i], true);
			}
			float num4 = 0.35f;
			for (int j = num3; j < artifacts.Count; j++)
			{
				bool flag = Random.value < num4;
				RunArtifactManager.instance.SetArtifactEnabledServer(artifacts[j], flag);
				if (flag)
				{
					num4 *= 0.5f;
				}
			}
			Log.Info($"Pandemonium: randomized {artifacts.Count} other artifacts.");
			foreach (ArtifactDef artifact in artifacts)
			{
				bool flag2 = RunArtifactManager.instance.IsArtifactEnabled(artifact);
				Log.Info("  " + artifact.cachedName + ": " + (flag2 ? "ENABLED" : "disabled"));
			}
		}

		private void UnleashTruePandemonium(Run run)
		{
			Chat.AddMessage("<color=#ff2222>Too few artifacts are unlocked for Pandemonium to choose from. True pandemonium will be unleashed...</color>");
			Log.Warning("Pandemonium: fewer than 2 other unlocked artifacts found. Forcing max enemy level.");
			run.NetworkfixedTime = 99999f;
		}

		private void OnStageSettings(StageSettings settings, StageInfo stageInfo)
		{
			if (IsBlessingActive())
			{
				settings.SceneDirectorInteractableCredits = Mathf.RoundToInt((float)settings.SceneDirectorInteractableCredits * 1.25f);
			}
		}

		private static bool IsBlessingActive()
		{
			if ((Object)(object)RunArtifactManager.instance != (Object)null)
			{
				return RunArtifactManager.instance.IsArtifactEnabled(artifactDef);
			}
			return false;
		}

		private void OnBodyStartGlobal(CharacterBody body)
		{
			if (IsBlessingActive() && body.isPlayerControlled)
			{
				body.AddBuff(blessingBuffDef);
				body.onRecalculateStats += OnRecalculateStats;
			}
		}

		private void OnRecalculateStats(CharacterBody body)
		{
			if (body.HasBuff(blessingBuffDef))
			{
				body.moveSpeed *= 1.1f;
			}
		}

		private void OnGiveMoney(orig_GiveMoney orig, CharacterMaster self, uint amount)
		{
			if (IsBlessingActive())
			{
				amount = (uint)Mathf.RoundToInt((float)amount * 1.1f);
			}
			orig.Invoke(self, amount);
		}

		private static Sprite LoadSprite(string resourceName)
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Expected O, but got Unknown
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			using Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
			byte[] array = new byte[stream.Length];
			stream.Read(array, 0, array.Length);
			Texture2D val = new Texture2D(256, 256);
			ImageConversion.LoadImage(val, array);
			return Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), new Vector2(0.5f, 0.5f));
		}
	}
	internal static class Log
	{
		private static ManualLogSource _logSource;

		internal static void Init(ManualLogSource logSource)
		{
			_logSource = logSource;
		}

		internal static void Debug(object data)
		{
			_logSource.LogDebug(data);
		}

		internal static void Error(object data)
		{
			_logSource.LogError(data);
		}

		internal static void Fatal(object data)
		{
			_logSource.LogFatal(data);
		}

		internal static void Info(object data)
		{
			_logSource.LogInfo(data);
		}

		internal static void Message(object data)
		{
			_logSource.LogMessage(data);
		}

		internal static void Warning(object data)
		{
			_logSource.LogWarning(data);
		}
	}
}