Please disclose if any significant portion of your mod was created using AI tools by adding the 'AI Generated' category. Failing to do so may result in the mod being removed from Thunderstore.
Decompiled source of TamePerf v1.0.0
Cjayride.TamePerf.dll
Decompiled 11 hours agousing System; using System.Collections.Generic; using System.Diagnostics; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.Versioning; using System.Security; using System.Security.Permissions; using BepInEx; using BepInEx.Bootstrap; using BepInEx.Configuration; using HarmonyLib; using Microsoft.CodeAnalysis; using UnityEngine; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: IgnoresAccessChecksTo("assembly_valheim")] [assembly: TargetFramework(".NETFramework,Version=v4.8.1", FrameworkDisplayName = ".NET Framework 4.8.1")] [assembly: AssemblyCompany("Cjayride.TamePerf")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyFileVersion("1.0.0.0")] [assembly: AssemblyInformationalVersion("1.0.0+91e86fd5b297090233a881f332c7fdf3f95a6713")] [assembly: AssemblyProduct("Cjayride.TamePerf")] [assembly: AssemblyTitle("Cjayride.TamePerf")] [assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)] [assembly: AssemblyVersion("1.0.0.0")] [module: UnverifiableCode] [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 Cjayride.TamePerf { internal static class Census { private struct Entry { public Vector3 Pos; public bool ReadyForProcreation; public bool EventCreature; } private static readonly Dictionary<string, List<Entry>> ByCloneName = new Dictionary<string, List<Entry>>(); private static float _builtAt = -999f; private const float RebuildSeconds = 0.35f; internal static void Ensure() { //IL_00fb: Unknown result type (might be due to invalid IL or missing references) //IL_0100: Unknown result type (might be due to invalid IL or missing references) if (Time.time - _builtAt < 0.35f) { return; } _builtAt = Time.time; foreach (List<Entry> value2 in ByCloneName.Values) { value2.Clear(); } List<BaseAI> baseAIInstances = BaseAI.BaseAIInstances; if (baseAIInstances == null) { return; } for (int i = 0; i < baseAIInstances.Count; i++) { BaseAI val = baseAIInstances[i]; if (!((Object)(object)val == (Object)null)) { string name = ((Object)((Component)val).gameObject).name; if (!ByCloneName.TryGetValue(name, out var value)) { value = new List<Entry>(16); ByCloneName[name] = value; } bool readyForProcreation = false; Procreation component = ((Component)val).GetComponent<Procreation>(); if ((Object)(object)component != (Object)null) { readyForProcreation = component.ReadyForProcreation(); } bool eventCreature = false; MonsterAI val2 = (MonsterAI)(object)((val is MonsterAI) ? val : null); if ((Object)(object)val2 != (Object)null) { eventCreature = val2.IsEventCreature(); } value.Add(new Entry { Pos = ((Component)val).transform.position, ReadyForProcreation = readyForProcreation, EventCreature = eventCreature }); } } } internal static int Count(string cloneName, Vector3 center, float maxRange, bool eventCreaturesOnly, bool procreationOnly) { //IL_0034: Unknown result type (might be due to invalid IL or missing references) //IL_0036: Unknown result type (might be due to invalid IL or missing references) Ensure(); if (!ByCloneName.TryGetValue(cloneName, out var value) || value.Count == 0) { return 0; } int num = 0; for (int i = 0; i < value.Count; i++) { Entry entry = value[i]; if ((!(maxRange > 0f) || !(Vector3.Distance(center, entry.Pos) > maxRange)) && (!eventCreaturesOnly || entry.EventCreature) && (!procreationOnly || entry.ReadyForProcreation)) { num++; } } return num; } } internal static class IdleGate { private static readonly Dictionary<int, float> Acc = new Dictionary<int, float>(); internal static bool TryThrottle(BaseAI ai, ref float dt) { if (!ModConfig.Enabled.Value || !ModConfig.RestIdleTames.Value) { return false; } if (Plugin.IdleBeastsPresent && ModConfig.DeferRestToIdleBeasts.Value) { return false; } if (!IsIdleTame(ai, out var interval)) { Acc.Remove(((Object)ai).GetInstanceID()); return false; } int instanceID = ((Object)ai).GetInstanceID(); Acc.TryGetValue(instanceID, out var value); value += dt; if (value < interval) { Acc[instanceID] = value; return true; } dt = value; Acc[instanceID] = 0f; return false; } internal static bool ShouldMuteIdleSound(BaseAI ai) { //IL_0078: Unknown result type (might be due to invalid IL or missing references) if (!ModConfig.Enabled.Value || !ModConfig.ThrottleIdleSounds.Value || (Object)(object)ai == (Object)null) { return false; } Character character = ai.m_character; if ((Object)(object)character == (Object)null) { return false; } float metres = ((NameLooksLikeHorse(((Object)((Component)ai).gameObject).name) && ModConfig.MuteHorseIdleSoundsWhenIdle.Value && character.IsTamed()) ? ModConfig.WakeDistance.Value : ModConfig.IdleSoundPlayerDistance.Value); return !PlayerWithin(((Component)ai).transform.position, metres); } private static bool IsIdleTame(BaseAI ai, out float interval) { //IL_00e0: Unknown result type (might be due to invalid IL or missing references) //IL_00e5: Unknown result type (might be due to invalid IL or missing references) //IL_00e6: Unknown result type (might be due to invalid IL or missing references) //IL_011b: Unknown result type (might be due to invalid IL or missing references) interval = ModConfig.RestInterval.Value; if ((Object)(object)ai == (Object)null || (Object)(object)ai.m_nview == (Object)null || !ai.m_nview.IsValid() || !ai.m_nview.IsOwner()) { return false; } Character character = ai.m_character; if ((Object)(object)character == (Object)null || !character.IsTamed() || character.IsDead()) { return false; } if (ai.IsAlerted() || ai.m_timeSinceHurt < 10f) { return false; } if (character.IsSwimming() || character.IsFlying() || (Object)(object)character.GetStandingOnShip() != (Object)null) { return false; } Tameable tamable = ai.m_tamable; if ((Object)(object)tamable != (Object)null && (tamable.HaveRider() || tamable.IsHungry())) { return false; } MonsterAI val = (MonsterAI)(object)((ai is MonsterAI) ? ai : null); if ((Object)(object)val != (Object)null && (Object)(object)val.GetFollowTarget() != (Object)null) { return false; } Vector3 position = ((Component)ai).transform.position; float num = NearestPlayer(position); if (num < ModConfig.WakeDistance.Value) { return false; } bool num2 = num >= ModConfig.FarDistance.Value; bool flag = !ModConfig.RequireTrough.Value || Troughs.NearTrough(position, ModConfig.TroughRadius.Value); if (num2) { interval = Mathf.Max(ModConfig.FarInterval.Value, 0.05f); return true; } if (!flag) { return false; } interval = Mathf.Max(ModConfig.RestInterval.Value, 0.1f); return true; } private static bool NameLooksLikeHorse(string name) { if (string.IsNullOrEmpty(name)) { return false; } if (name.IndexOf("horse", StringComparison.OrdinalIgnoreCase) < 0 && name.IndexOf("OdinHorse", StringComparison.OrdinalIgnoreCase) < 0) { return name.IndexOf("M2Horse", StringComparison.OrdinalIgnoreCase) >= 0; } return true; } internal static bool PlayerWithin(Vector3 pos, float metres) { //IL_0000: Unknown result type (might be due to invalid IL or missing references) return NearestPlayer(pos) <= metres; } private static float NearestPlayer(Vector3 pos) { //IL_0032: Unknown result type (might be due to invalid IL or missing references) //IL_0039: Unknown result type (might be due to invalid IL or missing references) List<Player> allPlayers = Player.GetAllPlayers(); if (allPlayers == null || allPlayers.Count == 0) { return 99999f; } float num = 99999f; for (int i = 0; i < allPlayers.Count; i++) { Player val = allPlayers[i]; if (!((Object)(object)val == (Object)null)) { float num2 = Vector3.Distance(pos, ((Component)val).transform.position); if (num2 < num) { num = num2; } } } return num; } } internal static class ModConfig { internal static ConfigEntry<bool> Enabled; internal static ConfigEntry<bool> CheapCensus; internal static ConfigEntry<bool> RestIdleTames; internal static ConfigEntry<bool> DeferRestToIdleBeasts; internal static ConfigEntry<bool> RequireTrough; internal static ConfigEntry<string> TroughNameContains; internal static ConfigEntry<float> TroughRadius; internal static ConfigEntry<float> WakeDistance; internal static ConfigEntry<float> RestInterval; internal static ConfigEntry<float> FarDistance; internal static ConfigEntry<float> FarInterval; internal static ConfigEntry<bool> ThrottleIdleSounds; internal static ConfigEntry<float> IdleSoundPlayerDistance; internal static ConfigEntry<bool> MuteHorseIdleSoundsWhenIdle; internal static string[] TroughTokens = Array.Empty<string>(); internal static void Bind(ConfigFile config) { Enabled = config.Bind<bool>("General", "Enabled", true, "Master switch."); CheapCensus = config.Bind<bool>("Breeding", "CheapCensus", true, "Replace vanilla per-animal world scans during breeding with one shared count of loaded tames. Same caps and ranges; much cheaper on large herds."); RestIdleTames = config.Bind<bool>("Rest", "RestIdleTames", true, "Idle tamed animals think about once per RestInterval instead of every physics tick. Skipped when IdleBeasts is installed if DeferRestToIdleBeasts is true."); DeferRestToIdleBeasts = config.Bind<bool>("Rest", "DeferRestToIdleBeasts", true, "If Einherjer IdleBeasts is loaded, do not rest/throttle AI (IdleBeasts already does that). Census and idle-sound rules still run."); RequireTrough = config.Bind<bool>("Rest", "RequireTrough", true, "Only rest tames within TroughRadius of a piece whose name matches TroughNameContains (FeedingTrough). Far throttle still applies without a trough."); TroughNameContains = config.Bind<string>("Rest", "TroughNameContains", "trough,feed", "Comma-separated substrings matched against piece prefab names (case-insensitive)."); TroughRadius = config.Bind<float>("Rest", "TroughRadius", 24f, "Metres from a matching trough to count as 'in the ranch'."); WakeDistance = config.Bind<float>("Rest", "WakeDistance", 20f, "Tames rest only while every player is farther than this (metres)."); RestInterval = config.Bind<float>("Rest", "RestInterval", 1f, "Seconds between AI ticks while resting (vanilla is ~0.02)."); FarDistance = config.Bind<float>("Rest", "FarDistance", 50f, "Beyond this distance from every player, idle tames use FarInterval even without a trough."); FarInterval = config.Bind<float>("Rest", "FarInterval", 0.25f, "Seconds between AI ticks for far idle tames."); ThrottleIdleSounds = config.Bind<bool>("Audio", "ThrottleIdleSounds", true, "Skip vanilla idle creature SFX when no player is within IdleSoundPlayerDistance."); IdleSoundPlayerDistance = config.Bind<float>("Audio", "IdleSoundPlayerDistance", 24f, "Metres. Idle sounds play only if a player is this close."); MuteHorseIdleSoundsWhenIdle = config.Bind<bool>("Audio", "MuteHorseIdleSoundsWhenIdle", true, "Also skip idle SFX on tamed horses (OdinHorse / name contains horse) unless a player is within WakeDistance."); TroughNameContains.SettingChanged += delegate { RefreshTokens(); }; RefreshTokens(); } internal static void RefreshTokens() { TroughTokens = ((TroughNameContains != null) ? TroughNameContains.Value : "trough,feed").Split(new char[2] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < TroughTokens.Length; i++) { TroughTokens[i] = TroughTokens[i].Trim(); } } } [HarmonyPatch(typeof(SpawnSystem), "GetNrOfInstances", new Type[] { typeof(GameObject), typeof(Vector3), typeof(float), typeof(bool), typeof(bool) })] internal static class PatchCensus { private static bool Prefix(GameObject prefab, Vector3 center, float maxRange, bool eventCreaturesOnly, bool procreationOnly, ref int __result) { //IL_0045: Unknown result type (might be due to invalid IL or missing references) if (!ModConfig.Enabled.Value || !ModConfig.CheapCensus.Value || (Object)(object)prefab == (Object)null) { return true; } if ((Object)(object)prefab.GetComponent<BaseAI>() == (Object)null) { return true; } __result = Census.Count(((Object)prefab).name + "(Clone)", center, maxRange, eventCreaturesOnly, procreationOnly); return false; } } [HarmonyPatch(typeof(BaseAI), "UpdateAI")] internal static class PatchUpdateAI { private static bool Prefix(BaseAI __instance, ref float dt, ref bool __result) { if (!IdleGate.TryThrottle(__instance, ref dt)) { return true; } __result = false; return false; } } [HarmonyPatch(typeof(BaseAI), "DoIdleSound")] internal static class PatchIdleSound { private static bool Prefix(BaseAI __instance) { return !IdleGate.ShouldMuteIdleSound(__instance); } } [BepInPlugin("cjayride.tameperf", "TamePerf", "1.0.0")] public class Plugin : BaseUnityPlugin { public const string PluginGUID = "cjayride.tameperf"; public const string PluginName = "TamePerf"; public const string PluginVersion = "1.0.0"; internal const string IdleBeastsGuid = "anwo.IdleBeasts"; internal static Plugin Instance; internal static Harmony Harmony; internal static bool IdleBeastsPresent; private void Awake() { //IL_0034: Unknown result type (might be due to invalid IL or missing references) //IL_003e: Expected O, but got Unknown Instance = this; ModConfig.Bind(((BaseUnityPlugin)this).Config); IdleBeastsPresent = Chainloader.PluginInfos != null && Chainloader.PluginInfos.ContainsKey("anwo.IdleBeasts"); Harmony = new Harmony("cjayride.tameperf"); Harmony.PatchAll(); ((BaseUnityPlugin)this).Logger.LogInfo((object)("TamePerf 1.0.0 loaded." + (IdleBeastsPresent ? " IdleBeasts detected — AI rest deferred." : " AI rest enabled (no IdleBeasts)."))); } } internal static class Troughs { private static readonly List<Vector3> Positions = new List<Vector3>(32); private static float _builtAt = -999f; private const float RebuildSeconds = 2f; internal static void Ensure() { //IL_008b: Unknown result type (might be due to invalid IL or missing references) if (Time.time - _builtAt < 2f) { return; } _builtAt = Time.time; Positions.Clear(); List<Piece> s_allPieces = Piece.s_allPieces; if (s_allPieces == null) { return; } string[] troughTokens = ModConfig.TroughTokens; for (int i = 0; i < s_allPieces.Count; i++) { Piece val = s_allPieces[i]; if (!((Object)(object)val == (Object)null) && !((Object)(object)((Component)val).gameObject == (Object)null) && (Matches(PrefabName(((Component)val).gameObject), troughTokens) || Matches(((Object)((Component)val).gameObject).name, troughTokens))) { Positions.Add(((Component)val).transform.position); } } } internal static bool NearTrough(Vector3 pos, float radius) { //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_0018: Unknown result type (might be due to invalid IL or missing references) //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_001e: Unknown result type (might be due to invalid IL or missing references) Ensure(); float num = radius * radius; for (int i = 0; i < Positions.Count; i++) { Vector3 val = Positions[i] - pos; if (((Vector3)(ref val)).sqrMagnitude <= num) { return true; } } return false; } private static string PrefabName(GameObject go) { string name = ((Object)go).name; int num = name.IndexOf("(Clone)", StringComparison.Ordinal); if (num > 0) { return name.Substring(0, num); } return name; } private static bool Matches(string name, string[] tokens) { if (string.IsNullOrEmpty(name) || tokens == null) { return false; } foreach (string text in tokens) { if (text.Length > 0 && name.IndexOf(text, StringComparison.OrdinalIgnoreCase) >= 0) { return true; } } return false; } } } namespace System.Runtime.CompilerServices { [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] internal sealed class IgnoresAccessChecksToAttribute : Attribute { public IgnoresAccessChecksToAttribute(string assemblyName) { } } }