using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Photon.Pun;
using Photon.Voice.Unity;
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("Conchversations")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Conchversations")]
[assembly: AssemblyCopyright("Copyright © 2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("32855ccf-9a00-4c8f-8e6f-798feca50f5e")]
[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 Conchversations;
[BepInPlugin("tony4twentys.Conchversations", "Conchversations", "1.1.0")]
public class Conchversations : BaseUnityPlugin
{
[HarmonyPatch(typeof(CharacterItems), "EquipSlotRpc")]
public class CharacterItemsPatch
{
private static void Postfix(CharacterItems __instance, int slotID, int objectViewID)
{
PhotonView photonView = PhotonNetwork.GetPhotonView(objectViewID);
Item val = null;
if ((Object)(object)photonView != (Object)null)
{
val = ((Component)photonView).GetComponent<Item>();
}
if ((Object)(object)val != (Object)null && val.itemID == 69)
{
Instance.SetTransmitting(((MonoBehaviourPun)__instance).photonView.Controller.ActorNumber, isTransmitting: true);
}
else
{
Instance.SetTransmitting(((MonoBehaviourPun)__instance).photonView.Controller.ActorNumber, isTransmitting: false);
}
}
}
public class CharacterAudioData
{
public Character Character;
public Speaker Speaker;
public AudioSource AudioSource;
public AudioEchoFilter Echo;
public AudioLowPassFilter LowPass;
public AudioHighPassFilter HighPass;
public CharacterAudioData(Character character, Speaker speaker)
{
Character = character;
Speaker = speaker;
AudioSource = ((Component)speaker).GetComponent<AudioSource>();
Echo = ((Component)speaker).GetComponent<AudioEchoFilter>();
if ((Object)(object)Echo == (Object)null)
{
Echo = ((Component)speaker).gameObject.AddComponent<AudioEchoFilter>();
}
LowPass = ((Component)speaker).GetComponent<AudioLowPassFilter>();
if ((Object)(object)LowPass == (Object)null)
{
LowPass = ((Component)speaker).gameObject.AddComponent<AudioLowPassFilter>();
}
LowPass.cutoffFrequency = 2000f;
LowPass.lowpassResonanceQ = 3f;
((Behaviour)LowPass).enabled = false;
HighPass = ((Component)speaker).GetComponent<AudioHighPassFilter>();
if ((Object)(object)HighPass == (Object)null)
{
HighPass = ((Component)speaker).gameObject.AddComponent<AudioHighPassFilter>();
}
HighPass.cutoffFrequency = 1000f;
HighPass.highpassResonanceQ = 10f;
((Behaviour)HighPass).enabled = false;
}
}
[HarmonyPatch(typeof(CharacterVoiceHandler), "Start")]
private class CharacterVoiceHandlerPatch
{
private static void Postfix(CharacterVoiceHandler __instance)
{
//IL_0029: Unknown result type (might be due to invalid IL or missing references)
//IL_002f: Expected O, but got Unknown
FieldInfo fieldInfo = AccessTools.Field(typeof(CharacterVoiceHandler), "m_character");
if (fieldInfo != null)
{
Character val = (Character)fieldInfo.GetValue(__instance);
Speaker component = ((Component)__instance).GetComponent<Speaker>();
if ((Object)(object)val != (Object)null && (Object)(object)component != (Object)null)
{
AddCharacterAudio(val, new CharacterAudioData(val, component));
}
}
}
}
[HarmonyPatch(typeof(CharacterVoiceHandler), "LateUpdate")]
private class PatchVoiceHandlerLateUpdate
{
private static readonly FieldInfo characterField = AccessTools.Field(typeof(CharacterVoiceHandler), "m_character");
private static void Postfix(CharacterVoiceHandler __instance)
{
//IL_000c: Unknown result type (might be due to invalid IL or missing references)
//IL_0012: Expected O, but got Unknown
Character val = (Character)characterField.GetValue(__instance);
if ((Object)(object)val == (Object)null || !CharacterAudioDict.TryGetValue(val, out var value) || value == null)
{
return;
}
if (Instance.GetTransmitting(((MonoBehaviourPun)value.Character).photonView.Controller.ActorNumber) && Instance.canListen)
{
value.AudioSource.spatialBlend = 0f;
((Behaviour)value.Echo).enabled = false;
if (ConfigEnableRadioFilter.Value)
{
((Behaviour)value.LowPass).enabled = true;
((Behaviour)value.HighPass).enabled = true;
}
}
else
{
value.AudioSource.spatialBlend = 1f;
((Behaviour)value.Echo).enabled = true;
if (ConfigEnableRadioFilter.Value)
{
((Behaviour)value.LowPass).enabled = false;
((Behaviour)value.HighPass).enabled = false;
}
}
}
}
[HarmonyPatch(/*Could not decode attribute arguments.*/)]
private class ItemCarryWeightPatch
{
private static bool Prefix(Item __instance, ref int __result)
{
if ((Object)(object)__instance != (Object)null && __instance.itemID == 69)
{
__result = 0;
return false;
}
return true;
}
}
private static Conchversations Instance;
private const int CONCH_ITEM_ID = 69;
internal static ConfigEntry<bool> ConfigEnableRadioFilter;
public bool canTransmit = false;
public bool canListen = false;
public Dictionary<int, bool> CharacterTransmittingDict = new Dictionary<int, bool>();
public static Dictionary<Character, CharacterAudioData> CharacterAudioDict = new Dictionary<Character, CharacterAudioData>();
private void Awake()
{
//IL_000c: Unknown result type (might be due to invalid IL or missing references)
Instance = this;
new Harmony("tony4twentys.Conchversations").PatchAll();
((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin initialized and waiting for gameplay scene.");
ConfigEnableRadioFilter = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "EnableRadioFilter", true, "Enables the radio filter for all communications through the conch.");
}
public bool GetTransmitting(int characterId)
{
bool value;
return CharacterTransmittingDict.TryGetValue(characterId, out value) && value;
}
public void AddCharacter(int characterId, bool isTransmitting = false)
{
if (!CharacterTransmittingDict.ContainsKey(characterId))
{
CharacterTransmittingDict.Add(characterId, isTransmitting);
}
}
public void SetTransmitting(int characterId, bool isTransmitting)
{
if (CharacterTransmittingDict.ContainsKey(characterId))
{
CharacterTransmittingDict[characterId] = isTransmitting;
}
else
{
CharacterTransmittingDict.Add(characterId, isTransmitting);
}
}
public static void CleanupCharacterAudioDict()
{
List<Character> list = CharacterAudioDict.Keys.Where((Character c) => (Object)(object)c == (Object)null || (Object)(object)((MonoBehaviourPun)c).photonView == (Object)null || ((MonoBehaviourPun)c).photonView.Controller == null).ToList();
foreach (Character item in list)
{
CharacterAudioDict.Remove(item);
}
}
public static void AddCharacterAudio(Character character, CharacterAudioData audioData)
{
if ((Object)(object)character != (Object)null && audioData != null)
{
CharacterAudioDict[character] = audioData;
}
}
public static void GetCharacterValue(CharacterVoiceHandler handler)
{
//IL_0029: Unknown result type (might be due to invalid IL or missing references)
//IL_002f: Expected O, but got Unknown
FieldInfo fieldInfo = AccessTools.Field(typeof(CharacterVoiceHandler), "m_character");
if (fieldInfo != null)
{
Character val = (Character)fieldInfo.GetValue(handler);
}
}
private void Update()
{
if ((Object)(object)Character.localCharacter != (Object)null)
{
canListen = HasConchAnywhere(Character.localCharacter.player);
}
}
public static bool HasConchAnywhere(Player p)
{
ItemSlot[] itemSlots = p.itemSlots;
foreach (ItemSlot val in itemSlots)
{
if (!val.IsEmpty() && (Object)(object)val.prefab != (Object)null && val.prefab.itemID == 69)
{
return true;
}
}
if (!p.tempFullSlot.IsEmpty() && (Object)(object)p.tempFullSlot.prefab != (Object)null && p.tempFullSlot.prefab.itemID == 69)
{
return true;
}
if (p.backpackSlot.hasBackpack && HasConchInBackpack(((ItemSlot)p.backpackSlot).data))
{
return true;
}
return false;
}
private static bool IsConch(ItemSlot slot)
{
return (Object)(object)slot?.prefab != (Object)null && slot.prefab.itemID == 69;
}
public static bool HasConchInHand(Player p)
{
if ((Object)(object)p.character.data.currentItem != (Object)null && p.character.data.currentItem.itemID == 69)
{
return true;
}
if (IsConch(p.tempFullSlot))
{
return true;
}
return false;
}
public static bool HasConchInBackpack(ItemInstanceData data)
{
BackpackData val = default(BackpackData);
if (data.TryGetDataEntry<BackpackData>((DataEntryKey)7, ref val))
{
ItemSlot[] itemSlots = val.itemSlots;
ItemSlot[] array = itemSlots;
foreach (ItemSlot val2 in array)
{
if ((Object)(object)val2.prefab != (Object)null && val2.prefab.itemID == 69)
{
return true;
}
}
}
return false;
}
}