Decompiled source of QuotaReward v2.0.0

QuotaReward.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using GameNetcodeStuff;
using HarmonyLib;
using LethalNetworkAPI;
using Unity.Netcode;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("QuotaReward")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("QuotaReward")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("67e3800e-2ee1-4fef-99f9-10ee153408ff")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
[BepInPlugin("finishim.quotareward", "QuotaReward", "2.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class QuotaReward : BaseUnityPlugin
{
	internal class ShieldState
	{
		public int Max;

		public int Current;

		public int LastSeenQuota;

		public int LastSeenDay;

		public bool Loaded;
	}

	public static QuotaReward Instance;

	internal static ConfigEntry<int> ShieldPerQuota;

	internal static ConfigEntry<int> ShieldMaxLimit;

	internal static ConfigEntry<string> ShieldLabel;

	internal static ConfigEntry<int> HudFontSize;

	internal static ConfigEntry<float> HudPosX;

	internal static ConfigEntry<float> HudPosY;

	internal static ConfigEntry<float> HudWidth;

	internal static ConfigEntry<float> HudColorR;

	internal static ConfigEntry<float> HudColorG;

	internal static ConfigEntry<float> HudColorB;

	internal static Dictionary<ulong, ShieldState> Shields = new Dictionary<ulong, ShieldState>();

	private static LNetworkVariable<int> hostShieldMax;

	internal static float shieldDamageFlashUntil = 0f;

	private void Awake()
	{
		//IL_016d: Unknown result type (might be due to invalid IL or missing references)
		Instance = this;
		ShieldPerQuota = ((BaseUnityPlugin)this).Config.Bind<int>("Shield", "ShieldPerQuota", 25, "Shield gained per completed quota.");
		ShieldMaxLimit = ((BaseUnityPlugin)this).Config.Bind<int>("Shield", "ShieldMaxLimit", 500, "Maximum shield allowed.");
		ShieldLabel = ((BaseUnityPlugin)this).Config.Bind<string>("HUD", "ShieldLabel", "Shield", "HUD text label (max 10 chars).");
		HudFontSize = ((BaseUnityPlugin)this).Config.Bind<int>("HUD", "FontSize", 18, "HUD font size.");
		HudPosX = ((BaseUnityPlugin)this).Config.Bind<float>("HUD", "PosX", 0.02f, "HUD X position (0–1).");
		HudPosY = ((BaseUnityPlugin)this).Config.Bind<float>("HUD", "PosY", 0.08f, "HUD Y position (0–1).");
		HudWidth = ((BaseUnityPlugin)this).Config.Bind<float>("HUD", "Width", 300f, "HUD width in pixels.");
		HudColorR = ((BaseUnityPlugin)this).Config.Bind<float>("HUD", "ColorR", 0.4f, "HUD color RED (0–1).");
		HudColorG = ((BaseUnityPlugin)this).Config.Bind<float>("HUD", "ColorG", 0.8f, "HUD color GREEN (0–1).");
		HudColorB = ((BaseUnityPlugin)this).Config.Bind<float>("HUD", "ColorB", 1f, "HUD color BLUE (0–1).");
		new Harmony("finishim.quotareward").PatchAll();
		hostShieldMax = LNetworkVariable<int>.Connect("QuotaReward_HostShieldMax", 0, (LNetworkVariableWritePerms)0, (Action<int, int>)null);
		Debug.Log((object)"[QuotaReward] Mod loaded successfully! Shield system ready.");
		Debug.Log((object)$"[QuotaReward] Configuration: ShieldPerQuota = {ShieldPerQuota.Value}, MaxLimit = {ShieldMaxLimit.Value}");
	}

	private void Update()
	{
		if (!Object.op_Implicit((Object)(object)StartOfRound.Instance) || !Object.op_Implicit((Object)(object)TimeOfDay.Instance))
		{
			return;
		}
		PlayerControllerB localPlayerController = StartOfRound.Instance.localPlayerController;
		if (!((Object)(object)localPlayerController == (Object)null))
		{
			if (GameNetworkManager.Instance.isHostingGame)
			{
				UpdateShieldFromQuota(localPlayerController.playerClientId);
			}
			SyncShieldFromHost(localPlayerController.playerClientId);
			HandleDayReset(localPlayerController.playerClientId);
		}
	}

	private void UpdateShieldFromQuota(ulong clientId)
	{
		if (GameNetworkManager.Instance.isHostingGame)
		{
			ShieldState shield = GetShield(clientId);
			int timesFulfilledQuota = TimeOfDay.Instance.timesFulfilledQuota;
			if (shield.LastSeenQuota != timesFulfilledQuota)
			{
				shield.LastSeenQuota = timesFulfilledQuota;
				shield.Current = (shield.Max = Mathf.Clamp(timesFulfilledQuota * ShieldPerQuota.Value, 0, ShieldMaxLimit.Value));
				SaveShield(clientId);
				Debug.Log((object)("[QuotaReward] Quota reward → " + $"Quota: {timesFulfilledQuota} | Max: {shield.Max} | ClientId: {clientId}"));
			}
			if (hostShieldMax != null && hostShieldMax.Value != shield.Max)
			{
				hostShieldMax.Value = shield.Max;
			}
		}
	}

	private void SyncShieldFromHost(ulong clientId)
	{
		if (GameNetworkManager.Instance.isHostingGame)
		{
			return;
		}
		ShieldState shield = GetShield(clientId);
		if (hostShieldMax == null)
		{
			return;
		}
		int value = hostShieldMax.Value;
		if (shield.Max != value)
		{
			int max = shield.Max;
			shield.Max = value;
			if (max == 0)
			{
				shield.Current = shield.Max;
			}
			else if (shield.Max > max)
			{
				shield.Current = shield.Max;
			}
			else if (shield.Current > shield.Max)
			{
				shield.Current = shield.Max;
			}
			SaveShield(clientId);
			Debug.Log((object)($"[QuotaReward] Client {clientId} synced shield from host: " + $"{shield.Current}/{shield.Max}"));
		}
	}

	private void HandleDayReset(ulong clientId)
	{
		ShieldState shield = GetShield(clientId);
		int daysUntilDeadline = TimeOfDay.Instance.daysUntilDeadline;
		if (daysUntilDeadline != shield.LastSeenDay)
		{
			shield.LastSeenDay = daysUntilDeadline;
			if (shield.Max > 0)
			{
				shield.Current = shield.Max;
				SaveShield(clientId);
				Debug.Log((object)$"[QuotaReward] Day reset → Shield healed | ClientId: {clientId}");
			}
		}
	}

	internal static ShieldState GetShield(ulong clientId)
	{
		if (!Shields.TryGetValue(clientId, out var value))
		{
			value = new ShieldState();
			Shields[clientId] = value;
		}
		if (!value.Loaded)
		{
			LoadShield(clientId);
		}
		return value;
	}

	private static string GetSavePath(ulong clientId)
	{
		if (!Object.op_Implicit((Object)(object)GameNetworkManager.Instance))
		{
			return null;
		}
		return Path.Combine(Application.persistentDataPath, "QuotaReward_" + GameNetworkManager.Instance.currentSaveFileName + "_" + clientId + ".txt");
	}

	internal static void SaveShield(ulong clientId)
	{
		string savePath = GetSavePath(clientId);
		if (savePath != null && Shields.TryGetValue(clientId, out var value))
		{
			File.WriteAllText(savePath, $"{value.Max}|{value.Current}|{value.LastSeenQuota}|{value.LastSeenDay}");
		}
	}

	internal static void LoadShield(ulong clientId)
	{
		if (!Shields.TryGetValue(clientId, out var value))
		{
			return;
		}
		string savePath = GetSavePath(clientId);
		if (savePath == null)
		{
			return;
		}
		if (!File.Exists(savePath))
		{
			value.Max = 0;
			value.Current = 0;
			value.LastSeenQuota = 0;
			value.LastSeenDay = 0;
			value.Loaded = true;
			return;
		}
		string[] array = File.ReadAllText(savePath).Split(new char[1] { '|' });
		value.Max = int.Parse(array[0]);
		value.Current = int.Parse(array[1]);
		value.LastSeenQuota = int.Parse(array[2]);
		value.LastSeenDay = int.Parse(array[3]);
		value.Loaded = true;
		if (value.Max > 0)
		{
			value.Current = value.Max;
		}
		if ((Object)(object)GameNetworkManager.Instance != (Object)null && GameNetworkManager.Instance.isHostingGame && value.Max > 0 && hostShieldMax != null)
		{
			hostShieldMax.Value = value.Max;
		}
	}

	private void OnGUI()
	{
		//IL_0074: Unknown result type (might be due to invalid IL or missing references)
		//IL_007a: Expected O, but got Unknown
		//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_009c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0109: Unknown result type (might be due to invalid IL or missing references)
		if (!Object.op_Implicit((Object)(object)StartOfRound.Instance) || (Object)(object)StartOfRound.Instance.localPlayerController == (Object)null || Time.timeScale == 0f)
		{
			return;
		}
		ShieldState shield = GetShield(StartOfRound.Instance.localPlayerController.playerClientId);
		if (shield.Max > 0)
		{
			string text = ShieldLabel.Value;
			if (text.Length > 10)
			{
				text = text.Substring(0, 10);
			}
			GUIStyle val = new GUIStyle(GUI.skin.label);
			val.fontSize = HudFontSize.Value;
			if (Time.time < shieldDamageFlashUntil)
			{
				val.normal.textColor = Color.red;
			}
			else
			{
				val.normal.textColor = new Color(HudColorR.Value, HudColorG.Value, HudColorB.Value);
			}
			float num = (float)Screen.width * HudPosX.Value;
			float num2 = (float)Screen.height * HudPosY.Value;
			GUI.Label(new Rect(num, num2, HudWidth.Value, 30f), $"{text}: {shield.Current}/{shield.Max}", val);
		}
	}
}
[HarmonyPatch(typeof(PlayerControllerB), "DamagePlayer")]
internal class ShieldDamagePatch
{
	private static bool Prefix(ref int damageNumber, PlayerControllerB __instance)
	{
		if (!((NetworkBehaviour)__instance).IsOwner)
		{
			return true;
		}
		if (__instance.isPlayerDead)
		{
			return true;
		}
		if (damageNumber <= 0 || damageNumber >= 999)
		{
			return true;
		}
		QuotaReward.ShieldState shield = QuotaReward.GetShield(__instance.playerClientId);
		if (shield.Current <= 0)
		{
			return true;
		}
		int num = Mathf.Min(shield.Current, damageNumber);
		shield.Current -= num;
		damageNumber -= num;
		if (shield.Current < 0)
		{
			shield.Current = 0;
		}
		QuotaReward.shieldDamageFlashUntil = Time.time + 0.5f;
		QuotaReward.SaveShield(__instance.playerClientId);
		return damageNumber > 0;
	}
}