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 System.Security.Cryptography;
using System.Text;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using LCSoundTool;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("CustomSounds")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CustomSounds")]
[assembly: AssemblyCopyright("Copyright © 2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("9e086160-a7fd-4721-ba09-3e8534cb7011")]
[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 CustomSounds;
[BepInPlugin("CustomSounds", "Custom Sounds", "1.2.0")]
public class Plugin : BaseUnityPlugin
{
private const string PLUGIN_GUID = "CustomSounds";
private const string PLUGIN_NAME = "Custom Sounds";
private const string PLUGIN_VERSION = "1.2.0";
public static Plugin Instance;
internal ManualLogSource logger;
private Harmony harmony;
public HashSet<string> currentSounds = new HashSet<string>();
public HashSet<string> oldSounds = new HashSet<string>();
public HashSet<string> modifiedSounds = new HashSet<string>();
public Dictionary<string, string> soundHashes = new Dictionary<string, string>();
public Dictionary<string, string> soundPacks = new Dictionary<string, string>();
public static bool Initialized { get; private set; }
private void Awake()
{
//IL_0041: Unknown result type (might be due to invalid IL or missing references)
//IL_004b: Expected O, but got Unknown
if (!((Object)(object)Instance == (Object)null))
{
return;
}
Instance = this;
logger = Logger.CreateLogSource("CustomSounds");
logger.LogInfo((object)"Plugin CustomSounds is loaded!");
harmony = new Harmony("CustomSounds");
harmony.PatchAll();
modifiedSounds = new HashSet<string>();
string path = Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location), "CustomSounds");
if (!Directory.Exists(path))
{
logger.LogInfo((object)"\"CustomSounds\" folder not found. Creating it now.");
Directory.CreateDirectory(path);
}
string path2 = Path.Combine(Paths.BepInExConfigPath);
try
{
List<string> list = File.ReadAllLines(path2).ToList();
int num = list.FindIndex((string line) => line.StartsWith("HideManagerGameObject"));
if (num != -1)
{
logger.LogInfo((object)"\"hideManagerGameObject\" value not correctly set. Fixing it now.");
list[num] = "HideManagerGameObject = true";
}
File.WriteAllLines(path2, list);
}
catch (Exception ex)
{
logger.LogError((object)("Erreur lors de la modification du fichier de configuration: " + ex.Message));
}
}
internal void Start()
{
Initialize();
}
internal void OnDestroy()
{
Initialize();
}
internal void Initialize()
{
if (!Initialized)
{
Initialized = true;
ReloadSounds();
}
}
public void RevertSounds()
{
foreach (string currentSound in currentSounds)
{
logger.LogInfo((object)(currentSound + " restored."));
SoundTool.RestoreAudioClip(currentSound);
}
logger.LogInfo((object)"Original game sounds restored.");
}
public static string CalculateMD5(string filename)
{
using MD5 mD = MD5.Create();
using FileStream inputStream = File.OpenRead(filename);
byte[] array = mD.ComputeHash(inputStream);
return BitConverter.ToString(array).Replace("-", "").ToLowerInvariant();
}
public void ReloadSounds()
{
foreach (string currentSound in currentSounds)
{
SoundTool.RestoreAudioClip(currentSound);
}
oldSounds = new HashSet<string>(currentSounds);
modifiedSounds.Clear();
string directoryName = Path.GetDirectoryName(Paths.PluginPath);
currentSounds.Clear();
ProcessDirectory(directoryName);
}
private void ProcessDirectory(string directoryPath)
{
string[] directories = Directory.GetDirectories(directoryPath, "CustomSounds", SearchOption.AllDirectories);
foreach (string text in directories)
{
string fileName = Path.GetFileName(Path.GetDirectoryName(text));
ProcessSoundFiles(text, fileName);
string[] directories2 = Directory.GetDirectories(text);
foreach (string text2 in directories2)
{
string fileName2 = Path.GetFileName(text2);
ProcessSoundFiles(text2, fileName2);
}
}
}
private void ProcessSoundFiles(string directoryPath, string packName)
{
string[] files = Directory.GetFiles(directoryPath, "*.wav");
foreach (string text in files)
{
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(text);
string text2 = CalculateMD5(text);
if (soundHashes.TryGetValue(fileNameWithoutExtension, out var value) && value != text2)
{
modifiedSounds.Add(fileNameWithoutExtension);
}
AudioClip audioClip = SoundTool.GetAudioClip(directoryPath, "", text);
SoundTool.ReplaceAudioClip(fileNameWithoutExtension, audioClip);
soundHashes[fileNameWithoutExtension] = text2;
currentSounds.Add(fileNameWithoutExtension);
soundPacks[fileNameWithoutExtension] = packName;
logger.LogInfo((object)("[" + packName + "] " + fileNameWithoutExtension + " sound replaced!"));
}
}
public string GetSoundChanges()
{
StringBuilder stringBuilder = new StringBuilder("Customsounds reloaded.\n\n");
HashSet<string> arg = new HashSet<string>(currentSounds.Except(oldSounds));
HashSet<string> arg2 = new HashSet<string>(oldSounds.Except(currentSounds));
HashSet<string> arg3 = new HashSet<string>(oldSounds.Intersect(currentSounds).Except(modifiedSounds));
HashSet<string> arg4 = new HashSet<string>(modifiedSounds);
Dictionary<string, List<string>> soundsByPack = new Dictionary<string, List<string>>();
Action<HashSet<string>, string> action = delegate(HashSet<string> soundsSet, string status)
{
foreach (string item in soundsSet)
{
string key = soundPacks[item];
if (!soundsByPack.ContainsKey(key))
{
soundsByPack[key] = new List<string>();
}
soundsByPack[key].Add(item + " (" + status + ")");
}
};
action(arg, "New");
action(arg2, "Deleted");
action(arg4, "Modified");
action(arg3, "Already Existed");
foreach (string key2 in soundsByPack.Keys)
{
stringBuilder.AppendLine(key2 + " :");
foreach (string item2 in soundsByPack[key2])
{
stringBuilder.AppendLine("- " + item2);
}
stringBuilder.AppendLine();
}
return stringBuilder.ToString();
}
public string ListAllSounds()
{
StringBuilder stringBuilder = new StringBuilder("Listing all currently loaded custom sounds:\n\n");
Dictionary<string, List<string>> dictionary = new Dictionary<string, List<string>>();
foreach (string currentSound in currentSounds)
{
string key = soundPacks[currentSound];
if (!dictionary.ContainsKey(key))
{
dictionary[key] = new List<string>();
}
dictionary[key].Add(currentSound);
}
foreach (string key2 in dictionary.Keys)
{
stringBuilder.AppendLine(key2 + " :");
foreach (string item in dictionary[key2])
{
stringBuilder.AppendLine("- " + item);
}
stringBuilder.AppendLine();
}
return stringBuilder.ToString();
}
}
[HarmonyPatch(typeof(Terminal), "ParsePlayerSentence")]
public static class TerminalParsePlayerSentencePatch
{
public static bool Prefix(Terminal __instance, ref TerminalNode __result)
{
string[] array = __instance.screenText.text.Split(new char[1] { '\n' });
if (array.Length == 0)
{
return true;
}
string[] array2 = array.Last().Trim().ToLower()
.Split(new char[1] { ' ' });
if (array2.Length == 0 || array2[0] != "customsounds")
{
return true;
}
Plugin.Instance.logger.LogInfo((object)("Received terminal command: " + string.Join(" ", array2)));
if (array2.Length > 1 && array2[0] == "customsounds")
{
switch (array2[1])
{
case "reload":
Plugin.Instance.ReloadSounds();
__result = CreateTerminalNode(Plugin.Instance.GetSoundChanges());
return false;
case "revert":
Plugin.Instance.RevertSounds();
__result = CreateTerminalNode("Game sounds reverted to original.");
return false;
case "list":
__result = CreateTerminalNode(Plugin.Instance.ListAllSounds());
return false;
case "help":
__result = CreateTerminalNode("CustomSounds commands.\n\n>CUSTOMSOUNDS LIST\nTo displays all currently loaded sounds\n\n>CUSTOMSOUNDS RELOAD\nTo reloads and applies sounds from the 'CustomSounds' folder and its subfolders.\n\n>CUSTOMSOUNDS REVERT\nTo unloads all custom sounds and restores original game sounds");
return false;
default:
__result = CreateTerminalNode("Unknown customsounds command.");
return false;
}
}
return true;
}
private static TerminalNode CreateTerminalNode(string message)
{
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
//IL_0006: Unknown result type (might be due to invalid IL or missing references)
//IL_000d: Unknown result type (might be due to invalid IL or missing references)
//IL_0015: Expected O, but got Unknown
return new TerminalNode
{
displayText = message,
clearPreviousText = true
};
}
}