Decompiled source of KokorikiOsts v1.45.320

SmesharikiOsts.dll

Decompiled a month ago
using System;
using System.Collections;
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 UnityEngine;
using UnityEngine.SceneManagement;

[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: AssemblyCompany("SmesharikiOsts")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("My first plugin")]
[assembly: AssemblyTitle("SmesharikiOsts")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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;
		}
	}
}
[BepInPlugin("Mxyfell.audioreplacer", "SmesharikiSongs", "1.0")]
public class AudioReplacer : BaseUnityPlugin
{
	[CompilerGenerated]
	private sealed class <DelayedAudioReplacement>d__10 : IEnumerator<object>, IEnumerator, IDisposable
	{
		private int <>1__state;

		private object <>2__current;

		public AudioReplacer <>4__this;

		object IEnumerator<object>.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		object IEnumerator.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		[DebuggerHidden]
		public <DelayedAudioReplacement>d__10(int <>1__state)
		{
			this.<>1__state = <>1__state;
		}

		[DebuggerHidden]
		void IDisposable.Dispose()
		{
			<>1__state = -2;
		}

		private bool MoveNext()
		{
			switch (<>1__state)
			{
			default:
				return false;
			case 0:
				<>1__state = -1;
				<>2__current = null;
				<>1__state = 1;
				return true;
			case 1:
				<>1__state = -1;
				<>4__this.ReplaceAllAudioInGame();
				return false;
			}
		}

		bool IEnumerator.MoveNext()
		{
			//ILSpy generated this explicit interface implementation from .override directive in MoveNext
			return this.MoveNext();
		}

		[DebuggerHidden]
		void IEnumerator.Reset()
		{
			throw new NotSupportedException();
		}
	}

	[CompilerGenerated]
	private sealed class <Initialize>d__7 : IEnumerator<object>, IEnumerator, IDisposable
	{
		private int <>1__state;

		private object <>2__current;

		public AudioReplacer <>4__this;

		object IEnumerator<object>.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		object IEnumerator.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		[DebuggerHidden]
		public <Initialize>d__7(int <>1__state)
		{
			this.<>1__state = <>1__state;
		}

		[DebuggerHidden]
		void IDisposable.Dispose()
		{
			<>1__state = -2;
		}

		private bool MoveNext()
		{
			switch (<>1__state)
			{
			default:
				return false;
			case 0:
				<>1__state = -1;
				if (_isInitialized)
				{
					return false;
				}
				<>2__current = <>4__this.LoadAudioBundle();
				<>1__state = 1;
				return true;
			case 1:
				<>1__state = -1;
				if (_bundleLoaded)
				{
					<>4__this.ReplaceAllAudioInGame();
					_logger.LogInfo((object)$"Audio Replacer initialized! Loaded {BundleClips.Count} clips");
				}
				else
				{
					_logger.LogError((object)"Failed to load audio bundle!");
				}
				_isInitialized = true;
				return false;
			}
		}

		bool IEnumerator.MoveNext()
		{
			//ILSpy generated this explicit interface implementation from .override directive in MoveNext
			return this.MoveNext();
		}

		[DebuggerHidden]
		void IEnumerator.Reset()
		{
			throw new NotSupportedException();
		}
	}

	[CompilerGenerated]
	private sealed class <LoadAudioBundle>d__8 : IEnumerator<object>, IEnumerator, IDisposable
	{
		private int <>1__state;

		private object <>2__current;

		public AudioReplacer <>4__this;

		private string <bundlePath>5__1;

		private AssetBundleCreateRequest <bundleCreateRequest>5__2;

		private AssetBundleRequest <assetLoadRequest>5__3;

		private IEnumerator<AudioClip> <>s__4;

		private AudioClip <clip>5__5;

		private string <clipName>5__6;

		object IEnumerator<object>.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		object IEnumerator.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		[DebuggerHidden]
		public <LoadAudioBundle>d__8(int <>1__state)
		{
			this.<>1__state = <>1__state;
		}

		[DebuggerHidden]
		void IDisposable.Dispose()
		{
			<bundlePath>5__1 = null;
			<bundleCreateRequest>5__2 = null;
			<assetLoadRequest>5__3 = null;
			<>s__4 = null;
			<clip>5__5 = null;
			<clipName>5__6 = null;
			<>1__state = -2;
		}

		private bool MoveNext()
		{
			switch (<>1__state)
			{
			default:
				return false;
			case 0:
				<>1__state = -1;
				if (_bundleLoaded)
				{
					return false;
				}
				<bundlePath>5__1 = Path.Combine(Paths.PluginPath, "Bundles", "Smeshariki");
				<bundleCreateRequest>5__2 = AssetBundle.LoadFromFileAsync(<bundlePath>5__1);
				<>2__current = <bundleCreateRequest>5__2;
				<>1__state = 1;
				return true;
			case 1:
				<>1__state = -1;
				_loadedBundle = <bundleCreateRequest>5__2.assetBundle;
				if ((Object)(object)_loadedBundle == (Object)null)
				{
					return false;
				}
				<assetLoadRequest>5__3 = _loadedBundle.LoadAllAssetsAsync<AudioClip>();
				<>2__current = <assetLoadRequest>5__3;
				<>1__state = 2;
				return true;
			case 2:
				<>1__state = -1;
				<>s__4 = <assetLoadRequest>5__3.allAssets.OfType<AudioClip>().GetEnumerator();
				try
				{
					while (<>s__4.MoveNext())
					{
						<clip>5__5 = <>s__4.Current;
						<clipName>5__6 = ((Object)<clip>5__5).name.ToLower();
						BundleClips[<clipName>5__6] = <clip>5__5;
						<clipName>5__6 = null;
						<clip>5__5 = null;
					}
				}
				finally
				{
					if (<>s__4 != null)
					{
						<>s__4.Dispose();
					}
				}
				<>s__4 = null;
				_bundleLoaded = true;
				return false;
			}
		}

		bool IEnumerator.MoveNext()
		{
			//ILSpy generated this explicit interface implementation from .override directive in MoveNext
			return this.MoveNext();
		}

		[DebuggerHidden]
		void IEnumerator.Reset()
		{
			throw new NotSupportedException();
		}
	}

	private const string BundleName = "Smeshariki";

	private static ManualLogSource _logger;

	private static AssetBundle _loadedBundle;

	private static readonly Dictionary<string, AudioClip> BundleClips = new Dictionary<string, AudioClip>();

	private static bool _isInitialized = false;

	private static bool _bundleLoaded = false;

	private void Awake()
	{
		_logger = ((BaseUnityPlugin)this).Logger;
		SceneManager.sceneLoaded += OnSceneLoaded;
		((MonoBehaviour)this).StartCoroutine(Initialize());
	}

	[IteratorStateMachine(typeof(<Initialize>d__7))]
	private IEnumerator Initialize()
	{
		//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
		return new <Initialize>d__7(0)
		{
			<>4__this = this
		};
	}

	[IteratorStateMachine(typeof(<LoadAudioBundle>d__8))]
	private IEnumerator LoadAudioBundle()
	{
		//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
		return new <LoadAudioBundle>d__8(0)
		{
			<>4__this = this
		};
	}

	private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
	{
		if (_bundleLoaded)
		{
			((MonoBehaviour)this).StartCoroutine(DelayedAudioReplacement());
		}
	}

	[IteratorStateMachine(typeof(<DelayedAudioReplacement>d__10))]
	private IEnumerator DelayedAudioReplacement()
	{
		//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
		return new <DelayedAudioReplacement>d__10(0)
		{
			<>4__this = this
		};
	}

	private void ReplaceAllAudioInGame()
	{
		if (BundleClips.Count == 0)
		{
			return;
		}
		List<AudioSource> list = (from src in Resources.FindObjectsOfTypeAll<AudioSource>()
			where (Object)(object)src != (Object)null && (Object)(object)src.clip != (Object)null
			select src).ToList();
		int num = 0;
		foreach (AudioSource item in list)
		{
			try
			{
				string key = ((Object)item.clip).name.ToLower();
				if (BundleClips.TryGetValue(key, out var value))
				{
					bool isPlaying = item.isPlaying;
					float time = item.time;
					bool loop = item.loop;
					float volume = item.volume;
					item.clip = value;
					item.time = time;
					item.volume = volume;
					item.loop = loop;
					if (isPlaying)
					{
						item.Play();
					}
					num++;
				}
			}
			catch (Exception arg)
			{
				_logger.LogError((object)$"Error processing AudioSource: {arg}");
			}
		}
		if (num > 0)
		{
			_logger.LogInfo((object)$"Replaced audio in {num} sources");
		}
	}

	private void OnDestroy()
	{
		SceneManager.sceneLoaded -= OnSceneLoaded;
		if ((Object)(object)_loadedBundle != (Object)null)
		{
			_loadedBundle.Unload(true);
			_bundleLoaded = false;
		}
	}
}
namespace SmesharikiOsts
{
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "SmesharikiOsts";

		public const string PLUGIN_NAME = "My first plugin";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}