using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using MelonLoader;
using QuestDeathVignetteFix;
using UnityEngine;
using UnityEngine.Rendering;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(/*Could not decode attribute arguments.*/)]
[assembly: MelonInfo(typeof(Main), "Quest death effect replacer", "1.0.0", "genisis inc", null)]
[assembly: MelonGame("Stress Level Zero", "BONELAB")]
[assembly: TargetFramework(".NETCoreApp,Version=v10.0", FrameworkDisplayName = ".NET 10.0")]
[assembly: AssemblyCompany("death effect replacer")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("death effect replacer")]
[assembly: AssemblyTitle("death effect replacer")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace QuestDeathVignetteFix;
public class Main : MelonMod
{
private GameObject vignetteObj;
private Material vignetteMat;
private Texture2D vignetteTex;
private float alpha = 0f;
public override void OnInitializeMelon()
{
MelonLogger.Msg("Quest Death Vignette Fix loaded.");
CreateVignette();
}
public override void OnUpdate()
{
//IL_005f: Unknown result type (might be due to invalid IL or missing references)
//IL_0064: Unknown result type (might be due to invalid IL or missing references)
//IL_0078: Unknown result type (might be due to invalid IL or missing references)
if (!((Object)(object)Camera.main == (Object)null))
{
AttachToCamera();
bool flag = IsPlayerDeadOrVeryLowHealth();
alpha = Mathf.MoveTowards(alpha, flag ? 0.85f : 0f, Time.deltaTime * 3f);
if ((Object)(object)vignetteMat != (Object)null)
{
Color black = Color.black;
black.a = alpha;
vignetteMat.color = black;
}
if ((Object)(object)vignetteObj != (Object)null)
{
vignetteObj.SetActive(alpha > 0.01f);
}
if (flag)
{
DisableQuestSpikeDeathEffect();
}
}
}
private void CreateVignette()
{
//IL_0022: Unknown result type (might be due to invalid IL or missing references)
//IL_002c: Expected O, but got Unknown
//IL_0058: Unknown result type (might be due to invalid IL or missing references)
vignetteTex = GenerateVignetteTexture(512, 512);
vignetteMat = new Material(Shader.Find("Unlit/Transparent"));
vignetteMat.mainTexture = (Texture)(object)vignetteTex;
vignetteMat.color = new Color(0f, 0f, 0f, 0f);
vignetteObj = GameObject.CreatePrimitive((PrimitiveType)5);
((Object)vignetteObj).name = "PCVR_Style_Death_Vignette";
Object.Destroy((Object)(object)vignetteObj.GetComponent<Collider>());
MeshRenderer component = vignetteObj.GetComponent<MeshRenderer>();
((Renderer)component).material = vignetteMat;
((Renderer)component).shadowCastingMode = (ShadowCastingMode)0;
((Renderer)component).receiveShadows = false;
vignetteObj.SetActive(false);
Object.DontDestroyOnLoad((Object)(object)vignetteObj);
}
private void AttachToCamera()
{
//IL_0039: 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_009f: Unknown result type (might be due to invalid IL or missing references)
Camera main = Camera.main;
vignetteObj.transform.SetParent(((Component)main).transform, false);
vignetteObj.transform.localPosition = new Vector3(0f, 0f, 0.31f);
vignetteObj.transform.localRotation = Quaternion.identity;
float num = 0.62f * Mathf.Tan(main.fieldOfView * 0.5f * ((float)Math.PI / 180f));
float num2 = num * main.aspect;
vignetteObj.transform.localScale = new Vector3(num2 * 1.25f, num * 1.25f, 1f);
}
private Texture2D GenerateVignetteTexture(int width, int height)
{
//IL_0005: Unknown result type (might be due to invalid IL or missing references)
//IL_000b: Expected O, but got Unknown
//IL_0022: Unknown result type (might be due to invalid IL or missing references)
//IL_0027: Unknown result type (might be due to invalid IL or missing references)
//IL_003e: Unknown result type (might be due to invalid IL or missing references)
//IL_0043: 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)
Texture2D val = new Texture2D(width, height, (TextureFormat)4, false);
Vector2 val2 = default(Vector2);
((Vector2)(ref val2))..ctor((float)width / 2f, (float)height / 2f);
float num = Vector2.Distance(Vector2.zero, val2);
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
float num2 = Vector2.Distance(new Vector2((float)j, (float)i), val2) / num;
float num3 = Mathf.InverseLerp(0.35f, 1f, num2);
num3 = Mathf.Pow(num3, 2.2f);
val.SetPixel(j, i, new Color(0f, 0f, 0f, num3));
}
}
val.Apply();
return val;
}
private bool IsPlayerDeadOrVeryLowHealth()
{
try
{
Il2CppArrayBase<GameObject> val = Object.FindObjectsOfType<GameObject>();
global::System.Collections.Generic.IEnumerator<GameObject> enumerator = val.GetEnumerator();
try
{
bool flag = default(bool);
while (((global::System.Collections.IEnumerator)enumerator).MoveNext())
{
GameObject current = enumerator.Current;
if ((Object)(object)current == (Object)null)
{
continue;
}
string text = ((Object)current).name.ToLower();
if (!text.Contains("player") && !text.Contains("rig"))
{
continue;
}
Il2CppArrayBase<Component> components = current.GetComponents<Component>();
global::System.Collections.Generic.IEnumerator<Component> enumerator2 = components.GetEnumerator();
try
{
while (((global::System.Collections.IEnumerator)enumerator2).MoveNext())
{
Component current2 = enumerator2.Current;
if ((Object)(object)current2 == (Object)null)
{
continue;
}
global::System.Type type = ((object)current2).GetType();
FieldInfo[] fields = type.GetFields();
foreach (FieldInfo val2 in fields)
{
string text2 = ((MemberInfo)val2).Name.ToLower();
if (text2.Contains("dead") || text2.Contains("dying"))
{
object value = val2.GetValue((object)current2);
int num;
if (value is bool)
{
flag = (bool)value;
num = 1;
}
else
{
num = 0;
}
if (((uint)num & (flag ? 1u : 0u)) != 0)
{
return true;
}
}
if (text2.Contains("health") || text2.Contains("hp"))
{
object value2 = val2.GetValue((object)current2);
if (value2 is float num2 && num2 <= 0.01f)
{
return true;
}
if (value2 is int num3 && num3 <= 0)
{
return true;
}
}
}
}
}
finally
{
((global::System.IDisposable)enumerator2)?.Dispose();
}
}
}
finally
{
((global::System.IDisposable)enumerator)?.Dispose();
}
}
catch
{
}
return false;
}
private void DisableQuestSpikeDeathEffect()
{
try
{
Il2CppArrayBase<Renderer> val = Object.FindObjectsOfType<Renderer>();
global::System.Collections.Generic.IEnumerator<Renderer> enumerator = val.GetEnumerator();
try
{
while (((global::System.Collections.IEnumerator)enumerator).MoveNext())
{
Renderer current = enumerator.Current;
if ((Object)(object)current == (Object)null)
{
continue;
}
string text = ((Object)current).name.ToLower();
if (text.Contains("death") && (text.Contains("spike") || text.Contains("thorn") || text.Contains("vignette")))
{
current.enabled = false;
}
if ((Object)(object)current.material != (Object)null)
{
string text2 = ((Object)current.material).name.ToLower();
if (text2.Contains("death") && (text2.Contains("spike") || text2.Contains("thorn")))
{
current.enabled = false;
}
}
}
}
finally
{
((global::System.IDisposable)enumerator)?.Dispose();
}
}
catch
{
}
}
}