Decompiled source of BoomboxMusicShare v2.5.0

BoomboxMusicShare.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("LimeMod")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("LimeMod")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("3543362c-37b7-4d26-ae93-d93ec6946a41")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace BoomboxShare;

[BepInPlugin("0LimeSkillZ.0BoomboxMusicShare", "0BoomboxMusicShare", "2.3.0")]
public class BoomboxFix : BaseUnityPlugin
{
	private const string modGUID = "0LimeSkillZ.0BoomboxMusicShare";

	private const string modName = "0BoomboxMusicShare";

	private const string modVersion = "2.3.0";

	private string CustomSongsPluginPath => Path.Combine(Paths.PluginPath, "Custom Songs");

	[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
	private static extern bool CreateHardLink(string lpFileName, string lpExistingFileName, IntPtr lpSecurityAttributes);

	private void Awake()
	{
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		//IL_0028: Expected O, but got Unknown
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin BoomboxMusicShare is loaded!");
		ConfigFile val = new ConfigFile(Path.Combine(Paths.ConfigPath, "BoomboxMusicShare.cfg"), true);
		ConfigEntry<bool> val2 = val.Bind<bool>("General", "WipeCustomSongsFolder", true, "Whether the 'Custom Songs' folder in the plugins folder should be deleted every time you launch the game or not.");
		CreatePluginCustomSongsFolder();
		if (val2.Value)
		{
			DeleteAllFilesInTargetPath();
		}
		SearchAndCopyCustomSongs();
	}

	private void CreatePluginCustomSongsFolder()
	{
		if (!Directory.Exists(CustomSongsPluginPath))
		{
			Directory.CreateDirectory(CustomSongsPluginPath);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Created 'Custom Songs' folder in plugin directory.");
		}
	}

	private void DeleteAllFilesInTargetPath()
	{
		try
		{
			if (Directory.Exists(CustomSongsPluginPath))
			{
				string[] files = Directory.GetFiles(CustomSongsPluginPath);
				((BaseUnityPlugin)this).Logger.LogInfo((object)("Deleting files in '" + CustomSongsPluginPath + "'..."));
				string[] array = files;
				string[] array2 = array;
				foreach (string path in array2)
				{
					File.Delete(path);
				}
				((BaseUnityPlugin)this).Logger.LogInfo((object)("Deleted files in '" + CustomSongsPluginPath + "'"));
			}
			else
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("Target path '" + CustomSongsPluginPath + "' does not exist."));
			}
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)("An error occurred while trying to delete files: " + ex.Message));
		}
	}

	private void SearchAndCopyCustomSongs()
	{
		string[] directories = Directory.GetDirectories(Paths.PluginPath);
		string[] array = directories;
		string[] array2 = array;
		foreach (string path in array2)
		{
			string text = Path.Combine(path, "Custom Songs");
			if (!Directory.Exists(text))
			{
				continue;
			}
			IEnumerable<string> enumerable = Directory.GetFiles(text, "*.mp3").Concat(Directory.GetFiles(text, "*.wav")).Concat(Directory.GetFiles(text, "*.ogg"));
			foreach (string item in enumerable)
			{
				string fileName = Path.GetFileName(item);
				string text2 = Path.Combine(CustomSongsPluginPath, fileName);
				if (!File.Exists(text2))
				{
					if (!CreateHardLink(text2, item, IntPtr.Zero))
					{
						((BaseUnityPlugin)this).Logger.LogInfo((object)"Failed to create hard link. Copying file instead.");
						File.Copy(item, text2);
						((BaseUnityPlugin)this).Logger.LogInfo((object)("Copied " + fileName + " from " + text + " to Custom Songs folder."));
					}
					else
					{
						((BaseUnityPlugin)this).Logger.LogInfo((object)("Hard Linked " + fileName + " from " + text + " to Custom Songs folder."));
					}
				}
			}
		}
	}
}