Decompiled source of ForcedConsumableSpawns v1.1.0

ForcedConsumableSpawns.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text.Json.Serialization;
using AIGraph;
using AssetShards;
using BepInEx;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using ForcedConsumableSpawns.CustomFields;
using ForcedConsumableSpawns.JSON;
using GameData;
using HarmonyLib;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppJsonNet.Linq;
using Il2CppSystem;
using InjectLib.FieldInjection;
using InjectLib.JsonNETInjection;
using InjectLib.JsonNETInjection.Handler;
using InjectLib.JsonNETInjection.Supports;
using LevelGeneration;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("ForcedConsumableSpawns")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+ca1677873adca81a4b0c8f6c579075fc10e5d9bf")]
[assembly: AssemblyProduct("ForcedConsumableSpawns")]
[assembly: AssemblyTitle("ForcedConsumableSpawns")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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]
	[Microsoft.CodeAnalysis.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;
		}
	}
}
namespace ForcedConsumableSpawns
{
	internal static class DinoLogger
	{
		private static ManualLogSource logger = Logger.CreateLogSource("ForcedConsumableSpawns");

		public static void Log(string format, params object[] args)
		{
			Log(string.Format(format, args));
		}

		public static void Log(string str)
		{
			if (logger != null)
			{
				logger.Log((LogLevel)8, (object)str);
			}
		}

		public static void Warning(string format, params object[] args)
		{
			Warning(string.Format(format, args));
		}

		public static void Warning(string str)
		{
			if (logger != null)
			{
				logger.Log((LogLevel)4, (object)str);
			}
		}

		public static void Error(string format, params object[] args)
		{
			Error(string.Format(format, args));
		}

		public static void Error(string str)
		{
			if (logger != null)
			{
				logger.Log((LogLevel)2, (object)str);
			}
		}

		public static void Debug(string format, params object[] args)
		{
			Debug(string.Format(format, args));
		}

		public static void Debug(string str)
		{
			if (logger != null)
			{
				logger.Log((LogLevel)32, (object)str);
			}
		}
	}
	[BepInPlugin("Dinorush.ForcedConsumableSpawns", "ForcedConsumableSpawns", "1.1.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	internal sealed class EntryPoint : BasePlugin
	{
		public const string MODNAME = "ForcedConsumableSpawns";

		public override void Load()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			new Harmony("ForcedConsumableSpawns").PatchAll();
			BPDDataFields.Init();
			CDDataFields.Init();
			JsonInjector.AddHandler<ConsumableDistributionDataBlock>((Il2CppJsonReferenceTypeHandler<ConsumableDistributionDataBlock>)(object)new ConsumableForceDataHandler());
			JsonInjector.AddHandler<BigPickupDistributionDataBlock>((Il2CppJsonReferenceTypeHandler<BigPickupDistributionDataBlock>)(object)new BigPickupForceDataHandler());
			((BasePlugin)this).Log.LogMessage((object)"Loaded ForcedConsumableSpawns");
		}
	}
}
namespace ForcedConsumableSpawns.Patches
{
	[HarmonyPatch]
	internal static class LevelGenerationPatches
	{
		private const eCommodityType CustomCommodityType = 4115;

		private static readonly ZonePlacementWeights DefaultWeights = new ZonePlacementWeights();

		[HarmonyPatch(typeof(LG_Distribute_DetailedPlacedFunctionPerZone), "Build")]
		[HarmonyWrapSafe]
		[HarmonyPrefix]
		private static void Pre_Build(LG_Distribute_DetailedPlacedFunctionPerZone __instance)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Invalid comparison between Unknown and I4
			if ((int)__instance.m_func == 6)
			{
				DoForceConsumableSpawns(__instance.m_zone);
				DoForceBigPickupSpawns(__instance.m_zone);
			}
		}

		private static void DoForceBigPickupSpawns(LG_Zone zone)
		{
			//IL_011d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0124: Expected O, but got Unknown
			//IL_0176: Unknown result type (might be due to invalid IL or missing references)
			uint bigPickupDistributionInZone = zone.m_settings.m_zoneData.BigPickupDistributionInZone;
			if (bigPickupDistributionInZone == 0)
			{
				return;
			}
			BigPickupDistributionDataBlock block = GameDataBlockBase<BigPickupDistributionDataBlock>.GetBlock(bigPickupDistributionInZone);
			if (block == null)
			{
				DinoLogger.Error($"Attempted to get BigPickupDistribution {bigPickupDistributionInZone}, but it doesn't exist!");
			}
			else
			{
				if (!block.TryGetForceList(out List<PickupForceData> data))
				{
					return;
				}
				LG_DistributeItem val2 = default(LG_DistributeItem);
				AIG_CourseNode val3 = default(AIG_CourseNode);
				foreach (PickupForceData item in data)
				{
					for (int i = 0; i < item.Count; i++)
					{
						uint itemID = item.ItemID;
						LG_DistributePickUpItem val = null;
						if (LG_DistributionJobUtils.TryGetExistingZoneFunctionDistribution(zone, (ExpeditionFunction)12, Builder.SessionSeedRandom.Value("Dist_Pickup_TryGetZoneFunctionDistribution_Pickupitems"), item.PlacementWeights ?? DefaultWeights, ref val2, ref val3, false, ""))
						{
							LG_DistributePickUpItem val4 = ((Il2CppObjectBase)val2).TryCast<LG_DistributePickUpItem>();
							if (val4 == null)
							{
								DinoLogger.Error("LG_Distribute_PickupItems could not cast distItem as LG_DistributePickupItem!");
								continue;
							}
							val = val4;
						}
						else
						{
							AIG_CourseNode randomNodeFromZoneForFunction = LG_DistributionJobUtils.GetRandomNodeFromZoneForFunction(zone, (ExpeditionFunction)12, Builder.BuildSeedRandom.Value("Dist_Pickup_GetRandomNodeFromZoneForFunction"), 1f);
							val = new LG_DistributePickUpItem((ExpeditionFunction)12, (ePickupItemType)3, randomNodeFromZoneForFunction, Builder.BuildSeedRandom.Range(0, int.MaxValue, "Dist_Pickup_GetRandomNodeFromZoneForFunction"), itemID);
							randomNodeFromZoneForFunction.m_zone.DistributionData.PickupItems.Enqueue(val);
							DinoLogger.Error(string.Concat("LG_Distribute_PickupItems Had to create a new LG_DistributePickUpItem for function ", (object)(ExpeditionFunction)12, " in zone ", zone.NavInfo.GetFormattedText((LG_NavInfoFormat)5), " dim:", zone.DimensionIndex, "! m_isWardenObjectiveGatherItem: ", false));
						}
						val.m_type = (ePickupItemType)3;
						val.m_genericItemId = itemID;
						val.m_consumableData = null;
						val.m_bigPickupData = null;
						((LG_DistributeItem)val).m_isWardenObjective = false;
						val.m_artifactCategory = (ArtifactCategory)0;
					}
				}
			}
		}

		private static void DoForceConsumableSpawns(LG_Zone zone)
		{
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0096: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: Expected O, but got Unknown
			//IL_017e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0183: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a1: Expected O, but got Unknown
			uint consumableDistributionInZone = zone.m_settings.m_zoneData.ConsumableDistributionInZone;
			if (consumableDistributionInZone == 0)
			{
				return;
			}
			ConsumableDistributionDataBlock block = GameDataBlockBase<ConsumableDistributionDataBlock>.GetBlock(consumableDistributionInZone);
			if (block == null)
			{
				DinoLogger.Error($"Attempted to get ConsumableDistribution {consumableDistributionInZone}, but it doesn't exist!");
			}
			else
			{
				if (!block.TryGetForceList(out List<PickupForceData> data))
				{
					return;
				}
				LG_DistributeItem val2 = default(LG_DistributeItem);
				AIG_CourseNode val3 = default(AIG_CourseNode);
				foreach (PickupForceData item in data)
				{
					for (int i = 0; i < item.Count; i++)
					{
						ResourceContainerSpawnData val = new ResourceContainerSpawnData
						{
							m_type = (eResourceContainerSpawnType)6,
							m_comType = (eCommodityType)4115,
							m_genericItemId = item.ItemID
						};
						if (LG_DistributionJobUtils.TryGetExistingZoneFunctionDistribution(zone, (ExpeditionFunction)10, Builder.SessionSeedRandom.Value("Dist_Pickup_TryGetZoneFunctionDistribution_reoourceContainers"), item.PlacementWeights ?? DefaultWeights, ref val2, ref val3, false, ""))
						{
							LG_DistributeResourceContainer val4 = ((Il2CppObjectBase)val2).TryCast<LG_DistributeResourceContainer>();
							if (val4 != null)
							{
								val4.m_packs.Add(val);
								val4.m_locked = Builder.SessionSeedRandom.Value("Dist_Pickup_ChanceForConsumableLocker_Locked") > 0.85f;
							}
							else
							{
								DinoLogger.Error("LG_Distribute_PickupItems could not cast distItem as LG_DistributeResourceContainer!");
							}
						}
						else
						{
							AIG_CourseNode randomNodeFromZoneForFunction = LG_DistributionJobUtils.GetRandomNodeFromZoneForFunction(zone, (ExpeditionFunction)10, Builder.BuildSeedRandom.Value("Dist_Pickup_GetRandomNodeFromZoneForFunction"), 1f);
							LG_DistributeResourceContainer val5 = new LG_DistributeResourceContainer((ExpeditionFunction)10, val, false, randomNodeFromZoneForFunction, Builder.BuildSeedRandom.Range(0, int.MaxValue, "Dist_Pickup_GetRandomNodeFromZoneForFunction_Container"), Builder.BuildSeedRandom.Value("Dist_Pickup_GetRandomNodeFromZoneForFunction_ContainerLock"), Builder.BuildSeedRandom.Range(2, 4, "Dist_Pickup_GetRandomNodeFromZoneForFunction_StoragePotential"))
							{
								m_locked = (Builder.SessionSeedRandom.Value("Dist_Pickup_ChanceForConsumableLocker_Locked") > 0.85f)
							};
							zone.DistributionData.ResourceContainerItems.Enqueue(val5);
							DinoLogger.Error("Had to create a new resourceContainer!");
						}
					}
				}
			}
		}

		[HarmonyPatch(typeof(LG_ResourceContainer_Storage), "SpawnConsumable")]
		[HarmonyWrapSafe]
		[HarmonyPrefix]
		private static bool Pre_SpawnConsumable(LG_ResourceContainer_Storage __instance, ResourceContainerSpawnData pack, Transform align, int randomSeed)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Invalid comparison between Unknown and I4
			if ((int)pack.m_comType != 4115)
			{
				return true;
			}
			LG_PickupItem val = GOUtil.SpawnChildAndGetComp<LG_PickupItem>(AssetShardManager.GetLoadedAsset<GameObject>(__instance.m_pickupAssetPath, false), align);
			val.SetupAsConsumable(randomSeed, pack.m_genericItemId);
			__instance.SetSpawnNode(((Component)val).gameObject, __instance.m_core.SpawnNode);
			__instance.DisableInteraction(((Component)val).gameObject);
			return false;
		}
	}
}
namespace ForcedConsumableSpawns.JSON
{
	internal class ConsumableForceDataHandler : Il2CppJsonReferenceTypeHandler<ConsumableDistributionDataBlock>
	{
		public override void OnRead(in Object result, in JToken jToken)
		{
			JToken val = default(JToken);
			if (((Il2CppObjectBase)jToken).TryCast<JObject>().TryGetValue("ForceSpawnData", ref val))
			{
				List<PickupForceData> data = InjectLibJSON.Deserialize<List<PickupForceData>>(((Object)val).ToString(), Array.Empty<JsonConverter>());
				((Il2CppObjectBase)result).Cast<ConsumableDistributionDataBlock>().SetForceList(data);
			}
		}
	}
	internal class BigPickupForceDataHandler : Il2CppJsonReferenceTypeHandler<BigPickupDistributionDataBlock>
	{
		public override void OnRead(in Object result, in JToken jToken)
		{
			JToken val = default(JToken);
			if (((Il2CppObjectBase)jToken).TryCast<JObject>().TryGetValue("ForceSpawnData", ref val))
			{
				List<PickupForceData> data = InjectLibJSON.Deserialize<List<PickupForceData>>(((Object)val).ToString(), Array.Empty<JsonConverter>());
				((Il2CppObjectBase)result).Cast<BigPickupDistributionDataBlock>().SetForceList(data);
			}
		}
	}
}
namespace ForcedConsumableSpawns.CustomFields
{
	internal static class BPDDataFields
	{
		internal static void Init()
		{
			FieldInjector<BigPickupDistributionDataBlock>.DefineManagedField<List<PickupForceData>>("m_ForceList");
		}

		public static void SetForceList(this BigPickupDistributionDataBlock target, List<PickupForceData> data)
		{
			FieldInjector<BigPickupDistributionDataBlock>.TrySetManagedField<List<PickupForceData>>(target, "m_ForceList", data);
		}

		public static bool TryGetForceList(this BigPickupDistributionDataBlock target, [MaybeNullWhen(false)] out List<PickupForceData> data)
		{
			return FieldInjector<BigPickupDistributionDataBlock>.TryGetManagedField<List<PickupForceData>>(target, "m_ForceList", ref data);
		}
	}
	internal static class CDDataFields
	{
		internal static void Init()
		{
			FieldInjector<ConsumableDistributionDataBlock>.DefineManagedField<List<PickupForceData>>("m_ForceList");
		}

		public static void SetForceList(this ConsumableDistributionDataBlock target, List<PickupForceData> data)
		{
			FieldInjector<ConsumableDistributionDataBlock>.TrySetManagedField<List<PickupForceData>>(target, "m_ForceList", data);
		}

		public static bool TryGetForceList(this ConsumableDistributionDataBlock target, [MaybeNullWhen(false)] out List<PickupForceData> data)
		{
			return FieldInjector<ConsumableDistributionDataBlock>.TryGetManagedField<List<PickupForceData>>(target, "m_ForceList", ref data);
		}
	}
	public sealed class PickupForceData
	{
		public uint ItemID { get; set; }

		public uint Count { get; set; } = 1u;


		public ZonePlacementWeights? PlacementWeights { get; set; }
	}
}