Decompiled source of EWP Player Reward v1.1.0

plugins/CoinReward.dll

Decompiled a month ago
using System;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Bootstrap;
using EWP;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("CoinReward")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("CoinReward")]
[assembly: AssemblyTitle("CoinReward")]
[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.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 EWP
{
	public static class Api
	{
		public const string GUID = "expand_world_prefabs";

		private static bool isSetup;

		private static MethodInfo? registerValueFunctionHandlerMethod;

		private static void SetupIfNeeded()
		{
			if (isSetup)
			{
				return;
			}
			isSetup = true;
			if (Chainloader.PluginInfos.TryGetValue("expand_world_prefabs", out var value))
			{
				Assembly assembly = ((object)value.Instance).GetType().Assembly;
				Type type = assembly.GetType("ExpandWorld.Prefab.Api");
				if (!(type == null))
				{
					registerValueFunctionHandlerMethod = AccessTools.Method(type, "RegisterValueFunctionHandler", new Type[2]
					{
						typeof(string),
						typeof(Func<string, string>)
					}, (Type[])null);
				}
			}
		}

		public static bool AddValueFunction(string key, Func<string, string> handler)
		{
			SetupIfNeeded();
			if (registerValueFunctionHandlerMethod == null)
			{
				return false;
			}
			registerValueFunctionHandlerMethod.Invoke(null, new object[2] { key, handler });
			return true;
		}
	}
}
namespace CoinReward
{
	[BepInPlugin("coinreward.ewp", "Coin Reward", "1.1.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class CoinRewardPlugin : BaseUnityPlugin
	{
		private void Awake()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"CoinReward Awake BEGIN");
			try
			{
				bool flag = Api.AddValueFunction("PlayerReward", PlayerReward);
				bool flag2 = Api.AddValueFunction("GoblinReward", GoblinReward);
				((BaseUnityPlugin)this).Logger.LogInfo((object)$"PlayerReward registered: {flag}");
				((BaseUnityPlugin)this).Logger.LogInfo((object)$"GoblinReward registered: {flag2}");
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)ex);
			}
		}

		private string PlayerReward(string value)
		{
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			if (!TryParseArguments(value, out var position, out var radius, out var baseReward, out var level, out var bonus, out var levelBonus))
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("PlayerReward: invalid arguments: " + value));
				return "0";
			}
			int num = 0;
			foreach (Player allPlayer in Player.GetAllPlayers())
			{
				if (!((Object)(object)allPlayer == (Object)null))
				{
					float num2 = Vector3.Distance(position, ((Component)allPlayer).transform.position);
					if (num2 <= radius)
					{
						num++;
					}
				}
			}
			int num3 = CalculateReward(num, baseReward, level, bonus, levelBonus);
			((BaseUnityPlugin)this).Logger.LogInfo((object)("PlayerReward | " + $"players={num} | " + $"radius={radius} | " + $"base={baseReward} | " + $"level={level} | " + $"bonus={bonus} | " + $"levelBonus={levelBonus} | " + $"reward={num3}"));
			return num3.ToString(CultureInfo.InvariantCulture);
		}

		private string GoblinReward(string value)
		{
			//IL_0094: 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)
			if (!TryParseArguments(value, out var position, out var radius, out var baseReward, out var level, out var bonus, out var levelBonus))
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("GoblinReward: invalid arguments: " + value));
				return "0";
			}
			int num = 0;
			Character[] array = Object.FindObjectsByType<Character>((FindObjectsSortMode)0);
			Character[] array2 = array;
			foreach (Character val in array2)
			{
				if ((Object)(object)val == (Object)null)
				{
					continue;
				}
				string name = ((Object)((Component)val).gameObject).name;
				if (name.StartsWith("Goblin", StringComparison.Ordinal))
				{
					float num2 = Vector3.Distance(position, ((Component)val).transform.position);
					if (num2 <= radius)
					{
						num++;
					}
				}
			}
			int num3 = CalculateReward(num, baseReward, level, bonus, levelBonus);
			((BaseUnityPlugin)this).Logger.LogInfo((object)("GoblinReward | " + $"goblins={num} | " + $"radius={radius} | " + $"base={baseReward} | " + $"level={level} | " + $"bonus={bonus} | " + $"levelBonus={levelBonus} | " + $"reward={num3}"));
			return num3.ToString(CultureInfo.InvariantCulture);
		}

		private static int CalculateReward(int count, int baseReward, int level, float bonus, float levelBonus)
		{
			count = Mathf.Max(1, count);
			level = Mathf.Max(1, level);
			float num = (float)baseReward * (1f + (float)(level - 1) * levelBonus);
			float num2 = 1f + bonus * (float)(count - 1);
			return Mathf.FloorToInt(num * num2 + 0.5f);
		}

		private static bool TryParseArguments(string value, out Vector3 position, out float radius, out int baseReward, out int level, out float bonus, out float levelBonus)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0127: Unknown result type (might be due to invalid IL or missing references)
			//IL_012c: Unknown result type (might be due to invalid IL or missing references)
			position = Vector3.zero;
			radius = 0f;
			baseReward = 0;
			level = 1;
			bonus = 0f;
			levelBonus = 1f;
			string[] array = value.Split(':');
			if (array.Length != 8)
			{
				return false;
			}
			if (!TryFloat(array[0], out var result))
			{
				return false;
			}
			if (!TryFloat(array[1], out var result2))
			{
				return false;
			}
			if (!TryFloat(array[2], out var result3))
			{
				return false;
			}
			if (!TryFloat(array[3], out radius))
			{
				return false;
			}
			if (!int.TryParse(array[4], NumberStyles.Integer, CultureInfo.InvariantCulture, out baseReward))
			{
				return false;
			}
			if (!int.TryParse(array[5], NumberStyles.Integer, CultureInfo.InvariantCulture, out level))
			{
				return false;
			}
			if (!TryFloat(array[6], out bonus))
			{
				return false;
			}
			if (!TryFloat(array[7], out levelBonus))
			{
				return false;
			}
			position = new Vector3(result, result2, result3);
			return true;
		}

		private static bool TryFloat(string value, out float result)
		{
			return float.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out result);
		}
	}
}