Decompiled source of LGTunerAdvanced v0.0.4

plugins/LGTunerAdvanced.dll

Decompiled 4 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text.Json;
using System.Text.Json.Serialization;
using AssetShards;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using Expedition;
using GTFO.API;
using GTFO.API.Utilities;
using GameData;
using HarmonyLib;
using Il2CppInterop.Runtime.Injection;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppSystem;
using LGTunerAdvanced.Data;
using LGTunerAdvanced.Render;
using LevelGeneration;
using LevelGeneration.Core;
using Microsoft.CodeAnalysis;
using Player;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyFileVersion("0.0.4")]
[assembly: AssemblyInformationalVersion("0.0.4")]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyVersion("0.0.4.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;
		}
	}
}
internal class ManifestInfo
{
	internal const string TSName = "LGTunerAdvanced";

	internal const string TSDescription = "Changes GTFO to use a custom level builder rather than vanilla RNG heavy system.";

	internal const string TSVersion = "0.0.4";

	internal const string TSAuthor = "Tgb03";

	internal const string TSWebsite = "https://github.com/Tgb03/LGTunerAdvanced/";
}
namespace LGTunerAdvanced
{
	[BepInPlugin("dev.Tgb03.gtfo.LGTunerAdvanced", "LGTunerAdvanced", "0.0.4")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BasePlugin
	{
		public const string GUID = "dev.Tgb03.gtfo.LGTunerAdvanced";

		public const string MOD_NAME = "LGTunerAdvanced";

		public const string VERSION = "0.0.4";

		internal static ManualLogSource L;

		private static readonly Harmony _harmony = new Harmony("dev.Tgb03.gtfo.LGTunerAdvanced");

		public override void Load()
		{
			L = ((BasePlugin)this).Log;
			LGTunerAdvancedConfig.Initialize(((BasePlugin)this).Config);
			_harmony.PatchAll(Assembly.GetExecutingAssembly());
			LoadLevelGenerationData.LoadDictionary();
			LG_AreaExpanderRender.Register();
			if (LGTunerAdvancedConfig.RenderExpanders.Value)
			{
				RundownManager.OnExpeditionGameplayStarted += Action.op_Implicit((Action)delegate
				{
					LG_AreaExpanderRender.AddRenderer();
				});
			}
			L.LogInfo((object)"Plugin loaded!");
		}
	}
}
namespace LGTunerAdvanced.Render
{
	internal class LG_AreaExpanderRender : MonoBehaviour
	{
		private static PlayerAgent agent;

		public LG_AreaExpanderRender(IntPtr ptr)
			: base(ptr)
		{
		}

		public LG_AreaExpanderRender()
		{
		}

		public static void Register()
		{
			ClassInjector.RegisterTypeInIl2Cpp<LG_AreaExpanderRender>();
		}

		public static void AddRenderer()
		{
			PlayerAgent localPlayerAgent = PlayerManager.GetLocalPlayerAgent();
			if ((Object)(object)((Component)localPlayerAgent).gameObject.GetComponent<LG_AreaExpanderRender>() == (Object)null)
			{
				((Component)localPlayerAgent).gameObject.AddComponent<LG_AreaExpanderRender>();
			}
		}

		public void Start()
		{
			agent = PlayerManager.GetLocalPlayerAgent();
		}

		public void Update()
		{
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_0089: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0107: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0111: Unknown result type (might be due to invalid IL or missing references)
			//IL_0125: Unknown result type (might be due to invalid IL or missing references)
			//IL_012a: Unknown result type (might be due to invalid IL or missing references)
			//IL_013e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0143: 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_017b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0197: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bc: Unknown result type (might be due to invalid IL or missing references)
			if (!((Behaviour)this).enabled)
			{
				return;
			}
			LG_Area area = agent.m_courseNode.m_area;
			if ((Object)(object)area == (Object)null || area.m_zoneExpanders == null)
			{
				return;
			}
			LG_Geomorph geomorph = area.m_geomorph;
			int num = 0;
			using (IEnumerator<LG_Area> enumerator = ((Il2CppArrayBase<LG_Area>)(object)geomorph.m_areas).GetEnumerator())
			{
				while (enumerator.MoveNext() && !(((Il2CppObjectBase)enumerator.Current).Pointer == ((Il2CppObjectBase)area).Pointer))
				{
					num++;
				}
			}
			LG_GridPosition gridPosition = geomorph.m_tile.m_shape.m_gridPosition;
			Fig.DrawText($"Position: ({gridPosition.x - 20}, {gridPosition.z - 20})\nInternalAreaID: {num}", ((Component)area).transform.position, Quaternion.LookRotation(((Component)agent).transform.position - ((Component)area).transform.position) * Quaternion.Euler(0f, 180f, 0f), new Vector3(1f, 1f, 1f), LGTunerAdvancedConfig.AreaTextColor, (TextAnchor)7, (TextAlignment)0, 1f);
			for (int i = 0; i < area.m_zoneExpanders.Count; i++)
			{
				LG_ZoneExpander val = area.m_zoneExpanders[i];
				if (!((Object)(object)val == (Object)null))
				{
					if (!val.m_isZoneBuildBlocked)
					{
						_ = Color.white;
					}
					else
					{
						_ = Color.red;
					}
					Fig.DrawText(i.ToString(), ((Component)val).transform.position, ((Component)val).transform.rotation, new Vector3(5f, 5f, 5f), LGTunerAdvancedConfig.ExpanderColor, (TextAnchor)7, (TextAlignment)0, 1f);
				}
			}
		}
	}
}
namespace LGTunerAdvanced.Patches
{
	[HarmonyPatch(typeof(Builder), "Build")]
	public static class BuildResetter
	{
		[HarmonyPrefix]
		public static void Prefix()
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			LG_ZoneJob_CreateExpandFromDataPatch.built_geos.Clear();
			LG_ZoneJob_CreateExpandFromDataPatch.blocked_expanders.Clear();
			LG_ZoneJob_CreateExpandFromDataPatch.built_geos.Add(new LG_GridPosition(0, 0));
		}
	}
	[HarmonyPatch(typeof(LG_LevelBuilder), "PlaceRoot")]
	internal static class Inject_BuildGeomorph
	{
		[HarmonyPrefix]
		[HarmonyWrapSafe]
		[HarmonyPatch("PlaceRoot")]
		private static void Pre_PlaceRoot(LG_Tile tile, ref GameObject tilePrefab, ref bool forceAlignToVector, ref Vector3 alignVector, LG_Zone zone)
		{
			//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_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Expected O, but got Unknown
			//IL_0078: 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)
			forceAlignToVector = alignVector != Vector3.zero;
			GeoOverride geoOverride;
			if (tile == null)
			{
				Plugin.L.LogError((object)"PlaceRoot failed because tile variable is null");
			}
			else if (tile.m_shape == null)
			{
				Plugin.L.LogError((object)"PlaceRoot failed because tile.m_shape variable is null");
			}
			else if (!LoadLevelGenerationData.TryGrabGeoOverride(zone, tile.m_shape.m_gridPosition, out geoOverride))
			{
				ManualLogSource l = Plugin.L;
				bool flag = default(bool);
				BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(58, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("PlaceRoot failed because could not find geoOverride for ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(tile.m_shape.m_gridPosition.x);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(", ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(tile.m_shape.m_gridPosition.z);
				}
				l.LogError(val);
			}
			else
			{
				Object loadedAsset = AssetAPI.GetLoadedAsset(geoOverride.Geomorph);
				GameObject val2 = ((loadedAsset != null) ? ((Il2CppObjectBase)loadedAsset).Cast<GameObject>() : null);
				if ((Object)(object)val2 != (Object)null)
				{
					tilePrefab = val2;
				}
			}
		}

		[HarmonyPostfix]
		[HarmonyWrapSafe]
		[HarmonyPatch("PlaceRoot")]
		private static void Post_PlaceRoot(LG_Tile tile, LG_Zone zone, LG_Geomorph __result)
		{
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Expected O, but got Unknown
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
			GameObject gameObject = ((Component)__result).gameObject;
			GeoOverride geoOverride;
			if (tile == null)
			{
				Plugin.L.LogError((object)"PlaceRoot failed because tile variable is null");
			}
			else if (tile.m_shape == null)
			{
				Plugin.L.LogError((object)"PlaceRoot failed because tile.m_shape variable is null");
			}
			else if (!LoadLevelGenerationData.TryGrabGeoOverride(zone, tile.m_shape.m_gridPosition, out geoOverride))
			{
				ManualLogSource l = Plugin.L;
				bool flag = default(bool);
				BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(58, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("PlaceRoot failed because could not find geoOverride for ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(tile.m_shape.m_gridPosition.x);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(", ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(tile.m_shape.m_gridPosition.z);
				}
				l.LogError(val);
			}
			else
			{
				float num = geoOverride.Rotation switch
				{
					GeoRotation.Forwards => 0, 
					GeoRotation.Right => 90, 
					GeoRotation.Backwards => 180, 
					GeoRotation.Left => 270, 
					_ => 0, 
				};
				gameObject.transform.rotation = Quaternion.Euler(0f, num, 0f);
				__result.SetPlaced();
			}
		}

		[HarmonyPostfix]
		[HarmonyWrapSafe]
		[HarmonyPatch("GetTilePosition")]
		private static void Post_GetTilePosition(LG_Tile tile, LG_Floor floor, int dimensionIndex, ref Vector3 __result)
		{
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_0076: Unknown result type (might be due to invalid IL or missing references)
			if (floor.allZones.Count != 0 && tile != null && tile.m_shape != null && LoadLevelGenerationData.TryGrabLastQueried(out LevelGenerationOverride levelOverride) && levelOverride.TryGrabGeoAtPos(tile.m_shape.m_gridPosition, out var result))
			{
				ComplexResourceSetDataBlock block = GameDataBlockBase<ComplexResourceSetDataBlock>.GetBlock(RundownManager.ActiveExpedition.Expedition.ComplexResourceData);
				Vector3 val = default(Vector3);
				((Vector3)(ref val))..ctor(__result.x, (float)result.Altitude * block.LevelGenConfig.AltitudeOffset, __result.z);
				__result = val;
			}
		}
	}
	[HarmonyPatch(typeof(LG_ZoneJob_CreateExpandFromData), "ExpandZone")]
	public static class LG_ZoneJob_CreateExpandFromDataPatch
	{
		internal static HashSet<LG_GridPosition> built_geos = new HashSet<LG_GridPosition>();

		internal static HashSet<IntPtr> blocked_expanders = new HashSet<IntPtr>();

		[HarmonyPrefix]
		public static void Prefix(LG_Zone zone, ref LG_Area buildFromArea, ref LG_ZoneExpander buildFromExpander, uint seed)
		{
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Expected O, but got Unknown
			if (LoadLevelGenerationData.TryGrabZoneOverride(zone, out ZoneOverride zoneOverride) && GrabExpander(zone, zoneOverride, out var lgZoneExpander, out var buildFromArea2))
			{
				blocked_expanders.Add(((Il2CppObjectBase)lgZoneExpander).Pointer);
				if (zone.m_areas.Count == 0)
				{
					zone.m_sourceExpander = lgZoneExpander;
				}
				ManualLogSource l = Plugin.L;
				bool flag = default(bool);
				BepInExMessageLogInterpolatedStringHandler val = new BepInExMessageLogInterpolatedStringHandler(53, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("   Succeeded in finding connection in zone ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(zone.ID);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" for area ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(zone.m_areas.Count);
				}
				l.LogMessage(val);
				buildFromExpander = lgZoneExpander;
				buildFromArea = buildFromArea2;
			}
		}

		[HarmonyPostfix]
		public static void Postfix(LG_Zone zone, LG_Area buildFromArea, LG_ZoneExpander buildFromExpander, uint seed, ref LG_Area area, ref CoverageResult __result)
		{
			if (LoadLevelGenerationData.TryGrabZoneOverride(zone, out ZoneOverride zoneOverride))
			{
				if (zone.m_areas.Count < zoneOverride.AreaOverrides.Count)
				{
					__result = (CoverageResult)1;
				}
				else
				{
					__result = (CoverageResult)4;
				}
			}
		}

		private static bool GrabExpander(LG_Zone zone, ZoneOverride zoneOverride, out LG_ZoneExpander lgZoneExpander, out LG_Area buildFromArea)
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Expected O, but got Unknown
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Expected O, but got Unknown
			//IL_00db: 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_012b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0131: Expected O, but got Unknown
			//IL_01a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ac: Expected O, but got Unknown
			bool flag = default(bool);
			if ((Object)(object)zone == (Object)null || zoneOverride == null)
			{
				ManualLogSource l = Plugin.L;
				BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(49, 0, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("   Something ended up null and could not proceed.");
				}
				l.LogError(val);
				lgZoneExpander = null;
				buildFromArea = null;
				return false;
			}
			if (zone.m_areas.Count < 0 || zone.m_areas.Count >= zoneOverride.AreaOverrides.Count)
			{
				ManualLogSource l2 = Plugin.L;
				BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(61, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("   zone.m_areas.Count > zoneOverride.AreaOverrides.Count: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(zone.m_areas.Count);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" > ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(zoneOverride.AreaOverrides.Count);
				}
				l2.LogError(val);
				lgZoneExpander = null;
				buildFromArea = null;
				return false;
			}
			try
			{
				AreaOverride areaOverride = zoneOverride.AreaOverrides[zone.m_areas.Count];
				LG_Area val2 = ((Il2CppArrayBase<LG_Area>)(object)((CellBase<LG_Tile, LG_Cell>)(object)((CellGridBase<LG_Grid, LG_Tile, LG_Cell>)(object)zone.Dimension.Grid).GetCell(GetRealPos(areaOverride.PreviousCellPosition))).m_grouping.m_geoRoot.m_areas)[areaOverride.PreviousCellInternalAreaID];
				if (areaOverride.LG_ExpanderID < 0 || areaOverride.LG_ExpanderID >= val2.m_zoneExpanders.Count)
				{
					ManualLogSource l3 = Plugin.L;
					BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(73, 2, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("   areaOverride.LG_ExpanderID >= previous_area.m_zoneExpanders.Count: ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(areaOverride.LG_ExpanderID);
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" > ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(val2.m_zoneExpanders.Count);
					}
					l3.LogError(val);
					lgZoneExpander = null;
					buildFromArea = null;
					return false;
				}
				if (blocked_expanders.Contains(((Il2CppObjectBase)val2.m_zoneExpanders[areaOverride.LG_ExpanderID]).Pointer))
				{
					ManualLogSource l4 = Plugin.L;
					bool flag2 = default(bool);
					BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(81, 0, ref flag2);
					if (flag2)
					{
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("   LG_ZoneExpander was already parsed and blocked. This means it cannot be reused");
					}
					l4.LogError(val);
					lgZoneExpander = null;
					buildFromArea = null;
					return false;
				}
				lgZoneExpander = val2.m_zoneExpanders[areaOverride.LG_ExpanderID];
				buildFromArea = val2;
				return true;
			}
			catch (Exception ex)
			{
				Plugin.L.LogError((object)ex);
			}
			lgZoneExpander = null;
			buildFromArea = null;
			return false;
		}

		private static LG_GridPosition GetRealPos(LG_GridPosition position)
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			position.x += 20;
			position.z += 20;
			return position;
		}
	}
	[HarmonyPatch(typeof(LG_LoadComplexDataSetResourcesJob), "Build")]
	internal class LoadComplexShard
	{
		private static int _waitingShared;

		[HarmonyWrapSafe]
		[HarmonyPrefix]
		private static void Prefix(LG_LoadComplexDataSetResourcesJob __instance)
		{
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Expected I4, but got Unknown
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			if (__instance.m_loadingStarted)
			{
				return;
			}
			if (!LoadLevelGenerationData.TryGrabLastQueried(out LevelGenerationOverride levelOverride))
			{
				Plugin.L.LogError((object)"Failed to find levelOverride, nothing was queried");
				return;
			}
			foreach (Complex item in levelOverride.ExtraComplexResourceToLoad)
			{
				AssetBundleName val = (AssetBundleName)((int)item switch
				{
					0 => 2, 
					1 => 4, 
					2 => 3, 
					_ => 0, 
				});
				if ((int)val != 0)
				{
					AssetShardManager.LoadAllShardsForBundleAsync(val, Action.op_Implicit((Action)Loaded), 1, (LoadSceneMode)1);
					_waitingShared++;
				}
			}
		}

		private static void Loaded()
		{
			_waitingShared--;
		}

		[HarmonyWrapSafe]
		[HarmonyPostfix]
		private static void Postfix(LG_LoadComplexDataSetResourcesJob __instance, ref bool __result)
		{
			if (_waitingShared > 0)
			{
				__result = false;
			}
		}
	}
	[HarmonyPatch(typeof(RundownManager), "MasterSelectActiveExpedition")]
	internal class SelectActiveExpeditionPatch
	{
		[HarmonyPostfix]
		public static void Postfix()
		{
			LoadLevelGenerationData.SetLayoutID(RundownManager.ActiveExpedition.LevelLayoutData);
		}
	}
}
namespace LGTunerAdvanced.Data
{
	[Serializable]
	internal class LevelGenerationOverride
	{
		public uint LevelLayoutID;

		public List<ZoneOverride> ZoneOverrides = new List<ZoneOverride>();

		public List<GeoOverride> GeoOverrides = new List<GeoOverride>();

		public List<Complex> ExtraComplexResourceToLoad = new List<Complex>();

		public bool TryGrabGeoAtPos(LG_GridPosition position, out GeoOverride result)
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			foreach (GeoOverride geoOverride in GeoOverrides)
			{
				if (geoOverride.Position.x == position.x - 20 && geoOverride.Position.z == position.z - 20)
				{
					result = geoOverride;
					return true;
				}
			}
			result = null;
			return false;
		}
	}
	[Serializable]
	internal class ZoneOverride
	{
		public List<AreaOverride> AreaOverrides = new List<AreaOverride>();
	}
	[Serializable]
	internal class AreaOverride
	{
		public LG_GridPosition PreviousCellPosition;

		public int PreviousCellInternalAreaID;

		public int LG_ExpanderID;
	}
	[Serializable]
	internal class GeoOverride
	{
		public LG_GridPosition Position;

		public string Geomorph;

		public GeoRotation Rotation;

		public int Altitude;
	}
	[Serializable]
	internal enum GeoRotation
	{
		Forwards,
		Right,
		Backwards,
		Left
	}
	internal class LoadLevelGenerationData
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static LiveEditEventHandler <0>__LiveEditFileChanged;
		}

		public static Dictionary<uint, LevelGenerationOverride> levelGenerationOverrides = new Dictionary<uint, LevelGenerationOverride>();

		private static readonly JsonSerializerOptions _jsonOptions = new JsonSerializerOptions
		{
			PropertyNameCaseInsensitive = true,
			IncludeFields = true,
			ReadCommentHandling = JsonCommentHandling.Skip,
			AllowTrailingCommas = true,
			Converters = { (JsonConverter)new JsonStringEnumConverter() }
		};

		private static LevelGenerationOverride lastQueried = null;

		public static void SetLayoutID(uint id)
		{
			lastQueried = levelGenerationOverrides[id];
		}

		public static void LoadDictionary()
		{
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Expected O, but got Unknown
			//IL_0144: Unknown result type (might be due to invalid IL or missing references)
			//IL_0149: Unknown result type (might be due to invalid IL or missing references)
			//IL_014f: Expected O, but got Unknown
			//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: Expected O, but got Unknown
			string text = Paths.PluginPath + "/Custom/LGTunerAdvanced";
			if (!Directory.Exists(text))
			{
				Directory.CreateDirectory(text);
			}
			ManualLogSource l = Plugin.L;
			bool flag = default(bool);
			BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(39, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Loading LevelGenerationOverrides from: ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(text);
			}
			l.LogInfo(val);
			string[] files = Directory.GetFiles(text);
			foreach (string text2 in files)
			{
				if (text2.EndsWith("schema.json"))
				{
					continue;
				}
				string text3 = Path.GetExtension(text2).ToLowerInvariant();
				if (text3 != ".json" && text3 != ".jsonc")
				{
					continue;
				}
				try
				{
					LevelGenerationOverride levelGenerationOverride = JsonSerializer.Deserialize<LevelGenerationOverride>(File.ReadAllText(text2), _jsonOptions);
					levelGenerationOverrides[levelGenerationOverride.LevelLayoutID] = levelGenerationOverride;
					ManualLogSource l2 = Plugin.L;
					val = new BepInExInfoLogInterpolatedStringHandler(18, 2, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("     Loaded ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<uint>(levelGenerationOverride.LevelLayoutID);
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" from ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(text2);
					}
					l2.LogInfo(val);
				}
				catch (Exception ex)
				{
					Plugin.L.LogError((object)ex);
				}
			}
			LiveEditListener obj = LiveEdit.CreateListener(text, "*.*", true);
			object obj2 = <>O.<0>__LiveEditFileChanged;
			if (obj2 == null)
			{
				LiveEditEventHandler val2 = LiveEditFileChanged;
				<>O.<0>__LiveEditFileChanged = val2;
				obj2 = (object)val2;
			}
			obj.FileChanged += (LiveEditEventHandler)obj2;
			obj.StartListen();
		}

		private static void LiveEditFileChanged(LiveEditEventArgs e)
		{
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Expected O, but got Unknown
			string fullPath = e.FullPath;
			if (!fullPath.Contains("LGTunerAdvanced"))
			{
				return;
			}
			string text = Path.GetExtension(fullPath).ToLowerInvariant();
			if (text != ".json" && text != ".jsonc")
			{
				return;
			}
			try
			{
				LevelGenerationOverride levelGenerationOverride = JsonSerializer.Deserialize<LevelGenerationOverride>(File.ReadAllText(fullPath), _jsonOptions);
				levelGenerationOverrides[levelGenerationOverride.LevelLayoutID] = levelGenerationOverride;
				ManualLogSource l = Plugin.L;
				bool flag = default(bool);
				BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(18, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("     Loaded ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<uint>(levelGenerationOverride.LevelLayoutID);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" from ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(e.FileName);
				}
				l.LogInfo(val);
			}
			catch (Exception ex)
			{
				Plugin.L.LogError((object)ex);
			}
		}

		private static uint GetLayoutID(LG_Zone zone)
		{
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Expected I4, but got Unknown
			if (!zone.IsMainDimension)
			{
				return zone.Dimension.DimensionData.LevelLayoutData;
			}
			LG_LayerType type = zone.Layer.m_type;
			return (int)type switch
			{
				0 => Builder.LevelGenExpedition.LevelLayoutData, 
				1 => Builder.LevelGenExpedition.SecondaryLayout, 
				2 => Builder.LevelGenExpedition.ThirdLayout, 
				_ => throw new ArgumentException($"LG_LayerType of LG_Zone with id: {zone.ID} is out of bounds."), 
			};
		}

		public static bool TryGrabZoneOverride(LG_Zone zone, out ZoneOverride? zoneOverride)
		{
			uint layoutID = GetLayoutID(zone);
			if (levelGenerationOverrides.TryGetValue(layoutID, out var value))
			{
				if (zone.IDinLayer >= value.ZoneOverrides.Count)
				{
					zoneOverride = null;
					return false;
				}
				zoneOverride = value.ZoneOverrides[zone.IDinLayer];
				lastQueried = value;
			}
			else
			{
				zoneOverride = null;
			}
			return zoneOverride != null;
		}

		public static bool TryGrabGeoOverride(LG_Zone zone, LG_GridPosition position, out GeoOverride? geoOverride)
		{
			//IL_0037: 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)
			uint layoutID = GetLayoutID(zone);
			if (levelGenerationOverrides.TryGetValue(layoutID, out var value))
			{
				foreach (GeoOverride geoOverride2 in value.GeoOverrides)
				{
					if (geoOverride2.Position.x == position.x - 20 && geoOverride2.Position.z == position.z - 20)
					{
						geoOverride = geoOverride2;
						lastQueried = value;
						return true;
					}
				}
			}
			geoOverride = null;
			return false;
		}

		public static bool TryGrabLastQueried(out LevelGenerationOverride? levelOverride)
		{
			levelOverride = lastQueried;
			return levelOverride != null;
		}
	}
	internal class LGTunerAdvancedConfig
	{
		public static ConfigEntry<bool> RenderExpanders { get; private set; }

		public static ConfigEntry<string> ExpanderColorHex { get; private set; }

		public static ConfigEntry<string> AreaTextColorHex { get; private set; }

		public static Color ExpanderColor { get; private set; }

		public static Color AreaTextColor { get; private set; }

		internal static void Initialize(ConfigFile cfg)
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: 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)
			RenderExpanders = cfg.Bind<bool>("General", "RenderExpanders", false, "Show LG_AreaExpander visually when in the map.");
			ExpanderColorHex = cfg.Bind<string>("General", "ExpanderColor", ToHex(Color.white), "Color of expanders text");
			AreaTextColorHex = cfg.Bind<string>("General", "AreaTextColor", ToHex(new Color(255f, 0f, 127f)), "Color of area text");
			ExpanderColor = ParseColor(ExpanderColorHex.Value, Color.white);
			AreaTextColor = ParseColor(AreaTextColorHex.Value, new Color(255f, 0f, 127f));
		}

		private static Color ParseColor(string hex, Color fallback)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			Color result = default(Color);
			if (!ColorUtility.TryParseHtmlString(hex, ref result))
			{
				return fallback;
			}
			return result;
		}

		private static string ToHex(Color color)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			return "#" + ColorUtility.ToHtmlStringRGB(color);
		}
	}
}