Decompiled source of OverweightSkill v1.0.2

plugins/OverweightSkill.dll

Decompiled an hour ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
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("OverweightSkill")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: AssemblyInformationalVersion("1.0.2+242327d8948e3a30537bc80f7c53478dd73bf6ff")]
[assembly: AssemblyProduct("OverweightSkill")]
[assembly: AssemblyTitle("OverweightSkill")]
[assembly: AssemblyVersion("1.0.2.0")]
namespace OverweightSkill;

[BepInPlugin("fr.romaindurand.overweightskill", "OverweightSkill", "1.0.2")]
public sealed class Plugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(ObjectDB), "UpdateRegisters")]
	private static class LoadEncumberedIcon
	{
		private static void Postfix(ObjectDB __instance)
		{
			SetEncumberedIcon(__instance);
		}
	}

	[HarmonyPatch(typeof(Skills), "Awake")]
	private static class RegisterSkill
	{
		private static void Postfix(Skills __instance)
		{
			if (!Object.op_Implicit((Object)(object)SkillDefinition.m_icon))
			{
				SetEncumberedIcon(ObjectDB.instance);
			}
			__instance.m_skills.Add(SkillDefinition);
		}
	}

	[HarmonyPatch(typeof(Skills), "IsSkillValid")]
	private static class LoadSkill
	{
		private static void Postfix(SkillType __0, ref bool __result)
		{
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Invalid comparison between Unknown and I4
			__result |= (int)__0 == 476197535;
		}
	}

	[HarmonyPatch(typeof(Player), "GetMaxCarryWeight")]
	private static class IncreaseCapacity
	{
		private static void Postfix(Player __instance, ref float __result)
		{
			if (!((Object)(object)__instance != (Object)(object)Player.m_localPlayer))
			{
				Skills skills = ((Character)__instance).GetSkills();
				if (Object.op_Implicit((Object)(object)skills))
				{
					__result += WalkingProgress.CarryBonus(skills.GetSkillLevel((SkillType)476197535));
				}
			}
		}
	}

	[HarmonyPatch(typeof(Localization), "SetupLanguage")]
	private static class LocalizeSkill
	{
		private static void Postfix(string __0, Dictionary<string, string> ___m_translations)
		{
			SetTranslations(__0, ___m_translations);
		}
	}

	private const string Guid = "fr.romaindurand.overweightskill";

	private const string Version = "1.0.2";

	private const SkillType Overweight = (SkillType)476197535;

	private const string DescriptionKey = "overweightskill_description";

	private static readonly string NameKey = "skill_" + 476197535.ToString(CultureInfo.InvariantCulture);

	private static readonly SkillDef SkillDefinition = new SkillDef
	{
		m_skill = (SkillType)476197535,
		m_description = "$overweightskill_description",
		m_increseStep = 1f
	};

	private readonly WalkingProgress progress = new WalkingProgress();

	private Harmony harmony;

	private Player trackedPlayer;

	private Rigidbody body;

	private Vector2 previousPosition;

	private bool hasPreviousPosition;

	private bool wasWalkingOverweight;

	private void Awake()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_0010: Expected O, but got Unknown
		harmony = new Harmony("fr.romaindurand.overweightskill");
		harmony.PatchAll(typeof(Plugin).Assembly);
		((BaseUnityPlugin)this).Logger.LogInfo((object)$"OverweightSkill {((BaseUnityPlugin)this).Info.Metadata.Version} loaded ({((BaseUnityPlugin)this).Info.Metadata.GUID}).");
	}

	private void Start()
	{
		Localization instance = Localization.instance;
		SetTranslations(instance.GetSelectedLanguage(), AccessTools.FieldRefAccess<Localization, Dictionary<string, string>>(instance, "m_translations"));
	}

	private void OnDestroy()
	{
		Harmony obj = harmony;
		if (obj != null)
		{
			obj.UnpatchSelf();
		}
	}

	private void FixedUpdate()
	{
		//IL_006d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_0075: Unknown result type (might be due to invalid IL or missing references)
		//IL_007b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0086: Unknown result type (might be due to invalid IL or missing references)
		//IL_0088: Unknown result type (might be due to invalid IL or missing references)
		//IL_008d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0092: Unknown result type (might be due to invalid IL or missing references)
		//IL_0094: Unknown result type (might be due to invalid IL or missing references)
		//IL_0099: Unknown result type (might be due to invalid IL or missing references)
		//IL_009d: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b9: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ba: 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_0154: Unknown result type (might be due to invalid IL or missing references)
		//IL_0178: Unknown result type (might be due to invalid IL or missing references)
		//IL_017f: Unknown result type (might be due to invalid IL or missing references)
		Player localPlayer = Player.m_localPlayer;
		if (localPlayer != trackedPlayer)
		{
			trackedPlayer = localPlayer;
			body = (Object.op_Implicit((Object)(object)localPlayer) ? ((Component)localPlayer).GetComponent<Rigidbody>() : null);
			ResetTracking();
		}
		if (!Object.op_Implicit((Object)(object)body) || ((Character)localPlayer).IsDead() || ((Character)localPlayer).IsTeleporting() || ((Character)localPlayer).InIntro() || ((Character)localPlayer).IsDebugFlying())
		{
			ResetTracking();
			return;
		}
		Vector3 position = body.position;
		Vector2 val = default(Vector2);
		((Vector2)(ref val))..ctor(position.x, position.z);
		Vector2 val2 = val - previousPosition;
		Vector3 moveDir = ((Character)localPlayer).GetMoveDir();
		Vector2 val3 = default(Vector2);
		((Vector2)(ref val3))..ctor(moveDir.x, moveDir.z);
		bool flag = ((Vector2)(ref val3)).sqrMagnitude > 0.01f && ((Character)localPlayer).IsOnGround() && !((Character)localPlayer).IsSwimming() && !((Character)localPlayer).IsAttached() && !((Character)localPlayer).IsRiding() && !((Character)localPlayer).InDodge() && !((Character)localPlayer).IsStaggering() && !Object.op_Implicit((Object)(object)((Character)localPlayer).GetStandingOnShip()) && ((Character)localPlayer).HaveStamina(0f) && ((Character)localPlayer).IsEncumbered();
		if (hasPreviousPosition)
		{
			float magnitude = ((Vector2)(ref val2)).magnitude;
			float fixedDeltaTime = Time.fixedDeltaTime;
			if (magnitude > 15f * fixedDeltaTime)
			{
				progress.Reset();
			}
			else if (magnitude >= 0.1f * fixedDeltaTime)
			{
				bool flag2 = Vector2.Dot(val2, val3) >= 0.5f * magnitude * ((Vector2)(ref val3)).magnitude;
				double num = progress.Record(val2.x, val2.y, flag && wasWalkingOverweight && flag2);
				if (num > 0.0)
				{
					((Character)localPlayer).RaiseSkill((SkillType)476197535, (float)num);
				}
			}
		}
		previousPosition = val;
		hasPreviousPosition = true;
		wasWalkingOverweight = flag;
	}

	private void ResetTracking()
	{
		if (hasPreviousPosition)
		{
			progress.Reset();
		}
		hasPreviousPosition = false;
		wasWalkingOverweight = false;
	}

	private static void SetTranslations(string language, Dictionary<string, string> words)
	{
		bool flag = language == "French";
		words[NameKey] = (flag ? "Surcharge" : "Overweight");
		words["overweightskill_description"] = (flag ? "Marchez en étant surchargé pour progresser. Chaque niveau ajoute 2 à votre capacité de portage." : "Walk while overencumbered to gain experience. Each level adds 2 to your carrying capacity.");
	}

	private static void SetEncumberedIcon(ObjectDB database)
	{
		if (Object.op_Implicit((Object)(object)database))
		{
			StatusEffect statusEffect = database.GetStatusEffect(SEMan.s_statusEffectEncumbered);
			if (Object.op_Implicit((Object)(object)statusEffect))
			{
				SkillDefinition.m_icon = statusEffect.m_icon;
			}
		}
	}
}
internal sealed class WalkingProgress
{
	private readonly struct Step
	{
		public readonly double X;

		public readonly double Z;

		public readonly double Length;

		public Step(double x, double z, double length)
		{
			X = x;
			Z = z;
			Length = length;
		}
	}

	private const double WindowDistance = 24.0;

	private const double MinimumDistance = 4.0;

	private const double MinimumDirectness = 0.6;

	private readonly Queue<Step> steps = new Queue<Step>();

	private double distance;

	private double netX;

	private double netZ;

	public double Record(double x, double z, bool eligible)
	{
		double num = Math.Sqrt(x * x + z * z);
		if (num <= 0.0)
		{
			return 0.0;
		}
		steps.Enqueue(new Step(x, z, num));
		distance += num;
		netX += x;
		netZ += z;
		while (distance - steps.Peek().Length >= 24.0)
		{
			Step step = steps.Dequeue();
			distance -= step.Length;
			netX -= step.X;
			netZ -= step.Z;
		}
		if (!eligible || distance < 4.0)
		{
			return 0.0;
		}
		double val = (Math.Sqrt(netX * netX + netZ * netZ) / distance - 0.6) / 0.4;
		return num * Math.Max(0.0, Math.Min(1.0, val));
	}

	public void Reset()
	{
		steps.Clear();
		distance = 0.0;
		netX = 0.0;
		netZ = 0.0;
	}

	public static float CarryBonus(float level)
	{
		return 2f * Math.Max(0f, Math.Min(100f, level));
	}
}