Decompiled source of LetMeTameYouServerFix v1.0.1

LetMeTameYouServerFix.dll

Decompiled 17 hours ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
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(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("LetMeTameYouServerFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1")]
[assembly: AssemblyProduct("LetMeTameYouServerFix")]
[assembly: AssemblyTitle("LetMeTameYouServerFix")]
[assembly: AssemblyVersion("1.0.1.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 LetMeTameYouServerFix
{
	[BepInPlugin("qua8ion.valheim.letmetameyouserverfix", "LetMeTameYouServerFix", "1.0.1")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class LetMeTameYouServerFixPlugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(ObjectDB), "Awake")]
		private static class ObjectDBAwakePatch
		{
			[HarmonyPriority(0)]
			[HarmonyAfter(new string[] { "meldurson.LetMeTameYou" })]
			private static void Postfix(ObjectDB __instance)
			{
				if ((Object)(object)ZNet.instance == (Object)null || !ZNet.instance.IsDedicated() || (Object)(object)((Component)__instance).GetComponent<ZNetScene>() == (Object)null)
				{
					return;
				}
				if ((bool)hasInitTames.GetValue(null))
				{
					log.LogInfo((object)"LetMeTameYou has already set up tames on this server, nothing to do");
					return;
				}
				try
				{
					updatesFromServer.Invoke(null, null);
				}
				catch (Exception e)
				{
					log.LogError((object)$"Setting up tames on the server failed: {Unwrap(e)}");
					return;
				}
				if (itemReg != null)
				{
					try
					{
						itemReg.Invoke(null, null);
					}
					catch (Exception e2)
					{
						log.LogWarning((object)$"Taming tool is not registered on the server: {Unwrap(e2)}");
					}
				}
				Report();
			}
		}

		public const string PluginGuid = "qua8ion.valheim.letmetameyouserverfix";

		public const string PluginName = "LetMeTameYouServerFix";

		public const string PluginVersion = "1.0.1";

		private const string LetMeTameYouGuid = "meldurson.LetMeTameYou";

		private static ManualLogSource log;

		private static MethodInfo updatesFromServer;

		private static MethodInfo itemReg;

		private static FieldInfo hasInitTames;

		private static FieldInfo tamesList;

		private void Awake()
		{
			//IL_009c: Unknown result type (might be due to invalid IL or missing references)
			log = ((BaseUnityPlugin)this).Logger;
			updatesFromServer = AccessTools.Method("LetMeTameYou.TameManager:UpdatesFromServer", (Type[])null, (Type[])null);
			itemReg = AccessTools.Method("LetMeTameYou.PrefabManager:ItemReg", (Type[])null, (Type[])null);
			hasInitTames = AccessTools.Field(AccessTools.TypeByName("LetMeTameYou.TameManager"), "hasInitTames");
			tamesList = AccessTools.Field(AccessTools.TypeByName("LetMeTameYou.LetMeTameYou"), "customTamesList");
			if (updatesFromServer == null || hasInitTames == null || tamesList == null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"LetMeTameYou has changed: TameManager.UpdatesFromServer, TameManager.hasInitTames or LetMeTameYou.customTamesList is missing. Tames are not set up on the server.");
				return;
			}
			new Harmony("qua8ion.valheim.letmetameyouserverfix").PatchAll(typeof(LetMeTameYouServerFixPlugin).Assembly);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Tames from LetMeTameYou are set up on a dedicated server too");
		}

		private static Exception Unwrap(Exception e)
		{
			if (!(e is TargetInvocationException { InnerException: not null } ex))
			{
				return e;
			}
			return ex.InnerException;
		}

		private static void Report()
		{
			ZNetScene instance = ZNetScene.instance;
			List<string> list = new List<string>();
			foreach (object key in ((IDictionary)tamesList.GetValue(null)).Keys)
			{
				string text = key as string;
				GameObject prefab = instance.GetPrefab(text);
				if (!((Object)(object)prefab == (Object)null) && !((Object)(object)prefab.GetComponent<Tameable>() == (Object)null))
				{
					Procreation component = prefab.GetComponent<Procreation>();
					GameObject val = (((Object)(object)component != (Object)null) ? component.m_offspring : null);
					if ((Object)(object)val == (Object)null)
					{
						list.Add(text);
					}
					else if ((Object)(object)instance.GetPrefab(((Object)val).name) == (Object)null)
					{
						list.Add(text + " (offspring " + ((Object)val).name + " NOT registered)");
						log.LogWarning((object)(text + ": offspring prefab " + ((Object)val).name + " is not registered, the server will delete newborns"));
					}
					else
					{
						list.Add(text + " (offspring " + ((Object)val).name + ")");
					}
				}
			}
			log.LogInfo((object)("Tames set up on the server: " + ((list.Count > 0) ? string.Join(", ", list) : "none")));
		}
	}
}