Decompiled source of Sandwich v1.0.3

LC_Sandwich.dll

Decompiled 4 months ago
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using LethalLib.Modules;
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("LC_Sandwich")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("LC_Sandwich")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("26d313e5-594d-421e-bedc-ee6b8638ca8e")]
[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 LC_Sandwich;

public class Sandwich : PhysicsProp
{
	public static int sandwichSize = 4;

	public static float timeToEat = 2f;

	public static int healing = 15;

	public static float healCrippleChance = 1f / (float)sandwichSize;

	public float eatingSpeed = 1f;

	private float eaten = 0f;

	private int originalValue = 0;

	private Material material;

	private AudioSource audioSource;

	public static AudioClip eatingSFX;

	public static AudioClip finishSFX;

	public bool eating;

	private PlayerControllerB previousPlayerHeldBy;

	private Coroutine startEating;

	public static Vector3 defaultRotation;

	public static Vector3 defaultOffset;

	public static Item originalProperties;

	public static Item eatingProperties;

	public static Vector3 fullSandwichEatingPosition = new Vector3(-0.007f, 0.088f, -0.011f);

	public static Vector3 OneBiteLeftSandwichEatingPosition = new Vector3(0.355f, 0.104f, -0.004f);

	public static Vector3 SandwichEatingRotation = new Vector3(-10f, -190f, 0f);

	public override void Start()
	{
		//IL_007e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0088: Expected O, but got Unknown
		((GrabbableObject)this).Start();
		((GrabbableObject)this).grabbable = true;
		((GrabbableObject)this).useCooldown = 0.3f;
		material = ((Renderer)((Component)this).GetComponent<MeshRenderer>()).material;
		material.SetInt("_Size", sandwichSize);
		UpdateEating(0f);
		audioSource = ((Component)this).GetComponent<AudioSource>();
		eatingSpeed = (float)sandwichSize * timeToEat;
		((GrabbableObject)this).itemProperties = originalProperties;
		((GrabbableObject)this).insertedBattery = new Battery(false, 0f);
		((GrabbableObject)this).SetScrapValue(Random.Range(((GrabbableObject)this).itemProperties.minValue, ((GrabbableObject)this).itemProperties.maxValue));
	}

	public override void ItemActivate(bool used, bool buttonDown = true)
	{
		((GrabbableObject)this).ItemActivate(used, buttonDown);
		if (buttonDown)
		{
			((GrabbableObject)this).isBeingUsed = true;
			startEating = ((MonoBehaviour)this).StartCoroutine((IEnumerator)StartEating());
			previousPlayerHeldBy.activatingItem = true;
			previousPlayerHeldBy.playerBodyAnimator.SetBool("useTZPItem", true);
			return;
		}
		((GrabbableObject)this).isBeingUsed = false;
		if (startEating != null)
		{
			((MonoBehaviour)this).StopCoroutine(startEating);
		}
		Stop();
	}

	public override void Update()
	{
		//IL_011a: Unknown result type (might be due to invalid IL or missing references)
		((GrabbableObject)this).Update();
		if (eating)
		{
			if ((Object)(object)previousPlayerHeldBy == (Object)null || !((GrabbableObject)this).isHeld || eaten > 1f)
			{
				eating = false;
			}
			UpdateEating(Mathf.MoveTowards(eaten, 1f, Time.deltaTime / eatingSpeed));
		}
		if (((GrabbableObject)this).isHeld && (Object)(object)previousPlayerHeldBy == (Object)(object)StartOfRound.Instance.localPlayerController)
		{
			((GrabbableObject)this).insertedBattery.charge = eaten;
			((GrabbableObject)this).SyncBatteryServerRpc((int)(eaten * 100f));
		}
		else if (eaten != ((GrabbableObject)this).insertedBattery.charge)
		{
			UpdateEating(((GrabbableObject)this).insertedBattery.charge, ignoreEating: true);
		}
		if (eaten >= 1f)
		{
			audioSource.PlayOneShot(finishSFX);
			RoundManager.Instance.PlayAudibleNoise(((Component)this).transform.position, 15f, 1.5f, 0, ((GrabbableObject)this).isInElevator && StartOfRound.Instance.hangarDoorsClosed, 0);
			((GrabbableObject)this).SyncBatteryServerRpc(100);
			((GrabbableObject)this).DestroyObjectInHand(previousPlayerHeldBy);
			((Behaviour)this).enabled = false;
		}
	}

	public void UpdateEating(float newValue, bool ignoreEating = false)
	{
		if (Mathf.Floor(newValue * (float)sandwichSize) / (float)sandwichSize != Mathf.Floor(eaten * (float)sandwichSize) / (float)sandwichSize && !ignoreEating)
		{
			Stop();
			((GrabbableObject)this).SetScrapValue((int)Mathf.Lerp(0f, (float)originalValue, 1f - eaten));
			((MonoBehaviour)this).StartCoroutine((IEnumerator)HealPlayer());
		}
		eaten = newValue;
		material.SetFloat("_Eating", eaten);
	}

	public IEnumerator<WaitForEndOfFrame> HealPlayer()
	{
		float overDuration = eatingSpeed / 4f;
		float time = 0f;
		int starting = previousPlayerHeldBy.health;
		int target = starting + healing;
		while (time < overDuration)
		{
			float hp = Mathf.Lerp((float)starting, (float)target, Mathf.InverseLerp(0f, overDuration, time));
			if (Mathf.CeilToInt(hp) != previousPlayerHeldBy.health)
			{
				previousPlayerHeldBy.health = Mathf.CeilToInt(hp);
				HUDManager.Instance.UpdateHealthUI(previousPlayerHeldBy.health, false);
			}
			time += Time.deltaTime;
			yield return null;
		}
		if (previousPlayerHeldBy.criticallyInjured && Random.Range(0f, 1f) < healCrippleChance)
		{
			previousPlayerHeldBy.criticallyInjured = false;
		}
	}

	public override void EquipItem()
	{
		((PhysicsProp)this).EquipItem();
		if ((Object)(object)((GrabbableObject)this).playerHeldBy != (Object)null)
		{
			previousPlayerHeldBy = ((GrabbableObject)this).playerHeldBy;
			if (originalValue == 0)
			{
				originalValue = ((GrabbableObject)this).scrapValue;
			}
		}
	}

	public override void DiscardItem()
	{
		if (startEating != null)
		{
			((MonoBehaviour)this).StopCoroutine(startEating);
		}
		Stop();
		if ((Object)(object)previousPlayerHeldBy != (Object)null)
		{
			previousPlayerHeldBy.activatingItem = false;
		}
		((GrabbableObject)this).DiscardItem();
	}

	public void Stop()
	{
		//IL_0047: Unknown result type (might be due to invalid IL or missing references)
		//IL_004c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0056: 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)
		eating = false;
		previousPlayerHeldBy.activatingItem = false;
		audioSource.Stop();
		previousPlayerHeldBy.playerBodyAnimator.SetBool("useTZPItem", false);
		((GrabbableObject)this).itemProperties = originalProperties;
		eatingProperties.rotationOffset = defaultRotation;
		eatingProperties.positionOffset = defaultOffset;
	}

	public IEnumerator<WaitForEndOfFrame> StartEating()
	{
		float time = 0f;
		float duration = 0.5f;
		Vector3 position = Vector3.Lerp(fullSandwichEatingPosition, OneBiteLeftSandwichEatingPosition, Mathf.Floor(eaten * (float)sandwichSize) / (float)sandwichSize);
		((GrabbableObject)this).itemProperties = eatingProperties;
		while (time < duration)
		{
			float t = Mathf.InverseLerp(0f, duration, time);
			eatingProperties.rotationOffset = Vector3.Lerp(defaultRotation, SandwichEatingRotation, t);
			eatingProperties.positionOffset = Vector3.Lerp(defaultOffset, position, t);
			time += Time.deltaTime;
			yield return new WaitForEndOfFrame();
		}
		eating = true;
		audioSource.PlayOneShot(eatingSFX);
		audioSource.pitch = eatingSFX.length / timeToEat;
		RoundManager.Instance.PlayAudibleNoise(((Component)this).transform.position, 10f, 1f, 0, ((GrabbableObject)this).isInElevator && StartOfRound.Instance.hangarDoorsClosed, 0);
	}
}
[BepInPlugin("Mellowdy.YummySandwich", "YummySandwich", "1.0.0")]
public class SandwichMod : BaseUnityPlugin
{
	private const string modGUID = "Mellowdy.YummySandwich";

	private const string modName = "YummySandwich";

	private const string modVersion = "1.0.0";

	private readonly Harmony harmony = new Harmony("Mellowdy.YummySandwich");

	public static ManualLogSource mls;

	private static SandwichMod instance;

	public static AssetBundle assets;

	public static string assetName = "sandwich.asset";

	public static string itemName = "Sandwich.asset";

	public static string prefabName = "Sandwich.prefab";

	public static int rarity = 30;

	private void Awake()
	{
		//IL_00af: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)instance == (Object)null)
		{
			instance = this;
		}
		mls = Logger.CreateLogSource("Mellowdy.YummySandwich");
		string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
		string text = Path.Combine(directoryName, assetName).Replace("\\", "/");
		assets = AssetBundle.LoadFromFile(text);
		GameObject val = assets.LoadAsset<GameObject>(prefabName);
		Sandwich sandwich = val.AddComponent<Sandwich>();
		Item val2 = assets.LoadAsset<Item>(itemName);
		Sandwich.eatingSFX = assets.LoadAsset<AudioClip>("Eat.wav");
		Sandwich.finishSFX = assets.LoadAsset<AudioClip>("FinishEating.wav");
		Sandwich.defaultOffset = val2.positionOffset;
		Sandwich.defaultRotation = val2.rotationOffset;
		Sandwich.originalProperties = val2;
		Sandwich.eatingProperties = Object.Instantiate<Item>(val2);
		Sandwich.sandwichSize = ((BaseUnityPlugin)this).Config.Bind<int>("Properties", "Size", 4, "How many times can the sandwich be eaten").Value;
		Sandwich.healing = ((BaseUnityPlugin)this).Config.Bind<int>("Properties", "Healing", 35, "How many hitpoints should be restored when one part is consumed").Value;
		float value = ((BaseUnityPlugin)this).Config.Bind<float>("Properties", "Heal Critical-injury Chance", -1f, "The chance to heal the critical-injury state with each bite (between 0 - 1)(set to -1 if you want the chance to be based off sandwich size)").Value;
		Sandwich.healCrippleChance = ((value == -1f) ? (1f / (float)Sandwich.sandwichSize) : value);
		Sandwich.timeToEat = ((BaseUnityPlugin)this).Config.Bind<int>("Properties", "Time", 2, "How long in seconds does it take to eat one part of the sandwich").Value;
		((GrabbableObject)sandwich).itemProperties = val2;
		val2.spawnPrefab = val;
		rarity = ((BaseUnityPlugin)this).Config.Bind<int>("Rarity", "Base rarity", 30, "Rarity of the sandwich between 1 - 100 (for context, on march: bottles/large axel/engine: 80 - 100. gold bar/robot/lazer pointer: 1 - 6)").Value;
		NetworkPrefabs.RegisterNetworkPrefab(val2.spawnPrefab);
		Items.RegisterScrap(val2, (int)((float)rarity * ((BaseUnityPlugin)this).Config.Bind<float>("Rarity", "Experimentation", 0.3f, "Rarity multiplier for Experimentation").Value), (LevelTypes)4);
		Items.RegisterScrap(val2, (int)((float)rarity * ((BaseUnityPlugin)this).Config.Bind<float>("Rarity", "Assurance", 0f, "Rarity multiplier for Assurance").Value), (LevelTypes)8);
		Items.RegisterScrap(val2, (int)((float)rarity * ((BaseUnityPlugin)this).Config.Bind<float>("Rarity", "Vow", 1.5f, "Rarity multiplier for Vow").Value), (LevelTypes)16);
		Items.RegisterScrap(val2, (int)((float)rarity * ((BaseUnityPlugin)this).Config.Bind<float>("Rarity", "Offense", 1f, "Rarity multiplier for Offense").Value), (LevelTypes)32);
		Items.RegisterScrap(val2, (int)((float)rarity * ((BaseUnityPlugin)this).Config.Bind<float>("Rarity", "March", 2f, "Rarity multiplier for March").Value), (LevelTypes)64);
		Items.RegisterScrap(val2, (int)((float)rarity * ((BaseUnityPlugin)this).Config.Bind<float>("Rarity", "Rend", 1f, "Rarity multiplier for Rend").Value), (LevelTypes)128);
		Items.RegisterScrap(val2, (int)((float)rarity * ((BaseUnityPlugin)this).Config.Bind<float>("Rarity", "Dine", 1f, "Rarity multiplier for Dine").Value), (LevelTypes)256);
		Items.RegisterScrap(val2, (int)((float)rarity * ((BaseUnityPlugin)this).Config.Bind<float>("Rarity", "Titan", 0.5f, "Rarity multiplier for Titan").Value), (LevelTypes)512);
		harmony.PatchAll();
		mls.LogInfo((object)"YummySandwich has been loaded");
	}
}