Decompiled source of AF ShopSale v1.0.0

AF_ShopSale.dll

Decompiled 4 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using ExitGames.Client.Photon;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using Photon.Realtime;
using TMPro;
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: IgnoresAccessChecksTo("")]
[assembly: AssemblyCompany("Kuraputi")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("AF_ShopSale")]
[assembly: AssemblyTitle("AF_ShopSale")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 AF_ShopSale
{
	[BepInPlugin("Kuraputi.AF_ShopSale", "AF_ShopSale", "1.0.0")]
	public class AF_ShopSale : BaseUnityPlugin
	{
		internal static ConfigEntry<int> SaleChance;

		internal static ConfigEntry<int> CheapMin;

		internal static ConfigEntry<int> CheapMax;

		internal static ConfigEntry<int> ExpensiveMin;

		internal static ConfigEntry<int> ExpensiveMax;

		internal static AF_ShopSale Instance { get; private set; }

		internal static ManualLogSource Logger => Instance._logger;

		private ManualLogSource _logger => ((BaseUnityPlugin)this).Logger;

		internal Harmony? Harmony { get; set; }

		private void Awake()
		{
			Instance = this;
			((Component)this).gameObject.transform.parent = null;
			((Object)((Component)this).gameObject).hideFlags = (HideFlags)61;
			BindConfig();
			Patch();
			Logger.LogInfo((object)$"{((BaseUnityPlugin)this).Info.Metadata.GUID} v{((BaseUnityPlugin)this).Info.Metadata.Version} has loaded!");
		}

		private void BindConfig()
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Expected O, but got Unknown
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Expected O, but got Unknown
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Expected O, but got Unknown
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b4: Expected O, but got Unknown
			//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e0: Expected O, but got Unknown
			AcceptableValueRange<int> val = new AcceptableValueRange<int>(0, 100);
			SaleChance = ((BaseUnityPlugin)this).Config.Bind<int>("Sale", "SaleChance", 40, new ConfigDescription("セールが発生する確率(%)", (AcceptableValueBase)(object)val, Array.Empty<object>()));
			CheapMin = ((BaseUnityPlugin)this).Config.Bind<int>("Discount", "CheapDiscountMin", 10, new ConfigDescription("安い商品の割引率の下限(%)", (AcceptableValueBase)(object)val, Array.Empty<object>()));
			CheapMax = ((BaseUnityPlugin)this).Config.Bind<int>("Discount", "CheapDiscountMax", 40, new ConfigDescription("安い商品の割引率の上限(%)", (AcceptableValueBase)(object)val, Array.Empty<object>()));
			ExpensiveMin = ((BaseUnityPlugin)this).Config.Bind<int>("Discount", "ExpensiveDiscountMin", 40, new ConfigDescription("高い商品の割引率の下限(%)", (AcceptableValueBase)(object)val, Array.Empty<object>()));
			ExpensiveMax = ((BaseUnityPlugin)this).Config.Bind<int>("Discount", "ExpensiveDiscountMax", 70, new ConfigDescription("高い商品の割引率の上限(%)", (AcceptableValueBase)(object)val, Array.Empty<object>()));
		}

		internal void Patch()
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Expected O, but got Unknown
			//IL_0025: Expected O, but got Unknown
			if (Harmony == null)
			{
				Harmony val = new Harmony(((BaseUnityPlugin)this).Info.Metadata.GUID);
				Harmony val2 = val;
				Harmony = val;
			}
			Harmony.PatchAll();
		}

		internal void Unpatch()
		{
			Harmony? harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}

		private void Update()
		{
		}
	}
	[HarmonyPatch(typeof(WorldSpaceUIValue), "Show", new Type[]
	{
		typeof(PhysGrabObject),
		typeof(int),
		typeof(bool),
		typeof(Vector3)
	})]
	internal static class SaleLabelPatch
	{
		private static void Postfix(WorldSpaceUIValue __instance, PhysGrabObject _grabObject, int _value, bool _cost)
		{
			if (_cost && !((Object)(object)__instance.currentPhysGrabObject != (Object)(object)_grabObject))
			{
				ItemAttributes component = ((Component)_grabObject).GetComponent<ItemAttributes>();
				if (!((Object)(object)component == (Object)null) && ShopSalePatch.TryGetSale(component, _value, out var original, out var rate))
				{
					string text = SemiFunc.DollarGetString(original);
					string text2 = ((_value <= 0) ? "FREE" : ("-$" + SemiFunc.DollarGetString(_value) + "K"));
					((TMP_Text)__instance.text).enableWordWrapping = false;
					((TMP_Text)__instance.text).overflowMode = (TextOverflowModes)0;
					((TMP_Text)__instance.text).text = "<line-height=80%>" + $"<size=60%><color=#4CFF4C>SALE -{rate}%</color></size>\n" + "<size=70%><color=#AAAAAA><s>-$" + text + "K</s></color></size> " + text2;
				}
			}
		}
	}
	[HarmonyPatch(typeof(ItemAttributes))]
	internal static class ShopSalePatch
	{
		private static List<float>? baseValues;

		internal static readonly Dictionary<ItemAttributes, (int original, int rate, int viewId)> SaleInfos = new Dictionary<ItemAttributes, (int, int, int)>();

		[HarmonyPostfix]
		[HarmonyPatch("GetValue")]
		private static void GetValue_Postfix(ItemAttributes __instance)
		{
			//IL_0172: Unknown result type (might be due to invalid IL or missing references)
			//IL_0179: Expected O, but got Unknown
			if (!SemiFunc.IsMasterClientOrSingleplayer() || !__instance.shopItem)
			{
				return;
			}
			int value = __instance.value;
			if (Random.Range(0, 100) >= AF_ShopSale.SaleChance.Value)
			{
				return;
			}
			float pricePos = GetPricePos(__instance.item);
			float num = Mathf.Lerp((float)AF_ShopSale.CheapMin.Value, (float)AF_ShopSale.ExpensiveMin.Value, pricePos);
			float num2 = Mathf.Lerp((float)AF_ShopSale.CheapMax.Value, (float)AF_ShopSale.ExpensiveMax.Value, pricePos);
			if (num > num2)
			{
				float num3 = num2;
				num2 = num;
				num = num3;
			}
			int num4 = Mathf.Clamp(Mathf.RoundToInt(Random.Range(num, num2) / 10f) * 10, 0, 100);
			int num5 = Mathf.Max(0, Mathf.RoundToInt((float)value * (1f - (float)num4 / 100f)));
			if (num5 < value)
			{
				int num6 = Mathf.RoundToInt((1f - (float)num5 / (float)value) * 100f);
				__instance.value = num5;
				if (SemiFunc.IsMultiplayer() && (Object)(object)__instance.photonView != (Object)null && __instance.photonView.ViewID != 0)
				{
					__instance.photonView.RPC("GetValueRPC", (RpcTarget)1, new object[1] { num5 });
				}
				RemoveOldSales();
				int num7 = (((Object)(object)__instance.photonView != (Object)null) ? __instance.photonView.ViewID : 0);
				SaleInfos[__instance] = (value, num6, num7);
				if (SemiFunc.IsMultiplayer() && PhotonNetwork.InRoom && num7 != 0)
				{
					Room currentRoom = PhotonNetwork.CurrentRoom;
					Hashtable val = new Hashtable();
					((Dictionary<object, object>)(object)val).Add((object)RoomKey(num7), (object)new int[3] { value, num5, num6 });
					currentRoom.SetCustomProperties(val, (Hashtable)null, (WebFlags)null);
				}
			}
		}

		private static string RoomKey(int viewId)
		{
			return $"AFShopSale.{viewId}";
		}

		private static void RemoveOldSales()
		{
			//IL_0076: 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_0088: Expected O, but got Unknown
			//IL_008f: Expected O, but got Unknown
			foreach (ItemAttributes item2 in SaleInfos.Keys.Where((ItemAttributes k) => (Object)(object)k == (Object)null).ToList())
			{
				int item = SaleInfos[item2].viewId;
				SaleInfos.Remove(item2);
				if (SemiFunc.IsMultiplayer() && PhotonNetwork.InRoom && item != 0)
				{
					Room currentRoom = PhotonNetwork.CurrentRoom;
					Hashtable val = new Hashtable();
					((Dictionary<object, object>)val).Add((object)RoomKey(item), (object)null);
					currentRoom.SetCustomProperties(val, (Hashtable)null, (WebFlags)null);
				}
			}
		}

		internal static bool TryGetSale(ItemAttributes item, int currentValue, out int original, out int rate)
		{
			original = 0;
			rate = 0;
			if (SaleInfos.TryGetValue(item, out (int, int, int) value))
			{
				(original, rate, _) = value;
				return true;
			}
			if (!SemiFunc.IsMultiplayer() || !PhotonNetwork.InRoom || (Object)(object)item.photonView == (Object)null)
			{
				return false;
			}
			if (((Dictionary<object, object>)(object)((RoomInfo)PhotonNetwork.CurrentRoom).CustomProperties).TryGetValue((object)RoomKey(item.photonView.ViewID), out object value2) && value2 is int[] array && array.Length == 3 && array[1] == currentValue)
			{
				original = array[0];
				rate = array[2];
				return true;
			}
			return false;
		}

		private static float GetPricePos(Item item)
		{
			if (baseValues == null)
			{
				baseValues = (from i in StatsManager.instance.itemDictionary.Values
					select i.value.valueMax into v
					orderby v
					select v).ToList();
			}
			if (baseValues.Count <= 1)
			{
				return 0f;
			}
			int num = baseValues.FindLastIndex((float v) => v <= item.value.valueMax);
			return Mathf.Clamp01((float)num / (float)(baseValues.Count - 1));
		}
	}
}