Decompiled source of PotatoHammer v1.0.1

plugins/PotatoHammer.dll

Decompiled 7 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Jotunn.Entities;
using Jotunn.Managers;
using Jotunn.Utils;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace PotatoHammer;

[BepInPlugin("potato.hammer", "Potato Hammer", "1.0.1")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
public class Plugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Player), "SetPlaceMode")]
	internal static class TakeOutHammer
	{
		private static void Prefix(PieceTable buildPieces)
		{
			if (!Object.op_Implicit((Object)(object)buildPieces) || (Object)(object)buildPieces != (Object)(object)Hammer())
			{
				return;
			}
			try
			{
				Sort(buildPieces);
			}
			catch (Exception ex)
			{
				Log.LogWarning((object)("Couldn't sort the hammer: " + ex));
			}
		}
	}

	public const string Id = "potato.hammer";

	public const string Version = "1.0.1";

	private const string TabsSection = "2 - Tabs";

	internal static ManualLogSource Log;

	private static Plugin instance;

	private static ConfigEntry<bool> sortOn;

	private static ConfigEntry<string> leaveAlone;

	private static ConfigEntry<string> extraPieces;

	private static readonly Dictionary<string, ConfigEntry<string>> TabNames = new Dictionary<string, ConfigEntry<string>>();

	private static readonly Dictionary<Piece, PieceCategory> Original = new Dictionary<Piece, PieceCategory>();

	private static readonly Dictionary<Piece, UsageTagFlags> OriginalUsage = new Dictionary<Piece, UsageTagFlags>();

	private static string lastSummary;

	private void Awake()
	{
		//IL_0083: Unknown result type (might be due to invalid IL or missing references)
		instance = this;
		Log = ((BaseUnityPlugin)this).Logger;
		sortOn = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - General", "Sort the hammer", true, "Move each mod's hammer pieces to a tab named after the mod. The game's own pieces stay where they are. Takes effect the next time you take out the hammer.");
		leaveAlone = ((BaseUnityPlugin)this).Config.Bind<string>("1 - General", "Mods to leave alone", "", "Mods whose pieces stay where the mod put them, separated by commas (mod name as in the tab list below, or its id).");
		extraPieces = ((BaseUnityPlugin)this).Config.Bind<string>("1 - General", "Extra pieces", "", "Pieces whose mod can't be told (they stay where they are; the log lists the hammer's pieces by tab), put on a tab by name: piece=Tab, separated by commas. Example: piece_mytable=My Mod.");
		ModQuery.Enable();
		new Harmony("potato.hammer").PatchAll(typeof(Plugin).Assembly);
	}

	private static PieceTable Hammer()
	{
		GameObject val = (Object.op_Implicit((Object)(object)ObjectDB.instance) ? ObjectDB.instance.GetItemPrefab("Hammer") : null);
		if (!Object.op_Implicit((Object)(object)val))
		{
			return null;
		}
		return val.GetComponent<ItemDrop>()?.m_itemData?.m_shared?.m_buildPieces;
	}

	private static BepInPlugin SourceOf(GameObject prefab, Dictionary<string, BepInPlugin> jotunnPieces)
	{
		try
		{
			IModPrefab prefab2 = ModQuery.GetPrefab(((Object)prefab).name);
			BepInPlugin val = ((prefab2 != null) ? prefab2.SourceMod : null);
			if (val != null)
			{
				return val;
			}
		}
		catch (Exception)
		{
		}
		if (!jotunnPieces.TryGetValue(((Object)prefab).name, out var value))
		{
			return null;
		}
		return value;
	}

	private static string Clean(string name)
	{
		return new string(name.Where((char c) => c != '=' && c != '\n' && c != '\t' && c != '\\' && c != '"' && c != '\'' && c != '[' && c != ']').ToArray()).Trim();
	}

	private static string TabFor(BepInPlugin mod)
	{
		string key = Clean(mod.Name);
		if (key.Length == 0)
		{
			key = Clean(mod.GUID);
		}
		if (!TabNames.TryGetValue(key, out var value))
		{
			value = ((BaseUnityPlugin)instance).Config.Bind<string>("2 - Tabs", key, Spaced(key), "Tab for " + mod.Name + "'s hammer pieces (" + mod.GUID + "). Mods with the same tab name share it; empty = leave its pieces where the mod put them.");
			TabNames[key] = value;
		}
		if ((from part in leaveAlone.Value.Split(',')
			select part.Trim() into part
			where part.Length > 0
			select part).Any((string part) => part.Equals(key, StringComparison.OrdinalIgnoreCase) || part.Equals(mod.GUID, StringComparison.OrdinalIgnoreCase)))
		{
			return null;
		}
		string text = value.Value.Trim();
		if (text == key)
		{
			text = Spaced(key);
		}
		if (text.Length <= 0)
		{
			return null;
		}
		return text;
	}

	private static string Spaced(string name)
	{
		if (name.Contains(" "))
		{
			return name;
		}
		StringBuilder stringBuilder = new StringBuilder();
		for (int i = 0; i < name.Length; i++)
		{
			char c = name[i];
			if (c == '_' || c == '-')
			{
				stringBuilder.Append(' ');
				continue;
			}
			if (i > 0 && char.IsUpper(c) && (char.IsLower(name[i - 1]) || (i + 1 < name.Length && char.IsLower(name[i + 1]) && char.IsUpper(name[i - 1]))))
			{
				stringBuilder.Append(' ');
			}
			stringBuilder.Append(c);
		}
		return stringBuilder.ToString().Trim();
	}

	private static PieceCategory Category(string tab)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_002b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		//IL_0040: Unknown result type (might be due to invalid IL or missing references)
		//IL_003e: Unknown result type (might be due to invalid IL or missing references)
		PieceCategory result = PieceManager.Instance.AddPieceCategory(tab);
		if (Listed(tab))
		{
			return result;
		}
		PieceCategory result2 = PieceManager.Instance.AddPieceCategory(tab + "\u200b");
		if (!Listed(tab + "\u200b"))
		{
			return result;
		}
		return result2;
	}

	private static bool Listed(string name)
	{
		try
		{
			return !(Traverse.Create((object)PieceManager.Instance).Field("PieceCategories").GetValue() is Dictionary<string, PieceCategory> dictionary) || dictionary.ContainsKey(name);
		}
		catch (Exception)
		{
			return true;
		}
	}

	private static Dictionary<string, string> Extras()
	{
		Dictionary<string, string> dictionary = new Dictionary<string, string>();
		string[] array = extraPieces.Value.Split(',');
		foreach (string text in array)
		{
			int num = text.IndexOf('=');
			if (num > 0)
			{
				string text2 = text.Substring(0, num).Trim();
				string text3 = text.Substring(num + 1).Trim();
				if (text2.Length > 0 && text3.Length > 0)
				{
					dictionary[text2] = text3;
				}
			}
		}
		return dictionary;
	}

	internal static void Sort(PieceTable table)
	{
		//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
		//IL_0102: Unknown result type (might be due to invalid IL or missing references)
		//IL_0103: Unknown result type (might be due to invalid IL or missing references)
		//IL_0185: Unknown result type (might be due to invalid IL or missing references)
		//IL_018a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0192: Unknown result type (might be due to invalid IL or missing references)
		//IL_016f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0171: Unknown result type (might be due to invalid IL or missing references)
		//IL_0178: Unknown result type (might be due to invalid IL or missing references)
		//IL_017a: Unknown result type (might be due to invalid IL or missing references)
		Dictionary<string, BepInPlugin> dictionary = new Dictionary<string, BepInPlugin>();
		try
		{
			foreach (CustomPiece piece in ModRegistry.GetPieces())
			{
				if (Object.op_Implicit((Object)(object)((piece != null) ? piece.PiecePrefab : null)) && ((CustomEntity)piece).SourceMod != null)
				{
					dictionary[((Object)piece.PiecePrefab).name] = ((CustomEntity)piece).SourceMod;
				}
			}
		}
		catch (Exception)
		{
		}
		Dictionary<string, string> dictionary2 = Extras();
		SortedDictionary<string, int> sortedDictionary = new SortedDictionary<string, int>();
		List<string> list = new List<string>();
		foreach (GameObject piece2 in table.m_pieces)
		{
			Piece val = (Object.op_Implicit((Object)(object)piece2) ? piece2.GetComponent<Piece>() : null);
			if (!Object.op_Implicit((Object)(object)val))
			{
				continue;
			}
			if (!Original.TryGetValue(val, out var value))
			{
				value = (Original[val] = val.m_category);
			}
			if (!OriginalUsage.TryGetValue(val, out var value2))
			{
				value2 = (OriginalUsage[val] = val.m_usage);
			}
			string text = null;
			if (sortOn.Value)
			{
				if (dictionary2.TryGetValue(((Object)piece2).name, out var value3))
				{
					text = value3;
				}
				else
				{
					BepInPlugin val2 = SourceOf(piece2, dictionary);
					if (val2 == null)
					{
						list.Add(((Object)piece2).name);
					}
					else if (val2.GUID != "com.jotunn.jotunn")
					{
						text = TabFor(val2);
					}
				}
			}
			if (text == null)
			{
				val.m_category = value;
				val.m_usage = value2;
				continue;
			}
			val.m_category = Category(text);
			val.m_usage = (UsageTagFlags)0;
			sortedDictionary.TryGetValue(text, out var value4);
			sortedDictionary[text] = value4 + 1;
		}
		string text2 = ((sortedDictionary.Count == 0) ? "no mod pieces moved" : string.Join(", ", sortedDictionary.Select((KeyValuePair<string, int> pair) => $"{pair.Key} ({pair.Value})")));
		if (text2 != lastSummary)
		{
			lastSummary = text2;
			Log.LogInfo((object)("Hammer tabs: " + text2));
			Log.LogInfo((object)("Pieces from the game or from a mod that can't be told, left where they were (use \"Extra pieces\" for a mod's): " + string.Join(", ", list)));
		}
	}
}