Decompiled source of WildSands v0.1.1

WildSands.dll

Decompiled 3 weeks ago
using System;
using System.Collections.Concurrent;
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.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
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(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("WildSands")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("WildSands revamps the desert map to be more crazy and fun - Increases Cactus spawns by ALOT , increases their knockback, and adds extra gravity if you are too high, to help keep you grounded.")]
[assembly: AssemblyFileVersion("0.1.1.0")]
[assembly: AssemblyInformationalVersion("0.1.1")]
[assembly: AssemblyProduct("WildSands")]
[assembly: AssemblyTitle("WildSands")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.1.1.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 WildSands
{
	[BepInPlugin("BedlessSleeper_WildSands", "WildSands", "0.1.1")]
	public class Plugin : BasePlugin
	{
		private static class Il2CppGetters
		{
			private static readonly MethodInfo GenericGetComponent = typeof(GameObject).GetMethods(BindingFlags.Instance | BindingFlags.Public).FirstOrDefault((MethodInfo m) => m.Name == "GetComponent" && m.IsGenericMethodDefinition && m.GetParameters().Length == 0);

			private static readonly MethodInfo GenericGetComponentInChildren = typeof(GameObject).GetMethods(BindingFlags.Instance | BindingFlags.Public).FirstOrDefault((MethodInfo m) => m.Name == "GetComponentInChildren" && m.IsGenericMethodDefinition && m.GetParameters().Length == 1 && m.GetParameters()[0].ParameterType == typeof(bool));

			public static object GetComponentByType(GameObject go, Type type)
			{
				//IL_0059: Unknown result type (might be due to invalid IL or missing references)
				//IL_0060: Expected O, but got Unknown
				if ((Object)(object)go == (Object)null || type == null || GenericGetComponent == null)
				{
					return null;
				}
				try
				{
					return GenericGetComponent.MakeGenericMethod(type).Invoke(go, null);
				}
				catch (Exception ex)
				{
					ManualLogSource log = Log;
					if (log != null)
					{
						bool flag = default(bool);
						BepInExDebugLogInterpolatedStringHandler val = new BepInExDebugLogInterpolatedStringHandler(27, 1, ref flag);
						if (flag)
						{
							((BepInExLogInterpolatedStringHandler)val).AppendLiteral("GetComponentByType failed: ");
							((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.Message);
						}
						log.LogDebug(val);
					}
					return null;
				}
			}

			public static object GetComponentInChildrenByType(GameObject go, Type type, bool includeInactive = true)
			{
				//IL_0067: Unknown result type (might be due to invalid IL or missing references)
				//IL_006e: Expected O, but got Unknown
				if ((Object)(object)go == (Object)null || type == null || GenericGetComponentInChildren == null)
				{
					return null;
				}
				try
				{
					return GenericGetComponentInChildren.MakeGenericMethod(type).Invoke(go, new object[1] { includeInactive });
				}
				catch (Exception ex)
				{
					ManualLogSource log = Log;
					if (log != null)
					{
						bool flag = default(bool);
						BepInExDebugLogInterpolatedStringHandler val = new BepInExDebugLogInterpolatedStringHandler(37, 1, ref flag);
						if (flag)
						{
							((BepInExLogInterpolatedStringHandler)val).AppendLiteral("GetComponentInChildrenByType failed: ");
							((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.Message);
						}
						log.LogDebug(val);
					}
					return null;
				}
			}
		}

		private static class TypeHelper
		{
			private static readonly ConcurrentDictionary<string, Type> Cache = new ConcurrentDictionary<string, Type>(StringComparer.Ordinal);

			public static Type SafeFindType(string typeName)
			{
				if (string.IsNullOrEmpty(typeName))
				{
					return null;
				}
				if (Cache.TryGetValue(typeName, out var value))
				{
					return value;
				}
				Type type = Type.GetType(typeName, throwOnError: false);
				if (type != null)
				{
					Cache[typeName] = type;
					return type;
				}
				Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
				foreach (Assembly assembly in assemblies)
				{
					try
					{
						Type[] types = assembly.GetTypes();
						Type type2 = Array.Find(types, (Type x) => x != null && (x.Name == typeName || x.FullName == typeName));
						if (type2 != null)
						{
							Cache[typeName] = type2;
							return type2;
						}
					}
					catch (ReflectionTypeLoadException ex)
					{
						Type[] types2 = ex.Types;
						if (types2 != null)
						{
							Type type3 = Array.Find(types2, (Type x) => x != null && (x.Name == typeName || x.FullName == typeName));
							if (type3 != null)
							{
								Cache[typeName] = type3;
								return type3;
							}
						}
					}
					catch
					{
					}
				}
				Cache[typeName] = null;
				return null;
			}
		}

		[HarmonyPatch]
		private static class GameManager_Start_Patch
		{
			private static MethodBase TargetMethod()
			{
				Type type = TypeHelper.SafeFindType("GameManager");
				if (type == null)
				{
					return null;
				}
				return AccessTools.Method(type, "Start", (Type[])null, (Type[])null);
			}

			private static void Postfix()
			{
				//IL_0069: Unknown result type (might be due to invalid IL or missing references)
				//IL_0070: Expected O, but got Unknown
				try
				{
					GameObject val = GameObject.Find("Player");
					if ((Object)(object)val == (Object)null)
					{
						Log.LogWarning((object)"Player GameObject not found or not Desert Stage");
						return;
					}
					playerRb = val.GetComponent<Rigidbody>();
					if ((Object)(object)playerRb == (Object)null)
					{
						Log.LogWarning((object)"Player has no Rigidbody.");
					}
				}
				catch (Exception ex)
				{
					ManualLogSource log = Log;
					bool flag = default(bool);
					BepInExErrorLogInterpolatedStringHandler val2 = new BepInExErrorLogInterpolatedStringHandler(33, 1, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("GameManager.Start postfix error: ");
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<Exception>(ex);
					}
					log.LogError(val2);
				}
			}
		}

		[HarmonyPatch]
		public static class GameManager_UpdatePatch
		{
			private static MethodBase TargetMethod()
			{
				Type type = AccessTools.TypeByName("GameManager");
				return AccessTools.Method(type, "Update", (Type[])null, (Type[])null);
			}

			private static void Postfix()
			{
				//IL_0105: Unknown result type (might be due to invalid IL or missing references)
				//IL_010c: Expected O, but got Unknown
				//IL_0047: Unknown result type (might be due to invalid IL or missing references)
				//IL_004c: Unknown result type (might be due to invalid IL or missing references)
				//IL_004d: Unknown result type (might be due to invalid IL or missing references)
				//IL_005a: Unknown result type (might be due to invalid IL or missing references)
				//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
				//IL_0094: Unknown result type (might be due to invalid IL or missing references)
				//IL_0099: Unknown result type (might be due to invalid IL or missing references)
				//IL_009b: Unknown result type (might be due to invalid IL or missing references)
				//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
				//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
				//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
				if ((Object)(object)Plugin.playerRb == (Object)null)
				{
					return;
				}
				try
				{
					PropertyInfo property = ((object)Plugin.playerRb).GetType().GetProperty("worldCenterOfMass");
					if (!(property == null))
					{
						Vector3 val = (Vector3)property.GetValue(Plugin.playerRb);
						if (val.y > lowerHeight && val.y < upperHeight)
						{
							Vector3 val2 = default(Vector3);
							((Vector3)(ref val2))..ctor(0f, (0f - gravityStrength1) * Time.deltaTime, 0f);
							Rigidbody playerRb = Plugin.playerRb;
							playerRb.velocity += val2;
						}
						else if (val.y > upperHeight)
						{
							Vector3 val3 = default(Vector3);
							((Vector3)(ref val3))..ctor(0f, (0f - gravityStrength2) * Time.deltaTime, 0f);
							Rigidbody playerRb2 = Plugin.playerRb;
							playerRb2.velocity += val3;
						}
					}
				}
				catch (Exception ex)
				{
					ManualLogSource log = Log;
					bool flag = default(bool);
					BepInExErrorLogInterpolatedStringHandler val4 = new BepInExErrorLogInterpolatedStringHandler(39, 1, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val4).AppendLiteral("GameManager.UpdatePatch Postfix error: ");
						((BepInExLogInterpolatedStringHandler)val4).AppendFormatted<Exception>(ex);
					}
					log.LogError(val4);
				}
			}
		}

		[HarmonyPatch]
		private static class RandomMapObject_GetAmount_Patch
		{
			private static readonly HashSet<object> patchedInstances = new HashSet<object>();

			private static MethodBase TargetMethod()
			{
				Type type = TypeHelper.SafeFindType("RandomMapObject");
				if (type == null)
				{
					return null;
				}
				return AccessTools.Method(type, "GetAmount", (Type[])null, (Type[])null);
			}

			private static void Postfix(object __instance, ref int __result)
			{
				//IL_0183: Unknown result type (might be due to invalid IL or missing references)
				//IL_018a: Expected O, but got Unknown
				if (__instance == null)
				{
					return;
				}
				try
				{
					if (patchedInstances.Contains(__instance))
					{
						return;
					}
					PropertyInfo property = __instance.GetType().GetProperty("prefabs");
					if (property == null || !(property.GetValue(__instance) is IEnumerable<GameObject> enumerable))
					{
						return;
					}
					foreach (GameObject item in enumerable)
					{
						if ((Object)(object)item == (Object)null)
						{
							continue;
						}
						string name = ((Object)item).name;
						if (CactusNames.Contains(name))
						{
							PropertyInfo property2 = __instance.GetType().GetProperty("amount");
							PropertyInfo property3 = __instance.GetType().GetProperty("maxAmount");
							if (!(property2 == null) && !(property3 == null))
							{
								int num = (int)property2.GetValue(__instance);
								num += 1000;
								property2.SetValue(__instance, num);
								int num2 = num + 1;
								property3.SetValue(__instance, num2);
								__result = num;
								TrySetCactusKnockbackOnObject(item, 50f, 1f, name);
								patchedInstances.Add(__instance);
							}
						}
					}
				}
				catch (Exception ex)
				{
					ManualLogSource log = Log;
					bool flag = default(bool);
					BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(41, 1, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("RandomMapObject.GetAmount postfix error: ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<Exception>(ex);
					}
					log.LogError(val);
				}
			}

			private static void TrySetCactusKnockbackOnObject(GameObject target, float knockback, float damage, string friendlyName)
			{
				//IL_0214: Unknown result type (might be due to invalid IL or missing references)
				//IL_021b: Expected O, but got Unknown
				//IL_0057: Unknown result type (might be due to invalid IL or missing references)
				//IL_005e: Expected O, but got Unknown
				//IL_0112: Unknown result type (might be due to invalid IL or missing references)
				//IL_0119: Expected O, but got Unknown
				//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
				//IL_00bb: Expected O, but got Unknown
				//IL_01c9: Unknown result type (might be due to invalid IL or missing references)
				//IL_01d0: Expected O, but got Unknown
				//IL_016b: Unknown result type (might be due to invalid IL or missing references)
				//IL_0172: Expected O, but got Unknown
				bool flag = default(bool);
				try
				{
					Type type = TypeHelper.SafeFindType("DamagePlayerCollider");
					if (type == null)
					{
						Log.LogDebug((object)"DamagePlayerCollider type not found");
						return;
					}
					object obj = Il2CppGetters.GetComponentByType(target, type) ?? Il2CppGetters.GetComponentInChildrenByType(target, type);
					if (obj == null)
					{
						ManualLogSource log = Log;
						BepInExDebugLogInterpolatedStringHandler val = new BepInExDebugLogInterpolatedStringHandler(75, 1, ref flag);
						if (flag)
						{
							((BepInExLogInterpolatedStringHandler)val).AppendLiteral("DamagePlayerCollider not present on prefab '");
							((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(friendlyName);
							((BepInExLogInterpolatedStringHandler)val).AppendLiteral("'. Instances may still have it.");
						}
						log.LogDebug(val);
						return;
					}
					if (TrySetFloatMember(obj, type, "knockbackForce", knockback))
					{
						ManualLogSource log2 = Log;
						BepInExInfoLogInterpolatedStringHandler val2 = new BepInExInfoLogInterpolatedStringHandler(44, 2, ref flag);
						if (flag)
						{
							((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("Set ");
							((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(friendlyName);
							((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(".DamagePlayerCollider.knockbackForce = ");
							((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<float>(knockback);
							((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(".");
						}
						log2.LogInfo(val2);
					}
					else
					{
						ManualLogSource log3 = Log;
						BepInExDebugLogInterpolatedStringHandler val = new BepInExDebugLogInterpolatedStringHandler(64, 1, ref flag);
						if (flag)
						{
							((BepInExLogInterpolatedStringHandler)val).AppendLiteral("DamagePlayerCollider on '");
							((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(friendlyName);
							((BepInExLogInterpolatedStringHandler)val).AppendLiteral("' has no float 'knockbackForce' member.");
						}
						log3.LogDebug(val);
					}
					if (TrySetFloatMember(obj, type, "damage", damage))
					{
						ManualLogSource log4 = Log;
						BepInExInfoLogInterpolatedStringHandler val2 = new BepInExInfoLogInterpolatedStringHandler(36, 2, ref flag);
						if (flag)
						{
							((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("Set ");
							((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(friendlyName);
							((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(".DamagePlayerCollider.damage = ");
							((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<float>(damage);
							((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(".");
						}
						log4.LogInfo(val2);
					}
					else
					{
						ManualLogSource log5 = Log;
						BepInExDebugLogInterpolatedStringHandler val = new BepInExDebugLogInterpolatedStringHandler(56, 1, ref flag);
						if (flag)
						{
							((BepInExLogInterpolatedStringHandler)val).AppendLiteral("DamagePlayerCollider on '");
							((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(friendlyName);
							((BepInExLogInterpolatedStringHandler)val).AppendLiteral("' has no float 'damage' member.");
						}
						log5.LogDebug(val);
					}
				}
				catch (Exception ex)
				{
					ManualLogSource log6 = Log;
					BepInExErrorLogInterpolatedStringHandler val3 = new BepInExErrorLogInterpolatedStringHandler(31, 2, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val3).AppendLiteral("Error setting knockback on '");
						((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(friendlyName);
						((BepInExLogInterpolatedStringHandler)val3).AppendLiteral("': ");
						((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<Exception>(ex);
					}
					log6.LogError(val3);
				}
			}
		}

		public const string MODNAME = "WildSands";

		public const string AUTHOR = "BedlessSleeper";

		public const string GUID = "BedlessSleeper_WildSands";

		public const string VERSION = "0.1.1";

		private static readonly string[] CactusNames = new string[2] { "Cactus1", "Cactus2" };

		private const float cactusKnockbackForce = 50f;

		private const float cactusDamage = 1f;

		public static float gravityStrength1 = 75f;

		public static float gravityStrength2 = 125f;

		public static float lowerHeight = 100f;

		public static float upperHeight = 200f;

		public static Rigidbody playerRb;

		internal static ManualLogSource Log { get; private set; }

		public override void Load()
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Expected O, but got Unknown
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: Expected O, but got Unknown
			Log = ((BasePlugin)this).Log;
			ManualLogSource log = Log;
			bool flag = default(bool);
			BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(17, 3, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("WildSands");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" v");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("0.1.1");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" by ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("BedlessSleeper");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" loading...");
			}
			log.LogInfo(val);
			TypeHelper.SafeFindType("DamagePlayerCollider");
			TypeHelper.SafeFindType("RandomMapObject");
			TypeHelper.SafeFindType("GameManager");
			Harmony val2 = new Harmony("BedlessSleeper_WildSands");
			val2.PatchAll();
		}

		private static bool TrySetFloatMember(object componentInstance, Type componentType, string memberName, float value)
		{
			if (componentInstance == null || componentType == null)
			{
				return false;
			}
			FieldInfo field = componentType.GetField(memberName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (field != null && field.FieldType == typeof(float))
			{
				field.SetValue(componentInstance, value);
				return true;
			}
			PropertyInfo property = componentType.GetProperty(memberName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (property != null && property.PropertyType == typeof(float) && property.CanWrite)
			{
				property.SetValue(componentInstance, value);
				return true;
			}
			return false;
		}
	}
}