Decompiled source of UpgradesInLevels v1.0.0

UpgradesInLevels.dll

Decompiled a day 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 BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using UnityEngine;

[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("UpgradesInLevels")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+e4ae53f6071726b34dec60c3e79f7f5228ba3be2")]
[assembly: AssemblyProduct("UpgradesInLevels")]
[assembly: AssemblyTitle("UpgradesInLevels")]
[assembly: AssemblyVersion("1.0.0.0")]
[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 UpgradesInLevels
{
	[BepInPlugin("UpgradesInLevels", "UpgradesInLevels", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "UpgradesInLevels";

		public const string Version = "1.0.0";

		internal static ManualLogSource Log;

		internal static ConfigEntry<float> SpawnChancePercent;

		private void Awake()
		{
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			SpawnChancePercent = ((BaseUnityPlugin)this).Config.Bind<float>("General", "SpawnChancePercent", 2f, "Chance (en %) qu'un emplacement d'objet de valeur libre reçoive une amélioration.");
			new Harmony("UpgradesInLevels").PatchAll();
			Log.LogInfo((object)string.Format("{0} {1} chargé (SpawnChancePercent = {2}).", "UpgradesInLevels", "1.0.0", SpawnChancePercent.Value));
		}
	}
	[HarmonyPatch(typeof(ValuableDirector), "VolumesAndSwitchSetup")]
	internal static class UpgradeSpawner
	{
		private const float OccupiedDistance = 0.1f;

		private const string ExcludedUpgradeName = "Map Player Count";

		[HarmonyPostfix]
		private static void Postfix()
		{
			if (!SemiFunc.IsMasterClientOrSingleplayer() || !SemiFunc.RunIsLevel())
			{
				return;
			}
			try
			{
				SpawnUpgrades();
			}
			catch (Exception arg)
			{
				Plugin.Log.LogError((object)$"Échec de l'ajout d'améliorations dans le niveau : {arg}");
			}
		}

		private static void SpawnUpgrades()
		{
			float spawnChancePercent = GetSpawnChancePercent();
			if (spawnChancePercent <= 0f)
			{
				return;
			}
			List<Item> eligibleUpgrades = GetEligibleUpgrades();
			if (eligibleUpgrades.Count == 0)
			{
				Plugin.Log.LogError((object)"Aucune amélioration éligible trouvée dans StatsManager.itemDictionary : rien n'est ajouté.");
				return;
			}
			List<ValuableVolume> freeVolumes = GetFreeVolumes();
			int num = 0;
			foreach (ValuableVolume item in freeVolumes)
			{
				if (Random.value * 100f < spawnChancePercent && Spawn(PickWeighted(eligibleUpgrades), item))
				{
					num++;
				}
			}
			Plugin.Log.LogInfo((object)$"{freeVolumes.Count} emplacements libres, {num} améliorations ajoutées (taux {spawnChancePercent} %).");
		}

		private static float GetSpawnChancePercent()
		{
			float value = Plugin.SpawnChancePercent.Value;
			float num = (float.IsNaN(value) ? 0f : Mathf.Clamp(value, 0f, 100f));
			if (num != value)
			{
				Plugin.Log.LogWarning((object)$"SpawnChancePercent={value} hors [0, 100], valeur bornée à {num}.");
			}
			return num;
		}

		private static List<Item> GetEligibleUpgrades()
		{
			return StatsManager.instance.itemDictionary.Values.Where((Item item) => (Object)(object)item != (Object)null && (int)item.itemType == 3 && !item.disabled && (Object)(object)item.value != (Object)null && item.value.valueMin > 0f && ((Object)item).name.IndexOf("Map Player Count", StringComparison.OrdinalIgnoreCase) < 0).ToList();
		}

		private static Item PickWeighted(List<Item> upgrades)
		{
			float num = upgrades.Sum((Item item) => 1f / item.value.valueMin);
			float num2 = Random.value * num;
			foreach (Item upgrade in upgrades)
			{
				num2 -= 1f / upgrade.value.valueMin;
				if (num2 <= 0f)
				{
					return upgrade;
				}
			}
			return upgrades[upgrades.Count - 1];
		}

		private static List<ValuableVolume> GetFreeVolumes()
		{
			List<Vector3> occupied = (from o in Object.FindObjectsByType<ValuableObject>((FindObjectsSortMode)0)
				select ((Component)o).transform.position).Concat(from o in Object.FindObjectsByType<CosmeticWorldObject>((FindObjectsSortMode)0)
				select ((Component)o).transform.position).ToList();
			return (from volume in Object.FindObjectsByType<ValuableVolume>((FindObjectsSortMode)0)
				where (int)volume.VolumeType != 0 && !occupied.Any((Vector3 p) => Vector3.Distance(p, ((Component)volume).transform.position) < 0.1f)
				select volume).ToList();
		}

		private static bool Spawn(Item item, ValuableVolume volume)
		{
			//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_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			Vector3 position = ((Component)volume).transform.position;
			Quaternion val = ((Component)volume).transform.rotation * item.spawnRotationOffset;
			try
			{
				if (SemiFunc.IsMultiplayer())
				{
					PhotonNetwork.InstantiateRoomObject(((PrefabRef<GameObject>)(object)item.prefab).ResourcePath, position, val, (byte)0, (object[])null);
				}
				else
				{
					Object.Instantiate<GameObject>(((PrefabRef<GameObject>)(object)item.prefab).Prefab, position, val);
				}
				return true;
			}
			catch (Exception arg)
			{
				Plugin.Log.LogError((object)$"Échec de l'apparition de '{((Object)item).name}' à {position} : {arg}");
				return false;
			}
		}
	}
}