Decompiled source of KALI Build Anywhere v0.1.0

BepInEx/plugins/KALI_Build_Anywhere/FinallyBuildAnywhere.dll

Decompiled 11 hours 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 BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("FinallyBuildAnywhere")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0")]
[assembly: AssemblyProduct("FinallyBuildAnywhere")]
[assembly: AssemblyTitle("FinallyBuildAnywhere")]
[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;
		}
	}
}
[BepInPlugin("local.finally.buildanywhere", "Finally Build Anywhere", "0.1.0")]
[BepInProcess("valheim_server.exe")]
public sealed class FinallyBuildAnywhere : BaseUnityPlugin
{
	[HarmonyPatch(typeof(ZNetScene), "Awake")]
	private static class RegisterTargets
	{
		private static void Postfix(ZNetScene __instance)
		{
			Targets.Clear();
			foreach (GameObject prefab in __instance.m_prefabs)
			{
				if ((Object)(object)prefab.GetComponent<LocationProxy>() != (Object)null || prefab.GetComponentsInChildren<Location>(true).Length != 0)
				{
					Targets.Add(StringExtensionMethods.GetStableHashCode(((Object)prefab).name));
				}
			}
			log.LogInfo((object)("Registered " + Targets.Count + " network prefab types containing landmark protection."));
		}
	}

	[HarmonyPatch(typeof(ZDO), "Serialize")]
	private static class NetworkScope
	{
		private static void Prefix(ZDO __instance, out ZDO __state)
		{
			__state = serializing;
			serializing = (IsTarget(__instance) ? __instance : null);
		}

		private static Exception Finalizer(Exception __exception, ZDO __state)
		{
			serializing = __state;
			return __exception;
		}
	}

	[HarmonyPatch(typeof(ZDOExtraData), "GetData")]
	private static class OutgoingFields
	{
		private static void Postfix(ZDOID zid, ref List<KeyValuePair<int, int>> ints)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			if (serializing != null && !(serializing.m_uid != zid))
			{
				ints = ints.Where((KeyValuePair<int, int> kv) => !FieldKeys.Contains(kv.Key)).ToList();
				ints.Add(new KeyValuePair<int, int>(FieldKeys[0], 1));
				ints.Add(new KeyValuePair<int, int>(FieldKeys[1], 1));
				ints.Add(new KeyValuePair<int, int>(FieldKeys[2], 0));
				ModifiedPackets++;
				if (ModifiedPackets == 1)
				{
					log.LogInfo((object)"First landmark building override serialized for a vanilla client.");
				}
			}
		}
	}

	public sealed class OriginalFields
	{
		public ZDO Zdo;

		public readonly int?[] Values = new int?[3];
	}

	[HarmonyPatch(typeof(ZDO), "Deserialize")]
	private static class IncomingFields
	{
		private static void Prefix(ZDO __instance, ZPackage pkg, out OriginalFields __state)
		{
			__state = null;
			if (!Enabled || (Object)(object)ZNet.instance == (Object)null || !ZNet.instance.IsServer())
			{
				return;
			}
			int pos = pkg.GetPos();
			int item;
			try
			{
				pkg.ReadUShort();
				item = pkg.ReadInt();
			}
			finally
			{
				pkg.SetPos(pos);
			}
			if (!Targets.Contains(item))
			{
				return;
			}
			__state = new OriginalFields
			{
				Zdo = __instance
			};
			int value = default(int);
			for (int i = 0; i < FieldKeys.Length; i++)
			{
				if (__instance.GetInt(FieldKeys[i], ref value))
				{
					__state.Values[i] = value;
				}
			}
		}

		private static Exception Finalizer(Exception __exception, OriginalFields __state)
		{
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			if (__state == null)
			{
				return __exception;
			}
			for (int i = 0; i < FieldKeys.Length; i++)
			{
				if (__state.Values[i].HasValue)
				{
					ZDOExtraData.Set(__state.Zdo.m_uid, FieldKeys[i], __state.Values[i].Value);
				}
				else
				{
					ZDOExtraData.RemoveInt(__state.Zdo.m_uid, FieldKeys[i]);
				}
			}
			RestoredEchoes++;
			return __exception;
		}
	}

	public const string Id = "local.finally.buildanywhere";

	public static readonly int[] FieldKeys = new int[3]
	{
		StringExtensionMethods.GetStableHashCode("HasFields"),
		StringExtensionMethods.GetStableHashCode("HasFieldsLocation"),
		StringExtensionMethods.GetStableHashCode("Location.m_noBuild")
	};

	private static readonly HashSet<int> Targets = new HashSet<int>();

	[ThreadStatic]
	private static ZDO serializing;

	public static bool Enabled;

	public static long ModifiedPackets;

	public static long RestoredEchoes;

	private Harmony harmony;

	private static ManualLogSource log;

	private void Awake()
	{
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		//IL_003d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0074: Unknown result type (might be due to invalid IL or missing references)
		//IL_007e: Expected O, but got Unknown
		log = ((BaseUnityPlugin)this).Logger;
		Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Disable landmark no-build circles for vanilla clients. Restart server and reconnect clients after changing. Does not change player wards or boss-combat restrictions.").Value;
		if (Enabled)
		{
			if (((object)Version.CurrentVersion/*cast due to .constrained prefix*/).ToString() != "1.0.20")
			{
				Enabled = false;
				((BaseUnityPlugin)this).Logger.LogWarning((object)"Disabled: this prototype was verified on Valheim 1.0.20. Review compatibility before enabling it on a new game version.");
			}
			else
			{
				harmony = new Harmony("local.finally.buildanywhere");
				harmony.PatchAll(typeof(FinallyBuildAnywhere).Assembly);
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Enabled: network-only landmark building overrides. World saves and original location objects are preserved.");
			}
		}
	}

	private void OnDestroy()
	{
		Harmony obj = harmony;
		if (obj != null)
		{
			obj.UnpatchSelf();
		}
		Enabled = false;
	}

	public static bool IsTarget(ZDO zdo)
	{
		if (Enabled && zdo != null)
		{
			return Targets.Contains(zdo.GetPrefab());
		}
		return false;
	}
}