Decompiled source of PS1 Graphics Mod v1.0.0

plugins/PS1GraphicsPlugin.dll

Decompiled 2 days ago
using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[BepInPlugin("com.fluxxen.ps1graphics", "PS1 Graphics Mod", "1.0.0")]
public class PS1GraphicsPlugin : BaseUnityPlugin
{
	private const int RENDER_WIDTH = 640;

	private const int RENDER_HEIGHT = 224;

	private const int SCREEN_WIDTH = 1280;

	private const int SCREEN_HEIGHT = 720;

	private const int TARGET_FPS = 30;

	public static PS1GraphicsPlugin Instance;

	public Camera m_MainCamera;

	public PS1Effect m_Effect;

	public bool m_Initialized;

	private Material m_PS1Material;

	private Shader m_PS1Shader;

	private AssetBundle m_ShaderBundle;

	private GameObject m_PersistentObject;

	public static ConfigEntry<int> ConfigColorDepth;

	public static ConfigEntry<float> ConfigDitherStrength;

	public static ConfigEntry<float> ConfigDitherScale;

	public static ConfigEntry<bool> ConfigAffineEnabled;

	public static ConfigEntry<float> ConfigAffineIntensity;

	public static ConfigEntry<bool> ConfigJitterEnabled;

	public static ConfigEntry<float> ConfigJitterIntensity;

	public static ConfigEntry<bool> ConfigCRTEnabled;

	public static ConfigEntry<float> ConfigScanlineIntensity;

	public static ConfigEntry<float> ConfigChromaticAberration;

	public static ConfigEntry<float> ConfigCurvature;

	public static ConfigEntry<float> ConfigVignette;

	public static ConfigEntry<bool> ConfigFogEnabled;

	public static ConfigEntry<float> ConfigFogColorR;

	public static ConfigEntry<float> ConfigFogColorG;

	public static ConfigEntry<float> ConfigFogColorB;

	public static ConfigEntry<float> ConfigFogStart;

	public static ConfigEntry<float> ConfigFogEnd;

	public static ConfigEntry<float> ConfigFogDensity;

	public static ConfigEntry<bool> ConfigNoiseEnabled;

	public static ConfigEntry<float> ConfigNoiseIntensity;

	public static ConfigEntry<float> ConfigInterlaceStrength;

	public static ConfigEntry<float> ConfigSaturation;

	public static ConfigEntry<float> ConfigContrast;

	public static ConfigEntry<float> ConfigBrightness;

	public static ConfigEntry<float> ConfigPosterize;

	public static ConfigEntry<int> ConfigRenderWidth;

	public static ConfigEntry<int> ConfigRenderHeight;

	public static ConfigEntry<int> ConfigScreenWidth;

	public static ConfigEntry<int> ConfigScreenHeight;

	public static ConfigEntry<int> ConfigTargetFPS;

	private void Awake()
	{
		Instance = this;
		InitializeConfig();
		ApplyGraphicsSettings();
		LoadShaderBundle();
		CreatePersistentObject();
		SceneManager.sceneLoaded += OnSceneLoaded;
		((BaseUnityPlugin)this).Logger.LogInfo((object)"PS1 Graphics Mod initialized");
	}

	private void InitializeConfig()
	{
		//IL_0025: Unknown result type (might be due to invalid IL or missing references)
		//IL_002f: Expected O, but got Unknown
		//IL_0063: Unknown result type (might be due to invalid IL or missing references)
		//IL_006d: Expected O, but got Unknown
		//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ab: Expected O, but got Unknown
		//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
		//IL_0109: Expected O, but got Unknown
		//IL_015d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0167: Expected O, but got Unknown
		//IL_01bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c5: Expected O, but got Unknown
		//IL_01f9: Unknown result type (might be due to invalid IL or missing references)
		//IL_0203: Expected O, but got Unknown
		//IL_0237: Unknown result type (might be due to invalid IL or missing references)
		//IL_0241: Expected O, but got Unknown
		//IL_0275: Unknown result type (might be due to invalid IL or missing references)
		//IL_027f: Expected O, but got Unknown
		//IL_02d3: Unknown result type (might be due to invalid IL or missing references)
		//IL_02dd: Expected O, but got Unknown
		//IL_0311: Unknown result type (might be due to invalid IL or missing references)
		//IL_031b: Expected O, but got Unknown
		//IL_034f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0359: Expected O, but got Unknown
		//IL_038d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0397: Expected O, but got Unknown
		//IL_03cb: Unknown result type (might be due to invalid IL or missing references)
		//IL_03d5: Expected O, but got Unknown
		//IL_0409: Unknown result type (might be due to invalid IL or missing references)
		//IL_0413: Expected O, but got Unknown
		//IL_0467: Unknown result type (might be due to invalid IL or missing references)
		//IL_0471: Expected O, but got Unknown
		//IL_04a5: Unknown result type (might be due to invalid IL or missing references)
		//IL_04af: Expected O, but got Unknown
		//IL_04e3: Unknown result type (might be due to invalid IL or missing references)
		//IL_04ed: Expected O, but got Unknown
		//IL_0521: Unknown result type (might be due to invalid IL or missing references)
		//IL_052b: Expected O, but got Unknown
		//IL_055f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0569: Expected O, but got Unknown
		//IL_059d: Unknown result type (might be due to invalid IL or missing references)
		//IL_05a7: Expected O, but got Unknown
		//IL_05db: Unknown result type (might be due to invalid IL or missing references)
		//IL_05e5: Expected O, but got Unknown
		//IL_0616: Unknown result type (might be due to invalid IL or missing references)
		//IL_0620: Expected O, but got Unknown
		//IL_0654: Unknown result type (might be due to invalid IL or missing references)
		//IL_065e: Expected O, but got Unknown
		//IL_0692: Unknown result type (might be due to invalid IL or missing references)
		//IL_069c: Expected O, but got Unknown
		//IL_06c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_06d1: Expected O, but got Unknown
		ConfigColorDepth = ((BaseUnityPlugin)this).Config.Bind<int>("Shader Settings", "ColorDepth", 32, new ConfigDescription("Color bit depth for the PS1 effect", (AcceptableValueBase)(object)new AcceptableValueRange<int>(4, 32), new object[0]));
		ConfigDitherStrength = ((BaseUnityPlugin)this).Config.Bind<float>("Shader Settings", "DitherStrength", 0.182f, new ConfigDescription("Strength of the dithering effect", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), new object[0]));
		ConfigDitherScale = ((BaseUnityPlugin)this).Config.Bind<float>("Shader Settings", "DitherScale", 1.26f, new ConfigDescription("Scale of the dithering pattern", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 10f), new object[0]));
		ConfigAffineEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("Shader Settings", "AffineEnabled", false, "Enable affine texture warping (PS1 style texture distortion)");
		ConfigAffineIntensity = ((BaseUnityPlugin)this).Config.Bind<float>("Shader Settings", "AffineIntensity", 0.3f, new ConfigDescription("Intensity of affine texture warping", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), new object[0]));
		ConfigJitterEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("Shader Settings", "JitterEnabled", true, "Enable vertex jitter (PS1 style vertex snapping)");
		ConfigJitterIntensity = ((BaseUnityPlugin)this).Config.Bind<float>("Shader Settings", "JitterIntensity", 0.115f, new ConfigDescription("Intensity of vertex jitter", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), new object[0]));
		ConfigCRTEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("CRT Effects", "CRTEnabled", true, "Enable CRT monitor simulation effects");
		ConfigScanlineIntensity = ((BaseUnityPlugin)this).Config.Bind<float>("CRT Effects", "ScanlineIntensity", 0.483f, new ConfigDescription("Intensity of CRT scanlines", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), new object[0]));
		ConfigChromaticAberration = ((BaseUnityPlugin)this).Config.Bind<float>("CRT Effects", "ChromaticAberration", 0.0041f, new ConfigDescription("Chromatic aberration amount", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 0.1f), new object[0]));
		ConfigCurvature = ((BaseUnityPlugin)this).Config.Bind<float>("CRT Effects", "Curvature", 1f, new ConfigDescription("CRT screen curvature", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 2f), new object[0]));
		ConfigVignette = ((BaseUnityPlugin)this).Config.Bind<float>("CRT Effects", "Vignette", 0.2f, new ConfigDescription("Vignette darkening at screen edges", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), new object[0]));
		ConfigFogEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("Fog Settings", "FogEnabled", true, "Enable distance fog effect");
		ConfigFogColorR = ((BaseUnityPlugin)this).Config.Bind<float>("Fog Settings", "FogColorR", 0f, new ConfigDescription("Fog color red component", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), new object[0]));
		ConfigFogColorG = ((BaseUnityPlugin)this).Config.Bind<float>("Fog Settings", "FogColorG", 0.25f, new ConfigDescription("Fog color green component", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), new object[0]));
		ConfigFogColorB = ((BaseUnityPlugin)this).Config.Bind<float>("Fog Settings", "FogColorB", 0.2f, new ConfigDescription("Fog color blue component", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), new object[0]));
		ConfigFogStart = ((BaseUnityPlugin)this).Config.Bind<float>("Fog Settings", "FogStart", 23.9f, new ConfigDescription("Distance where fog starts", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 100f), new object[0]));
		ConfigFogEnd = ((BaseUnityPlugin)this).Config.Bind<float>("Fog Settings", "FogEnd", 40f, new ConfigDescription("Distance where fog is fully opaque", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 200f), new object[0]));
		ConfigFogDensity = ((BaseUnityPlugin)this).Config.Bind<float>("Fog Settings", "FogDensity", 0.157f, new ConfigDescription("Fog density", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), new object[0]));
		ConfigNoiseEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("Noise Effects", "NoiseEnabled", true, "Enable film grain noise effect");
		ConfigNoiseIntensity = ((BaseUnityPlugin)this).Config.Bind<float>("Noise Effects", "NoiseIntensity", 0.0121f, new ConfigDescription("Intensity of noise/grain effect", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 0.5f), new object[0]));
		ConfigInterlaceStrength = ((BaseUnityPlugin)this).Config.Bind<float>("Noise Effects", "InterlaceStrength", 0.017f, new ConfigDescription("Strength of interlacing effect", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), new object[0]));
		ConfigSaturation = ((BaseUnityPlugin)this).Config.Bind<float>("Color Adjustments", "Saturation", 1f, new ConfigDescription("Color saturation", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 2f), new object[0]));
		ConfigContrast = ((BaseUnityPlugin)this).Config.Bind<float>("Color Adjustments", "Contrast", 1f, new ConfigDescription("Image contrast", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 2f), new object[0]));
		ConfigBrightness = ((BaseUnityPlugin)this).Config.Bind<float>("Color Adjustments", "Brightness", 0f, new ConfigDescription("Image brightness offset", (AcceptableValueBase)(object)new AcceptableValueRange<float>(-1f, 1f), new object[0]));
		ConfigPosterize = ((BaseUnityPlugin)this).Config.Bind<float>("Color Adjustments", "Posterize", 0.3f, new ConfigDescription("Posterization amount", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), new object[0]));
		ConfigRenderWidth = ((BaseUnityPlugin)this).Config.Bind<int>("Resolution", "RenderWidth", 640, new ConfigDescription("Internal rendering width (PS1 resolution)", (AcceptableValueBase)(object)new AcceptableValueRange<int>(160, 1920), new object[0]));
		ConfigRenderHeight = ((BaseUnityPlugin)this).Config.Bind<int>("Resolution", "RenderHeight", 224, new ConfigDescription("Internal rendering height (PS1 resolution)", (AcceptableValueBase)(object)new AcceptableValueRange<int>(120, 1080), new object[0]));
		ConfigScreenWidth = ((BaseUnityPlugin)this).Config.Bind<int>("Resolution", "ScreenWidth", 1280, new ConfigDescription("Screen output width", (AcceptableValueBase)(object)new AcceptableValueRange<int>(640, 3840), new object[0]));
		ConfigScreenHeight = ((BaseUnityPlugin)this).Config.Bind<int>("Resolution", "ScreenHeight", 720, new ConfigDescription("Screen output height", (AcceptableValueBase)(object)new AcceptableValueRange<int>(480, 2160), new object[0]));
		ConfigTargetFPS = ((BaseUnityPlugin)this).Config.Bind<int>("Resolution", "TargetFPS", 30, new ConfigDescription("Target frame rate", (AcceptableValueBase)(object)new AcceptableValueRange<int>(15, 60), new object[0]));
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Configuration loaded");
	}

	private void CreatePersistentObject()
	{
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		//IL_001e: Expected O, but got Unknown
		if ((Object)(object)m_PersistentObject == (Object)null)
		{
			m_PersistentObject = new GameObject("PS1GraphicsPersistent");
			Object.DontDestroyOnLoad((Object)(object)m_PersistentObject);
			m_PersistentObject.AddComponent<PS1GraphicsManager>();
		}
	}

	private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
	{
		((BaseUnityPlugin)this).Logger.LogInfo((object)("Scene loaded: " + ((Scene)(ref scene)).name));
		ApplyGraphicsSettings();
		m_Initialized = false;
		m_MainCamera = null;
		m_Effect = null;
		if ((Object)(object)m_PersistentObject != (Object)null)
		{
			PS1GraphicsManager component = m_PersistentObject.GetComponent<PS1GraphicsManager>();
			if ((Object)(object)component != (Object)null)
			{
				((MonoBehaviour)component).StartCoroutine(component.DelayedReinitialize());
				((MonoBehaviour)component).StartCoroutine(component.ReapplySettingsMultipleTimes());
			}
		}
	}

	private void ApplyGraphicsSettings()
	{
		int num = ((ConfigTargetFPS != null) ? ConfigTargetFPS.Value : 30);
		int num2 = ((ConfigScreenWidth != null) ? ConfigScreenWidth.Value : 1280);
		int num3 = ((ConfigScreenHeight != null) ? ConfigScreenHeight.Value : 720);
		Application.targetFrameRate = num;
		QualitySettings.vSyncCount = 0;
		Screen.SetResolution(num2, num3, (FullScreenMode)1);
		QualitySettings.SetQualityLevel(0, true);
		QualitySettings.shadows = (ShadowQuality)0;
		QualitySettings.shadowResolution = (ShadowResolution)0;
		QualitySettings.shadowDistance = 0f;
		QualitySettings.shadowCascades = 0;
		QualitySettings.antiAliasing = 0;
		QualitySettings.softParticles = false;
		QualitySettings.realtimeReflectionProbes = false;
		QualitySettings.billboardsFaceCameraPosition = false;
		QualitySettings.anisotropicFiltering = (AnisotropicFiltering)0;
		QualitySettings.lodBias = 0.3f;
		QualitySettings.maximumLODLevel = 2;
		QualitySettings.particleRaycastBudget = 4;
		QualitySettings.asyncUploadTimeSlice = 1;
		QualitySettings.asyncUploadBufferSize = 4;
		try
		{
			PropertyInfo property = typeof(QualitySettings).GetProperty("pixelLightCount");
			if (!object.ReferenceEquals(property, null))
			{
				property.SetValue(null, 0, null);
			}
			PropertyInfo property2 = typeof(QualitySettings).GetProperty("masterTextureLimit");
			if (!object.ReferenceEquals(property2, null))
			{
				property2.SetValue(null, 3, null);
			}
			PropertyInfo property3 = typeof(QualitySettings).GetProperty("softVegetation");
			if (!object.ReferenceEquals(property3, null))
			{
				property3.SetValue(null, false, null);
			}
		}
		catch
		{
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)("Screen: " + num2 + "x" + num3 + ", FPS: " + num));
	}

	private void LoadShaderBundle()
	{
		string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
		string text = Path.Combine(directoryName, "ps1shaders");
		((BaseUnityPlugin)this).Logger.LogInfo((object)("Looking for shader bundle at: " + text));
		if (File.Exists(text))
		{
			try
			{
				m_ShaderBundle = AssetBundle.LoadFromFile(text);
				if ((Object)(object)m_ShaderBundle != (Object)null)
				{
					((BaseUnityPlugin)this).Logger.LogInfo((object)"AssetBundle loaded, listing assets...");
					string[] allAssetNames = m_ShaderBundle.GetAllAssetNames();
					string[] array = allAssetNames;
					foreach (string text2 in array)
					{
						((BaseUnityPlugin)this).Logger.LogInfo((object)("Asset in bundle: " + text2));
					}
					Shader[] array2 = m_ShaderBundle.LoadAllAssets<Shader>();
					if (array2 != null && array2.Length > 0)
					{
						m_PS1Shader = array2[0];
						((BaseUnityPlugin)this).Logger.LogInfo((object)("Loaded shader: " + ((Object)m_PS1Shader).name + " (isSupported: " + m_PS1Shader.isSupported + ")"));
					}
					else
					{
						((BaseUnityPlugin)this).Logger.LogWarning((object)"No shaders found in bundle");
					}
				}
				else
				{
					((BaseUnityPlugin)this).Logger.LogError((object)"Failed to load AssetBundle");
				}
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Failed to load shader bundle: " + ex.Message));
			}
		}
		else
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)("Shader bundle not found at: " + text));
		}
		if ((Object)(object)m_PS1Shader == (Object)null)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Trying Shader.Find for Fluxxen/PS1Graphics...");
			m_PS1Shader = Shader.Find("Fluxxen/PS1Graphics");
		}
		if ((Object)(object)m_PS1Shader == (Object)null)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Trying Shader.Find for Hidden/PS1Graphics...");
			m_PS1Shader = Shader.Find("Hidden/PS1Graphics");
		}
		if ((Object)(object)m_PS1Shader != (Object)null)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Final shader: " + ((Object)m_PS1Shader).name + " (isSupported: " + m_PS1Shader.isSupported + ")"));
		}
		else
		{
			((BaseUnityPlugin)this).Logger.LogError((object)"No PS1 shader could be loaded!");
		}
	}

	public void UpdateLogic()
	{
		int targetFrameRate = ((ConfigTargetFPS != null) ? ConfigTargetFPS.Value : 30);
		Application.targetFrameRate = targetFrameRate;
		QualitySettings.vSyncCount = 0;
		if (!m_Initialized || (Object)(object)m_MainCamera == (Object)null || (Object)(object)m_Effect == (Object)null)
		{
			TryInitialize();
		}
		else if ((Object)(object)m_MainCamera != (Object)null && !((Component)m_MainCamera).gameObject.activeInHierarchy)
		{
			m_Initialized = false;
			m_MainCamera = null;
			m_Effect = null;
		}
		if ((Object)(object)m_Effect != (Object)null && !((Behaviour)m_Effect).enabled)
		{
			((Behaviour)m_Effect).enabled = true;
		}
	}

	private void TryInitialize()
	{
		m_MainCamera = Camera.main;
		if ((Object)(object)m_MainCamera == (Object)null)
		{
			Camera[] array = Object.FindObjectsOfType<Camera>();
			if (array != null)
			{
				Camera[] array2 = array;
				foreach (Camera val in array2)
				{
					if ((Object)(object)val != (Object)null && ((Behaviour)val).enabled && ((Component)val).gameObject.activeInHierarchy)
					{
						m_MainCamera = val;
						break;
					}
				}
			}
		}
		if (!((Object)(object)m_MainCamera == (Object)null))
		{
			CreateMaterial();
			AttachEffectToCamera();
			m_Initialized = true;
			((BaseUnityPlugin)this).Logger.LogInfo((object)("PS1 Graphics effect attached to camera: " + ((Object)m_MainCamera).name));
		}
	}

	private void CreateMaterial()
	{
		//IL_0088: Unknown result type (might be due to invalid IL or missing references)
		//IL_0092: Expected O, but got Unknown
		//IL_011f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0129: Expected O, but got Unknown
		//IL_017d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0187: Expected O, but got Unknown
		if ((Object)(object)m_PS1Material != (Object)null)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Material already exists, skipping creation");
			return;
		}
		if ((Object)(object)m_PS1Shader != (Object)null)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Creating material from shader: " + ((Object)m_PS1Shader).name + " (isSupported: " + m_PS1Shader.isSupported + ")"));
			m_PS1Material = new Material(m_PS1Shader);
			((Object)m_PS1Material).hideFlags = (HideFlags)61;
			bool flag = m_PS1Material.HasProperty("_ColorDepth");
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Material created. HasProperty _ColorDepth: " + flag));
			return;
		}
		((BaseUnityPlugin)this).Logger.LogWarning((object)"No shader available, trying Shader.Find...");
		m_PS1Shader = Shader.Find("Fluxxen/PS1Graphics");
		if ((Object)(object)m_PS1Shader == (Object)null)
		{
			m_PS1Shader = Shader.Find("Hidden/PS1Graphics");
		}
		if ((Object)(object)m_PS1Shader != (Object)null)
		{
			m_PS1Material = new Material(m_PS1Shader);
			((Object)m_PS1Material).hideFlags = (HideFlags)61;
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Material created via Shader.Find: " + ((Object)m_PS1Shader).name));
			return;
		}
		((BaseUnityPlugin)this).Logger.LogError((object)"No PS1 shader found! Using fallback.");
		Shader val = Shader.Find("Sprites/Default");
		if ((Object)(object)val != (Object)null)
		{
			m_PS1Material = new Material(val);
			((Object)m_PS1Material).hideFlags = (HideFlags)61;
		}
	}

	private void AttachEffectToCamera()
	{
		if (!((Object)(object)m_MainCamera == (Object)null))
		{
			PS1Effect component = ((Component)m_MainCamera).gameObject.GetComponent<PS1Effect>();
			if ((Object)(object)component != (Object)null)
			{
				Object.Destroy((Object)(object)component);
			}
			int width = ((ConfigRenderWidth != null) ? ConfigRenderWidth.Value : 640);
			int height = ((ConfigRenderHeight != null) ? ConfigRenderHeight.Value : 224);
			m_Effect = ((Component)m_MainCamera).gameObject.AddComponent<PS1Effect>();
			m_Effect.Initialize(m_PS1Material, width, height);
		}
	}

	private void OnDestroy()
	{
		SceneManager.sceneLoaded -= OnSceneLoaded;
		if ((Object)(object)m_PersistentObject != (Object)null)
		{
			Object.Destroy((Object)(object)m_PersistentObject);
		}
		if ((Object)(object)m_PS1Material != (Object)null)
		{
			Object.Destroy((Object)(object)m_PS1Material);
		}
		if ((Object)(object)m_ShaderBundle != (Object)null)
		{
			m_ShaderBundle.Unload(true);
		}
	}
}
public class PS1GraphicsManager : MonoBehaviour
{
	private void Update()
	{
		if ((Object)(object)PS1GraphicsPlugin.Instance != (Object)null)
		{
			PS1GraphicsPlugin.Instance.UpdateLogic();
		}
	}

	public IEnumerator DelayedReinitialize()
	{
		yield return (object)new WaitForSeconds(0.1f);
		yield return (object)new WaitForEndOfFrame();
		if ((Object)(object)PS1GraphicsPlugin.Instance != (Object)null)
		{
			int targetFrameRate = ((PS1GraphicsPlugin.ConfigTargetFPS != null) ? PS1GraphicsPlugin.ConfigTargetFPS.Value : 30);
			Application.targetFrameRate = targetFrameRate;
			PS1GraphicsPlugin.Instance.m_Initialized = false;
			PS1GraphicsPlugin.Instance.m_MainCamera = null;
			PS1GraphicsPlugin.Instance.m_Effect = null;
		}
	}

	public IEnumerator ReapplySettingsMultipleTimes()
	{
		for (int i = 0; i < 5; i++)
		{
			yield return (object)new WaitForSeconds(0.5f);
			int targetFPS = ((PS1GraphicsPlugin.ConfigTargetFPS != null) ? PS1GraphicsPlugin.ConfigTargetFPS.Value : 30);
			int screenW = ((PS1GraphicsPlugin.ConfigScreenWidth != null) ? PS1GraphicsPlugin.ConfigScreenWidth.Value : 1280);
			int screenH = ((PS1GraphicsPlugin.ConfigScreenHeight != null) ? PS1GraphicsPlugin.ConfigScreenHeight.Value : 720);
			Application.targetFrameRate = targetFPS;
			QualitySettings.vSyncCount = 0;
			Screen.SetResolution(screenW, screenH, (FullScreenMode)1);
			if ((Object)(object)PS1GraphicsPlugin.Instance != (Object)null && ((Object)(object)PS1GraphicsPlugin.Instance.m_Effect == (Object)null || (Object)(object)PS1GraphicsPlugin.Instance.m_MainCamera == (Object)null))
			{
				PS1GraphicsPlugin.Instance.m_Initialized = false;
			}
		}
	}
}
[DefaultExecutionOrder(-32000)]
public class PS1Effect : MonoBehaviour
{
	private Material m_Material;

	private int m_Width;

	private int m_Height;

	private Camera m_Camera;

	private bool m_Initialized;

	private bool m_HasPS1Shader;

	public void Initialize(Material mat, int width, int height)
	{
		//IL_0036: Unknown result type (might be due to invalid IL or missing references)
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		m_Material = mat;
		m_Width = width;
		m_Height = height;
		m_Camera = ((Component)this).GetComponent<Camera>();
		if ((Object)(object)m_Camera != (Object)null)
		{
			Camera camera = m_Camera;
			camera.depthTextureMode = (DepthTextureMode)(camera.depthTextureMode | 1);
		}
		m_HasPS1Shader = (Object)(object)m_Material != (Object)null && m_Material.HasProperty("_ColorDepth");
		m_Initialized = true;
	}

	private void OnRenderImage(RenderTexture source, RenderTexture destination)
	{
		if (!m_Initialized || (Object)(object)m_Material == (Object)null || !m_HasPS1Shader)
		{
			RenderTexture temporary = RenderTexture.GetTemporary(m_Width, m_Height, 0, (RenderTextureFormat)0);
			((Texture)temporary).filterMode = (FilterMode)0;
			Graphics.Blit((Texture)(object)source, temporary);
			Graphics.Blit((Texture)(object)temporary, destination);
			RenderTexture.ReleaseTemporary(temporary);
		}
		else
		{
			SetShaderParameters();
			RenderTexture temporary2 = RenderTexture.GetTemporary(m_Width, m_Height, 0, (RenderTextureFormat)0);
			((Texture)temporary2).filterMode = (FilterMode)0;
			Graphics.Blit((Texture)(object)source, temporary2);
			Graphics.Blit((Texture)(object)temporary2, destination, m_Material);
			RenderTexture.ReleaseTemporary(temporary2);
		}
	}

	private void SetShaderParameters()
	{
		//IL_0297: Unknown result type (might be due to invalid IL or missing references)
		//IL_045b: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)m_Material == (Object)null) && m_HasPS1Shader)
		{
			m_Material.SetFloat("_ColorDepth", (float)((PS1GraphicsPlugin.ConfigColorDepth != null) ? PS1GraphicsPlugin.ConfigColorDepth.Value : 32));
			m_Material.SetFloat("_DitherStrength", (PS1GraphicsPlugin.ConfigDitherStrength != null) ? PS1GraphicsPlugin.ConfigDitherStrength.Value : 0.182f);
			m_Material.SetFloat("_DitherScale", (PS1GraphicsPlugin.ConfigDitherScale != null) ? PS1GraphicsPlugin.ConfigDitherScale.Value : 1.26f);
			m_Material.SetFloat("_AffineEnabled", (PS1GraphicsPlugin.ConfigAffineEnabled != null && PS1GraphicsPlugin.ConfigAffineEnabled.Value) ? 1f : 0f);
			m_Material.SetFloat("_AffineIntensity", (PS1GraphicsPlugin.ConfigAffineIntensity != null) ? PS1GraphicsPlugin.ConfigAffineIntensity.Value : 0.3f);
			m_Material.SetFloat("_JitterEnabled", (PS1GraphicsPlugin.ConfigJitterEnabled != null && PS1GraphicsPlugin.ConfigJitterEnabled.Value) ? 1f : 0f);
			m_Material.SetFloat("_JitterIntensity", (PS1GraphicsPlugin.ConfigJitterIntensity != null) ? PS1GraphicsPlugin.ConfigJitterIntensity.Value : 0.115f);
			m_Material.SetFloat("_CRTEnabled", (PS1GraphicsPlugin.ConfigCRTEnabled != null && PS1GraphicsPlugin.ConfigCRTEnabled.Value) ? 1f : 0f);
			m_Material.SetFloat("_ScanlineIntensity", (PS1GraphicsPlugin.ConfigScanlineIntensity != null) ? PS1GraphicsPlugin.ConfigScanlineIntensity.Value : 0.483f);
			m_Material.SetFloat("_ChromaticAberration", (PS1GraphicsPlugin.ConfigChromaticAberration != null) ? PS1GraphicsPlugin.ConfigChromaticAberration.Value : 0.0041f);
			m_Material.SetFloat("_Curvature", (PS1GraphicsPlugin.ConfigCurvature != null) ? PS1GraphicsPlugin.ConfigCurvature.Value : 1f);
			m_Material.SetFloat("_Vignette", (PS1GraphicsPlugin.ConfigVignette != null) ? PS1GraphicsPlugin.ConfigVignette.Value : 0.2f);
			float num = ((PS1GraphicsPlugin.ConfigFogColorR != null) ? PS1GraphicsPlugin.ConfigFogColorR.Value : 0f);
			float num2 = ((PS1GraphicsPlugin.ConfigFogColorG != null) ? PS1GraphicsPlugin.ConfigFogColorG.Value : 0.25f);
			float num3 = ((PS1GraphicsPlugin.ConfigFogColorB != null) ? PS1GraphicsPlugin.ConfigFogColorB.Value : 0.2f);
			m_Material.SetFloat("_FogEnabled", (PS1GraphicsPlugin.ConfigFogEnabled != null && PS1GraphicsPlugin.ConfigFogEnabled.Value) ? 1f : 0f);
			m_Material.SetColor("_FogColor", new Color(num, num2, num3, 1f));
			m_Material.SetFloat("_FogStart", (PS1GraphicsPlugin.ConfigFogStart != null) ? PS1GraphicsPlugin.ConfigFogStart.Value : 23.9f);
			m_Material.SetFloat("_FogEnd", (PS1GraphicsPlugin.ConfigFogEnd != null) ? PS1GraphicsPlugin.ConfigFogEnd.Value : 40f);
			m_Material.SetFloat("_FogDensity", (PS1GraphicsPlugin.ConfigFogDensity != null) ? PS1GraphicsPlugin.ConfigFogDensity.Value : 0.157f);
			m_Material.SetFloat("_NoiseEnabled", (PS1GraphicsPlugin.ConfigNoiseEnabled != null && PS1GraphicsPlugin.ConfigNoiseEnabled.Value) ? 1f : 0f);
			m_Material.SetFloat("_NoiseIntensity", (PS1GraphicsPlugin.ConfigNoiseIntensity != null) ? PS1GraphicsPlugin.ConfigNoiseIntensity.Value : 0.0121f);
			m_Material.SetFloat("_InterlaceStrength", (PS1GraphicsPlugin.ConfigInterlaceStrength != null) ? PS1GraphicsPlugin.ConfigInterlaceStrength.Value : 0.017f);
			m_Material.SetFloat("_Saturation", (PS1GraphicsPlugin.ConfigSaturation != null) ? PS1GraphicsPlugin.ConfigSaturation.Value : 1f);
			m_Material.SetFloat("_Contrast", (PS1GraphicsPlugin.ConfigContrast != null) ? PS1GraphicsPlugin.ConfigContrast.Value : 1f);
			m_Material.SetFloat("_Brightness", (PS1GraphicsPlugin.ConfigBrightness != null) ? PS1GraphicsPlugin.ConfigBrightness.Value : 0f);
			m_Material.SetFloat("_Posterize", (PS1GraphicsPlugin.ConfigPosterize != null) ? PS1GraphicsPlugin.ConfigPosterize.Value : 0.3f);
			m_Material.SetVector("_Resolution", new Vector4((float)m_Width, (float)m_Height, 0f, 0f));
		}
	}
}