Decompiled source of DroneCombinerSpawn v1.0.1

BepInEx\plugins\DroneCombinerSpawn\DroneCombinerSpawn.dll

Decompiled a day ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using DroneCombinerSpawn.Patches;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using RoR2;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.Networking;
using UnityEngine.ResourceManagement.AsyncOperations;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("DroneCombinerSpawn")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("DroneCombinerSpawn")]
[assembly: AssemblyTitle("DroneCombinerSpawn")]
[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 DroneCombinerSpawn
{
	[BepInPlugin("com.ror2.DroneCombinerSpawn", "DroneCombinerSpawn", "1.0.1")]
	public sealed class DroneCombinerSpawnPlugin : BaseUnityPlugin
	{
		public const string PluginGUID = "com.ror2.DroneCombinerSpawn";

		public const string PluginName = "DroneCombinerSpawn";

		public const string PluginVersion = "1.0.1";

		internal static DroneCombinerSpawnPlugin Instance { get; private set; }

		internal static ManualLogSource Log
		{
			get
			{
				DroneCombinerSpawnPlugin instance = Instance;
				if (instance == null)
				{
					return null;
				}
				return ((BaseUnityPlugin)instance).Logger;
			}
		}

		internal static ModConfig ModSettings { get; private set; }

		private void Awake()
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Expected O, but got Unknown
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Expected O, but got Unknown
			Instance = this;
			ModSettings = new ModConfig((BaseUnityPlugin)(object)this);
			Harmony val = new Harmony("com.ror2.DroneCombinerSpawn");
			MethodInfo methodInfo = AccessTools.Method(typeof(SceneDirector), "PopulateScene", (Type[])null, (Type[])null);
			if (methodInfo == null)
			{
				Log.LogError((object)"DroneCombinerSpawn: 找不到 RoR2.SceneDirector.PopulateScene,补丁未挂载。");
				return;
			}
			val.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(SceneDirectorPatch), "Prefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			Log.LogInfo((object)string.Format("{0} v{1} 已加载(仅主机生效,客机无需安装)。几率={2:P0},最低资源点={3}", "DroneCombinerSpawn", "1.0.1", ModSettings.SpawnChance.Value, ModSettings.MinCredits.Value));
		}
	}
	internal sealed class ModConfig
	{
		public ConfigEntry<float> SpawnChance { get; }

		public ConfigEntry<int> MinCredits { get; }

		public ModConfig(BaseUnityPlugin plugin)
		{
			SpawnChance = plugin.Config.Bind<float>("General", "SpawnChance", 0.5f, "无人机组合站的免费生成几率(0~1)。");
			MinCredits = plugin.Config.Bind<int>("General", "MinCredits", 1, "关卡至少要有多少资源点才会触发生成。设为 0 表示总是尝试。");
		}
	}
}
namespace DroneCombinerSpawn.Patches
{
	[HarmonyPatch(typeof(SceneDirector), "PopulateScene")]
	public static class SceneDirectorPatch
	{
		private const string CombinerCardAddress = "RoR2/DLC3/DroneCombinerStation/iscDroneCombinerStation.asset";

		private static InteractableSpawnCard _card;

		private static AsyncOperationHandle<InteractableSpawnCard> _handle;

		private static bool _loadStarted;

		private static bool _loadFailed;

		private static bool _loggedMissing;

		public static bool Prefix(SceneDirector __instance)
		{
			try
			{
				TrySpawnFreeCombiner(__instance);
			}
			catch (Exception arg)
			{
				ManualLogSource log = DroneCombinerSpawnPlugin.Log;
				if (log != null)
				{
					log.LogError((object)$"DroneCombinerSpawn: 免费生成无人机组合站时出错:{arg}");
				}
			}
			return true;
		}

		private static void TrySpawnFreeCombiner(SceneDirector director)
		{
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c1: 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_00ca: Expected O, but got Unknown
			//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Expected O, but got Unknown
			//IL_012a: Unknown result type (might be due to invalid IL or missing references)
			//IL_012f: Unknown result type (might be due to invalid IL or missing references)
			//IL_013b: Expected O, but got Unknown
			if ((Object)(object)director == (Object)null || !NetworkServer.active)
			{
				return;
			}
			ModConfig modSettings = DroneCombinerSpawnPlugin.ModSettings;
			if (modSettings == null || (Object)(object)((Component)director).GetComponent<CombinerSpawnedMarker>() != (Object)null || director.interactableCredit < modSettings.MinCredits.Value)
			{
				return;
			}
			((Component)director).gameObject.AddComponent<CombinerSpawnedMarker>();
			float num = Mathf.Clamp01(modSettings.SpawnChance.Value);
			if (num <= 0f)
			{
				return;
			}
			object? obj = AccessTools.Field(typeof(SceneDirector), "rng")?.GetValue(director);
			Xoroshiro128Plus val = (Xoroshiro128Plus)((obj is Xoroshiro128Plus) ? obj : null);
			if (val == null || val.nextNormalizedFloat >= num)
			{
				return;
			}
			InteractableSpawnCard card = GetCard();
			if ((Object)(object)card == (Object)null)
			{
				return;
			}
			DirectorCore instance = DirectorCore.instance;
			if ((Object)(object)instance == (Object)null)
			{
				return;
			}
			DirectorPlacementRule val2 = new DirectorPlacementRule
			{
				placementMode = (PlacementMode)4
			};
			GameObject val3 = instance.TrySpawnObject(new DirectorSpawnRequest((SpawnCard)(object)card, val2, val));
			if ((Object)(object)val3 == (Object)null)
			{
				ManualLogSource log = DroneCombinerSpawnPlugin.Log;
				if (log != null)
				{
					log.LogWarning((object)"DroneCombinerSpawn: 无人机组合站生成失败(找不到可用位置?)。");
				}
				return;
			}
			(AccessTools.Field(typeof(SceneDirector), "onSpawnInteractableServer")?.GetValue(null) as Action<SceneDirector, GameObject, DirectorCard>)?.Invoke(director, val3, new DirectorCard
			{
				spawnCard = (SpawnCard)(object)card
			});
			ManualLogSource log2 = DroneCombinerSpawnPlugin.Log;
			if (log2 != null)
			{
				log2.LogInfo((object)"DroneCombinerSpawn: 已免费生成一个无人机组合站。");
			}
		}

		private static InteractableSpawnCard GetCard()
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Invalid comparison between Unknown and I4
			if ((Object)(object)_card != (Object)null)
			{
				return _card;
			}
			if (_loadFailed)
			{
				return null;
			}
			if (!_loadStarted)
			{
				_loadStarted = true;
				try
				{
					_handle = Addressables.LoadAssetAsync<InteractableSpawnCard>((object)"RoR2/DLC3/DroneCombinerStation/iscDroneCombinerStation.asset");
				}
				catch (Exception ex)
				{
					_loadFailed = true;
					ManualLogSource log = DroneCombinerSpawnPlugin.Log;
					if (log != null)
					{
						log.LogWarning((object)("DroneCombinerSpawn: 无法加载 RoR2/DLC3/DroneCombinerStation/iscDroneCombinerStation.asset(未安装 Seekers of the Storm DLC?):" + ex.Message));
					}
					return null;
				}
			}
			try
			{
				_handle.WaitForCompletion();
			}
			catch (Exception ex2)
			{
				_loadFailed = true;
				ManualLogSource log2 = DroneCombinerSpawnPlugin.Log;
				if (log2 != null)
				{
					log2.LogWarning((object)("DroneCombinerSpawn: 等待资产加载失败:" + ex2.Message));
				}
				return null;
			}
			if ((int)_handle.Status != 1)
			{
				_loadFailed = true;
				if (!_loggedMissing)
				{
					_loggedMissing = true;
					ManualLogSource log3 = DroneCombinerSpawnPlugin.Log;
					if (log3 != null)
					{
						log3.LogWarning((object)string.Format("DroneCombinerSpawn: 资产 {0} 加载失败(未安装 Seekers of the Storm DLC?):{1}", "RoR2/DLC3/DroneCombinerStation/iscDroneCombinerStation.asset", _handle.OperationException));
					}
				}
				return null;
			}
			_card = _handle.Result;
			if ((Object)(object)_card == (Object)null)
			{
				_loadFailed = true;
				ManualLogSource log4 = DroneCombinerSpawnPlugin.Log;
				if (log4 != null)
				{
					log4.LogWarning((object)"DroneCombinerSpawn: 资产 RoR2/DLC3/DroneCombinerStation/iscDroneCombinerStation.asset 结果为空。");
				}
			}
			return _card;
		}
	}
	public sealed class CombinerSpawnedMarker : MonoBehaviour
	{
	}
}