Decompiled source of DrakkarShrinker v1.0.1

DrakkarShrinker.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
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(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("0.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 DrakkarShrinker
{
	[BepInPlugin("com.bcbry.valheim.drakkarshrinker", "Drakkar Shrinker", "1.0.0")]
	public sealed class DrakkarShrinkerPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.bcbry.valheim.drakkarshrinker";

		public const string PluginName = "Drakkar Shrinker";

		public const string PluginVersion = "1.0.0";

		internal static ConfigEntry<float> Scale;

		internal static ManualLogSource Log;

		private void Awake()
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Expected O, but got Unknown
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Expected O, but got Unknown
			//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f8: Expected O, but got Unknown
			Scale = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Scale", 0.65f, new ConfigDescription("Uniform scale for the Drakkar. Restart Valheim after changing this value.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.25f, 1f), Array.Empty<object>()));
			Log = ((BaseUnityPlugin)this).Logger;
			Harmony val = new Harmony("com.bcbry.valheim.drakkarshrinker");
			Type type = AccessTools.TypeByName("ZNetScene");
			if (type == null)
			{
				Log.LogWarning((object)"Could not find ZNetScene type; runtime patch skipped.");
				return;
			}
			MethodInfo methodInfo = AccessTools.Method(type, "Awake", (Type[])null, (Type[])null);
			if (methodInfo == null)
			{
				Log.LogWarning((object)"Could not find ZNetScene.Awake method; runtime patch skipped.");
				return;
			}
			MethodInfo method = typeof(ZNetSceneAwakePatch).GetMethod("Postfix", BindingFlags.Static | BindingFlags.NonPublic);
			if (method == null)
			{
				Log.LogWarning((object)"Could not find postfix method to patch ZNetScene.Awake.");
				return;
			}
			val.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(method), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			Log.LogInfo((object)"Patched ZNetScene.Awake via runtime reflection.");
		}
	}
	internal static class ZNetSceneAwakePatch
	{
		private const string DrakkarPrefabName = "VikingShip_Ashlands";

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

		private static void Postfix(object __instance)
		{
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			if (__instance == null)
			{
				return;
			}
			Type type = __instance.GetType();
			MethodInfo method = type.GetMethod("GetPrefab", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (method == null)
			{
				DrakkarShrinkerPlugin.Log.LogWarning((object)"ZNetScene does not contain GetPrefab; cannot resize Drakkar.");
				return;
			}
			object obj = method.Invoke(__instance, new object[1] { "VikingShip_Ashlands" });
			GameObject val = (GameObject)((obj is GameObject) ? obj : null);
			if ((Object)(object)val == (Object)null)
			{
				DrakkarShrinkerPlugin.Log.LogWarning((object)"Could not find the VikingShip_Ashlands prefab; no Drakkar was resized.");
				return;
			}
			int instanceID = ((Object)val).GetInstanceID();
			if (RescaledPrefabs.Add(instanceID))
			{
				float value = DrakkarShrinkerPlugin.Scale.Value;
				Transform transform = val.transform;
				transform.localScale *= value;
				DrakkarShrinkerPlugin.Log.LogInfo((object)$"Resized Drakkar to {value:P0} of its normal size.");
			}
		}
	}
}