Decompiled source of TurretsGoBrrrrrr v0.1.2

ValheimTurretMod.dll

Decompiled 4 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using JetBrains.Annotations;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("ValheimTurretMod")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ValheimTurretMod")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("4536e4c9-96dd-4a09-83fe-b7919bb1b2dc")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8.1", FrameworkDisplayName = ".NET Framework 4.8.1")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Mods.Turrets
{
	[BepInPlugin("de.davidfvp.vhmods.turrets", "TurretsGoBrrrrrr", "0.1.2")]
	public class TurretMod : BaseUnityPlugin
	{
		public static ConfigEntry<float> HorizontalAngle;

		public static ConfigEntry<float> VerticalAngle;

		public static ConfigEntry<float> ViewDistance;

		public static ConfigEntry<float> AttackCooldown;

		public static ConfigEntry<float> AttackWarmup;

		public static ConfigEntry<bool> AttackBosses;

		public const string PluginId = "de.davidfvp.vhmods.turrets";

		public const string DisplayName = "TurretsGoBrrrrrr";

		public const string Version = "0.1.2";

		private Harmony _harmony;

		[UsedImplicitly]
		private void Awake()
		{
			HorizontalAngle = ((BaseUnityPlugin)this).Config.Bind<float>("Look & Scan", "HorizontalAngle", 80f, "");
			VerticalAngle = ((BaseUnityPlugin)this).Config.Bind<float>("Look & Scan", "VerticalAngle", 35f, "");
			ViewDistance = ((BaseUnityPlugin)this).Config.Bind<float>("Look & Scan", "ViewDistance", 40f, "");
			AttackCooldown = ((BaseUnityPlugin)this).Config.Bind<float>("Look & Scan", "AttackCooldown", 0.5f, "");
			AttackWarmup = ((BaseUnityPlugin)this).Config.Bind<float>("Look & Scan", "AttackWarmup", 0.5f, "");
			AttackBosses = ((BaseUnityPlugin)this).Config.Bind<bool>("Look & Scan", "AttackBosses", false, "Should turrets attack bosses?");
			_harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "de.davidfvp.vhmods.turrets");
		}
	}
}
namespace Mods.Turrets.Patches
{
	[HarmonyPatch(typeof(Turret))]
	public static class Turret_Patch
	{
		[HarmonyPatch("Awake")]
		[HarmonyPostfix]
		private static void ChangeTargetBehaviour(Turret __instance, int ___m_maxAmmo, ZNetView ___m_nview)
		{
			if (!((Object)(object)__instance == (Object)null))
			{
				__instance.m_targetPlayers = false;
				__instance.m_targetTamed = false;
				__instance.m_targetEnemies = true;
				__instance.m_horizontalAngle = TurretMod.HorizontalAngle.Value;
				__instance.m_verticalAngle = TurretMod.VerticalAngle.Value;
				__instance.m_viewDistance = TurretMod.ViewDistance.Value;
				__instance.m_attackCooldown = TurretMod.AttackCooldown.Value;
				__instance.m_attackWarmup = TurretMod.AttackWarmup.Value;
				__instance.m_maxAmmo = 0;
				__instance.m_defaultAmmo = ZNetScene.instance.GetPrefab("TurretBolt").GetComponent<ItemDrop>();
			}
		}

		[HarmonyPatch("RPC_SetTarget")]
		[HarmonyPrefix]
		private static bool RPC_SetTarget(long sender, ref ZDOID character)
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Invalid comparison between Unknown and I4
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			if (TurretMod.AttackBosses.Value)
			{
				return true;
			}
			GameObject val = ZNetScene.instance.FindInstance(character);
			if (Object.op_Implicit((Object)(object)val))
			{
				Character component = val.GetComponent<Character>();
				if ((Object)(object)component != (Object)null && (int)component.GetFaction() == 8)
				{
					character = ZDOID.None;
				}
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(ObjectDB))]
	public static class ObjectDB_Patch
	{
		[HarmonyPatch("Awake")]
		[HarmonyPostfix]
		private static void ChangeRecipeCosts(ObjectDB __instance)
		{
		}

		private static void ChangeTurretBoltCosts(ObjectDB objectDb)
		{
			ChangeBoltCostToOneWood(objectDb, "Recipe_TurretBolt");
			ChangeBoltCostToOneWood(objectDb, "Recipe_TurretBoltWood");
		}

		private static void ChangeBoltCostToOneWood(ObjectDB objectDb, string recipe)
		{
			//IL_003b: 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_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Expected O, but got Unknown
			Recipe val = ((IEnumerable<Recipe>)objectDb.m_recipes).FirstOrDefault((Func<Recipe, bool>)((Recipe r) => recipe.Equals(((Object)r).name)));
			if ((Object)(object)val != (Object)null)
			{
				val.m_resources = (Requirement[])(object)new Requirement[1]
				{
					new Requirement
					{
						m_resItem = objectDb.GetItemPrefab("Wood").GetComponent<ItemDrop>(),
						m_amount = 1
					}
				};
				val.m_craftingStation = null;
				val.m_enabled = true;
				Debug.Log((object)("Turret bold recipe [" + recipe + "] modified to require only 1 wood."));
			}
		}
	}
	[HarmonyPatch(typeof(ZNetScene))]
	public static class ZNetScene_Patch
	{
		[HarmonyPatch("Awake")]
		[HarmonyPostfix]
		private static void ChangePieceCosts(ZNetScene __instance)
		{
			Debug.Log((object)$"GetPrefabNames: {__instance.GetPrefabNames().Count}");
			List<string> prefabNames = __instance.GetPrefabNames();
			if (prefabNames != null)
			{
				ChangeTurretCosts(__instance, prefabNames);
			}
		}

		private static void ChangeTurretCosts(ZNetScene scene, List<string> prefabs)
		{
			//IL_0039: 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)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Expected O, but got Unknown
			GameObject prefab = scene.GetPrefab("piece_turret");
			if (!((Object)(object)prefab == (Object)null))
			{
				Piece component = prefab.GetComponent<Piece>();
				if (!((Object)(object)component == (Object)null))
				{
					component.m_resources = (Requirement[])(object)new Requirement[1]
					{
						new Requirement
						{
							m_resItem = scene.GetPrefab("Wood").GetComponent<ItemDrop>(),
							m_amount = 1
						}
					};
				}
			}
		}
	}
}