Decompiled source of ViewExtension v1.3.0

ViewExtension.dll

Decompiled 5 months ago
using System;
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 BepInEx;
using BepInEx.Configuration;
using GameNetcodeStuff;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("ViewExtension")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Increase the camera's culling distance")]
[assembly: AssemblyFileVersion("1.3.0.0")]
[assembly: AssemblyInformationalVersion("1.3.0")]
[assembly: AssemblyProduct("ViewExtension")]
[assembly: AssemblyTitle("ViewExtension")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.3.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;
		}
	}
}
public static class ViewExtensionConfig
{
	public static ConfigEntry<float> FarClipPlane;

	public static ConfigEntry<bool> MenuOption { get; set; }

	public static ConfigEntry<bool> DebugLogging { get; set; }

	internal static ConfigFile Config { get; private set; }

	internal static void Init(ConfigFile config)
	{
		Config = config;
		FarClipPlane = config.Bind<float>("1. General", "FarClipPlane", 1000f, "View distance in units. Vanilla is 400. (between 1 and 10000)");
		MenuOption = config.Bind<bool>("1. General", "MenuOption", true, "Whether to add an option in the settings menu to change the view distance.");
		DebugLogging = config.Bind<bool>("2. Debugging", "DebugLogging", false, "Whether to display debug messages.");
	}

	public static void Save()
	{
		Config.Save();
	}
}
[BepInPlugin("ViewExtension", "ViewExtension", "1.3.0")]
public class ViewExtensionLoader : BaseUnityPlugin
{
	private static string assetBundleName = "viewextensionassetbundle";

	private static AssetBundle assetBundle;

	private static GameObject settingsObject;

	public static bool isMainMenu;

	private void Awake()
	{
		ViewExtensionConfig.Init(((BaseUnityPlugin)this).Config);
		if (ViewExtensionConfig.DebugLogging.Value)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)("MenuOption is set to " + ViewExtensionConfig.MenuOption.Value));
		}
		if (ViewExtensionConfig.MenuOption.Value)
		{
			LoadAssetBundle();
		}
		if (float.IsNaN(ViewExtensionConfig.FarClipPlane.Value) || ViewExtensionConfig.FarClipPlane.Value < 1f || ViewExtensionConfig.FarClipPlane.Value > 10000f)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)("Invalid value for FarClipPlane in the config file (" + ViewExtensionConfig.FarClipPlane.Value + "). Using default value of 1000."));
			ViewExtensionConfig.FarClipPlane.Value = 1000f;
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)("View distance adjusted to " + ViewExtensionConfig.FarClipPlane.Value));
		if (ViewExtensionConfig.MenuOption.Value)
		{
			SceneManager.activeSceneChanged += CheckSceneState;
		}
	}

	private static void LoadAssetBundle()
	{
		string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
		string text = Path.Combine(directoryName, assetBundleName);
		if (File.Exists(text))
		{
			assetBundle = AssetBundle.LoadFromFile(text);
			if ((Object)(object)assetBundle != (Object)null)
			{
				if (ViewExtensionConfig.DebugLogging.Value)
				{
					Debug.Log((object)("[ViewExtension] AssetBundle loaded successfully from path: " + text));
				}
			}
			else
			{
				Debug.LogError((object)("[ViewExtension] Failed to load AssetBundle at path: " + text));
			}
		}
		else
		{
			Debug.LogError((object)("[ViewExtension] AssetBundle not found at path: " + text));
		}
	}

	private static void CheckSceneState(Scene current, Scene next)
	{
		if (((Scene)(ref next)).name == "SampleSceneRelay")
		{
			if (ViewExtensionConfig.DebugLogging.Value)
			{
				Debug.Log((object)"[ViewExtension] SampleSceneRelay loaded");
			}
			SpawnPlayerMenu();
			isMainMenu = false;
		}
	}

	private static void SpawnPlayerMenu()
	{
		settingsObject = assetBundle.LoadAsset<GameObject>("ViewDistance");
		if ((Object)(object)settingsObject == (Object)null)
		{
			Debug.LogError((object)"[ViewExtension] Failed to load ViewDistance from AssetBundle.");
			return;
		}
		GameObject val = GameObject.Find("QuickMenuManager");
		if ((Object)(object)val == (Object)null)
		{
			Debug.LogError((object)"[ViewExtension] QuickMenuManager object not found.");
			return;
		}
		QuickMenuManager component = val.GetComponent<QuickMenuManager>();
		if ((Object)(object)component == (Object)null)
		{
			Debug.LogError((object)"[ViewExtension] QuickMenuManager script not found.");
			return;
		}
		GameObject settingsPanel = component.settingsPanel;
		GameObject val2 = ((settingsPanel != null) ? settingsPanel.gameObject : null);
		if ((Object)(object)val2 == (Object)null)
		{
			Debug.LogError((object)"[ViewExtension] SettingsPanel object not found.");
			return;
		}
		Object.Instantiate<GameObject>(settingsObject, val2.transform, false);
		if (ViewExtensionConfig.DebugLogging.Value)
		{
			Debug.Log((object)"[ViewExtension] SettingsObject instantiated as child of PlayerMenu.");
		}
	}
}
public class ViewExtensionSettings : MonoBehaviour
{
	public TextMeshProUGUI sliderText;

	private Slider slider;

	private Camera gameplayCamera;

	private void Awake()
	{
		slider = ((Component)this).GetComponent<Slider>();
		if ((Object)(object)sliderText == (Object)null)
		{
			Debug.LogError((object)"[ViewExtension] Slider text reference is missing.");
			return;
		}
		((UnityEvent<float>)(object)slider.onValueChanged).AddListener((UnityAction<float>)OnSettingsChange);
		float value = ViewExtensionConfig.FarClipPlane.Value / 50f;
		slider.value = value;
		UpdateSliderText(ViewExtensionConfig.FarClipPlane.Value);
		if (ViewExtensionLoader.isMainMenu)
		{
			return;
		}
		GameObject val = GameObject.Find("Player");
		if ((Object)(object)val != (Object)null)
		{
			PlayerControllerB component = val.GetComponent<PlayerControllerB>();
			if ((Object)(object)component != (Object)null)
			{
				gameplayCamera = component.gameplayCamera;
				gameplayCamera.farClipPlane = ViewExtensionConfig.FarClipPlane.Value;
			}
			else
			{
				Debug.LogError((object)"[ViewExtension] PlayerControllerB script not found on Player object.");
			}
		}
		else
		{
			Debug.LogError((object)"[ViewExtension] Player object not found.");
		}
	}

	public void OnSettingsChange(float tempDistanceValue)
	{
		int num = Mathf.RoundToInt(tempDistanceValue * 50f);
		ViewExtensionConfig.FarClipPlane.Value = num;
		UpdateSliderText(num);
		if (ViewExtensionConfig.DebugLogging.Value)
		{
			Debug.Log((object)((TMP_Text)sliderText).text);
		}
		if ((Object)(object)gameplayCamera != (Object)null)
		{
			gameplayCamera.farClipPlane = num;
		}
		ViewExtensionConfig.Save();
	}

	private void UpdateSliderText(float value)
	{
		((TMP_Text)sliderText).text = "View Distance: " + Mathf.RoundToInt(value).ToString("G");
	}
}
namespace ViewExtension
{
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "ViewExtension";

		public const string PLUGIN_NAME = "ViewExtension";

		public const string PLUGIN_VERSION = "1.3.0";
	}
}