Decompiled source of IvoryMoon v1.1.3

BepInEx/plugins/IvoryMoon/IvoryMoon.dll

Decompiled a day ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using Microsoft.CodeAnalysis;
using SoftReferenceableAssets;
using UnityEngine;
using UnityEngine.Rendering;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyVersion("0.0.0.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 IvoryMoon
{
	[BepInPlugin("local.ivorymoon", "Ivory Moon", "1.1.2")]
	public sealed class Plugin : BaseUnityPlugin
	{
		private ConfigEntry<float> size;

		private ConfigEntry<float> brightness;

		private Texture2D texture;

		private Material original;

		private Material replacement;

		private Material moonMaterial;

		private Mesh quad;

		private Camera targetCamera;

		private CommandBuffer commands;

		private float originalRadius;

		private int drawPass;

		private bool warned;

		private SoftReference<Shader> shaderReference;

		private bool shaderHeld;

		private static readonly int FogParams = Shader.PropertyToID("unity_FogParams");

		private static readonly int SunDirection = Shader.PropertyToID("_SkyboxSunDir");

		private void Awake()
		{
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Expected O, but got Unknown
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Expected O, but got Unknown
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0094: Expected O, but got Unknown
			//IL_0109: Unknown result type (might be due to invalid IL or missing references)
			//IL_010e: Unknown result type (might be due to invalid IL or missing references)
			//IL_011e: Expected O, but got Unknown
			//IL_013b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0140: Unknown result type (might be due to invalid IL or missing references)
			//IL_0156: 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_0171: Unknown result type (might be due to invalid IL or missing references)
			//IL_0176: Unknown result type (might be due to invalid IL or missing references)
			//IL_018c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0191: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_01df: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_0212: Unknown result type (might be due to invalid IL or missing references)
			//IL_0217: 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_0223: Unknown result type (might be due to invalid IL or missing references)
			//IL_022a: Unknown result type (might be due to invalid IL or missing references)
			//IL_022f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0236: Unknown result type (might be due to invalid IL or missing references)
			//IL_023b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0253: Unknown result type (might be due to invalid IL or missing references)
			//IL_0258: Unknown result type (might be due to invalid IL or missing references)
			//IL_025f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0264: Unknown result type (might be due to invalid IL or missing references)
			//IL_026b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0270: Unknown result type (might be due to invalid IL or missing references)
			//IL_0277: Unknown result type (might be due to invalid IL or missing references)
			//IL_027c: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_02cf: Expected O, but got Unknown
			//IL_02cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d9: Expected O, but got Unknown
			if (Application.isBatchMode)
			{
				return;
			}
			size = ((BaseUnityPlugin)this).Config.Bind<float>("Appearance", "SizeMultiplier", 1.25f, new ConfigDescription("Размер относительно штатной луны.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.5f, 2f), Array.Empty<object>()));
			brightness = ((BaseUnityPlugin)this).Config.Bind<float>("Appearance", "Brightness", 1f, new ConfigDescription("Яркость серебристой текстуры луны.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.2f, 2f), Array.Empty<object>()));
			try
			{
				texture = new Texture2D(2, 2, (TextureFormat)4, true, false);
				string path = Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location), "Assets", "moon.png");
				if (!ImageConversion.LoadImage(texture, File.ReadAllBytes(path), false))
				{
					throw new InvalidDataException("Cannot decode moon.png");
				}
				((Object)texture).name = "IvoryMoon_Texture";
				((Texture)texture).wrapMode = (TextureWrapMode)1;
				((Texture)texture).filterMode = (FilterMode)2;
				texture.Apply(true, true);
				quad = new Mesh
				{
					name = "IvoryMoon_SkyDisk"
				};
				quad.vertices = (Vector3[])(object)new Vector3[4]
				{
					new Vector3(-0.5f, -0.5f, 0f),
					new Vector3(0.5f, -0.5f, 0f),
					new Vector3(0.5f, 0.5f, 0f),
					new Vector3(-0.5f, 0.5f, 0f)
				};
				quad.uv = (Vector2[])(object)new Vector2[4]
				{
					new Vector2(0f, 0f),
					new Vector2(1f, 0f),
					new Vector2(1f, 1f),
					new Vector2(0f, 1f)
				};
				quad.colors = (Color[])(object)new Color[4]
				{
					Color.white,
					Color.white,
					Color.white,
					Color.white
				};
				quad.normals = (Vector3[])(object)new Vector3[4]
				{
					Vector3.back,
					Vector3.back,
					Vector3.back,
					Vector3.back
				};
				quad.triangles = new int[6] { 0, 2, 1, 0, 3, 2 };
				quad.RecalculateBounds();
				quad.UploadMeshData(true);
				Camera.onPreCull = (CameraCallback)Delegate.Combine((Delegate?)(object)Camera.onPreCull, (Delegate?)new CameraCallback(BeforeCamera));
				((MonoBehaviour)this).InvokeRepeating("CheckSky", 1f, 2f);
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Ivory Moon 1.1.2 loaded: explicit shader loading and render probe.");
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Moon setup failed; original sky preserved: " + ex));
				((Behaviour)this).enabled = false;
			}
		}

		private unsafe void CheckSky()
		{
			//IL_0058: 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_005e: 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)
			//IL_007b: 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_03c6: Expected O, but got Unknown
			//IL_03e6: 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_011e: Unknown result type (might be due to invalid IL or missing references)
			//IL_012e: Expected O, but got Unknown
			//IL_0211: Unknown result type (might be due to invalid IL or missing references)
			//IL_0216: Unknown result type (might be due to invalid IL or missing references)
			//IL_021b: Unknown result type (might be due to invalid IL or missing references)
			if (!Object.op_Implicit((Object)(object)EnvMan.instance))
			{
				ReleaseSky();
				DetachCamera();
				return;
			}
			if (!Object.op_Implicit((Object)(object)moonMaterial))
			{
				Shader val = Shader.Find("Particles/Standard Unlit2");
				if (!Object.op_Implicit((Object)(object)val) && !shaderHeld)
				{
					AssetID val2 = default(AssetID);
					AssetID.TryParse("0f5a6e59b3eb19f3ba0b7e7955445af0", ref val2);
					shaderReference = new SoftReference<Shader>(val2);
					if (shaderReference.IsValid)
					{
						LoadResult val3 = shaderReference.Load();
						shaderHeld = true;
						val = shaderReference.Asset;
						((BaseUnityPlugin)this).Logger.LogInfo((object)("Explicit shader load: " + ((object)(*(LoadResult*)(&val3))/*cast due to .constrained prefix*/).ToString() + "; shader=" + (Object.op_Implicit((Object)(object)val) ? ((Object)val).name : "null")));
					}
				}
				if (!Object.op_Implicit((Object)(object)val) && shaderHeld)
				{
					val = shaderReference.Asset;
				}
				if (!Object.op_Implicit((Object)(object)val) || !val.isSupported)
				{
					if (!warned)
					{
						((BaseUnityPlugin)this).Logger.LogWarning((object)"Alpha-blended game shader unavailable; vanilla moon preserved.");
					}
					warned = true;
					return;
				}
				moonMaterial = new Material(val)
				{
					name = "IvoryMoon_Alpha"
				};
				moonMaterial.SetTexture("_MainTex", (Texture)(object)texture);
				moonMaterial.SetFloat("_Mode", 2f);
				moonMaterial.SetInt("_SrcBlend", 5);
				moonMaterial.SetInt("_DstBlend", 10);
				moonMaterial.SetInt("_ZWrite", 0);
				moonMaterial.SetInt("_Cull", 0);
				moonMaterial.SetFloat("_SoftParticlesEnabled", 0f);
				moonMaterial.SetFloat("_CameraFadingEnabled", 0f);
				moonMaterial.SetFloat("_DistortionEnabled", 0f);
				moonMaterial.DisableKeyword("SOFTPARTICLES_ON");
				moonMaterial.EnableKeyword("_ALPHABLEND_ON");
				drawPass = -1;
				for (int i = 0; i < val.passCount; i++)
				{
					ShaderTagId val4 = val.FindPassTagValue(i, new ShaderTagId("LightMode"));
					if (string.Equals(((ShaderTagId)(ref val4)).name, "ForwardBase", StringComparison.OrdinalIgnoreCase))
					{
						drawPass = i;
						break;
					}
				}
				if (drawPass < 0 || drawPass >= moonMaterial.passCount)
				{
					((BaseUnityPlugin)this).Logger.LogError((object)"Unexpected particle shader pass layout; vanilla moon preserved.");
					Object.Destroy((Object)(object)moonMaterial);
					moonMaterial = null;
					((MonoBehaviour)this).CancelInvoke("CheckSky");
					return;
				}
				((BaseUnityPlugin)this).Logger.LogInfo((object)("Color pass=" + drawPass + "; texture=" + ((Texture)texture).width + "x" + ((Texture)texture).height));
				if (!ProbeTexture())
				{
					((BaseUnityPlugin)this).Logger.LogError((object)"Moon GPU probe failed; original moon preserved. See IvoryMoon-render-probe.png.");
					Object.Destroy((Object)(object)moonMaterial);
					moonMaterial = null;
					((MonoBehaviour)this).CancelInvoke("CheckSky");
					return;
				}
			}
			Material skybox = RenderSettings.skybox;
			if (Object.op_Implicit((Object)(object)skybox) && !((Object)(object)skybox == (Object)(object)replacement) && skybox.HasProperty("_MoonSize") && skybox.HasProperty("_MoonColor"))
			{
				ReleaseSky();
				original = skybox;
				originalRadius = Mathf.Max(0.001f, original.GetFloat("_MoonSize"));
				replacement = new Material(original)
				{
					name = ((Object)original).name + "_IvoryMoon"
				};
				replacement.SetFloat("_MoonSize", 1E-06f);
				replacement.SetColor("_MoonColor", Color.black);
				RenderSettings.skybox = replacement;
				((BaseUnityPlugin)this).Logger.LogInfo((object)("Round moon renderer ready; original angular radius=" + originalRadius));
			}
		}

		private void BeforeCamera(Camera camera)
		{
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_009f: 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_005d: 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_0072: Expected O, but got Unknown
			//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: 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_0100: 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_016b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0170: 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_0177: Unknown result type (might be due to invalid IL or missing references)
			//IL_017c: Unknown result type (might be due to invalid IL or missing references)
			//IL_017d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0182: 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_0191: Unknown result type (might be due to invalid IL or missing references)
			//IL_0196: Unknown result type (might be due to invalid IL or missing references)
			//IL_019b: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01de: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0215: Unknown result type (might be due to invalid IL or missing references)
			//IL_0234: Unknown result type (might be due to invalid IL or missing references)
			if (!Object.op_Implicit((Object)(object)EnvMan.instance) || !Object.op_Implicit((Object)(object)moonMaterial) || !Object.op_Implicit((Object)(object)replacement) || !Object.op_Implicit((Object)(object)quad) || (Object)(object)camera != (Object)(object)Utils.GetMainCamera())
			{
				return;
			}
			if ((Object)(object)targetCamera != (Object)(object)camera)
			{
				DetachCamera();
				targetCamera = camera;
				commands = new CommandBuffer
				{
					name = "Ivory Moon"
				};
				targetCamera.AddCommandBuffer((CameraEvent)15, commands);
			}
			commands.Clear();
			Vector3 val = -Vector4.op_Implicit(Shader.GetGlobalVector(SunDirection));
			if (!(((Vector3)(ref val)).sqrMagnitude < 0.5f))
			{
				((Vector3)(ref val)).Normalize();
				float num = Mathf.SmoothStep(0f, 1f, Mathf.InverseLerp(-0.015f, 0.08f, val.y));
				if (!(num <= 0f))
				{
					Vector3 val2 = ((Mathf.Abs(Vector3.Dot(val, Vector3.up)) > 0.98f) ? Vector3.forward : Vector3.up);
					float num2 = camera.farClipPlane * 0.9f;
					float num3 = 2f * Mathf.Asin(Mathf.Clamp(originalRadius * size.Value * 0.5f, 0f, 0.4f));
					float num4 = 2f * num2 * Mathf.Tan(num3) / 0.92f;
					Matrix4x4 val3 = Matrix4x4.TRS(((Component)camera).transform.position + val * num2, Quaternion.LookRotation(-val, val2), new Vector3(num4, num4, 1f));
					moonMaterial.SetColor("_Color", new Color(brightness.Value, brightness.Value, brightness.Value, num));
					Vector4 globalVector = Shader.GetGlobalVector(FogParams);
					commands.SetGlobalVector(FogParams, new Vector4(0f, 0f, 0f, 1f));
					commands.DrawMesh(quad, val3, moonMaterial, 0, drawPass);
					commands.SetGlobalVector(FogParams, globalVector);
				}
			}
		}

		private void DetachCamera()
		{
			if (commands != null)
			{
				if (Object.op_Implicit((Object)(object)targetCamera))
				{
					targetCamera.RemoveCommandBuffer((CameraEvent)15, commands);
				}
				commands.Release();
				commands = null;
				targetCamera = null;
			}
		}

		private bool ProbeTexture()
		{
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Expected O, but got Unknown
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Expected O, but got Unknown
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: 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_0088: 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_0093: 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_009b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: 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_00c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e8: 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_010a: 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_0199: Unknown result type (might be due to invalid IL or missing references)
			//IL_019e: Unknown result type (might be due to invalid IL or missing references)
			RenderTexture temporary = RenderTexture.GetTemporary(128, 128, 24, (RenderTextureFormat)0);
			Texture2D val = new Texture2D(128, 128, (TextureFormat)3, false);
			CommandBuffer val2 = new CommandBuffer
			{
				name = "IvoryMoon texture probe"
			};
			RenderTexture active = RenderTexture.active;
			try
			{
				moonMaterial.SetColor("_Color", Color.white);
				val2.SetRenderTarget(RenderTargetIdentifier.op_Implicit((Texture)(object)temporary));
				val2.ClearRenderTarget(true, true, Color.black);
				Matrix4x4 gPUProjectionMatrix = GL.GetGPUProjectionMatrix(Matrix4x4.Ortho(-0.5f, 0.5f, -0.5f, 0.5f, 0.1f, 10f), true);
				val2.SetViewProjectionMatrices(Matrix4x4.identity, gPUProjectionMatrix);
				Vector4 globalVector = Shader.GetGlobalVector(FogParams);
				val2.SetGlobalVector(FogParams, new Vector4(0f, 0f, 0f, 1f));
				val2.DrawMesh(quad, Matrix4x4.Translate(new Vector3(0f, 0f, -1f)), moonMaterial, 0, drawPass);
				val2.SetGlobalVector(FogParams, globalVector);
				Graphics.ExecuteCommandBuffer(val2);
				RenderTexture.active = temporary;
				val.ReadPixels(new Rect(0f, 0f, 128f, 128f), 0, 0);
				val.Apply();
				File.WriteAllBytes(Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location), "IvoryMoon-render-probe.png"), ImageConversion.EncodeToPNG(val));
				float num = 1f;
				float num2 = 0f;
				float num3 = 0f;
				float num4 = 0f;
				int num5 = 0;
				for (int i = 32; i < 96; i++)
				{
					for (int j = 32; j < 96; j++)
					{
						Color pixel = val.GetPixel(j, i);
						float grayscale = ((Color)(ref pixel)).grayscale;
						num = Mathf.Min(num, grayscale);
						num2 = Mathf.Max(num2, grayscale);
						num3 += grayscale;
						num4 += grayscale * grayscale;
						num5++;
					}
				}
				float num6 = num4 / (float)num5 - num3 / (float)num5 * (num3 / (float)num5);
				bool flag = num2 > 0.15f && num2 - num > 0.12f && num6 > 0.001f;
				((BaseUnityPlugin)this).Logger.LogInfo((object)("GPU texture probe: " + (flag ? "PASS" : "FAIL") + "; range=" + num + ".." + num2 + "; variance=" + num6));
				return flag;
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("GPU probe error: " + ex));
				return false;
			}
			finally
			{
				RenderTexture.active = active;
				val2.Release();
				RenderTexture.ReleaseTemporary(temporary);
				Object.Destroy((Object)(object)val);
			}
		}

		private void ReleaseSky()
		{
			if (Object.op_Implicit((Object)(object)replacement))
			{
				if ((Object)(object)RenderSettings.skybox == (Object)(object)replacement)
				{
					RenderSettings.skybox = original;
				}
				Object.Destroy((Object)(object)replacement);
			}
			replacement = null;
			original = null;
			if (commands != null)
			{
				commands.Clear();
			}
		}

		private void OnDestroy()
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Expected O, but got Unknown
			Camera.onPreCull = (CameraCallback)Delegate.Remove((Delegate?)(object)Camera.onPreCull, (Delegate?)new CameraCallback(BeforeCamera));
			((MonoBehaviour)this).CancelInvoke();
			DetachCamera();
			ReleaseSky();
			if (Object.op_Implicit((Object)(object)texture))
			{
				Object.Destroy((Object)(object)texture);
			}
			if (Object.op_Implicit((Object)(object)quad))
			{
				Object.Destroy((Object)(object)quad);
			}
			if (Object.op_Implicit((Object)(object)moonMaterial))
			{
				Object.Destroy((Object)(object)moonMaterial);
			}
			if (shaderHeld)
			{
				shaderReference.Release();
				shaderHeld = false;
			}
		}
	}
}