using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("TakeoffAnySpeed")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("TakeoffAnySpeed")]
[assembly: AssemblyTitle("TakeoffAnySpeed")]
[assembly: AssemblyVersion("1.0.0.0")]
[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 TakeoffAnySpeed
{
internal static class EnergyPatches
{
[HarmonyPrefix]
[HarmonyPatch(typeof(PlayerMove_Walk), "UseWalkEnergy")]
[HarmonyPatch(typeof(PlayerMove_Fly), "UseFlyEnergy")]
[HarmonyPatch(typeof(PlayerMove_Drift), "UseDriftEnergy")]
private static void Movement(ref double energy)
{
if (Plugin.On(Plugin.MovementEnergy))
{
energy *= Plugin.Ratio;
}
}
[HarmonyPrefix]
[HarmonyPatch(typeof(PlayerMove_Fly), "UseThrustEnergy")]
[HarmonyPatch(typeof(PlayerMove_Drift), "UseThrustEnergy")]
private static void Thrust(ref double dt)
{
if (Plugin.On(Plugin.MovementEnergy))
{
dt *= Plugin.Ratio;
}
}
}
public static class RotationPatch
{
public const float Step = 2.011694f;
public static int Sites;
public static float RotateStep()
{
if (!Plugin.On(Plugin.MinerRotation))
{
return 2.011694f;
}
return 2.011694f * Plugin.Ratio;
}
[HarmonyTranspiler]
[HarmonyPatch(typeof(BuildTool_Click), "DeterminePreviews")]
private static IEnumerable<CodeInstruction> FreeRotation(IEnumerable<CodeInstruction> instructions)
{
int sites = 0;
foreach (CodeInstruction instruction in instructions)
{
if (instruction.opcode == OpCodes.Ldc_R4 && instruction.operand is float num && Mathf.Abs(num - 2.011694f) < 1E-05f)
{
instruction.opcode = OpCodes.Call;
instruction.operand = AccessTools.Method(typeof(RotationPatch), "RotateStep", (Type[])null, (Type[])null);
sites++;
}
yield return instruction;
}
Sites = sites;
}
}
[BepInPlugin("com.github.jiesiluo.takeoffanyspeed", "Takeoff At Any Speed", "1.1.0")]
public class Plugin : BaseUnityPlugin
{
public const string Guid = "com.github.jiesiluo.takeoffanyspeed";
public const string Version = "1.1.0";
private const float Tick = 1f / 60f;
internal static ConfigEntry<bool> Enabled;
internal static ConfigEntry<bool> TakeoffWindow;
internal static ConfigEntry<bool> MovementEnergy;
internal static ConfigEntry<bool> MinerRotation;
public static bool Suspended;
private float lastChance;
public static float Ratio => Mathf.Clamp01(Time.fixedDeltaTime * 60f);
internal static bool On(ConfigEntry<bool> feature)
{
if (Enabled.Value && !Suspended)
{
return feature.Value;
}
return false;
}
private void Awake()
{
Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Master switch for the whole mod.");
TakeoffWindow = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "TakeoffWindow", true, "Keep the double-tap take-off window at its normal 0.8 s when the game runs faster than 60 updates per second.");
MovementEnergy = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "MovementEnergy", true, "Walking, flying and hovering on a planet cost the same energy per second as at normal speed (the mecha does not move faster there when the game is sped up).");
MinerRotation = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "MinerRotation", true, "Holding Shift and the rotate key turns a miner at the normal rate when the game is sped up.");
Patch(typeof(EnergyPatches));
Patch(typeof(RotationPatch));
}
private void Patch(Type patches)
{
try
{
Harmony.CreateAndPatchAll(patches, "com.github.jiesiluo.takeoffanyspeed");
}
catch (Exception arg)
{
((BaseUnityPlugin)this).Logger.LogError((object)$"{patches.Name} not applied: {arg}");
}
}
private void FixedUpdate()
{
if (!On(TakeoffWindow))
{
return;
}
Player mainPlayer = GameMain.mainPlayer;
if (mainPlayer == null || (Object)(object)mainPlayer.controller == (Object)null)
{
return;
}
PlayerMove_Walk actionWalk = mainPlayer.controller.actionWalk;
if (actionWalk == null)
{
return;
}
float num = actionWalk.flyUpChance;
if (num <= 0f)
{
lastChance = 0f;
}
else if (num != lastChance)
{
float num2 = 1f / 60f - Time.fixedDeltaTime;
if (num2 > 0f && num < lastChance)
{
num = Mathf.Min(num + num2, lastChance);
}
actionWalk.flyUpChance = num;
lastChance = num;
}
}
}
}