Decompiled source of TumorShadowFix v0.0.8

TumorShadowFix.dll

Decompiled 5 months ago
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using Agents;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Unity.IL2CPP;
using CullingSystem;
using Enemies;
using FluffyUnderware.DevTools.Extensions;
using HarmonyLib;
using IRF;
using Il2CppInterop.Runtime.Injection;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppSystem;
using Il2CppSystem.Collections.Generic;
using Microsoft.CodeAnalysis;
using Player;
using UnityEngine;
using UnityEngine.Rendering;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("TumorShadowFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("TumorShadowFix")]
[assembly: AssemblyTitle("TumorShadowFix")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace TumorShadowFix
{
	[BepInPlugin("TumorShadowFix", "TumorShadowFix", "0.0.8")]
	public class Plugin : BasePlugin
	{
		public class FixEnemySER : MonoBehaviour
		{
			private EnemyAgent enemy;

			private List<MeshRenderer> sammuta = new List<MeshRenderer>();

			private List<SkinnedMeshRenderer> sammuta2 = new List<SkinnedMeshRenderer>();

			private SkinnedMeshRenderer smr;

			private ShadowEnemyRenderer ser;

			private Il2CppArrayBase<ShadowEnemyRenderer> sers;

			private bool initialized;

			private bool thermalShadows = _cfgThermalShadows.Value;

			public void Update()
			{
				if (!initialized || (Object)(object)enemy == (Object)null || sers.Count == 0)
				{
					return;
				}
				foreach (ShadowEnemyRenderer ser in sers)
				{
					if (!((Component)ser).gameObject.activeSelf)
					{
						((Component)ser).gameObject.SetActive(true);
					}
				}
				if (!((Agent)enemy).Alive)
				{
					return;
				}
				foreach (MeshRenderer sammutum in sammuta)
				{
					((Renderer)sammutum).shadowCastingMode = (ShadowCastingMode)3;
				}
				foreach (SkinnedMeshRenderer item in sammuta2)
				{
					((Renderer)item).shadowCastingMode = (ShadowCastingMode)3;
				}
			}

			public void Attach(EnemyAgent enemy)
			{
				this.enemy = enemy;
				sers = ((Component)enemy).GetComponentsInChildren<ShadowEnemyRenderer>();
				if (sers.Count > 0)
				{
					initialized = true;
					return;
				}
				foreach (MeshRenderer componentsInChild in ((Component)enemy).GetComponentsInChildren<MeshRenderer>())
				{
					if (((Object)((Component)componentsInChild).gameObject).name.ToLower().Contains("infested"))
					{
						sammuta.Add(componentsInChild);
					}
				}
				Il2CppArrayBase<SkinnedMeshRenderer> componentsInChildren = ((Component)enemy).GetComponentsInChildren<SkinnedMeshRenderer>();
				foreach (SkinnedMeshRenderer item in componentsInChildren)
				{
					if (((Object)((Component)item).gameObject).name.ToLower() == "g_pouncer")
					{
						sammuta2.Add(item);
					}
					if (((Object)((Component)item).gameObject).name.ToLower() == "g_body")
					{
						sammuta2.Add(item);
					}
				}
				foreach (MeshRenderer sammutum in sammuta)
				{
					((Renderer)sammutum).shadowCastingMode = (ShadowCastingMode)3;
				}
				foreach (SkinnedMeshRenderer item2 in sammuta2)
				{
					((Renderer)item2).shadowCastingMode = (ShadowCastingMode)3;
				}
				if (!thermalShadows)
				{
					return;
				}
				Il2CppArrayBase<SkinnedMeshRenderer> componentsInChildren2 = ((Component)this.enemy).GetComponentsInChildren<SkinnedMeshRenderer>(true);
				if (componentsInChildren2.Length == 1)
				{
					((Renderer)componentsInChildren[0]).shadowCastingMode = (ShadowCastingMode)3;
					ser = ((Component)componentsInChildren2[0]).gameObject.AddComponent<ShadowEnemyRenderer>();
					ser.Renderer = (Renderer)(object)componentsInChildren2[0];
					ArrayExt.Add<ShadowEnemyRenderer>(sers, ser);
				}
				if (componentsInChildren2.Length > 1)
				{
					foreach (SkinnedMeshRenderer item3 in componentsInChildren2)
					{
						if (((Object)((Component)item3).gameObject).name.ToLower().Contains("g_body_shadow") || ((Object)((Component)item3).gameObject).name.ToLower().Contains("g_shooter") || ((Object)((Component)item3).gameObject).name.ToLower().Contains("g_tank") || ((Object)((Component)item3).gameObject).name.ToLower().Contains("g_birther") || ((Object)((Component)item3).gameObject).name.ToLower().Contains("g_pouncer_shadow"))
						{
							((Renderer)item3).shadowCastingMode = (ShadowCastingMode)3;
							ser = ((Component)item3).gameObject.AddComponent<ShadowEnemyRenderer>();
							ser.Renderer = (Renderer)(object)item3;
							ArrayExt.Add<ShadowEnemyRenderer>(sers, ser);
						}
					}
				}
				if (sers.Count > 0)
				{
					ser.MovingCuller = this.enemy.MovingCuller;
					initialized = true;
				}
			}
		}

		public class FixShadows : MonoBehaviour
		{
			private Renderer orig;

			private EnemyAgent enemy;

			private Renderer mr1;

			private Renderer mr2;

			private bool destroyed;

			private bool initialized;

			private List<Renderer> renderers = new List<Renderer>();

			private float jee;

			private Dam_EnemyDamageLimb_Custom bulb;

			private bool tempset;

			private bool visibleFromBehind = _cfgVisibleFromBehind.Value;

			private bool unhide;

			private PlayerAgent plr = PlayerManager.GetLocalPlayerAgent();

			private float debugreporttime;

			public void Attach(EnemyAgent enemy, Renderer rend, Renderer sphere, Renderer shadow)
			{
				jee = Time.realtimeSinceStartup;
				this.enemy = enemy;
				mr1 = sphere;
				mr2 = shadow;
				orig = rend;
				foreach (Renderer componentsInChild in ((Component)enemy).GetComponentsInChildren<Renderer>())
				{
					renderers.Add(componentsInChild);
				}
				bulb = ((Component)orig).GetComponentInParent<Dam_EnemyDamageLimb_Custom>();
				if ((Object)(object)bulb == (Object)null)
				{
					Debug.Log(Object.op_Implicit("TumorShadowFix - egg not found. damn."));
				}
				if ((Object)(object)bulb != (Object)null)
				{
					((Dam_EnemyDamageLimb)bulb).OnLimbDestroyed += Action.op_Implicit((Action)Done);
				}
				((Agent)enemy).SetAnimatorCullingEnabled(false);
				initialized = true;
			}

			public void Done()
			{
				Debug.Log(Object.op_Implicit($"TumorShadowFix - tumor has been popped from enemy {((Agent)enemy).GlobalID}, removing shadow.."));
				mr2.forceRenderingOff = true;
				mr2.castShadows = false;
				mr2.receiveShadows = false;
				enemy.MovingCuller.CullBucket.ShadowRenderers.Remove(mr2);
				Enumerator<MaterialRef> enumerator = enemy.MaterialHandler.m_materialRefs.GetEnumerator();
				while (enumerator.MoveNext())
				{
					MaterialRef current = enumerator.Current;
					try
					{
						current.m_renderers.Remove(mr2);
					}
					catch (Exception)
					{
					}
				}
				enemy.MovingCuller.CullBucket.ComputeTotalBounds();
				((C_Cullable)enemy.MovingCuller.CullBucket).NeedsShadowRefresh = true;
				destroyed = true;
			}

			public void Update()
			{
				//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
				//IL_0118: 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_0138: Unknown result type (might be due to invalid IL or missing references)
				//IL_013d: 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_0145: Unknown result type (might be due to invalid IL or missing references)
				//IL_014a: 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_01a9: Unknown result type (might be due to invalid IL or missing references)
				//IL_01af: Invalid comparison between Unknown and I4
				//IL_023e: Unknown result type (might be due to invalid IL or missing references)
				//IL_0244: Invalid comparison between Unknown and I4
				if (destroyed || !initialized || Time.realtimeSinceStartup - jee < 1f || (Object)(object)enemy == (Object)null)
				{
					return;
				}
				if (!((Agent)enemy).Alive)
				{
					enemy.MovingCuller.CullBucket.Renderers.Remove(mr1);
					enemy.MovingCuller.CullBucket.ShadowRenderers.Remove(mr2);
					Enumerator<MaterialRef> enumerator = enemy.MaterialHandler.m_materialRefs.GetEnumerator();
					while (enumerator.MoveNext())
					{
						enumerator.Current.m_renderers.Remove(mr2);
					}
					destroyed = true;
					return;
				}
				unhide = false;
				if ((int)mr1.shadowCastingMode != 0)
				{
					mr1.shadowCastingMode = (ShadowCastingMode)0;
				}
				if (!((Object)enemy).name.ToLower().Contains("shadow"))
				{
					return;
				}
				if (visibleFromBehind)
				{
					Vector3 forward = ((Component)enemy).transform.forward;
					Vector3 val = ((Component)plr).transform.position - ((Component)enemy).transform.position;
					Vector3 normalized = ((Vector3)(ref val)).normalized;
					if (Vector3.Angle(forward, normalized) > 168f)
					{
						unhide = true;
					}
					if (unhide)
					{
						mr1.forceRenderingOff = false;
						return;
					}
				}
				if (!mr1.forceRenderingOff)
				{
					mr1.forceRenderingOff = true;
				}
				foreach (Renderer renderer in renderers)
				{
					if ((int)renderer.shadowCastingMode != 3)
					{
						renderer.shadowCastingMode = (ShadowCastingMode)3;
					}
					if (!renderer.castShadows)
					{
						renderer.castShadows = true;
					}
					if (!enemy.MovingCuller.m_disableAnimatorCullingWhenRenderingShadow)
					{
						enemy.MovingCuller.m_disableAnimatorCullingWhenRenderingShadow = true;
					}
					if (enemy.MovingCuller.m_animatorCullingEnabled)
					{
						((Agent)enemy).SetAnimatorCullingEnabled(false);
					}
					if (!enemy.MovingCuller.Culler.hasShadowsEnabled)
					{
						enemy.MovingCuller.Culler.hasShadowsEnabled = true;
					}
					if ((int)mr2.shadowCastingMode != 3)
					{
						mr2.shadowCastingMode = (ShadowCastingMode)3;
					}
				}
			}
		}

		[HarmonyPatch]
		private class testpatches
		{
			[HarmonyPatch(typeof(EnemyAllocator), "SpawnEnemy")]
			[HarmonyPostfix]
			private static void Postfix(EnemyAllocator __instance)
			{
				//IL_024f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0266: Unknown result type (might be due to invalid IL or missing references)
				//IL_0289: Unknown result type (might be due to invalid IL or missing references)
				//IL_02a0: Unknown result type (might be due to invalid IL or missing references)
				//IL_0328: 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)
				EnemyAgent spawnedEnemyAgent = __instance.m_spawnedEnemyAgent;
				if (((Object)spawnedEnemyAgent).name.ToLower().Contains("shadow"))
				{
					spawnedEnemyAgent.RequireTagForDetection = true;
					((Agent)spawnedEnemyAgent).SetAnimatorCullingEnabled(false);
					spawnedEnemyAgent.MovingCuller.m_disableAnimatorCullingWhenRenderingShadow = true;
					foreach (InstancedRenderFeature componentsInChild in ((Component)spawnedEnemyAgent).GetComponentsInChildren<InstancedRenderFeature>())
					{
						((Behaviour)componentsInChild).enabled = false;
					}
					if (_cfgThermalShadows.Value)
					{
						((Component)spawnedEnemyAgent).gameObject.AddComponent<FixEnemySER>().Attach(spawnedEnemyAgent);
					}
				}
				foreach (Renderer componentsInChild2 in ((Component)spawnedEnemyAgent).GetComponentsInChildren<Renderer>())
				{
					if (((Object)spawnedEnemyAgent).name.ToLower().Contains("tank") || ((Object)spawnedEnemyAgent).name.ToLower().Contains("birther"))
					{
						componentsInChild2.receiveShadows = true;
						componentsInChild2.castShadows = true;
						componentsInChild2.shadowCastingMode = (ShadowCastingMode)1;
					}
					if (((Object)spawnedEnemyAgent).name.ToLower().Contains("shadow"))
					{
						SkinnedMeshRenderer val = ((Il2CppObjectBase)componentsInChild2).TryCast<SkinnedMeshRenderer>();
						if ((Object)(object)val != (Object)null && (((Object)((Component)val).gameObject).name.ToLower().Contains("g_body_shadow") || ((Object)((Component)val).gameObject).name.ToLower().Contains("g_shooter") || ((Object)((Component)val).gameObject).name.ToLower().Contains("g_tank") || ((Object)((Component)val).gameObject).name.ToLower().Contains("g_birther") || ((Object)((Component)val).gameObject).name.ToLower().Contains("g_pouncer_shadow")))
						{
							val.updateWhenOffscreen = true;
						}
						if (componentsInChild2.enabled)
						{
							componentsInChild2.shadowCastingMode = (ShadowCastingMode)3;
						}
					}
					if (!((Object)componentsInChild2).name.ToLower().Contains("egg") && !((Object)componentsInChild2).name.ToLower().Contains("fleshsack"))
					{
						continue;
					}
					spawnedEnemyAgent.MovingCuller.m_disableAnimatorCullingWhenRenderingShadow = true;
					componentsInChild2.receiveShadows = false;
					componentsInChild2.castShadows = false;
					componentsInChild2.shadowCastingMode = (ShadowCastingMode)0;
					componentsInChild2.enabled = false;
					GameObject val2 = GameObject.CreatePrimitive((PrimitiveType)0);
					GameObject val3 = GameObject.CreatePrimitive((PrimitiveType)0);
					((Object)val2).name = "fd_egg_sphere1";
					((Object)val3).name = "fd_egg_sphere2_shadow";
					val2.transform.parent = ((Component)componentsInChild2).transform;
					val2.transform.localScale = Vector3.one;
					val2.transform.localPosition = ((Component)componentsInChild2).transform.localPosition;
					val3.transform.parent = ((Component)componentsInChild2).transform;
					val3.transform.localScale = Vector3.one;
					val3.transform.localPosition = ((Component)componentsInChild2).transform.localPosition;
					MeshRenderer component = val2.GetComponent<MeshRenderer>();
					MeshRenderer component2 = val3.GetComponent<MeshRenderer>();
					Material material = Object.Instantiate<Material>(((Renderer)component).sharedMaterial);
					Material material2 = Object.Instantiate<Material>(((Renderer)component).sharedMaterial);
					((Renderer)component).material = material;
					((Renderer)component2).material = material2;
					((Renderer)component).material.SetTexture("_MainTex", componentsInChild2.sharedMaterial.mainTexture);
					((Renderer)component).shadowCastingMode = (ShadowCastingMode)0;
					((Renderer)component).castShadows = false;
					((Renderer)component).material.color = new Color(1f, 1f, 1f, 1f);
					((Renderer)component).material.shader = Shader.Find("Cell/Enemy/EnemyFlesh_CD");
					((Renderer)component).material.enableInstancing = false;
					((Renderer)component2).shadowCastingMode = (ShadowCastingMode)3;
					((Renderer)component2).castShadows = true;
					((Renderer)component2).receiveShadows = false;
					((Renderer)component2).material.color = new Color(1f, 1f, 1f, 1f);
					((Renderer)component2).material.shader = Shader.Find("Cell/Enemy/EnemyFlesh_CD");
					((Renderer)component2).material.enableInstancing = false;
					((Collider)val2.GetComponent<SphereCollider>()).enabled = false;
					((Collider)val3.GetComponent<SphereCollider>()).enabled = false;
					Renderer component3 = val2.GetComponent<Renderer>();
					Renderer component4 = val3.GetComponent<Renderer>();
					try
					{
						Enumerator<MaterialRef> enumerator3 = spawnedEnemyAgent.MaterialHandler.m_materialRefs.GetEnumerator();
						while (enumerator3.MoveNext())
						{
							enumerator3.Current.m_renderers.Add(component4);
						}
					}
					catch (Exception value)
					{
						Debug.Log(Object.op_Implicit($"cmh error {value}"));
					}
					component3.shadowCastingMode = (ShadowCastingMode)0;
					component4.shadowCastingMode = (ShadowCastingMode)3;
					spawnedEnemyAgent.MovingCuller.CullBucket.Renderers.Remove(componentsInChild2);
					spawnedEnemyAgent.MovingCuller.CullBucket.Renderers.Add(component3);
					spawnedEnemyAgent.MovingCuller.Culler.hasShadowsEnabled = true;
					spawnedEnemyAgent.MovingCuller.CullBucket.ShadowRenderers.Add(component4);
					((Component)componentsInChild2).gameObject.AddComponent<FixShadows>().Attach(spawnedEnemyAgent, componentsInChild2, component3, component4);
				}
				spawnedEnemyAgent.MovingCuller.CullBucket.ComputeTotalBounds();
				((C_Cullable)spawnedEnemyAgent.MovingCuller.CullBucket).NeedsShadowRefresh = true;
			}
		}

		public static string CONFIG_PATH = Path.Combine(Paths.ConfigPath, "TumorShadowFix.cfg");

		public static ConfigFile config = new ConfigFile(CONFIG_PATH, true);

		public static ConfigEntry<bool> _cfgVisibleFromBehind;

		public static ConfigEntry<bool> _cfgThermalShadows;

		public override void Load()
		{
			//IL_000f: 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)
			//IL_002a: Expected O, but got Unknown
			//IL_002a: Expected O, but got Unknown
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Expected O, but got Unknown
			//IL_0059: Expected O, but got Unknown
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			_cfgVisibleFromBehind = config.Bind<bool>(new ConfigDefinition("General", "VisibleFromBehind"), false, new ConfigDescription("Make tumors visible when viewed from behind enemy", (AcceptableValueBase)null, Array.Empty<object>()));
			_cfgThermalShadows = config.Bind<bool>(new ConfigDefinition("General", "ThermalShadows"), true, new ConfigDescription("Make shadows made by this mod be visible on thermal imaging", (AcceptableValueBase)null, Array.Empty<object>()));
			ClassInjector.RegisterTypeInIl2Cpp<FixShadows>();
			ClassInjector.RegisterTypeInIl2Cpp<FixEnemySER>();
			new Harmony("oerr").PatchAll();
		}
	}
	[GeneratedCode("VersionInfoGenerator", "2.0.0+git50a4b1a-master")]
	[CompilerGenerated]
	internal static class VersionInfo
	{
		public const string RootNamespace = "TumorShadowFix";

		public const string Version = "1.0.0";

		public const string VersionPrerelease = null;

		public const string VersionMetadata = null;

		public const string SemVer = "1.0.0";

		public const string GitRevShort = null;

		public const string GitRevLong = null;

		public const string GitBranch = null;

		public const string GitTag = null;

		public const bool GitIsDirty = false;
	}
}