Decompiled source of SuperNightLight v1.0.8

SuperNightLight.dll

Decompiled 2 days ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("SuperNightLight")]
[assembly: AssemblyDescription("Dyson Sphere Program Mod - Super Night Light")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SuperNightLight")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("2fb0ffb7-74da-4e0d-b2fa-32202036ed71")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
[BepInPlugin("AlphaDSPMod.SuperNightLight", "Super Night Light", "1.0.8")]
[BepInProcess("DSPGAME.exe")]
public class SuperNightLight : BaseUnityPlugin
{
	public ConfigEntry<KeyboardShortcut> mEnableKey;

	public static bool mIsEnable;

	private AnimationState mSail;

	private Light mSunLight;

	private int mPhase;

	private void Start()
	{
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Mod Installing.");
		mEnableKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys/快捷键", "Turn On/Off", new KeyboardShortcut((KeyCode)108, (KeyCode[])(object)new KeyCode[1] { (KeyCode)306 }), (ConfigDescription)null);
		mIsEnable = false;
		mPhase = 1;
		Harmony.CreateAndPatchAll(typeof(SuperNightLight), (string)null);
	}

	private void LateUpdate()
	{
		switch (mPhase)
		{
		case 1:
			Ready();
			break;
		case 2:
			Go();
			break;
		}
	}

	private void Ready()
	{
		if (GameMain.isRunning && ((Component)GameMain.mainPlayer.controller.model).gameObject.activeInHierarchy)
		{
			if ((TrackedReference)(object)mSail == (TrackedReference)null)
			{
				mSail = GameMain.mainPlayer.animator.sails[0];
			}
			mPhase = 2;
		}
	}

	private void Go()
	{
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0034: 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_00c3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
		//IL_0098: Unknown result type (might be due to invalid IL or missing references)
		if (!GameMain.isRunning)
		{
			End();
			return;
		}
		if (mSail.enabled)
		{
			if (mIsEnable)
			{
				Reset();
			}
			return;
		}
		KeyboardShortcut value = mEnableKey.Value;
		if (((KeyboardShortcut)(ref value)).IsDown())
		{
			if ((Object)(object)mSunLight == (Object)null)
			{
				mSunLight = GameMain.universeSimulator.LocalStarSimulator().sunLight;
			}
			if ((Object)(object)mSunLight != (Object)null)
			{
				mIsEnable = !mIsEnable;
				if (!mIsEnable)
				{
					((Component)mSunLight).transform.localEulerAngles = new Vector3(0f, 180f);
				}
			}
		}
		if (mIsEnable)
		{
			((Component)mSunLight).transform.rotation = Quaternion.LookRotation(-GameMain.mainPlayer.transform.up);
		}
	}

	private void Reset()
	{
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		if (mIsEnable)
		{
			mIsEnable = false;
		}
		if ((Object)(object)mSunLight != (Object)null)
		{
			((Component)mSunLight).transform.localEulerAngles = new Vector3(0f, 180f);
			mSunLight = null;
		}
	}

	private void End()
	{
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		if (mIsEnable)
		{
			mIsEnable = false;
		}
		if ((Object)(object)mSunLight != (Object)null)
		{
			((Component)mSunLight).transform.localEulerAngles = new Vector3(0f, 180f);
			mSunLight = null;
		}
		if ((TrackedReference)(object)mSail != (TrackedReference)null)
		{
			mSail = null;
		}
		if (mPhase != 1)
		{
			mPhase = 1;
		}
	}

	private void OnDestroy()
	{
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Mod Uninstalled.");
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(StarSimulator), "LateUpdate")]
	public static bool SSLateUpdatePatch(StarSimulator __instance, Material ___bodyMaterial, Material ___haloMaterial)
	{
		//IL_0102: 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_0112: Unknown result type (might be due to invalid IL or missing references)
		//IL_0117: Unknown result type (might be due to invalid IL or missing references)
		//IL_011c: Unknown result type (might be due to invalid IL or missing references)
		//IL_011f: 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_012d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0132: Unknown result type (might be due to invalid IL or missing references)
		//IL_014b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0150: Unknown result type (might be due to invalid IL or missing references)
		//IL_0155: Unknown result type (might be due to invalid IL or missing references)
		//IL_015b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0161: Unknown result type (might be due to invalid IL or missing references)
		//IL_0167: Unknown result type (might be due to invalid IL or missing references)
		//IL_0172: Unknown result type (might be due to invalid IL or missing references)
		//IL_0181: Unknown result type (might be due to invalid IL or missing references)
		//IL_0190: Unknown result type (might be due to invalid IL or missing references)
		//IL_019f: Unknown result type (might be due to invalid IL or missing references)
		//IL_005b: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d0: 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_0066: Unknown result type (might be due to invalid IL or missing references)
		//IL_006c: 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)
		//IL_007d: Unknown result type (might be due to invalid IL or missing references)
		//IL_008c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0092: Unknown result type (might be due to invalid IL or missing references)
		//IL_009d: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ac: 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_00bd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
		//IL_0231: Unknown result type (might be due to invalid IL or missing references)
		//IL_0237: Invalid comparison between Unknown and I4
		((Behaviour)__instance.sunLight).enabled = GameMain.localStar == __instance.starData && !FactoryModel.whiteMode0;
		if (GameMain.localStar == __instance.starData)
		{
			if (!FactoryModel.whiteMode0)
			{
				Vector3 val = (mIsEnable ? GameMain.mainPlayer.transform.up : ((Component)__instance).transform.forward);
				Shader.SetGlobalVector("_Global_SunDir", new Vector4(val.x, val.y, val.z, 0f));
				Shader.SetGlobalColor("_Global_SunsetColor0", Color.Lerp(Color.white, __instance.sunsetColor0, __instance.useSunsetColor));
				Shader.SetGlobalColor("_Global_SunsetColor1", Color.Lerp(Color.white, __instance.sunsetColor1, __instance.useSunsetColor));
				Shader.SetGlobalColor("_Global_SunsetColor2", Color.Lerp(Color.white, __instance.sunsetColor2, __instance.useSunsetColor));
			}
			else
			{
				Transform transform = ((Component)GameCamera.instance.camLight).transform;
				Vector3 val2 = GameMain.mainPlayer.position * 0.75f - transform.position;
				Vector3 normalized = ((Vector3)(ref val2)).normalized;
				val2 = transform.position;
				transform.rotation = Quaternion.LookRotation(normalized, ((Vector3)(ref val2)).normalized);
				Vector3 val3 = -((Component)GameCamera.instance.camLight).transform.forward;
				Shader.SetGlobalVector("_Global_SunDir", new Vector4(val3.x, val3.y, val3.z, 0f));
				Shader.SetGlobalColor("_Global_SunsetColor0", Color.white);
				Shader.SetGlobalColor("_Global_SunsetColor1", Color.white);
				Shader.SetGlobalColor("_Global_SunsetColor2", Color.white);
			}
			if (((Component)__instance.atmosRenderer).gameObject.activeSelf)
			{
				((Component)__instance.atmosRenderer).transform.rotation = ((Component)GameCamera.main).transform.rotation;
			}
		}
		___bodyMaterial.renderQueue = ((GameMain.localStar == __instance.starData) ? 2981 : 2979);
		___haloMaterial.renderQueue = ((GameMain.localStar == __instance.starData) ? 2981 : 2979);
		__instance.blackRenderer.enabled = GameMain.localStar == __instance.starData && (int)__instance.starData.type != 4;
		SSGpuAnalysisPatch(__instance);
		return false;
	}

	public static void SSGpuAnalysisPatch(StarSimulator starSimulator)
	{
		if (PerformanceMonitor.GpuProfilerOn)
		{
			Renderer val = null;
			Mesh val2 = null;
			val = starSimulator.bodyRenderer;
			if ((Object)(object)val != (Object)null && val.enabled && ((Component)val).gameObject.activeInHierarchy)
			{
				val2 = ((Component)val).GetComponent<MeshFilter>().sharedMesh;
				PerformanceMonitor.RecordGpuWork((EGpuWorkEntry)2, 1, val2.vertexCount);
			}
			val = starSimulator.haloRenderer;
			if ((Object)(object)val != (Object)null && val.enabled && ((Component)val).gameObject.activeInHierarchy)
			{
				val2 = ((Component)val).GetComponent<MeshFilter>().sharedMesh;
				PerformanceMonitor.RecordGpuWork((EGpuWorkEntry)2, 1, val2.vertexCount);
			}
			val = starSimulator.massRenderer;
			if ((Object)(object)val != (Object)null && val.enabled && ((Component)val).gameObject.activeInHierarchy)
			{
				val2 = ((Component)val).GetComponent<MeshFilter>().sharedMesh;
				PerformanceMonitor.RecordGpuWork((EGpuWorkEntry)2, 1, val2.vertexCount);
			}
			val = starSimulator.blackRenderer;
			if ((Object)(object)val != (Object)null && val.enabled && ((Component)val).gameObject.activeInHierarchy)
			{
				val2 = ((Component)val).GetComponent<MeshFilter>().sharedMesh;
				PerformanceMonitor.RecordGpuWork((EGpuWorkEntry)2, 1, val2.vertexCount);
			}
			val = starSimulator.atmosRenderer;
			if ((Object)(object)val != (Object)null && val.enabled && ((Component)val).gameObject.activeInHierarchy)
			{
				val2 = ((Component)val).GetComponent<MeshFilter>().sharedMesh;
				PerformanceMonitor.RecordGpuWork((EGpuWorkEntry)2, 1, val2.vertexCount);
			}
			if ((Object)(object)starSimulator.effect != (Object)null && ((Component)starSimulator.effect).gameObject.activeInHierarchy && starSimulator.effect.isPlaying)
			{
				PerformanceMonitor.RecordGpuWork((EGpuWorkEntry)2, 1, starSimulator.effect.particleCount * 4);
			}
		}
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(PlanetSimulator), "LateUpdate")]
	public static bool PSLateUpdatePatch(PlanetSimulator __instance)
	{
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_0009: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		//IL_0062: 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_001f: Unknown result type (might be due to invalid IL or missing references)
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0044: Unknown result type (might be due to invalid IL or missing references)
		//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_0051: Unknown result type (might be due to invalid IL or missing references)
		//IL_0067: Unknown result type (might be due to invalid IL or missing references)
		//IL_007e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: Unknown result type (might be due to invalid IL or missing references)
		//IL_0088: Unknown result type (might be due to invalid IL or missing references)
		//IL_0207: Unknown result type (might be due to invalid IL or missing references)
		//IL_0227: Unknown result type (might be due to invalid IL or missing references)
		//IL_0247: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e3: 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_00bf: 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)
		//IL_0107: 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_012b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0146: Unknown result type (might be due to invalid IL or missing references)
		//IL_014b: Unknown result type (might be due to invalid IL or missing references)
		//IL_02c3: 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_016f: Unknown result type (might be due to invalid IL or missing references)
		//IL_018a: Unknown result type (might be due to invalid IL or missing references)
		//IL_018f: Unknown result type (might be due to invalid IL or missing references)
		//IL_02e6: Unknown result type (might be due to invalid IL or missing references)
		//IL_02df: Unknown result type (might be due to invalid IL or missing references)
		//IL_02eb: Unknown result type (might be due to invalid IL or missing references)
		//IL_02f0: Unknown result type (might be due to invalid IL or missing references)
		//IL_02fc: Unknown result type (might be due to invalid IL or missing references)
		//IL_0301: Unknown result type (might be due to invalid IL or missing references)
		//IL_0370: Unknown result type (might be due to invalid IL or missing references)
		//IL_037b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0380: Unknown result type (might be due to invalid IL or missing references)
		//IL_0385: Unknown result type (might be due to invalid IL or missing references)
		//IL_0396: Unknown result type (might be due to invalid IL or missing references)
		//IL_039b: Unknown result type (might be due to invalid IL or missing references)
		//IL_03a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_03a6: Unknown result type (might be due to invalid IL or missing references)
		//IL_0362: Unknown result type (might be due to invalid IL or missing references)
		//IL_0367: Unknown result type (might be due to invalid IL or missing references)
		//IL_031d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0329: Unknown result type (might be due to invalid IL or missing references)
		//IL_0333: Unknown result type (might be due to invalid IL or missing references)
		//IL_0338: Unknown result type (might be due to invalid IL or missing references)
		//IL_033d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0342: Unknown result type (might be due to invalid IL or missing references)
		//IL_0346: 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_03ee: Unknown result type (might be due to invalid IL or missing references)
		//IL_03f5: Unknown result type (might be due to invalid IL or missing references)
		//IL_03fc: Unknown result type (might be due to invalid IL or missing references)
		//IL_0403: Unknown result type (might be due to invalid IL or missing references)
		//IL_0417: Unknown result type (might be due to invalid IL or missing references)
		//IL_041b: Unknown result type (might be due to invalid IL or missing references)
		//IL_044e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0491: Unknown result type (might be due to invalid IL or missing references)
		//IL_0496: Unknown result type (might be due to invalid IL or missing references)
		//IL_049a: Unknown result type (might be due to invalid IL or missing references)
		//IL_04cd: Unknown result type (might be due to invalid IL or missing references)
		//IL_04d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_04d6: Unknown result type (might be due to invalid IL or missing references)
		//IL_04e9: Unknown result type (might be due to invalid IL or missing references)
		//IL_04ee: Unknown result type (might be due to invalid IL or missing references)
		//IL_0503: Unknown result type (might be due to invalid IL or missing references)
		//IL_0504: Unknown result type (might be due to invalid IL or missing references)
		//IL_0519: Unknown result type (might be due to invalid IL or missing references)
		//IL_052d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0544: Unknown result type (might be due to invalid IL or missing references)
		//IL_0559: Unknown result type (might be due to invalid IL or missing references)
		//IL_03db: Unknown result type (might be due to invalid IL or missing references)
		//IL_03e2: Unknown result type (might be due to invalid IL or missing references)
		//IL_03e7: Unknown result type (might be due to invalid IL or missing references)
		//IL_03ec: Unknown result type (might be due to invalid IL or missing references)
		//IL_03b9: Unknown result type (might be due to invalid IL or missing references)
		//IL_03bc: 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_03c8: Unknown result type (might be due to invalid IL or missing references)
		//IL_03c9: Unknown result type (might be due to invalid IL or missing references)
		//IL_03cb: Unknown result type (might be due to invalid IL or missing references)
		//IL_03d0: Unknown result type (might be due to invalid IL or missing references)
		//IL_03b0: Unknown result type (might be due to invalid IL or missing references)
		//IL_03b5: Unknown result type (might be due to invalid IL or missing references)
		//IL_036c: Unknown result type (might be due to invalid IL or missing references)
		//IL_05e9: Unknown result type (might be due to invalid IL or missing references)
		//IL_05ee: Unknown result type (might be due to invalid IL or missing references)
		//IL_0603: Unknown result type (might be due to invalid IL or missing references)
		//IL_0604: Unknown result type (might be due to invalid IL or missing references)
		//IL_0619: Unknown result type (might be due to invalid IL or missing references)
		//IL_062d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0644: Unknown result type (might be due to invalid IL or missing references)
		//IL_0659: Unknown result type (might be due to invalid IL or missing references)
		PlanetData localPlanet = GameMain.localPlanet;
		Quaternion val = localPlanet?.runtimeRotation ?? Quaternion.identity;
		Vector3 val4;
		if (!mIsEnable)
		{
			Quaternion val2 = Quaternion.Inverse(val);
			VectorLF3 val3 = __instance.planetData.star.uPosition - __instance.planetData.uPosition;
			val4 = val2 * VectorLF3.op_Implicit(((VectorLF3)(ref val3)).normalized);
		}
		else
		{
			val4 = GameMain.mainPlayer.transform.up;
		}
		Vector3 val5 = val4;
		if (FactoryModel.whiteMode0)
		{
			val5 = -((Component)GameCamera.instance.camLight).transform.forward;
		}
		if (localPlanet == __instance.planetData && localPlanet != null)
		{
			if (__instance.surfaceRenderer != null && __instance.surfaceRenderer.Length != 0)
			{
				__instance.surfaceRenderer[0].sharedMaterial.SetVector("_SunDir", Vector4.op_Implicit(val5));
			}
			if ((Object)(object)__instance.reformMat0 != (Object)null)
			{
				__instance.reformMat0.SetVector("_SunDir", Vector4.op_Implicit(val5));
			}
			if ((Object)(object)__instance.reformMat1 != (Object)null)
			{
				__instance.reformMat1.SetVector("_SunDir", Vector4.op_Implicit(val5));
			}
			if ((Object)(object)__instance.atmoMat != (Object)null)
			{
				__instance.atmoMat.SetVector("_SunDir", Vector4.op_Implicit(val5));
				__instance.atmoMat.SetVector("_PlanetPos", Vector4.op_Implicit(((Component)__instance).transform.localPosition));
			}
			if ((Object)(object)__instance.atmoMatLate != (Object)null)
			{
				__instance.atmoMatLate.SetVector("_SunDir", Vector4.op_Implicit(val5));
				__instance.atmoMatLate.SetVector("_PlanetPos", Vector4.op_Implicit(((Component)__instance).transform.localPosition));
			}
		}
		if ((Object)(object)__instance.cloudSimulator != (Object)null)
		{
			__instance.cloudSimulator.OnLateUpdate();
		}
		if ((Object)(object)__instance.oceanMat != (Object)null)
		{
			if (__instance.oceanRenderQueue > 2987)
			{
				__instance.oceanMat.renderQueue = ((localPlanet == __instance.planetData) ? __instance.oceanRenderQueue : 2988);
			}
			__instance.oceanMat.SetColor("_Planet_WaterAmbientColor0", __instance.planetData.ambientDesc.waterAmbientColor0);
			__instance.oceanMat.SetColor("_Planet_WaterAmbientColor1", __instance.planetData.ambientDesc.waterAmbientColor1);
			__instance.oceanMat.SetColor("_Planet_WaterAmbientColor2", __instance.planetData.ambientDesc.waterAmbientColor2);
		}
		if ((Object)(object)__instance.atmoTrans0 != (Object)null && __instance.planetData != null && !__instance.planetData.loading && !__instance.planetData.factoryLoading)
		{
			Transform value = Traverse.Create((object)__instance).Field("lookCamera").GetValue<Transform>();
			StarSimulator value2 = Traverse.Create((object)__instance).Field("star").GetValue<StarSimulator>();
			__instance.atmoTrans0.rotation = value.localRotation;
			Vector4 val6 = Vector4.op_Implicit(((Object)(object)GameCamera.generalTarget == (Object)null) ? Vector3.zero : GameCamera.generalTarget.position);
			Vector3 position = ((Component)GameCamera.main).transform.position;
			if (((Vector4)(ref val6)).sqrMagnitude == 0f)
			{
				Vector4 val8;
				if (GameCamera.instance.isPlanetMode)
				{
					Vector4 val7 = Vector4.op_Implicit(position + ((Component)GameCamera.main).transform.forward * 30f);
					val8 = ((Vector4)(ref val7)).normalized * __instance.planetData.realRadius;
				}
				else
				{
					val8 = Vector4.op_Implicit(GameMain.mainPlayer.position);
				}
				val6 = val8;
			}
			Vector3 val9 = value.localPosition - ((Component)__instance).transform.localPosition;
			float magnitude = ((Vector3)(ref val9)).magnitude;
			VectorLF3 val10 = __instance.planetData.uPosition;
			if (localPlanet != null)
			{
				val = localPlanet.runtimeRotation;
				if (localPlanet == __instance.planetData)
				{
					val10 = VectorLF3.zero;
				}
				else
				{
					val10 -= localPlanet.uPosition;
					val10 = Maths.QInvRotateLF(val, val10);
				}
			}
			else if (GameMain.mainPlayer != null)
			{
				val10 -= GameMain.mainPlayer.uPosition;
			}
			VirtualMapping(val10.x, val10.y, val10.z, position, out var _, out var vscale);
			float num = Vector3.Dot(val9, value.forward);
			__instance.atmoTrans1.localPosition = new Vector3(0f, 0f, Mathf.Clamp(num + 10f, 0f, 320f));
			float num2 = Mathf.Clamp01(8000f / magnitude);
			float num3 = Mathf.Clamp01(4000f / magnitude);
			float num4 = Mathf.Max(0f, magnitude / 6000f - 1f);
			Vector4 atmoMatRadiusParam = __instance.atmoMatRadiusParam;
			atmoMatRadiusParam.z = atmoMatRadiusParam.x + (__instance.atmoMatRadiusParam.z - __instance.atmoMatRadiusParam.x) * (2.7f - num3 * 1.7f);
			atmoMatRadiusParam *= vscale;
			__instance.atmoMat.SetVector("_PlanetPos", Vector4.op_Implicit(((Component)__instance).transform.localPosition));
			__instance.atmoMat.SetVector("_SunDir", Vector4.op_Implicit(val5));
			__instance.atmoMat.SetVector("_PlanetRadius", atmoMatRadiusParam);
			__instance.atmoMat.SetColor("_Color4", value2.sunAtmosColor);
			__instance.atmoMat.SetColor("_Sky4", value2.sunriseAtmosColor);
			__instance.atmoMat.SetVector("_LocalPos", val6);
			__instance.atmoMat.SetFloat("_SunRiseScatterPower", Mathf.Max(60f, (magnitude - __instance.planetData.realRadius * 2f) * 0.18f));
			__instance.atmoMat.SetFloat("_IntensityControl", num2);
			__instance.atmoMat.SetFloat("_DistanceControl", num4);
			__instance.atmoMat.renderQueue = ((__instance.planetData == localPlanet) ? 2991 : 2989);
			__instance.atmoMatLate.SetVector("_PlanetPos", Vector4.op_Implicit(((Component)__instance).transform.localPosition));
			__instance.atmoMatLate.SetVector("_SunDir", Vector4.op_Implicit(val5));
			__instance.atmoMatLate.SetVector("_PlanetRadius", atmoMatRadiusParam);
			__instance.atmoMatLate.SetColor("_Color4", value2.sunAtmosColor);
			__instance.atmoMatLate.SetColor("_Sky4", value2.sunriseAtmosColor);
			__instance.atmoMatLate.SetVector("_LocalPos", val6);
			__instance.atmoMatLate.SetFloat("_SunRiseScatterPower", Mathf.Max(60f, (magnitude - __instance.planetData.realRadius * 2f) * 0.18f));
			__instance.atmoMatLate.SetFloat("_IntensityControl", num2);
			__instance.atmoMatLate.SetFloat("_DistanceControl", num4);
			if (__instance.planetData == localPlanet)
			{
				__instance.atmoMatLate.renderQueue = 3200;
				__instance.atmoMatLate.SetInt("_StencilRef", 2);
				__instance.atmoMatLate.SetInt("_StencilComp", 3);
			}
			else
			{
				__instance.atmoMatLate.renderQueue = 2989;
				__instance.atmoMatLate.SetInt("_StencilRef", 0);
				__instance.atmoMatLate.SetInt("_StencilComp", 1);
			}
		}
		PSGpuAnalysisPatch(__instance);
		return false;
	}

	public static void VirtualMapping(double x, double y, double z, Vector3 campos, out Vector3 vpos, out float vscale, double mappingDist = 10000.0)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		//IL_0091: 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_00b7: Unknown result type (might be due to invalid IL or missing references)
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_004b: 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)
		x -= (double)campos.x;
		y -= (double)campos.y;
		z -= (double)campos.z;
		double num = Math.Sqrt(x * x + y * y + z * z);
		if (num < mappingDist)
		{
			vpos.x = (float)(x + (double)campos.x);
			vpos.y = (float)(y + (double)campos.y);
			vpos.z = (float)(z + (double)campos.z);
			vscale = 1f;
		}
		else
		{
			double num2 = (Math.Log(num / mappingDist) + 1.0) * mappingDist / num;
			vpos.x = (float)(x * num2 + (double)campos.x);
			vpos.y = (float)(y * num2 + (double)campos.y);
			vpos.z = (float)(z * num2 + (double)campos.z);
			vscale = (float)num2;
		}
	}

	public static void PSGpuAnalysisPatch(PlanetSimulator planetSimulator)
	{
		if (!PerformanceMonitor.GpuProfilerOn)
		{
			return;
		}
		if (planetSimulator.surfaceRenderer != null)
		{
			Renderer[] surfaceRenderer = planetSimulator.surfaceRenderer;
			foreach (Renderer val in surfaceRenderer)
			{
				if ((Object)(object)val != (Object)null && val.enabled && ((Component)val).gameObject.activeInHierarchy)
				{
					PerformanceMonitor.RecordGpuWork((EGpuWorkEntry)2, 1, ((Component)val).GetComponent<MeshFilter>().sharedMesh.vertexCount);
				}
			}
		}
		if ((Object)(object)planetSimulator.reformRenderer != (Object)null && planetSimulator.reformRenderer.enabled && ((Component)planetSimulator.reformRenderer).gameObject.activeInHierarchy)
		{
			PerformanceMonitor.RecordGpuWork((EGpuWorkEntry)2, 1, ((Component)planetSimulator.reformRenderer).GetComponent<MeshFilter>().sharedMesh.vertexCount);
		}
		if ((Object)(object)planetSimulator.oceanCollider != (Object)null && ((Component)planetSimulator.oceanCollider).gameObject.activeInHierarchy)
		{
			MeshFilter component = ((Component)planetSimulator.oceanCollider).GetComponent<MeshFilter>();
			if ((Object)(object)component != (Object)null)
			{
				PerformanceMonitor.RecordGpuWork((EGpuWorkEntry)2, 1, component.sharedMesh.vertexCount);
			}
		}
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(CloudSimulator), "OnLateUpdate")]
	public static bool CSOnLateUpdatePatch(CloudSimulator __instance)
	{
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Invalid comparison between Unknown and I4
		//IL_0097: Unknown result type (might be due to invalid IL or missing references)
		//IL_009c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
		//IL_00db: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
		//IL_010b: Unknown result type (might be due to invalid IL or missing references)
		//IL_010d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0127: Unknown result type (might be due to invalid IL or missing references)
		//IL_0128: 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_01a2: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a4: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ee: Unknown result type (might be due to invalid IL or missing references)
		//IL_020a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0226: Unknown result type (might be due to invalid IL or missing references)
		//IL_0247: Unknown result type (might be due to invalid IL or missing references)
		//IL_024c: Unknown result type (might be due to invalid IL or missing references)
		//IL_025b: Unknown result type (might be due to invalid IL or missing references)
		bool flag = GameMain.mainPlayer != null && (int)GameMain.mainPlayer.controller.actionBuild.blueprintMode > 0;
		__instance.nephogram.enabled = CloudSimulator.renderNephogram && !flag;
		if (CloudSimulator.renderNephogram || CloudSimulator.renderCloudParticles)
		{
			int num = __instance.planet.seed % 4096;
			double gameTime = GameMain.gameTime;
			gameTime += (double)(num * 10);
			gameTime /= 900.0;
			gameTime = gameTime % 288.0 + (double)__instance.timeOffset;
			Vector3 localPosition = ((Component)__instance.planetSimulator).transform.localPosition;
			Vector4 val = SRotVector(((Component)__instance.planetSimulator).transform.localRotation, ((Component)__instance.planetSimulator).transform.localScale.x);
			Vector3 val2 = (mIsEnable ? GameMain.mainPlayer.transform.up : ((Vector3)(ref __instance.planet.runtimeLocalSunDirection)).normalized);
			if (__instance.cloudr != null)
			{
				__instance.cloudr.material.SetVector("_LocalSunDirection", Vector4.op_Implicit(val2));
				__instance.cloudr.material.SetVector("_PlanetRPos", Vector4.op_Implicit(localPosition));
				__instance.cloudr.material.SetVector("_PlanetRRot", val);
			}
			if (CloudSimulator.renderNephogram)
			{
				float num2 = __instance.planet.atmosphereHeight * 0.355f + __instance.planet.realRadius;
				Material nephogramMaterial = __instance.planet.nephogramMaterial;
				nephogramMaterial.SetFloat("_Seed", (float)num);
				nephogramMaterial.SetFloat("_CloudTime", (float)gameTime);
				nephogramMaterial.SetVector("_LocalSunDirection", Vector4.op_Implicit(val2));
				nephogramMaterial.SetVector("_PlanetRPos", Vector4.op_Implicit(localPosition));
				nephogramMaterial.SetVector("_PlanetRRot", val);
				nephogramMaterial.SetFloat("_NephogramRadius", num2);
				nephogramMaterial.SetColor("_Planet_AmbientColor0", __instance.planet.ambientDesc.ambientColor0);
				nephogramMaterial.SetColor("_Planet_AmbientColor1", __instance.planet.ambientDesc.ambientColor1);
				nephogramMaterial.SetColor("_Planet_AmbientColor2", __instance.planet.ambientDesc.ambientColor2);
				if (GameMain.localPlanet == __instance.planet)
				{
					Vector3 position = ((Component)GameCamera.main).transform.position;
					if (((Vector3)(ref position)).magnitude > ((Component)__instance).transform.localScale.x / 2f)
					{
						nephogramMaterial.renderQueue = 3004;
					}
					else
					{
						nephogramMaterial.renderQueue = 2996;
					}
				}
				else
				{
					nephogramMaterial.renderQueue = 2990;
				}
			}
			CSGpuAnalysisPatch(__instance);
		}
		return false;
	}

	public static Vector4 SRotVector(Quaternion q, float s)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		return new Vector4(q.x * s, q.y * s, q.z * s, q.w * s);
	}

	public static void CSGpuAnalysisPatch(CloudSimulator planetSimulator)
	{
		if (PerformanceMonitor.GpuProfilerOn && (Object)(object)planetSimulator.nephogram != (Object)null && planetSimulator.nephogram.enabled && ((Component)planetSimulator.nephogram).gameObject.activeInHierarchy)
		{
			PerformanceMonitor.RecordGpuWork((EGpuWorkEntry)3, 1, ((Component)planetSimulator.nephogram).GetComponent<MeshFilter>().sharedMesh.vertexCount);
		}
	}
}