using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BoneLib;
using BoneLib.BoneMenu;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppSLZ.Marrow;
using MelonLoader;
using MelonLoader.Preferences;
using SpectatorMod;
using UnityEngine;
using UnityEngine.Rendering;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: MelonInfo(typeof(SpectatorModMain), "Spectator Cam", "2.5.5", "Int_Void", null)]
[assembly: MelonGame("Stress Level Zero", "BONELAB")]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("SpectatorMod")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("SpectatorMod")]
[assembly: AssemblyTitle("SpectatorMod")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace SpectatorMod;
public class SpectatorModMain : MelonMod
{
public enum ControllerSide
{
Left,
Right,
Both
}
private class TrackedPlayer
{
public Transform HeadBone;
public string Name;
}
public static MelonPreferences_Category PrefCategory;
public static MelonPreferences_Entry<ControllerSide> PrefController;
public static MelonPreferences_Entry<bool> PrefIsEnabled;
public static MelonPreferences_Entry<bool> PrefNoiseEffect;
public static MelonPreferences_Entry<float> PrefHeadForward;
public static MelonPreferences_Entry<float> PrefHeadUp;
public static MelonPreferences_Entry<float> PrefNoiseAlpha;
private bool _isSpectating = false;
private Camera _specCamera;
private Camera _mainCamera;
private bool _mainCamWasEnabled = true;
private float _buttonPressTime = -1f;
private const float MAX_HOLD_TIME = 0.4f;
private bool _isButtonHeld = false;
private List<TrackedPlayer> _players = new List<TrackedPlayer>();
private int _currentPlayerIndex = 0;
private TrackedPlayer _currentPlayer;
private GameObject _hudRoot;
private MeshRenderer _noiseRenderer;
private Texture2D _noiseTex;
private Color32[] _noisePixels;
private int _noiseCounter = 0;
private const int NOISE_SIZE = 64;
private Mesh _quadMesh;
private Shader _hudShader;
public override void OnInitializeMelon()
{
((MelonBase)this).LoggerInstance.Msg("Spectator Mod v2.5.5 Initialized!");
Hooking.OnLevelLoaded += OnLevelLoaded;
SetupPreferences();
SetupBoneMenu();
}
private void SetupPreferences()
{
PrefCategory = MelonPreferences.CreateCategory("SpectatorMod", "Spectator Mod");
PrefIsEnabled = PrefCategory.CreateEntry<bool>("IsEnabled", true, "Enable Mod", (string)null, false, false, (ValueValidator)null, (string)null);
PrefController = PrefCategory.CreateEntry<ControllerSide>("ControllerSide", ControllerSide.Left, "Controller Side", (string)null, false, false, (ValueValidator)null, (string)null);
PrefNoiseEffect = PrefCategory.CreateEntry<bool>("NoiseEffect", true, "Noise Effect", (string)null, false, false, (ValueValidator)null, (string)null);
PrefNoiseAlpha = PrefCategory.CreateEntry<float>("NoiseAlpha", 0.16f, "Noise Alpha", (string)null, false, false, (ValueValidator)null, (string)null);
PrefHeadForward = PrefCategory.CreateEntry<float>("HeadForward", 0.14f, "Camera Forward Offset", (string)null, false, false, (ValueValidator)null, (string)null);
PrefHeadUp = PrefCategory.CreateEntry<float>("HeadUp", 0.1f, "Camera Up Offset", (string)null, false, false, (ValueValidator)null, (string)null);
}
private void SetupBoneMenu()
{
//IL_000b: 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_0045: Unknown result type (might be due to invalid IL or missing references)
//IL_0084: Unknown result type (might be due to invalid IL or missing references)
//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
//IL_00e1: 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)
Page val = Page.Root.CreatePage("Spectator Mod", Color.white, 0, true);
val.CreateBool("Mod Enabled", Color.green, PrefIsEnabled.Value, (Action<bool>)delegate(bool flag)
{
PrefIsEnabled.Value = flag;
PrefCategory.SaveToFile(false);
if (!flag && _isSpectating)
{
StopSpectating();
}
});
val.CreateEnum("Controller Side", Color.white, (Enum)PrefController.Value, (Action<Enum>)delegate(Enum obj)
{
PrefController.Value = (ControllerSide)(object)obj;
PrefCategory.SaveToFile(false);
});
val.CreateBool("Noise Effect", Color.cyan, PrefNoiseEffect.Value, (Action<bool>)delegate(bool value)
{
PrefNoiseEffect.Value = value;
PrefCategory.SaveToFile(false);
ApplyNoiseVisibility();
});
val.CreateFloat("Noise Alpha", Color.white, PrefNoiseAlpha.Value, 0.02f, 0.02f, 0.6f, (Action<float>)delegate(float value)
{
PrefNoiseAlpha.Value = value;
PrefCategory.SaveToFile(false);
ApplyNoiseAlpha();
});
val.CreateFloat("Camera Forward", Color.white, PrefHeadForward.Value, 0.01f, 0f, 0.4f, (Action<float>)delegate(float value)
{
PrefHeadForward.Value = value;
PrefCategory.SaveToFile(false);
});
val.CreateFloat("Camera Up", Color.white, PrefHeadUp.Value, 0.01f, 0f, 0.4f, (Action<float>)delegate(float value)
{
PrefHeadUp.Value = value;
PrefCategory.SaveToFile(false);
});
}
private void OnLevelLoaded(LevelInfo info)
{
StopSpectating();
}
public override void OnUpdate()
{
//IL_018b: 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_01ba: Unknown result type (might be due to invalid IL or missing references)
//IL_01bf: Unknown result type (might be due to invalid IL or missing references)
if (!PrefIsEnabled.Value || (Object)(object)Player.RigManager == (Object)null)
{
return;
}
bool flag = false;
bool flag2 = false;
try
{
BaseController leftController = Player.LeftController;
BaseController rightController = Player.RightController;
bool flag3 = PrefController.Value == ControllerSide.Left || PrefController.Value == ControllerSide.Both;
bool flag4 = PrefController.Value == ControllerSide.Right || PrefController.Value == ControllerSide.Both;
bool flag5 = flag3 && (Object)(object)leftController != (Object)null && leftController.GetBButtonDown();
bool flag6 = flag4 && (Object)(object)rightController != (Object)null && rightController.GetBButtonDown();
bool flag7 = flag3 && (Object)(object)leftController != (Object)null && leftController.GetBButton();
bool flag8 = flag4 && (Object)(object)rightController != (Object)null && rightController.GetBButton();
bool flag9 = flag7 || flag8;
if ((flag5 || flag6) && !_isButtonHeld)
{
_buttonPressTime = Time.time;
_isButtonHeld = true;
}
else if (!flag9 && _isButtonHeld)
{
_isButtonHeld = false;
float num = Time.time - _buttonPressTime;
if (num <= 0.4f && _buttonPressTime > 0f)
{
flag = true;
}
_buttonPressTime = -1f;
}
Vector2 thumbStickAxis;
if (flag3 && (Object)(object)leftController != (Object)null)
{
bool num2 = flag2;
thumbStickAxis = leftController.GetThumbStickAxis();
flag2 = num2 | (((Vector2)(ref thumbStickAxis)).magnitude > 0.2f);
}
if (flag4 && (Object)(object)rightController != (Object)null)
{
bool num3 = flag2;
thumbStickAxis = rightController.GetThumbStickAxis();
flag2 = num3 | (((Vector2)(ref thumbStickAxis)).magnitude > 0.2f);
}
}
catch
{
}
if (Input.GetKeyDown((KeyCode)107))
{
flag = true;
}
if (Input.GetKey((KeyCode)119) || Input.GetKey((KeyCode)97) || Input.GetKey((KeyCode)115) || Input.GetKey((KeyCode)100))
{
flag2 = true;
}
if (flag)
{
if (!_isSpectating)
{
StartSpectating();
}
else
{
CycleTarget();
}
}
if (_isSpectating)
{
if (flag2)
{
StopSpectating();
return;
}
UpdateCameraPosition();
UpdateHudTransform();
UpdateRecNoise();
}
}
private void StartSpectating()
{
RefreshPlayers();
if (_players.Count != 0)
{
_isSpectating = true;
_currentPlayerIndex = 0;
SetupCamera();
AttachToCurrentPlayer();
CreateHud();
}
}
private void StopSpectating()
{
_isSpectating = false;
_currentPlayer = null;
DestroyHud();
if ((Object)(object)_specCamera != (Object)null)
{
Object.Destroy((Object)(object)((Component)_specCamera).gameObject);
_specCamera = null;
}
if ((Object)(object)_mainCamera != (Object)null)
{
((Behaviour)_mainCamera).enabled = _mainCamWasEnabled;
_mainCamera = null;
}
}
private void CycleTarget()
{
RefreshPlayers();
if (_players.Count == 0)
{
StopSpectating();
return;
}
_currentPlayerIndex++;
if (_currentPlayerIndex >= _players.Count)
{
_currentPlayerIndex = 0;
}
AttachToCurrentPlayer();
}
private void RefreshPlayers()
{
_players.Clear();
RigManager rigManager = Player.RigManager;
object obj;
if (rigManager == null)
{
obj = null;
}
else
{
PhysicsRig physicsRig = rigManager.physicsRig;
obj = ((physicsRig != null) ? ((Rig)physicsRig).m_head : null);
}
if ((Object)obj != (Object)null)
{
_players.Add(new TrackedPlayer
{
HeadBone = ((Rig)Player.RigManager.physicsRig).m_head,
Name = "LOCAL PLAYER"
});
}
Il2CppArrayBase<RigManager> val = Object.FindObjectsOfType<RigManager>();
foreach (RigManager item in val)
{
if ((Object)(object)item != (Object)(object)Player.RigManager)
{
PhysicsRig physicsRig2 = item.physicsRig;
if ((Object)(object)((physicsRig2 != null) ? ((Rig)physicsRig2).m_head : null) != (Object)null)
{
_players.Add(new TrackedPlayer
{
HeadBone = ((Rig)item.physicsRig).m_head,
Name = "FUSION PLAYER"
});
}
}
}
}
private void SetupCamera()
{
//IL_001b: Unknown result type (might be due to invalid IL or missing references)
//IL_0021: Expected O, but got Unknown
if ((Object)(object)_specCamera != (Object)null)
{
return;
}
GameObject val = new GameObject("SpectatorCamera_VR");
_specCamera = val.AddComponent<Camera>();
if ((Object)(object)Camera.main != (Object)null)
{
_mainCamera = Camera.main;
_mainCamWasEnabled = ((Behaviour)_mainCamera).enabled;
_specCamera.CopyFrom(Camera.main);
((Behaviour)_mainCamera).enabled = false;
}
else
{
_specCamera.fieldOfView = 90f;
}
_specCamera.nearClipPlane = 0.05f;
_specCamera.targetTexture = null;
_specCamera.depth = 100f;
((Behaviour)_specCamera).enabled = true;
try
{
_specCamera.stereoTargetEye = (StereoTargetEyeMask)3;
}
catch
{
}
try
{
_specCamera.clearFlags = (CameraClearFlags)1;
}
catch
{
}
}
private void AttachToCurrentPlayer()
{
if (_players.Count != 0 && _currentPlayerIndex < _players.Count)
{
_currentPlayer = _players[_currentPlayerIndex];
}
}
private void UpdateCameraPosition()
{
//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_004e: 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_0054: Unknown result type (might be due to invalid IL or missing references)
//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_006f: Unknown result type (might be due to invalid IL or missing references)
//IL_0074: Unknown result type (might be due to invalid IL or missing references)
//IL_0079: Unknown result type (might be due to invalid IL or missing references)
//IL_0086: Unknown result type (might be due to invalid IL or missing references)
//IL_008b: 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_00a2: Unknown result type (might be due to invalid IL or missing references)
//IL_00a3: 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)
if ((Object)(object)_currentPlayer?.HeadBone == (Object)null || (Object)(object)_specCamera == (Object)null)
{
StopSpectating();
return;
}
Transform headBone = _currentPlayer.HeadBone;
Vector3 up = headBone.up;
Vector3 forward = headBone.forward;
Vector3 val = up * PrefHeadUp.Value + forward * PrefHeadForward.Value;
((Component)_specCamera).transform.position = headBone.position + val;
((Component)_specCamera).transform.rotation = Quaternion.LookRotation(forward, up);
}
private void CreateHud()
{
//IL_0034: Unknown result type (might be due to invalid IL or missing references)
//IL_003e: Expected O, but got Unknown
//IL_0075: Unknown result type (might be due to invalid IL or missing references)
//IL_008b: Unknown result type (might be due to invalid IL or missing references)
//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
//IL_00c9: 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)
if (!((Object)(object)_hudRoot != (Object)null) && !((Object)(object)_specCamera == (Object)null))
{
EnsureHudAssets();
_hudRoot = new GameObject("SpectatorHUD");
_hudRoot.transform.SetParent(((Component)_specCamera).transform, false);
_hudRoot.transform.localPosition = new Vector3(0f, 0f, 0.15f);
_hudRoot.transform.localRotation = Quaternion.identity;
_hudRoot.transform.localScale = Vector3.one;
_noiseTex = CreateNoiseTexture();
_noiseRenderer = MakeQuad(Vector3.zero, new Vector2(1f, 1f), (Texture)(object)_noiseTex, new Color(1f, 1f, 1f, PrefNoiseAlpha.Value)).GetComponent<MeshRenderer>();
if ((Object)(object)_noiseRenderer != (Object)null && (Object)(object)((Renderer)_noiseRenderer).sharedMaterial != (Object)null)
{
((Renderer)_noiseRenderer).sharedMaterial.renderQueue = 4000;
}
ApplyNoiseVisibility();
}
}
private void UpdateHudTransform()
{
//IL_0041: Unknown result type (might be due to invalid IL or missing references)
//IL_0057: Unknown result type (might be due to invalid IL or missing references)
//IL_0097: Unknown result type (might be due to invalid IL or missing references)
//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
if (!((Object)(object)_hudRoot == (Object)null) && !((Object)(object)_specCamera == (Object)null))
{
_hudRoot.transform.localPosition = new Vector3(0f, 0f, 0.15f);
_hudRoot.transform.localRotation = Quaternion.identity;
float num = 0.3f * Mathf.Tan(_specCamera.fieldOfView * 0.5f * ((float)Math.PI / 180f));
_hudRoot.transform.localScale = new Vector3(num, num, 1f);
float num2 = Mathf.Max(0.1f, _specCamera.aspect);
if ((Object)(object)_noiseRenderer != (Object)null)
{
((Component)_noiseRenderer).transform.localPosition = Vector3.zero;
((Component)_noiseRenderer).transform.localScale = new Vector3(num2, 1f, 1f);
}
}
}
private void EnsureHudAssets()
{
//IL_0016: Unknown result type (might be due to invalid IL or missing references)
//IL_0020: Expected O, but got Unknown
//IL_004e: 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_0069: Unknown result type (might be due to invalid IL or missing references)
//IL_006e: Unknown result type (might be due to invalid IL or missing references)
//IL_0084: Unknown result type (might be due to invalid IL or missing references)
//IL_0089: 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_00cc: Unknown result type (might be due to invalid IL or missing references)
//IL_00d1: 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_00e7: Unknown result type (might be due to invalid IL or missing references)
//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
//IL_00fd: 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_0113: Unknown result type (might be due to invalid IL or missing references)
if ((Object)(object)_quadMesh == (Object)null)
{
_quadMesh = new Mesh();
((Object)_quadMesh).name = "SpectatorQuad";
_quadMesh.vertices = Il2CppStructArray<Vector3>.op_Implicit((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)
});
_quadMesh.uv = Il2CppStructArray<Vector2>.op_Implicit((Vector2[])(object)new Vector2[4]
{
new Vector2(0f, 0f),
new Vector2(1f, 0f),
new Vector2(0f, 1f),
new Vector2(1f, 1f)
});
_quadMesh.triangles = Il2CppStructArray<int>.op_Implicit(new int[6] { 0, 2, 1, 2, 3, 1 });
_quadMesh.RecalculateNormals();
_quadMesh.RecalculateBounds();
}
if ((Object)(object)_hudShader == (Object)null)
{
_hudShader = FindShader("Unlit/Transparent", "Sprites/Default", "Unlit/Color", "Standard");
}
}
private static Shader FindShader(params string[] names)
{
for (int i = 0; i < names.Length; i++)
{
Shader val = Shader.Find(names[i]);
if ((Object)(object)val != (Object)null)
{
return val;
}
}
return null;
}
private GameObject MakeQuad(Vector3 localPos, Vector2 size, Texture tex, Color color)
{
//IL_0006: Unknown result type (might be due to invalid IL or missing references)
//IL_000c: Expected O, but got Unknown
//IL_002a: Unknown result type (might be due to invalid IL or missing references)
//IL_0037: Unknown result type (might be due to invalid IL or missing references)
//IL_0048: Unknown result type (might be due to invalid IL or missing references)
//IL_004e: 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_00a6: Unknown result type (might be due to invalid IL or missing references)
//IL_00ad: Expected O, but got Unknown
//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
GameObject val = new GameObject("Noise");
val.transform.SetParent(_hudRoot.transform, false);
val.transform.localPosition = localPos;
val.transform.localRotation = Quaternion.identity;
val.transform.localScale = new Vector3(size.x, size.y, 1f);
MeshFilter val2 = val.AddComponent<MeshFilter>();
val2.sharedMesh = _quadMesh;
MeshRenderer val3 = val.AddComponent<MeshRenderer>();
((Renderer)val3).shadowCastingMode = (ShadowCastingMode)0;
((Renderer)val3).receiveShadows = false;
if ((Object)(object)_hudShader != (Object)null)
{
Material val4 = new Material(_hudShader);
if ((Object)(object)tex != (Object)null)
{
val4.mainTexture = tex;
}
val4.color = color;
val4.renderQueue = 4000;
((Renderer)val3).sharedMaterial = val4;
}
return val;
}
private Texture2D CreateNoiseTexture()
{
//IL_0007: Unknown result type (might be due to invalid IL or missing references)
//IL_000d: Expected O, but got Unknown
Texture2D val = new Texture2D(64, 64, (TextureFormat)4, false);
((Texture)val).filterMode = (FilterMode)0;
((Texture)val).wrapMode = (TextureWrapMode)0;
_noisePixels = (Color32[])(object)new Color32[4096];
RandomizeNoise();
val.SetPixels32(Il2CppStructArray<Color32>.op_Implicit(_noisePixels));
val.Apply(false);
return val;
}
private void RandomizeNoise()
{
//IL_0031: Unknown result type (might be due to invalid IL or missing references)
//IL_0036: Unknown result type (might be due to invalid IL or missing references)
if (_noisePixels != null)
{
for (int i = 0; i < _noisePixels.Length; i++)
{
byte b = (byte)Random.Range(0, 255);
_noisePixels[i] = new Color32(b, b, b, byte.MaxValue);
}
}
}
private void UpdateRecNoise()
{
if (!((Object)(object)_noiseTex == (Object)null) && !((Object)(object)_noiseRenderer == (Object)null))
{
_noiseCounter++;
if (_noiseCounter >= 3)
{
_noiseCounter = 0;
RandomizeNoise();
_noiseTex.SetPixels32(Il2CppStructArray<Color32>.op_Implicit(_noisePixels));
_noiseTex.Apply(false);
}
}
}
private void ApplyNoiseVisibility()
{
bool value = PrefNoiseEffect.Value;
if ((Object)(object)_noiseRenderer != (Object)null)
{
((Renderer)_noiseRenderer).enabled = value;
}
}
private void ApplyNoiseAlpha()
{
//IL_0034: Unknown result type (might be due to invalid IL or missing references)
//IL_0039: Unknown result type (might be due to invalid IL or missing references)
//IL_0056: Unknown result type (might be due to invalid IL or missing references)
if (!((Object)(object)_noiseRenderer == (Object)null) && !((Object)(object)((Renderer)_noiseRenderer).sharedMaterial == (Object)null))
{
Color color = ((Renderer)_noiseRenderer).sharedMaterial.color;
color.a = PrefNoiseAlpha.Value;
((Renderer)_noiseRenderer).sharedMaterial.color = color;
}
}
private void DestroyHud()
{
if ((Object)(object)_noiseTex != (Object)null)
{
Object.Destroy((Object)(object)_noiseTex);
_noiseTex = null;
}
if ((Object)(object)_hudRoot != (Object)null)
{
Object.Destroy((Object)(object)_hudRoot);
_hudRoot = null;
}
_noiseRenderer = null;
_noisePixels = null;
}
}