Decompiled source of SWUpscaleLib v1.0.0

com.github.glarmer.UpscalerLib.dll

Decompiled a day ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.NVIDIA;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("com.github.glarmer.UpscalerLib")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+aa7e4ede1bbd2a0828cf325c1a72c7f9d6066ff2")]
[assembly: AssemblyProduct("com.github.glarmer.UpscalerLib")]
[assembly: AssemblyTitle("Upscaler Lib")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[CompilerGenerated]
	[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 UpscalerLib
{
	[BepInPlugin("com.github.glarmer.UpscalerLib", "Upscaler Lib", "1.0.0")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string Guid = "com.github.glarmer.UpscalerLib";

		public const string Name = "Upscaler Lib";

		public const string Version = "1.0.0";

		internal static ManualLogSource Log { get; private set; }

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			Log.LogInfo((object)"Upscaler Lib 1.0.0 loaded.");
		}
	}
}
namespace UpscalerLib.Native
{
	public static class NvidiaNativeLoader
	{
		private const string NVUnityPluginDLL = "NVUnityPlugin.dll";

		private const string NVNGXDLSSDLL = "nvngx_dlss.dll";

		private static bool _attempted;

		private static bool _loaded;

		private static string? _nativeDirectory;

		private static string? _pluginDirectory;

		private static ManualLogSource? _fallbackLog;

		public static string NativeDirectory => _nativeDirectory ?? GetAssemblyDirectory();

		public static bool EnsureLoaded(ManualLogSource? logger = null)
		{
			if (logger == null)
			{
				logger = Plugin.Log ?? _fallbackLog ?? (_fallbackLog = Logger.CreateLogSource("Upscaler Lib"));
			}
			if (NVUnityPlugin.IsLoaded())
			{
				return true;
			}
			if (_attempted)
			{
				if (!_loaded)
				{
					return NVUnityPlugin.IsLoaded();
				}
				return true;
			}
			_attempted = true;
			_pluginDirectory = GetAssemblyDirectory();
			_nativeDirectory = GetExecutableDirectory();
			if (!StageBundledNativeDLLs(_pluginDirectory, _nativeDirectory, logger))
			{
				_nativeDirectory = _pluginDirectory;
			}
			string path = Path.Combine(_nativeDirectory, "nvngx_dlss.dll");
			string path2 = Path.Combine(_nativeDirectory, "NVUnityPlugin.dll");
			if (!File.Exists(path) || !File.Exists(path2))
			{
				logger.LogWarning((object)("DLSS native files are missing. Expected NVUnityPlugin.dll and nvngx_dlss.dll in " + _nativeDirectory));
				return false;
			}
			if (!SetDLLDirectoryW(_nativeDirectory))
			{
				logger.LogWarning((object)"Failed to add DLSS native directory to DLL search path.");
			}
			TryLoadLibrary(path, logger);
			try
			{
				_loaded = NVUnityPlugin.Load() || NVUnityPlugin.IsLoaded();
				if (!_loaded)
				{
					logger.LogWarning((object)("NVIDIA Unity plugin was found but Unity did not load it. Native directory: " + _nativeDirectory));
				}
			}
			catch (Exception ex)
			{
				logger.LogWarning((object)("NVIDIA Unity plugin load failed: " + ex.GetType().Name + ": " + ex.Message));
				_loaded = false;
			}
			if (!_loaded)
			{
				return NVUnityPlugin.IsLoaded();
			}
			return true;
		}

		private static bool StageBundledNativeDLLs(string sourceDirectory, string destinationDirectory, ManualLogSource logger)
		{
			if (string.Equals(sourceDirectory, destinationDirectory, StringComparison.OrdinalIgnoreCase))
			{
				return true;
			}
			bool flag = true;
			flag &= StageBundledNativeDLL(sourceDirectory, destinationDirectory, "NVUnityPlugin.dll", logger);
			return flag & StageBundledNativeDLL(sourceDirectory, destinationDirectory, "nvngx_dlss.dll", logger);
		}

		private static bool StageBundledNativeDLL(string sourceDirectory, string destinationDirectory, string fileName, ManualLogSource logger)
		{
			string text = Path.Combine(sourceDirectory, fileName);
			string text2 = Path.Combine(destinationDirectory, fileName);
			if (!File.Exists(text))
			{
				return false;
			}
			try
			{
				if (File.Exists(text2) && new FileInfo(text2).Length == new FileInfo(text).Length)
				{
					return true;
				}
				File.Copy(text, text2, overwrite: true);
				logger.LogInfo((object)("Staged " + fileName + " to " + destinationDirectory + " for DLSS."));
				return true;
			}
			catch (Exception ex)
			{
				logger.LogWarning((object)("Failed to stage " + fileName + " to " + destinationDirectory + ": " + ex.GetType().Name + ": " + ex.Message));
				return false;
			}
		}

		private static string GetAssemblyDirectory()
		{
			string location = typeof(NvidiaNativeLoader).Assembly.Location;
			string text = (string.IsNullOrEmpty(location) ? null : Path.GetDirectoryName(location));
			if (!string.IsNullOrEmpty(text))
			{
				return text;
			}
			return Directory.GetCurrentDirectory();
		}

		private static string GetExecutableDirectory()
		{
			try
			{
				string dataPath = Application.dataPath;
				if (!string.IsNullOrEmpty(dataPath))
				{
					string directoryName = Path.GetDirectoryName(dataPath);
					if (!string.IsNullOrEmpty(directoryName))
					{
						return directoryName;
					}
				}
			}
			catch
			{
			}
			return Directory.GetCurrentDirectory();
		}

		private static bool TryLoadLibrary(string path, ManualLogSource logger)
		{
			try
			{
				IntPtr intPtr = LoadLibraryW(path);
				if (intPtr != IntPtr.Zero)
				{
					return true;
				}
				logger.LogWarning((object)("Failed to preload " + Path.GetFileName(path) + "."));
			}
			catch (Exception ex) when (((ex is DllNotFoundException || ex is EntryPointNotFoundException) ? 1 : 0) != 0)
			{
				logger.LogWarning((object)("Windows native library loader is unavailable " + ex.GetType().Name));
			}
			catch (Exception ex2)
			{
				logger.LogWarning((object)("Failed to preload " + Path.GetFileName(path) + ": " + ex2.GetType().Name + ": " + ex2.Message));
			}
			return false;
		}

		[DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
		private static extern IntPtr LoadLibraryW(string fileName);

		[DllImport("kernel32", CharSet = CharSet.Unicode, EntryPoint = "SetDllDirectoryW", SetLastError = true)]
		private static extern bool SetDLLDirectoryW(string pathName);
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}