Decompiled source of MuckRandom v1.0.0

muck.dll

Decompiled 4 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using Steamworks;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = ".NET Standard 2.0")]
[assembly: AssemblyCompany("muck")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("My first plugin")]
[assembly: AssemblyTitle("muck")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace muck
{
	public class Mod : MonoBehaviour
	{
		private BindingFlags flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;

		public bool show = false;

		public bool showDebug = false;

		public bool typing = false;

		public bool godmode = false;

		public bool autoRegenHealth = false;

		public bool autoRegenHunger = false;

		public bool autoRegenStamina = false;

		public string s = "";

		public static string debug = "";

		private Random rng = new Random();

		public void Start()
		{
		}

		public void OnGUI()
		{
			//IL_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Invalid comparison between Unknown and I4
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			if (show)
			{
				Event current = Event.current;
				Rect val = default(Rect);
				((Rect)(ref val))..ctor(100f, 100f, 200f, 20f);
				Rect val2 = default(Rect);
				((Rect)(ref val2))..ctor(100f, 160f, 1000f, 800f);
				if (showDebug)
				{
					GUI.Box(val2, debug);
				}
				GUI.SetNextControlName("typing");
				s = GUI.TextField(val, s);
				if ((int)current.keyCode == 13 && typing)
				{
					executeCommand();
					typing = false;
				}
				if (typing)
				{
					GUI.FocusControl("typing");
				}
				else
				{
					GUI.FocusControl((string)null);
				}
			}
		}

		public string join(string[] argv, string on, int l, int r)
		{
			string text = "";
			for (int i = l; i < r; i++)
			{
				text = text + argv[i] + on;
			}
			return text + argv[r];
		}

		public void InventoryItemToPacket(Packet packet, InventoryItem item)
		{
			try
			{
				packet.Write(item.id);
				packet.Write(item.name);
				packet.Write(item.max);
				packet.Write(item.stackable);
				packet.Write(item.requirements.Length);
				for (int i = 0; i < item.requirements.Length; i++)
				{
					packet.Write(item.requirements[i].item.id);
					packet.Write(item.requirements[i].amount);
				}
				packet.Write(item.craftAmount);
				packet.Write(item.unlockWithFirstRequirementOnly);
				packet.Write(item.craftable);
				if (item.craftable)
				{
					packet.Write(item.stationRequirement.id);
				}
			}
			catch (Exception ex)
			{
				debug += ex.ToString();
			}
		}

		public static InventoryItem InventoryItemFromPacket(Packet packet)
		{
			//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0101: Expected O, but got Unknown
			debug = "";
			try
			{
				InventoryItem val = ScriptableObject.CreateInstance<InventoryItem>();
				val.id = packet.ReadInt(true);
				debug = debug + val.id + " ";
				val.name = packet.ReadString(true);
				debug = debug + val.name + " ";
				val.max = packet.ReadInt(true);
				debug = debug + val.max + " ";
				val.stackable = packet.ReadBool(true);
				debug = debug + val.stackable + " ";
				int num = packet.ReadInt(true);
				val.requirements = (CraftRequirement[])(object)new CraftRequirement[num];
				debug = debug + num + " ";
				for (int i = 0; i < num; i++)
				{
					val.requirements[i] = new CraftRequirement();
					val.requirements[i].item = ScriptableObject.CreateInstance<InventoryItem>();
					val.requirements[i].item.id = packet.ReadInt(true);
					val.requirements[i].amount = packet.ReadInt(true);
					debug = debug + val.requirements[i].item.id + " " + val.requirements[i].amount + " ";
				}
				val.craftAmount = packet.ReadInt(true);
				debug = debug + val.craftAmount + " ";
				val.unlockWithFirstRequirementOnly = packet.ReadBool(true);
				debug = debug + val.unlockWithFirstRequirementOnly + " ";
				val.craftable = packet.ReadBool(true);
				debug = debug + val.craftable + " ";
				val.stationRequirement = ScriptableObject.CreateInstance<InventoryItem>();
				if (val.craftable)
				{
					val.stationRequirement.id = packet.ReadInt(true);
					debug = debug + val.stationRequirement.id + " ";
				}
				Plugin.Logger.LogInfo((object)debug);
				return val;
			}
			catch (Exception ex)
			{
				debug = ex.ToString();
				return null;
			}
		}

		public static void Test(Packet packet)
		{
			Plugin.Logger.LogInfo((object)"packet received");
		}

		public static void ClientCopyItems(Packet packet)
		{
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b6: Expected O, but got Unknown
			debug += "copying items..\n";
			try
			{
				Dictionary<int, InventoryItem> allItems = ItemManager.Instance.allItems;
				int[] array = allItems.Keys.ToArray();
				int num = packet.ReadInt(true);
				Plugin.Logger.LogInfo((object)("Client count: " + num));
				for (int i = 0; i < num; i++)
				{
					InventoryItem val = InventoryItemFromPacket(packet);
					int num2 = 0;
					int id = val.id;
					for (int j = 0; j < array.Length; j++)
					{
						num2 = array[j];
						if (allItems[num2].name == val.name)
						{
							break;
						}
					}
					InventoryItem val2 = new InventoryItem();
					val2.Copy(allItems[num2], allItems[num2].amount);
					allItems[num2].Copy(allItems[id], allItems[id].amount);
					allItems[id] = val2;
					allItems[id].id = num2;
					allItems[num2].id = id;
					val2 = allItems[id];
					val2.id = val.id;
					val2.name = val.name;
					val2.stackable = val.stackable;
					val2.requirements = val.requirements;
					val2.craftAmount = val.craftAmount;
					val2.unlockWithFirstRequirementOnly = val.unlockWithFirstRequirementOnly;
					val2.craftable = val.craftable;
					val2.stationRequirement = val.stationRequirement;
				}
				debug += "success";
			}
			catch (Exception ex)
			{
				debug += ex.ToString();
			}
		}

		public int NextValidKeyIdx(int[] keys_id, int[] unobtainable_keys, int n)
		{
			int num = rng.Next(n);
			while (unobtainable_keys.Contains(keys_id[num]))
			{
				num = rng.Next(n);
			}
			return num;
		}

		public void SendPacket(Packet packet)
		{
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Invalid comparison between Unknown and I4
			//IL_0093: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				packet.WriteLength();
				if ((int)NetworkController.Instance.networkType == 1)
				{
					for (int i = 1; i < Server.MaxPlayers; i++)
					{
						Server.clients[i].tcp.SendData(packet);
					}
					return;
				}
				foreach (Client value in Server.clients.Values)
				{
					if (value != null && value.player != null)
					{
						SteamPacketManager.SendPacket(SteamId.op_Implicit(value.player.steamId.Value), packet, (P2PSend)2, (NetworkChannel)0);
					}
				}
			}
			catch (Exception ex)
			{
				Plugin.Logger.LogInfo((object)ex.ToString());
			}
		}

		public void executeCommand()
		{
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Expected O, but got Unknown
			//IL_07f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_07fc: Expected O, but got Unknown
			//IL_0609: Unknown result type (might be due to invalid IL or missing references)
			//IL_0610: Expected O, but got Unknown
			//IL_0767: Unknown result type (might be due to invalid IL or missing references)
			//IL_076d: Expected O, but got Unknown
			string[] array = s.Split(new string[1] { " " }, StringSplitOptions.None);
			s = "";
			if (array[0] == "test")
			{
				Packet packet = new Packet(101);
				SendPacket(packet);
			}
			if (array[0] == "skip")
			{
				Tutorial.Instance.ContinueTutorial();
			}
			if (array[0] == "item")
			{
				string text = (debug = join(array, " ", 1, array.Length - 2));
				InventoryItem val = Object.Instantiate<InventoryItem>(ItemManager.Instance.GetItemByName(text));
				val.amount = int.Parse(array[^1]);
				InventoryUI.Instance.AddItemToInventory(val);
			}
			if (array[0] == "itemid")
			{
				int key = int.Parse(array[1]);
				InventoryItem val2 = Object.Instantiate<InventoryItem>(ItemManager.Instance.allItems[key]);
				val2.amount = int.Parse(array[2]);
				InventoryUI.Instance.AddItemToInventory(val2);
			}
			if (array[0] == "god")
			{
				godmode = !godmode;
				autoRegenHealth = godmode;
				autoRegenHunger = godmode;
				autoRegenStamina = godmode;
			}
			if (array[0] == "power")
			{
				string text2 = "";
				for (int i = 1; i < array.Length - 2; i++)
				{
					text2 = text2 + array[i] + " ";
				}
				text2 += array[^2];
				int num = ItemManager.Instance.stringToPowerupId[text2];
				int num2 = int.Parse(array[^1]);
				for (int j = 0; j < num2; j++)
				{
					PowerupUI.Instance.AddPowerup(num);
				}
				FieldInfo field = ((object)PowerupInventory.Instance).GetType().GetField("powerups", flags);
				int[] array2 = (int[])field.GetValue(PowerupInventory.Instance);
				array2[num] += num2;
				field.SetValue(PowerupInventory.Instance, array2);
			}
			if (array[0] == "powerid")
			{
				int num3 = int.Parse(array[^2]);
				int num4 = int.Parse(array[^1]);
				for (int k = 0; k < num4; k++)
				{
					PowerupUI.Instance.AddPowerup(num3);
				}
				FieldInfo field2 = ((object)PowerupInventory.Instance).GetType().GetField("powerups", flags);
				int[] array3 = (int[])field2.GetValue(PowerupInventory.Instance);
				array3[num3] += num4;
				field2.SetValue(PowerupInventory.Instance, array3);
			}
			if (array[0] == "powerall")
			{
				int num5 = int.Parse(array[1]);
				FieldInfo field3 = ((object)PowerupInventory.Instance).GetType().GetField("powerups", flags);
				int[] array4 = (int[])field3.GetValue(PowerupInventory.Instance);
				for (int l = 0; l < array4.Length; l++)
				{
					for (int m = 0; m < num5; m++)
					{
						PowerupUI.Instance.AddPowerup(l);
					}
					array4[l] += num5;
				}
				field3.SetValue(PowerupInventory.Instance, array4);
			}
			if (array[0] == "set" && array[1] == "maxhealth")
			{
				int num6 = int.Parse(array[2]);
				if (num6 > 0)
				{
					PlayerStatus.Instance.maxHp = num6;
				}
			}
			if (array[0] == "toggle")
			{
				if (array[1] == "hunger")
				{
					autoRegenHunger = !autoRegenHunger;
				}
				if (array[1] == "health")
				{
					autoRegenHealth = !autoRegenHealth;
				}
				if (array[1] == "stamina")
				{
					autoRegenStamina = !autoRegenStamina;
				}
			}
			if (array[0] == "discover")
			{
				foreach (InventoryItem value in ItemManager.Instance.allItems.Values)
				{
					UiEvents.Instance.AddPickup(value);
				}
			}
			if (!(array[0] == "randomize"))
			{
				return;
			}
			Dictionary<int, InventoryItem> allItems = ItemManager.Instance.allItems;
			int[] array5 = new int[11]
			{
				-1, 73, 76, 77, 78, 79, 80, 82, 124, 130,
				141
			};
			int[] array6 = new int[3] { 96, 98, 100 };
			int count = allItems.Count;
			int num7 = count;
			int[] array7 = new int[num7];
			InventoryItem[] array8 = (InventoryItem[])(object)new InventoryItem[num7];
			allItems.Keys.CopyTo(array7, 0);
			allItems.Values.CopyTo(array8, 0);
			while (num7 > 1)
			{
				num7--;
				if (array5.Contains(array7[num7]))
				{
					continue;
				}
				int num8 = NextValidKeyIdx(array7, array5, count);
				for (int n = 0; n < array6.Length; n++)
				{
					if (array6[n] != array7[num7])
					{
						continue;
					}
					for (int num9 = 0; num9 < array6.Length; num9++)
					{
						if (array6[num9] == array7[num8])
						{
							array6[num9] = array7[num7];
						}
					}
					array6[n] = array7[num8];
				}
				InventoryItem val3 = new InventoryItem();
				val3.Copy(array8[num8], array8[num8].amount);
				array8[num8].Copy(array8[num7], array8[num7].amount);
				array8[num7].Copy(val3, val3.amount);
				array8[num8].id = array7[num8];
				array8[num7].id = array7[num7];
			}
			int num10 = 0;
			for (int num11 = 0; num11 < count; num11++)
			{
				if (!array5.Contains(array7[num11]))
				{
					num10++;
					array5[0] = array7[num11];
					InventoryItem val4 = array8[num11];
					val4.stackable = true;
					val4.unlockWithFirstRequirementOnly = true;
					val4.max = 9999;
					val4.craftAmount = rng.Next(10) + 1;
					int num12 = rng.Next(4);
					val4.craftable = num12 != 0;
					val4.requirements = (CraftRequirement[])(object)new CraftRequirement[num12];
					val4.craftAmount = (val4.craftable ? (rng.Next(10) + 1) : 0);
					val4.stationRequirement = allItems[array6[rng.Next(array6.Length)]];
					for (int num13 = 0; num13 < num12; num13++)
					{
						val4.requirements[num13] = new CraftRequirement();
						val4.requirements[num13].item = array8[NextValidKeyIdx(array7, array5, count)];
						val4.requirements[num13].amount = rng.Next(10) + 1;
					}
				}
			}
			array5[0] = -1;
			Plugin.Logger.LogInfo((object)("Server count: " + num10));
			Packet val5 = new Packet(100);
			val5.Write(num10);
			for (int num14 = 0; num14 < count; num14++)
			{
				if (!array5.Contains(array7[num14]))
				{
					InventoryItem item = array8[num14];
					InventoryItemToPacket(val5, item);
				}
			}
			SendPacket(val5);
		}

		public void InitLocalPacketHandlers()
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Expected O, but got Unknown
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Expected O, but got Unknown
			LocalClient.packetHandlers[100] = new PacketHandler(ClientCopyItems);
			LocalClient.packetHandlers[101] = new PacketHandler(Test);
			Plugin.Logger.LogInfo((object)"initialized packet handlers");
		}

		public void Update()
		{
			if (LocalClient.packetHandlers != null && !LocalClient.packetHandlers.ContainsKey(100))
			{
				InitLocalPacketHandlers();
			}
			if (Input.GetKeyDown((KeyCode)282))
			{
				show = !show;
			}
			if (Input.GetKeyDown((KeyCode)283))
			{
				typing = true;
			}
			if (Input.GetKeyDown((KeyCode)284))
			{
				debug = "";
			}
			if (Input.GetKeyDown((KeyCode)285))
			{
				showDebug = !showDebug;
			}
			if (autoRegenStamina)
			{
				PlayerStatus.Instance.stamina = PlayerStatus.Instance.maxStamina;
			}
			if (autoRegenHunger)
			{
				PlayerStatus.Instance.hunger = PlayerStatus.Instance.maxHunger;
			}
			if (autoRegenHealth)
			{
				PlayerStatus.Instance.hp = PlayerStatus.Instance.maxHp;
			}
		}
	}
	[BepInPlugin("fancydev.muckrandom", "Muck Randomizer", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public static ManualLogSource Logger;

		public static GameObject L;

		private void Awake()
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Expected O, but got Unknown
			Logger = ((BaseUnityPlugin)this).Logger;
			Logger.LogInfo((object)"Plugin muck is loaded!");
			L = new GameObject();
			L.AddComponent<Mod>();
			Object.DontDestroyOnLoad((Object)(object)L);
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "muck";

		public const string PLUGIN_NAME = "My first plugin";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}