Decompiled source of AutoRainieLove v1.1.0

AutoRainieLove.dll

Decompiled a week ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.Networking;

[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: IgnoresAccessChecksTo("")]
[assembly: AssemblyCompany("Huang")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("AutoRainieLove")]
[assembly: AssemblyTitle("AutoRainieLove")]
[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 AutoRainieLove
{
	[BepInPlugin("com.Huang.AutoRainieLove", "雨爱自动播放", "2.1.7")]
	public class Plugin : BaseUnityPlugin
	{
		internal static ManualLogSource Log;

		private AudioSource audioSource;

		private AudioSource previewSource;

		private bool isPlayingRainie = false;

		private bool isPlayingNianshi = false;

		private bool isPreviewing = false;

		private bool hasEverHadTeammates = false;

		private bool hasBeenSoleSurvivor = false;

		private bool hasPlayedNianshi = false;

		private AudioClip cachedRainie = null;

		private AudioClip cachedNianshi = null;

		private float nextCheckTime = 0f;

		private float nianshiTimer = 0f;

		private bool lastRunIsLevel = false;

		public static ConfigEntry<bool> EnableModConfig;

		public static ConfigEntry<float> RainieVolumeConfig;

		public static ConfigEntry<float> NianshiVolumeConfig;

		public static ConfigEntry<bool> EnableNianshiConfig;

		public static ConfigEntry<float> NianshiDurationConfig;

		public static Plugin Instance { get; private set; }

		private void Awake()
		{
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Expected O, but got Unknown
			//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Expected O, but got Unknown
			//IL_010b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0115: Expected O, but got Unknown
			Instance = this;
			Log = ((BaseUnityPlugin)this).Logger;
			Log.LogInfo((object)"【雨爱自动播放】Mod 已加载!(v2.1.7 纯净日志版)");
			EnableModConfig = ((BaseUnityPlugin)this).Config.Bind<bool>("常规设置", "启用模组", true, "是否启用本模组(关闭后所有功能失效)");
			RainieVolumeConfig = ((BaseUnityPlugin)this).Config.Bind<float>("常规设置", "雨爱音量", 0.5f, new ConfigDescription("《雨爱》的音乐音量大小", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
			NianshiVolumeConfig = ((BaseUnityPlugin)this).Config.Bind<float>("常规设置", "念牢师音量", 0.5f, new ConfigDescription("《念牢师》的音乐音量大小", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
			EnableNianshiConfig = ((BaseUnityPlugin)this).Config.Bind<bool>("常规设置", "启用念牢师", true, "当全队彻底团灭时是否播放(需先触发过独活状态)");
			NianshiDurationConfig = ((BaseUnityPlugin)this).Config.Bind<float>("常规设置", "念牢师播放时长(秒)", 15f, new ConfigDescription("《念牢师》播放时长(秒)", (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 60f), Array.Empty<object>()));
			audioSource = ((Component)this).gameObject.AddComponent<AudioSource>();
			audioSource.playOnAwake = false;
			audioSource.spatialBlend = 0f;
			previewSource = ((Component)this).gameObject.AddComponent<AudioSource>();
			previewSource.playOnAwake = false;
			previewSource.spatialBlend = 0f;
			previewSource.loop = true;
			string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
			string text = Path.Combine(directoryName, "rainielove.ogg");
			string text2 = Path.Combine(directoryName, "nianlaoshi.ogg");
			if (File.Exists(text))
			{
				((MonoBehaviour)this).StartCoroutine(PreloadAudio(text, isRainie: true));
			}
			else
			{
				Log.LogError((object)("【严重错误】找不到音频: " + text));
			}
			if (File.Exists(text2))
			{
				((MonoBehaviour)this).StartCoroutine(PreloadAudio(text2, isRainie: false));
			}
			else
			{
				Log.LogError((object)("【严重错误】找不到音频: " + text2));
			}
		}

		private void Update()
		{
			if (!EnableModConfig.Value)
			{
				if (audioSource.isPlaying)
				{
					StopAllAudio();
				}
				if (isPreviewing)
				{
					StopPreview();
				}
				return;
			}
			if (Input.GetKeyDown((KeyCode)289))
			{
				if (isPreviewing)
				{
					StopPreview();
				}
				else
				{
					StartPreview();
				}
			}
			if (isPreviewing && (Object)(object)previewSource != (Object)null)
			{
				previewSource.volume = Mathf.Clamp01(RainieVolumeConfig.Value);
			}
			if ((Object)(object)audioSource != (Object)null)
			{
				if (isPlayingRainie)
				{
					audioSource.volume = Mathf.Clamp01(RainieVolumeConfig.Value);
				}
				else if (isPlayingNianshi)
				{
					audioSource.volume = Mathf.Clamp01(NianshiVolumeConfig.Value);
				}
			}
			if (isPlayingNianshi)
			{
				nianshiTimer -= Time.deltaTime;
				if (nianshiTimer <= 0f)
				{
					Log.LogInfo((object)"【重要事件】念牢师播放完毕");
					StopAllAudio();
				}
			}
			bool flag = SemiFunc.RunIsLevel();
			if (flag && !lastRunIsLevel && isPreviewing)
			{
				StopPreview();
			}
			lastRunIsLevel = flag;
			if (!flag)
			{
				if (audioSource.isPlaying)
				{
					StopAllAudio();
				}
				hasEverHadTeammates = false;
				hasBeenSoleSurvivor = false;
				hasPlayedNianshi = false;
			}
			else if (Time.time >= nextCheckTime)
			{
				CheckSurvivorStatus();
				nextCheckTime = Time.time + 0.5f;
			}
		}

		public void CheckSurvivorStatus()
		{
			PlayerAvatar instance = PlayerAvatar.instance;
			if ((Object)(object)instance == (Object)null)
			{
				return;
			}
			List<PlayerAvatar> playerList = GameDirector.instance.PlayerList;
			if (playerList == null || playerList.Count == 0)
			{
				return;
			}
			if (playerList.Count >= 2)
			{
				hasEverHadTeammates = true;
			}
			int num = 0;
			bool deadSet = instance.deadSet;
			foreach (PlayerAvatar item in playerList)
			{
				if ((Object)(object)item != (Object)null && !item.deadSet)
				{
					num++;
				}
			}
			if (deadSet && num == 0 && hasEverHadTeammates && hasBeenSoleSurvivor)
			{
				if (EnableNianshiConfig.Value && !hasPlayedNianshi)
				{
					hasPlayedNianshi = true;
					Log.LogInfo((object)"【重要事件】全队彻底团灭,播放念牢师!");
					PlayNianshi();
				}
				else if (!EnableNianshiConfig.Value && isPlayingRainie)
				{
					StopAllAudio();
				}
			}
			else
			{
				if (deadSet)
				{
					return;
				}
				if (hasEverHadTeammates && num == 1)
				{
					hasBeenSoleSurvivor = true;
					if (!isPlayingRainie && !isPlayingNianshi)
					{
						Log.LogInfo((object)"【重要事件】队友全灭,你是幸存者,播放雨爱!");
						PlayRainie();
					}
				}
				else if (num >= 2)
				{
					if (isPlayingRainie || isPlayingNianshi)
					{
						Log.LogInfo((object)"【重要事件】有队友存活,停止自动播放。");
						StopAllAudio();
					}
					hasBeenSoleSurvivor = false;
				}
			}
		}

		private void PlayRainie()
		{
			if ((Object)(object)cachedRainie == (Object)null)
			{
				Log.LogError((object)"【严重错误】《雨爱》音频未加载!");
				return;
			}
			if (isPreviewing)
			{
				StopPreview();
			}
			isPlayingRainie = true;
			isPlayingNianshi = false;
			audioSource.volume = Mathf.Clamp01(RainieVolumeConfig.Value);
			audioSource.loop = true;
			audioSource.clip = cachedRainie;
			audioSource.Play();
		}

		private void PlayNianshi()
		{
			if ((Object)(object)cachedNianshi == (Object)null)
			{
				Log.LogError((object)"【严重错误】《念牢师》音频未加载!");
				return;
			}
			if (isPreviewing)
			{
				StopPreview();
			}
			isPlayingRainie = false;
			isPlayingNianshi = true;
			nianshiTimer = NianshiDurationConfig.Value;
			audioSource.volume = Mathf.Clamp01(NianshiVolumeConfig.Value);
			audioSource.loop = false;
			audioSource.clip = cachedNianshi;
			audioSource.Play();
		}

		public void StopAllAudio()
		{
			isPlayingRainie = false;
			isPlayingNianshi = false;
			if ((Object)(object)audioSource != (Object)null && audioSource.isPlaying)
			{
				audioSource.Stop();
			}
		}

		private void StartPreview()
		{
			if ((Object)(object)cachedRainie == (Object)null)
			{
				Log.LogError((object)"【严重错误】《雨爱》音频未加载,无法试听!");
			}
			else if (!isPlayingRainie && !isPlayingNianshi)
			{
				isPreviewing = true;
				previewSource.volume = Mathf.Clamp01(RainieVolumeConfig.Value);
				previewSource.loop = true;
				previewSource.clip = cachedRainie;
				previewSource.Play();
			}
		}

		private void StopPreview()
		{
			isPreviewing = false;
			if ((Object)(object)previewSource != (Object)null && previewSource.isPlaying)
			{
				previewSource.Stop();
			}
		}

		private IEnumerator PreloadAudio(string path, bool isRainie)
		{
			string uriPath;
			try
			{
				uriPath = new Uri(path).AbsoluteUri;
			}
			catch (Exception ex)
			{
				Exception ex2 = ex;
				Log.LogError((object)("【严重错误】路径解析失败: " + path + " - " + ex2.Message));
				yield break;
			}
			UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip(uriPath, (AudioType)14);
			try
			{
				yield return www.SendWebRequest();
				if ((int)www.result == 1)
				{
					if (isRainie)
					{
						cachedRainie = DownloadHandlerAudioClip.GetContent(www);
					}
					else
					{
						cachedNianshi = DownloadHandlerAudioClip.GetContent(www);
					}
					Log.LogInfo((object)("【加载完成】" + Path.GetFileName(path)));
				}
				else
				{
					Log.LogError((object)("【严重错误】" + Path.GetFileName(path) + " 加载失败: " + www.error));
				}
			}
			finally
			{
				((IDisposable)www)?.Dispose();
			}
		}
	}
}