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.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using ExitGames.Client.Photon;
using HarmonyLib;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
using Zorro.Core;
[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("PoopMod")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.2.0.0")]
[assembly: AssemblyInformationalVersion("0.2.0+8230d9fd9c87902a367e862a7754baee312893cd")]
[assembly: AssemblyProduct("PoopMod")]
[assembly: AssemblyTitle("PoopMod")]
[assembly: AssemblyVersion("0.2.0.0")]
namespace PoopMod;
internal sealed class PoopItem
{
[HarmonyPatch(typeof(Item), "GetName")]
internal static class NamePatch
{
private static bool Prefix(Item __instance, ref string __result)
{
if (__instance.itemID != 64001)
{
return true;
}
__result = "Poop";
return false;
}
}
private const string PrefabName = "PoopMod_Poop";
private const string ResourcePath = "0_Items/PoopMod_Poop";
private const ushort ItemId = 64001;
private readonly Mesh mesh;
private readonly Material material;
private readonly ManualLogSource logger;
private GameObject prefabRoot;
private Item prefab;
private DefaultPool registeredPool;
internal string TemplateName { get; private set; }
internal PoopItem(Mesh mesh, Material material, ManualLogSource logger)
{
this.mesh = mesh;
this.material = material;
this.logger = logger;
}
internal bool Register()
{
IPunPrefabPool prefabPool = PhotonNetwork.PrefabPool;
DefaultPool val = (DefaultPool)(object)((prefabPool is DefaultPool) ? prefabPool : null);
if (val == null)
{
logger.LogWarning((object)"PoopMod requires PEAK's default item prefab pool.");
return false;
}
Dictionary<ushort, Item> itemLookup = SingletonAsset<ItemDatabase>.Instance.itemLookup;
if ((itemLookup.TryGetValue(64001, out var value) && (Object)(object)value != (Object)(object)prefab) || (val.ResourceCache.TryGetValue("0_Items/PoopMod_Poop", out var value2) && ((Object)(object)prefab == (Object)null || (Object)(object)value2 != (Object)(object)((Component)prefab).gameObject)))
{
logger.LogWarning((object)"PoopMod item registration conflicts with another item.");
return false;
}
if ((Object)(object)prefab == (Object)null)
{
GameObject[] array = Resources.LoadAll<GameObject>("0_Items");
Item[] array2 = (from item in itemLookup.Values.Concat(array.Select((GameObject asset) => asset.GetComponent<Item>()))
where (Object)(object)item != (Object)null && item.UIData != null && ((Object)(object)item.rig != (Object)null || (Object)(object)((Component)item).GetComponent<Rigidbody>() != (Object)null)
select item).Distinct().ToArray();
Item val2 = (from item in array2
orderby item.UIData.canThrow && item.UIData.canPocket descending, item.mustUseOnFriend, ((Component)item).GetComponentsInChildren<ItemActionBase>(true).Length, ((Component)item).GetComponentsInChildren<ItemComponent>(true).Length, item.itemID
select item).FirstOrDefault();
if ((Object)(object)val2 == (Object)null)
{
logger.LogWarning((object)$"PoopMod found no usable item template. Database: {itemLookup.Count}, resources: {array.Length}, candidates: {array2.Length}.");
return false;
}
logger.LogInfo((object)$"PoopMod item template: {((Object)val2).name}; FBX mesh: {mesh.vertexCount} vertices.");
BuildPrefab(val2);
TemplateName = ((Object)val2).name;
}
itemLookup[64001] = prefab;
val.ResourceCache["0_Items/PoopMod_Poop"] = ((Component)prefab).gameObject;
registeredPool = val;
return true;
}
internal void Spawn(Vector3 position)
{
//IL_000e: Unknown result type (might be due to invalid IL or missing references)
//IL_000f: Unknown result type (might be due to invalid IL or missing references)
//IL_002d: Unknown result type (might be due to invalid IL or missing references)
if (Register())
{
if ((Object)(object)PhotonNetwork.InstantiateItemRoom("PoopMod_Poop", position, Quaternion.identity, false) != (Object)null)
{
logger.LogInfo((object)$"Spawned PoopMod item at {position}.");
}
else
{
logger.LogWarning((object)"PEAK did not instantiate the PoopMod item.");
}
}
}
private void BuildPrefab(Item template)
{
//IL_0006: Unknown result type (might be due to invalid IL or missing references)
//IL_0010: Expected O, but got Unknown
//IL_01e8: Unknown result type (might be due to invalid IL or missing references)
//IL_01ed: Unknown result type (might be due to invalid IL or missing references)
//IL_0203: Unknown result type (might be due to invalid IL or missing references)
//IL_021a: Unknown result type (might be due to invalid IL or missing references)
prefabRoot = new GameObject("PoopMod Item Templates");
prefabRoot.SetActive(false);
Object.DontDestroyOnLoad((Object)(object)prefabRoot);
prefab = Object.Instantiate<Item>(template, prefabRoot.transform, false);
((Component)prefab).gameObject.SetActive(false);
((Object)((Component)prefab).gameObject).name = "PoopMod_Poop";
prefab.itemID = 64001;
prefab.totalUses = 1;
prefab.UIData.hasMainInteract = true;
prefab.UIData.canDrop = true;
prefab.UIData.canPocket = true;
prefab.UIData.canBackpack = true;
prefab.UIData.canThrow = true;
prefab.UIData.hasSecondInteract = false;
prefab.canUseOnFriend = false;
prefab.mustUseOnFriend = false;
prefab.usingTimePrimary = 1f;
prefab.showUseProgress = true;
if ((Object)(object)prefab.rig == (Object)null)
{
prefab.rig = ((Component)prefab).GetComponent<Rigidbody>();
}
ItemActionBase[] componentsInChildren = ((Component)prefab).GetComponentsInChildren<ItemActionBase>(true);
for (int i = 0; i < componentsInChildren.Length; i++)
{
Object.DestroyImmediate((Object)(object)componentsInChildren[i]);
}
((ItemAction)((Component)prefab).gameObject.AddComponent<Action_Consume>()).OnCastFinished = true;
MeshFilter[] componentsInChildren2 = ((Component)prefab).GetComponentsInChildren<MeshFilter>(true);
for (int i = 0; i < componentsInChildren2.Length; i++)
{
componentsInChildren2[i].sharedMesh = null;
}
Renderer[] componentsInChildren3 = ((Component)prefab).GetComponentsInChildren<Renderer>(true);
foreach (Renderer obj in componentsInChildren3)
{
SkinnedMeshRenderer val = (SkinnedMeshRenderer)(object)((obj is SkinnedMeshRenderer) ? obj : null);
if (val != null)
{
val.sharedMesh = null;
}
obj.enabled = false;
obj.forceRenderingOff = true;
}
GameObject val2 = new GameObject("Poop Model")
{
layer = ((Component)prefab).gameObject.layer
};
val2.transform.SetParent(prefab.transform, false);
val2.AddComponent<MeshFilter>().sharedMesh = mesh;
MeshRenderer val3 = val2.AddComponent<MeshRenderer>();
((Renderer)val3).sharedMaterial = material;
prefab.mainRenderer = (Renderer)(object)val3;
prefab.addtlRenderers = Array.Empty<Renderer>();
}
internal void Dispose()
{
if (!((Object)(object)prefab == (Object)null))
{
Dictionary<ushort, Item> itemLookup = SingletonAsset<ItemDatabase>.Instance.itemLookup;
if (itemLookup.TryGetValue(64001, out var value) && (Object)(object)value == (Object)(object)prefab)
{
itemLookup.Remove(64001);
}
if (registeredPool != null && registeredPool.ResourceCache.TryGetValue("0_Items/PoopMod_Poop", out var value2) && (Object)(object)value2 == (Object)(object)((Component)prefab).gameObject)
{
registeredPool.ResourceCache.Remove("0_Items/PoopMod_Poop");
}
Object.Destroy((Object)(object)prefabRoot);
}
}
}
[BepInPlugin("com.poopmod.peak", "PoopMod", "0.2.0")]
public class PoopPlugin : BaseUnityPlugin
{
[HarmonyPatch(typeof(CharacterMovement), "SetCrouch")]
private static class CrouchPatch
{
private static void Prefix(ref bool __0, Character ___character)
{
if ((Object)(object)squattingCharacter != (Object)null && (Object)(object)___character == (Object)(object)squattingCharacter)
{
__0 = true;
}
}
}
private const string PluginId = "com.poopmod.peak";
private const string PluginVersion = "0.2.0";
private const string ReadyProperty = "com.poopmod.peak.ready";
private static Character squattingCharacter;
private Room registeredRoom;
private string readyValue;
private float nextRegistrationAttempt;
private ConfigEntry<Key> poopKey;
private Harmony harmony;
private Mesh poopMesh;
private Material poopMaterial;
private PoopItem poopItem;
private float startedAt;
private bool spawned;
private void Awake()
{
//IL_0062: Unknown result type (might be due to invalid IL or missing references)
//IL_0067: Unknown result type (might be due to invalid IL or missing references)
//IL_0077: Unknown result type (might be due to invalid IL or missing references)
//IL_0086: Expected O, but got Unknown
//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
//IL_00b3: Expected O, but got Unknown
poopKey = ((BaseUnityPlugin)this).Config.Bind<Key>("Controls", "PoopKey", (Key)30, "Start the poop action.");
poopMesh = LoadMesh();
Shader val = Shader.Find("Universal Render Pipeline/Lit") ?? Shader.Find("Universal Render Pipeline/Unlit");
if ((Object)(object)val == (Object)null)
{
((BaseUnityPlugin)this).Logger.LogError((object)"No supported shader found for PoopMod.");
return;
}
poopMaterial = new Material(val)
{
color = new Color(0.29f, 0.13f, 0.055f)
};
poopItem = new PoopItem(poopMesh, poopMaterial, ((BaseUnityPlugin)this).Logger);
harmony = new Harmony("com.poopmod.peak");
harmony.CreateClassProcessor(typeof(CrouchPatch)).Patch();
harmony.CreateClassProcessor(typeof(PoopItem.NamePatch)).Patch();
((BaseUnityPlugin)this).Logger.LogInfo((object)"PoopMod Loaded!");
}
private void Update()
{
//IL_0018: Unknown result type (might be due to invalid IL or missing references)
//IL_0026: Unknown result type (might be due to invalid IL or missing references)
if (harmony == null)
{
return;
}
Keyboard current = Keyboard.current;
bool flag = current != null && (int)poopKey.Value != 0 && ((ButtonControl)current[poopKey.Value]).wasPressedThisFrame;
if (!PhotonNetwork.InRoom)
{
registeredRoom = null;
readyValue = null;
squattingCharacter = null;
if (flag)
{
((BaseUnityPlugin)this).Logger.LogInfo((object)$"Poop key ignored: offline={PhotonNetwork.OfflineMode}, inRoom={PhotonNetwork.InRoom}.");
}
return;
}
Character localCharacter = Character.localCharacter;
if (!PhotonNetwork.OfflineMode && !UpdateMultiplayerReadiness(flag))
{
squattingCharacter = null;
}
else if (!PhotonNetwork.OfflineMode && flag && CanSquat(localCharacter) && PhotonNetwork.CurrentRoom.IsOpen)
{
if (PhotonNetwork.IsMasterClient)
{
PhotonNetwork.CurrentRoom.IsOpen = false;
((BaseUnityPlugin)this).Logger.LogInfo((object)"PoopMod closed the room to new joins. Press the Poop key again to spawn.");
}
else
{
((BaseUnityPlugin)this).Logger.LogInfo((object)"The host must press the Poop key to close the room before spawning.");
}
}
else if ((Object)(object)squattingCharacter != (Object)null)
{
if ((Object)(object)localCharacter != (Object)(object)squattingCharacter || !CanSquat(localCharacter) || Time.time - startedAt >= 1.25f)
{
if (!spawned)
{
((BaseUnityPlugin)this).Logger.LogInfo((object)"Poop action ended before spawning an item.");
}
squattingCharacter = null;
}
else if (!spawned && localCharacter.data.isCrouching && Time.time - startedAt >= 0.65f)
{
spawned = true;
if (PhotonNetwork.OfflineMode || !PhotonNetwork.CurrentRoom.IsOpen)
{
SpawnPoop(localCharacter);
}
}
}
else if (flag)
{
if (!CanSquat(localCharacter))
{
((BaseUnityPlugin)this).Logger.LogInfo((object)"Poop key ignored: no grounded, active local character.");
return;
}
squattingCharacter = localCharacter;
startedAt = Time.time;
spawned = false;
((BaseUnityPlugin)this).Logger.LogInfo((object)"Poop action started.");
}
}
private bool UpdateMultiplayerReadiness(bool pressed)
{
//IL_007c: Unknown result type (might be due to invalid IL or missing references)
//IL_0081: Unknown result type (might be due to invalid IL or missing references)
//IL_008d: Expected O, but got Unknown
//IL_0094: Expected O, but got Unknown
Room currentRoom = PhotonNetwork.CurrentRoom;
if (registeredRoom != currentRoom)
{
registeredRoom = currentRoom;
readyValue = null;
nextRegistrationAttempt = 0f;
}
if (readyValue == null && Time.unscaledTime >= nextRegistrationAttempt)
{
nextRegistrationAttempt = Time.unscaledTime + 2f;
if (poopItem.Register())
{
string value = currentRoom.Name + ":0.2.0:" + poopItem.TemplateName;
Player localPlayer = PhotonNetwork.LocalPlayer;
Hashtable val = new Hashtable();
((Dictionary<object, object>)val).Add((object)"com.poopmod.peak.ready", (object)value);
if (localPlayer.SetCustomProperties(val, (Hashtable)null, (WebFlags)null))
{
readyValue = value;
}
}
}
if (readyValue == null)
{
return false;
}
foreach (Player value3 in currentRoom.Players.Values)
{
if (value3.IsInactive || !((Dictionary<object, object>)(object)value3.CustomProperties).TryGetValue((object)"com.poopmod.peak.ready", out object value2) || !(value2 is string text) || !(text == readyValue))
{
if (pressed)
{
((BaseUnityPlugin)this).Logger.LogInfo((object)$"PoopMod is waiting for compatible registration from player {value3.ActorNumber}.");
}
return false;
}
}
return true;
}
private static bool CanSquat(Character player)
{
if ((Object)(object)player != (Object)null && (Object)(object)player.data != (Object)null && player.data.isGrounded && !player.data.dead && !player.data.passedOut && !player.data.isClimbing && !player.data.isRopeClimbing)
{
return !player.data.isVineClimbing;
}
return false;
}
private void SpawnPoop(Character player)
{
//IL_0007: Unknown result type (might be due to invalid IL or missing references)
//IL_000c: Unknown result type (might be due to invalid IL or missing references)
//IL_0011: Unknown result type (might be due to invalid IL or missing references)
//IL_0016: Unknown result type (might be due to invalid IL or missing references)
//IL_0019: Unknown result type (might be due to invalid IL or missing references)
//IL_001e: Unknown result type (might be due to invalid IL or missing references)
//IL_001f: Unknown result type (might be due to invalid IL or missing references)
//IL_0024: Unknown result type (might be due to invalid IL or missing references)
//IL_002a: Unknown result type (might be due to invalid IL or missing references)
//IL_002f: Unknown result type (might be due to invalid IL or missing references)
//IL_0034: Unknown result type (might be due to invalid IL or missing references)
//IL_0035: Unknown result type (might be due to invalid IL or missing references)
//IL_0036: Unknown result type (might be due to invalid IL or missing references)
//IL_0040: Unknown result type (might be due to invalid IL or missing references)
//IL_0045: Unknown result type (might be due to invalid IL or missing references)
//IL_004a: Unknown result type (might be due to invalid IL or missing references)
//IL_0082: Unknown result type (might be due to invalid IL or missing references)
//IL_0083: Unknown result type (might be due to invalid IL or missing references)
//IL_008d: Unknown result type (might be due to invalid IL or missing references)
//IL_0092: Unknown result type (might be due to invalid IL or missing references)
//IL_0076: Unknown result type (might be due to invalid IL or missing references)
//IL_007b: Unknown result type (might be due to invalid IL or missing references)
CharacterData data = player.data;
Vector3 val = Vector3.ProjectOnPlane(data.groundedForward, Vector3.up);
Vector3 normalized = ((Vector3)(ref val)).normalized;
Vector3 val2 = data.groundPos - normalized * 0.65f;
RaycastHit val3 = default(RaycastHit);
if (Physics.Raycast(val2 + Vector3.up * 0.35f, Vector3.down, ref val3, 0.95f, -5, (QueryTriggerInteraction)1) && (Object)(object)((Component)((RaycastHit)(ref val3)).collider).GetComponentInParent<Character>() == (Object)null)
{
val2 = ((RaycastHit)(ref val3)).point;
}
poopItem.Spawn(val2 + Vector3.up * 0.05f);
}
private static Mesh LoadMesh()
{
//IL_004f: Unknown result type (might be due to invalid IL or missing references)
//IL_0054: Unknown result type (might be due to invalid IL or missing references)
//IL_007f: Unknown result type (might be due to invalid IL or missing references)
//IL_0084: Unknown result type (might be due to invalid IL or missing references)
//IL_008f: Unknown result type (might be due to invalid IL or missing references)
//IL_0096: Unknown result type (might be due to invalid IL or missing references)
//IL_009d: Unknown result type (might be due to invalid IL or missing references)
//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
//IL_00ab: Expected O, but got Unknown
using BinaryReader binaryReader = new BinaryReader(typeof(PoopPlugin).Assembly.GetManifestResourceStream("PoopMod.PoopMesh.bin"));
Vector3[] array = (Vector3[])(object)new Vector3[binaryReader.ReadInt32()];
int[] array2 = new int[binaryReader.ReadInt32()];
for (int i = 0; i < array.Length; i++)
{
array[i] = new Vector3(binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle());
}
for (int j = 0; j < array2.Length; j++)
{
array2[j] = binaryReader.ReadInt32();
}
Mesh val = new Mesh
{
name = "Poop",
vertices = array,
triangles = array2
};
val.RecalculateNormals();
val.RecalculateBounds();
return val;
}
private void OnDestroy()
{
squattingCharacter = null;
Harmony obj = harmony;
if (obj != null)
{
obj.UnpatchSelf();
}
poopItem?.Dispose();
Object.Destroy((Object)(object)poopMaterial);
Object.Destroy((Object)(object)poopMesh);
}
}