Decompiled source of EWP Player Reward v1.0.0

plugins/CoinReward.dll

Decompiled a day 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.0.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_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: 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))
			{
				((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);
			((BaseUnityPlugin)this).Logger.LogInfo((object)("PlayerReward | " + $"players={num} | " + $"radius={radius} | " + $"base={baseReward} | " + $"level={level} | " + $"bonus={bonus} | " + $"reward={num3}"));
			return num3.ToString(CultureInfo.InvariantCulture);
		}

		private string? GoblinReward(string value)
		{
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: 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))
			{
				((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);
			((BaseUnityPlugin)this).Logger.LogInfo((object)("GoblinReward | " + $"goblins={num} | " + $"radius={radius} | " + $"base={baseReward} | " + $"level={level} | " + $"bonus={bonus} | " + $"reward={num3}"));
			return num3.ToString(CultureInfo.InvariantCulture);
		}

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

		private static bool TryParseArguments(string value, out Vector3 position, out float radius, out int baseReward, out int level, out float bonus)
		{
			//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_0101: Unknown result type (might be due to invalid IL or missing references)
			//IL_0106: Unknown result type (might be due to invalid IL or missing references)
			position = Vector3.zero;
			radius = 0f;
			baseReward = 0;
			level = 1;
			bonus = 0f;
			string[] array = value.Split(':');
			if (array.Length != 7)
			{
				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;
			}
			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);
		}
	}
}