Decompiled source of RedHotSilencers v2.0.1

NGA.RedHot.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using FistVR;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyCompany("NGA")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Persistent player progression! Raid, stash loot, and deploy with seemless scene/loadout saving.")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0")]
[assembly: AssemblyProduct("NGA.RedHot")]
[assembly: AssemblyTitle("BepInEx Plugin Title")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.1.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 BepInEx
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class BepInAutoPluginAttribute : Attribute
	{
		public BepInAutoPluginAttribute(string id = null, string name = null, string version = null)
		{
		}
	}
}
namespace BepInEx.Preloader.Core.Patching
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class PatcherAutoPluginAttribute : Attribute
	{
		public PatcherAutoPluginAttribute(string id = null, string name = null, string version = null)
		{
		}
	}
}
namespace NGA
{
	[BepInPlugin("NGA.RedHot", "RedHot", "2.0.0")]
	[BepInDependency("nrgill28.Sodalite", "1.4.1")]
	[BepInProcess("h3vr.exe")]
	public class RedHot : BaseUnityPlugin
	{
		public class SuppressorTempMan : MonoBehaviour
		{
			public Suppressor mySupp;

			public List<Renderer> meshRenderers = new List<Renderer>();

			public List<ObjectTemperature> geoTemps = new List<ObjectTemperature>();

			public float timeSinceLogged = 0f;

			public float currentTemp = 0f;

			public static Texture2D GradientMask = null;

			public static Color emissionColor = new Color(3f, 1.055172f, 0f, 1f);

			private void Awake()
			{
				//IL_0028: Unknown result type (might be due to invalid IL or missing references)
				//IL_002e: Expected O, but got Unknown
				EmissionMaskProvider();
				mySupp = ((Component)this).GetComponent<Suppressor>();
				foreach (Transform item in ((Component)this).transform)
				{
					Transform val = item;
					if (((Component)val).gameObject.activeSelf && (Object)(object)((Component)val).GetComponent<MeshRenderer>() != (Object)null)
					{
						Renderer component = (Renderer)(object)((Component)val).GetComponent<MeshRenderer>();
						if ((Object)(object)component != (Object)null)
						{
							meshRenderers.Add(component);
							AddObjectTemperatureToChild(val);
							CopyMaterialEnableEmission(component);
							AddEmmissionToRendererV1(component, 0f);
						}
					}
				}
			}

			private void AddObjectTemperatureToChild(Transform child)
			{
				//IL_000f: Unknown result type (might be due to invalid IL or missing references)
				ObjectTemperature val = ((Component)child).gameObject.AddComponent<ObjectTemperature>();
				val.thermalIntensityProfile = (ThermalProfile)8;
				if (allow_diffusion_pattern.Value && (Object)(object)GradientMask != (Object)null)
				{
					val._thermalTexture = GradientMask;
				}
				val.GetRenderers();
				val.thermalIntensityScale = 0f;
				val.thermalTextureStrength = 0f;
				geoTemps.Add(val);
			}

			public static void CopyMaterialEnableEmission(Renderer renderer)
			{
				//IL_0007: Unknown result type (might be due to invalid IL or missing references)
				//IL_000d: Expected O, but got Unknown
				//IL_002f: Unknown result type (might be due to invalid IL or missing references)
				Material val2 = (renderer.material = new Material(renderer.sharedMaterial));
				val2.EnableKeyword("_EMISSION");
				val2.globalIlluminationFlags = (MaterialGlobalIlluminationFlags)1;
				val2.SetColor("_EmissionColor", emissionColor);
				if (allow_diffusion_pattern.Value && (Object)(object)GradientMask != (Object)null)
				{
					val2.SetTexture("_EmissionMap", (Texture)(object)GradientMask);
				}
			}

			public static void AddEmmissionToRendererV1(Renderer renderer, float emissionWeight)
			{
				//IL_0001: Unknown result type (might be due to invalid IL or missing references)
				//IL_0007: Expected O, but got Unknown
				//IL_0015: Unknown result type (might be due to invalid IL or missing references)
				MaterialPropertyBlock val = new MaterialPropertyBlock();
				renderer.GetPropertyBlock(val);
				val.SetColor("_EmissionColor", emissionColor);
				val.SetFloat("_EmissionWeight", emissionWeight);
				renderer.SetPropertyBlock(val);
			}

			public void FixedUpdate()
			{
				timeSinceLogged += Time.deltaTime;
				RenderSmoke();
				DecrementCurrTempWithCurve();
				foreach (ObjectTemperature geoTemp in geoTemps)
				{
					geoTemp.thermalIntensityScale = currentTemp * currentTemp * 8f;
					geoTemp.thermalTextureStrength = currentTemp * currentTemp * 8f;
				}
				foreach (Renderer meshRenderer in meshRenderers)
				{
					AddEmmissionToRendererV1(meshRenderer, currentTemp * currentTemp);
				}
				if (currentTemp >= 0.01f && timeSinceLogged >= 1f)
				{
					timeSinceLogged = 0f;
					LogMessage("Current Temp: " + currentTemp);
				}
			}

			private void DecrementCurrTempWithCurve()
			{
				currentTemp -= Time.deltaTime * temp_decrease_rate.Value;
				currentTemp = Mathf.Clamp(currentTemp, 0f, 1f);
			}

			private void RenderSmoke()
			{
				//IL_008d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0093: Expected O, but got Unknown
				//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
				if (!spit_smoke.Value || !(currentTemp > 0.2f) || !Object.op_Implicit((Object)(object)mySupp) || !Object.op_Implicit((Object)(object)((FVRFireArmAttachment)mySupp).curMount) || !Object.op_Implicit((Object)(object)((FVRFireArmAttachment)mySupp).curMount.Parent) || !(((FVRFireArmAttachment)mySupp).curMount.Parent is FVRFireArm))
				{
					return;
				}
				FVRFireArm val = (FVRFireArm)((FVRFireArmAttachment)mySupp).curMount.Parent;
				for (int i = 0; i < val.GasOutEffects.Length; i++)
				{
					if (val.GasOutEffects[i].FollowsMuzzle)
					{
						val.GasOutEffects[i].GasPerEvent.y = val.GasOutEffects[i].GasPerEvent.x;
						((Component)val.GasOutEffects[i].PSystem).transform.position = val.GetMuzzle().position;
						val.GasOutEffects[i].PSystem.Emit(1);
					}
				}
			}

			public void ShotFired()
			{
				currentTemp += temp_increase_per_shot.Value;
				LogMessage("Shot fired!!! Current Temp: " + currentTemp);
			}

			private static void EmissionMaskProvider()
			{
				//IL_0068: Unknown result type (might be due to invalid IL or missing references)
				//IL_006f: Expected O, but got Unknown
				if ((Object)(object)GradientMask != (Object)null)
				{
					return;
				}
				string path = Path.Combine(Paths.PluginPath, "NGA-RedHotSilencers");
				string text = Path.Combine(path, "DiffuseScatteringOnRing.png");
				if (!File.Exists(text))
				{
					Logger.LogError((object)("[MyMod] EmissionMaskProvider: PNG not found at " + text));
					return;
				}
				try
				{
					byte[] array = File.ReadAllBytes(text);
					Texture2D val = new Texture2D(2, 2, (TextureFormat)4, false);
					if (!val.LoadImage(array))
					{
						Logger.LogError((object)"[MyMod] EmissionMaskProvider: Failed to load PNG data.");
						return;
					}
					((Texture)val).wrapMode = (TextureWrapMode)1;
					((Texture)val).filterMode = (FilterMode)1;
					GradientMask = val;
					LogMessage("[MyMod] EmissionMaskProvider: Loaded custom mask from " + text);
				}
				catch (Exception arg)
				{
					Logger.LogError((object)$"[MyMod] EmissionMaskProvider: Exception while loading texture - {arg}");
				}
			}
		}

		[HarmonyPatch(typeof(Suppressor))]
		[HarmonyPatch("ShotEffect")]
		private class SuppressorShotEffectHook
		{
			private static void Prefix(Suppressor __instance)
			{
				SuppressorTempMan component = ((Component)__instance).GetComponent<SuppressorTempMan>();
				if ((Object)(object)component != (Object)null)
				{
					component.ShotFired();
				}
				else
				{
					LogError("SuppressorTempMan component not found on suppressor!");
				}
			}
		}

		[HarmonyPatch(typeof(MuzzleDevice))]
		[HarmonyPatch("Awake")]
		private class SuppressorAwakeHook
		{
			private static void Postfix(MuzzleDevice __instance)
			{
				if (__instance is Suppressor && (Object)(object)((Component)__instance).GetComponent<SuppressorTempMan>() == (Object)null)
				{
					((Component)__instance).gameObject.AddComponent<SuppressorTempMan>();
					LogMessage("SuppressorTempMan component added to suppressor.");
				}
			}
		}

		private static ConfigEntry<bool> debug_mode;

		private static ConfigEntry<bool> spit_smoke;

		private static ConfigEntry<float> temp_increase_per_shot;

		private static ConfigEntry<float> temp_decrease_rate;

		private static ConfigEntry<bool> allow_diffusion_pattern;

		internal static ManualLogSource Logger { get; private set; }

		private void Awake()
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Expected O, but got Unknown
			Logger = ((BaseUnityPlugin)this).Logger;
			Harmony val = new Harmony("NGA.RedHot");
			SetUpConfigFields();
			val.PatchAll();
			Logger.LogMessage((object)"Hello, world! Sent from NGA.RedHot 2.0.0");
		}

		private void SetUpConfigFields()
		{
			spit_smoke = ((BaseUnityPlugin)this).Config.Bind<bool>("Options", "Spit Smoke.", true, "Enables smoke emission from hot suppressors.");
			allow_diffusion_pattern = ((BaseUnityPlugin)this).Config.Bind<bool>("Options", "Allow Diffusion Pattern.", false, "Paints the suppressor so it's not uniformly red, instead using a pattern which may break.");
			temp_increase_per_shot = ((BaseUnityPlugin)this).Config.Bind<float>("Options", "Temp Increase Per Shot.", 0.005f, "Amount the suppressor's temperature increases per shot fired.");
			temp_decrease_rate = ((BaseUnityPlugin)this).Config.Bind<float>("Options", "Temp Decrease Rate", 0.005f, "Rate at which the suppressor cools down over time.");
			debug_mode = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "Debug Mode", false, "Enables debug mode, which outputs extra information to the console.");
		}

		internal static void LogMessage(string message)
		{
			if (debug_mode.Value)
			{
				ManualLogSource logger = Logger;
				if (logger != null)
				{
					logger.LogInfo((object)message);
				}
			}
		}

		internal static void LogError(string message)
		{
			if (debug_mode.Value)
			{
				ManualLogSource logger = Logger;
				if (logger != null)
				{
					logger.LogError((object)message);
				}
			}
		}
	}
}