Decompiled source of XpBuy v0.2.1

XpBuy.dll

Decompiled 5 days ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using LethalRPG;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("XpBuy")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.2.1.0")]
[assembly: AssemblyInformationalVersion("0.2.1")]
[assembly: AssemblyProduct("XpBuy")]
[assembly: AssemblyTitle("XpBuy")]
[assembly: AssemblyMetadata("AI_Assisted_Creation", "This assembly was partially or fully created with the assistance of Generative AI (e.g., Code Suggestions, Refactoring, Documentation Generation).")]
[assembly: AssemblyMetadata("AI_Model_Vendor", "DeepSeek")]
[assembly: AssemblyVersion("0.2.1.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 XpBuy
{
	[BepInPlugin("Kasablanca.XpBuy", "XpBuy", "0.2.1")]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "Kasablanca.XpBuy";

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> Enabled;

		internal static ConfigEntry<float> Ratio;

		internal static ConfigEntry<bool> SaveOnQuit;

		internal static ConfigEntry<int> AutosaveSeconds;

		private float _nextAutosave;

		private void Awake()
		{
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Buy LethalRPG upgrades with XP instead of money.");
			Ratio = ((BaseUnityPlugin)this).Config.Bind<float>("General", "XpPerMoneyRatio", 4f, "XP price = money price / this. 4 means a $40 level costs 10 XP.");
			SaveOnQuit = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "SaveOnQuit", true, "Write LethalRPG progress to the save file when leaving a game, even when you are not on the ship. Without this, a day's XP is lost if you quit from inside the facility.");
			AutosaveSeconds = ((BaseUnityPlugin)this).Config.Bind<int>("General", "AutosaveSeconds", 60, "While you host, write LethalRPG progress to the save file every N seconds. 0 turns it off. Protects against a hard crash or a forced close.");
			try
			{
				new Harmony("Kasablanca.XpBuy").PatchAll(typeof(Plugin).Assembly);
				Log.LogInfo((object)("XpBuy 0.2.0 loaded (ratio " + Ratio.Value + ", save on quit " + SaveOnQuit.Value + ", autosave " + AutosaveSeconds.Value + "s)."));
				Log.LogInfo((object)("LethalRPG save hook: " + RpgSaveBridge.Describe()));
			}
			catch (Exception ex)
			{
				Log.LogError((object)("XpBuy patch failed: " + ex));
			}
		}

		private void Update()
		{
			int value = AutosaveSeconds.Value;
			if (value > 0 && !(Time.realtimeSinceStartup < _nextAutosave))
			{
				_nextAutosave = Time.realtimeSinceStartup + (float)value;
				GameNetworkManager instance = GameNetworkManager.Instance;
				if (!((Object)(object)instance == (Object)null) && instance.isHostingGame && !((Object)(object)StartOfRound.Instance == (Object)null))
				{
					RpgSaveBridge.Save("autosave", quiet: true);
				}
			}
		}
	}
	internal static class RpgSaveBridge
	{
		private static MethodInfo _save;

		private static float _last = -100f;

		internal static string Describe()
		{
			try
			{
				if (_save == null)
				{
					Resolve();
				}
				return (_save == null) ? "LethalRPG.RpgServer.Save NOT found - progress saving is disabled" : ("LethalRPG.RpgServer.Save resolved (" + (_save.IsPublic ? "public" : "non-public") + " static)");
			}
			catch (Exception ex)
			{
				return "lookup failed: " + ex.Message;
			}
		}

		private static void Resolve()
		{
			Type type = AccessTools.TypeByName("LethalRPG.RpgServer");
			if (!(type == null))
			{
				_save = type.GetMethod("Save", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
			}
		}

		internal static void Save(string why, bool quiet = false)
		{
			if (!Plugin.SaveOnQuit.Value || Time.realtimeSinceStartup - _last < 2f)
			{
				return;
			}
			try
			{
				if (_save == null)
				{
					Resolve();
					if (_save == null)
					{
						Plugin.Log.LogWarning((object)"LethalRPG.RpgServer.Save was not found.");
						return;
					}
				}
				GameNetworkManager instance = GameNetworkManager.Instance;
				if (!((Object)(object)instance == (Object)null) && instance.isHostingGame)
				{
					string currentSaveFileName = instance.currentSaveFileName;
					_save.Invoke(null, null);
					_last = Time.realtimeSinceStartup;
					string text = "LethalRPG progress saved to " + currentSaveFileName + " on " + why + ".";
					if (quiet)
					{
						Plugin.Log.LogDebug((object)text);
					}
					else
					{
						Plugin.Log.LogInfo((object)text);
					}
				}
			}
			catch (Exception ex)
			{
				Plugin.Log.LogWarning((object)("Saving on " + why + " failed: " + ex.Message));
			}
		}
	}
	[HarmonyPatch(typeof(GameNetworkManager), "OnApplicationQuit")]
	internal static class QuitPatch
	{
		[HarmonyPrefix]
		private static void Prefix()
		{
			RpgSaveBridge.Save("quit");
		}
	}
	[HarmonyPatch(typeof(GameNetworkManager), "Disconnect")]
	internal static class DisconnectPatch
	{
		[HarmonyPrefix]
		private static void Prefix()
		{
			RpgSaveBridge.Save("disconnect");
		}
	}
	[HarmonyPatch(typeof(RpgSettings), "PaysWithXp")]
	internal static class PaysWithXpPatch
	{
		[HarmonyPostfix]
		private static void Postfix(UpgradeType type, ref bool __result)
		{
			if (Plugin.Enabled.Value && !__result)
			{
				__result = true;
			}
		}
	}
	[HarmonyPatch(typeof(RpgSettings), "XpPrice")]
	internal static class XpPricePatch
	{
		[HarmonyPostfix]
		private static void Postfix(UpgradeType type, RpgSettings __instance, ref int __result)
		{
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			if (!Plugin.Enabled.Value || __result > 0)
			{
				return;
			}
			try
			{
				PlayerControllerB val = (((Object)(object)GameNetworkManager.Instance != (Object)null) ? GameNetworkManager.Instance.localPlayerController : null);
				if ((Object)(object)val == (Object)null)
				{
					return;
				}
				PlayerProgress orCreateProgress = RpgState.GetOrCreateProgress(val);
				if (orCreateProgress != null)
				{
					int level = orCreateProgress.GetLevel(type);
					int num = RpgState.Price(type, level);
					if (num > 0)
					{
						float num2 = Mathf.Max(0.5f, Plugin.Ratio.Value);
						__result = Mathf.Max(1, Mathf.RoundToInt((float)num / num2));
					}
				}
			}
			catch (Exception ex)
			{
				Plugin.Log.LogWarning((object)("XpPrice patch: " + ex.Message));
			}
		}
	}
}