Decompiled source of RepoShopDeals v0.1.0

ShopDeals.dll

Decompiled 2 hours ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[BepInPlugin("pavuk.repo.shopdeals", "R.E.P.O. Shop Deals", "0.1.0")]
public sealed class ShopDeals : BaseUnityPlugin
{
	private const string Id = "pavuk.repo.shopdeals";

	private static readonly Random Rolls = new Random();

	private static readonly FieldInfo ShopItem = AccessTools.Field(typeof(ItemAttributes), "shopItem");

	private static ShopDeals plugin;

	private static int discount;

	private static bool shopReady;

	private ConfigEntry<bool> enableDiscount;

	private ConfigEntry<int> freeChance;

	private Harmony harmony;

	private void Awake()
	{
		//IL_004c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0056: Expected O, but got Unknown
		//IL_0061: Unknown result type (might be due to invalid IL or missing references)
		//IL_006b: Expected O, but got Unknown
		//IL_0102: Unknown result type (might be due to invalid IL or missing references)
		//IL_010e: Expected O, but got Unknown
		//IL_0126: Unknown result type (might be due to invalid IL or missing references)
		//IL_0134: Expected O, but got Unknown
		plugin = this;
		enableDiscount = ((BaseUnityPlugin)this).Config.Bind<bool>("Shop", "EnableDiscount", true, "Roll a discount for each shop visit.");
		freeChance = ((BaseUnityPlugin)this).Config.Bind<int>("Shop", "FreeItemChancePercent", 5, new ConfigDescription("Chance for each shop item to cost zero.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 100), new object[0]));
		harmony = new Harmony("pavuk.repo.shopdeals");
		try
		{
			if (ShopItem == null || ShopItem.FieldType != typeof(bool))
			{
				throw new InvalidOperationException("The game shop item field has changed.");
			}
			MethodInfo methodInfo = AccessTools.GetDeclaredMethods(typeof(ShopManager)).Single((MethodInfo method) => method.Name == "ShopInitialize" && method.GetParameters().Length == 1);
			harmony.Patch((MethodBase)AccessTools.DeclaredMethod(typeof(ItemAttributes), "GetValue", Type.EmptyTypes, (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(typeof(ShopDeals), "InjectPrice", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null);
			harmony.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(ShopDeals), "RollShop", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"R.E.P.O. Shop Deals loaded.");
		}
		catch (Exception ex)
		{
			harmony.UnpatchSelf();
			((BaseUnityPlugin)this).Logger.LogError((object)ex);
		}
	}

	private void OnDestroy()
	{
		if (harmony != null)
		{
			harmony.UnpatchSelf();
		}
	}

	private static void RollShop()
	{
		shopReady = SemiFunc.IsMasterClientOrSingleplayer() && SemiFunc.RunIsShop();
		if (shopReady)
		{
			discount = (plugin.enableDiscount.Value ? RollDiscount() : 0);
			((BaseUnityPlugin)plugin).Logger.LogInfo((object)("Shop discount for this visit: " + discount + "%."));
		}
	}

	private static int RollDiscount()
	{
		int num = Rolls.Next(100);
		if (num < 45)
		{
			return Rolls.Next(0, 20);
		}
		if (num < 75)
		{
			return Rolls.Next(20, 60);
		}
		if (num < 90)
		{
			return Rolls.Next(60, 70);
		}
		if (num < 97)
		{
			return Rolls.Next(70, 80);
		}
		return Rolls.Next(80, 91);
	}

	private static int AdjustPrice(int price, ItemAttributes item)
	{
		if (!shopReady || !SemiFunc.RunIsShop() || !(bool)ShopItem.GetValue(item) || price <= 0)
		{
			return price;
		}
		if (Rolls.Next(100) < plugin.freeChance.Value)
		{
			return 0;
		}
		return Math.Max(1, (int)((long)price * (long)(100 - discount) / 100));
	}

	private static IEnumerable<CodeInstruction> InjectPrice(IEnumerable<CodeInstruction> instructions)
	{
		//IL_007f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0085: Expected O, but got Unknown
		//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c9: Expected O, but got Unknown
		List<CodeInstruction> list = instructions.ToList();
		FieldInfo value = AccessTools.Field(typeof(ItemAttributes), "value");
		MethodInfo methodInfo = AccessTools.DeclaredMethod(typeof(ShopDeals), "AdjustPrice", (Type[])null, (Type[])null);
		int num = list.FindIndex((CodeInstruction instruction) => CodeInstructionExtensions.StoresField(instruction, value));
		if (num < 0 || list.Count((CodeInstruction instruction) => CodeInstructionExtensions.StoresField(instruction, value)) != 1)
		{
			throw new InvalidOperationException("The game's price calculation has changed.");
		}
		CodeInstruction val = new CodeInstruction(OpCodes.Ldarg_0, (object)null);
		val.labels.AddRange(list[num].labels);
		list[num].labels.Clear();
		list.Insert(num, val);
		list.Insert(num + 1, new CodeInstruction(OpCodes.Call, (object)methodInfo));
		return list;
	}
}