Decompiled source of YtDlpBoombox v1.0.0

YtDlpBoombox.dll

Decompiled 11 months ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Cryptography;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("YtDlpBoombox")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("YtDlpBoombox")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("eef4f2ca-a786-4670-9421-7b8bed170f3c")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace YtDlpBoombox;

[BepInPlugin("rafl.YtDlpBoombox", "yt-dlp Boombox", "1.0.0")]
public class YtDlpBoomboxBase : BaseUnityPlugin
{
	private const string modGUID = "rafl.YtDlpBoombox";

	private const string modName = "yt-dlp Boombox";

	private const string modVersion = "1.0.0";

	private readonly Harmony harmony = new Harmony("rafl.YtDlpBoombox");

	private static YtDlpBoomboxBase instance;

	internal static ManualLogSource mls;

	internal static string pluginFolder;

	internal static string boomboxMusicPath;

	internal static string hashFile;

	private void Awake()
	{
		if ((Object)(object)instance == (Object)null)
		{
			instance = this;
		}
		mls = Logger.CreateLogSource("rafl.YtDlpBoombox");
		mls.LogInfo((object)string.Format("Initializing {0}", "yt-dlp Boombox"));
		pluginFolder = Path.GetDirectoryName(((BaseUnityPlugin)instance).Info.Location);
		string bepInExRootPath = Paths.BepInExRootPath;
		string path = Path.Combine(bepInExRootPath, "config");
		hashFile = Path.Combine(pluginFolder, "sources.md5");
		boomboxMusicPath = Path.Combine(bepInExRootPath, "Custom Songs/Boombox Music");
		Type typeFromHandle = typeof(YtDlpBoomboxBase);
		harmony.PatchAll(typeFromHandle);
		mls.LogInfo((object)("Executed Patch '" + typeFromHandle.Name + "'"));
		string sources = Path.Combine(path, "yt-dlp-boombox-sources.cfg");
		if (SourcesHaveChanged(sources))
		{
			mls.LogInfo((object)"Changes in yt-dlp-boombox-sources.cfg detected, clearing cached Songs and download sources.");
			WipeSongs(boomboxMusicPath);
			DownloadSongs(sources);
		}
		else
		{
			mls.LogInfo((object)"No changes in yt-dlp-boombox-sources.cfg detected.");
		}
	}

	private bool SourcesHaveChanged(string sources)
	{
		string text = GenerateSourcesHash(sources);
		if (!File.Exists(hashFile))
		{
			mls.LogWarning((object)(hashFile + " doesn't exist!"));
			return true;
		}
		string text2 = File.ReadAllText(hashFile).Trim();
		if (text2 != text)
		{
			mls.LogWarning((object)(text2 ?? ""));
			mls.LogWarning((object)(text ?? ""));
			return true;
		}
		return false;
	}

	private string GenerateSourcesHash(string sources)
	{
		MD5 mD = MD5.Create();
		FileStream inputStream = File.OpenRead(sources);
		return BitConverter.ToString(mD.ComputeHash(inputStream)).Replace("-", "").ToLowerInvariant()
			.Trim();
	}

	private void WriteSourcesHash(string hash)
	{
		File.WriteAllLines(hashFile, new string[1] { hash });
	}

	private void WipeSongs(string path)
	{
		if (!Directory.Exists(path))
		{
			return;
		}
		foreach (string item in Directory.EnumerateFiles(path))
		{
			File.Delete(item);
		}
	}

	private void DownloadSongs(string sources)
	{
		string currentDirectory = Directory.GetCurrentDirectory();
		if (!Directory.Exists(boomboxMusicPath))
		{
			Directory.CreateDirectory(boomboxMusicPath);
		}
		Directory.SetCurrentDirectory(boomboxMusicPath);
		foreach (string item in File.ReadLines(sources))
		{
			if (!item.StartsWith("#") && item.Length != 0)
			{
				DownloadSong(item);
			}
		}
		Directory.SetCurrentDirectory(currentDirectory);
		string hash = GenerateSourcesHash(sources);
		WriteSourcesHash(hash);
	}

	private void DownloadSong(string url)
	{
		mls.LogInfo((object)("Downloading: " + url));
		string text = Path.Combine(pluginFolder, "yt-dlp.exe");
		string text2 = "-x --audio-format mp3 \"" + url + "\"";
		mls.LogInfo((object)(text + " " + text2));
		Process.Start(text, text2);
	}
}