Decompiled source of PlushPals v0.1.0

plugins/PlushPals/PlushPals.dll

Decompiled 41 minutes ago
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using GameNetcodeStuff;
using LethalLib.Modules;
using Unity.Netcode;
using UnityEngine;

[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("PlushPals")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+41bf698bab080babeb93ea726b07e64ca16a4618")]
[assembly: AssemblyProduct("PlushPals")]
[assembly: AssemblyTitle("PlushPals")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace PlushPals;

[DefaultExecutionOrder(10)]
internal sealed class PlushCarryPose : MonoBehaviour
{
	private const float PalmClearance = 0.045f;

	private const float ForwardClearance = 0.08f;

	private const float SqueezeAttack = 0.045f;

	private const float SqueezeRelease = 0.17f;

	private static readonly Vector3 SqueezedScale = new Vector3(0.91f, 0.94f, 1.04f);

	[SerializeField]
	private PhysicsProp prop;

	[SerializeField]
	private Transform visual;

	[SerializeField]
	private Vector3 restScale;

	private bool wasCarried;

	private float squeezeTime = 0.215f;

	private float squeezeAmount;

	private float squeezeStart;

	internal void Initialize(PhysicsProp item, Transform model)
	{
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_0015: Unknown result type (might be due to invalid IL or missing references)
		prop = item;
		visual = model;
		restScale = model.localScale;
	}

	internal void Squeeze()
	{
		squeezeStart = squeezeAmount;
		squeezeTime = 0f;
	}

	private void LateUpdate()
	{
		//IL_0052: Unknown result type (might be due to invalid IL or missing references)
		//IL_0057: Unknown result type (might be due to invalid IL or missing references)
		//IL_0068: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
		//IL_0107: Unknown result type (might be due to invalid IL or missing references)
		//IL_010c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0111: Unknown result type (might be due to invalid IL or missing references)
		//IL_0112: Unknown result type (might be due to invalid IL or missing references)
		//IL_0117: Unknown result type (might be due to invalid IL or missing references)
		//IL_0121: Unknown result type (might be due to invalid IL or missing references)
		//IL_0126: Unknown result type (might be due to invalid IL or missing references)
		//IL_012b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0133: Unknown result type (might be due to invalid IL or missing references)
		//IL_0135: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
		//IL_01bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d6: Unknown result type (might be due to invalid IL or missing references)
		Transform parentObject = ((GrabbableObject)prop).parentObject;
		PlayerControllerB playerHeldBy = ((GrabbableObject)prop).playerHeldBy;
		if (!((GrabbableObject)prop).isHeld || ((GrabbableObject)prop).isPocketed || (Object)(object)parentObject == (Object)null || (Object)(object)playerHeldBy == (Object)null)
		{
			if (wasCarried)
			{
				visual.SetLocalPositionAndRotation(Vector3.zero, Quaternion.identity);
				visual.localScale = restScale;
			}
			wasCarried = false;
			squeezeTime = 0.215f;
			squeezeAmount = 0f;
			return;
		}
		Vector3 val = Vector3.ProjectOnPlane((((Object)(object)playerHeldBy.gameplayCamera != (Object)null) ? ((Component)playerHeldBy.gameplayCamera).transform : ((Component)playerHeldBy).transform).position - parentObject.position, Vector3.up);
		if (((Vector3)(ref val)).sqrMagnitude < 0.0001f)
		{
			val = -((Component)playerHeldBy).transform.forward;
		}
		Quaternion val2 = Quaternion.LookRotation(val, Vector3.up);
		Vector3 val3 = parentObject.position + Vector3.up * 0.045f - val2 * Vector3.forward * 0.08f;
		visual.SetPositionAndRotation(val3, val2);
		if (squeezeTime < 0.215f)
		{
			squeezeTime += Time.deltaTime;
			squeezeAmount = ((squeezeTime < 0.045f) ? Mathf.Lerp(squeezeStart, 1f, squeezeTime / 0.045f) : (1f - Mathf.SmoothStep(0f, 1f, (squeezeTime - 0.045f) / 0.17f)));
			visual.localScale = Vector3.Scale(restScale, Vector3.Lerp(Vector3.one, SqueezedScale, squeezeAmount));
		}
		wasCarried = true;
	}
}
internal static class PlushMaterials
{
	internal static void Apply(MeshRenderer[] renderers)
	{
		//IL_0072: 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_00ac: Unknown result type (might be due to invalid IL or missing references)
		Shader val = Shader.Find("HDRP/Lit");
		if ((Object)(object)val == (Object)null || !val.isSupported)
		{
			return;
		}
		Dictionary<Material, Material> dictionary = new Dictionary<Material, Material>();
		foreach (MeshRenderer val2 in renderers)
		{
			Material[] sharedMaterials = ((Renderer)val2).sharedMaterials;
			for (int j = 0; j < sharedMaterials.Length; j++)
			{
				Material val3 = sharedMaterials[j];
				if (!((Object)(object)val3 == (Object)null) && !((Object)(object)val3.shader == (Object)(object)val))
				{
					if (!dictionary.TryGetValue(val3, out var value))
					{
						value = new Material(val)
						{
							name = ((Object)val3).name
						};
						value.SetTexture("_BaseColorMap", val3.GetTexture("_MainTex"));
						value.SetColor("_BaseColor", val3.GetColor("_Color"));
						value.SetFloat("_Smoothness", 0.15f);
						dictionary.Add(val3, value);
					}
					sharedMaterials[j] = value;
				}
			}
			((Renderer)val2).sharedMaterials = sharedMaterials;
		}
	}
}
[BepInPlugin("plushpals", "PlushPals", "0.1.0")]
[BepInDependency("evaisa.lethallib", "1.2.0")]
internal sealed class PlushPalsPlugin : BaseUnityPlugin
{
	private static readonly PlushDefinition Frankie = new PlushDefinition("Frankie", "Assets/Models/Frankie.prefab", "Assets/Models/Frankie-Icon.png", 20, (Min: 60, Max: 125));

	private static readonly PlushDefinition Redmint = new PlushDefinition("Redmint", "Assets/Models/Redmint.prefab", "Assets/Models/Redmint-Icon.png", 20, (Min: 60, Max: 125));

	private static readonly PlushDefinition Alan = new PlushDefinition("Alan", "Assets/Models/Alan.prefab", "Assets/Models/Alan-Icon.png", 20, (Min: 60, Max: 125));

	private static readonly PlushDefinition Radiance = new PlushDefinition("Radiance", "Assets/Models/Radiance.prefab", "Assets/Models/Radiance-Icon.png", 20, (Min: 60, Max: 125));

	private static readonly PlushDefinition Grooto = new PlushDefinition("Grooto", "Assets/Models/Grooto.prefab", "Assets/Models/Grooto-Icon.png", 20, (Min: 60, Max: 125));

	private AssetBundle bundle;

	private void Awake()
	{
		string text = Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location), "plushpals.assets");
		bundle = (File.Exists(text) ? AssetBundle.LoadFromFile(text) : null);
		if ((Object)(object)bundle == (Object)null)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)("Could not load PlushPals assets from " + text));
			return;
		}
		(AudioClip, AudioClip) sounds = (LoadSound("Assets/Audio/PlushPickup.wav"), LoadSound("Assets/Audio/PlushDrop.wav"));
		Register(Frankie, sounds);
		Register(Redmint, sounds);
		Register(Alan, sounds);
		Register(Radiance, sounds);
		Register(Grooto, sounds);
	}

	private void OnDestroy()
	{
		if ((Object)(object)bundle != (Object)null)
		{
			bundle.Unload(false);
		}
	}

	private AudioClip LoadSound(string path)
	{
		//IL_001f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0025: Invalid comparison between Unknown and I4
		AudioClip val = bundle.LoadAsset<AudioClip>(path);
		if ((Object)(object)val != (Object)null && val.LoadAudioData() && (int)val.loadState == 2)
		{
			return val;
		}
		((BaseUnityPlugin)this).Logger.LogError((object)("Could not load plush audio from " + path));
		return null;
	}

	private void Register(PlushDefinition definition, (AudioClip Pickup, AudioClip Drop) sounds)
	{
		GameObject val = bundle.LoadAsset<GameObject>(definition.ModelAsset);
		Sprite val2 = bundle.LoadAsset<Sprite>(definition.IconAsset);
		if ((Object)(object)val == (Object)null || !Object.op_Implicit((Object)(object)val.GetComponentInChildren<MeshRenderer>(true)) || (Object)(object)val2 == (Object)null)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)("The " + definition.Name + " model or icon is missing from the PlushPals asset bundle"));
			return;
		}
		PlushScrap.Register(definition, val, val2, sounds);
		((BaseUnityPlugin)this).Logger.LogInfo((object)("Registered " + definition.Name + " scrap"));
	}
}
internal sealed class PlushProp : PhysicsProp
{
	private const int PitchSteps = 7;

	[SerializeField]
	private PlushCarryPose pose;

	[SerializeField]
	private AudioSource spatialAudio;

	[SerializeField]
	private AudioSource squeezeAudio;

	private int lastPitchStep = -1;

	internal void Initialize(PlushCarryPose carryPose, AudioSource itemAudio, AudioSource localAudio)
	{
		pose = carryPose;
		spatialAudio = itemAudio;
		squeezeAudio = localAudio;
	}

	public override void EquipItem()
	{
		((PhysicsProp)this).EquipItem();
		if (!((Object)(object)((GrabbableObject)this).playerHeldBy == (Object)null) && !((Object)(object)squeezeAudio.clip == (Object)null))
		{
			((((NetworkBehaviour)((GrabbableObject)this).playerHeldBy).IsOwner && (Object)(object)((GrabbableObject)this).playerHeldBy.itemAudio != (Object)null) ? ((GrabbableObject)this).playerHeldBy.itemAudio : spatialAudio).PlayOneShot(squeezeAudio.clip);
		}
	}

	public override void DiscardItem()
	{
		((GrabbableObject)this).DiscardItem();
		if (((GrabbableObject)this).fallTime < 1f)
		{
			((GrabbableObject)this).hasHitGround = false;
		}
	}

	public override void ItemActivate(bool used, bool buttonDown = true)
	{
		if (!buttonDown || !((NetworkBehaviour)this).IsOwner || !((GrabbableObject)this).isHeld || ((GrabbableObject)this).isPocketed || (Object)(object)((GrabbableObject)this).playerHeldBy == (Object)null || !((NetworkBehaviour)((GrabbableObject)this).playerHeldBy).IsOwner)
		{
			return;
		}
		pose.Squeeze();
		if (!((Object)(object)squeezeAudio.clip == (Object)null))
		{
			int num = Random.Range(0, 7 - ((lastPitchStep >= 0) ? 1 : 0));
			if (lastPitchStep >= 0 && num >= lastPitchStep)
			{
				num++;
			}
			lastPitchStep = num;
			squeezeAudio.pitch = Mathf.Lerp(0.82f, 1.18f, (float)num / 6f);
			squeezeAudio.Play();
		}
	}
}
internal readonly struct PlushDefinition
{
	internal string Name { get; }

	internal string ModelAsset { get; }

	internal string IconAsset { get; }

	internal int SpawnWeight { get; }

	internal (int Min, int Max) Value { get; }

	internal PlushDefinition(string name, string modelAsset, string iconAsset, int spawnWeight, (int Min, int Max) value)
	{
		Name = name;
		ModelAsset = modelAsset;
		IconAsset = iconAsset;
		SpawnWeight = spawnWeight;
		Value = value;
	}
}
internal static class PlushScrap
{
	internal static void Register(PlushDefinition definition, GameObject model, Sprite icon, (AudioClip Pickup, AudioClip Drop) sounds)
	{
		//IL_005b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0060: Unknown result type (might be due to invalid IL or missing references)
		//IL_006a: 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_00a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
		//IL_0121: Unknown result type (might be due to invalid IL or missing references)
		Item val = CreateItem(definition, icon, sounds.Drop);
		GameObject val2 = NetworkPrefabs.CreateNetworkPrefab(((Object)val).name);
		val2.layer = LayerMask.NameToLayer("Props");
		val2.tag = "PhysicsProp";
		val2.GetComponent<NetworkObject>().DontDestroyWithOwner = true;
		val.spawnPrefab = val2;
		(Transform Visual, Bounds Bounds) tuple = AttachModel(val2.transform, model);
		Transform item = tuple.Visual;
		Bounds item2 = tuple.Bounds;
		BoxCollider obj = val2.AddComponent<BoxCollider>();
		obj.center = ((Bounds)(ref item2)).center;
		obj.size = ((Bounds)(ref item2)).size;
		AudioSource val3 = val2.AddComponent<AudioSource>();
		val3.playOnAwake = false;
		val3.spatialBlend = 1f;
		GameObject val4 = new GameObject("SqueezeAudio");
		val4.transform.SetParent(val2.transform, false);
		AudioSource val5 = val4.AddComponent<AudioSource>();
		val5.playOnAwake = false;
		val5.clip = sounds.Pickup;
		PlushProp plushProp = val2.AddComponent<PlushProp>();
		((GrabbableObject)plushProp).grabbable = true;
		((GrabbableObject)plushProp).grabbableToEnemies = true;
		((GrabbableObject)plushProp).isInFactory = true;
		((GrabbableObject)plushProp).itemProperties = val;
		PlushCarryPose plushCarryPose = val2.AddComponent<PlushCarryPose>();
		plushCarryPose.Initialize((PhysicsProp)(object)plushProp, item);
		plushProp.Initialize(plushCarryPose, val3, val5);
		AttachScanNode(val2.transform, item2, definition.Name);
		Items.RegisterScrap(val, definition.SpawnWeight, (LevelTypes)(-1));
	}

	private static Item CreateItem(PlushDefinition definition, Sprite icon, AudioClip drop)
	{
		//IL_008f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0094: Unknown result type (might be due to invalid IL or missing references)
		Item obj = ScriptableObject.CreateInstance<Item>();
		((Object)obj).name = "PlushPals." + definition.Name;
		obj.itemName = definition.Name;
		obj.itemIcon = icon;
		obj.dropSFX = drop;
		obj.isScrap = true;
		obj.requiresBattery = false;
		obj.automaticallySetUsingPower = false;
		obj.syncUseFunction = false;
		Item val = obj;
		(int, int) value = definition.Value;
		val.minValue = value.Item1;
		obj.maxValue = value.Item2;
		obj.weight = 1f;
		obj.canBeGrabbedBeforeGameStart = true;
		obj.itemSpawnsOnGround = true;
		obj.restingRotation = Vector3.zero;
		obj.spawnPositionTypes = new List<ItemGroup>();
		return obj;
	}

	private static (Transform Visual, Bounds Bounds) AttachModel(Transform parent, GameObject model)
	{
		//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_002d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0046: Unknown result type (might be due to invalid IL or missing references)
		//IL_004b: Unknown result type (might be due to invalid IL or missing references)
		//IL_005b: Unknown result type (might be due to invalid IL or missing references)
		GameObject val = Object.Instantiate<GameObject>(model, parent, false);
		MeshRenderer[] componentsInChildren = val.GetComponentsInChildren<MeshRenderer>(true);
		PlushMaterials.Apply(componentsInChildren);
		Bounds bounds = ((Renderer)componentsInChildren[0]).bounds;
		MeshRenderer[] array = componentsInChildren;
		foreach (MeshRenderer val2 in array)
		{
			((Bounds)(ref bounds)).Encapsulate(((Renderer)val2).bounds);
		}
		((Bounds)(ref bounds)).center = parent.InverseTransformPoint(((Bounds)(ref bounds)).center);
		return (Visual: val.transform, Bounds: bounds);
	}

	private static void AttachScanNode(Transform parent, Bounds bounds, string name)
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_000a: Unknown result type (might be due to invalid IL or missing references)
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0027: 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_0039: Unknown result type (might be due to invalid IL or missing references)
		//IL_0041: Unknown result type (might be due to invalid IL or missing references)
		GameObject val = new GameObject("ScanNode")
		{
			layer = LayerMask.NameToLayer("ScanNode")
		};
		val.transform.SetParent(parent, false);
		val.transform.localPosition = ((Bounds)(ref bounds)).center;
		val.AddComponent<BoxCollider>().size = ((Bounds)(ref bounds)).size;
		ScanNodeProperties obj = val.AddComponent<ScanNodeProperties>();
		obj.headerText = name;
		obj.subText = "Value: ";
		obj.nodeType = 2;
		obj.creatureScanID = -1;
		obj.minRange = 1;
		obj.maxRange = 13;
		obj.requiresLineOfSight = true;
	}
}