Decompiled source of TebArrowsQoL v0.1.2

TebArrowsQoL.dll

Decompiled a year ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("TebArrowsQoL")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TebArrowsQoL")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("8f4211a9-4196-4696-9cc5-78086fe13a38")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace TebArrowsQoL;

[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInPlugin("com.tebbeh.mod.TebArrowQoL", "TebArrowQoL", "0.1.0")]
public class TebArrowsQoL : BaseUnityPlugin
{
	private const string ModName = "TebArrowQoL";

	private const string ModVersion = "0.1.0";

	private const string Author = "com.tebbeh.mod";

	private const string ModGUID = "com.tebbeh.mod.TebArrowQoL";

	private static string ConfigFileName = "com.tebbeh.mod.TebArrowQoL.cfg";

	private static string ConfigFileFullPath;

	private readonly Harmony HarmonyInstance = new Harmony("com.tebbeh.mod.TebArrowQoL");

	public static readonly ManualLogSource TebLogger;

	internal static ConfigEntry<bool> AdminBypass;

	internal static ConfigEntry<bool> ChanceToSaveArrowsOn;

	internal static ConfigEntry<float> ChanceToSaveArrowsValue;

	private readonly ConfigurationManagerAttributes AdminConfig = new ConfigurationManagerAttributes
	{
		IsAdminOnly = true
	};

	private readonly ConfigurationManagerAttributes ClientConfig = new ConfigurationManagerAttributes
	{
		IsAdminOnly = false
	};

	public static bool GetAdminBypass()
	{
		return AdminBypass.Value;
	}

	public static bool GetChanceToSaveArrowsOn()
	{
		return ChanceToSaveArrowsOn.Value;
	}

	public static float GetChanceToSaveArrowsValue()
	{
		return ChanceToSaveArrowsValue.Value;
	}

	private void AddConfig<T>(string key, string section, string description, bool synced, T value, ref ConfigEntry<T> configEntry)
	{
		//IL_0034: Unknown result type (might be due to invalid IL or missing references)
		//IL_003e: Expected O, but got Unknown
		string extendedDescription = GetExtendedDescription(description, synced);
		configEntry = ((BaseUnityPlugin)this).Config.Bind<T>(section, key, value, new ConfigDescription(extendedDescription, (AcceptableValueBase)null, new object[1] { synced ? AdminConfig : ClientConfig }));
	}

	public string GetExtendedDescription(string description, bool synchronizedSetting)
	{
		return description + (synchronizedSetting ? " [Synced with Server]" : " [Not Synced with Server]");
	}

	private void Awake()
	{
		AddConfig("AdminBypass", "General", "True to allow admins to bypass some setting restrictions (boolean).", synced: true, value: true, ref AdminBypass);
		AddConfig("ChanceToSaveArrowsOn", "Misc", "Turn on chance to recover arrows (bool).", synced: true, value: true, ref ChanceToSaveArrowsOn);
		AddConfig("ChanceToSaveArrowsValue", "Misc", "Chance to save arrows. 0 - 100 would represent procentage of chance to get arrows back (float).", synced: true, 50f, ref ChanceToSaveArrowsValue);
		Assembly executingAssembly = Assembly.GetExecutingAssembly();
		HarmonyInstance.PatchAll(executingAssembly);
		SetupWatcher();
	}

	private void OnDestroy()
	{
		((BaseUnityPlugin)this).Config.Save();
	}

	private void SetupWatcher()
	{
		FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(Paths.ConfigPath, ConfigFileName);
		fileSystemWatcher.Changed += ReadConfigValues;
		fileSystemWatcher.Created += ReadConfigValues;
		fileSystemWatcher.Renamed += ReadConfigValues;
		fileSystemWatcher.IncludeSubdirectories = true;
		fileSystemWatcher.SynchronizingObject = ThreadingHelper.SynchronizingObject;
		fileSystemWatcher.EnableRaisingEvents = true;
	}

	private void ReadConfigValues(object sender, FileSystemEventArgs e)
	{
		if (!File.Exists(ConfigFileFullPath))
		{
			return;
		}
		try
		{
			TebLogger.LogDebug((object)"Attempting to reload configuration...");
			((BaseUnityPlugin)this).Config.Reload();
		}
		catch
		{
			TebLogger.LogError((object)("There was an issue loading " + ConfigFileName));
		}
	}

	static TebArrowsQoL()
	{
		string configPath = Paths.ConfigPath;
		char directorySeparatorChar = Path.DirectorySeparatorChar;
		ConfigFileFullPath = configPath + directorySeparatorChar + ConfigFileName;
		TebLogger = Logger.CreateLogSource("TebArrowQoL");
		AdminBypass = null;
		ChanceToSaveArrowsOn = null;
		ChanceToSaveArrowsValue = null;
	}
}
[HarmonyPatch(typeof(Projectile), "Setup")]
internal static class GetArrowBackOnHit
{
	[HarmonyPostfix]
	private static void ChanceToGetArrowBackOnHit(Projectile __instance)
	{
		if (!TebArrowsQoL.GetChanceToSaveArrowsOn())
		{
			return;
		}
		Player localPlayer = Player.m_localPlayer;
		string name = ((Object)((Humanoid)localPlayer).GetAmmoItem().m_dropPrefab.gameObject).name;
		GameObject itemPrefab = ObjectDB.instance.GetItemPrefab(name);
		if (!name.Contains("arrow"))
		{
			return;
		}
		float chanceToSaveArrowsValue = TebArrowsQoL.GetChanceToSaveArrowsValue();
		if (chanceToSaveArrowsValue != 100f)
		{
			Random random = new Random();
			int num = random.Next(0, 101);
			if (chanceToSaveArrowsValue < (float)num)
			{
				__instance.m_spawnOnHit = itemPrefab;
			}
		}
		else
		{
			__instance.m_spawnOnHit = itemPrefab;
		}
	}
}