Decompiled source of TurretsGoBrrrrrr v0.2.3

ValheimTurretMod.dll

Decompiled a month 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 Microsoft.CodeAnalysis;
using ServerSync;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[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")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 Mods.Turrets
{
	[BepInPlugin("de.davidfvp.vhmods.turrets", "TurretsGoBrrrrrr", "0.2.3")]
	public class TurretMod : BaseUnityPlugin
	{
		private static ConfigEntry<bool> ServerConfigLocked;

		public static ConfigEntry<float> DamageModifier;

		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.2.3";

		private Harmony _harmony;

		private static readonly ConfigSync configSync = new ConfigSync("TurretsGoBrrrrrr")
		{
			CurrentVersion = "0.2.3",
			MinimumRequiredVersion = "0.2.3"
		};

		private static List<string> _keysBossesDefeated = new List<string> { "defeated_eikthyr", "defeated_elder", "defeated_bonemass", "defeated_dragon", "defeated_goblinking" };

		[UsedImplicitly]
		private void Awake()
		{
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Expected O, but got Unknown
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Expected O, but got Unknown
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Expected O, but got Unknown
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dd: Expected O, but got Unknown
			//IL_010b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0116: Expected O, but got Unknown
			//IL_0144: Unknown result type (might be due to invalid IL or missing references)
			//IL_014f: Expected O, but got Unknown
			//IL_017d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0188: Expected O, but got Unknown
			ServerConfigLocked = ConfigEntry("General", "Lock Configuration", value: true, new ConfigDescription("If on, only server admins can change the configuration.", (AcceptableValueBase)null, Array.Empty<object>()));
			configSync.AddLockingConfigEntry<bool>(ServerConfigLocked);
			DamageModifier = ConfigEntry("Damage", "DamageModifier", 0.5f, new ConfigDescription("Multiplier for turret damage output.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.5f, 10f), Array.Empty<object>()));
			AttackCooldown = ConfigEntry("Damage", "AttackCooldown", 0.5f, new ConfigDescription("Cooldown time in seconds between turret attacks.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.5f, 5f), Array.Empty<object>()));
			AttackWarmup = ConfigEntry("Damage", "AttackWarmup", 0.5f, new ConfigDescription("Time in seconds before the turret fires its first shot after locking onto a target.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.5f, 5f), Array.Empty<object>()));
			HorizontalAngle = ConfigEntry("Targeting", "HorizontalAngle", 80f, new ConfigDescription("Horizontal field of view (in degrees) for scanning targets.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(10f, 180f), Array.Empty<object>()));
			VerticalAngle = ConfigEntry("Targeting", "VerticalAngle", 35f, new ConfigDescription("Vertical field of view (in degrees) for scanning targets.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(10f, 90f), Array.Empty<object>()));
			ViewDistance = ConfigEntry("Targeting", "ViewDistance", 40f, new ConfigDescription("Maximum detection range (in meters) for targets.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(15f, 100f), Array.Empty<object>()));
			_harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "de.davidfvp.vhmods.turrets");
		}

		private ConfigEntry<T> ConfigEntry<T>(string group, string name, T value, ConfigDescription description, bool synchronizedSetting = true)
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Expected O, but got Unknown
			ConfigEntry<T> val = ((BaseUnityPlugin)this).Config.Bind<T>(group, name, value, new ConfigDescription(description.Description, description.AcceptableValues, Array.Empty<object>()));
			((OwnConfigEntryBase)configSync.AddConfigEntry<T>(val)).SynchronizedConfig = synchronizedSetting;
			return val;
		}

		[UsedImplicitly]
		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}

		public static int GetBossDefeatCount()
		{
			return _keysBossesDefeated.Count((string key) => ZoneSystem.instance.GetGlobalKey(key));
		}
	}
}
namespace Mods.Turrets.Patches
{
	[HarmonyPatch(typeof(Projectile))]
	public static class Projectile_Patch
	{
		[HarmonyPatch("Setup")]
		[HarmonyPrefix]
		private static bool Setup(Character owner, Vector3 velocity, float hitNoise, HitData hitData, ItemData item, ItemData ammo)
		{
			if ("$item_turretbolt".Equals(ammo?.m_shared?.m_name))
			{
				float num = (0.1f + (float)TurretMod.GetBossDefeatCount() * 0.2f) * TurretMod.DamageModifier.Value;
				((DamageTypes)(ref hitData.m_damage)).Modify(num);
			}
			return true;
		}
	}
	[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(typeof(ZNetScene))]
	public static class ZNetScene_Patch
	{
		[HarmonyPatch("Awake")]
		[HarmonyPostfix]
		private static void ChangePieceCosts(ZNetScene __instance)
		{
			List<string> prefabNames = __instance.GetPrefabNames();
			if (prefabNames != null)
			{
				ChangeTurretCosts(__instance, prefabNames);
			}
		}

		private static void ChangeTurretCosts(ZNetScene scene, List<string> prefabs)
		{
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: 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 = 10
						}
					};
				}
			}
		}
	}
}