Decompiled source of PUPPlankCompatibility v0.1.0

plugins/PUPPlankCompatibility/PUPPlankCompatibility.dll

Decompiled 3 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security.Cryptography;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("PUPPlankCompatibility")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0")]
[assembly: AssemblyProduct("PUPPlankCompatibility")]
[assembly: AssemblyTitle("PUPPlankCompatibility")]
[assembly: AssemblyVersion("0.1.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 Vercadi.PUPPlankCompatibility
{
	[BepInPlugin("vercadi.pupplankcompatibility", "PUP Plank Compatibility", "0.1.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInIncompatibility("vercadi.test.pupplankexclusion")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string Id = "vercadi.pupplankcompatibility";

		public const string Version = "0.1.0";

		private const string PupId = "com.pup.instancecombiner.safe";

		private static ManualLogSource log;

		private static int reports;

		private Harmony harmony;

		private void Awake()
		{
			//IL_0112: Unknown result type (might be due to invalid IL or missing references)
			//IL_011c: Expected O, but got Unknown
			//IL_0134: Unknown result type (might be due to invalid IL or missing references)
			//IL_0141: Expected O, but got Unknown
			log = ((BaseUnityPlugin)this).Logger;
			try
			{
				PluginInfo val = Chainloader.PluginInfos["com.pup.instancecombiner.safe"];
				Type type = ((object)val.Instance).GetType();
				string sha;
				using (FileStream inputStream = File.OpenRead(type.Assembly.Location))
				{
					using SHA256 sHA = SHA256.Create();
					sha = BitConverter.ToString(sHA.ComputeHash(inputStream)).Replace("-", "");
				}
				if (!ReviewedPup.IsSupported(val.Metadata.Version.ToString(), sha))
				{
					throw new NotSupportedException("Requires the original PUP FPS 1.0.32 or 1.0.33 DLL. Installed version: " + val.Metadata.Version?.ToString() + ". No patch applied.");
				}
				MethodInfo method = type.GetMethod("CollectTargets", BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[1] { typeof(Vector2Int) }, null);
				if (method == null || method.ReturnType != typeof(List<(ZNetView, Piece)>))
				{
					throw new MissingMethodException("Reviewed CollectTargets signature not found.");
				}
				harmony = new Harmony("vercadi.pupplankcompatibility");
				harmony.Patch((MethodBase)method, (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "AfterCollect", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				((BaseUnityPlugin)this).Logger.LogInfo((object)("ACTIVE with PUP FPS " + val.Metadata.Version?.ToString() + ": leaves mm_corewood_planks out of new bake selections. The planks remain ordinary building pieces."));
			}
			catch (Exception ex)
			{
				Harmony obj = harmony;
				if (obj != null)
				{
					obj.UnpatchSelf();
				}
				((BaseUnityPlugin)this).Logger.LogWarning((object)("PUP Plank Compatibility disabled: " + ex.Message));
			}
		}

		private static void AfterCollect(ref List<(ZNetView view, Piece piece)> __result)
		{
			if (__result == null)
			{
				return;
			}
			List<(ZNetView, Piece)> list = __result;
			try
			{
				List<(ZNetView, Piece)> list2 = (__result = Selection.Filter<(ZNetView, Piece)>(list, ((ZNetView view, Piece piece) entry) => ((Object)(object)entry.view != (Object)null && entry.view.GetZDO() != null) ? entry.view.GetZDO().GetPrefab() : 0, delegate((ZNetView view, Piece piece) entry)
				{
					if (!((Object)(object)ZNetScene.instance != (Object)null) || !((Object)(object)entry.view != (Object)null) || entry.view.GetZDO() == null)
					{
						return (string)null;
					}
					GameObject prefab = ZNetScene.instance.GetPrefab(entry.view.GetZDO().GetPrefab());
					return (prefab == null) ? null : ((Object)prefab).name;
				}));
				if (list2.Count != list.Count && reports++ < 32)
				{
					try
					{
						log.LogInfo((object)$"Left {list.Count - list2.Count} mm_corewood_planks unchanged; {list2.Count} pieces remain in the bake selection.");
						return;
					}
					catch
					{
						return;
					}
				}
			}
			catch (Exception ex)
			{
				__result = list;
				if (reports++ < 32)
				{
					try
					{
						log.LogWarning((object)("Selection left unchanged after inspection failed: " + ex.GetType().Name));
						return;
					}
					catch
					{
						return;
					}
				}
			}
		}

		private void OnDestroy()
		{
			Harmony obj = harmony;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
		}
	}
	public static class ReviewedPup
	{
		public const string Versions = "1.0.32 or 1.0.33";

		public static bool IsSupported(string version, string sha256)
		{
			if (!(version == "1.0.32") || !string.Equals(sha256, "8093FBB057C155623934DC2B9BE9CE92EB4C6A3ABD3A60D63F2E8E71744858F1", StringComparison.OrdinalIgnoreCase))
			{
				if (version == "1.0.33")
				{
					return string.Equals(sha256, "6058533BEB2864F462F5236CEE17D00072832F4166FAF2F07FC1FD47445AECCA", StringComparison.OrdinalIgnoreCase);
				}
				return false;
			}
			return true;
		}
	}
	public static class Selection
	{
		public static List<T> Filter<T>(List<T> source, Func<T, int> getHash, Func<T, string> getPrefabName)
		{
			if (source == null || source.Count == 0)
			{
				return source;
			}
			List<T> list = new List<T>(source.Count);
			foreach (T item in source)
			{
				if (getHash(item) != 1872747639 || !string.Equals(getPrefabName(item), "mm_corewood_planks", StringComparison.Ordinal))
				{
					list.Add(item);
				}
			}
			if (list.Count != source.Count)
			{
				return list;
			}
			return source;
		}
	}
}