Decompiled source of TTSVerityTranslationLayer v1.2.0

plugins/0TTSCompanyFix.dll

Decompiled 19 hours ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("0TTSCompanyFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("0TTSCompanyFix")]
[assembly: AssemblyTitle("0TTSCompanyFix")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 TTSCompanyFix
{
	[BepInPlugin("teamverity.0ttscompanyfix", "0TTSCompanyFix", "1.2.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "teamverity.0ttscompanyfix";

		public const string PluginName = "0TTSCompanyFix";

		public const string PluginVersion = "1.2.0";

		private const string TargetFolderName = "TeamVerity-TTSCompanyFix";

		private const string SubFolderName = "lukepcuniversal-lab-TTSCompanyFix";

		private const string VoicesFolderName = "TTS-Company-Voices";

		private const string EmbeddedVoicesPrefix = "Voices.";

		internal static ManualLogSource Log;

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			try
			{
				Run();
			}
			catch (Exception ex)
			{
				Log.LogError((object)("0TTSCompanyFix failed: " + ex));
			}
		}

		private static void Run()
		{
			string location = Assembly.GetExecutingAssembly().Location;
			string directoryName = Path.GetDirectoryName(location);
			string text = FindAncestor(directoryName, "TeamVerity-TTSCompanyFix");
			if (text == null)
			{
				Log.LogWarning((object)("Could not find 'TeamVerity-TTSCompanyFix' in or above '" + directoryName + "'."));
				return;
			}
			try
			{
				MoveSubfolderContents(location, text);
			}
			catch (Exception ex)
			{
				Log.LogError((object)("Moving files failed: " + ex));
			}
			try
			{
				ExtractEmbeddedVoices(text);
			}
			catch (Exception ex2)
			{
				Log.LogError((object)("Extracting voice files failed: " + ex2));
			}
		}

		private static void MoveSubfolderContents(string dllPath, string targetDir)
		{
			string text = Path.Combine(targetDir, "lukepcuniversal-lab-TTSCompanyFix");
			if (!Directory.Exists(text))
			{
				Log.LogWarning((object)("Subfolder 'lukepcuniversal-lab-TTSCompanyFix' not found in '" + targetDir + "'."));
				return;
			}
			int num = 0;
			int num2 = 0;
			string[] files = Directory.GetFiles(text, "*", SearchOption.AllDirectories);
			foreach (string text2 in files)
			{
				if (string.Equals(text2, dllPath, StringComparison.OrdinalIgnoreCase))
				{
					continue;
				}
				string text3 = text2.Substring(text.Length).TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
				string text4 = Path.Combine(targetDir, text3);
				try
				{
					Directory.CreateDirectory(Path.GetDirectoryName(text4));
					if (File.Exists(text4))
					{
						File.Delete(text4);
					}
					File.Move(text2, text4);
					num++;
				}
				catch (Exception ex)
				{
					num2++;
					Log.LogWarning((object)("Could not move '" + text3 + "': " + ex.Message));
				}
			}
			Log.LogInfo((object)("Moved " + num + " file(s) from 'lukepcuniversal-lab-TTSCompanyFix' into 'TeamVerity-TTSCompanyFix'" + ((num2 > 0) ? (" (" + num2 + " failed).") : ".")));
		}

		private static void ExtractEmbeddedVoices(string targetDir)
		{
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			string text = Path.Combine(targetDir, "TTS-Company-Voices");
			int num = 0;
			int num2 = 0;
			int num3 = 0;
			string[] manifestResourceNames = executingAssembly.GetManifestResourceNames();
			foreach (string text2 in manifestResourceNames)
			{
				if (!text2.StartsWith("Voices.", StringComparison.Ordinal))
				{
					continue;
				}
				string fileName = Path.GetFileName(text2.Substring("Voices.".Length));
				if (string.IsNullOrEmpty(fileName))
				{
					continue;
				}
				string text3 = Path.Combine(text, fileName);
				try
				{
					Directory.CreateDirectory(text);
					using (Stream stream = executingAssembly.GetManifestResourceStream(text2))
					{
						if (stream == null)
						{
							throw new IOException("Embedded resource could not be opened.");
						}
						if (File.Exists(text3) && new FileInfo(text3).Length == stream.Length)
						{
							num2++;
							continue;
						}
						if (File.Exists(text3))
						{
							File.SetAttributes(text3, FileAttributes.Normal);
						}
						using FileStream destination = new FileStream(text3, FileMode.Create, FileAccess.Write, FileShare.Read);
						stream.CopyTo(destination);
					}
					num++;
				}
				catch (Exception ex)
				{
					num3++;
					Log.LogWarning((object)("Could not write '" + fileName + "' to 'TTS-Company-Voices': " + ex.Message));
				}
			}
			Log.LogInfo((object)("'TTS-Company-Voices': " + num + " written, " + num2 + " already present" + ((num3 > 0) ? (", " + num3 + " failed.") : ".")));
		}

		private static string FindAncestor(string startDir, string folderName)
		{
			string text = startDir;
			while (!string.IsNullOrEmpty(text))
			{
				if (string.Equals(Path.GetFileName(text.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)), folderName, StringComparison.OrdinalIgnoreCase))
				{
					return text;
				}
				string[] directories = Directory.GetDirectories(text);
				foreach (string text2 in directories)
				{
					if (string.Equals(Path.GetFileName(text2), folderName, StringComparison.OrdinalIgnoreCase))
					{
						return text2;
					}
				}
				DirectoryInfo parent = Directory.GetParent(text);
				if (parent == null)
				{
					break;
				}
				text = parent.FullName;
			}
			return null;
		}
	}
}