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 WaypointTeleportsOdinHorse v2.0.0
WaypointTeleportsOdinHorse.dll
Decompiled 6 days agousing System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Versioning; using System.Security.Permissions; using BepInEx; using BepInEx.Bootstrap; using BepInEx.Configuration; using BepInEx.Logging; using HarmonyLib; using UnityEngine; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: AssemblyTitle("Waypoint Teleports OdinHorse")] [assembly: AssemblyDescription("Your following OdinHorse travels with you through RustyMods Waypoints.")] [assembly: AssemblyCompany("RDMods")] [assembly: AssemblyProduct("WaypointTeleportsOdinHorse")] [assembly: AssemblyCopyright("Copyright © RDMods 2026")] [assembly: ComVisible(false)] [assembly: AssemblyFileVersion("2.0.0.0")] [assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")] [assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)] [assembly: AssemblyVersion("2.0.0.0")] namespace RDMods.WaypointTeleportsOdinHorse; internal static class HorseTeleport { private const string HorsePrefabName = "rae_OdinHorse"; private static readonly int s_horsePrefabHash = StringExtensionMethods.GetStableHashCode("rae_OdinHorse"); private static readonly int s_odinHorseWaitingHash = StringExtensionMethods.GetStableHashCode("IsWaiting"); private const float SpreadRadius = 2.5f; private const float SpreadStepDegrees = 40f; private const float SettleTimeout = 30f; private const float SettlePollSeconds = 0.25f; private static readonly List<Character> s_chosen = new List<Character>(2); private static readonly List<ZDOID> s_settling = new List<ZDOID>(2); private static Character s_ridden; private static Coroutine s_settleRoutine; private static readonly WaitForSeconds s_settleWait = new WaitForSeconds(0.25f); internal static void Reset() { s_chosen.Clear(); s_ridden = null; } internal static bool BeforeTeleport() { //IL_0058: Unknown result type (might be due to invalid IL or missing references) //IL_005d: Unknown result type (might be due to invalid IL or missing references) //IL_00cc: Unknown result type (might be due to invalid IL or missing references) //IL_00d1: Unknown result type (might be due to invalid IL or missing references) //IL_00d2: Unknown result type (might be due to invalid IL or missing references) //IL_00d7: Unknown result type (might be due to invalid IL or missing references) s_chosen.Clear(); Plugin.ReloadConfigIfChanged(); if (!Plugin.Enabled.Value) { return false; } Player localPlayer = Player.m_localPlayer; if ((Object)(object)localPlayer == (Object)null || ((Character)localPlayer).IsDead() || ((Character)localPlayer).IsTeleporting() || localPlayer.m_teleportCooldown < 2f) { return false; } Character riddenHorse = GetRiddenHorse(localPlayer); Vector3 position = ((Component)localPlayer).transform.position; float num = Plugin.MaxDistance.Value * Plugin.MaxDistance.Value; GameObject gameObject = ((Component)localPlayer).gameObject; string playerName = localPlayer.GetPlayerName(); foreach (Character allCharacter in Character.GetAllCharacters()) { if ((Object)(object)riddenHorse != (Object)null && (Object)(object)allCharacter == (Object)(object)riddenHorse) { s_chosen.Add(allCharacter); } else if (IsTamedHorse(allCharacter)) { Vector3 val = ((Component)allCharacter).transform.position - position; if (!(((Vector3)(ref val)).sqrMagnitude > num) && IsFollowing(allCharacter, gameObject, playerName)) { s_chosen.Add(allCharacter); } } } if (s_chosen.Count == 0) { return false; } s_ridden = riddenHorse; return true; } private static bool IsFollowing(Character horse, GameObject playerObject, string playerName) { ZDO zDO = horse.m_nview.GetZDO(); if (zDO.GetBool(s_odinHorseWaitingHash, false)) { return false; } BaseAI baseAI = horse.m_baseAI; MonsterAI val = (MonsterAI)(object)((baseAI is MonsterAI) ? baseAI : null); if (val != null && (Object)(object)val.GetFollowTarget() == (Object)(object)playerObject) { return true; } return zDO.GetString(ZDOVars.s_follow, "") == playerName; } internal static void AfterTeleport(bool waypointResult) { //IL_004c: Unknown result type (might be due to invalid IL or missing references) //IL_0051: Unknown result type (might be due to invalid IL or missing references) //IL_0053: Unknown result type (might be due to invalid IL or missing references) //IL_0058: Unknown result type (might be due to invalid IL or missing references) //IL_0122: Unknown result type (might be due to invalid IL or missing references) //IL_00bd: 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) //IL_00c3: Unknown result type (might be due to invalid IL or missing references) //IL_00c8: Unknown result type (might be due to invalid IL or missing references) //IL_00cc: Unknown result type (might be due to invalid IL or missing references) //IL_00ce: Unknown result type (might be due to invalid IL or missing references) //IL_00df: Unknown result type (might be due to invalid IL or missing references) //IL_00e8: Unknown result type (might be due to invalid IL or missing references) //IL_00f5: Unknown result type (might be due to invalid IL or missing references) Player localPlayer = Player.m_localPlayer; if (!waypointResult || (Object)(object)localPlayer == (Object)null || !((Character)localPlayer).IsTeleporting()) { Plugin.Debug("Waypoint teleport did not start; horses stay."); Reset(); return; } if ((Object)(object)s_ridden != (Object)null && ((Character)localPlayer).IsRiding()) { localPlayer.StopDoodadControl(); } s_ridden = null; Vector3 teleportTargetPos = localPlayer.m_teleportTargetPos; Quaternion teleportTargetRot = localPlayer.m_teleportTargetRot; bool flag = false; int count = s_chosen.Count; for (int i = 0; i < count; i++) { Character val = s_chosen[i]; if (!((Object)(object)val == (Object)null) && !val.IsDead()) { ZNetView nview = val.m_nview; if (!((Object)(object)nview == (Object)null) && nview.IsValid()) { flag |= UnhitchCart(val); nview.ClaimOwnership(); Vector3 position = SpreadPoint(teleportTargetPos, teleportTargetRot, i, count); MoveInstance(val, position, teleportTargetRot); ZDO zDO = nview.GetZDO(); zDO.SetPosition(position); zDO.SetRotation(teleportTargetRot); s_settling.Add(zDO.m_uid); } } } Plugin.Debug($"Waypoint teleport: moved {s_settling.Count} horse(s) to {teleportTargetPos}."); s_chosen.Clear(); if (flag) { ((Character)localPlayer).Message((MessageType)2, Localization.instance.Localize("$wtoh_cart_left"), 0, (Sprite)null, false); } if (s_settling.Count != 0) { if (s_settleRoutine != null) { ((MonoBehaviour)Plugin.Instance).StopCoroutine(s_settleRoutine); } s_settleRoutine = ((MonoBehaviour)Plugin.Instance).StartCoroutine(SettleAfterLanding(localPlayer)); } } internal static void RemoveChosenHorses(List<Character> tames) { if (s_chosen.Count == 0) { return; } for (int num = tames.Count - 1; num >= 0; num--) { if (s_chosen.Contains(tames[num])) { tames.RemoveAt(num); } } } private static IEnumerator SettleAfterLanding(Player player) { float deadline = Time.time + 30f; while ((Object)(object)player != (Object)null && ((Character)player).IsTeleporting() && Time.time < deadline) { yield return s_settleWait; } deadline = Time.time + 30f; while (s_settling.Count > 0 && Time.time < deadline && (Object)(object)ZNetScene.instance != (Object)null) { for (int num = s_settling.Count - 1; num >= 0; num--) { GameObject val = ZNetScene.instance.FindInstance(s_settling[num]); if (!((Object)(object)val == (Object)null) && ZNetScene.instance.IsAreaReady(val.transform.position)) { PlaceOnGround(val); s_settling.RemoveAt(num); } } if (s_settling.Count > 0) { yield return s_settleWait; } } if (s_settling.Count > 0) { Plugin.Debug($"{s_settling.Count} horse(s) had not loaded after {30f} s; the game's own " + "underground check will catch them."); } s_settling.Clear(); s_settleRoutine = null; } private static void PlaceOnGround(GameObject go) { //IL_0032: Unknown result type (might be due to invalid IL or missing references) //IL_0037: 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_0047: Unknown result type (might be due to invalid IL or missing references) //IL_004d: Unknown result type (might be due to invalid IL or missing references) //IL_0059: Unknown result type (might be due to invalid IL or missing references) //IL_0064: Unknown result type (might be due to invalid IL or missing references) //IL_0076: Unknown result type (might be due to invalid IL or missing references) //IL_00a7: Unknown result type (might be due to invalid IL or missing references) //IL_00ae: Unknown result type (might be due to invalid IL or missing references) Character component = go.GetComponent<Character>(); if (!((Object)(object)component == (Object)null) && !((Object)(object)component.m_nview == (Object)null) && component.m_nview.IsValid()) { Vector3 position = go.transform.position; ZoneSystem instance = ZoneSystem.instance; float groundHeight = instance.GetGroundHeight(position); Vector3 val = default(Vector3); ((Vector3)(ref val))..ctor(position.x, Mathf.Max(position.y, groundHeight), position.z); float num2 = default(float); float num = (instance.GetSolidHeight(val, ref num2, 1) ? num2 : groundHeight); if (!(Mathf.Abs(position.y - num) < 0.3f)) { position.y = num + 0.2f; component.m_nview.ClaimOwnership(); MoveInstance(component, position, go.transform.rotation); } } } private static void MoveInstance(Character horse, Vector3 position, Quaternion rotation) { //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0007: 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) //IL_0025: Unknown result type (might be due to invalid IL or missing references) //IL_002c: Unknown result type (might be due to invalid IL or missing references) //IL_0037: Unknown result type (might be due to invalid IL or missing references) ((Component)horse).transform.SetPositionAndRotation(position, rotation); Rigidbody body = horse.m_body; if ((Object)(object)body != (Object)null) { body.position = position; body.rotation = rotation; body.linearVelocity = Vector3.zero; body.angularVelocity = Vector3.zero; } } private static Vector3 SpreadPoint(Vector3 landing, Quaternion facing, int index, int count) { //IL_001a: Unknown result type (might be due to invalid IL or missing references) //IL_0026: Unknown result type (might be due to invalid IL or missing references) //IL_002b: Unknown result type (might be due to invalid IL or missing references) //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_003f: 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_0051: Unknown result type (might be due to invalid IL or missing references) //IL_0052: Unknown result type (might be due to invalid IL or missing references) //IL_0053: Unknown result type (might be due to invalid IL or missing references) float num = 180f + ((float)index - (float)(count - 1) * 0.5f) * 40f; Vector3 val = facing * (Quaternion.Euler(0f, num, 0f) * Vector3.forward) * 2.5f; val.y = 0f; return landing + val; } private static bool UnhitchCart(Character horse) { bool result = false; GameObject gameObject = ((Component)horse).gameObject; foreach (Vagon instance in Vagon.m_instances) { ConfigurableJoint val = (((Object)(object)instance != (Object)null) ? instance.m_attachJoin : null); if (!((Object)(object)val == (Object)null) && !((Object)(object)((Joint)val).connectedBody == (Object)null) && !((Object)(object)((Component)((Joint)val).connectedBody).gameObject != (Object)(object)gameObject)) { instance.Detach(); result = true; } } return result; } private static Character GetRiddenHorse(Player player) { if (((Character)player).IsRiding()) { IDoodadController doodadController = player.m_doodadController; Sadle val = (Sadle)(object)((doodadController is Sadle) ? doodadController : null); if (val != null) { Character character = val.GetCharacter(); if (!IsTamedHorse(character)) { return null; } return character; } } return null; } private static bool IsTamedHorse(Character character) { if ((Object)(object)character == (Object)null || character.IsDead() || !character.IsTamed()) { return false; } ZNetView nview = character.m_nview; if ((Object)(object)nview != (Object)null && nview.IsValid()) { return nview.GetZDO().GetPrefab() == s_horsePrefabHash; } return false; } } [BepInPlugin("RDMods.WaypointTeleportsOdinHorse", "WaypointTeleportsOdinHorse", "2.0.0")] [BepInDependency(/*Could not decode attribute arguments.*/)] [BepInDependency(/*Could not decode attribute arguments.*/)] public sealed class Plugin : BaseUnityPlugin { public const string PluginGUID = "RDMods.WaypointTeleportsOdinHorse"; public const string PluginName = "WaypointTeleportsOdinHorse"; public const string PluginVersion = "2.0.0"; public const string OdinHorseGUID = "Raelaziel.OdinHorse"; internal static Plugin Instance; internal static ManualLogSource Log; internal static ConfigEntry<bool> Enabled; internal static ConfigEntry<float> MaxDistance; internal static ConfigEntry<bool> DebugLogging; private readonly Harmony _harmony = new Harmony("RDMods.WaypointTeleportsOdinHorse"); private FileSystemWatcher _watcher; private static volatile bool s_configChanged; private void Awake() { //IL_0017: Unknown result type (might be due to invalid IL or missing references) //IL_001d: Invalid comparison between Unknown and I4 Instance = this; Log = ((BaseUnityPlugin)this).Logger; BindConfig(); if ((int)SystemInfo.graphicsDeviceType == 4) { Log.LogInfo((object)"Headless server detected: nothing to do here."); } else if (WaypointsHooks.Apply(_harmony)) { Translations.Apply(_harmony); WatchConfigFile(); } } private void OnDestroy() { _watcher?.Dispose(); _harmony.UnpatchSelf(); } private void BindConfig() { //IL_004e: Unknown result type (might be due to invalid IL or missing references) //IL_0058: Expected O, but got Unknown Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - General", "Enabled", true, "Your tamed OdinHorse travels with you when you teleport with a Waypoints shrine."); MaxDistance = ((BaseUnityPlugin)this).Config.Bind<float>("1 - General", "Max Distance", 20f, new ConfigDescription("How close a following horse must be to come along, in metres.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(5f, 50f), Array.Empty<object>())); DebugLogging = ((BaseUnityPlugin)this).Config.Bind<bool>("9 - Debug", "Debug Logging", false, "Writes one log line per teleport about which horses were moved. Leave off unless reporting a bug."); } private void WatchConfigFile() { string configFilePath = ((BaseUnityPlugin)this).Config.ConfigFilePath; _watcher = new FileSystemWatcher(Path.GetDirectoryName(configFilePath), Path.GetFileName(configFilePath)) { NotifyFilter = (NotifyFilters.FileName | NotifyFilters.Size | NotifyFilters.LastWrite), IncludeSubdirectories = false }; _watcher.Changed += delegate { s_configChanged = true; }; _watcher.Created += delegate { s_configChanged = true; }; _watcher.Renamed += delegate { s_configChanged = true; }; _watcher.EnableRaisingEvents = true; } internal static void ReloadConfigIfChanged() { if (!s_configChanged || (Object)(object)Instance == (Object)null) { return; } s_configChanged = false; try { ((BaseUnityPlugin)Instance).Config.Reload(); } catch (Exception ex) { s_configChanged = true; Log.LogWarning((object)("Could not reload the config, keeping the current values: " + ex.Message)); } } internal static void Debug(string message) { if (DebugLogging.Value) { Log.LogInfo((object)message); } } } internal static class Translations { private static readonly KeyValuePair<string, string>[] English = new KeyValuePair<string, string>[1] { new KeyValuePair<string, string>("wtoh_cart_left", "Your cart was unhitched and left behind.") }; internal static void Apply(Harmony harmony) { //IL_0028: Unknown result type (might be due to invalid IL or missing references) //IL_0035: Expected O, but got Unknown try { harmony.Patch((MethodBase)AccessTools.Method(typeof(Localization), "SetupLanguage", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(Translations), "SetupLanguagePostfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null); } catch (Exception ex) { Plugin.Log.LogWarning((object)("Could not hook the game's language setup, messages will show as tokens: " + ex.Message)); } if (Localization.m_instance != null) { AddWords(Localization.m_instance, Localization.m_instance.GetSelectedLanguage()); } } private static void SetupLanguagePostfix(Localization __instance, string language) { try { AddWords(__instance, language); } catch (Exception ex) { Plugin.Log.LogWarning((object)("Could not add translations: " + ex.Message)); } } private static void AddWords(Localization localization, string language) { KeyValuePair<string, string>[] english = English; for (int i = 0; i < english.Length; i++) { KeyValuePair<string, string> keyValuePair = english[i]; localization.AddWord(keyValuePair.Key, keyValuePair.Value); } if (string.IsNullOrEmpty(language) || language == "English") { return; } string path = Path.Combine(Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)Plugin.Instance).Info.Location) ?? "", "Translations"), language + ".txt"); if (!File.Exists(path)) { return; } string[] array = File.ReadAllLines(path); foreach (string text in array) { int num = text.IndexOf('='); if (num > 0 && !text.TrimStart(Array.Empty<char>()).StartsWith("#")) { string text2 = text.Substring(0, num).Trim(); string text3 = text.Substring(num + 1).Trim(); if (text2.Length > 0 && text3.Length > 0) { localization.AddWord(text2, text3); } } } } } internal static class WaypointsHooks { internal const string WaypointsGUID = "RustyMods.Waypoints"; private static readonly string[] WaypointTypeNames = new string[2] { "Waypoints.Waypoint", "Waypoints.Behaviors.Waypoint" }; internal static bool Apply(Harmony harmony) { //IL_00a4: Unknown result type (might be due to invalid IL or missing references) //IL_00b9: Unknown result type (might be due to invalid IL or missing references) //IL_00c6: Expected O, but got Unknown //IL_00c6: Expected O, but got Unknown //IL_0147: Unknown result type (might be due to invalid IL or missing references) //IL_0154: Expected O, but got Unknown Type type = FindWaypointType(); if (type == null) { Plugin.Log.LogWarning((object)"Could not find Waypoints' shrine code (a Waypoints update may have changed it). Horses will not follow through waypoints until this mod is updated."); return false; } MethodInfo methodInfo = AccessTools.Method(type, "Teleport", new Type[2] { typeof(Vector3), typeof(bool) }, (Type[])null); if (methodInfo == null || methodInfo.ReturnType != typeof(bool) || !methodInfo.IsStatic) { Plugin.Log.LogWarning((object)(type.FullName + ".Teleport(Vector3, bool) not found. Horses will not follow through waypoints until this mod is updated.")); return false; } try { harmony.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(WaypointsHooks), "TeleportPrefix", (Type[])null), new HarmonyMethod(typeof(WaypointsHooks), "TeleportPostfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null); } catch (Exception ex) { Plugin.Log.LogWarning((object)("Could not patch Waypoints' teleport, mod is off: " + ex.Message)); return false; } MethodInfo methodInfo2 = AccessTools.Method(type, "GetTames", new Type[1] { typeof(Player) }, (Type[])null); if (methodInfo2 != null && methodInfo2.IsStatic && methodInfo2.ReturnType == typeof(List<Character>)) { try { harmony.Patch((MethodBase)methodInfo2, (HarmonyMethod)null, new HarmonyMethod(typeof(WaypointsHooks), "GetTamesPostfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null); } catch (Exception ex2) { methodInfo2 = null; Plugin.Log.LogWarning((object)("Could not patch Waypoints' tame list: " + ex2.Message)); } } else { methodInfo2 = null; } if (methodInfo2 == null) { Plugin.Log.LogWarning((object)"Waypoints' tame list was not found. Keep Waypoints' \"Teleport Tames\" off, or it may move your horse a second time."); } return true; } private static Type FindWaypointType() { if (!Chainloader.PluginInfos.TryGetValue("RustyMods.Waypoints", out var value) || (Object)(object)value.Instance == (Object)null) { return null; } Assembly assembly = ((object)value.Instance).GetType().Assembly; string[] waypointTypeNames = WaypointTypeNames; foreach (string name in waypointTypeNames) { Type type = assembly.GetType(name, throwOnError: false); if (type != null) { return type; } } return null; } private static void TeleportPrefix(out bool __state) { __state = false; try { __state = HorseTeleport.BeforeTeleport(); } catch (Exception arg) { HorseTeleport.Reset(); Plugin.Log.LogError((object)$"Error before waypoint teleport: {arg}"); } } private static void TeleportPostfix(bool __result, bool __state) { if (!__state) { return; } try { HorseTeleport.AfterTeleport(__result); } catch (Exception arg) { HorseTeleport.Reset(); Plugin.Log.LogError((object)$"Error after waypoint teleport: {arg}"); } } private static void GetTamesPostfix(List<Character> __result) { if (__result != null) { HorseTeleport.RemoveChosenHorses(__result); } } }