Decompiled source of MoreStacks v1.0.1

plugins\MoreStacks.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyFileVersion("1.0.1")]
[assembly: AssemblyVersion("1.0.1.0")]
namespace MoreStacks;

[BepInPlugin("stapz.morestacks", "MoreStacks", "1.0.1")]
public class Plugin : BaseUnityPlugin
{
	public const string GUID = "stapz.morestacks";

	public const string Name = "MoreStacks";

	public const string Version = "1.0.1";

	private const string RpcName = "MoreStacks_Multiplier";

	public static ConfigEntry<int> Multiplier;

	private static ManualLogSource log;

	private static int? serverValue;

	private static readonly Dictionary<SharedData, int> originals = new Dictionary<SharedData, int>();

	private static int ActiveMultiplier
	{
		get
		{
			int num = (serverValue.HasValue ? serverValue.Value : Multiplier.Value);
			if (num >= 1)
			{
				return num;
			}
			return 1;
		}
	}

	private void Awake()
	{
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		log = ((BaseUnityPlugin)this).Logger;
		Multiplier = ((BaseUnityPlugin)this).Config.Bind<int>("General", "Multiplier", 4, "Multiplier applied to the max stack size of every stackable item (stack size > 1). When connected to a server, the server's value is used.");
		new Harmony("stapz.morestacks").PatchAll();
	}

	public static void Apply(ObjectDB db)
	{
		if ((Object)(object)db == (Object)null)
		{
			return;
		}
		int activeMultiplier = ActiveMultiplier;
		int num = 0;
		foreach (GameObject item in db.m_items)
		{
			if ((Object)(object)item == (Object)null)
			{
				continue;
			}
			ItemDrop component = item.GetComponent<ItemDrop>();
			if ((Object)(object)component == (Object)null || component.m_itemData == null)
			{
				continue;
			}
			SharedData shared = component.m_itemData.m_shared;
			if (shared == null)
			{
				continue;
			}
			if (!originals.TryGetValue(shared, out var value))
			{
				if (shared.m_maxStackSize <= 1)
				{
					continue;
				}
				value = shared.m_maxStackSize;
				originals[shared] = value;
			}
			shared.m_maxStackSize = value * activeMultiplier;
			num++;
		}
		log.LogInfo((object)("Stack size x" + activeMultiplier + " applied to " + num + " item(s)" + (serverValue.HasValue ? " (server value)" : "")));
	}

	public static void OnPeerConnected(ZNetPeer peer)
	{
		peer.m_rpc.Register<int>("MoreStacks_Multiplier", (Action<ZRpc, int>)OnReceiveMultiplier);
	}

	public static void OnPeerInfo(ZRpc rpc)
	{
		if ((Object)(object)ZNet.instance != (Object)null && ZNet.instance.IsServer())
		{
			rpc.Invoke("MoreStacks_Multiplier", new object[1] { Multiplier.Value });
		}
	}

	private static void OnReceiveMultiplier(ZRpc rpc, int value)
	{
		if (!((Object)(object)ZNet.instance == (Object)null) && !ZNet.instance.IsServer())
		{
			serverValue = value;
			Apply(ObjectDB.instance);
		}
	}

	public static void OnDisconnected()
	{
		serverValue = null;
	}
}
[HarmonyPatch(typeof(ObjectDB), "Awake")]
internal static class ObjectDB_Awake_Patch
{
	[HarmonyPriority(0)]
	private static void Postfix(ObjectDB __instance)
	{
		Plugin.Apply(__instance);
	}
}
[HarmonyPatch(typeof(ObjectDB), "CopyOtherDB")]
internal static class ObjectDB_CopyOtherDB_Patch
{
	[HarmonyPriority(0)]
	private static void Postfix(ObjectDB __instance)
	{
		Plugin.Apply(__instance);
	}
}
[HarmonyPatch(typeof(ZNet), "OnNewConnection")]
internal static class ZNet_OnNewConnection_Patch
{
	private static void Postfix(ZNetPeer peer)
	{
		Plugin.OnPeerConnected(peer);
	}
}
[HarmonyPatch(typeof(ZNet), "RPC_PeerInfo")]
internal static class ZNet_RPC_PeerInfo_Patch
{
	private static void Postfix(ZRpc rpc)
	{
		Plugin.OnPeerInfo(rpc);
	}
}
[HarmonyPatch(typeof(ZNet), "Shutdown")]
internal static class ZNet_Shutdown_Patch
{
	private static void Postfix()
	{
		Plugin.OnDisconnected();
	}
}