using System; 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 System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using BepInEx; using BepInEx.Configuration; using CustomRadio.Configuration; using Microsoft.CodeAnalysis; using YoutubeExplode; using YoutubeExplode.Common; using YoutubeExplode.Converter; using YoutubeExplode.Playlists; using YoutubeExplode.Videos; [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("CustomRadio")] [assembly: AssemblyConfiguration("Debug")] [assembly: AssemblyDescription("My first plugin")] [assembly: AssemblyFileVersion("0.0.1.0")] [assembly: AssemblyInformationalVersion("0.0.1")] [assembly: AssemblyProduct("CustomRadio")] [assembly: AssemblyTitle("CustomRadio")] [assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)] [assembly: AssemblyVersion("0.0.1.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.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)] internal sealed class NullableAttribute : Attribute { public readonly byte[] NullableFlags; public NullableAttribute(byte P_0) { NullableFlags = new byte[1] { P_0 }; } public NullableAttribute(byte[] P_0) { NullableFlags = P_0; } } [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)] internal sealed class NullableContextAttribute : Attribute { public readonly byte Flag; public NullableContextAttribute(byte P_0) { Flag = P_0; } } [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; } } } namespace CustomRadio { [BepInPlugin("com.zerouni.CustomRadio", "CustomRadio", "0.0.1")] public class Radio : BaseUnityPlugin { private string songPath = Path.Combine(Paths.PluginPath, "zerouni-CustomRadio", "Custom Songs"); private string ffmpegPath = Path.Combine(Paths.PluginPath, "zerouni-CustomRadio", "ffmpeg.exe"); private string boomboxPath = Path.Combine(Paths.BepInExRootPath, "Custom Songs", "Boombox Music"); private static readonly YoutubeClient youtube = new YoutubeClient(); private async Task Awake() { ((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin CustomRadio is loaded!"); CreatePluginCustomSongsFolder(); Config.Init(); ((BaseUnityPlugin)this).Logger.LogInfo((object)"Downloading music..."); Task task = CreateMusic(); await task; ((BaseUnityPlugin)this).Logger.LogInfo((object)"Music downloaded!"); } private void CreatePluginCustomSongsFolder() { if (!Directory.Exists(songPath)) { Directory.CreateDirectory(songPath); } if (!Directory.Exists(boomboxPath)) { Directory.CreateDirectory(boomboxPath); } } private (string, string) createNamesFromTitle(PlaylistVideo video, string source) { string input = Regex.Replace(video.Title, "\\W+", "_"); input = Regex.Replace(input, "_+", "_"); string item = Path.Combine(source, input + ".mp3"); string item2 = Path.Combine(boomboxPath, input + ".mp3"); return (item, item2); } private void copyFile(string source, string destination) { ((BaseUnityPlugin)this).Logger.LogInfo((object)("Copying " + source + " to " + destination)); if (File.Exists(destination)) { ((BaseUnityPlugin)this).Logger.LogInfo((object)("Deleting " + destination)); try { File.Delete(destination); } catch (Exception ex) { ((BaseUnityPlugin)this).Logger.LogError((object)("An error occurred while deleting " + destination)); ((BaseUnityPlugin)this).Logger.LogError((object)ex.Message); ((BaseUnityPlugin)this).Logger.LogError((object)ex.InnerException.Message); } } try { File.Copy(source, destination); } catch (Exception ex2) { ((BaseUnityPlugin)this).Logger.LogError((object)("An error occurred while copying " + source + " to " + destination)); ((BaseUnityPlugin)this).Logger.LogError((object)ex2.Message); ((BaseUnityPlugin)this).Logger.LogError((object)ex2.InnerException.Message); } } private async Task CreateMusic() { string playlistUrl = Config.PlaylistUrl; try { string source = songPath; ((BaseUnityPlugin)this).Logger.LogInfo((object)("Downloading music to " + source)); ((BaseUnityPlugin)this).Logger.LogInfo((object)("Downloading playlist " + playlistUrl)); string[] songNames = new string[100]; List downloadTasks = new List(); await foreach (Batch batch in youtube.Playlists.GetVideoBatchesAsync(PlaylistId.op_Implicit(playlistUrl), default(CancellationToken))) { foreach (PlaylistVideo video2 in batch.Items) { var (newFileName2, boomboxFileName3) = createNamesFromTitle(video2, source); if (songNames[^1] != null) { string[] temp = new string[songNames.Length + 100]; for (int i = 0; i < songNames.Length; i++) { temp[i] = songNames[i]; } songNames = temp; } songNames[Array.IndexOf(songNames, null)] = newFileName2; ((BaseUnityPlugin)this).Logger.LogInfo((object)("Downloading " + newFileName2)); if (File.Exists(newFileName2)) { ((BaseUnityPlugin)this).Logger.LogInfo((object)("Skipping " + video2.Title + " because it already exists")); copyFile(newFileName2, boomboxFileName3); continue; } ((BaseUnityPlugin)this).Logger.LogInfo((object)("Converting " + video2.Title + " to " + newFileName2)); downloadTasks.Add(ConversionExtensions.DownloadAsync(youtube.Videos, VideoId.op_Implicit(video2.Url), newFileName2, (Action)delegate(ConversionRequestBuilder o) { o.SetPreset((ConversionPreset)3).SetContainer("mp3").SetFFmpegPath(ffmpegPath); }, (IProgress)null, default(CancellationToken)).AsTask()); ((BaseUnityPlugin)this).Logger.LogInfo((object)"Continuing"); } await Task.WhenAll(downloadTasks); foreach (PlaylistVideo video in batch.Items) { var (newFileName, boomboxFileName2) = createNamesFromTitle(video, source); copyFile(newFileName, boomboxFileName2); } } ((BaseUnityPlugin)this).Logger.LogInfo((object)"Done!"); string[] files = Directory.GetFiles(source); foreach (string file in files) { if (Array.IndexOf(songNames, file) != -1) { continue; } ((BaseUnityPlugin)this).Logger.LogInfo((object)("Deleting " + file)); try { File.Delete(file); } catch (Exception ex) { Exception e3 = ex; ((BaseUnityPlugin)this).Logger.LogError((object)("An error occurred while deleting " + file)); ((BaseUnityPlugin)this).Logger.LogError((object)e3.Message); ((BaseUnityPlugin)this).Logger.LogError((object)e3.InnerException.Message); } string boomboxFileName = Path.Combine(boomboxPath, Path.GetFileName(file)); if (File.Exists(boomboxFileName)) { ((BaseUnityPlugin)this).Logger.LogInfo((object)("Deleting " + boomboxFileName)); try { File.Delete(boomboxFileName); } catch (Exception ex) { Exception e2 = ex; ((BaseUnityPlugin)this).Logger.LogError((object)("An error occurred while deleting " + boomboxFileName)); ((BaseUnityPlugin)this).Logger.LogError((object)e2.Message); ((BaseUnityPlugin)this).Logger.LogError((object)e2.InnerException.Message); } } } } catch (Exception ex) { Exception e = ex; ((BaseUnityPlugin)this).Logger.LogError((object)"An error occurred while creating music"); ((BaseUnityPlugin)this).Logger.LogError((object)e.Message); ((BaseUnityPlugin)this).Logger.LogError((object)e.InnerException.Message); } } } public static class PluginInfo { public const string PLUGIN_GUID = "CustomRadio"; public const string PLUGIN_NAME = "CustomRadio"; public const string PLUGIN_VERSION = "0.0.1"; } } namespace CustomRadio.Configuration { internal static class Config { private const string CONFIG_FILE_NAME = "zerouni.CustomRadio.cfg"; private static ConfigFile _configFile; private static ConfigEntry _playlistUrl; public static string PlaylistUrl { get { return _playlistUrl.Value ?? "https://www.youtube.com/playlist?list=PLrsKG8lXztu1oGy7A9Lu_N0Y9z_4A9mc_"; } set { _playlistUrl.Value = value; } } public static void Init() { //IL_0011: Unknown result type (might be due to invalid IL or missing references) //IL_001b: Expected O, but got Unknown _configFile = new ConfigFile(Path.Combine(Paths.ConfigPath, "zerouni.CustomRadio.cfg"), true); _playlistUrl = _configFile.Bind("General", "PlaylistUrl", "https://www.youtube.com/playlist?list=PLrsKG8lXztu1oGy7A9Lu_N0Y9z_4A9mc_", (ConfigDescription)null); } } }