Decompiled source of BuildBackBetter v1.0.7

Build.Back.Better.v.1.0.7.dll

Decompiled 19 hours ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("Loki Moon Labs")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.7.0")]
[assembly: AssemblyInformationalVersion("1.0.7")]
[assembly: AssemblyProduct("Build Back Better")]
[assembly: AssemblyTitle("BuildBackBetter")]
[assembly: AssemblyVersion("1.0.7.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[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 BuildBackBetter
{
	internal static class ModInfo
	{
		internal const string Guid = "com.lokimoonlabs.buildbackbetter";

		internal const string Name = "Build Back Better";

		internal const string Version = "1.0.7";

		internal const string HarmonyId = "com.lokimoonlabs.buildbackbetter.patches";
	}
	[HarmonyPatch(typeof(PlayerAction_Build), "Open")]
	internal static class BuildOpenPatch
	{
		private static void Postfix()
		{
			Plugin.Instance?.SetConstructMode(active: true);
		}
	}
	[HarmonyPatch(typeof(PlayerAction_Build), "Close")]
	internal static class BuildClosePatch
	{
		private static void Postfix()
		{
			Plugin.Instance?.SetConstructMode(active: false);
		}
	}
	[HarmonyPatch(typeof(PlayerAction_Build), "CloseWithoutResetAxes")]
	internal static class BuildCloseWithoutResetAxesPatch
	{
		private static void Postfix()
		{
			Plugin.Instance?.SetConstructMode(active: false);
		}
	}
	[HarmonyPatch(typeof(PlayerAction_Build), "GameTick")]
	internal static class BlueprintModeTransitionPatch
	{
		private static void Postfix(PlayerAction_Build __instance)
		{
			Plugin.Instance?.TrackBlueprintMode(__instance);
		}
	}
	[HarmonyPatch(typeof(FactoryModel), "SetGlobalRenderState")]
	internal static class FactoryRenderPatch
	{
		private static void Postfix(FactoryModel __instance)
		{
			Plugin instance = Plugin.Instance;
			if (!((Object)(object)instance == (Object)null) && instance.ShouldApplyVisuals())
			{
				FactoryModel.whiteMode0 = true;
				Shader.SetGlobalFloat("_Global_WhiteMode0", 1f);
				if (__instance.gpuiManager != null)
				{
					__instance.gpuiManager.renderVegetable = false;
				}
			}
		}
	}
	[HarmonyPatch(typeof(UIBuildingGrid), "Update")]
	internal static class BuildingGridPatch
	{
		private static void Postfix(UIBuildingGrid __instance)
		{
			//IL_013e: Unknown result type (might be due to invalid IL or missing references)
			//IL_014e: Unknown result type (might be due to invalid IL or missing references)
			//IL_016e: Unknown result type (might be due to invalid IL or missing references)
			//IL_017e: 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_00b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
			Plugin instance = Plugin.Instance;
			if ((Object)(object)instance == (Object)null || !instance.ShouldApplyVisuals())
			{
				return;
			}
			PlanetData localPlanet = GameMain.localPlanet;
			object obj;
			if (localPlanet == null)
			{
				obj = null;
			}
			else
			{
				PlanetAuxData aux = localPlanet.aux;
				obj = ((aux != null) ? aux.activeGrid : null);
			}
			PlanetGrid val = (PlanetGrid)obj;
			Renderer blueprintGridRnd = __instance.blueprintGridRnd;
			if (val == null || (Object)(object)blueprintGridRnd == (Object)null)
			{
				return;
			}
			if ((Object)(object)__instance.gridRnd != (Object)null)
			{
				__instance.gridRnd.enabled = false;
			}
			if ((Object)(object)__instance.altGridRnd != (Object)null)
			{
				__instance.altGridRnd.enabled = false;
			}
			Material sharedMaterial = blueprintGridRnd.sharedMaterial;
			if ((Object)(object)sharedMaterial != (Object)null)
			{
				if (instance.ConsumeBlueprintGridReset())
				{
					sharedMaterial.SetVector("_CursorGratBox", Vector4.zero);
					sharedMaterial.SetVector("_AnchorPos", Vector4.zero);
					sharedMaterial.SetFloat("_ShowDivideLine", 0f);
					sharedMaterial.SetFloat("_DivideLine", 0f);
					for (int i = 0; i < 64; i++)
					{
						sharedMaterial.SetVector("_CursorGratBox" + i, Vector4.zero);
						sharedMaterial.SetFloat("_CursorGratBoxInfo" + i, 0f);
					}
				}
				sharedMaterial.SetFloat("_Segment", (float)val.segment);
				sharedMaterial.SetColor("_TintColor", __instance.blueprintColor);
				sharedMaterial.SetVector("_AnchorPos", Vector4.zero);
				sharedMaterial.SetFloat("_ShowDivideLine", 0f);
				sharedMaterial.SetVector("_CursorGratBox", Vector4.zero);
				sharedMaterial.SetVector("_SelectColor", Vector4.one);
			}
			blueprintGridRnd.enabled = true;
		}
	}
	internal struct BuildPoserState
	{
		internal float zoomSensitivity;

		internal float weight;

		internal bool restoreWeight;
	}
	[HarmonyPatch(typeof(RTSPoser), "Calculate")]
	internal static class BuildPoserInputPatch
	{
		private static void Prefix(RTSPoser __instance, out BuildPoserState __state)
		{
			__state = new BuildPoserState
			{
				zoomSensitivity = GameCamera.camZoomSens,
				weight = __instance.weight,
				restoreWeight = false
			};
			Plugin instance = Plugin.Instance;
			GameCamera instance2 = GameCamera.instance;
			if (!((Object)(object)instance == (Object)null) && !((Object)(object)instance2 == (Object)null) && !((Object)(object)__instance != (Object)(object)instance2.buildPoser))
			{
				if (instance.ShouldUseBuildCameraPoseInBlueprintMode())
				{
					__instance.weight = 1f;
					__state.restoreWeight = true;
				}
				if (instance.ShouldSmoothBuildCameraZoom())
				{
					GameCamera.camZoomSens = __state.zoomSensitivity * 0.4f;
				}
			}
		}

		private static void Postfix(RTSPoser __instance, BuildPoserState __state)
		{
			GameCamera.camZoomSens = __state.zoomSensitivity;
			if (__state.restoreWeight)
			{
				__instance.weight = __state.weight;
			}
		}
	}
	[HarmonyPatch(typeof(GraticulePoser), "Calculate")]
	internal static class BlueprintCameraPosePatch
	{
		private static void Postfix(GraticulePoser __instance)
		{
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			Plugin instance = Plugin.Instance;
			GameCamera instance2 = GameCamera.instance;
			if (!((Object)(object)instance == (Object)null) && !((Object)(object)instance2 == (Object)null) && instance.ShouldUseBuildCameraPoseInBlueprintMode() && !((Object)(object)__instance != (Object)(object)instance2.blueprintPoser) && !((Object)(object)instance2.buildPoser == (Object)null))
			{
				((CameraPoser)__instance).cameraPose = ((CameraPoser)instance2.buildPoser).cameraPose;
			}
		}
	}
	[BepInPlugin("com.lokimoonlabs.buildbackbetter", "Build Back Better", "1.0.7")]
	public sealed class Plugin : BaseUnityPlugin
	{
		private Harmony harmony;

		private GameObject togglePanel;

		private Toggle blueprintToggle;

		private EBlueprintMode lastBlueprintMode;

		private bool moddedBlueprintSession;

		internal static Plugin Instance { get; private set; }

		internal bool BlueprintViewEnabled { get; private set; }

		internal bool InConstructMode { get; private set; }

		internal bool BlueprintGridNeedsReset { get; private set; }

		private void Awake()
		{
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Expected O, but got Unknown
			Instance = this;
			BlueprintViewEnabled = false;
			harmony = new Harmony("com.lokimoonlabs.buildbackbetter.patches");
			harmony.PatchAll();
		}

		private void OnDestroy()
		{
			Harmony obj = harmony;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
			if ((Object)(object)togglePanel != (Object)null)
			{
				Object.Destroy((Object)(object)togglePanel);
			}
			Instance = null;
		}

		internal bool ShouldApplyVisuals()
		{
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Invalid comparison between Unknown and I4
			if (!InConstructMode || !BlueprintViewEnabled)
			{
				return false;
			}
			Player mainPlayer = GameMain.mainPlayer;
			PlayerAction_Build val = ((mainPlayer == null) ? null : mainPlayer.controller?.actionBuild);
			if (val != null && val.active)
			{
				return (int)val.blueprintMode == 0;
			}
			return false;
		}

		internal bool ShouldUseBuildCameraPoseInBlueprintMode()
		{
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Invalid comparison between Unknown and I4
			if (!BlueprintViewEnabled)
			{
				return false;
			}
			Player mainPlayer = GameMain.mainPlayer;
			PlayerAction_Build val = ((mainPlayer == null) ? null : mainPlayer.controller?.actionBuild);
			if (val != null && val.active)
			{
				return (int)val.blueprintMode > 0;
			}
			return false;
		}

		internal bool ShouldSmoothBuildCameraZoom()
		{
			if (!ShouldApplyVisuals())
			{
				return ShouldUseBuildCameraPoseInBlueprintMode();
			}
			return true;
		}

		internal void TrackBlueprintMode(PlayerAction_Build actionBuild)
		{
			//IL_0005: 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)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: 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)
			if (actionBuild == null)
			{
				return;
			}
			EBlueprintMode blueprintMode = actionBuild.blueprintMode;
			if ((int)blueprintMode != 0 && BlueprintViewEnabled)
			{
				moddedBlueprintSession = true;
			}
			if ((int)lastBlueprintMode != 0 && (int)blueprintMode == 0 && moddedBlueprintSession)
			{
				try
				{
					Player mainPlayer = GameMain.mainPlayer;
					if (mainPlayer != null)
					{
						FactoryModel factoryModel = mainPlayer.factoryModel;
						if (factoryModel != null)
						{
							BPGPUInstancingManager bpgpuiManager = factoryModel.bpgpuiManager;
							if (bpgpuiManager != null)
							{
								bpgpuiManager.Reset();
							}
						}
					}
				}
				catch (Exception ex)
				{
					((BaseUnityPlugin)this).Logger.LogWarning((object)("Blueprint preview cleanup failed: " + ex.Message));
				}
				BlueprintGridNeedsReset = true;
				moddedBlueprintSession = false;
			}
			lastBlueprintMode = blueprintMode;
		}

		internal bool ConsumeBlueprintGridReset()
		{
			if (!BlueprintGridNeedsReset)
			{
				return false;
			}
			BlueprintGridNeedsReset = false;
			return true;
		}

		internal void SetConstructMode(bool active)
		{
			InConstructMode = active;
			if (active)
			{
				EnsureTogglePanel();
			}
			if ((Object)(object)togglePanel != (Object)null)
			{
				togglePanel.SetActive(active);
			}
			RefreshFactoryRenderState();
		}

		private void OnBlueprintToggleChanged(bool enabled)
		{
			BlueprintViewEnabled = enabled;
			RefreshFactoryRenderState();
		}

		internal void RefreshFactoryRenderState()
		{
			try
			{
				Player mainPlayer = GameMain.mainPlayer;
				if (mainPlayer != null)
				{
					FactoryModel factoryModel = mainPlayer.factoryModel;
					if (factoryModel != null)
					{
						factoryModel.SetGlobalRenderState();
					}
				}
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("Factory render refresh failed: " + ex.Message));
			}
		}

		private bool EnsureTogglePanel()
		{
			//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c7: Expected O, but got Unknown
			//IL_00ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_0104: Unknown result type (might be due to invalid IL or missing references)
			//IL_0119: Unknown result type (might be due to invalid IL or missing references)
			//IL_012e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0142: Unknown result type (might be due to invalid IL or missing references)
			//IL_016c: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b5: Expected O, but got Unknown
			//IL_01df: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_0209: Unknown result type (might be due to invalid IL or missing references)
			//IL_021e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0232: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_02bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_030d: Unknown result type (might be due to invalid IL or missing references)
			//IL_026f: Unknown result type (might be due to invalid IL or missing references)
			//IL_027d: Unknown result type (might be due to invalid IL or missing references)
			//IL_039d: Unknown result type (might be due to invalid IL or missing references)
			//IL_03af: Unknown result type (might be due to invalid IL or missing references)
			//IL_03c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_03c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_03d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_03e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_03f8: Expected O, but got Unknown
			//IL_0348: Unknown result type (might be due to invalid IL or missing references)
			//IL_0356: Unknown result type (might be due to invalid IL or missing references)
			//IL_046a: Unknown result type (might be due to invalid IL or missing references)
			//IL_046f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0486: Unknown result type (might be due to invalid IL or missing references)
			//IL_048d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0498: Unknown result type (might be due to invalid IL or missing references)
			//IL_04ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_04c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0509: Unknown result type (might be due to invalid IL or missing references)
			//IL_0501: Unknown result type (might be due to invalid IL or missing references)
			//IL_0561: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)togglePanel != (Object)null)
			{
				return true;
			}
			UIRoot instance = UIRoot.instance;
			UIGame val = instance?.uiGame;
			UIToggle val2 = instance?.optionWindow?.cameraRotateXToggle;
			Text val3 = val?.buildMenu?.blueprintTip;
			object obj;
			if (val == null)
			{
				obj = null;
			}
			else
			{
				UIFunctionPanel functionPanel = val.functionPanel;
				obj = ((functionPanel != null) ? ((Component)functionPanel).transform : null);
			}
			Transform val4 = (Transform)obj;
			Toggle val5 = (((Object)(object)val2 != (Object)null) ? ((Component)val2).GetComponent<Toggle>() : null);
			if ((Object)(object)val5 == (Object)null || (Object)(object)val4 == (Object)null)
			{
				return false;
			}
			togglePanel = new GameObject("Build Back Better Toggle", new Type[2]
			{
				typeof(RectTransform),
				typeof(Image)
			});
			togglePanel.transform.SetParent(val4, false);
			RectTransform component = togglePanel.GetComponent<RectTransform>();
			component.anchorMin = new Vector2(1f, 0f);
			component.anchorMax = new Vector2(1f, 0f);
			component.pivot = new Vector2(1f, 0f);
			component.sizeDelta = new Vector2(160f, 40f);
			component.anchoredPosition = new Vector2(28f, 42f);
			Image component2 = togglePanel.GetComponent<Image>();
			((Graphic)component2).color = new Color(0.125f, 0.125f, 0.125f, 0.85f);
			((Graphic)component2).raycastTarget = false;
			GameObject val6 = new GameObject("check-box", new Type[3]
			{
				typeof(RectTransform),
				typeof(Image),
				typeof(Toggle)
			});
			val6.transform.SetParent(togglePanel.transform, false);
			RectTransform component3 = val6.GetComponent<RectTransform>();
			component3.anchorMin = new Vector2(0f, 0.5f);
			component3.anchorMax = new Vector2(0f, 0.5f);
			component3.pivot = new Vector2(0f, 0.5f);
			component3.sizeDelta = new Vector2(22f, 22f);
			component3.anchoredPosition = new Vector2(8f, 0f);
			Graphic targetGraphic = ((Selectable)val5).targetGraphic;
			Image val7 = (Image)(object)((targetGraphic is Image) ? targetGraphic : null);
			Image component4 = val6.GetComponent<Image>();
			if ((Object)(object)val7 != (Object)null)
			{
				component4.sprite = val7.sprite;
				component4.type = val7.type;
				((Graphic)component4).color = ((Graphic)val7).color;
				((Graphic)component4).material = ((Graphic)val7).material;
			}
			GameObject val8 = new GameObject("checkmark", new Type[2]
			{
				typeof(RectTransform),
				typeof(Image)
			});
			val8.transform.SetParent(val6.transform, false);
			RectTransform component5 = val8.GetComponent<RectTransform>();
			component5.anchorMin = Vector2.zero;
			component5.anchorMax = Vector2.one;
			component5.offsetMin = new Vector2(3f, 3f);
			component5.offsetMax = new Vector2(-3f, -3f);
			Graphic graphic = val5.graphic;
			Image val9 = (Image)(object)((graphic is Image) ? graphic : null);
			Image component6 = val8.GetComponent<Image>();
			if ((Object)(object)val9 != (Object)null)
			{
				component6.sprite = val9.sprite;
				component6.type = val9.type;
				((Graphic)component6).color = ((Graphic)val9).color;
				((Graphic)component6).material = ((Graphic)val9).material;
			}
			blueprintToggle = val6.GetComponent<Toggle>();
			((Selectable)blueprintToggle).targetGraphic = (Graphic)(object)component4;
			blueprintToggle.graphic = (Graphic)(object)component6;
			((Selectable)blueprintToggle).transition = ((Selectable)val5).transition;
			((Selectable)blueprintToggle).colors = ((Selectable)val5).colors;
			blueprintToggle.toggleTransition = val5.toggleTransition;
			Toggle obj2 = blueprintToggle;
			Navigation navigation = default(Navigation);
			((Navigation)(ref navigation)).mode = (Mode)0;
			((Selectable)obj2).navigation = navigation;
			blueprintToggle.onValueChanged = new ToggleEvent();
			blueprintToggle.isOn = BlueprintViewEnabled;
			((Graphic)component6).canvasRenderer.SetAlpha(BlueprintViewEnabled ? 1f : 0f);
			((UnityEvent<bool>)(object)blueprintToggle.onValueChanged).AddListener((UnityAction<bool>)OnBlueprintToggleChanged);
			GameObject val10 = new GameObject("label", new Type[2]
			{
				typeof(RectTransform),
				typeof(Text)
			});
			val10.transform.SetParent(togglePanel.transform, false);
			RectTransform component7 = val10.GetComponent<RectTransform>();
			component7.anchorMin = Vector2.zero;
			component7.anchorMax = Vector2.one;
			component7.offsetMin = new Vector2(36f, 0f);
			component7.offsetMax = new Vector2(-6f, 0f);
			Text component8 = val10.GetComponent<Text>();
			component8.text = "Blueprint View";
			component8.alignment = (TextAnchor)3;
			((Graphic)component8).raycastTarget = false;
			component8.horizontalOverflow = (HorizontalWrapMode)1;
			component8.verticalOverflow = (VerticalWrapMode)1;
			((Graphic)component8).color = (((Object)(object)val3 != (Object)null) ? ((Graphic)val3).color : Color.white);
			component8.font = (((Object)(object)val3 != (Object)null) ? val3.font : Resources.GetBuiltinResource<Font>("Arial.ttf"));
			component8.fontSize = (((Object)(object)val3 != (Object)null) ? Math.Max(14, val3.fontSize) : 14);
			component8.fontStyle = (FontStyle)(((Object)(object)val3 != (Object)null) ? ((int)val3.fontStyle) : 0);
			togglePanel.SetActive(InConstructMode);
			return true;
		}
	}
}