Decompiled source of VoiceVolumeSetter v0.0.2

Mods/VoiceVolumeSetter.dll

Decompiled 2 years 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.Xml;
using MelonLoader;
using VoiceVolumeSetter;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: MelonInfo(typeof(VoiceVolumeSetterClass), "VoiceVolumeSetter", "0.0.2", "Sky7734", null)]
[assembly: MelonGame("Buckethead Entertainment", "RUMBLE")]
[assembly: AssemblyTitle("VoiceVolumeSetter")]
[assembly: AssemblyDescription("Sets the volume of the RUMBLE config file to a set number on runtime.")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("VoiceVolumeSetter")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("26098c18-ff5a-4774-8bdf-025a36c7dce4")]
[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 VoiceVolumeSetter;

public class VoiceVolumeSetterClass : MelonMod
{
	private string configFilePath = "UserData\\VoiceVolumeSetter\\VoiceVolume.xml";

	private string localLowFolder = "";

	private string audioXmlPath = "";

	public override void OnInitializeMelon()
	{
		SetupFolders();
		EnsureConfigFileExists();
		if (File.Exists(audioXmlPath))
		{
			string text = FindVoiceVolumeLine();
			if (text != null)
			{
				UpdateVoiceVolume();
			}
			else
			{
				Console.WriteLine("Line starting with <VoiceVolume> not found in Audio.xml.");
			}
		}
		else
		{
			Console.WriteLine("Audio.xml not found in the Config folder.");
		}
	}

	private void SetupFolders()
	{
		string path = Path.Combine(Environment.CurrentDirectory, "UserData", "VoiceVolumeSetter");
		if (!Directory.Exists(path))
		{
			Directory.CreateDirectory(path);
			Console.WriteLine("Created UserData\\VoiceVolumeSetter folder.");
		}
		localLowFolder = GetLocalLowFolder();
		audioXmlPath = Path.Combine(localLowFolder, "Audio.xml");
	}

	private void EnsureConfigFileExists()
	{
		if (!File.Exists(configFilePath))
		{
			try
			{
				string currentVolumeFromAudioXml = GetCurrentVolumeFromAudioXml();
				XmlDocument xmlDocument = new XmlDocument();
				XmlElement xmlElement = xmlDocument.CreateElement("VoiceVolume");
				xmlElement.InnerText = currentVolumeFromAudioXml;
				xmlDocument.AppendChild(xmlElement);
				xmlDocument.Save(configFilePath);
				Console.WriteLine("Created VoiceVolume.xml with default value " + currentVolumeFromAudioXml + ".");
			}
			catch (Exception ex)
			{
				Console.WriteLine("An error occurred while creating VoiceVolume.xml: " + ex.Message);
			}
		}
	}

	private string GetCurrentVolumeFromAudioXml()
	{
		try
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(audioXmlPath);
			XmlNode xmlNode = xmlDocument.SelectSingleNode("//VoiceVolume");
			if (xmlNode != null)
			{
				return xmlNode.InnerText;
			}
			throw new Exception("VoiceVolume node not found in Audio.xml.");
		}
		catch (Exception ex)
		{
			Console.WriteLine("An error occurred while getting current volume: " + ex.Message);
			return "1";
		}
	}

	private string FindVoiceVolumeLine()
	{
		try
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(audioXmlPath);
			XmlNode xmlNode = xmlDocument.SelectSingleNode("//VoiceVolume");
			if (xmlNode != null)
			{
				return xmlNode.OuterXml;
			}
			throw new Exception("VoiceVolume node not found in Audio.xml.");
		}
		catch (Exception ex)
		{
			Console.WriteLine("An error occurred while reading Audio.xml: " + ex.Message);
			return null;
		}
	}

	private void UpdateVoiceVolume()
	{
		try
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(configFilePath);
			string text = xmlDocument.SelectSingleNode("//VoiceVolume")?.InnerText;
			XmlDocument xmlDocument2 = new XmlDocument();
			xmlDocument2.Load(audioXmlPath);
			XmlNode xmlNode = xmlDocument2.SelectSingleNode("//VoiceVolume");
			if (xmlNode != null)
			{
				xmlNode.InnerText = text;
				xmlDocument2.Save(audioXmlPath);
				Console.WriteLine("VoiceVolume value updated in Audio.xml.");
				Console.WriteLine("Changed value in Audio.xml: " + text);
				return;
			}
			throw new Exception("VoiceVolume node not found in Audio.xml.");
		}
		catch (Exception ex)
		{
			Console.WriteLine("An error occurred while updating volume: " + ex.Message);
		}
	}

	private string GetLocalLowFolder()
	{
		try
		{
			string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
			string text = Path.Combine(folderPath, "..", "LocalLow", "Buckethead Entertainment", "RUMBLE", "Steam");
			string[] directories = Directory.GetDirectories(text);
			string text2 = ((directories.Length == 1) ? directories[0] : null);
			if (text2 != null)
			{
				text = Path.Combine(text, text2, "Config");
			}
			return Path.GetFullPath(text);
		}
		catch (Exception ex)
		{
			Console.WriteLine("An error occurred while getting LocalLow folder: " + ex.Message);
			return "";
		}
	}
}