Decompiled source of ReactionWheel v1.0.0

ReactionWheel.dll

Decompiled 5 days ago
using 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.Configuration;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using HarmonyLib;
using Il2CppInterop.Runtime;
using Il2CppInterop.Runtime.Injection;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppSystem.Collections.Generic;
using Microsoft.CodeAnalysis;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Physics;
using Unity.Transforms;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("ReactionWheel")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ReactionWheel")]
[assembly: AssemblyTitle("ReactionWheel")]
[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 ReactionWheel
{
	[BepInPlugin("com.approximatelyfixing.reactionwheel", "Reaction Wheel", "1.0.0")]
	public class Plugin : BasePlugin
	{
		public const string PluginGuid = "com.approximatelyfixing.reactionwheel";

		public const string PluginName = "Reaction Wheel";

		public const string PluginVersion = "1.0.0";

		internal static ManualLogSource LogSource;

		internal static ConfigEntry<float> TorqueStrength;

		internal static ConfigEntry<float> PowerConsumption;

		internal static ConfigEntry<float> BlockMass;

		internal static ConfigEntry<float> InputDeadzone;

		internal static ConfigEntry<float> SasDampingStrength;

		internal static ConfigEntry<int> YawPortIndex;

		internal static ConfigEntry<int> PitchPortIndex;

		internal static ConfigEntry<int> RollPortIndex;

		internal static ConfigEntry<bool> InvertYaw;

		internal static ConfigEntry<bool> InvertPitch;

		internal static ConfigEntry<bool> InvertRoll;

		internal static EPC_SpaceshipComponent ReactionWheelPrefabComp;

		internal static SCPrefab ReactionWheelPrefabId;

		internal static bool IsRegistered;

		public override void Load()
		{
			//IL_0189: Unknown result type (might be due to invalid IL or missing references)
			//IL_018e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0196: Unknown result type (might be due to invalid IL or missing references)
			//IL_019c: Expected O, but got Unknown
			//IL_01a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cc: Expected O, but got Unknown
			LogSource = ((BasePlugin)this).Log;
			TorqueStrength = ((BasePlugin)this).Config.Bind<float>("Physics", "TorqueStrength", 1800f, "Rotational torque strength (N*m per full input axis). Proportional to stick input.");
			PowerConsumption = ((BasePlugin)this).Config.Bind<float>("Power", "PowerConsumption", 6f, "Power consumption in P/s shown in the build catalog.");
			BlockMass = ((BasePlugin)this).Config.Bind<float>("Physics", "BlockMass", 30f, "Mass of the Reaction Wheel block.");
			InputDeadzone = ((BasePlugin)this).Config.Bind<float>("Controls", "InputDeadzone", 0.01f, "Minimum stick magnitude (0.01 = 1%) required to apply torque.");
			SasDampingStrength = ((BasePlugin)this).Config.Bind<float>("Physics", "SasDampingStrength", 0f, "Automatic rotational damping (SAS) when stick axes are centered. Set > 0 (e.g. 250) to auto-stop spin.");
			YawPortIndex = ((BasePlugin)this).Config.Bind<int>("PortMapping", "YawPortIndex", 0, "Buffer index for Yaw (0 = Port0 / Left Yellow Port).");
			PitchPortIndex = ((BasePlugin)this).Config.Bind<int>("PortMapping", "PitchPortIndex", 1, "Buffer index for Pitch (1 = Port1 / Middle Yellow Port).");
			RollPortIndex = ((BasePlugin)this).Config.Bind<int>("PortMapping", "RollPortIndex", 2, "Buffer index for Roll (2 = Port2 / Right Yellow Port).");
			InvertYaw = ((BasePlugin)this).Config.Bind<bool>("Controls", "InvertYaw", false, "Invert Yaw rotation direction.");
			InvertPitch = ((BasePlugin)this).Config.Bind<bool>("Controls", "InvertPitch", false, "Invert Pitch rotation direction.");
			InvertRoll = ((BasePlugin)this).Config.Bind<bool>("Controls", "InvertRoll", false, "Invert Roll rotation direction.");
			ClassInjector.RegisterTypeInIl2Cpp<ReactionWheelUpdater>();
			GameObject val = new GameObject("ReactionWheel_Updater")
			{
				hideFlags = (HideFlags)61
			};
			Object.DontDestroyOnLoad((Object)val);
			val.AddComponent<ReactionWheelUpdater>();
			new Harmony("com.approximatelyfixing.reactionwheel").PatchAll(typeof(ManagedPatches));
			ManualLogSource log = ((BasePlugin)this).Log;
			bool flag = default(bool);
			BepInExInfoLogInterpolatedStringHandler val2 = new BepInExInfoLogInterpolatedStringHandler(10, 2, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>("Reaction Wheel");
				((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" v");
				((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>("1.0.0");
				((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" loaded.");
			}
			log.LogInfo(val2);
		}

		internal static Core FindCoreInstance()
		{
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Expected O, but got Unknown
			try
			{
				Il2CppReferenceArray<Object> val = Resources.FindObjectsOfTypeAll(Il2CppType.Of<Core>());
				if (val != null && ((Il2CppArrayBase<Object>)(object)val).Length > 0 && ((Il2CppArrayBase<Object>)(object)val)[0] != (Object)null)
				{
					return ((Il2CppObjectBase)((Il2CppArrayBase<Object>)(object)val)[0]).TryCast<Core>();
				}
			}
			catch (Exception ex)
			{
				ManualLogSource logSource = LogSource;
				bool flag = default(bool);
				BepInExWarningLogInterpolatedStringHandler val2 = new BepInExWarningLogInterpolatedStringHandler(24, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("FindCoreInstance check: ");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(ex.Message);
				}
				logSource.LogWarning(val2);
			}
			return null;
		}

		internal static void TryRegisterCustomBlock(string sourceTag)
		{
			//IL_01dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e3: Expected O, but got Unknown
			//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_0141: Unknown result type (might be due to invalid IL or missing references)
			//IL_0151: Unknown result type (might be due to invalid IL or missing references)
			//IL_0153: Unknown result type (might be due to invalid IL or missing references)
			//IL_0168: Unknown result type (might be due to invalid IL or missing references)
			//IL_016f: Expected O, but got Unknown
			//IL_0195: Unknown result type (might be due to invalid IL or missing references)
			if (IsRegistered)
			{
				return;
			}
			Core val = FindCoreInstance();
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			Il2CppReferenceArray<EPC_SpaceshipComponent> spaceshipComponents = val._spaceshipComponents;
			Dictionary<SCPrefab, EPC_SpaceshipComponent> componentsMap = val._componentsMap;
			if (spaceshipComponents == null || ((Il2CppArrayBase<EPC_SpaceshipComponent>)(object)spaceshipComponents).Length == 0 || componentsMap == null)
			{
				return;
			}
			EPC_SCGimbalController val2 = null;
			for (int i = 0; i < ((Il2CppArrayBase<EPC_SpaceshipComponent>)(object)spaceshipComponents).Length; i++)
			{
				EPC_SpaceshipComponent val3 = ((Il2CppArrayBase<EPC_SpaceshipComponent>)(object)spaceshipComponents)[i];
				if (!((Object)(object)val3 == (Object)null))
				{
					EPC_SCGimbalController val4 = ((Il2CppObjectBase)val3).TryCast<EPC_SCGimbalController>();
					if ((Object)(object)val4 != (Object)null)
					{
						val2 = val4;
						break;
					}
				}
			}
			if ((Object)(object)val2 == (Object)null)
			{
				return;
			}
			bool flag = default(bool);
			try
			{
				GameObject obj = Object.Instantiate<GameObject>(((Component)val2).gameObject);
				((Object)obj).name = "SC_ReactionWheel";
				((Object)obj).hideFlags = (HideFlags)61;
				Object.DontDestroyOnLoad((Object)(object)obj);
				EPC_SpaceshipComponent component = obj.GetComponent<EPC_SpaceshipComponent>();
				if ((Object)(object)component == (Object)null)
				{
					LogSource.LogError((object)"Cloned Reaction Wheel prefab is missing EPC_SpaceshipComponent.");
					return;
				}
				component._mass = BlockMass.Value;
				component._categories = ((EPC_SpaceshipComponent)val2)._categories;
				Il2CppReferenceArray<EPC_SpaceshipComponent> val5 = new Il2CppReferenceArray<EPC_SpaceshipComponent>((long)(((Il2CppArrayBase<EPC_SpaceshipComponent>)(object)spaceshipComponents).Length + 1));
				for (int j = 0; j < ((Il2CppArrayBase<EPC_SpaceshipComponent>)(object)spaceshipComponents).Length; j++)
				{
					((Il2CppArrayBase<EPC_SpaceshipComponent>)(object)val5)[j] = ((Il2CppArrayBase<EPC_SpaceshipComponent>)(object)spaceshipComponents)[j];
				}
				((Il2CppArrayBase<EPC_SpaceshipComponent>)(object)val5)[((Il2CppArrayBase<EPC_SpaceshipComponent>)(object)spaceshipComponents).Length] = component;
				val._spaceshipComponents = val5;
				SCPrefab val6 = default(SCPrefab);
				((SCPrefab)(ref val6))..ctor(component);
				componentsMap[val6] = component;
				ReactionWheelPrefabComp = component;
				ReactionWheelPrefabId = val6;
				IsRegistered = true;
				ManualLogSource logSource = LogSource;
				BepInExInfoLogInterpolatedStringHandler val7 = new BepInExInfoLogInterpolatedStringHandler(58, 3, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val7).AppendLiteral("Registered 'REACTION WHEEL' via ");
					((BepInExLogInterpolatedStringHandler)val7).AppendFormatted<string>(sourceTag);
					((BepInExLogInterpolatedStringHandler)val7).AppendLiteral(" (SCPrefab=");
					((BepInExLogInterpolatedStringHandler)val7).AppendFormatted<ulong>(val6._prefab);
					((BepInExLogInterpolatedStringHandler)val7).AppendLiteral(", TotalParts=");
					((BepInExLogInterpolatedStringHandler)val7).AppendFormatted<int>(((Il2CppArrayBase<EPC_SpaceshipComponent>)(object)val5).Length);
					((BepInExLogInterpolatedStringHandler)val7).AppendLiteral(").");
				}
				logSource.LogInfo(val7);
			}
			catch (Exception ex)
			{
				ManualLogSource logSource2 = LogSource;
				BepInExErrorLogInterpolatedStringHandler val8 = new BepInExErrorLogInterpolatedStringHandler(41, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val8).AppendLiteral("Failed to register Reaction Wheel block: ");
					((BepInExLogInterpolatedStringHandler)val8).AppendFormatted<Exception>(ex);
				}
				logSource2.LogError(val8);
			}
		}

		internal static bool IsReactionWheel(EPC_SpaceshipComponent comp)
		{
			if ((Object)(object)comp == (Object)null)
			{
				return false;
			}
			if ((Object)(object)ReactionWheelPrefabComp != (Object)null && ((Il2CppObjectBase)comp).Pointer == ((Il2CppObjectBase)ReactionWheelPrefabComp).Pointer)
			{
				return true;
			}
			if ((Object)(object)((Component)comp).gameObject != (Object)null)
			{
				return ((Object)((Component)comp).gameObject).name == "SC_ReactionWheel";
			}
			return false;
		}
	}
	internal static class ManagedPatches
	{
		[HarmonyPatch(typeof(UIInventory), "PreAwake")]
		[HarmonyPrefix]
		private static void UIInventory_PreAwake_Prefix()
		{
			Plugin.TryRegisterCustomBlock("UIInventory.PreAwake");
		}

		[HarmonyPatch(typeof(UIInventory), "Awake")]
		[HarmonyPrefix]
		private static void UIInventory_Awake_Prefix()
		{
			Plugin.TryRegisterCustomBlock("UIInventory.Awake");
		}

		[HarmonyPatch(typeof(EPC_SpaceshipComponent), "GetName")]
		[HarmonyPostfix]
		private static void GetName_Postfix(EPC_SpaceshipComponent __instance, ref string __result)
		{
			if (Plugin.IsReactionWheel(__instance))
			{
				__result = "REACTION WHEEL";
			}
		}

		[HarmonyPatch(typeof(EPC_SpaceshipComponent), "GetDescription")]
		[HarmonyPostfix]
		private static void GetDescription_Postfix(EPC_SpaceshipComponent __instance, ref string __result)
		{
			if (Plugin.IsReactionWheel(__instance))
			{
				__result = "Internal Control Moment Gyroscope (CMG). Applies proportional rotational torque on Yaw, Pitch, and Roll without external thrusters.";
			}
		}

		[HarmonyPatch(typeof(EPC_SpaceshipComponent), "GetPropertiesList")]
		[HarmonyPostfix]
		private static void GetPropertiesList_Postfix(EPC_SpaceshipComponent __instance, List<string> list)
		{
			if (Plugin.IsReactionWheel(__instance) && list != null)
			{
				list.Clear();
				list.Add($"Mass: {Plugin.BlockMass.Value:0.#}");
				list.Add($"Power consumption: {Plugin.PowerConsumption.Value:0.#} P/s");
				list.Add($"Torque: {Plugin.TorqueStrength.Value:0} N*m");
			}
		}
	}
	public class ReactionWheelUpdater : MonoBehaviour
	{
		private static readonly HashSet<Entity> TrackedWheels = new HashSet<Entity>();

		private static readonly List<Entity> StaleBuffer = new List<Entity>();

		private int _scanCooldown;

		public ReactionWheelUpdater(IntPtr ptr)
			: base(ptr)
		{
		}

		private void Update()
		{
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			if (!Plugin.IsRegistered)
			{
				Plugin.TryRegisterCustomBlock("Updater.Update");
				return;
			}
			World defaultGameObjectInjectionWorld = World.DefaultGameObjectInjectionWorld;
			if (defaultGameObjectInjectionWorld != null && defaultGameObjectInjectionWorld.IsCreated)
			{
				EntityManager entityManager = defaultGameObjectInjectionWorld.EntityManager;
				if (_scanCooldown <= 0)
				{
					_scanCooldown = 15;
					DiscoverReactionWheelEntities(entityManager);
				}
				else
				{
					_scanCooldown--;
				}
			}
		}

		private void LateUpdate()
		{
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ae: Expected O, but got Unknown
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: 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_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_0117: Unknown result type (might be due to invalid IL or missing references)
			if (!Plugin.IsRegistered || TrackedWheels.Count == 0)
			{
				return;
			}
			World defaultGameObjectInjectionWorld = World.DefaultGameObjectInjectionWorld;
			if (defaultGameObjectInjectionWorld == null || !defaultGameObjectInjectionWorld.IsCreated)
			{
				return;
			}
			EntityManager entityManager = defaultGameObjectInjectionWorld.EntityManager;
			float value = Plugin.TorqueStrength.Value;
			float value2 = Plugin.InputDeadzone.Value;
			StaleBuffer.Clear();
			bool flag = default(bool);
			foreach (Entity trackedWheel in TrackedWheels)
			{
				if (trackedWheel == Entity.Null || !((EntityManager)(ref entityManager)).Exists(trackedWheel))
				{
					StaleBuffer.Add(trackedWheel);
					continue;
				}
				try
				{
					ProcessReactionWheel(entityManager, trackedWheel, value, value2);
				}
				catch (Exception ex)
				{
					ManualLogSource logSource = Plugin.LogSource;
					BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(30, 2, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("ReactionWheel tick error on ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<Entity>(trackedWheel);
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral(": ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.Message);
					}
					logSource.LogWarning(val);
				}
			}
			for (int i = 0; i < StaleBuffer.Count; i++)
			{
				TrackedWheels.Remove(StaleBuffer[i]);
			}
		}

		private static void DiscoverReactionWheelEntities(EntityManager em)
		{
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Expected O, but got Unknown
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: 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_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			ulong prefab = Plugin.ReactionWheelPrefabId._prefab;
			if (prefab == 0L)
			{
				return;
			}
			EntityQuery val = default(EntityQuery);
			bool flag = false;
			NativeArray<Entity> val2 = null;
			NativeArray<SCPrefab> val3 = null;
			bool flag2 = false;
			bool flag3 = false;
			try
			{
				val = ((EntityManager)(ref em)).CreateEntityQuery((ComponentType[])(object)new ComponentType[1] { ComponentType.ReadOnly<SCPrefab>() });
				flag = true;
				val2 = ((EntityQuery)(ref val)).ToEntityArray(AllocatorHandle.op_Implicit((Allocator)2));
				flag2 = true;
				val3 = ((EntityQuery)(ref val)).ToComponentDataArray<SCPrefab>(AllocatorHandle.op_Implicit((Allocator)2));
				flag3 = true;
				for (int i = 0; i < val2.Length; i++)
				{
					if (val3[i]._prefab == prefab)
					{
						TrackedWheels.Add(val2[i]);
					}
				}
			}
			catch (Exception ex)
			{
				ManualLogSource logSource = Plugin.LogSource;
				bool flag4 = default(bool);
				BepInExWarningLogInterpolatedStringHandler val4 = new BepInExWarningLogInterpolatedStringHandler(37, 1, ref flag4);
				if (flag4)
				{
					((BepInExLogInterpolatedStringHandler)val4).AppendLiteral("DiscoverReactionWheelEntities error: ");
					((BepInExLogInterpolatedStringHandler)val4).AppendFormatted<string>(ex.Message);
				}
				logSource.LogWarning(val4);
			}
			finally
			{
				if (flag3)
				{
					val3.Dispose();
				}
				if (flag2)
				{
					val2.Dispose();
				}
				if (flag)
				{
					((EntityQuery)(ref val)).Dispose();
				}
			}
		}

		private static void ProcessReactionWheel(EntityManager em, Entity wheelEntity, float torqueStrength, float deadzone)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: 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_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: 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_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: Unknown result type (might be due to invalid IL or missing references)
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: 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_010e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0113: Unknown result type (might be due to invalid IL or missing references)
			//IL_0117: Unknown result type (might be due to invalid IL or missing references)
			//IL_0157: Unknown result type (might be due to invalid IL or missing references)
			//IL_0168: Unknown result type (might be due to invalid IL or missing references)
			//IL_016d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0172: Unknown result type (might be due to invalid IL or missing references)
			//IL_0174: Unknown result type (might be due to invalid IL or missing references)
			//IL_0185: Unknown result type (might be due to invalid IL or missing references)
			//IL_018a: Unknown result type (might be due to invalid IL or missing references)
			//IL_018f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0191: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0121: Unknown result type (might be due to invalid IL or missing references)
			//IL_0245: Unknown result type (might be due to invalid IL or missing references)
			//IL_012b: Unknown result type (might be due to invalid IL or missing references)
			//IL_012c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0131: Unknown result type (might be due to invalid IL or missing references)
			//IL_0135: Unknown result type (might be due to invalid IL or missing references)
			//IL_0136: Unknown result type (might be due to invalid IL or missing references)
			//IL_013b: Unknown result type (might be due to invalid IL or missing references)
			//IL_013f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0144: Unknown result type (might be due to invalid IL or missing references)
			//IL_014b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0150: Unknown result type (might be due to invalid IL or missing references)
			//IL_0155: Unknown result type (might be due to invalid IL or missing references)
			//IL_0258: Unknown result type (might be due to invalid IL or missing references)
			//IL_025c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0261: Unknown result type (might be due to invalid IL or missing references)
			//IL_0298: Unknown result type (might be due to invalid IL or missing references)
			//IL_0282: Unknown result type (might be due to invalid IL or missing references)
			//IL_029d: Unknown result type (might be due to invalid IL or missing references)
			//IL_029f: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_02bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_02dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0301: Unknown result type (might be due to invalid IL or missing references)
			//IL_021c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0226: Unknown result type (might be due to invalid IL or missing references)
			//IL_0227: Unknown result type (might be due to invalid IL or missing references)
			//IL_022c: Unknown result type (might be due to invalid IL or missing references)
			//IL_022e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0230: Unknown result type (might be due to invalid IL or missing references)
			//IL_0232: Unknown result type (might be due to invalid IL or missing references)
			//IL_0239: Unknown result type (might be due to invalid IL or missing references)
			//IL_023e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0243: Unknown result type (might be due to invalid IL or missing references)
			Entity val = FindShipRootWithForceBuffer(em, wheelEntity);
			if (val == Entity.Null || (((EntityManager)(ref em)).HasComponent<SCElectricPower>(wheelEntity) && ((EntityManager)(ref em)).GetComponentData<SCElectricPower>(wheelEntity)._isElectricityInterrupted) || !((EntityManager)(ref em)).HasBuffer<SpaceshipElectricPortRef>(wheelEntity))
			{
				return;
			}
			DynamicBuffer<SpaceshipElectricPortRef> buffer = ((EntityManager)(ref em)).GetBuffer<SpaceshipElectricPortRef>(wheelEntity, true);
			if (buffer.Length >= 3)
			{
				int num = math.clamp(Plugin.YawPortIndex.Value, 0, buffer.Length - 1);
				int num2 = math.clamp(Plugin.PitchPortIndex.Value, 0, buffer.Length - 1);
				int num3 = math.clamp(Plugin.RollPortIndex.Value, 0, buffer.Length - 1);
				float num4 = ReadSignedAxis(em, buffer[num]._portEntity, deadzone);
				float num5 = ReadSignedAxis(em, buffer[num2]._portEntity, deadzone);
				float num6 = ReadSignedAxis(em, buffer[num3]._portEntity, deadzone);
				if (Plugin.InvertYaw.Value)
				{
					num4 = 0f - num4;
				}
				if (Plugin.InvertPitch.Value)
				{
					num5 = 0f - num5;
				}
				if (Plugin.InvertRoll.Value)
				{
					num6 = 0f - num6;
				}
				quaternion val2 = quaternion.identity;
				if (((EntityManager)(ref em)).HasComponent<LocalToWorld>(wheelEntity) && ((EntityManager)(ref em)).HasComponent<LocalToWorld>(val))
				{
					LocalToWorld componentData = ((EntityManager)(ref em)).GetComponentData<LocalToWorld>(wheelEntity);
					LocalToWorld componentData2 = ((EntityManager)(ref em)).GetComponentData<LocalToWorld>(val);
					val2 = math.mul(math.inverse(((LocalToWorld)(ref componentData2)).Rotation), ((LocalToWorld)(ref componentData)).Rotation);
				}
				float3 val3 = math.mul(val2, new float3(1f, 0f, 0f));
				float3 val4 = math.mul(val2, new float3(0f, 1f, 0f));
				float3 val5 = math.mul(val2, new float3(0f, 0f, 1f));
				float3 val6 = (val3 * num5 + val4 * num4 + val5 * num6) * torqueStrength;
				float value = Plugin.SasDampingStrength.Value;
				if (value > 0f && Math.Abs(num5) < 0.0001f && Math.Abs(num4) < 0.0001f && Math.Abs(num6) < 0.0001f && ((EntityManager)(ref em)).HasComponent<PhysicsVelocity>(val))
				{
					PhysicsVelocity componentData3 = ((EntityManager)(ref em)).GetComponentData<PhysicsVelocity>(val);
					val6 -= componentData3.Angular * value;
				}
				float num7 = math.length(val6);
				if (!(num7 < 0.001f))
				{
					float3 val7 = val6 / num7;
					float3 val8 = ((Math.Abs(val7.y) < 0.9f) ? new float3(0f, 1f, 0f) : new float3(1f, 0f, 0f));
					float3 val9 = math.normalize(math.cross(val7, val8));
					float3 val10 = math.cross(val7, val9) * (num7 * 0.5f);
					DynamicBuffer<SpaceshipPartForce> buffer2 = ((EntityManager)(ref em)).GetBuffer<SpaceshipPartForce>(val, false);
					buffer2.Add(new SpaceshipPartForce(val10, val9, (Type)1, 0f, 0f));
					buffer2.Add(new SpaceshipPartForce(-val10, -val9, (Type)1, 0f, 0f));
				}
			}
		}

		private static float ReadSignedAxis(EntityManager em, Entity portEntity, float deadzone)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: 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)
			if (portEntity == Entity.Null || !((EntityManager)(ref em)).Exists(portEntity))
			{
				return 0f;
			}
			if (!((EntityManager)(ref em)).HasComponent<SpaceshipElectricPort>(portEntity))
			{
				return 0f;
			}
			SpaceshipElectricPort componentData = ((EntityManager)(ref em)).GetComponentData<SpaceshipElectricPort>(portEntity);
			float num = ((SpaceshipElectricPort)(ref componentData)).DataValue();
			if (!(Math.Abs(num) < deadzone))
			{
				return math.clamp(num, -1f, 1f);
			}
			return 0f;
		}

		private static Entity FindShipRootWithForceBuffer(EntityManager em, Entity startEntity)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: 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_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: 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_0039: Unknown result type (might be due to invalid IL or missing references)
			Entity val = startEntity;
			for (int i = 0; i < 16; i++)
			{
				if (((EntityManager)(ref em)).HasBuffer<SpaceshipPartForce>(val))
				{
					return val;
				}
				if (!((EntityManager)(ref em)).HasComponent<Parent>(val))
				{
					break;
				}
				val = ((EntityManager)(ref em)).GetComponentData<Parent>(val).Value;
				if (val == Entity.Null || !((EntityManager)(ref em)).Exists(val))
				{
					break;
				}
			}
			return Entity.Null;
		}
	}
}