This is decompiled code. Please rewrite it so it's more readable and has less mistakes. For context this is terraria mod code.

"using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using Mono.Cecil;
using Mono.Cecil.Cil;
using MonoMod.Cil;
using SerousCommonLib.API;
using Terraria;
using Terraria.GameContent;
using Terraria.GameContent.Personalities;
using Terraria.ModLoader;
using Terraria.ModLoader.Core;

namespace NoNPCHappinessReborn;

internal class HappinessEdits : Edit
{
	[CompilerGenerated]
	private static class _003C_003EO
	{
		public static Manipulator _003C0_003E__ShopHelper_ProcessMood;

		public static Manipulator _003C1_003E__ShopHelper_ApplyNpcRelationshipEffect;

		public static Manipulator _003C2_003E__ShopHelper_ApplyBiomeRelationshipEffect;

		public static Manipulator _003C3_003E__ShopHelper_LimitAndRoundMultiplier;

		public static hook_GetShoppingSettings _003C4_003E__ShopHelper_GetShoppingSettings;

		public static Manipulator _003C5_003E__Condition_cctor;

		public static PatchingContextDelegate _003C6_003E__Patch_ProcessMood;

		public static PatchingContextDelegate _003C7_003E__Patch_ApplyRelationshipEffect;

		public static PatchingContextDelegate _003C8_003E__Patch_LimitAndRoundMultiplier;

		public static PatchingContextDelegate _003C9_003E__FindHappyEnoughMethod;

		public static Func<bool> _003C10_003E__HappyEnoughToSellPylonsOverride;
	}

	private static Func<bool> HappyEnoughToSellPylons_orig;

	private static readonly FieldInfo Main_remixWorld = typeof(Main).GetField("remixWorld", BindingFlags.Static | BindingFlags.Public);

	public override void LoadEdits()
	{
		//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_001c: Expected O, but got Unknown
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		//IL_003d: Expected O, but got Unknown
		//IL_0053: Unknown result type (might be due to invalid IL or missing references)
		//IL_0058: Unknown result type (might be due to invalid IL or missing references)
		//IL_005e: Expected O, but got Unknown
		//IL_0074: Unknown result type (might be due to invalid IL or missing references)
		//IL_0079: Unknown result type (might be due to invalid IL or missing references)
		//IL_007f: Expected O, but got Unknown
		//IL_0095: Unknown result type (might be due to invalid IL or missing references)
		//IL_009a: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a0: Expected O, but got Unknown
		//IL_00b6: 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_00c1: Expected O, but got Unknown
		object obj = _003C_003EO._003C0_003E__ShopHelper_ProcessMood;
		if (obj == null)
		{
			Manipulator val = ShopHelper_ProcessMood;
			_003C_003EO._003C0_003E__ShopHelper_ProcessMood = val;
			obj = (object)val;
		}
		IL_ShopHelper.ProcessMood += (Manipulator)obj;
		object obj2 = _003C_003EO._003C1_003E__ShopHelper_ApplyNpcRelationshipEffect;
		if (obj2 == null)
		{
			Manipulator val2 = ShopHelper_ApplyNpcRelationshipEffect;
			_003C_003EO._003C1_003E__ShopHelper_ApplyNpcRelationshipEffect = val2;
			obj2 = (object)val2;
		}
		IL_ShopHelper.ApplyNpcRelationshipEffect += (Manipulator)obj2;
		object obj3 = _003C_003EO._003C2_003E__ShopHelper_ApplyBiomeRelationshipEffect;
		if (obj3 == null)
		{
			Manipulator val3 = ShopHelper_ApplyBiomeRelationshipEffect;
			_003C_003EO._003C2_003E__ShopHelper_ApplyBiomeRelationshipEffect = val3;
			obj3 = (object)val3;
		}
		IL_ShopHelper.ApplyBiomeRelationshipEffect += (Manipulator)obj3;
		object obj4 = _003C_003EO._003C3_003E__ShopHelper_LimitAndRoundMultiplier;
		if (obj4 == null)
		{
			Manipulator val4 = ShopHelper_LimitAndRoundMultiplier;
			_003C_003EO._003C3_003E__ShopHelper_LimitAndRoundMultiplier = val4;
			obj4 = (object)val4;
		}
		IL_ShopHelper.LimitAndRoundMultiplier += (Manipulator)obj4;
		object obj5 = _003C_003EO._003C4_003E__ShopHelper_GetShoppingSettings;
		if (obj5 == null)
		{
			hook_GetShoppingSettings val5 = ShopHelper_GetShoppingSettings;
			_003C_003EO._003C4_003E__ShopHelper_GetShoppingSettings = val5;
			obj5 = (object)val5;
		}
		On_ShopHelper.GetShoppingSettings += (hook_GetShoppingSettings)obj5;
		object obj6 = _003C_003EO._003C5_003E__Condition_cctor;
		if (obj6 == null)
		{
			Manipulator val6 = Condition_cctor;
			_003C_003EO._003C5_003E__Condition_cctor = val6;
			obj6 = (object)val6;
		}
		IL_Condition.cctor += (Manipulator)obj6;
		LoaderUtils.ResetStaticMembers(typeof(Condition), true);
		if (HappyEnoughToSellPylons_orig == null)
		{
			((Mod)NoNPCHappinessReborn.Instance).Logger.Error((object)"Could not find Condition.HappyEnough delegate");
		}
	}

	public override void UnloadEdits()
	{
		//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_001c: Expected O, but got Unknown
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		//IL_003d: Expected O, but got Unknown
		//IL_0053: Unknown result type (might be due to invalid IL or missing references)
		//IL_0058: Unknown result type (might be due to invalid IL or missing references)
		//IL_005e: Expected O, but got Unknown
		//IL_0074: Unknown result type (might be due to invalid IL or missing references)
		//IL_0079: Unknown result type (might be due to invalid IL or missing references)
		//IL_007f: Expected O, but got Unknown
		//IL_0095: Unknown result type (might be due to invalid IL or missing references)
		//IL_009a: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a0: Expected O, but got Unknown
		//IL_00b6: 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_00c1: Expected O, but got Unknown
		object obj = _003C_003EO._003C0_003E__ShopHelper_ProcessMood;
		if (obj == null)
		{
			Manipulator val = ShopHelper_ProcessMood;
			_003C_003EO._003C0_003E__ShopHelper_ProcessMood = val;
			obj = (object)val;
		}
		IL_ShopHelper.ProcessMood -= (Manipulator)obj;
		object obj2 = _003C_003EO._003C1_003E__ShopHelper_ApplyNpcRelationshipEffect;
		if (obj2 == null)
		{
			Manipulator val2 = ShopHelper_ApplyNpcRelationshipEffect;
			_003C_003EO._003C1_003E__ShopHelper_ApplyNpcRelationshipEffect = val2;
			obj2 = (object)val2;
		}
		IL_ShopHelper.ApplyNpcRelationshipEffect -= (Manipulator)obj2;
		object obj3 = _003C_003EO._003C2_003E__ShopHelper_ApplyBiomeRelationshipEffect;
		if (obj3 == null)
		{
			Manipulator val3 = ShopHelper_ApplyBiomeRelationshipEffect;
			_003C_003EO._003C2_003E__ShopHelper_ApplyBiomeRelationshipEffect = val3;
			obj3 = (object)val3;
		}
		IL_ShopHelper.ApplyBiomeRelationshipEffect -= (Manipulator)obj3;
		object obj4 = _003C_003EO._003C3_003E__ShopHelper_LimitAndRoundMultiplier;
		if (obj4 == null)
		{
			Manipulator val4 = ShopHelper_LimitAndRoundMultiplier;
			_003C_003EO._003C3_003E__ShopHelper_LimitAndRoundMultiplier = val4;
			obj4 = (object)val4;
		}
		IL_ShopHelper.LimitAndRoundMultiplier -= (Manipulator)obj4;
		object obj5 = _003C_003EO._003C4_003E__ShopHelper_GetShoppingSettings;
		if (obj5 == null)
		{
			hook_GetShoppingSettings val5 = ShopHelper_GetShoppingSettings;
			_003C_003EO._003C4_003E__ShopHelper_GetShoppingSettings = val5;
			obj5 = (object)val5;
		}
		On_ShopHelper.GetShoppingSettings -= (hook_GetShoppingSettings)obj5;
		object obj6 = _003C_003EO._003C5_003E__Condition_cctor;
		if (obj6 == null)
		{
			Manipulator val6 = Condition_cctor;
			_003C_003EO._003C5_003E__Condition_cctor = val6;
			obj6 = (object)val6;
		}
		IL_Condition.cctor -= (Manipulator)obj6;
		LoaderUtils.ResetStaticMembers(typeof(Condition), true);
	}

	private static void ShopHelper_ProcessMood(ILContext il)
	{
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: Expected O, but got Unknown
		NoNPCHappinessReborn instance = NoNPCHappinessReborn.Instance;
		object obj = _003C_003EO._003C6_003E__Patch_ProcessMood;
		if (obj == null)
		{
			PatchingContextDelegate val = Patch_ProcessMood;
			_003C_003EO._003C6_003E__Patch_ProcessMood = val;
			obj = (object)val;
		}
		ILHelper.CommonPatchingWrapper(il, (Mod)(object)instance, false, (PatchingContextDelegate)obj);
	}

	private static bool Patch_ProcessMood(ILCursor c, ref string badReturnReason)
	{
		//IL_0089: Unknown result type (might be due to invalid IL or missing references)
		ILLabel remixCheck = null;
		if (!c.TryGotoNext((MoveType)2, new Func<Instruction, bool>[2]
		{
			(Instruction i) => ILPatternMatchingExt.MatchLdsfld(i, Main_remixWorld),
			(Instruction i) => ILPatternMatchingExt.MatchBrfalse(i, ref remixCheck)
		}))
		{
			badReturnReason = "Could not find instruction sequence for checking Main.remixWorld";
			return false;
		}
		c.EmitDelegate<Func<bool>>((Func<bool>)(() => HappinessConfig.HappinessEnabled));
		c.Emit(OpCodes.Brtrue, (object)remixCheck);
		if (!c.TryGotoNext((MoveType)2, new Func<Instruction, bool>[1]
		{
			(Instruction i) => ILPatternMatchingExt.MatchLdcR4(i, 1.05f)
		}))
		{
			badReturnReason = "Could not find constant for crowded multiplier";
			return false;
		}
		c.EmitDelegate<Func<float, float>>((Func<float, float>)((float num) => HappinessConfig.PriceAdjustmentsEnabled ? HappinessConfig.NPCCrowdedPriceAdjustment : num));
		if (!c.TryGotoNext((MoveType)2, new Func<Instruction, bool>[1]
		{
			(Instruction i) => ILPatternMatchingExt.MatchLdcR4(i, 0.95f)
		}))
		{
			badReturnReason = "Could not find constant for space multiplier";
			return false;
		}
		c.EmitDelegate<Func<float, float>>((Func<float, float>)((float num) => HappinessConfig.PriceAdjustmentsEnabled ? HappinessConfig.NPCSpacePriceAdjustment : num));
		return true;
	}

	private static void ShopHelper_ApplyNpcRelationshipEffect(ILContext il)
	{
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: Expected O, but got Unknown
		NoNPCHappinessReborn instance = NoNPCHappinessReborn.Instance;
		object obj = _003C_003EO._003C7_003E__Patch_ApplyRelationshipEffect;
		if (obj == null)
		{
			PatchingContextDelegate val = Patch_ApplyRelationshipEffect;
			_003C_003EO._003C7_003E__Patch_ApplyRelationshipEffect = val;
			obj = (object)val;
		}
		ILHelper.CommonPatchingWrapper(il, (Mod)(object)instance, false, (PatchingContextDelegate)obj);
	}

	private static void ShopHelper_ApplyBiomeRelationshipEffect(ILContext il)
	{
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: Expected O, but got Unknown
		NoNPCHappinessReborn instance = NoNPCHappinessReborn.Instance;
		object obj = _003C_003EO._003C7_003E__Patch_ApplyRelationshipEffect;
		if (obj == null)
		{
			PatchingContextDelegate val = Patch_ApplyRelationshipEffect;
			_003C_003EO._003C7_003E__Patch_ApplyRelationshipEffect = val;
			obj = (object)val;
		}
		ILHelper.CommonPatchingWrapper(il, (Mod)(object)instance, false, (PatchingContextDelegate)obj);
	}

	private static bool Patch_ApplyRelationshipEffect(ILCursor c, ref string badReturnReason)
	{
		//IL_0044: Unknown result type (might be due to invalid IL or missing references)
		if (!c.TryGotoNext((MoveType)0, new Func<Instruction, bool>[1]
		{
			(Instruction i) => ILPatternMatchingExt.MatchMul(i)
		}))
		{
			badReturnReason = "Could not find mul instruction";
			return false;
		}
		c.Emit(OpCodes.Ldarg_2);
		c.EmitDelegate<Func<float, AffectionLevel, float>>((Func<float, AffectionLevel, float>)delegate(float currentMultiplier, AffectionLevel affection)
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Invalid comparison between Unknown and I4
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Invalid comparison between Unknown and I4
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Invalid comparison between Unknown and I4
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Invalid comparison between Unknown and I4
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Invalid comparison between Unknown and I4
			if (!HappinessConfig.PriceAdjustmentsEnabled)
			{
				return currentMultiplier;
			}
			if (1 == 0)
			{
			}
			float result;
			if ((int)affection <= -50)
			{
				if ((int)affection != -100)
				{
					if ((int)affection != -50)
					{
						goto IL_0056;
					}
					result = HappinessConfig.NPCDislikesPriceAdjustment;
				}
				else
				{
					result = HappinessConfig.NPCHatesPriceAdjustment;
				}
			}
			else if ((int)affection != 50)
			{
				if ((int)affection != 100)
				{
					goto IL_0056;
				}
				result = HappinessConfig.NPCLovesPriceAdjustment;
			}
			else
			{
				result = HappinessConfig.NPCLikesPriceAdjustment;
			}
			goto IL_005a;
			IL_005a:
			if (1 == 0)
			{
			}
			return result;
			IL_0056:
			result = currentMultiplier;
			goto IL_005a;
		});
		return true;
	}

	private static void ShopHelper_LimitAndRoundMultiplier(ILContext il)
	{
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: Expected O, but got Unknown
		NoNPCHappinessReborn instance = NoNPCHappinessReborn.Instance;
		object obj = _003C_003EO._003C8_003E__Patch_LimitAndRoundMultiplier;
		if (obj == null)
		{
			PatchingContextDelegate val = Patch_LimitAndRoundMultiplier;
			_003C_003EO._003C8_003E__Patch_LimitAndRoundMultiplier = val;
			obj = (object)val;
		}
		ILHelper.CommonPatchingWrapper(il, (Mod)(object)instance, false, (PatchingContextDelegate)obj);
	}

	private static bool Patch_LimitAndRoundMultiplier(ILCursor c, ref string badReturnReason)
	{
		if (!c.TryGotoNext((MoveType)2, new Func<Instruction, bool>[1]
		{
			(Instruction i) => ILPatternMatchingExt.MatchLdcR4(i, 0.75f)
		}))
		{
			badReturnReason = "Could not find minimum price multiplier constant";
			return false;
		}
		c.EmitDelegate<Func<float, float>>((Func<float, float>)((float min) => HappinessConfig.PriceAdjustmentsEnabled ? HappinessConfig.MinimumPriceAdjustmentOverride : min));
		if (!c.TryGotoNext((MoveType)2, new Func<Instruction, bool>[1]
		{
			(Instruction i) => ILPatternMatchingExt.MatchLdcR4(i, 1.5f)
		}))
		{
			badReturnReason = "Could not find maximum price multiplier constant";
			return false;
		}
		c.EmitDelegate<Func<float, float>>((Func<float, float>)((float max) => HappinessConfig.PriceAdjustmentsEnabled ? HappinessConfig.MaximumPriceAdjustmentOverride : max));
		return true;
	}

	private static ShoppingSettings ShopHelper_GetShoppingSettings(orig_GetShoppingSettings orig, ShopHelper self, Player player, NPC npc)
	{
		//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_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_0039: Unknown result type (might be due to invalid IL or missing references)
		ShoppingSettings settings = orig.Invoke(self, player, npc);
		if (!HappinessConfig.HappinessEnabled)
		{
			settings.PriceAdjustment = 1.0;
			settings.HappinessReport = "";
		}
		return settings;
	}

	private static void Condition_cctor(ILContext il)
	{
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: Expected O, but got Unknown
		NoNPCHappinessReborn instance = NoNPCHappinessReborn.Instance;
		object obj = _003C_003EO._003C9_003E__FindHappyEnoughMethod;
		if (obj == null)
		{
			PatchingContextDelegate val = FindHappyEnoughMethod;
			_003C_003EO._003C9_003E__FindHappyEnoughMethod = val;
			obj = (object)val;
		}
		ILHelper.CommonPatchingWrapper(il, (Mod)(object)instance, false, (PatchingContextDelegate)obj);
	}

	private static bool FindHappyEnoughMethod(ILCursor c, ref string badReturnReason)
	{
		FieldReference val3 = default(FieldReference);
		MethodReference val2 = default(MethodReference);
		MethodReference val = default(MethodReference);
		if (!c.TryGotoNext((MoveType)2, new Func<Instruction, bool>[4]
		{
			(Instruction i) => ILPatternMatchingExt.MatchLdstr(i, "Conditions.HappyEnoughForPylons"),
			(Instruction i) => ILPatternMatchingExt.MatchLdsfld(i, ref val3),
			(Instruction i) => ILPatternMatchingExt.MatchLdftn(i, ref val2),
			(Instruction i) => ILPatternMatchingExt.MatchNewobj(i, ref val)
		}))
		{
			badReturnReason = "Could not find assignment to Conditions.HappyEnoughToSellPylons";
			return false;
		}
		c.EmitDelegate<Func<Func<bool>, Func<bool>>>((Func<Func<bool>, Func<bool>>)delegate(Func<bool> func)
		{
			HappyEnoughToSellPylons_orig = func;
			return HappyEnoughToSellPylonsOverride;
		});
		return true;
	}

	private static bool HappyEnoughToSellPylonsOverride()
	{
		return HappinessConfig.PylonsAlwaysSellableOverride || HappyEnoughToSellPylons_orig();
	}
}
"