Decompiled source of PeakBackpackAlt v1.0.3

PeakBackpackAlt.dll

Decompiled a day ago
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 BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("PeakBackpackAlt")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("PeakBackpackAlt")]
[assembly: AssemblyTitle("PeakBackpackAlt")]
[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 PeakBackpackAlt
{
	[BepInPlugin("com.example.peakbackpackalt", "PeakBackpackAlt", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.example.peakbackpackalt";

		public const string PluginName = "PeakBackpackAlt";

		public const string PluginVersion = "1.0.0";

		internal static ManualLogSource Log;

		internal static ConfigEntry<int> BackpackWeightReductionPercentage;

		internal static ConfigEntry<bool> VerboseLogging;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Expected O, but got Unknown
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			BackpackWeightReductionPercentage = ((BaseUnityPlugin)this).Config.Bind<int>("General", "BackpackWeightReductionPercentage", 50, new ConfigDescription("На сколько процентов снижается вес предметов, находящихся в рюкзаке (0-100).", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 100), Array.Empty<object>()));
			VerboseLogging = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "VerboseLogging", false, "Подробный лог патча веса (для отладки совместимости с другими модами).");
			_harmony = new Harmony("com.example.peakbackpackalt");
			try
			{
				_harmony.PatchAll(Assembly.GetExecutingAssembly());
				Log.LogInfo((object)string.Format("{0} v{1} загружен. Снижение веса: {2}%.", "PeakBackpackAlt", "1.0.0", BackpackWeightReductionPercentage.Value));
			}
			catch (Exception arg)
			{
				Log.LogError((object)$"Не удалось применить патчи: {arg}");
			}
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
	[HarmonyPatch(typeof(CharacterAfflictions), "UpdateWeight")]
	public static class UpdateWeightTranspiler
	{
		private const int BackpackLoopCallOccurrence = 2;

		private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b5: Expected O, but got Unknown
			MethodInfo methodInfo = AccessTools.PropertyGetter(typeof(Item), "CarryWeight");
			MethodInfo methodInfo2 = AccessTools.Method(typeof(UpdateWeightTranspiler), "ReduceIfBackpackCall", (Type[])null, (Type[])null);
			List<CodeInstruction> list = new List<CodeInstruction>(instructions);
			int num = 0;
			List<CodeInstruction> list2 = new List<CodeInstruction>();
			foreach (CodeInstruction item in list)
			{
				list2.Add(item);
				if ((!(item.opcode == OpCodes.Call) && !(item.opcode == OpCodes.Callvirt)) || !(item.operand is MethodInfo methodInfo3) || !(methodInfo3 == methodInfo))
				{
					continue;
				}
				num++;
				if (num == 2)
				{
					list2.Add(new CodeInstruction(OpCodes.Call, (object)methodInfo2));
					if (Plugin.VerboseLogging.Value)
					{
						Plugin.Log.LogInfo((object)"Патч применён к вызову get_CarryWeight #2 (цикл по рюкзаку) в UpdateWeight.");
					}
				}
			}
			if (num < 2)
			{
				Plugin.Log.LogWarning((object)($"UpdateWeightTranspiler: ожидалось {2} вызовов " + $"get_CarryWeight, найдено {num}. Похоже, версия игры изменилась — " + "скидка на вес рюкзака НЕ применяется. Проверьте обновление мода."));
			}
			return list2;
		}

		public static int ReduceIfBackpackCall(int rawWeight)
		{
			int value = Plugin.BackpackWeightReductionPercentage.Value;
			if (value <= 0)
			{
				return rawWeight;
			}
			if (value >= 100)
			{
				return 0;
			}
			return rawWeight * (100 - value) / 100;
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "PeakBackpackAlt";

		public const string PLUGIN_NAME = "PeakBackpackAlt";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}