Decompiled source of EatToSell v1.0.1

BepInEx\plugins\EatToSell\EatToSell.dll

Decompiled 19 hours ago
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;

[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: CompilationRelaxations(8)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace EatToSell;

[BepInPlugin("cn.htf.eattosell", "Eat To Sell", "1.0.0")]
public class EatToSellPlugin : BaseUnityPlugin
{
	internal static ManualLogSource Log;

	private void Awake()
	{
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_0018: Expected O, but got Unknown
		//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00df: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		try
		{
			Harmony val = new Harmony("cn.htf.eattosell");
			Type type = AccessTools.TypeByName("Server");
			if (type == null)
			{
				Log.LogError((object)"EatToSell: Server type not found");
				return;
			}
			MethodInfo methodInfo = null;
			MethodInfo[] methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
			foreach (MethodInfo methodInfo2 in methods)
			{
				if (methodInfo2.Name.StartsWith("RpcLogic___FinishEatingCreature", StringComparison.Ordinal))
				{
					methodInfo = methodInfo2;
					break;
				}
			}
			if (methodInfo == null)
			{
				Log.LogError((object)"EatToSell: eating RPC not found");
				return;
			}
			val.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(EatToSellPlugin).GetMethod("OnCreatureEaten", BindingFlags.Static | BindingFlags.NonPublic)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			Log.LogInfo((object)"Eat To Sell ready: eating food pays out its sell value.");
		}
		catch (Exception ex)
		{
			Log.LogError((object)("EatToSell init failed: " + ex));
		}
	}

	private static void OnCreatureEaten([HarmonyArgument(0)] object creature)
	{
		try
		{
			if (creature == null)
			{
				return;
			}
			Type type = AccessTools.TypeByName("MoneyManager");
			if (type == null)
			{
				return;
			}
			FieldInfo field = type.GetField("Instance", BindingFlags.Static | BindingFlags.Public);
			object obj = ((field == null) ? null : field.GetValue(null));
			if (obj == null)
			{
				return;
			}
			int num = 0;
			Type type2 = creature.GetType();
			while (type2 != null)
			{
				PropertyInfo property = type2.GetProperty("TotalWorth", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				if (property != null)
				{
					object value = property.GetValue(creature, null);
					if (value is int)
					{
						num = (int)value;
					}
					break;
				}
				type2 = type2.BaseType;
			}
			MethodInfo methodInfo = null;
			MethodInfo[] methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
			foreach (MethodInfo methodInfo2 in methods)
			{
				if (methodInfo2.Name == "SellItem" && methodInfo2.GetParameters().Length == 1)
				{
					methodInfo = methodInfo2;
					break;
				}
			}
			if (methodInfo != null)
			{
				methodInfo.Invoke(obj, new object[1] { creature });
				Log.LogInfo((object)("EatToSell: +$" + num));
			}
		}
		catch (Exception ex)
		{
			Log.LogWarning((object)("EatToSell error: " + ex.Message));
		}
	}
}