Please disclose if any significant portion of your mod was created using AI tools by adding the 'AI Generated' category. Failing to do so may result in the mod being removed from Thunderstore.
Decompiled source of GraphicsOverdrive v1.2.3
patchers/GraphicsOverdrive.Patcher.dll
Decompiled 2 weeks agousing System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Globalization; using System.IO; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Versioning; using System.Security.Cryptography; using System.Security.Principal; using System.Text; using System.Threading; using BepInEx; using BepInEx.Logging; using GraphicsOverdrive.Config; using GraphicsOverdrive.Tune; using Microsoft.CodeAnalysis; using Mono.Cecil; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: InternalsVisibleTo("PatcherTests")] [assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")] [assembly: AssemblyCompany("GraphicsOverdrive.Patcher")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyDescription("Client-side BepInEx preloader patcher of Graphics Overdrive: opens the native door for FSR 3 (Vulkan layers + router plugin) before Unity creates its GPU device.")] [assembly: AssemblyFileVersion("1.2.3.0")] [assembly: AssemblyInformationalVersion("1.2.3+82be26dbd217d29d0ff7d4efa8e668ca722d6538")] [assembly: AssemblyProduct("GraphicsOverdrive.Patcher")] [assembly: AssemblyTitle("GraphicsOverdrive.Patcher")] [assembly: AssemblyVersion("1.2.3.0")] [module: RefSafetyRules(11)] namespace Microsoft.CodeAnalysis { [CompilerGenerated] [Embedded] internal sealed class EmbeddedAttribute : Attribute { } } namespace System.Runtime.CompilerServices { [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 GraphicsOverdrive.Tune { public static class TuneProtocol { public const string EnvWaitPid = "GO_TUNE_WAIT_PID"; public const string EnvToken = "GO_TUNE_TOKEN"; public const string EnvWaitExe = "GO_TUNE_WAIT_EXE"; public const string EnvWaitMax = "GO_TUNE_WAIT_MAX"; public const string SidecarFile = "GraphicsOverdrive/tune-native.cfg"; public const string TimeoutFile = "GraphicsOverdrive/tune-wait-timeout.txt"; public const string OriginalEnvFile = "original-env.txt"; public const int WaitMaxSeconds = 60; public const int PollMilliseconds = 100; public const string SidecarSessionSection = "Session"; public const string SidecarTokenKey = "Token"; public const string SidecarNativeSection = "Native"; public static string NewToken() { return Guid.NewGuid().ToString("N"); } public static bool TokenMatches(string environmentToken, string fileToken) { if (!string.IsNullOrEmpty(environmentToken) && !string.IsNullOrEmpty(fileToken)) { return string.Equals(environmentToken.Trim(), fileToken.Trim(), StringComparison.Ordinal); } return false; } public static string BuildSidecar(string modVersion, string token, IEnumerable<KeyValuePair<string, string>> nativeValues) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append("## Settings file was created by plugin GraphicsOverdrive v").Append(modVersion ?? "").Append('\n'); stringBuilder.Append("## Test and Tune: the restart-only values of ONE start. A start without the restart token below ignores this file; the cfg is not changed.\n\n"); stringBuilder.Append('[').Append("Session").Append("]\n\n"); stringBuilder.Append("Token").Append(" = ").Append(token ?? "") .Append("\n\n"); stringBuilder.Append('[').Append("Native").Append("]\n\n"); if (nativeValues != null) { foreach (KeyValuePair<string, string> nativeValue in nativeValues) { stringBuilder.Append(OneLine(nativeValue.Key)).Append(" = ").Append(OneLine(nativeValue.Value)) .Append('\n'); } } return stringBuilder.ToString(); } private static string OneLine(string s) { return (s ?? "").Replace("\r", " ").Replace("\n", " "); } public static CfgSnapshot ReadSidecar(string path, string environmentToken, out string why) { why = ""; if (path == null || !File.Exists(path)) { why = "no sidecar"; return null; } CfgSnapshot cfgSnapshot = CfgSnapshot.Load(path); string value = cfgSnapshot.GetValue("Session", "Token"); if (string.IsNullOrEmpty(environmentToken)) { why = "this start has no restart token"; return null; } if (string.IsNullOrEmpty(value)) { why = "the sidecar has no token"; return null; } if (!TokenMatches(environmentToken, value)) { why = "the restart token is not the sidecar's"; return null; } return cfgSnapshot; } public static string BuildTimeoutMarker(string token, int waitedPid, int seconds) { return "token=" + token + "\npid=" + waitedPid.ToString(CultureInfo.InvariantCulture) + "\nseconds=" + seconds.ToString(CultureInfo.InvariantCulture) + "\ntime=" + DateTime.UtcNow.ToString("o", CultureInfo.InvariantCulture) + "\n"; } public static string BuildOriginalEnv(int pid, IEnumerable<KeyValuePair<string, string>> originals) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append("# Graphics Overdrive: the environment variables the Native Door patcher changed in this process, as they were before it ran (Test and Tune puts these back when it restarts the game on Windows).\n"); stringBuilder.Append("pid=").Append(pid.ToString(CultureInfo.InvariantCulture)).Append('\n'); if (originals != null) { foreach (KeyValuePair<string, string> original in originals) { if (!string.IsNullOrEmpty(original.Key) && original.Key.IndexOfAny(new char[4] { '\t', '\n', '\r', '=' }) < 0) { stringBuilder.Append(original.Key).Append('\t'); if (original.Value == null) { stringBuilder.Append("unset\n"); } else { stringBuilder.Append("set\t").Append(Convert.ToBase64String(Encoding.UTF8.GetBytes(original.Value))).Append('\n'); } } } } return stringBuilder.ToString(); } public static List<KeyValuePair<string, string>> ParseOriginalEnv(string text, int expectedPid, out string why) { why = ""; if (string.IsNullOrEmpty(text)) { why = "the file is empty"; return null; } List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>(); bool flag = false; string[] array = text.Split(new char[1] { '\n' }); for (int i = 0; i < array.Length; i++) { string text2 = array[i].TrimEnd(new char[1] { '\r' }); if (text2.Length == 0 || text2[0] == '#') { continue; } if (text2.StartsWith("pid=", StringComparison.Ordinal)) { if (!int.TryParse(text2.Substring(4).Trim(), NumberStyles.Integer, CultureInfo.InvariantCulture, out var result) || result != expectedPid) { why = "the file is from process " + text2.Substring(4).Trim() + ", not " + expectedPid.ToString(CultureInfo.InvariantCulture); return null; } flag = true; continue; } string[] array2 = text2.Split(new char[1] { '\t' }); if (array2.Length < 2 || array2[0].Length == 0) { continue; } if (array2[1] == "unset") { list.Add(new KeyValuePair<string, string>(array2[0], null)); } else if (array2[1] == "set" && array2.Length >= 3) { try { list.Add(new KeyValuePair<string, string>(array2[0], Encoding.UTF8.GetString(Convert.FromBase64String(array2[2])))); } catch (FormatException) { } } } if (!flag) { why = "the file has no process number"; return null; } return list; } public static string ParseTimeoutMarkerToken(string text) { if (string.IsNullOrEmpty(text)) { return ""; } string[] array = text.Split(new char[1] { '\n' }); for (int i = 0; i < array.Length; i++) { string text2 = array[i].Trim(); if (text2.StartsWith("token=", StringComparison.Ordinal)) { return text2.Substring(6).Trim(); } } return ""; } } public sealed class TuneProcessInfo { public bool Alive; public string Exe; } public interface ITuneProcessProbe { TuneProcessInfo Probe(int pid); } public enum TuneWaitOutcome { AlreadyGone, Gone, AnotherProgram, TimedOut } public sealed class TuneWaitResult { public TuneWaitOutcome Outcome; public double Seconds; public string Detail = ""; } public sealed class SystemProcessProbe : ITuneProcessProbe { public TuneProcessInfo Probe(int pid) { if (!TuneProcess.IsWindows) { return ProbeLinux(pid); } return ProbeWindows(pid); } private static TuneProcessInfo ProbeLinux(int pid) { string text = "/proc/" + pid.ToString(CultureInfo.InvariantCulture); string text2; try { text2 = TuneProcess.ReadSmallFile(text + "/stat"); } catch (FileNotFoundException) { return new TuneProcessInfo { Alive = false }; } catch (DirectoryNotFoundException) { return new TuneProcessInfo { Alive = false }; } catch (Exception) { return new TuneProcessInfo { Alive = Directory.Exists(text) }; } int num = text2.LastIndexOf(')'); char c = ((num >= 0 && num + 2 < text2.Length) ? text2[num + 2] : '?'); if (c == 'Z' || c == 'X' || c == 'x') { return new TuneProcessInfo { Alive = false }; } return new TuneProcessInfo { Alive = true, Exe = TuneProcess.Readlink(text + "/exe") }; } private static TuneProcessInfo ProbeWindows(int pid) { Process process = null; try { process = Process.GetProcessById(pid); if (process.HasExited) { return new TuneProcessInfo { Alive = false }; } string exe = null; try { exe = process.MainModule.FileName; } catch (Exception) { } return new TuneProcessInfo { Alive = true, Exe = exe }; } catch (ArgumentException) { return new TuneProcessInfo { Alive = false }; } catch (Exception) { return new TuneProcessInfo { Alive = true }; } finally { process?.Dispose(); } } } public static class TuneProcess { public static bool IsWindows => Environment.OSVersion.Platform == PlatformID.Win32NT; public static int OwnPid { get { using Process process = Process.GetCurrentProcess(); return process.Id; } } public static string OwnExe() { try { if (!IsWindows) { return Readlink("/proc/self/exe"); } using Process process = Process.GetCurrentProcess(); return process.MainModule.FileName; } catch (Exception) { return null; } } [DllImport("libc", EntryPoint = "readlink", SetLastError = true)] private static extern IntPtr c_readlink(string path, byte[] buffer, UIntPtr size); public static string Readlink(string path) { byte[] array = ReadlinkBytes(path); if (array != null) { return Encoding.UTF8.GetString(array); } return null; } public static byte[] ReadlinkBytes(string path) { try { byte[] array = new byte[4096]; long num = (long)c_readlink(path, array, (UIntPtr)(uint)array.Length); if (num <= 0) { return null; } byte[] array2 = new byte[num]; Array.Copy(array, array2, (int)num); return array2; } catch (Exception) { return null; } } [DllImport("kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "GetCommandLineW")] private static extern IntPtr c_GetCommandLineW(); public static string WindowsCommandLine() { try { IntPtr intPtr = c_GetCommandLineW(); if (intPtr != IntPtr.Zero) { return Marshal.PtrToStringUni(intPtr); } } catch (Exception) { } return Environment.CommandLine; } public static string ReadSmallFile(string path) { return Encoding.UTF8.GetString(ReadAllBytesToEnd(path)); } public static byte[] ReadAllBytesToEnd(string path) { using FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 4096, useAsync: false); using MemoryStream memoryStream = new MemoryStream(); byte[] array = new byte[4096]; int count; while ((count = fileStream.Read(array, 0, array.Length)) > 0) { memoryStream.Write(array, 0, count); } return memoryStream.ToArray(); } public static string StripDeleted(string exe) { if (exe == null || !exe.EndsWith(" (deleted)", StringComparison.Ordinal)) { return exe; } return exe.Substring(0, exe.Length - " (deleted)".Length); } public static bool SameProgram(string a, string b, bool windows) { if (string.IsNullOrEmpty(a) || string.IsNullOrEmpty(b)) { return true; } a = StripDeleted(a); b = StripDeleted(b); if (windows) { return string.Equals(a.Replace('/', '\\'), b.Replace('/', '\\'), StringComparison.OrdinalIgnoreCase); } return string.Equals(a, b, StringComparison.Ordinal); } public static TuneWaitResult WaitForExit(int pid, string expectedExe, TimeSpan max, TimeSpan poll, Action<string> note) { Stopwatch watch = Stopwatch.StartNew(); return WaitForExit(pid, expectedExe, max, poll, new SystemProcessProbe(), () => watch.Elapsed, Thread.Sleep, note, IsWindows); } public static TuneWaitResult WaitForExit(int pid, string expectedExe, TimeSpan max, TimeSpan poll, ITuneProcessProbe probe, Func<TimeSpan> elapsed, Action<TimeSpan> sleep, Action<string> note, bool windows) { TuneProcessInfo tuneProcessInfo = probe.Probe(pid); if (!tuneProcessInfo.Alive) { return new TuneWaitResult { Outcome = TuneWaitOutcome.AlreadyGone, Seconds = elapsed().TotalSeconds, Detail = "process " + pid + " is not running" }; } TuneWaitResult tuneWaitResult; if (!SameProgram(tuneProcessInfo.Exe, expectedExe, windows)) { tuneWaitResult = new TuneWaitResult(); tuneWaitResult.Outcome = TuneWaitOutcome.AnotherProgram; tuneWaitResult.Seconds = elapsed().TotalSeconds; tuneWaitResult.Detail = "process " + pid + " runs " + tuneProcessInfo.Exe + ", not " + expectedExe + " (the number was reused)"; return tuneWaitResult; } note?.Invoke("waiting for process " + pid + ((tuneProcessInfo.Exe != null) ? (" (" + tuneProcessInfo.Exe + ")") : "") + " to exit, up to " + Math.Round(max.TotalSeconds) + " s"); TimeSpan timeSpan; do { sleep(poll); tuneProcessInfo = probe.Probe(pid); timeSpan = elapsed(); if (!tuneProcessInfo.Alive) { return new TuneWaitResult { Outcome = TuneWaitOutcome.Gone, Seconds = timeSpan.TotalSeconds, Detail = "process " + pid + " exited" }; } if (!SameProgram(tuneProcessInfo.Exe, expectedExe, windows)) { tuneWaitResult = new TuneWaitResult(); tuneWaitResult.Outcome = TuneWaitOutcome.Gone; tuneWaitResult.Seconds = timeSpan.TotalSeconds; tuneWaitResult.Detail = "process " + pid + " is gone (its number belongs to " + tuneProcessInfo.Exe + " now)"; return tuneWaitResult; } } while (!(timeSpan >= max)); tuneWaitResult = new TuneWaitResult(); tuneWaitResult.Outcome = TuneWaitOutcome.TimedOut; tuneWaitResult.Seconds = timeSpan.TotalSeconds; tuneWaitResult.Detail = "process " + pid + " was still running after " + Math.Round(timeSpan.TotalSeconds) + " s"; return tuneWaitResult; } } } namespace GraphicsOverdrive.Config { public sealed class CfgSnapshot { public sealed class Entry { public string Section; public string Key; public string Value; public string RecordedDefault; public string TypeName; public int Line; } private const string HeaderPrefix = "## Settings file was created by plugin "; private const string DefaultPrefix = "# Default value:"; private const string TypePrefix = "# Setting type:"; private readonly List<Entry> m_entries = new List<Entry>(); private readonly Dictionary<string, Entry> m_index = new Dictionary<string, Entry>(StringComparer.Ordinal); public string PluginName { get; private set; } public string PluginVersion { get; private set; } public IReadOnlyList<Entry> Entries => m_entries; public bool IsEmpty => m_entries.Count == 0; public string RawText { get; private set; } = ""; public bool HasRecordedDefaults { get; private set; } public static CfgSnapshot Load(string path) { try { if (!string.IsNullOrEmpty(path) && File.Exists(path)) { return Parse(File.ReadAllText(path)); } } catch (Exception) { } return new CfgSnapshot(); } public static CfgSnapshot Parse(string text) { CfgSnapshot cfgSnapshot = new CfgSnapshot(); if (string.IsNullOrEmpty(text)) { return cfgSnapshot; } if (text[0] == '\ufeff') { text = text.Substring(1); } cfgSnapshot.RawText = text; string section = ""; string text2 = null; string typeName = null; string[] array = text.Replace("\r\n", "\n").Replace('\r', '\n').Split(new char[1] { '\n' }); for (int i = 0; i < array.Length; i++) { string text3 = array[i].Trim(); if (text3.Length == 0) { text2 = (typeName = null); continue; } if (text3.StartsWith("#", StringComparison.Ordinal)) { if (text3.StartsWith("## Settings file was created by plugin ", StringComparison.Ordinal)) { cfgSnapshot.ReadHeader(text3.Substring("## Settings file was created by plugin ".Length).Trim()); } else if (text3.StartsWith("# Default value:", StringComparison.Ordinal)) { text2 = text3.Substring("# Default value:".Length).Trim(); } else if (text3.StartsWith("# Setting type:", StringComparison.Ordinal)) { typeName = text3.Substring("# Setting type:".Length).Trim(); } continue; } if (text3.StartsWith("[", StringComparison.Ordinal) && text3.EndsWith("]", StringComparison.Ordinal)) { section = text3.Substring(1, text3.Length - 2); text2 = (typeName = null); continue; } int num = text3.IndexOf('='); if (num >= 0) { string key = text3.Substring(0, num).Trim(); string value = text3.Substring(num + 1).Trim(); string key2 = Id(section, key); if (!cfgSnapshot.m_index.TryGetValue(key2, out var value2)) { value2 = new Entry { Section = section, Key = key }; cfgSnapshot.m_index[key2] = value2; cfgSnapshot.m_entries.Add(value2); } value2.Value = value; value2.RecordedDefault = text2; value2.TypeName = typeName; value2.Line = i + 1; if (text2 != null) { cfgSnapshot.HasRecordedDefaults = true; } text2 = (typeName = null); } } return cfgSnapshot; } public bool TryGet(string section, string key, out Entry entry) { return m_index.TryGetValue(Id(section, key), out entry); } public string GetValue(string section, string key) { if (!TryGet(section, key, out var entry)) { return null; } return entry.Value; } public static int CompareVersions(string a, string b) { int[] array = ParseVersion(a); int[] array2 = ParseVersion(b); if (array == null || array2 == null) { if (array != null) { return 1; } if (array2 != null) { return -1; } return 0; } int num = Math.Max(array.Length, array2.Length); for (int i = 0; i < num; i++) { int num2 = ((i < array.Length) ? array[i] : 0); int num3 = ((i < array2.Length) ? array2[i] : 0); if (num2 != num3) { if (num2 >= num3) { return 1; } return -1; } } return 0; } public static bool IsVersion(string v) { return ParseVersion(v) != null; } private static int[] ParseVersion(string v) { if (string.IsNullOrEmpty(v)) { return null; } v = v.Trim(); if (v.StartsWith("v", StringComparison.OrdinalIgnoreCase)) { v = v.Substring(1); } string[] array = v.Split(new char[1] { '.' }); int[] array2 = new int[array.Length]; for (int i = 0; i < array.Length; i++) { int j; for (j = 0; j < array[i].Length && char.IsDigit(array[i][j]); j++) { } if (j == 0) { if (i != 0) { return Trim(array2, i); } return null; } array2[i] = int.Parse(array[i].Substring(0, Math.Min(j, 9))); if (j < array[i].Length) { return Trim(array2, i + 1); } } return array2; } private static int[] Trim(int[] a, int length) { int[] array = new int[length]; Array.Copy(a, array, length); return array; } private void ReadHeader(string rest) { int num = rest.LastIndexOf(' '); if (num <= 0) { PluginName = rest; return; } string text = rest.Substring(num + 1); if (text.StartsWith("v", StringComparison.OrdinalIgnoreCase)) { text = text.Substring(1); } if (text.Length > 0 && char.IsDigit(text[0])) { PluginName = rest.Substring(0, num).Trim(); PluginVersion = text; } else { PluginName = rest; } } private static string Id(string section, string key) { return section + "\u0001" + key; } } public static class MigrationRule { public enum ValueKind { Text, Boolean, Integer, Real, EnumName } public enum Outcome { Keep, TakeNew, NoBase } public static ValueKind KindOf(string typeName) { switch (typeName) { default: switch (typeName) { case "Vector2": case "Vector3": case "Vector4": case "String": case "Color": case "Quaternion": case "": break; default: return ValueKind.EnumName; } break; case "Boolean": return ValueKind.Boolean; case "Int64": case "SByte": case "Int16": case "Int32": case "UInt64": case "UInt16": case "UInt32": case "Byte": return ValueKind.Integer; case "Decimal": case "Single": case "Double": return ValueKind.Real; case null: break; } return ValueKind.Text; } public static ValueKind KindOf(Type type) { if (type == null) { return ValueKind.Text; } if (!type.IsEnum) { return KindOf(type.Name); } return ValueKind.EnumName; } public static bool SameValue(string a, string b, ValueKind kind) { if (a == null || b == null) { if (a == null) { return b == null; } return false; } a = a.Trim(); b = b.Trim(); switch (kind) { case ValueKind.Boolean: case ValueKind.EnumName: return string.Equals(a, b, StringComparison.OrdinalIgnoreCase); case ValueKind.Integer: { if (long.TryParse(a, NumberStyles.Integer, CultureInfo.InvariantCulture, out var result3) && long.TryParse(b, NumberStyles.Integer, CultureInfo.InvariantCulture, out var result4)) { return result3 == result4; } return string.Equals(a, b, StringComparison.Ordinal); } case ValueKind.Real: { if (double.TryParse(a, NumberStyles.Float, CultureInfo.InvariantCulture, out var result) && double.TryParse(b, NumberStyles.Float, CultureInfo.InvariantCulture, out var result2)) { return Math.Abs(result - result2) <= Math.Max(1E-06, 1E-05 * Math.Max(Math.Abs(result), Math.Abs(result2))); } return string.Equals(a, b, StringComparison.Ordinal); } default: return string.Equals(a, b, StringComparison.Ordinal); } } public static Outcome Decide(string mine, string baseValue, string newValue, ValueKind kind) { if (baseValue == null) { return Outcome.NoBase; } if (mine == null || !SameValue(mine, baseValue, kind) || SameValue(newValue, baseValue, kind)) { return Outcome.Keep; } return Outcome.TakeNew; } public static string BaseFor(string section, string key, CfgSnapshot ledger, CfgSnapshot cfg) { bool flag = ledger != null && cfg != null && cfg.PluginVersion != null && ledger.PluginVersion != null && CfgSnapshot.CompareVersions(cfg.PluginVersion, ledger.PluginVersion) < 0; if (ledger != null && !flag && ledger.TryGet(section, key, out var entry)) { return entry.Value; } if (cfg != null && cfg.TryGet(section, key, out entry) && entry.RecordedDefault != null) { return entry.RecordedDefault; } string version = ((cfg != null && cfg.PluginVersion != null) ? cfg.PluginVersion : ledger?.PluginVersion); return DefaultHistory.DefaultShippedBy(section, key, version); } } public static class DefaultHistory { public sealed class Row { public readonly string Section; public readonly string Key; public readonly string UntilVersion; public readonly string OldDefault; public readonly string ChangedIn; public Row(string section, string key, string untilVersion, string oldDefault, string changedIn = null) { Section = section; Key = key; UntilVersion = untilVersion; OldDefault = oldDefault; ChangedIn = changedIn; } } public const string ShippedDefaultsFile = "GraphicsOverdrive/shipped-defaults.cfg"; public static readonly Row[] Rows = new Row[2] { new Row("Native", "RayTracing", "1.1.0", "false", "1.1.1"), new Row("6 - Resolution & AA", "TexturePackFit", "1.1.4", "Auto", "1.2.0") }; public static string DefaultShippedBy(string section, string key, string version) { return DefaultShippedBy(Rows, section, key, version); } public static string ChangedInFor(string section, string key, string oldDefault) { return ChangedInFor(Rows, section, key, oldDefault); } public static string ChangedInFor(IEnumerable<Row> rows, string section, string key, string oldDefault) { if (oldDefault == null) { return null; } Row row = null; foreach (Row row2 in rows) { if (!(row2.Section != section) && !(row2.Key != key) && !string.IsNullOrEmpty(row2.ChangedIn) && CfgSnapshot.IsVersion(row2.ChangedIn) && string.Equals(row2.OldDefault, oldDefault, StringComparison.OrdinalIgnoreCase) && (row == null || CfgSnapshot.CompareVersions(row2.ChangedIn, row.ChangedIn) > 0)) { row = row2; } } return row?.ChangedIn; } public static string DefaultShippedBy(IEnumerable<Row> rows, string section, string key, string version) { if (string.IsNullOrEmpty(version)) { return null; } Row row = null; foreach (Row row2 in rows) { if (!(row2.Section != section) && !(row2.Key != key) && CfgSnapshot.CompareVersions(version, row2.UntilVersion) <= 0 && (row == null || CfgSnapshot.CompareVersions(row2.UntilVersion, row.UntilVersion) < 0)) { row = row2; } } return row?.OldDefault; } } } namespace GraphicsOverdrive.Patcher { public static class NativeDoor { internal const string LayerName = "VK_LAYER_WUBARRK_graphics_overdrive"; private const string LibBase = "VkLayer_wubarrk_graphicsoverdrive"; private const string PluginGuid = "com.wubarrk.graphicsoverdrive"; private const int StrikeLimit = 2; private const string StrikesSince = "1.1.1"; private const int StageFull = 0; private const int StageReduced = 1; private const string RayTracingExtensions = "VK_KHR_deferred_host_operations,VK_EXT_descriptor_indexing,VK_KHR_buffer_device_address,VK_KHR_shader_float_controls,VK_KHR_spirv_1_4,VK_KHR_acceleration_structure,VK_KHR_ray_query,VK_KHR_ray_tracing_pipeline"; private const string RayTracingFeatures = "bufferDeviceAddress,accelerationStructure,rayQuery,rayTracingPipeline"; private static ManualLogSource log; private static readonly StringBuilder pendingFile = new StringBuilder(); private static string workDir; private static NativeSettings settings; internal const string LayerDescription = "Graphics Overdrive native layer (client-side)"; private static readonly List<KeyValuePair<string, string>> originalEnv = new List<KeyValuePair<string, string>>(); public static IEnumerable<string> TargetDLLs => new string[0]; public static void Patch(AssemblyDefinition assembly) { } public static void Initialize() { try { if (!WaitForRestartingProcess()) { FlushFile(); QuitQuietly(); } else { Run(); } } catch (Exception ex) { Note("Native door failed and was ignored, the game starts normally: " + ex); } finally { WriteOriginalEnv(); FlushFile(); } } private static void WriteOriginalEnv() { try { if (workDir == null) { return; } bool flag = false; foreach (KeyValuePair<string, string> item in originalEnv) { if (item.Key != "GO_NATIVE_STATUS") { flag = true; break; } } if (flag) { File.WriteAllText(Path.Combine(workDir, "original-env.txt"), TuneProtocol.BuildOriginalEnv(TuneProcess.OwnPid, originalEnv)); } } catch (Exception) { } } private static bool WaitForRestartingProcess() { string environmentVariable = Environment.GetEnvironmentVariable("GO_TUNE_WAIT_PID"); if (string.IsNullOrEmpty(environmentVariable)) { return true; } if (log == null) { log = Logger.CreateLogSource("GraphicsOverdrive.Native"); } if (workDir == null) { workDir = Path.Combine(Path.Combine(Paths.CachePath, "GraphicsOverdrive"), "vulkan"); Directory.CreateDirectory(workDir); } if (!int.TryParse(environmentVariable.Trim(), out var result) || result <= 0 || result == TuneProcess.OwnPid) { Note("Test and Tune restart: GO_TUNE_WAIT_PID='" + environmentVariable + "' is not another process, nothing to wait for"); return true; } _ = Environment.OSVersion.Platform; string text = Environment.GetEnvironmentVariable("GO_TUNE_WAIT_EXE"); if (string.IsNullOrEmpty(text)) { text = TuneProcess.OwnExe(); } int num = 60; if (int.TryParse(Environment.GetEnvironmentVariable("GO_TUNE_WAIT_MAX"), out var result2) && result2 > 0) { num = result2; } TuneWaitResult tuneWaitResult = TuneProcess.WaitForExit(result, text, TimeSpan.FromSeconds(num), TimeSpan.FromMilliseconds(100.0), delegate(string m) { Note("Test and Tune restart: " + m); }); switch (tuneWaitResult.Outcome) { case TuneWaitOutcome.AlreadyGone: Note("Test and Tune restart: the old game process (" + result + ") is already gone, starting at once"); return true; case TuneWaitOutcome.Gone: Note("Test and Tune restart: the old game process (" + result + ") exited after " + tuneWaitResult.Seconds.ToString("0.0") + " s, starting now"); return true; case TuneWaitOutcome.AnotherProgram: Note("Test and Tune restart: process " + result + " is another program than the game (" + tuneWaitResult.Detail + "): not waited on, starting now"); return true; default: Note("Test and Tune restart: the old game process (" + result + ") was still running after " + num + " s. This start does not run beside it: it quits now. Start Valheim again once the old one is closed; Test and Tune starts by itself."); try { string path = Path.Combine(Paths.ConfigPath, "GraphicsOverdrive/tune-wait-timeout.txt".Replace('/', Path.DirectorySeparatorChar)); Directory.CreateDirectory(Path.GetDirectoryName(path)); File.WriteAllText(path, TuneProtocol.BuildTimeoutMarker(Environment.GetEnvironmentVariable("GO_TUNE_TOKEN"), result, num)); } catch (Exception ex) { Note("Test and Tune restart: could not write the timeout marker: " + ex.Message); } return false; } } private static void QuitQuietly() { try { Environment.Exit(0); } catch (Exception) { } } private static void Run() { if (log == null) { log = Logger.CreateLogSource("GraphicsOverdrive.Native"); } bool flag = Environment.OSVersion.Platform == PlatformID.Win32NT; if (flag) { WindowsLoaderCheck.CaptureEnvironment(); } bool flag2 = !flag && File.Exists("/System/Library/CoreServices/SystemVersion.plist"); string text = (flag ? "windows-x64" : (flag2 ? "macos" : "linux-x64")); string path = (flag ? "VkLayer_wubarrk_graphicsoverdrive.dll" : "VkLayer_wubarrk_graphicsoverdrive.so"); string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string text2 = Path.Combine(Path.Combine(Path.Combine(directoryName, "native"), text), path); if (!File.Exists(text2)) { log.LogInfo((object)("Native door not available: this package has no native files for " + text + " (" + text2 + "). FSR 3 stays off; the mod works as usual.")); return; } workDir = Path.Combine(Path.Combine(Paths.CachePath, "GraphicsOverdrive"), "vulkan"); Directory.CreateDirectory(workDir); settings = NativeSettings.Load(Path.Combine(Paths.ConfigPath, "com.wubarrk.graphicsoverdrive.cfg"), Path.Combine(Paths.ConfigPath, "GraphicsOverdrive/shipped-defaults.cfg".Replace('/', Path.DirectorySeparatorChar)), Note, Path.Combine(Paths.ConfigPath, "GraphicsOverdrive/tune-native.cfg".Replace('/', Path.DirectorySeparatorChar)), Environment.GetEnvironmentVariable("GO_TUNE_TOKEN")); string text3 = NativeFrameGen.Choice(settings.Text(NativeSettings.FrameGeneration)); bool flag3 = !(Paths.ProcessName ?? "").ToLowerInvariant().Contains("server"); string text4 = Path.Combine(Paths.ConfigPath, "GraphicsOverdrive/smooth-motion.txt".Replace('/', Path.DirectorySeparatorChar)); if (text3 == "NvidiaSmoothMotion" && flag3) { NativeFrameGen.SmoothMotion(text4, Environment.OSVersion.Platform == PlatformID.Win32NT, Environment.GetCommandLineArgs(), Note, SetEnv); } else if (flag3) { NativeFrameGen.ClearGuard(text4); } if (!ShouldActivate(out var reason, out var switchedOff)) { NotActive(reason, switchedOff ? "off" : "not active"); if (switchedOff) { NativeRoute.Remove(Note); if (flag) { WindowsLoaderCheck.RemoveRegistryRoute(Note, workDir); } } return; } string text5 = NativeFiles.ReadSha256File(text2 + ".sha256"); if (text5 != null) { string b = NativeFiles.Sha256Hex(text2); if (!string.Equals(text5, b, StringComparison.OrdinalIgnoreCase)) { NotActive("payload hash mismatch", "not active"); return; } } string state = Path.Combine(workDir, "state.txt"); string strikesFile = Path.Combine(workDir, "strikes.txt"); if (!ChooseStage(state, strikesFile, out var stage, out var note, out reason, out var _)) { NotActive(reason, "not active"); return; } if (stage == 0) { SetEnv("GO_NATIVE_STATUS", "active"); } else { SetEnv("GO_NATIVE_STATUS", "active (reduced start: " + note + ")"); SetEnv("GO_NATIVE_STAGE_NOTE", note); Note("Reduced start: " + note); } SetEnv("GO_NATIVE_STAGE", (stage == 0) ? "full" : "reduced"); SetEnv("GO_NATIVE_DIR", workDir); SafeDelete(Path.Combine(workDir, "hardware-scan.txt")); bool flag4 = settings.Bool(NativeSettings.RayTracing); bool flag5 = settings.Bool(NativeSettings.FrameGenPreload); string text6 = Path.Combine(workDir, "VkLayer_wubarrk_graphicsoverdrive.json"); string text7 = Path.Combine(workDir, "layer.cfg"); File.WriteAllText(text6, BuildManifest(text2)); File.WriteAllText(text7, BuildLayerConfig(state, stage == 0 && flag4, stage)); char sep = (flag ? ';' : ':'); SetEnv("VK_ADD_LAYER_PATH", Prepend(Environment.GetEnvironmentVariable("VK_ADD_LAYER_PATH"), workDir, sep)); SetEnv("VK_LOADER_LAYERS_ENABLE", Append(Environment.GetEnvironmentVariable("VK_LOADER_LAYERS_ENABLE"), "VK_LAYER_WUBARRK_graphics_overdrive", ',')); SetEnv("VK_INSTANCE_LAYERS", Append(Environment.GetEnvironmentVariable("VK_INSTANCE_LAYERS"), "VK_LAYER_WUBARRK_graphics_overdrive", sep)); SetEnv("GO_LAYER_CONFIG", text7); SetEnv("GO_LAYER_LIB", text2); Note((flag ? "Native Vulkan layer requested through the environment (the Windows loader check below decides): " : "Native Vulkan layer activated: ") + "VK_LAYER_WUBARRK_graphics_overdrive from " + text2 + " (manifest " + text6 + ")"); NativeRoute.Install(directoryName, text, workDir, Note, SetEnv); if (text3 == "FSR3" || text3 == "DLSS") { NativeFrameGen.ActivatePresenter(text3, directoryName, text, workDir, Note, SetEnv, (string a, string value) => Prepend(a, value, sep), Append); } else if (text3 == "Off" && flag5 && stage == 0) { NativeFrameGen.ActivatePresenter("Off", directoryName, text, workDir, Note, SetEnv, (string a, string value) => Prepend(a, value, sep), Append); } else if (text3 == "Off" && flag5) { Note("Frame generation presenter not preloaded: reduced start (frame generation is switched on with a restart this session)"); } if (flag) { string text8 = WindowsLoaderCheck.Run(workDir, text2, Note, SetEnv); if (text8 != null) { NotActive(text8, "not active"); WindowsLoaderCheck.UnsetDoorVariables(SetEnv); } } } private static void NotActive(string reason, string kind) { Note("Native Vulkan layer not activated: " + reason); SetEnv("GO_NATIVE_STATUS", kind + ": " + reason); } private static bool ShouldActivate(out string reason, out bool switchedOff) { switchedOff = false; string environmentVariable = Environment.GetEnvironmentVariable("GO_NATIVE"); if (environmentVariable == "0") { reason = "GO_NATIVE=0"; return false; } string[] commandLineArgs = Environment.GetCommandLineArgs(); string[] array = commandLineArgs; for (int i = 0; i < array.Length; i++) { if (array[i].ToLowerInvariant() == "-nographics") { reason = "headless process (-nographics)"; return false; } } string text = Paths.ProcessName ?? ""; if (text.ToLowerInvariant().Contains("server")) { reason = "dedicated server process (" + text + ")"; return false; } if (IntPtr.Size != 8) { reason = "32-bit process"; return false; } if (environmentVariable != "1" && !settings.Bool(NativeSettings.Enabled)) { reason = "switched off in the config ([Native] Enabled = false)"; switchedOff = true; return false; } bool flag = false; array = commandLineArgs; foreach (string text2 in array) { string text3 = text2.ToLowerInvariant(); if (text3 == "-force-vulkan") { flag = true; } switch (text3) { case "-force-d3d11": case "-force-d3d12": case "-force-glcore": case "-force-opengl": case "-force-gles": case "-force-metal": reason = "the game was launched with " + text2 + " (native mode is Vulkan only)"; return false; } } if (Environment.OSVersion.Platform == PlatformID.Win32NT && !flag) { reason = "on Windows the game runs Direct3D 11 unless it is started with -force-vulkan: add -force-vulkan to Valheim's launch options"; return false; } reason = null; return true; } private static bool ChooseStage(string state, string strikesFile, out int stage, out string note, out string reason, out bool firstStartOfVersion) { reason = null; note = null; stage = 0; string text = Path.Combine(Path.GetDirectoryName(strikesFile), "stage.txt"); if (Environment.GetEnvironmentVariable("GO_NATIVE_RESET") == "1") { SafeDelete(state); SafeDelete(strikesFile); SafeDelete(text); } string path = Path.Combine(Path.GetDirectoryName(strikesFile), "strikes-since.txt"); string text2 = null; try { if (File.Exists(path)) { text2 = File.ReadAllText(path).Trim(); } } catch { } firstStartOfVersion = text2 != "1.1.1"; if (firstStartOfVersion) { int result = 0; try { if (File.Exists(strikesFile)) { int.TryParse(File.ReadAllText(strikesFile).Trim(), out result); } } catch { } if (result > 0 || ReadStateValue(state) == "safe_mode" || File.Exists(text)) { Note("Safe-mode count from an older version cleared (" + result + " strike(s)): version 1.1.1 starts from a clean slate (it loads more by default and falls back in stages)"); } SafeDelete(state); SafeDelete(strikesFile); SafeDelete(text); try { File.WriteAllText(path, "1.1.1\n"); } catch { } } int result2 = 0; try { if (File.Exists(strikesFile)) { int.TryParse(File.ReadAllText(strikesFile).Trim(), out result2); } } catch { } int result3 = 0; bool flag = false; try { if (File.Exists(text)) { string[] array = File.ReadAllLines(text); foreach (string text3 in array) { if (text3.StartsWith("stage=")) { int.TryParse(text3.Substring(6).Trim(), out result3); } else if (text3.StartsWith("sticky=")) { flag = text3.Substring(7).Trim() == "1"; } } } } catch { } string text4 = ReadStateValue(state); bool flag2 = text4 == "instance_created" || text4 == "device_created" || text4 == "swapchain_requested"; if (text4 == "ok" || text4 == "clean_exit") { result2 = 0; } else if (flag2) { result2++; } try { File.WriteAllText(strikesFile, result2.ToString()); } catch { } if (result2 >= 2) { try { File.WriteAllText(state, "state=safe_mode\n"); } catch { } reason = "safe mode: " + result2 + " native starts in a row never reached a first frame. Delete " + strikesFile + " or start once with GO_NATIVE_RESET=1 to try again"; return false; } string text5 = ((text4 == "instance_created") ? "while the graphics device was being created" : ((text4 == "swapchain_requested") ? "while the game's swapchain was being created" : "after the graphics device was created (or it was closed while the game loaded its plugins)")); if (flag2) { stage = 1; bool flag3 = text4 != "device_created"; if (result3 == 0 && flag3) { flag = true; } note = "the last start never reached its first frame (it stopped " + text5 + "), so this start leaves out the ray tracing set and the frame generation preload. " + (flag ? ("Delete " + text + " or start once with GO_NATIVE_RESET=1 to try the full set again") : "If this start runs, the next one tries the full set again"); } else if (flag) { stage = 1; note = "an earlier start stopped while the graphics device or the swapchain was being created, so ray tracing set and frame generation preload stay left out. Delete " + text + " or start once with GO_NATIVE_RESET=1 to try the full set again"; } try { File.WriteAllText(text, "stage=" + stage + "\nsticky=" + (flag ? "1" : "0") + "\n"); } catch { } SafeDelete(state); return true; } private static string BuildManifest(string payload) { return ManifestJson("VK_LAYER_WUBARRK_graphics_overdrive", payload, "Graphics Overdrive native layer (client-side)", implicitLayer: false); } internal static string ManifestJson(string name, string libraryPath, string description, bool implicitLayer) { string text = libraryPath.Replace("\\", "\\\\").Replace("\"", "\\\""); return "{\n \"file_format_version\": \"1.2.0\",\n \"layer\": {\n \"name\": \"" + name + "\",\n \"type\": \"GLOBAL\",\n \"library_path\": \"" + text + "\",\n \"api_version\": \"1.3.0\",\n \"implementation_version\": \"1\",\n \"description\": \"" + description + "\",\n \"functions\": { \"vkNegotiateLoaderLayerInterfaceVersion\": \"vkNegotiateLoaderLayerInterfaceVersion\" }" + (implicitLayer ? ",\n \"disable_environment\": { \"GO_NATIVE_DOOR_OFF\": \"1\" },\n \"enable_environment\": { \"GO_NATIVE_DOOR\": \"1\" }\n" : "\n") + " }\n}\n"; } private static string BuildLayerConfig(string state, bool withRt, int stage) { string value = settings.Text(NativeSettings.DeviceExtensions); string value2 = settings.Text(NativeSettings.Features); bool flag = settings.Bool(NativeSettings.Inject); string value3 = settings.Text(NativeSettings.LayerLogLevel); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append("log=").Append(Path.Combine(workDir, "layer.log")).Append('\n'); stringBuilder.Append("state_file=").Append(state).Append('\n'); stringBuilder.Append("scan_file=").Append(Path.Combine(workDir, "hardware-scan.txt")).Append('\n'); stringBuilder.Append("stage=").Append((stage == 0) ? "full" : "reduced").Append('\n'); stringBuilder.Append("inject=").Append(flag ? "1" : "0").Append('\n'); stringBuilder.Append("log_level=").Append(value3).Append('\n'); stringBuilder.Append("device_extensions=").Append(value).Append('\n'); stringBuilder.Append("features=").Append(value2).Append('\n'); if (withRt) { stringBuilder.Append("rt_device_extensions=").Append("VK_KHR_deferred_host_operations,VK_EXT_descriptor_indexing,VK_KHR_buffer_device_address,VK_KHR_shader_float_controls,VK_KHR_spirv_1_4,VK_KHR_acceleration_structure,VK_KHR_ray_query,VK_KHR_ray_tracing_pipeline").Append('\n'); stringBuilder.Append("rt_features=").Append("bufferDeviceAddress,accelerationStructure,rayQuery,rayTracingPipeline").Append('\n'); Note("Ray tracing set requested ([Native] RayTracing): the layer adds the hardware ray tracing extensions the GPU offers, and drops them again if the driver refuses the device with them"); } return stringBuilder.ToString(); } internal static string Prepend(string existing, string value, char sep) { if (!string.IsNullOrEmpty(existing)) { return value + sep + existing; } return value; } internal static string Append(string existing, string value, char sep) { if (string.IsNullOrEmpty(existing)) { return value; } string[] array = existing.Split(new char[1] { sep }); for (int i = 0; i < array.Length; i++) { if (array[i] == value) { return existing; } } return existing + sep + value; } [DllImport("libc", EntryPoint = "setenv")] private static extern int c_setenv(string name, string value, int overwrite); private static void SetEnv(string name, string value) { bool flag = false; foreach (KeyValuePair<string, string> item in originalEnv) { if (item.Key == name) { flag = true; break; } } if (!flag) { originalEnv.Add(new KeyValuePair<string, string>(name, Environment.GetEnvironmentVariable(name))); } Environment.SetEnvironmentVariable(name, value); if (Environment.OSVersion.Platform != PlatformID.Win32NT) { try { c_setenv(name, value, 1); } catch (Exception ex) { Note("setenv(" + name + ") through libc failed: " + ex.Message); } } } private static string ReadStateValue(string state) { try { if (!File.Exists(state)) { return null; } string[] array = File.ReadAllLines(state); foreach (string text in array) { if (text.StartsWith("state=")) { return text.Substring(6).Trim(); } } } catch { } return null; } private static void SafeDelete(string path) { try { if (File.Exists(path)) { File.Delete(path); } } catch { } } private static void Note(string message) { if (log != null) { log.LogInfo((object)message); } pendingFile.Append(DateTime.UtcNow.ToString("HH:mm:ss.fff")).Append(' ').Append(message) .Append('\n'); } private static void FlushFile() { try { if (workDir != null) { File.AppendAllText(Path.Combine(workDir, "patcher.log"), pendingFile.ToString()); } } catch { } } } internal static class NativeFiles { internal static string ReadSha256File(string path) { try { return File.Exists(path) ? File.ReadAllText(path).Trim().Split(new char[1] { ' ' })[0] : null; } catch { return null; } } internal static string Sha256Hex(string path) { using SHA256 sHA = SHA256.Create(); using FileStream inputStream = File.OpenRead(path); byte[] array = sHA.ComputeHash(inputStream); StringBuilder stringBuilder = new StringBuilder(); byte[] array2 = array; foreach (byte b in array2) { stringBuilder.Append(b.ToString("x2")); } return stringBuilder.ToString(); } } internal static class NativeFrameGen { internal const string LayerName = "VK_LAYER_WUBARRK_frame_generation"; private const string LibBase = "VkLayer_wubarrk_frame_generation"; internal const string LayerDescription = "Graphics Overdrive frame generation presenter (client-side)"; internal const string GuardFile = "GraphicsOverdrive/smooth-motion.txt"; internal static string Choice(string value) { string a = (value ?? "Off").Trim(); if (string.Equals(a, "NvidiaSmoothMotion", StringComparison.OrdinalIgnoreCase)) { return "NvidiaSmoothMotion"; } if (string.Equals(a, "FSR3", StringComparison.OrdinalIgnoreCase)) { return "FSR3"; } if (string.Equals(a, "DLSS", StringComparison.OrdinalIgnoreCase)) { return "DLSS"; } return "Off"; } internal static string ReadGuard(string path) { try { if (File.Exists(path)) { string[] array = File.ReadAllLines(path); foreach (string text in array) { if (text.StartsWith("state=", StringComparison.Ordinal)) { return text.Substring(6).Trim(); } } } } catch { } return ""; } internal static void WriteGuard(string path, string state) { try { Directory.CreateDirectory(Path.GetDirectoryName(path)); File.WriteAllText(path, "state=" + state + "\ntime=" + DateTime.UtcNow.ToString("o") + "\n"); } catch { } } internal static void ClearGuard(string path) { try { if (File.Exists(path)) { File.Delete(path); } } catch { } } internal static bool SmoothMotion(string guardPath, bool windows, string[] commandLine, Action<string> note, Action<string, string> setEnv) { try { if (windows) { note("NVIDIA Smooth Motion not requested: the environment variable is documented for the Linux driver only"); return false; } foreach (string text in commandLine) { switch (text.ToLowerInvariant()) { case "-nographics": case "-force-glcore": case "-force-opengl": case "-force-gles": note("NVIDIA Smooth Motion not requested: " + text); return false; } } string text2 = ReadGuard(guardPath); if (text2 == "pending" || text2 == "held") { WriteGuard(guardPath, "held"); setEnv("GO_NATIVE_SM_HELD", (text2 == "pending") ? "crashed" : "held"); note((text2 == "pending") ? "NVIDIA Smooth Motion left off this start: the last start with it stopped before the main menu (Smooth Motion can crash the game at start). Both menus can try it again at the next start or turn it off." : "NVIDIA Smooth Motion still left off: an earlier start with it stopped before the main menu. Both menus can try it again at the next start or turn it off."); return false; } WriteGuard(guardPath, "pending"); setEnv("NVPRESENT_ENABLE_SMOOTH_MOTION", "1"); note("NVIDIA Smooth Motion requested (NVPRESENT_ENABLE_SMOOTH_MOTION=1): the NVIDIA driver generates frames itself on RTX 40 series and newer"); return true; } catch (Exception ex) { note("NVIDIA Smooth Motion switch ignored: " + ex.Message); return false; } } internal static void ActivatePresenter(string backend, string patcherDir, string platformDir, string workDir, Action<string> note, Action<string, string> setEnv, Func<string, string, string> prepend, Func<string, string, char, string> append) { try { bool flag = Environment.OSVersion.Platform == PlatformID.Win32NT; string text = Path.Combine(Path.Combine(Path.Combine(patcherDir, "native"), platformDir), "VkLayer_wubarrk_frame_generation" + (flag ? ".dll" : ".so")); if (!File.Exists(text)) { note("Frame generation not enabled: presenter payload missing (" + text + ")"); return; } string text2 = NativeFiles.ReadSha256File(text + ".sha256"); if (text2 != null && !string.Equals(text2, NativeFiles.Sha256Hex(text), StringComparison.OrdinalIgnoreCase)) { note("Frame generation not enabled: presenter payload hash mismatch"); return; } File.WriteAllText(Path.Combine(workDir, "VkLayer_wubarrk_frame_generation.json"), NativeDoor.ManifestJson("VK_LAYER_WUBARRK_frame_generation", text, "Graphics Overdrive frame generation presenter (client-side)", implicitLayer: false)); string text3 = Path.Combine(workDir, "fg.cfg"); File.WriteAllText(text3, "log=" + Path.Combine(workDir, "fg.log") + "\nlog_level=1\nbackend=" + ((backend == "DLSS") ? "dlssg" : ((backend == "FSR3") ? "fsr3" : "off")) + "\n"); char arg = (flag ? ';' : ':'); setEnv("VK_LOADER_LAYERS_ENABLE", append(Environment.GetEnvironmentVariable("VK_LOADER_LAYERS_ENABLE"), "VK_LAYER_WUBARRK_frame_generation", ',')); setEnv("VK_INSTANCE_LAYERS", append(Environment.GetEnvironmentVariable("VK_INSTANCE_LAYERS"), "VK_LAYER_WUBARRK_frame_generation", arg)); setEnv("GO_FG_CONFIG", text3); setEnv("GO_FG_LAYER_LIB", text); string environmentVariable = Environment.GetEnvironmentVariable("GO_ADDON_DIRS"); if (!string.IsNullOrEmpty(environmentVariable)) { setEnv("GO_FG_ADDON_DIRS", environmentVariable); } note("Frame generation presenter enabled: VK_LAYER_WUBARRK_frame_generation from " + text + ((backend == "Off") ? ", preloaded (frame generation is Off: it passes the game's frames through until it is switched on)" : (", initial backend " + backend))); } catch (Exception ex) { note("Frame generation not enabled (ignored, the game starts normally): " + ex.Message); } } } internal static class NativeRoute { private static string RouterFileName { get { if (!IsWindows) { return "libgo_router.so"; } return "go_router.dll"; } } private static string BackendPrefix { get { if (!IsWindows) { return "libgo_backend_"; } return "go_backend_"; } } private static string LibExtension { get { if (!IsWindows) { return ".so"; } return ".dll"; } } private static bool IsWindows => Environment.OSVersion.Platform == PlatformID.Win32NT; private static string UnityPluginDir => Path.Combine(Path.Combine(Path.GetDirectoryName(Paths.ManagedPath), "Plugins"), "x86_64"); internal static void Install(string patcherDir, string platformDir, string workDir, Action<string> note, Action<string, string> setEnv) { try { string text = Path.Combine(Path.Combine(Path.Combine(patcherDir, "native"), platformDir), RouterFileName); if (!File.Exists(text)) { note("Router not installed: payload missing (" + text + ")"); return; } string b = NativeFiles.Sha256Hex(text); string text2 = NativeFiles.ReadSha256File(text + ".sha256"); if (text2 != null && !string.Equals(text2, b, StringComparison.OrdinalIgnoreCase)) { note("Router not installed: payload hash mismatch"); return; } string unityPluginDir = UnityPluginDir; string text3 = Path.Combine(unityPluginDir, RouterFileName); if (File.Exists(text3) && string.Equals(NativeFiles.Sha256Hex(text3), b, StringComparison.OrdinalIgnoreCase)) { note("Router already in place: " + text3); } else { Directory.CreateDirectory(unityPluginDir); string text4 = text3 + ".go-tmp"; File.Copy(text, text4, overwrite: true); if (File.Exists(text3)) { File.Delete(text3); } File.Move(text4, text3); note("Router installed: " + text3); } string text5 = Path.Combine(workDir, "router-data"); Directory.CreateDirectory(text5); setEnv("GO_ROUTER_LIB", text3); setEnv("GO_ROUTER_LOG", Path.Combine(workDir, "router.log")); setEnv("GO_ROUTER_DATA", text5); string text6 = FindAddonDirs(platformDir, note); setEnv("GO_ADDON_DIRS", text6); note("Router add-on folders: " + ((text6.Length == 0) ? "none" : text6)); } catch (Exception ex) { note("Router not installed (ignored, the game starts normally): " + ex.Message); } } internal static void Remove(Action<string> note) { try { string text = Path.Combine(UnityPluginDir, RouterFileName); if (File.Exists(text)) { File.Delete(text); note("Router removed from " + text + " (native mode is off)"); } } catch (Exception ex) { note("Router could not be removed: " + ex.Message); } } private static string FindAddonDirs(string platformDir, Action<string> note) { List<string> list = new List<string>(); if (!Directory.Exists(Paths.PatcherPluginPath)) { return ""; } string[] directories = Directory.GetDirectories(Paths.PatcherPluginPath); for (int i = 0; i < directories.Length; i++) { string text = Path.Combine(Path.Combine(directories[i], "native"), platformDir); if (!Directory.Exists(text)) { continue; } bool flag = false; bool flag2 = true; string[] files = Directory.GetFiles(text, BackendPrefix + "*" + LibExtension); foreach (string text2 in files) { flag = true; string text3 = NativeFiles.ReadSha256File(text2 + ".sha256"); if (text3 != null && !string.Equals(text3, NativeFiles.Sha256Hex(text2), StringComparison.OrdinalIgnoreCase)) { flag2 = false; note("Add-on skipped, hash mismatch: " + text2); } } if (flag && flag2) { list.Add(text); } } return string.Join(IsWindows ? ";" : ":", list.ToArray()); } } internal sealed class NativeSettings { internal sealed class Spec { internal readonly string Key; internal readonly string Default; internal readonly MigrationRule.ValueKind Kind; internal Spec(string key, string dflt, MigrationRule.ValueKind kind) { Key = key; Default = dflt; Kind = kind; } } internal sealed class Result { internal string Value; internal string FileValue; internal string Base; internal bool Moved; internal bool HeldBack; } internal const string Section = "Native"; internal const string MasterSection = "0 - Master"; internal const string FollowKey = "FollowUpdatedDefaults"; internal const string DefaultDeviceExtensions = "VK_NVX_binary_import,VK_NVX_image_view_handle,VK_KHR_push_descriptor,VK_KHR_buffer_device_address,VK_KHR_timeline_semaphore,VK_KHR_synchronization2"; internal const string DefaultFeatures = "bufferDeviceAddress,timelineSemaphore,synchronization2,storageWriteNoFormat,storageReadNoFormat"; internal static readonly Spec Enabled = new Spec("Enabled", "true", MigrationRule.ValueKind.Boolean); internal static readonly Spec DeviceExtensions = new Spec("DeviceExtensions", "VK_NVX_binary_import,VK_NVX_image_view_handle,VK_KHR_push_descriptor,VK_KHR_buffer_device_address,VK_KHR_timeline_semaphore,VK_KHR_synchronization2", MigrationRule.ValueKind.Text); internal static readonly Spec Features = new Spec("Features", "bufferDeviceAddress,timelineSemaphore,synchronization2,storageWriteNoFormat,storageReadNoFormat", MigrationRule.ValueKind.Text); internal static readonly Spec Inject = new Spec("Inject", "true", MigrationRule.ValueKind.Boolean); internal static readonly Spec LayerLogLevel = new Spec("LayerLogLevel", "1", MigrationRule.ValueKind.Text); internal static readonly Spec RayTracing = new Spec("RayTracing", "true", MigrationRule.ValueKind.Boolean); internal static readonly Spec FrameGeneration = new Spec("FrameGeneration", "Off", MigrationRule.ValueKind.EnumName); internal static readonly Spec FrameGenPreload = new Spec("FrameGenPreload", "true", MigrationRule.ValueKind.Boolean); internal static readonly Spec[] All = new Spec[8] { Enabled, DeviceExtensions, Features, Inject, LayerLogLevel, RayTracing, FrameGeneration, FrameGenPreload }; private readonly CfgSnapshot cfg; private readonly CfgSnapshot ledger; private readonly CfgSnapshot session; private readonly bool follow; private readonly Action<string> note; private readonly Dictionary<string, string> values = new Dictionary<string, string>(); internal static Result Effective(Spec spec, CfgSnapshot cfg, CfgSnapshot ledger, bool follow) { cfg = cfg ?? new CfgSnapshot(); ledger = ledger ?? new CfgSnapshot(); Result result = new Result(); result.FileValue = cfg.GetValue("Native", spec.Key); result.Base = MigrationRule.BaseFor("Native", spec.Key, ledger, cfg); if (MigrationRule.Decide(result.FileValue, result.Base, spec.Default, spec.Kind) == MigrationRule.Outcome.TakeNew) { if (follow) { result.Moved = true; } else { result.HeldBack = true; } } result.Value = (result.Moved ? spec.Default : (result.FileValue ?? spec.Default)); return result; } internal static bool Follow(CfgSnapshot cfg) { string text = cfg?.GetValue("0 - Master", "FollowUpdatedDefaults"); bool result = default(bool); return text == null || !bool.TryParse(text, out result) || result; } internal static string Describe(Spec spec, Result r, CfgSnapshot cfg, CfgSnapshot ledger) { string text = "[Native] " + spec.Key + " = " + r.FileValue + " was the default of " + Era(spec, r, cfg, ledger); if (r.Moved) { return text + ": this start uses " + spec.Default + " (the mod saves it; Undo is in its menus)"; } return text + " and the default is " + spec.Default + " now: this start keeps " + r.FileValue + " because [0 - Master] FollowUpdatedDefaults = false"; } private static string Era(Spec spec, Result r, CfgSnapshot cfg, CfgSnapshot ledger) { DefaultHistory.Row[] rows = DefaultHistory.Rows; foreach (DefaultHistory.Row row in rows) { if (row.Section == "Native" && row.Key == spec.Key && MigrationRule.SameValue(row.OldDefault, r.Base, spec.Kind)) { return row.UntilVersion + " and earlier"; } } string text = null; if (ledger != null && ledger.TryGet("Native", spec.Key, out var entry) && MigrationRule.SameValue(entry.Value, r.Base, spec.Kind)) { text = ledger.PluginVersion; } else if (cfg != null && cfg.TryGet("Native", spec.Key, out entry) && entry.RecordedDefault != null && MigrationRule.SameValue(entry.RecordedDefault, r.Base, spec.Kind)) { text = cfg.PluginVersion; } return text ?? "an earlier version"; } internal NativeSettings(CfgSnapshot cfg, CfgSnapshot ledger, Action<string> note, CfgSnapshot session = null) { this.cfg = cfg ?? new CfgSnapshot(); this.ledger = ledger ?? new CfgSnapshot(); this.note = note ?? ((Action<string>)delegate { }); this.session = session; follow = Follow(this.cfg); } internal static NativeSettings Load(string cfgFile, string ledgerFile, Action<string> note) { return new NativeSettings(CfgSnapshot.Load(cfgFile), CfgSnapshot.Load(ledgerFile), note); } internal static NativeSettings Load(string cfgFile, string ledgerFile, Action<string> note, string sidecarFile, string restartToken) { Action<string> action = note ?? ((Action<string>)delegate { }); CfgSnapshot cfgSnapshot = null; try { if (sidecarFile != null && File.Exists(sidecarFile)) { cfgSnapshot = TuneProtocol.ReadSidecar(sidecarFile, restartToken, out var why); action((cfgSnapshot != null) ? "Test and Tune restart: this start uses the session's [Native] values (tune-native.cfg, the restart token matches)" : ("Test and Tune: tune-native.cfg is not used for this start (" + why + "); the cfg's values apply")); } } catch (Exception ex) { action("Test and Tune: tune-native.cfg could not be read (" + ex.Message + "); the cfg's values apply"); cfgSnapshot = null; } return new NativeSettings(CfgSnapshot.Load(cfgFile), CfgSnapshot.Load(ledgerFile), action, cfgSnapshot); } internal string Text(Spec spec) { if (values.TryGetValue(spec.Key, out var value)) { return value; } try { Result result = Effective(spec, cfg, ledger, follow); if (session != null && session.TryGet("Native", spec.Key, out var entry)) { if (!string.Equals(entry.Value, result.Value, StringComparison.Ordinal)) { note("[Native] " + spec.Key + " = " + entry.Value + ": this start uses the Test and Tune session's value (the cfg has " + result.Value + ")"); } value = entry.Value; } else { if (result.Moved || result.HeldBack) { note(Describe(spec, result, cfg, ledger)); } value = result.Value; } } catch (Exception ex) { note("[Native] " + spec.Key + " could not be worked out (" + ex.Message + "): this start uses " + spec.Default); value = spec.Default; } values[spec.Key] = value; return value; } internal bool Bool(Spec spec) { if (!bool.TryParse(Text(spec), out var result)) { return bool.Parse(spec.Default); } return result; } } internal static class WindowsLoaderCheck { internal sealed class Facts { internal bool Elevated; internal string Integrity = "unknown"; internal string LoaderPath = "unknown"; internal string LoaderFileVersion; internal string LoaderApiVersion; internal bool LayerPathSet; internal string VariablesBefore = ""; internal bool SettingsActive; internal bool Vkconfig; internal string Settings = "none"; internal string Uac = "unknown"; internal readonly List<string> LoadErrors = new List<string>(); internal bool UnlockLoads = true; internal bool PresenterLoads = true; internal bool OverrideLayer; internal bool NonAsciiPath; internal bool RegistryTried; internal string ListedEnvironment = "not checked"; internal string ListedRegistry = "not tried"; internal string Route = "not decided"; internal string LoaderVersion { get { if (string.IsNullOrEmpty(LoaderApiVersion)) { return LoaderFileVersion; } return LoaderApiVersion; } } } [UnmanagedFunctionPointer(CallingConvention.Winapi)] private delegate int EnumerateInstanceVersion(out uint apiVersion); [UnmanagedFunctionPointer(CallingConvention.Winapi)] private delegate int EnumerateInstanceLayerProperties(ref uint count, IntPtr properties); internal const string EnableVar = "GO_NATIVE_DOOR"; internal const string DisableVar = "GO_NATIVE_DOOR_OFF"; internal const string UnlockImplicit = "VkLayer_wubarrk_graphicsoverdrive.implicit.json"; internal const string PresenterImplicit = "VkLayer_wubarrk_frame_generation.implicit.json"; private const string ImplicitKey = "SOFTWARE\\Khronos\\Vulkan\\ImplicitLayers"; private const string SettingsKey = "SOFTWARE\\Khronos\\Vulkan\\LoaderSettings"; private const string VkconfigKey = "SOFTWARE\\LunarG\\vkconfig"; private const string Prefix = "Windows loader check: "; private const string LogHint = "see patcher.log in BepInEx/cache/GraphicsOverdrive/vulkan"; internal static readonly string[] WatchedVariables = new string[9] { "VK_LAYER_PATH", "VK_ADD_LAYER_PATH", "VK_INSTANCE_LAYERS", "VK_LOADER_LAYERS_ENABLE", "VK_LOADER_LAYERS_DISABLE", "VK_LOADER_LAYERS_ALLOW", "VK_IMPLICIT_LAYER_PATH", "VK_ADD_IMPLICIT_LAYER_PATH", "VK_LOADER_DEBUG" }; private static readonly string[] RouteVariables = new string[6] { "VK_LAYER_PATH", "VK_ADD_LAYER_PATH", "VK_LOADER_LAYERS_ENABLE", "VK_INSTANCE_LAYERS", "GO_NATIVE_DOOR", "GO_NATIVE_DOOR_OFF" }; private static readonly Dictionary<string, string> before = new Dictionary<string, string>(); private static IntPtr loaderModule; private static EnumerateInstanceLayerProperties enumerateLayers; private const int LayerPropertiesSize = 520; private const int VK_INCOMPLETE = 5; private static readonly string[] PresenterVariables = new string[3] { "GO_FG_LAYER_LIB", "GO_FG_CONFIG", "GO_FG_ADDON_DIRS" }; private static readonly string[] DoorVariables = new string[6] { "GO_ROUTER_LIB", "GO_LAYER_LIB", "GO_LAYER_CONFIG", "GO_FG_LAYER_LIB", "GO_FG_CONFIG", "GO_FG_ADDON_DIRS" }; private const uint LOAD_WITH_ALTERED_SEARCH_PATH = 8u; private const uint TOKEN_QUERY = 8u; private const int TokenIntegrityLevel = 25; private const int KEY_QUERY_VALUE = 1; private const int KEY_SET_VALUE = 2; private const int REG_DWORD = 4; private const int ERROR_FILE_NOT_FOUND = 2; internal static void CaptureEnvironment() { try { string[] watchedVariables = WatchedVariables; foreach (string text in watchedVariables) { before[text] = Environment.GetEnvironmentVariable(text); } before["GO_FG_LAYER_LIB"] = Environment.GetEnvironmentVariable("GO_FG_LAYER_LIB"); } catch { } } private static string Before(string name) { if (!before.TryGetValue(name, out var value)) { return null; } return value; } internal static string Run(string workDir, string unlockPayload, Action<string> note, Action<string, string> setEnv) { Facts facts = new Facts(); Dictionary<string, string> dictionary = new Dictionary<string, string>(); bool flag = false; try { string[] routeVariables = RouteVariables; foreach (string text in routeVariables) { dictionary[text] = Environment.GetEnvironmentVariable(text); } string environmentVariable = Environment.GetEnvironmentVariable("GO_FG_LAYER_LIB"); bool flag2 = !string.IsNullOrEmpty(environmentVariable) && string.IsNullOrEmpty(Before("GO_FG_LAYER_LIB")); facts.Elevated = IsElevated(out var how); facts.Integrity = how; note("Windows loader check: elevated: " + (facts.Elevated ? "yes" : "no") + " (" + how + ")" + (facts.Elevated ? ": the loader ignores VK_ADD_LAYER_PATH, VK_LOADER_LAYERS_ENABLE and the HKCU registry" : "")); facts.Uac = ReadUac(); if (facts.Uac == "off") { note("Windows loader check: User Account Control is off on this PC (EnableLUA = 0): every program of an administrator account runs with administrator rights, so the registry route is the normal way in here"); } OpenLoader(facts); note("Windows loader check: Vulkan loader " + facts.LoaderPath + ", file version " + (facts.LoaderFileVersion ?? "unknown") + ", " + ((facts.LoaderApiVersion == "1.0") ? "a 1.0 loader (no vkEnumerateInstanceVersion)" : ("instance version " + (facts.LoaderApiVersion ?? "not reported")))); RemoveRegistryRoute(note, workDir); List<string> list = new List<string>(); routeVariables = WatchedVariables; foreach (string text2 in routeVariables) { string text3 = Before(text2); if (!string.IsNullOrEmpty(text3)) { list.Add(text2); note("Windows loader check: " + text2 + " was set before this start: " + Cut(text3, 200)); } } if (list.Count == 0) { note("Windows loader check: none of the VK_* variables were set before this start"); } facts.VariablesBefore = string.Join(",", list.ToArray()); facts.LayerPathSet = !string.IsNullOrEmpty(Before("VK_LAYER_PATH")); ReadSettings(facts); note("Windows loader check: loader settings: " + facts.Settings + "; Vulkan Configurator " + (facts.Vkconfig ? "installed" : "not installed")); facts.UnlockLoads = LoadTest(unlockPayload, facts, note); if (flag2) { facts.PresenterLoads = LoadTest(environmentVariable, facts, note); } facts.NonAsciiPath = !IsAscii(workDir); if (facts.NonAsciiPath) { note("Windows loader check: the folder path has characters outside plain ASCII: " + workDir); } if (!facts.UnlockLoads) { note("Windows loader check: Windows cannot load the unlock layer, so the loader would skip it at game start: the door is not active this session"); facts.Route = "unlock layer does not load"; return Failed(facts, setEnv); } if (flag2 && !facts.PresenterLoads) { note("Windows loader check: Windows cannot load the frame generation presenter, so the loader would skip it: frame generation stays off this session"); flag2 = false; routeVariables = PresenterVariables; for (int i = 0; i < routeVariables.Length; i++) { Unset(routeVariables[i], setEnv); } } if (facts.LayerPathSet) { setEnv("VK_LAYER_PATH", NativeDoor.Prepend(Environment.GetEnvironmentVariable("VK_LAYER_PATH"), workDir, ';')); note("Windows loader check: VK_LAYER_PATH was set before this start, so the loader ignores VK_ADD_LAYER_PATH: put " + workDir + " in front of it"); } if (Listed(facts, flag2, note, out var listed)) { facts.ListedEnvironment = listed; facts.Route = "environment"; note("Windows loader check: the loader lists " + (flag2 ? "both of our layers" : "our layer") + " (environment route)"); return null; } facts.ListedEnvironment = listed; bool elevated = facts.Elevated; string text4 = HiveName(elevated) + "\\SOFTWARE\\Khronos\\Vulkan\\ImplicitLayers"; note("Windows loader check: the loader does not list our layers through the environment variables; trying the registry route (" + text4 + ")"); facts.RegistryTried = true; List<string> list2 = new List<string>(); string fullPath = Path.GetFullPath(Path.Combine(workDir, "VkLayer_wubarrk_graphicsoverdrive.implicit.json")); flag = true; File.WriteAllText(fullPath, ImplicitManifest("VK_LAYER_WUBARRK_graphics_overdrive", unlockPayload, "Graphics Overdrive native layer (client-side)")); list2.Add(fullPath); if (flag2) { string fullPath2 = Path.GetFullPath(Path.Combine(workDir, "VkLayer_wubarrk_frame_generation.implicit.json")); File.WriteAllText(fullPath2, ImplicitManifest("VK_LAYER_WUBARRK_frame_generation", environmentVariable, "Graphics Overdrive frame generation presenter (client-side)")); list2.Add(fullPath2); } int num = Register(elevated, list2, note); if (num != 0) { note("Windows loader check: could not write " + text4 + ": Windows error " + num + ": " + Win32Message(num)); RemoveRegistryRoute(note, workDir); facts.Route = "registry write failed"; return Failed(facts, setEnv); } setEnv("GO_NATIVE_DOOR", "1"); Unset("GO_NATIVE_DOOR_OFF", setEnv); UndoEnvironmentRoute(facts, setEnv); if (GetNative("GO_NATIVE_DOOR") != "1" || IsSetNative("GO_NATIVE_DOOR_OFF")) { note("Windows loader check: GO_NATIVE_DOOR is not '1' in the process environment, or GO_NATIVE_DOOR_OFF is set: the loader would keep the implicit layers off"); RemoveRegistryRoute(note, workDir); facts.Route = "registry enable variable not set"; return Failed(facts, setEnv); } bool num2 = Listed(facts, flag2, note, out listed); facts.ListedRegistry = listed; if (num2) { facts.Route = "registry " + HiveName(elevated); note("Windows loader check: Native Vulkan layer activated through the Windows registry (" + text4 + "): the loader found the registered manifests; the environment route did not reach it: " + Words(facts)); return null; } RemoveRegistryRoute(note, workDir); facts.Route = "registry not listed"; return Failed(facts, setEnv); } catch (Exception ex) { try { if (flag) { RemoveRegistryRoute(note, workDir); } foreach (KeyValuePair<string, string> item in dictionary) { Restore(item.Key, item.Value, setEnv); } } catch { } facts.Route = "skipped"; note("Windows loader check: skipped, the environment route stays as in 1.1.3 (whether the loader loads the layer is not known this session): " + ex.Message); return null; } finally { try { note("Windows loader check: verdict: " + Verdict(facts)); } catch { } try { setEnv("GO_NATIVE_WINCHECK", CompactLine(facts)); } catch { } } } internal static string Verdict(Facts f) { if (f.Route == "environment") { return "active (environment route)"; } if (f.Route.StartsWith("registry HK", StringComparison.Ordinal)) { return "active (" + f.Route + " route)"; } if (f.Route == "skipped") { return "skipped: the loader could not be asked, the environment route stays as in 1.1.3"; } return "not active (" + f.Route + ")"; } private static string Failed(Facts f, Action<string, string> setEnv) { UndoEnvironmentRoute(f, setEnv); Unset("GO_NATIVE_DOOR", setEnv); return "the Vulkan loader does not list the layer: " + Words(f) + "; see patcher.log in BepInEx/cache/GraphicsOverdrive/vulkan"; } internal static string Words(Facts f) { List<string> list = new List<string>(); if (f.Elevated) { list.Add((f.Uac == "off") ? "the game runs with administrator rights because User Account Control is off on this PC (every program does; Graphics Overdrive then registers its layer in the registry by itself)" : "the game runs with administrator rights (Steam or Gale was started with 'Run as administrator'; Graphics Overdrive then registers its layer in the registry by itself, or start them without it)"); } if (f.LayerPathSet) { list.Add("VK_LAYER_PATH is set on this PC"); } if (f.SettingsActive) { list.Add("a Vulkan loader settings file is active (Vulkan Configurator decides which layers load: add VK_LAYER_WUBARRK_graphics_overdrive there or remove the configuration)"); } if (f.OverrideLayer) { list.Add("the Vulkan Configurator override layer is active (close Vulkan Configurator or reset its layer configuration)"); } if (!f.Elevated && !string.IsNullOrEmpty(Before("VK_LOADER_LAYERS_DISABLE"))) { list.Add("VK_LOADER_LAYERS_DISABLE is set on this PC (" + Cut(Before("VK_LOADER_LAYERS_DISABLE"), 100) + ")"); } string loaderVersion = f.LoaderVersion; if (OlderThan(loaderVersion, 1, 3, 207)) { list.Add("the Vulkan loader (vulkan-1.dll " + loaderVersion + ") is older than 1.3.207"); } foreach (string loadError in f.LoadErrors) { list.Add("Windows could not load " + loadError); } if (f.NonAsciiPath && f.RegistryTried) { list.Add("the BepInEx folder path has characters outside plain ASCII, which the loader cannot read from the registry on every PC"); } if (list.Count != 0) { return string.Join("; ", list.ToArray()); } return "no known cause was found"; } internal static string CompactLine(Facts f) { List<string> list = new List<string> { "elevated=" + (f.Elevated ? "yes" : "no"), "integrity=" + f.Integrity, "loader=" + f.LoaderPath, "loaderFile=" + (f.LoaderFileVersion ?? "unknown"), "loaderApi=" + (f.LoaderApiVersion ?? "unknown"), "varsBefore=" + ((f.VariablesBefore.Length == 0) ? "none" : f.VariablesBefore), "settings=" + (f.SettingsActive ? "active" : ((f.Settings == "none") ? "none" : "present, not active")), "vkconfig=" + (f.Vkconfig ? "yes" : "no"), "uac=" + f.Uac, "dllLoad=" + ((f.LoadErrors.Count == 0) ? "ok" : string.Join(", ", f.LoadErrors.ToArray())), "asciiPath=" + (f.NonAsciiPath ? "no" : "yes"), "listedEnv=" + f.ListedEnvironment, "listedRegistry=" + f.ListedRegistry, "route=" + f.Route }; for (int i = 0; i < list.Count; i++) { list[i] = list[i].Replace(';', ','); } return string.Join("; ", list.ToArray()); } internal static string ImplicitManifest(string name, string libraryPath, string description) { return NativeDoor.ManifestJson(name, libraryPath, description, implicitLayer: true); } internal static string RemoveFromList(string list, string name, char sep) { if (string.IsNullOrEmpty(list)) { return null; } List<string> list2 = new List<string>(); string[] array = list.Split(new char[1] { sep }); foreach (string text in array) { if (text != name) { list2.Add(text); } } string text2 = string.Join(sep.ToString(), list2.ToArray()); if (text2.Length != 0) { return text2; } return null; } internal static bool Contains(string list, string name, char sep) { if (string.IsNullOrEmpty(list)) { return false; } string[] array = list.Split(new char[1] { sep }); for (int i = 0; i < array.Length; i++) { if (array[i] == name) { return true; } } return false; } internal static bool IsOurValue(string valueName) { if (!valueName.EndsWith("\\VkLayer_wubarrk_graphicsoverdrive.implicit.json", StringComparison.OrdinalIgnoreCase)) { return valueName.EndsWith("\\VkLayer_wubarrk_frame_generation.implicit.json", StringComparison.OrdinalIgnoreCase); } return true; } internal static List<string> LayerNames(byte[] buffer, int count) { List<string> list = new List<string>(); for (int i = 0; i < count; i++) { int num = i * 520; int j; for (j = 0; j < 256 && buffer[num + j] != 0; j++) { } list.Add(Encoding.UTF8.GetString(buffer, num, j)); } return list; } internal static bool OlderThan(string version, int major, int minor, int patch) { if (string.IsNullOrEmpty(version)) { return false; } string[] array = version.Trim().Split(new char[1] { '.' }); int[] array2 = new int[3]; for (int i = 0; i < 3 && i < array.Length; i++) { if (!int.TryParse(array[i].Trim(), out array2[i])) { return false; } } if (array2[0] != major) { return array2[0] < major; } if (array2[1] != minor) { return array2[1] < minor; } return array2[2] < patch; } internal static string ApiVersionText(uint v) { return ((v >> 22) & 0x7F) + "." + ((v >> 12) & 0x3FF) + "." + (v & 0xFFF); } private static string Cut(string s, int max) { if (s.Length > max) { return s.Substring(0, max) + "..."; } return s; } private static bool IsAscii(string s) { for (int i = 0; i < s.Length; i++) { if (s[i] > '\u007f') { return false; } } return true; } private static void OpenLoader(Facts f) { if (loaderModule == IntPtr.Zero) { loaderModule = LoadLibraryW("vulkan-1.dll"); } if (loaderModule == IntPtr.Zero) { int lastWin32Error = Marshal.GetLastWin32Error(); throw new InvalidOperationException("vulkan-1.dll could not be loaded: Windows error " + lastWin32Error + ": " + Win32Message(lastWin32Error)); } StringBuilder stringBuilder = new StringBuilder(1024); int moduleFileNameW = GetModuleFileNameW(loaderModule, stringBuilder, stringBuilder.Capacity); f.LoaderPath = ((moduleFileNameW > 0) ? stringBuilder.ToString() : "vulkan-1.dll"); try { f.LoaderFileVersion = FileVersionInfo.GetVersionInfo(f.LoaderPath).FileVersion; } catch { } IntPtr procAddress = GetProcAddress(loaderModule, "vkEnumerateInstanceLayerProperties"); if (procAddress == IntPtr.Zero) { throw new InvalidOperationException("vulkan-1.dll does not export vkEnumerateInstanceLayerProperties"); } enumerateLayers = (EnumerateInstanceLayerProperties)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(EnumerateInstanceLayerProperties)); uint count = 0u; int num = enumerateLayers(ref count, IntPtr.Zero); if (num != 0) { throw new InvalidOperationException("vkEnumerateInstanceLayerProperties returned " + num); } IntPtr procAddress2 = GetProcAddress(loaderModule, "vkEnumerateInstanceVersion"); uint apiVersion; if (procAddress2 == IntPtr.Zero) { f.LoaderApiVersion = "1.0"; } else if (((EnumerateInstanceVersion)Marshal.GetDelegateForFunctionPointer(procAddress2, typeof(EnumerateInstanceVersion)))(out apiVersion) == 0) { f.LoaderApiVersion = ApiVersionText(apiVersion); } } private static bool Listed(Facts f, bool presenter, Action<string> note, out string listed) { List<string> list = EnumerateLayers(); note("Windows loader check: the loader lists " + list.Count + " layer(s): " + Cut((list.Count == 0) ? "none" : string.Join(", ", list.ToArray()), 600)); if (list.Contains("VK_LAYER_LUNARG_override")) { f.OverrideLayer = true; } bool flag = list.Contains("VK_LAYER_WUBARRK_graphics_overdrive"); bool flag2 = presenter && list.Contains("VK_LAYER_WUBARRK_frame_generation"); listed = "unlock " + (flag ? "yes" : "no") + ", presenter " + ((!presenter) ? "not requested" : (flag2 ? "yes" : "no")); note("Windows loader check: ours: VK_LAYER_WUBARRK_graphics_overdrive " + (flag ? "yes" : "no") + ", VK_LAYER_WUBARRK_frame_generation " + ((!presenter) ? "not requested" : (flag2 ? "yes" : "no"))); if (flag) { return !presenter || flag2; } return false; } private static List<string> EnumerateLayers() { EnumerateInstanceLayerProperties enumerateInstanceLayerProperties = enumerateLayers; if (enumerateInstanceLayerProperties == null) { throw new InvalidOperationException("the loader was not opened"); } for (int i = 0; i < 4; i++) { uint count = 0u; int num = enumerateInstanceLayerProperties(ref count, IntPtr.Zero); if (num != 0) { throw new InvalidOperationException("vkEnumerateInstanceLayerProperties returned " + num); } if (count == 0) { return new List<string>(); } IntPtr intPtr = Marshal.AllocHGlobal((int)(count * 520)); try { uint count2 = count; num = enumerateInstanceLayerProperties(ref count2, intPtr); switch (num) { case 5: break; default: throw new InvalidOperationException("vkEnumerateInstanceLayerProperties returned " + num); case 0: { byte[] array = new byte[count2 * 520]; Marshal.Copy(intPtr, array, 0, array.Length); return LayerNames(array, (int)count2); } } } finally { Marshal.FreeHGlobal(intPtr); } } throw new InvalidOperationException("vkEnumerateInstanceLayerProperties kept returning VK_INCOMPLETE"); } private static bool LoadTest(string path, Facts f, Action<string> note) { IntPtr intPtr = LoadLibraryExW(path, IntPtr.Zero, 8u); if (intPtr == IntPtr.Zero) { int lastWin32Error = Marshal.GetLastWin32Error(); f.LoadErrors.Add(Path.GetFileName(path) + ": error " + lastWin32Error); note("Windows loader check: Windows could not load " + path + ": Windows error " + lastWin32Error + ": " + Win32Message(lastWin32Error)); return false; } FreeLibrary(intPtr); note("Windows loader check: Windows loads " + path); return true; } private static bool IsElevated(out string how) { IntPtr token = IntPtr.Zero; IntPtr intPtr = IntPtr.Zero; how = null; try { if (!OpenProcessToken(GetCurrentProcess(), 8u, out token)) { how = "OpenProcessToken failed: Windows error " + Marshal.GetLastWin32Error(); } else { GetTokenInformation(token, 25, IntPtr.Zero, 0, out var returnLength); if (returnLength <= 0) { how = "GetTokenInformation gave no size: Windows error " + Marshal.GetLastWin32Error(); } else { intPtr = Marshal.AllocHGlobal(returnLength); if (GetTokenInformation(token, 25, intPtr, returnLength, out returnLength)) { IntPtr sid = Marshal.ReadIntPtr(intPtr); int num = Marshal.ReadByte(GetSidSubAuthorityCount(sid)); uint num2 = (uint)Marshal.ReadInt32(GetSidSubAuthority(sid, (uint)(num - 1))); how = "integrity level 0x" + num2.ToString("x4"); return num2 >= 12288; } how = "GetTokenInformation failed: Windows error " + Marshal.GetLastWin32Error(); } } } catch (Exception ex) { how = "token query failed: " + ex.Message; } finally { if (intPtr != IntPtr.Zero) { Marshal.FreeHGlobal(intPtr); } if (token != IntPtr.Zero) { CloseHandle(token); } } try { bool flag = new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator); how = how + ", administrator group: " + (flag ? "yes" : "no"); return flag; } catch (Exception ex2) { how = how + ", administrator check failed: " + ex2.Message; return false; } } private static IntPtr Hive(bool machine) { return new IntPtr(machine ? (-2147483646) : (-2147483647)); } private static string HiveName(bool machine) { if (!machine) { return "HKCU"; } return "HKLM"; } private static List<string> ValueNames(IntPtr key) { List<string> list = new List<string>(); StringBuilder stringBuilder = new StringBuilder(16384); int num = 0; while (true) { stringBuilder.Length = 0; int nameLength = stringBuilder.Capacity; if (RegEnumValueW(key, num, stringBuilder, ref nameLength, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero) != 0) { break; } list.Add(stringBuilder.ToString()); num++; } return list; } private static int Register(bool machine, List<string> manifests, Action<string> note) { int num = RegCreateKeyExW(Hive(machine), "SOFTWARE\\Khronos\\Vulkan\\ImplicitLayers", 0, null, 0, 3, IntPtr.Zero, out var key, out var _); if (num != 0) { return num; } try { foreach (string manifest in manifests) { int data = 0; num = RegSetValueExW(key, manifest, 0, 4, ref data, 4); if (num != 0) { return num; } note("Windows loader check: registered " + manifest + " in " + HiveName(machine) + "\\SOFTWARE\\Khronos\\Vulkan\\ImplicitLayers"); } } finally { RegCloseKey(key); } return 0; } internal static void RemoveRegistryRoute(Action<string> note, string workDir) { bool[] array = new bool[2] { false, true }; foreach (bool machine in array) { try { IntPtr key; int num = RegOpenKeyExW(Hive(machine), "SOFTWARE\\Khronos\\Vulkan\\ImplicitLayers", 0, 3, out key); switch (num) { case 2: continue; case 0: try { foreach (string item in ValueNames(key)) { if (IsOurValue(item)) { int num2 = RegDeleteValueW(key, item); note((num2 == 0) ? ("Windows loader check: removed the registry value " + HiveName(machine) + "\\SOFTWARE\\Khronos\\Vulkan\\ImplicitLayers\\" + item + "; " + RemoveManifestFile(item)) : ("Windows loader check: could not remove the registry value " + HiveName(machine) + "\\SOFTWARE\\Khronos\\Vulkan\\ImplicitLayers\\" + item + ": Windows error " + num2 + "; " + RemoveManifestFile(item))); } } } finally { RegCloseKey(key); } continue; } if (RegOpenKeyExW(Hive(machine), "SOFTWARE\\Khronos\\Vulkan\\ImplicitLayers", 0, 1, out key) != 0) { continue; } try { foreach (string item2 in ValueNames(key)) { if (IsOurValue(item2)) { note("Windows loader check: cannot remove the registry value " + HiveName(machine) + "\\SOFTWARE\\Khronos\\Vulkan\\ImplicitLayers\\" + item2 + " (Windows error " + num + ", it needs administrator rights); " + RemoveManifestFile(item2)); } } } finally { RegCloseKey(key); } } catch (Exception ex) { note("Windows loader check: registry cleanup in " + HiveName(machine) + " failed: " + ex.Message); } } if (workDir == null) { return; } string[] array2 = new string[2] { "VkLayer_wubarrk_graphicsoverdrive.implicit.json", "VkLayer_wubarrk_frame_generation.implicit.json" }; foreach (string path in array2) { try { string path2 = Path.Combine(workDir, path); if (File.Exists(path2)) { File.Delete(path2); } } catch { } } } private static string RemoveManifestFile(string path) { try { if (!File.Exists(path)) { return "its manifest file is already gone, so the loader skips it"; } File.Delete(path); return "its manifest file was removed, so the loader skips it"; } catch (Exception ex) { return "its manifest file " + path + " could not be removed (" + ex.Message + "), so the loader may still list that layer"; } } private static void ReadSettings(Facts f) { List<string> list = new List<string>(); bool[] array = new bool[2] { false, true }; foreach (bool flag in array) { if (RegOpenKeyExW(Hive(flag), "SOFTWARE\\Khronos\\Vulkan\\LoaderSettings", 0, 1, out var key) != 0) { continue; } try { List<string> list2 = ValueNames(key); string text = null; foreach (string item in list2) { if (item.EndsWith("\\vk_loader_settings.json", StringComparison.Ordinal) && File.Exists(item)) { text = item; break; } } bool flag2 = flag || !f.Elevated; list.Add(HiveName(flag) + "\\SOFTWARE\\Khronos\\Vulkan\\LoaderSettings has " + list2.Count + " value(s)" + ((text == null) ? ", no existing vk_loader_settings.json" : (", settings file " + text + (flag2 ? "" : " (not read: elevated)")))); if (text != null && flag2) { f.SettingsActive = true; } } finally { RegCloseKey(key); } } if (RegOpenKeyExW(Hive(machine: false), "SOFTWARE\\LunarG\\vkconfig", 0, 1, out var key2) == 0) { f.Vkconfig = true; RegCloseKey(key2); } f.Settings = ((list.Count == 0) ? "none" : string.Join("; ", list.ToArray())); } private static string ReadUac() { try { if (RegOpenKeyExW(Hive(machine: true), "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", 0, 1, out var key) != 0) { return "unknown"; } try { int data = 0; int size = 4; if (RegQueryValueExW(key, "EnableLUA", IntPtr.Zero, out var type, ref data, ref size) != 0 || type != 4) { return "unknown"; } return (data == 0) ? "off" : "on"; } finally { RegCloseKey(key); } } catch { return "unknown"; } } private static void UndoEnvironmentRoute(Facts f, Action<string, string> setEnv) { Restore("VK_ADD_LAYER_PATH", Before("VK_ADD_LAYER_PATH"), setEnv); if (f.LayerPathSet) { Restore("VK_LAYER_PATH", Before("VK_LAYER_PATH"), setEnv); } Restore("VK_LOADER_LAYERS_ENABLE", Before("VK_LOADER_LAYERS_ENABLE"), setEnv); Restore("VK_INSTANCE_LAYERS", Before("VK_INSTANCE_LAYERS"), setEnv); } private static void Restore(string name, string value, Action<string, string> setEnv) { if (string.IsNullOrEmpty(value)) { Unset(name, setEnv); } else { setEnv(name, value); } } internal static void Unset(string name, Action<string, string> setEnv) { setEnv(name, null); if (IsSetNative(name)) { SetEnvironmentVariableW(name, null); } if (IsSetNative(name)) { setEnv(name, ""); } } private static bool IsSetNative(string name) { return GetEnvironmentVariableW(name, null, 0u) != 0; } private static string GetNative(string name) { uint environmentVariableW = GetEnvironmentVariableW(name, null, 0u); if (environmentVariableW == 0) { return null; } StringBuilder stringBuilder = new StringBuilder((int)environmentVariableW); uint environmentVariableW2 = GetEnvironmentVariableW(name, stringBuilder, environmentVariableW); if (environmentVariableW2 != 0 && environmentVariableW2 < environmentVariableW) { return stringBuilder.ToString(); } return null; } internal static void UnsetDoorVariables(Action<string, string> setEnv) { string[] doorVariables = DoorVariables; for (int i = 0; i < doorVariables.Length; i++) { Unset(doorVariables[i], setEnv); } } private static string Win32Message(int err) { try { return new Win32Exception(err).Message; } catch { return "(no message)"; } } [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)] private static extern IntPtr LoadLibraryW(string name); [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)] private static extern IntPtr LoadLibraryExW(string name, IntPtr file, uint flags); [DllImport("kernel32", SetLastError = true)] private static extern bool FreeLibrary(IntPtr module); [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)] private static extern int GetModuleFileNameW(IntPtr module, StringBuilder path, int size); [DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)] private static extern IntPtr GetProcAddress(IntPtr module, string name); [DllImport("kernel32")] private static extern IntPtr GetCurrentProcess(); [DllImport("kernel32", SetLastError = true)] private static extern bool CloseHandle(IntPtr handle); [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)] private static extern uint GetEnvironmentVariableW(string name, StringBuilder buffer, uint size); [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)] private static extern bool SetEnvironmentVariableW(string name, string value); [DllImport("advapi32", SetLastError = true)] private static extern bool OpenProcessToken(IntPtr process, uint access, out IntPtr token); [DllImport("advapi32", SetLastError = true)] private static extern bool GetTokenInformation(IntPtr token, int infoClass, IntPtr info, int length, out int returnLength); [DllImport("advapi32")] private static extern IntPtr GetSidSubAuthorityCount(IntPtr sid); [DllImport("advapi32")] private static extern IntPtr GetSidSubAuthority(IntPtr sid, uint index); [DllImport("advapi32", CharSet = CharSet.Unicode)] private static extern int RegOpenKeyExW(IntPtr hive, string subKey, int options, int access, out IntPtr key); [DllImport("advapi32", CharSet = CharSet.Unicode)] private static extern int RegCreateKeyExW(IntPtr hive, string subKey, int reserved, string cls, int options, int access, IntPtr security, out IntPtr key, out int disposition); [DllImport("advapi32", CharSet = CharSet.Unicode)] private static extern int RegSetValueExW(IntPtr key, string name, int reserved, int type, ref int data, int size); [DllImport("advapi32", CharSet = CharSet.Unicode)] private static extern int RegDeleteValueW(IntPtr key, string name); [DllImport("advapi32", CharSet = CharSet.Unicode)] private static extern int RegQueryValueExW(IntPtr key, string name, IntPtr reserved, out int type, ref int data, ref int size); [DllImport("advapi32", CharSet = CharSet.Unicode)] private static extern int RegEnumValueW(IntPtr key, int index, StringBuilder name, ref int nameLength, IntPtr reserved, IntPtr type, IntPtr data, IntPtr dataLength); [DllImport("advapi32")] private static extern int RegCloseKey(IntPtr key); } }
plugins/GraphicsOverdrive.dll
Decompiled 2 weeks ago
The result has been truncated due to the large size, download it to view full contents!
using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Versioning; using System.Security; using System.Security.Permissions; using System.Text; using System.Text.RegularExpressions; using System.Threading; using AmplifyOcclusion; using BepInEx; using BepInEx.Bootstrap; using BepInEx.Configuration; using BepInEx.Logging; using GraphicsOverdrive.Benchmark; using GraphicsOverdrive.Config; using GraphicsOverdrive.Native; using GraphicsOverdrive.Patches; using GraphicsOverdrive.Presets; using GraphicsOverdrive.Tune; using HarmonyLib; using Microsoft.CodeAnalysis; using TMPro; using Unity.Collections; using Unity.Collections.LowLevel.Unsafe; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.Events; using UnityEngine.Experimental.Rendering; using UnityEngine.PostProcessing; using UnityEngine.Profiling; using UnityEngine.Rendering; using UnityEngine.UI; using Valheim.SettingsGui; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")] [assembly: IgnoresAccessChecksTo("assembly_guiutils")] [assembly: IgnoresAccessChecksTo("assembly_postprocessing")] [assembly: IgnoresAccessChecksTo("assembly_utils")] [assembly: IgnoresAccessChecksTo("assembly_valheim")] [assembly: AssemblyCompany("GraphicsOverdrive")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyDescription("Unlocks all the graphic dials in the Unity engine and Valheim into an easy to use, understand and tweak format.")] [assembly: AssemblyFileVersion("1.2.3.0")] [assembly: AssemblyInformationalVersion("1.2.3+13a8f456c7e8a6c70476438f4378f858fd8674bb")] [assembly: AssemblyProduct("GraphicsOverdrive")] [assembly: AssemblyTitle("GraphicsOverdrive")] [assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)] [assembly: AssemblyVersion("1.2.3.0")] [module: UnverifiableCode] [module: RefSafetyRules(11)] namespace Microsoft.CodeAnalysis { [CompilerGenerated] [Embedded] internal sealed class EmbeddedAttribute : Attribute { } } namespace System.Runtime.CompilerServices { [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 GraphicsOverdrive { public static class AssetLoader { private static Texture2D s_cornerRibbonTex; private static Sprite s_cornerRibbonSprite; public static Sprite GetCornerRibbonSprite() { //IL_003b: Unknown result type (might be due to invalid IL or missing references) //IL_004a: Unknown result type (might be due to invalid IL or missing references) if ((Object)(object)s_cornerRibbonSprite != (Object)null) { return s_cornerRibbonSprite; } Texture2D cornerRibbonTexture = GetCornerRibbonTexture(); if ((Object)(object)cornerRibbonTexture != (Object)null) { s_cornerRibbonSprite = Sprite.Create(cornerRibbonTexture, new Rect(0f, 0f, (float)((Texture)cornerRibbonTexture).width, (float)((Texture)cornerRibbonTexture).height), new Vector2(0f, 1f)); ((Object)s_cornerRibbonSprite).hideFlags = (HideFlags)61; } return s_cornerRibbonSprite; } public static Texture2D GetCornerRibbonTexture() { if ((Object)(object)s_cornerRibbonTex != (Object)null) { return s_cornerRibbonTex; } s_cornerRibbonTex = LoadEmbeddedTexture("GraphicsOverdrive.wubarrk_red_ribbon.png"); if ((Object)(object)s_cornerRibbonTex == (Object)null) { s_cornerRibbonTex = CreateFallbackRibbon(); } ((Object)s_cornerRibbonTex).hideFlags = (HideFlags)61; return s_cornerRibbonTex; } private static Texture2D LoadEmbeddedTexture(string resourceName) { //IL_009a: Unknown result type (might be due to invalid IL or missing references) //IL_00a0: Expected O, but got Unknown Texture2D val = null; try { using Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName); if (stream == null) { return null; } byte[] array; using (MemoryStream memoryStream = new MemoryStream()) { stream.CopyTo(memoryStream); array = memoryStream.ToArray(); } MethodInfo methodInfo = (Type.GetType("UnityEngine.ImageConversion, UnityEngine.ImageConversionModule") ?? Type.GetType("UnityEngine.ImageConversion, UnityEngine"))?.GetMethod("LoadImage", new Type[2] { typeof(Texture2D), typeof(byte[]) }); if (methodInfo == null) { return null; } val = new Texture2D(2, 2, (TextureFormat)4, false); if ((bool)methodInfo.Invoke(null, new object[2] { val, array })) { ((Texture)val).wrapMode = (TextureWrapMode)1; ((Texture)val).filterMode = (FilterMode)1; return val; } } catch (Exception ex) { Plugin.LogWarning("[AssetLoader] Could not load embedded resource '" + resourceName + "': " + ex.Message); } if ((Object)(object)val != (Object)null) { Object.Destroy((Object)(object)val); } return null; } private static Texture2D CreateFallbackRibbon() { //IL_000c: Unknown result type (might be due to invalid IL or missing references) //IL_0012: Expected O, but got Unknown //IL_00a7: Unknown result type (might be due to invalid IL or missing references) //IL_009a: 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) Texture2D val = new Texture2D(180, 180, (TextureFormat)4, false); Color val2 = default(Color); ((Color)(ref val2))..ctor(0f, 0f, 0f, 0f); Color val3 = default(Color); ((Color)(ref val3))..ctor(0.55f, 0.05f, 0.08f, 0.95f); Color val4 = default(Color); ((Color)(ref val4))..ctor(0.85f, 0.68f, 0.25f, 1f); for (int i = 0; i < 180; i++) { for (int j = 0; j < 180; j++) { int num = j + i; if (num >= 80 && num <= 150) { val.SetPixel(j, i, (num <= 84 || num >= 146) ? val4 : val3); } else { val.SetPixel(j, i, val2); } } } val.Apply(); return val; } } public static class BasicMode { public enum RtLevel { Off, Blend, Ultra, Custom } private struct Recipe { public GraphicsConfig.RtShadowMode shadows; public GraphicsConfig.RtGiMode gi; public bool reflections; public bool water; public bool lights; public GraphicsConfig.RtResolution resolution; } public static readonly RtLevel[] Levels = new RtLevel[3] { RtLevel.Off, RtLevel.Blend, RtLevel.Ultra }; public static bool IsBasic { get { if (GraphicsConfig.MenuMode != null) { return GraphicsConfig.MenuMode.Value == GraphicsConfig.MenuModeChoice.Basic; } return true; } } public static bool RtOffered => GraphicsConfig.RtSunShadows != null; public static string Label(RtLevel level) { return level switch { RtLevel.Off => "No RT", RtLevel.Blend => "RT Blend", RtLevel.Ultra => "RT Ultra", _ => "Custom", }; } public static string Describe(RtLevel level) { return level switch { RtLevel.Off => "no ray tracing", RtLevel.Blend => "everything traced, the game's shadows blended in, half resolution", RtLevel.Ultra => "every ray-traced effect at full resolution, ray-traced shadows only", _ => "set in Expert mode", }; } private static Recipe Of(RtLevel level) { return level switch { RtLevel.Blend => new Recipe { shadows = GraphicsConfig.RtShadowMode.Hybrid, gi = GraphicsConfig.RtGiMode.Full, reflections = true, water = true, lights = true, resolution = GraphicsConfig.RtResolution.Half }, RtLevel.Ultra => new Recipe { shadows = GraphicsConfig.RtShadowMode.RtOnly, gi = GraphicsConfig.RtGiMode.Full, reflections = true, water = true, lights = true, resolution = GraphicsConfig.RtResolution.Full }, _ => new Recipe { shadows = GraphicsConfig.RtShadowMode.Off, gi = GraphicsConfig.RtGiMode.Off, resolution = GraphicsConfig.RtResolution.Half }, }; } public static RtLevel CurrentRtLevel() { if (!RtOffered) { return RtLevel.Off; } RtLevel[] levels = Levels; foreach (RtLevel rtLevel in levels) { Recipe recipe = Of(rtLevel); bool flag = recipe.gi != GraphicsConfig.RtGiMode.Off || recipe.reflections || recipe.water || recipe.lights; if (GraphicsConfig.RtSunShadows.Value == recipe.shadows && GraphicsConfig.RtGlobalIllumination.Value == recipe.gi && GraphicsConfig.RtReflections.Value == recipe.reflections && GraphicsConfig.RtWaterReflections.Value == recipe.water && GraphicsConfig.RtPointLights.Value == recipe.lights && (!flag || GraphicsConfig.RtLightingResolution.Value == recipe.resolution)) { return rtLevel; } } return RtLevel.Custom; } public static void ApplyRtLevel(RtLevel level) { if (RtOffered && level != RtLevel.Custom) { Recipe recipe = Of(level); GraphicsConfig.RtSunShadows.Value = recipe.shadows; GraphicsConfig.RtGlobalIllumination.Value = recipe.gi; GraphicsConfig.RtReflections.Value = recipe.reflections; GraphicsConfig.RtWaterReflections.Value = recipe.water; GraphicsConfig.RtPointLights.Value = recipe.lights; if (recipe.gi != GraphicsConfig.RtGiMode.Off || recipe.reflections || recipe.water || recipe.lights) { GraphicsConfig.RtLightingResolution.Value = recipe.resolution; } } } } public static class CameraDials { internal enum PresetField { FieldOfView, Zoom, Shake } private sealed class Slot { public float Game; public float Written = float.NaN; public float Dial = float.NaN; public bool Yielding; public float Theirs; public bool Logged; } private const int Fov = 0; private const int Zoom = 1; private const int Boat = 2; private const int Shake = 3; private const int Slots = 4; private static readonly string[] s_names = new string[4] { "Field of view", "Zoom limit on foot", "Zoom limit on a boat", "Screen shake" }; private static readonly float[] s_default = new float[4] { 65f, 8f, 16f, 1f }; private static readonly Slot[] s_slots = (from i in Enumerable.Range(0, 4) select new Slot { Game = s_default[i] }).ToArray(); private static GameCamera s_camera; private static bool s_summaryLogged; private static string s_otherMods; private static string s_status = ""; private static bool s_statusDirty = true; private static GraphicsConfig.CameraControlMode s_statusMode; private static Vector4 s_loggedVerbose = new Vector4(float.NaN, 0f, 0f, 0f); private static GraphicsConfig.CameraControlMode Mode { get { if (!GraphicsConfig.MasterEnable.Value) { return GraphicsConfig.CameraControlMode.Off; } if (GraphicsConfig.CameraControl == null) { return GraphicsConfig.CameraControlMode.Auto; } return GraphicsConfig.CameraControl.Value; } } public static string StatusLine { get { GraphicsConfig.CameraControlMode mode = Mode; if (s_statusDirty || mode != s_statusMode) { s_statusDirty = false; s_statusMode = mode; s_status = BuildStatus(mode); } return s_status; } } public static string OtherCameraMods => s_otherMods ?? (s_otherMods = FindOtherCameraMods()); private static bool Near(float a, float b) { return Mathf.Abs(a - b) <= 0.001f; } internal static void OnCameraAwake(GameCamera cam) { if (!((Object)(object)cam == (Object)null)) { Begin(cam); s_slots[0].Game = cam.m_fov; s_slots[1].Game = cam.m_maxDistance; s_slots[2].Game = cam.m_maxDistanceBoat; s_slots[3].Game = cam.m_shakeMovement; } } private static void Begin(GameCamera cam) { s_camera = cam; s_summaryLogged = false; s_statusDirty = true; for (int i = 0; i < 4; i++) { Slot obj = s_slots[i]; obj.Game = s_default[i]; obj.Written = float.NaN; obj.Dial = float.NaN; obj.Yielding = false; } } internal static void Apply(GameCamera cam) { //IL_0110: Unknown result type (might be due to invalid IL or missing references) //IL_0111: Unknown result type (might be due to invalid IL or missing references) //IL_011d: Unknown result type (might be due to invalid IL or missing references) //IL_011e: Unknown result type (might be due to invalid IL or missing references) //IL_0130: Unknown result type (might be due to invalid IL or missing references) //IL_013e: Unknown result type (might be due to invalid IL or missing references) //IL_014c: Unknown result type (might be due to invalid IL or missing references) //IL_015a: Unknown result type (might be due to invalid IL or missing references) if ((Object)(object)cam == (Object)null) { return; } if (cam != s_camera) { Begin(cam); } GraphicsConfig.CameraControlMode mode = Mode; if (!s_summaryLogged) { LogSummary(cam, mode); } if (mode == GraphicsConfig.CameraControlMode.Off) { Release(cam); return; } if ((!(cam.m_fovInertia > 0f) || Mathf.Approximately(cam.m_fovTarget, cam.m_fov)) && Keep(0, ref cam.m_fov, GraphicsConfig.CameraFieldOfView.Value, mode) && cam.m_fovBase != 0f) { cam.m_fovBase = cam.m_fov; cam.m_fovTarget = cam.m_fov; } float value = GraphicsConfig.CameraMaxDistance.Value; Keep(1, ref cam.m_maxDistance, value, mode); Keep(2, ref cam.m_maxDistanceBoat, value * 2f, mode); Keep(3, ref cam.m_shakeMovement, GraphicsConfig.CameraShakeScale.Value, mode); if (GraphicsConfig.VerboseLogging.Value) { Vector4 val = default(Vector4); ((Vector4)(ref val))..ctor(cam.m_fov, cam.m_maxDistance, cam.m_maxDistanceBoat, cam.m_shakeMovement); if (val != s_loggedVerbose) { s_loggedVerbose = val; Plugin.LogInfo($"[Camera] in force: field of view {val.x:0.#}, zoom {val.y:0.#} m (boat {val.z:0.#} m), shake {val.w:0.##}x; {StatusLine}"); } } } internal static void Release(GameCamera cam) { if (!((Object)(object)cam == (Object)null) && cam == s_camera) { if (ReleaseOne(0, ref cam.m_fov) && cam.m_fovBase != 0f) { cam.m_fovBase = cam.m_fov; cam.m_fovTarget = cam.m_fov; } ReleaseOne(1, ref cam.m_maxDistance); ReleaseOne(2, ref cam.m_maxDistanceBoat); ReleaseOne(3, ref cam.m_shakeMovement); } } private static bool ReleaseOne(int i, ref float field) { Slot slot = s_slots[i]; bool result = false; if (!float.IsNaN(slot.Written) && Near(field, slot.Written)) { field = slot.Game; result = true; } if (!float.IsNaN(slot.Written) || slot.Yielding) { s_statusDirty = true; } slot.Written = float.NaN; slot.Dial = float.NaN; slot.Yielding = false; return result; } private static bool Keep(int i, ref float field, float want, GraphicsConfig.CameraControlMode mode) { Slot slot = s_slots[i]; float num = field; bool flag = !float.IsNaN(slot.Dial) && !Near(slot.Dial, want); slot.Dial = want; bool flag2 = !float.IsNaN(slot.Written) && Near(num, slot.Written); if (mode == GraphicsConfig.CameraControlMode.Auto && !flag2 && !Near(num, slot.Game) && !Near(num, want)) { if (!slot.Yielding) { Yield(i, num, slot); } slot.Yielding = true; if (!flag) { return false; } } if (slot.Yielding) { slot.Yielding = false; s_statusDirty = true; } bool flag3 = mode != GraphicsConfig.CameraControlMode.On && Near(want, s_default[i]); if (flag3 && float.IsNaN(slot.Written)) { return false; } float num2 = (flag3 ? slot.Game : want); float num3 = (flag3 ? float.NaN : want); if (!Near(slot.Written, num3) && (!float.IsNaN(slot.Written) || !float.IsNaN(num3))) { s_statusDirty = true; } slot.Written = num3; if (Near(num, num2)) { return false; } field = num2; return true; } private static void Yield(int i, float theirs, Slot s) { s.Theirs = theirs; s_statusDirty = true; if (!s.Logged) { s.Logged = true; string text = (float.IsNaN(s.Written) ? "" : (", this mod's last value " + Fmt(i, s.Written))); Plugin.LogInfo("[Camera] " + s_names[i] + ": another mod set it to " + Fmt(i, theirs) + " (the game's own value is " + Fmt(i, s.Game) + text + "), so Graphics Overdrive leaves it to that mod. Moving the dial applies it once; [7 - Post-FX & Camera] CameraControl = On makes the dials always win, Off keeps this mod away from the camera. " + ((OtherCameraMods.Length > 0) ? ("Plugins that patch GameCamera: " + OtherCameraMods + ".") : "No other plugin's patch on GameCamera was found (it may write the field from its own Update).")); } } private static string Fmt(int i, float v) { return i switch { 3 => $"{v:0.##}x", 0 => $"{v:0.#} degrees", _ => $"{v:0.#} m", }; } internal static bool PresetMayWrite(PresetField field) { switch ((GraphicsConfig.CameraControl != null) ? GraphicsConfig.CameraControl.Value : GraphicsConfig.CameraControlMode.Auto) { case GraphicsConfig.CameraControlMode.Off: return false; case GraphicsConfig.CameraControlMode.On: return true; default: switch (field) { case PresetField.FieldOfView: return !s_slots[0].Yielding; case PresetField.Zoom: if (!s_slots[1].Yielding) { return !s_slots[2].Yielding; } return false; default: return !s_slots[3].Yielding; } } } private static string BuildStatus(GraphicsConfig.CameraControlMode mode) { string text = mode switch { GraphicsConfig.CameraControlMode.Off => "Off", GraphicsConfig.CameraControlMode.On => "On", _ => "Auto", }; if (mode == GraphicsConfig.CameraControlMode.Off) { return text + ": Graphics Overdrive leaves the camera to the game and other mods"; } if ((Object)(object)s_camera == (Object)null) { return text + ": waiting for the game camera"; } List<string> list = new List<string>(); List<string> list2 = new List<string>(); for (int i = 0; i < 4; i++) { if (s_slots[i].Yielding) { list2.Add(s_names[i].ToLowerInvariant()); } else if (!float.IsNaN(s_slots[i].Written)) { list.Add(s_names[i].ToLowerInvariant()); } } string text2 = text + ": "; text2 += ((list.Count > 0) ? ("Graphics Overdrive sets the " + string.Join(", ", list)) : "no camera value is set by Graphics Overdrive (the dials sit at the game's own values)"); if (list2.Count > 0) { text2 = text2 + "; another mod owns the " + string.Join(", ", list2); } return text2; } private static void LogSummary(GameCamera cam, GraphicsConfig.CameraControlMode mode) { s_summaryLogged = true; try { List<string> list = new List<string>(); Camera component = ((Component)cam).GetComponent<Camera>(); Camera[] array = (Camera[])(object)new Camera[Camera.allCamerasCount]; int allCameras = Camera.GetAllCameras(array); for (int i = 0; i < allCameras; i++) { Camera val = array[i]; if (!((Object)(object)val == (Object)null) && !((Object)(object)val == (Object)(object)component) && !((Object)(object)val == (Object)(object)cam.m_skyCamera) && (!((Object)(object)component != (Object)null) || !((Component)val).transform.IsChildOf(((Component)component).transform))) { list.Add(string.Format("{0} (depth {1:0.#}, {2}{3})", ((Object)val).name, val.depth, ((Object)(object)val.targetTexture != (Object)null) ? ("texture " + ((Texture)val.targetTexture).width + "x" + ((Texture)val.targetTexture).height) : "screen", ((Component)val).CompareTag("MainCamera") ? ", tagged MainCamera" : "")); } } Plugin.LogInfo($"[Camera] Game camera ready: CameraControl = {mode}; dials: field of view {GraphicsConfig.CameraFieldOfView.Value:0.#}, zoom limit {GraphicsConfig.CameraMaxDistance.Value:0.#} m, " + $"shake {GraphicsConfig.CameraShakeScale.Value:0.##}x (the game's own: {s_slots[0].Game:0.#}, {s_slots[1].Game:0.#} m, {s_slots[3].Game:0.##}x; a dial at the game's value is never written). " + ((OtherCameraMods.Length > 0) ? ("Plugins that patch GameCamera: " + OtherCameraMods + ". ") : "No other plugin patches GameCamera. ") + ((list.Count > 0) ? ("Other enabled cameras: " + string.Join("; ", list) + ".") : "No other enabled camera.")); } catch (Exception ex) { Plugin.LogWarning("[Camera] Could not list the cameras around the game camera: " + ex.Message); } } private static string FindOtherCameraMods() { try { SortedSet<string> sortedSet = new SortedSet<string>(); foreach (MethodBase allPatchedMethod in Harmony.GetAllPatchedMethods()) { if (allPatchedMethod.DeclaringType != typeof(GameCamera)) { continue; } Patches patchInfo = Harmony.GetPatchInfo(allPatchedMethod); if (patchInfo == null) { continue; } foreach (string owner in patchInfo.Owners) { if (owner != "com.wubarrk.graphicsoverdrive") { sortedSet.Add(owner); } } } PluginInfo value; return string.Join(", ", sortedSet.Select((string owner) => (!Chainloader.PluginInfos.TryGetValue(owner, out value)) ? owner : (value.Metadata.Name + " (" + owner + ")"))); } catch (Exception ex) { Plugin.LogWarning("[Camera] Could not list the plugins that patch GameCamera: " + ex.Message); return string.Empty; } } } public static class DisplayBasics { public enum WindowChoice { Fullscreen, Windowed } public struct Choice { public int Width; public int Height; public WindowChoice Window; public override string ToString() { return string.Format("{0} x {1} {2}", Width, Height, (Window == WindowChoice.Fullscreen) ? "fullscreen" : "windowed"); } } public interface IDisplayHost { Resolution[] Modes { get; } Resolution Desktop { get; } int Width { get; } int Height { get; } FullScreenMode Mode { get; } bool FrameGenOn { get; } string BlockedReason { get; } float Now { get; } void SetResolution(int width, int height, FullScreenMode mode, RefreshRate refresh); int GetPref(string key, int fallback); void SetPref(string key, int value); void SavePrefs(); bool GetPresent(out int fpsLimit, out bool vsync); void SetPresent(int fpsLimit, bool vsync); } private sealed class UnityHost : IDisplayHost { public Resolution[] Modes => Screen.resolutions; public Resolution Desktop { get { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_0005: 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) Resolution currentResolution = Screen.currentResolution; if (((Resolution)(ref currentResolution)).width <= 0 || ((Resolution)(ref currentResolution)).height <= 0) { ((Resolution)(ref currentResolution)).width = Display.main.systemWidth; ((Resolution)(ref currentResolution)).height = Display.main.systemHeight; } return currentResolution; } } public int Width => Screen.width; public int Height => Screen.height; public FullScreenMode Mode => Screen.fullScreenMode; public bool FrameGenOn { get { if ((!NativeBridge.FgPresenterLoaded && string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GO_FG_LAYER_LIB"))) || GraphicsConfig.NativeFrameGeneration == null) { return false; } GraphicsConfig.FrameGenChoice value = GraphicsConfig.NativeFrameGeneration.Value; if (value != GraphicsConfig.FrameGenChoice.FSR3) { return value == GraphicsConfig.FrameGenChoice.DLSS; } return true; } } public float Now => Time.realtimeSinceStartup; public string BlockedReason { get { if (!GraphicsSettingsManager.CanChangePresentSettings()) { return "This platform does not let the game change its display."; } if (!((Object)(object)Settings.instance != (Object)null)) { return ""; } return "Close the game's Settings window first: it keeps its own copy of these values."; } } public void SetResolution(int width, int height, FullScreenMode mode, RefreshRate refresh) { //IL_0002: Unknown result type (might be due to invalid IL or missing references) //IL_0003: Unknown result type (might be due to invalid IL or missing references) Screen.SetResolution(width, height, mode, refresh); } public int GetPref(string key, int fallback) { return PlayerPrefs.GetInt(key, fallback); } public void SetPref(string key, int value) { PlayerPrefs.SetInt(key, value); } public void SavePrefs() { PlayerPrefs.Save(); } public bool GetPresent(out int fpsLimit, out bool vsync) { //IL_001f: Unknown result type (might be due to invalid IL or missing references) //IL_0024: Unknown result type (might be due to invalid IL or missing references) //IL_0029: Unknown result type (might be due to invalid IL or missing references) //IL_002b: Unknown result type (might be due to invalid IL or missing references) //IL_0033: Unknown result type (might be due to invalid IL or missing references) fpsLimit = 361; vsync = false; GraphicsSettingsManager instance = GraphicsSettingsManager.Instance; if ((Object)(object)instance == (Object)null) { return false; } try { PresentSettingsState presentSettings = instance.GetCurrentSettingsWithCurrentPresetApplied(false, false).m_presentSettings; fpsLimit = presentSettings.m_fpsLimit; vsync = presentSettings.m_vsync; return true; } catch (Exception) { return false; } } public unsafe void SetPresent(int fpsLimit, bool vsync) { //IL_000c: Unknown result type (might be due to invalid IL or missing references) //IL_0011: Unknown result type (might be due to invalid IL or missing references) GraphicsSettingsManager instance = GraphicsSettingsManager.Instance; GraphicsSettingsState currentPlayerSettingsRaw = Unsafe.Read<GraphicsSettingsState>((void*)instance.CurrentPlayerSettingsRaw); currentPlayerSettingsRaw.m_presentSettings.m_fpsLimit = fpsLimit; currentPlayerSettingsRaw.m_presentSettings.m_vsync = vsync; GraphicsModeConfiguration currentGraphicsModeConfiguration = instance.GetCurrentGraphicsModeConfiguration(false); GraphicsSettingsPreset val = currentGraphicsModeConfiguration.GetPresetByID(instance.CurrentPresetID); if ((Object)(object)val == (Object)null && !currentGraphicsModeConfiguration.HasCustomPreset) { val = currentGraphicsModeConfiguration.DefaultPreset; } if ((Object)(object)val != (Object)null) { instance.SaveAndApplyGraphicsSettingsWithPreset(ref currentPlayerSettingsRaw, val.m_type); } else { instance.SaveAndApplyGraphicsSettingsCustom(ref currentPlayerSettingsRaw); } PlatformPrefs.Save(); } } public const float ConfirmSeconds = 10f; public const int FpsMin = 30; public const int FpsUnlimited = 361; private const int MinWidth = 1280; private const int MinHeight = 720; private const string PrefWidth = "Screenmanager Resolution Width"; private const string PrefHeight = "Screenmanager Resolution Height"; private const string PrefUseNative = "Screenmanager Resolution Use Native"; private const string PrefFullscreenMode = "Screenmanager Fullscreen mode"; private const float ListSeconds = 2f; private static IDisplayHost s_host = new UnityHost(); private static Choice s_pending; private static bool s_hasPending; private static Choice s_applied; private static int s_oldW; private static int s_oldH; private static FullScreenMode s_oldMode; private static RefreshRate s_oldRefresh; private static float s_deadline; private static List<Resolution> s_list; private static float s_listAt = -100f; private static bool s_listPresenter; public static IDisplayHost Host { get { return s_host; } set { s_host = value ?? new UnityHost(); Confirming = false; s_hasPending = false; s_list = null; LastResult = ""; } } public static string LastResult { get; private set; } = ""; public static bool Confirming { get; private set; } public static float Remaining { get { if (!Confirming) { return 0f; } return Math.Max(0f, s_deadline - Host.Now); } } private static bool GuardEnabled { get { if (GraphicsConfig.FixUnsupportedResolution != null) { return GraphicsConfig.FixUnsupportedResolution.Value; } return true; } } public static Choice Current => new Choice { Width = Host.Width, Height = Host.Height, Window = (((int)Host.Mode == 3) ? WindowChoice.Windowed : WindowChoice.Fullscreen) }; public static Choice Pending { get { if (!s_hasPending) { return Current; } return s_pending; } } public static bool HasPendingChange { get { if (s_hasPending) { return !Same(s_pending, Current); } return false; } } public static int HiddenInFullscreen => AllModes().Count - Offered(WindowChoice.Fullscreen).Count; private static bool Same(Choice a, Choice b) { if (a.Width == b.Width && a.Height == b.Height) { return a.Window == b.Window; } return false; } public static List<Resolution> Offered(WindowChoice window) { //IL_0023: Unknown result type (might be due to invalid IL or missing references) //IL_0028: Unknown result type (might be due to invalid IL or missing references) //IL_0042: Unknown result type (might be due to invalid IL or missing references) List<Resolution> list = AllModes(); if (window == WindowChoice.Windowed) { return list; } List<Resolution> list2 = new List<Resolution>(list.Count); foreach (Resolution item in list) { Resolution current = item; if (Allowed(((Resolution)(ref current)).width, ((Resolution)(ref current)).height, window, out var _)) { list2.Add(current); } } return list2; } private static List<Resolution> AllModes() { //IL_005e: Unknown result type (might be due to invalid IL or missing references) //IL_0063: Unknown result type (might be due to invalid IL or missing references) //IL_011c: Unknown result type (might be due to invalid IL or missing references) //IL_0145: Unknown result type (might be due to invalid IL or missing references) //IL_014a: Unknown result type (might be due to invalid IL or missing references) //IL_014e: Unknown result type (might be due to invalid IL or missing references) //IL_0158: Unknown result type (might be due to invalid IL or missing references) //IL_00c8: Unknown result type (might be due to invalid IL or missing references) //IL_00a5: Unknown result type (might be due to invalid IL or missing references) //IL_00aa: Unknown result type (might be due to invalid IL or missing references) //IL_00b5: 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) float now = Host.Now; bool frameGenOn = Host.FrameGenOn; if (s_list != null && now - s_listAt < 2f && frameGenOn == s_listPresenter) { return s_list; } Dictionary<long, Resolution> dictionary = new Dictionary<long, Resolution>(); Resolution[] array = (Resolution[])(((object)Host.Modes) ?? ((object)new Resolution[0])); for (int i = 0; i < array.Length; i++) { Resolution value = array[i]; if (((Resolution)(ref value)).width < 1280 || ((Resolution)(ref value)).height < 720) { continue; } long key = ((long)((Resolution)(ref value)).width << 32) | (uint)((Resolution)(ref value)).height; if (dictionary.TryGetValue(key, out var value2)) { RefreshRate refreshRateRatio = ((Resolution)(ref value)).refreshRateRatio; double value3 = ((RefreshRate)(ref refreshRateRatio)).value; refreshRateRatio = ((Resolution)(ref value2)).refreshRateRatio; if (!(value3 > ((RefreshRate)(ref refreshRateRatio)).value)) { continue; } } dictionary[key] = value; } List<Resolution> list = new List<Resolution>(dictionary.Values); Choice current = Current; if (current.Width > 0 && !dictionary.ContainsKey(((long)current.Width << 32) | (uint)current.Height)) { Resolution item = default(Resolution); ((Resolution)(ref item)).width = current.Width; ((Resolution)(ref item)).height = current.Height; Resolution desktop = Host.Desktop; ((Resolution)(ref item)).refreshRateRatio = ((Resolution)(ref desktop)).refreshRateRatio; list.Add(item); } list.Sort((Resolution a, Resolution b) => (((Resolution)(ref a)).width == ((Resolution)(ref b)).width) ? ((Resolution)(ref b)).height.CompareTo(((Resolution)(ref a)).height) : ((Resolution)(ref b)).width.CompareTo(((Resolution)(ref a)).width)); s_list = list; s_listAt = now; s_listPresenter = frameGenOn; return list; } public static bool Allowed(int width, int height, WindowChoice window, out string why) { //IL_0025: Unknown result type (might be due to invalid IL or missing references) //IL_002a: Unknown result type (might be due to invalid IL or missing references) why = ""; if (window == WindowChoice.Windowed || !GuardEnabled || !Host.FrameGenOn) { return true; } Resolution desktop = Host.Desktop; if (((Resolution)(ref desktop)).width <= 0 || ((Resolution)(ref desktop)).height <= 0 || !ShapeDiffers(width, height, ((Resolution)(ref desktop)).width, ((Resolution)(ref desktop)).height)) { return true; } why = $"{width} x {height} is another shape than the display ({((Resolution)(ref desktop)).width} x {((Resolution)(ref desktop)).height}): with frame generation on that crashed the game's graphics driver"; return false; } private static bool ShapeDiffers(int w, int h, int nw, int nh) { return Math.Abs((double)w / (double)h - (double)nw / (double)nh) > 0.01; } public static void SelectResolution(int width, int height) { if (!Confirming) { Choice pending = Pending; pending.Width = width; pending.Height = height; s_pending = pending; s_hasPending = true; } } public static void SelectWindow(WindowChoice window) { //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 (!Confirming) { Choice pending = Pending; pending.Window = window; if (!Allowed(pending.Width, pending.Height, window, out var _)) { Resolution desktop = Host.Desktop; LastResult = $"Fullscreen cannot use {pending.Width} x {pending.Height} while frame generation is loaded (another shape than the display): " + $"{((Resolution)(ref desktop)).width} x {((Resolution)(ref desktop)).height} picked."; pending.Width = ((Resolution)(ref desktop)).width; pending.Height = ((Resolution)(ref desktop)).height; } s_pending = pending; s_hasPending = true; } } public static void ClearPending() { if (!Confirming) { s_hasPending = false; } } public static bool CanApply(out string why) { why = Host.BlockedReason; if (why.Length > 0) { return false; } if (Confirming) { why = "Answer the question first: keep or revert."; return false; } if (!HasPendingChange) { why = "Pick a resolution or a window mode first."; return false; } Choice choice = s_pending; return Allowed(choice.Width, choice.Height, choice.Window, out why); } public static bool Apply() { //IL_003a: Unknown result type (might be due to invalid IL or missing references) //IL_003f: Unknown result type (might be due to invalid IL or missing references) //IL_0049: 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_0051: 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) //IL_0068: Unknown result type (might be due to invalid IL or missing references) //IL_007a: Unknown result type (might be due to invalid IL or missing references) //IL_0087: 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) if (!CanApply(out var why)) { LastResult = why; return false; } Choice choice = s_pending; s_oldW = Host.Width; s_oldH = Host.Height; s_oldMode = Host.Mode; Resolution desktop = Host.Desktop; s_oldRefresh = ((Resolution)(ref desktop)).refreshRateRatio; FullScreenMode mode = (FullScreenMode)((choice.Window != WindowChoice.Windowed) ? 1 : 3); try { Host.SetResolution(choice.Width, choice.Height, mode, RefreshFor(choice.Width, choice.Height)); } catch (Exception ex) { LastResult = "The game refused the change: " + ex.Message; Plugin.LogWarning("[Display] SetResolution failed: " + ex.Message); return false; } s_applied = choice; Confirming = true; s_deadline = Host.Now + 10f; s_list = null; LastResult = ""; Plugin.LogInfo($"[Display] Applied {choice} (was {s_oldW} x {s_oldH} {s_oldMode}): kept or reverted within {10f:0} s."); return true; } internal static RefreshRate RefreshFor(int w, int h) { //IL_0005: Unknown result type (might be due to invalid IL or missing references) //IL_000a: 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_0048: Unknown result type (might be due to invalid IL or missing references) //IL_0021: Unknown result type (might be due to invalid IL or missing references) //IL_0026: Unknown result type (might be due to invalid IL or missing references) //IL_003b: Unknown result type (might be due to invalid IL or missing references) //IL_0072: Unknown result type (might be due to invalid IL or missing references) //IL_0077: Unknown result type (might be due to invalid IL or missing references) //IL_00c5: Unknown result type (might be due to invalid IL or missing references) //IL_008f: Unknown result type (might be due to invalid IL or missing references) //IL_0094: 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_00a6: Unknown result type (might be due to invalid IL or missing references) //IL_00a9: Unknown result type (might be due to invalid IL or missing references) //IL_00ae: Unknown result type (might be due to invalid IL or missing references) Resolution desktop = Host.Desktop; RefreshRate refreshRateRatio; if (((Resolution)(ref desktop)).width == w && ((Resolution)(ref desktop)).height == h) { refreshRateRatio = ((Resolution)(ref desktop)).refreshRateRatio; if (((RefreshRate)(ref refreshRateRatio)).value > 1.0) { return ((Resolution)(ref desktop)).refreshRateRatio; } } RefreshRate refreshRateRatio2 = ((Resolution)(ref desktop)).refreshRateRatio; double num = -1.0; Resolution[] array = (Resolution[])(((object)Host.Modes) ?? ((object)new Resolution[0])); for (int i = 0; i < array.Length; i++) { Resolution val = array[i]; if (((Resolution)(ref val)).width == w && ((Resolution)(ref val)).height == h) { refreshRateRatio = ((Resolution)(ref val)).refreshRateRatio; if (((RefreshRate)(ref refreshRateRatio)).value > num) { refreshRateRatio2 = ((Resolution)(ref val)).refreshRateRatio; refreshRateRatio = ((Resolution)(ref val)).refreshRateRatio; num = ((RefreshRate)(ref refreshRateRatio)).value; } } } return refreshRateRatio2; } public static void Keep() { if (Confirming) { Confirming = false; Choice choice = s_applied; try { Host.SetPref("Screenmanager Resolution Width", choice.Width); Host.SetPref("Screenmanager Resolution Height", choice.Height); Host.SetPref("Screenmanager Fullscreen mode", (choice.Window != WindowChoice.Windowed) ? 1 : 3); Host.SetPref("Screenmanager Resolution Use Native", 0); Host.SavePrefs(); } catch (Exception ex) { Plugin.LogWarning("[Display] Could not save the screen state: " + ex.Message); } s_hasPending = false; s_list = null; LastResult = $"Kept {choice}."; Plugin.LogInfo($"[Display] Kept {choice}."); } } public static void Revert(string reason) { //IL_009d: Unknown result type (might be due to invalid IL or missing references) //IL_001d: Unknown result type (might be due to invalid IL or missing references) //IL_0022: Unknown result type (might be due to invalid IL or missing references) if (Confirming) { Confirming = false; try { Host.SetResolution(s_oldW, s_oldH, s_oldMode, s_oldRefresh); } catch (Exception ex) { Plugin.LogWarning("[Display] Could not switch back: " + ex.Message); } s_hasPending = false; s_list = null; LastResult = $"Reverted to {s_oldW} x {s_oldH} ({reason})."; Plugin.LogInfo($"[Display] Reverted to {s_oldW} x {s_oldH} {s_oldMode} ({reason})."); } } public static void Tick() { if (Confirming && Host.Now >= s_deadline) { Revert($"no answer in {10f:0} s"); } } public static bool TryGetPresent(out int fpsLimit, out bool vsync) { if (!Host.GetPresent(out var fpsLimit2, out vsync)) { fpsLimit = 361; return false; } fpsLimit = ((fpsLimit2 < 30 || fpsLimit2 > 360) ? 361 : fpsLimit2); return true; } public static void SetFpsLimit(int limit) { if (TryGetPresent(out var _, out var vsync) && Host.BlockedReason.Length <= 0) { Present(Mathf.Clamp(limit, 30, 361), vsync); } } public static void SetVsync(bool on) { if (TryGetPresent(out var fpsLimit, out var _) && Host.BlockedReason.Length <= 0) { Present(fpsLimit, on); } } private static void Present(int fps, bool vsync) { try { Host.SetPresent(fps, vsync); LastResult = ""; Plugin.LogInfo("[Display] FPS limit " + ((fps >= 361) ? "unlimited" : fps.ToString()) + ", Vsync " + (vsync ? "on" : "off") + " saved through the game's graphics settings."); } catch (Exception ex) { LastResult = "The game's graphics settings refused the change: " + ex.Message; Plugin.LogWarning("[Display] Could not save the FPS limit / Vsync: " + ex.Message); } } } public static class DisplayPacing { private static FieldRef<PresentManager, bool> s_vsyncField; private static bool s_vsyncKnown; private static bool s_vsyncOn; private static string s_logged = ""; private static bool s_limited; private static float s_freeFps; private static float s_freeFpsAt = -1000f; private static string s_queuedLogged = ""; private static bool s_capResting; private static int s_capSeen = -1; private static int s_capLogs; private static float s_dtAvg; private static float s_belowFor; private static float s_aboveFor; public static int DisplayHz { get { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_0005: Unknown result type (might be due to invalid IL or missing references) //IL_0008: Unknown result type (might be due to invalid IL or missing references) //IL_000d: Unknown result type (might be due to invalid IL or missing references) Resolution currentResolution = Screen.currentResolution; RefreshRate refreshRateRatio = ((Resolution)(ref currentResolution)).refreshRateRatio; double value = ((RefreshRate)(ref refreshRateRatio)).value; if (!(value > 1.0)) { return 60; } return Mathf.RoundToInt((float)value); } } public static int LimitSetting { get { if (GraphicsConfig.FrameGenLimit == null) { return 0; } return GraphicsConfig.FrameGenLimit.Value; } } public static int FrameGenTargetFps { get { if (LimitSetting <= 0 || VsyncOn) { return DisplayHz; } return LimitSetting; } } public static bool Half { get { if (GraphicsConfig.HalfRefreshRate != null) { return GraphicsConfig.HalfRefreshRate.Value; } return false; } } public static bool FillChosen { get { if (GraphicsConfig.FrameGenPacing != null) { return GraphicsConfig.FrameGenPacing.Value == GraphicsConfig.FrameGenPacingMode.Fill; } return false; } } public static bool FillActive { get { if (NativeUpscaler.FrameGenActive && FillChosen) { return NativeUpscaler.SelectedFrameGen == 1; } return false; } } public static bool VsyncOn { get { if (!s_vsyncKnown) { return QualitySettings.vSyncCount > 0; } return s_vsyncOn; } } public static int QueuedFrames { get { int num = ((GraphicsConfig.MaxQueuedFrames != null) ? GraphicsConfig.MaxQueuedFrames.Value : 2); if (num >= 2 || !Paced) { return num; } return 2; } } public static bool Paced { get { if (!s_limited && !VsyncOn && !NativeUpscaler.FrameGenActive && Application.targetFrameRate <= 0) { return QualitySettings.vSyncCount > 0; } return true; } } public static bool QueuedFramesRaised { get { if (GraphicsConfig.MaxQueuedFrames != null && GraphicsConfig.MaxQueuedFrames.Value < 2) { return Paced; } return false; } } public static int FreeFps { get { if (!(s_freeFps > 0f)) { return 0; } return Mathf.RoundToInt(s_freeFps); } } public static int FrameGenRenderCap { get { if (VsyncOn) { return Math.Max(15, DisplayHz / 2); } int num = ((FillChosen && GraphicsConfig.NativeFrameGeneration != null && GraphicsConfig.NativeFrameGeneration.Value == GraphicsConfig.FrameGenChoice.FSR3) ? FrameGenTargetFps : (FrameGenTargetFps / 2)); if (Half) { num = Math.Min(num, Math.Max(15, DisplayHz / 2)); } return Math.Max(15, num); } } public static bool CapResting { get { if (s_capResting) { return NativeUpscaler.FrameGenActive; } return false; } } public static void AfterLimit(int limit) { s_limited = limit > 0; int queuedFrames = QueuedFrames; if (QualitySettings.maxQueuedFrames != queuedFrames) { QualitySettings.maxQueuedFrames = queuedFrames; } string text = $"{queuedFrames}|{QueuedFramesRaised}"; if (text != s_queuedLogged) { s_queuedLogged = text; if (QueuedFramesRaised) { Plugin.LogInfo("[Display] frames the CPU may queue: 2 while a frame limit, Vsync or frame generation paces the frames (the setting is 1, which gives uneven frames there)"); } else { Plugin.LogInfo($"[Display] frames the CPU may queue: {queuedFrames}"); } } float unscaledDeltaTime = Time.unscaledDeltaTime; if (!Paced && (Object)(object)Game.instance != (Object)null && !Game.IsPaused() && Application.isFocused && unscaledDeltaTime > 0.0005f && unscaledDeltaTime < 0.25f && !BenchmarkController.IsRunning) { float num = 1f / unscaledDeltaTime; s_freeFps = ((Time.realtimeSinceStartup - s_freeFpsAt < 5f && s_freeFps > 0f) ? Mathf.Lerp(s_freeFps, num, Mathf.Clamp01(unscaledDeltaTime / 2f)) : num); s_freeFpsAt = Time.realtimeSinceStartup; } } public static int RenderCap(bool vsyncOn) { int num = Math.Max(15, DisplayHz / 2); if (NativeUpscaler.FrameGenActive) { if (vsyncOn) { return -1; } int num2 = (FillActive ? FrameGenTargetFps : (FrameGenTargetFps / 2)); if (Half) { num2 = Math.Min(num2, num); } return Math.Max(15, num2); } if (!Half) { return -1; } return num; } public static void Apply(PresentManager manager, ref int limit) { if (manager != null) { if (s_vsyncField == null) { s_vsyncField = AccessTools.FieldRefAccess<PresentManager, bool>("m_setVSyncEnabled"); } s_vsyncOn = s_vsyncField.Invoke(manager); s_vsyncKnown = true; } int num = RenderCap(VsyncOn); int num2 = CapFor(num); if (num2 > 0) { limit = ((limit < 0) ? num2 : Math.Min(limit, num2)); } bool frameGenActive = NativeUpscaler.FrameGenActive; string text = $"{DisplayHz}|{frameGenActive}|{FillActive}|{FrameGenTargetFps}|{Half}|{VsyncOn}|{num}"; if (text != s_logged) { s_logged = text; string text2 = ((!frameGenActive) ? "off" : (FillActive ? $"on, fills up to {FrameGenTargetFps} fps shown (real frames first)" : $"on, one frame halfway, up to {FrameGenTargetFps} fps shown")); Plugin.LogInfo(string.Format("[Display] display {0} Hz, frame generation {1}, half refresh {2}, Vsync {3}: ", DisplayHz, text2, Half ? "on" : "off", VsyncOn ? "on" : "off") + ((num > 0) ? $"the game renders at most {num} fps" : "no cap from these settings") + " (the game's own limits still apply)"); } } private static int CapFor(int cap) { if (cap <= 0 || !NativeUpscaler.FrameGenActive) { s_capResting = false; s_capSeen = cap; s_belowFor = (s_aboveFor = 0f); s_dtAvg = 0f; return cap; } if (cap != s_capSeen) { s_capSeen = cap; s_capResting = false; s_belowFor = (s_aboveFor = 0f); s_dtAvg = 0f; } float unscaledDeltaTime = Time.unscaledDeltaTime; if ((Object)(object)Game.instance != (Object)null && !Game.IsPaused() && Application.isFocused && unscaledDeltaTime > 0.0005f && unscaledDeltaTime < 0.25f) { s_dtAvg = ((s_dtAvg > 0f) ? Mathf.Lerp(s_dtAvg, unscaledDeltaTime, Mathf.Clamp01(unscaledDeltaTime / 0.5f)) : unscaledDeltaTime); float num = 1f / s_dtAvg; if (!s_capResting) { s_belowFor = ((num < 0.97f * (float)cap) ? (s_belowFor + unscaledDeltaTime) : 0f); if (s_belowFor >= 1.5f) { s_capResting = true; s_aboveFor = 0f; if (s_capLogs++ < 12) { Plugin.LogInfo($"[Display] frame generation: the game renders about {num:0} fps, under the cap of {cap}: the cap rests and the presenter paces the frames (it comes back when the game reaches {cap})"); } } } else { s_aboveFor = ((num >= (float)cap) ? (s_aboveFor + unscaledDeltaTime) : 0f); if (s_aboveFor >= 0.3f) { s_capResting = false; s_belowFor = 0f; if (s_capLogs++ < 12) { Plugin.LogInfo($"[Display] frame generation: the game reaches {num:0} fps: the cap of {cap} is back"); } } } } if (!s_capResting) { return cap; } return -1; } public static string StatusText(bool rich) { int displayHz = DisplayHz; int num = RenderCap(VsyncOn); int frameGenTargetFps = FrameGenTargetFps; int limitSetting = LimitSetting; bool frameGenActive = NativeUpscaler.FrameGenActive; bool vsyncOn = VsyncOn; bool fillActive = FillActive; string text = string.Format("Display {0} Hz • Vsync {1}", displayHz, vsyncOn ? "on" : "off"); text = ((frameGenActive && fillActive) ? (text + $" • frame generation fills up to {frameGenTargetFps} fps shown, real frames first" + ((num > 0) ? ($" (the game renders up to {num}" + (CapResting ? "; it is under that now: the cap rests)" : ")")) : "")) : ((frameGenActive && vsyncOn) ? (text + $" • frame generation in step with the display: up to {displayHz / 2} rendered + {displayHz / 2} generated") : (frameGenActive ? (text + $" • frame generation limited to {frameGenTargetFps} fps shown: the game renders up to {num}" + (CapResting ? " (it is under that now: the cap rests)" : "")) : ((num <= 0) ? (text + " • no cap") : (text + $" • the game renders up to {num} fps (half refresh)"))))); string text2 = ""; if (frameGenActive && !vsyncOn && limitSetting > displayHz) { text2 = $"The limit is above the display's {displayHz} Hz: the extra frames tear."; } else if (frameGenActive && !vsyncOn && limitSetting == 0) { text2 = $"Limit 0 = the display's {displayHz} Hz. For a VRR display set it a few below (e.g. {Math.Max(30, displayHz - 3)}) so frames stay in its range."; } else if (frameGenActive && !vsyncOn && limitSetting > 0 && limitSetting < displayHz && displayHz % limitSetting != 0 && EvenLimits(displayHz).Length > 0) { text2 = $"Without VRR, {limitSetting} fps on {displayHz} Hz holds frames for uneven numbers of refreshes (judder): {EvenLimits(displayHz)} keep them even."; } else if (frameGenActive && vsyncOn && limitSetting > 0) { text2 = "With the game's Vsync on the limiter is not needed: the display's refresh rate sets the pace."; } else if (frameGenActive && !fillActive && FillChosen && NativeUpscaler.SelectedFrameGen == 2) { text2 = "DLSS Frame Generation paces one frame halfway (fill pacing is FSR 3 only)."; } else if (!frameGenActive && Half && !vsyncOn) { text2 = "With Vsync off this is a frame cap: smooth with VRR, otherwise switch the game's Vsync on for every second refresh."; } if (text2.Length == 0) { return text; } if (!rich) { return text + "\n" + text2; } return text + "\n<size=9><color=#9A8A6A>" + text2 + " In menus the game caps at 60 fps.</color></size>"; } public static string AdviceText(bool rich) { int freeFps = FreeFps; int frameGenRenderCap = FrameGenRenderCap; bool num = GraphicsConfig.NativeFrameGeneration != null && GraphicsConfig.NativeFrameGeneration.Value != GraphicsConfig.FrameGenChoice.Off; string text = ""; if (num && freeFps > 0 && (float)freeFps > (float)frameGenRenderCap * 1.1f) { text = $"Without frame generation the game rendered about {freeFps} fps here; with it, at most {frameGenRenderCap} real ones. It pays off when the game renders fewer than {frameGenRenderCap} by itself" + (VsyncOn ? "." : ": otherwise raise the limit or switch it off."); } string text2 = (QueuedFramesRaised ? "Frames the CPU may queue: 2 is used while a frame limit, Vsync or frame generation paces the frames (1 gives uneven frames there)." : ""); string text3 = ((text.Length > 0 && text2.Length > 0) ? (text + "\n" + text2) : (text + text2)); if (text3.Length == 0) { return ""; } if (!rich) { return text3; } return "<size=9><color=#C8A050>" + text3 + "</color></size>"; } private static string EvenLimits(int display) { List<int> list = new List<int>(); for (int i = 2; i <= 8; i++) { if (list.Count >= 2) { break; } if (display % i == 0 && display / i >= 30) { list.Add(display / i); } } return string.Join(" or ", list); } } public static class DisplayResolutionGuard { private const string PrefWidth = "Screenmanager Resolution Width"; private const string PrefHeight = "Screenmanager Resolution Height"; private const string PrefUseNative = "Screenmanager Resolution Use Native"; private const string PrefFullscreenMode = "Screenmanager Fullscreen mode"; private const float WatchSeconds = 20f; private const float CheckInterval = 0.25f; private const int MaxFixes = 3; private static int s_badW; private static int s_badH; private static Resolution s_best; private static FullScreenMode s_mode; private static string s_reason = ""; private static float s_watchUntil = -1f; private static float s_nextCheck; private static int s_fixes; public static string LastAction { get; private set; } = ""; private static bool Enabled { get { if (GraphicsConfig.FixUnsupportedResolution != null) { return GraphicsConfig.FixUnsupportedResolution.Value; } return true; } } private static bool PresenterLoaded => !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GO_FG_LAYER_LIB")); private static bool FrameGenOnAtStart { get { if (PresenterLoaded && GraphicsConfig.NativeFrameGeneration != null) { if (GraphicsConfig.NativeFrameGeneration.Value != GraphicsConfig.FrameGenChoice.FSR3) { return GraphicsConfig.NativeFrameGeneration.Value == GraphicsConfig.FrameGenChoice.DLSS; } return true; } return false; } } public static void OnLoad() { //IL_0018: Unknown result type (might be due to invalid IL or missing references) //IL_001d: Unknown result type (might be due to invalid IL or missing references) //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_007d: Unknown result type (might be due to invalid IL or missing references) //IL_00b2: Unknown result type (might be due to invalid IL or missing references) //IL_00ef: Unknown result type (might be due to invalid IL or missing references) //IL_00f4: Unknown result type (might be due to invalid IL or missing references) //IL_012f: Unknown result type (might be due to invalid IL or missing references) //IL_0131: Invalid comparison between Unknown and I4 //IL_0184: Unknown result type (might be due to invalid IL or missing references) //IL_0187: Unknown result type (might be due to invalid IL or missing references) //IL_01c5: Unknown result type (might be due to invalid IL or missing references) //IL_01c6: Unknown result type (might be due to invalid IL or missing references) try { int num = PlayerPrefs.GetInt("Screenmanager Resolution Width", 0); int num2 = PlayerPrefs.GetInt("Screenmanager Resolution Height", 0); FullScreenMode val = SavedMode(); bool flag = PlayerPrefs.GetInt("Screenmanager Resolution Use Native", 0) != 0; Resolution[] resolutions = Screen.resolutions; Resolution currentResolution = Screen.currentResolution; string text = (FrameGenOnAtStart ? "on at start" : (PresenterLoaded ? "off (presenter preloaded)" : "off")); string text2 = string.Format("saved {0}x{1} {2}{3}, window now {4}x{5} {6}, ", num, num2, val, flag ? " (use native)" : "", Screen.width, Screen.height, Screen.fullScreenMode); object[] obj = new object[5] { ((Resolution)(ref currentResolution)).width, ((Resolution)(ref currentResolution)).height, null, null, null }; RefreshRate refreshRateRatio = ((Resolution)(ref currentResolution)).refreshRateRatio; obj[2] = ((RefreshRate)(ref refreshRateRatio)).value; obj[3] = text; obj[4] = ModeList(); string text3 = text2 + string.Format("desktop {0}x{1} @ {2:0.##} Hz, frame generation {3}, display modes [{4}]", obj); Resolution native; string text4 = ((!Enabled) ? "off ([Display] FixUnsupportedResolution = false)" : (((int)val == 3) ? "windowed: any size is fine" : (flag ? "Unity uses the display's own size" : ((num > 0 && num2 > 0) ? ((resolutions == null || resolutions.Length == 0) ? "the display lists no modes: not checked" : ((!IsMode(resolutions, num, num2)) ? Replace(num, num2, val, Largest(resolutions), "not a mode of this display") : ((!FrameGenOnAtStart || !Native(resolutions, out native) || !ShapeDiffers(num, num2, ((Resolution)(ref native)).width, ((Resolution)(ref native)).height)) ? "a mode of this display: kept" : Replace(num, num2, val, native, $"another shape than the display ({((Resolution)(ref native)).width}x{((Resolution)(ref native)).height}) while frame generation is on")))) : "nothing saved")))); Plugin.LogInfo("[Display] Startup resolution check: " + text3 + " -> " + text4); } catch (Exception ex) { Plugin.LogWarning("[Display] Could not check the saved screen resolution: " + ex.Message); } } private static FullScreenMode SavedMode() { //IL_0014: Unknown result type (might be due to invalid IL or missing references) int num = PlayerPrefs.GetInt("Screenmanager Fullscreen mode", -1); if (num < 0 || num > 3) { return Screen.fullScreenMode; } return (FullScreenMode)num; } public static void Tick() { if (s_watchUntil < 0f) { return; } float realtimeSinceStartup = Time.realtimeSinceStartup; if (realtimeSinceStartup > s_watchUntil) { s_watchUntil = -1f; } else { if (realtimeSinceStartup < s_nextCheck) { return; } s_nextCheck = realtimeSinceStartup + 0.25f; try { if (Enabled && s_fixes < 3 && Screen.width == s_badW && Screen.height == s_badH) { Fix("again, Unity still applied the old value"); } } catch (Exception ex) { s_watchUntil = -1f; Plugin.LogWarning("[Display] Could not check the screen resolution: " + ex.Message); } } } private static string Replace(int w, int h, FullScreenMode mode, Resolution best, string reason) { //IL_000c: Unknown result type (might be due to invalid IL or missing references) //IL_000d: Unknown result type (might be due to invalid IL or missing references) //IL_0012: Unknown result type (might be due to invalid IL or missing references) //IL_0013: Unknown result type (might be due to invalid IL or missing references) s_badW = w; s_badH = h; s_best = best; s_mode = mode; s_reason = reason; Fix("at start"); s_watchUntil = Time.realtimeSinceStartup + 20f; return $"{reason}: {((Resolution)(ref best)).width}x{((Resolution)(ref best)).height} set"; } private static void Fix(string when) { //IL_004d: 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_00b9: Unknown result type (might be due to invalid IL or missing references) //IL_00be: Unknown result type (might be due to invalid IL or missing references) //IL_012e: Unknown result type (might be due to invalid IL or missing references) //IL_0133: Unknown result type (might be due to invalid IL or missing references) s_fixes++; PlayerPrefs.SetInt("Screenmanager Resolution Width", ((Resolution)(ref s_best)).width); PlayerPrefs.SetInt("Screenmanager Resolution Height", ((Resolution)(ref s_best)).height); PlayerPrefs.Save(); Screen.SetResolution(((Resolution)(ref s_best)).width, ((Resolution)(ref s_best)).height, s_mode, ((Resolution)(ref s_best)).refreshRateRatio); object[] obj = new object[6] { s_badW, s_badH, s_reason, ((Resolution)(ref s_best)).width, ((Resolution)(ref s_best)).height, null }; RefreshRate refreshRateRatio = ((Resolution)(ref s_best)).refreshRateRatio; obj[5] = ((RefreshRate)(ref refreshRateRatio)).value; LastAction = string.Format("The saved resolution {0}x{1} was {2}: switched to {3}x{4} at {5:0} Hz.", obj); object[] obj2 = new object[6] { s_badW, s_badH, s_reason, ((Resolution)(ref s_best)).width, ((Resolution)(ref s_best)).height, null }; refreshRateRatio = ((Resolution)(ref s_best)).refreshRateRatio; obj2[5] = ((RefreshRate)(ref refreshRateRatio)).value; Plugin.LogWarning(string.Format("[Display] Saved screen resolution {0}x{1} is {2}: set {3}x{4} at {5:0.##} Hz ", obj2) + when + " ([Display] FixUnsupportedResolution = false leaves it)."); } public static string FrameGenShapeProblem() { //IL_0007: Unknown result type (might be due to invalid IL or missing references) //IL_000d: Invalid comparison between Unknown and I4 try { if (!Enabled || (int)Screen.fullScreenMode == 3) { return ""; } Resolution[] resolutions = Screen.resolutions; if (resolutions == null || resolutions.Length == 0 || !Native(resolutions, out var native) || !ShapeDiffers(Screen.width, Screen.height, ((Resolution)(ref native)).width, ((Resolution)(ref native)).height)) { return ""; } return $"not started: the fullscreen resolution {Screen.width}x{Screen.height} has another shape than the monitor ({((Resolution)(ref native)).width}x{((Resolution)(ref native)).height}), " + $"which crashed the graphics driver with frame generation (Linux, XWayland). Pick {((Resolution)(ref native)).width}x{((Resolution)(ref native)).height} (or windowed) in the graphics " + "settings, then switch frame generation on"; } catch (Exception) { return ""; } } private static bool Native(Resolution[] modes, out Resolution native) { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_0005: Unknown result type (might be due to invalid IL or missing references) //IL_0007: Unknown result type (might be due to invalid IL or missing references) //IL_0008: Unknown result type (might be due to invalid IL or missing references) //IL_0023: Unknown result type (might be due to invalid IL or missing references) //IL_0028: Unknown result type (might be due to invalid IL or missing references) //IL_007b: Unknown result type (might be due to invalid IL or missing references) //IL_008f: Unknown result type (might be due to invalid IL or missing references) //IL_0094: Unknown result type (might be due to invalid IL or missing references) //IL_00cf: 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_00af: Unknown result type (might be due to invalid IL or missing references) //IL_00b4: Unknown result type (might be due to invalid IL or missing references) //IL_00be: Unknown result type (might be due to invalid IL or missing references) //IL_00c3: Unknown result type (might be due to invalid IL or missing references) Resolution val = (native = Screen.currentResolution); RefreshRate refreshRateRatio; if (((Resolution)(ref val)).width > 0 && ((Resolution)(ref val)).height > 0) { refreshRateRatio = ((Resolution)(ref val)).refreshRateRatio; if (((RefreshRate)(ref refreshRateRatio)).value > 1.0) { return true; } } int num = ((((Resolution)(ref val)).width > 0) ? ((Resolution)(ref val)).width : Display.main.systemWidth); int num2 = ((((Resolution)(ref val)).height > 0) ? ((Resolution)(ref val)).height : Display.main.systemHeight); native = default(Resolution); bool flag = false; for (int i = 0; i < modes.Length; i++) { Resolution val2 = modes[i]; if (((Resolution)(ref val2)).width != num || ((Resolution)(ref val2)).height != num2) { continue; } if (flag) { refreshRateRatio = ((Resolution)(ref val2)).refreshRateRatio; double value = ((RefreshRate)(ref refreshRateRatio)).value; refreshRateRatio = ((Resolution)(ref native)).refreshRateRatio; if (!(value > ((RefreshRate)(ref refreshRateRatio)).value)) { continue; } } native = val2; flag = true; } return flag; } private static bool ShapeDiffers(int w, int h, int nw, int nh) { return Math.Abs((double)w / (double)h - (double)nw / (double)nh) > 0.01; } private static bool IsMode(Resolution[] modes, int w, int h) { //IL_0008: Unknown result type (might be due to invalid IL or missing references) //IL_000d: Unknown result type (might be due to invalid IL or missing references) for (int i = 0; i < modes.Length; i++) { Resolution val = modes[i]; if (((Resolution)(ref val)).width == w && ((Resolution)(ref val)).height == h) { return true; } } return false; } private static Resolution Largest(Resolution[] modes) { //IL_0002: Unknown result type (might be due to invalid IL or missing references) //IL_0007: Unknown result type (might be due to invalid IL or missing references) //IL_000e: Unknown result type (might be due to invalid IL or missing references) //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_0071: Unknown result type (might be due to invalid IL or missing references) //IL_0065: Unknown result type (might be due to invalid IL or missing references) //IL_0066: 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_004a: Unknown result type (might be due to invalid IL or missing references) //IL_0055: Unknown result type (might be due to invalid IL or missing references) //IL_005a: Unknown result type (might be due to invalid IL or missing references) Resolution result = modes[0]; for (int i = 1; i < modes.Length; i++) { Resolution val = modes[i]; long num = (long)((Resolution)(ref result)).width * (long)((Resolution)(ref result)).height; long num2 = (long)((Resolution)(ref val)).width * (long)((Resolution)(ref val)).height; if (num2 <= num) { if (num2 != num) { continue; } RefreshRate refreshRateRatio = ((Resolution)(ref val)).refreshRateRatio; double value = ((RefreshRate)(ref refreshRateRatio)).value; refreshRateRatio = ((Resolution)(ref result)).refreshRateRatio; if (!(value > ((RefreshRate)(ref refreshRateRatio)).value)) { continue; } } result = val; } return result; } private static string ModeList() { //IL_0012: Unknown result type (might be due to invalid IL or missing references) //IL_0017: Unknown result type (might be due to invalid IL or missing references) List<string> list = new List<string>(); Resolution[] resolutions = Screen.resolutions; for (int i = 0; i < resolutions.Length; i++) { Resolution val = resolutions[i]; string item = ((Resolution)(ref val)).width + "x" + ((Resolution)(ref val)).height; if (!list.Contains(item)) { list.Add(item); } } return string.Join(", ", list); } } public class FpsCounterHUD : MonoBehaviour { private const int SampleCapacity = 1000; private const int AverageWindow = 60; private readonly float[] m_frameDeltas = new float[1000]; private readonly float[] m_sortedDeltas = new float[1000]; private int m_sampleCount; private int m_sampleIndex; private float m_updateTimer; private const float UpdateInterval = 0.25f; private const float MaxSampleSeconds = 1f; private float m_displayFps; private float m_displayMs; private float m_display1PercentLow; private float m_display01PercentLow; private long m_displayTotalMemMb; private string m_fpsLine = ""; private string m_realLine = ""; private float m_colorFps; private string m_lowsLine = ""; private string m_memLine = ""; private string m_vramLine = ""; private bool m_rebuildRequested = true; private bool m_wasActive; private GUIStyle m_fpsStyle; private GUIStyle m_subStyle; private void Awake() { Object.DontDestroyOnLoad((Object)(object)((Component)this).gameObject); GraphicsConfig.OnSettingsChanged += RequestRebuild; } private void OnDestroy() { GraphicsConfig.OnSettingsChanged -= RequestRebuild; } private void RequestRebuild() { m_rebuildRequested = true; } private void Update() { //IL_0005: Unknown result type (might be due to invalid IL or missing references) if (Input.GetKeyDown(GraphicsConfig.FpsHudToggleKey.Value)) { GraphicsConfig.EnableFpsHud.Value = !GraphicsConfig.EnableFpsHud.Value; } if (!GraphicsConfig.EnableFpsHud.Value || !GraphicsConfig.MasterEnable.Value) { m_wasActive = false; return; } if (!m_wasActive) { m_wasActive = true; m_sampleCount = 0; m_sampleIndex = 0; m_updateTimer = 0f; m_rebuildRequested = true; } float unscaledDeltaTime = Time.unscaledDeltaTime; if (unscaledDeltaTime < 1f) { m_frameDeltas[m_sampleIndex] = unscaledDeltaTime; m_sampleIndex = (m_sampleIndex + 1) % 1000; if (m_sampleCount < 1000) { m_sampleCount++; } } m_updateTimer += unscaledDeltaTime; if (m_updateTimer >= 0.25f || m_rebuildRequested) { m_updateTimer = 0f; m_rebuildRequested = false; CalculateMetrics(); BuildLines(); } } private void CalculateMetrics() { if (m_sampleCount != 0) { for (int i = 0; i < m_sampleCount; i++) { m_sortedDeltas[i] = m_frameDeltas[i]; } int num = Mathf.Min(m_sampleCount, 60); float num2 = 0f; for (int j = 1; j <= num; j++) { num2 += m_frameDeltas[(m_sampleIndex - j + 1000) % 1000]; } float num3 = num2 / (float)num; m_displayFps = ((num3 > 0f) ? (1f / num3) : 0f); m_displayMs = num3 * 1000f; Array.Sort(m_sortedDeltas, 0, m_sampleCount); int num4 = Mathf.Clamp(Mathf.FloorToInt((float)m_sampleCount * 0.99f), 0, m_sampleCount - 1); float num5 = m_sortedDeltas[num4]; m_display1PercentLow = ((num5 > 0f) ? (1f / num5) : 0f); int num6 = Mathf.Clamp(Mathf.FloorToInt((float)m_sampleCount * 0.999f), 0, m_sampleCount - 1); float num7 = m_sortedDeltas[num6]; m_display01PercentLow = ((num7 > 0f) ? (1f / num7) : 0f); m_displayTotalMemMb = Profiler.GetTotalAllocatedMemoryLong() / 1048576; } } private void BuildLines() { int num = Mathf.RoundToInt(m_displayFps); float num2 = (NativeUpscaler.FrameGenActive ? NativeUpscaler.FrameGenGeneratedFps : 0f); if (num2 > 0.5f) { m_colorFps = m_displayFps + num2; m_fpsLine = $"{Mathf.RoundToInt(m_colorFps)} FPS <size=13><color=#C8A865>with frame gen</color></size>"; m_realLine = (GraphicsConfig.FpsHudShowFrameTime.Value ? $"Real: <color=#F0E6D2>{num} FPS</color> ({m_displayMs:F1} ms) | generated <color=#F0E6D2>{Mathf.RoundToInt(num2)}</color>" : $"Real: <color=#F0E6D2>{num} FPS</color> | generated <color=#F0E6D2>{Mathf.RoundToInt(num2)}</color>"); } else { m_colorFps = m_displayFps; m_fpsLine = (GraphicsConfig.FpsHudShowFrameTime.Value ? $"{num} FPS <size=13><color=#C8A865>({m_displayMs:F1} ms)</color></size>" : $"{num} FPS"); m_realLine = ""; } m_lowsLine = $"1% Low: <color=#F5D058>{Mathf.RoundToInt(m_display1PercentLow)}</color> | 0.1%: <color=#F87171>{Mathf.RoundToInt(m_display01PercentLow)}</color>"; string arg = (NativeUpscaler.IsActive ? string.Format("{0} {1} {2}x{3}", (NativeUpscaler.SelectedBackend == 2) ? "DLSS" : "FSR 3", NativeUpscaler.ModeLabel, NativeUpscaler.RenderWidth, NativeUpscaler.RenderHeight) : (Fsr1Scaler.IsActive ? $"FSR 1 {Fsr1Scaler.RenderWidth}x{Fsr1Scaler.RenderHeight}" : $"{GraphicsConfig.CustomRenderScalePercent.Value}%")); m_memLine = $"Mem: <color=#F0E6D2>{m_displayTotalMemMb}MB</color> | Scale: <color=#F0E6D2>{arg}</color>"; if (GraphicsConfig.FpsHudShowVram.Value) { VramTracker.VramProjection projection = VramTracker.GetProjection(); string statusColorHex = VramTracker.GetStatusColorHex(projection.HealthStatus); if (projection.DetectedGpuDriverMB > 0f) { m_vramLine = $"VRAM: <color={statusColorHex}>{projection.DetectedGpuDriverGB:F1}G</color> / {VramTracker.TotalVramGB:F1}G [est. <color={statusColorHex}>{projection.ProjectedTotalGB:F1}G</color>]"; } else { m_vramLine = $"VRAM est.: <color={statusColorHex}>{projection.ProjectedTotalGB:F1}G</color> / {VramTracker.TotalVramGB:F1}G (<color={statusColorHex}>{projection.ProjectedUsagePercent:F0}%</color>)"; } } } private void InitStyles() { //IL_0014: Unknown result type (might be due to invalid IL or missing references) //IL_001e: Expected O, but got Unknown //IL_004b: Unknown result type (might be due to invalid IL or missing references) //IL_0055: Expected O, but got Unknown //IL_0074: Unknown result type (might be due to invalid IL or missing references) float value = GraphicsConfig.FpsHudTextScale.Value; if (m_fpsStyle == null) { m_fpsStyle = new GUIStyle(); m_fpsStyle.fontStyle = (FontStyle)1; m_fpsStyle.alignment = (TextAnchor)3; m_fpsStyle.richText = true; } if (m_subStyle == null) { m_subStyle = new GUIStyle(); m_subStyle.normal.textColor = new Color(0.78f, 0.66f, 0.4f, 0.95f); m_subStyle.alignment = (TextAnchor)3; m_subStyle.richText = true; } m_fpsStyle.fontSize = Mathf.RoundToInt(20f * value); m_subStyle.fontSize = Mathf.RoundToInt(12f * value); } private void OnGUI() { //IL_008a: Unknown result type (might be due to invalid IL or missing references) //IL_01cc: Unknown result type (might be due to invalid IL or missing references) //IL_01fd: Unknown result type (might be due to invalid IL or missing references) //IL_0207: Unknown result type (might be due to invalid IL or missing references) //IL_0227: Unknown result type (might be due to invalid IL or missing references) //IL_024b: Unknown result type (might be due to invalid IL or missing references) //IL_0282: Unknown result type (might be due to invalid IL or missing references) //IL_02b7: Unknown result type (might be due to invalid IL or missing references) //IL_02fa: Unknown result type (might be due to invalid IL or missing references) //IL_0342: Unknown result type (might be due to invalid IL or missing references) if (GraphicsConfig.EnableFpsHud.Value && GraphicsConfig.MasterEnable.Value) { InitStyles(); Color textColor = default(Color); if (m_colorFps >= 59.5f) { ((Color)(ref textColor))..ctor(0.32f, 0.88f, 0.55f); } else if (m_colorFps >= 29.5f) { ((Color)(ref textColor))..ctor(0.96f, 0.82f, 0.35f); } else { ((Color)(ref textColor))..ctor(0.95f, 0.4f, 0.4f); } m_fpsStyle.normal.textColor = textColor; float num = 250f * GraphicsConfig.FpsHudTextScale.Value; float num2 = 40f * GraphicsConfig.FpsHudTextScale.Value; if (m_realLine.Length > 0) { num2 += 20f * GraphicsConfig.FpsHudTextScale.Value; } if (GraphicsConfig.FpsHudShowLows.Value) { num2 += 20f * GraphicsConfig.FpsHudTextScale.Value; } if (GraphicsConfig.FpsHudShowMemory.Value) { num2 += 20f * GraphicsConfig.FpsHudTextScale.Value; } if (GraphicsConfig.FpsHudShowVram.Value) { num2 += 20f * GraphicsConfig.FpsHudTextScale.Value; } float num3 = 15f; float num4 = 15f; switch (GraphicsConfig.FpsHudPosition.Value) { case GraphicsConfig.HudCorner.TopRight: num3 = (float)Screen.width - num - 15f; num4 = 15f; break; case GraphicsConfig.HudCorner.TopLeft: num3 = 15f; num4 = 15f; break; case GraphicsConfig.HudCorner.BottomRight: num3 = (float)Screen.width - num - 15f; num4 = (float)Screen.height - num2 - 15f; break; case GraphicsConfig.HudCorner.BottomLeft: num3 = 15f; num4 = (float)Screen.height - num2 - 15f; break; } Rect val = default(Rect); ((Rect)(ref val))..ctor(num3, num4, num, num2); Color color = GUI.color; float num5 = Mathf.Clamp(GraphicsConfig.FpsHudBackgroundAlpha.Value, 0.05f, 1f); GUI.color = new Color(0.02f, 0.02f, 0.04f, num5); GUI.DrawTexture(val, (Texture)(object)Texture2D.whiteTexture); GUI.color = new Color(0.85f, 0.68f, 0.28f, 0.7f); GUI.DrawTexture(new Rect(((Rect)(ref val)).x, ((Rect)(ref val)).y, ((Rect)(ref val)).width, 1f), (Texture)(object)Texture2D.whiteTexture); GUI.DrawTexture(new Rect(((Rect)(ref val)).x, ((Rect)(ref val)).y + ((Rect)(ref val)).height - 1f, ((Rect)(ref val)).width, 1f), (Texture)(object)Texture2D.whiteTexture); GUI.DrawTexture(new Rect(((Rect)(ref val)).x, ((Rect)(ref val)).y + 1f, 1f, ((Rect)(ref val)).height - 2f), (Texture)(object)Texture2D.whiteTexture); GUI.DrawTexture(new Rect(((Rect)(ref val)).x + ((Rect)(ref val)).width - 1f, ((Rect)(ref val)).y + 1f, 1f, ((Rect)(ref val)).height - 2f), (Texture)(object)Texture2D.whiteTexture); GUI.color = color; GUILayout.BeginArea(new Rect(((Rect)(ref val)).x + 8f, ((Rect)(ref val)).y + 5f, ((Rect)(ref val)).width - 16f, ((Rect)(ref val)).height - 8f)); GUILayout.Label(m_fpsLine, m_fpsStyle, Array.Empty<GUILayoutOption>()); if (m_realLine.Length > 0) { GUILayout.Label(m_realLine, m_subStyle, Array.Empty<GUILayoutOption>()); } if (GraphicsConfig.FpsHudShowLows.Value) { GUILayout.Label(m_lowsLine, m_subStyle, Array.Empty<GUILayoutOption>()); } if (GraphicsConfig.FpsHudShowMemory.Value) { GUILayout.Label(m_memLine, m_subStyle, Array.Empty<GUILayoutOption>()); } if (GraphicsConfig.FpsHudShowVram.Value) { GUILayout.Label(m_vramLine, m_subStyle, Array.Empty<GUILayoutOption>()); } GUILayout.EndArea(); } } } public static class Fsr1Scaler { public enum Pick { Vanilla, Fsr1, Fsr3, Dlss } [CompilerGenerated] private static class <>O { public static CameraCallback <0>__OnPreCull; } private static readonly int SrcSizeId = Shader.PropertyToID("_GoSrcSize"); private static readonly int ParamsId = Shader.PropertyToID("_GoParams"); private static UpscaledFrameBuffer s_ufb; private static Camera s_wantCam; private static Camera s_cam; private static CommandBuffer s_cb; private static bool s_attached; private static bool s_hooked; private static bool s_failed; private static RenderTexture s_mid; private static float s_nextCameraSearch; public static bool IsActive { get; private set; } public static string StatusLine { get; private set; } = ""; public static int RenderWidth { get; private set; } public static int RenderHeight { get; private set; } public static int OutputWidth { get; private set; } public static int OutputHeight { get; private set; } public static bool Sharpening { get; private set; } public static RenderTexture Upscaled => s_mid; public static bool Chosen { get { if (GraphicsConfig.UpscaleFilter != null && GraphicsConfig.UpscaleFilter.Value == GraphicsConfig.GameScalerFilter.FSR1) { return NativeUpscaler.SelectedBackend == 0; } return false; } } public static bool Available { get { if ((Object)(object)DepthCopy.Fsr1EasuMaterial != (Object)null) { return (Object)(object)DepthCopy.Fsr1RcasMaterial != (Object)null; } return false; } } public static string Why => DepthCopy.Fsr1Why; public static Pick Picked { get { switch (NativeUpscaler.SelectedBackend) { case 1: return Pick.Fsr3; case 2: return Pick.Dlss; default: if (!Chosen) { return Pick.Vanilla; } return Pick.Fsr1; } } } public static void Choose(Pick pick) { switch (pick) { case Pick.Fsr3: if (GraphicsConfig.Scaler != null) { GraphicsConfig.Scaler.Value = GraphicsConfig.ScalerChoice.FSR3; } return; case Pick.Dlss: if (GraphicsConfig.Scaler != null) { GraphicsConfig.Scaler.Value = GraphicsConfig.ScalerChoice.DLSS; } return; } if (GraphicsConfig.Scaler != null) { GraphicsConfig.Scaler.Value = GraphicsConfig.ScalerChoice.Vanilla; } GraphicsConfig.UpscaleFilter.Value = ((pick == Pick.Fsr1) ? GraphicsConfig.GameScalerFilter.FSR1 : GraphicsConfig.GameScalerFilter.Game); } public static void Tick() { if (s_failed || GraphicsConfig.UpscaleFilter == null) { return; } try { RenderTexture val = null; string why; bool flag = Wanted(out why); if (flag && !FindCamera()) { flag = false; why = "FSR 1 waits for the game camera"; } if (flag) { val = GameFrameBuffer.RenderTextureOf(s_ufb); if ((Object)(object)val == (Object)null || ((Texture)val).width >= Screen.width || ((Texture)val).height >= Screen.height) { flag = false; why = "FSR 1 waits for a 3D render scale below 100 (it only scales up)"; } } if (flag) { Apply(val); return; } Release(); StatusLine = why; } catch (Exception ex) { s_failed = true; try { Release(); } catch (Exception) { } StatusLine = "FSR 1 failed: " + ex.Message; Plugin.LogWarning("[FSR 1] " + ex?.ToString() + " - the game's own stretch takes over again."); } } public static void Shutdown() { try { Release(); } catch (Exception) { } } private static bool Wanted(out string why) { why = ""; if (!Chosen) { return false; } if (!GraphicsConfig.MasterEnable.Value) { why = "Graphics Overdrive is switched off"; return false; } if (!GameFrameBuffer.Available) { why = "the game's 3D-resolution code has changed"; return false; } if (NativeUpscaler.IsActive) { why = "FSR 1 waits for the other scaler to stop"; return false; } if (!Available) { why = Why; return false; } return true; } private static bool FindCamera() { if ((Object)(object)s_ufb == (Object)null && Time.unscaledTime >= s_nextCameraSearch) { s_nextCameraSearch = Time.unscaledTime + 1f; s_ufb = Object.FindAnyObjectByType<UpscaledFrameBuffer>(); } if ((Object)(object)s_ufb == (Object)null) { return false; } Camera val = GameFrameBuffer.CameraOf(s_ufb); s_wantCam = (((Object)(object)val != (Object)null) ? val : ((Component)s_ufb).GetComponent<Camera>()); return (Object)(object)s_wantCam != (Object)null; } private static void SetSource(Material easu, int width, int height, bool sharpen) { //IL_001a: Unknown result type (might be due to invalid IL or missing references) //IL_002a: Unknown result type (might be due to invalid IL or missing references) //IL_0030: Invalid comparison between Unknown and I4 //IL_0061: Unknown result type (might be due to invalid IL or missing references) easu.SetVector(SrcSizeId, new Vector4((float)width, (float)height, 1f / (float)width, 1f / (float)height)); easu.SetVector(ParamsId, new Vector4(((int)QualitySettings.activeColorSpace == 1) ? 1f : 0f, sharpen ? 1f : 0f, sharpen ? 0f : 1f, 0f)); } private static void Apply(RenderTexture rt) { //IL_0161: Unknown result type (might be due to invalid IL or missing references) //IL_0166: Unknown result type (might be due to invalid IL or missing references) //IL_0176: Expected O, but got Unknown //IL_0082: Unknown result type (might be due to invalid IL or missing references) //IL_0088: 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_0090: Unknown result type (might be due to invalid IL or missing references) //IL_009b: 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_00a9: Unknown result type (might be due to invalid IL or missing references) //IL_00b0: Unknown result type (might be due to invalid IL or missing references) //IL_00bd: Expected O, but got Unknown //IL_00e4: Unknown result type (might be due to invalid IL or missing references) //IL_00f4: Unknown result type (might be due to invalid IL or missing references) //IL_00fa: Invalid comparison between Unknown and I4 //IL_0129: Unknown result type (might be due to invalid IL or missing references) //IL_01c0: Unknown result type (might be due to invalid IL or missing references) //IL_01ca: Expected O, but got Unknown //IL_01b0: Unknown result type (might be due to invalid IL or missing references) //IL_01b5: Unknown result type (might be due to invalid IL or missing references) //IL_01bb: Expected O, but got Unknown //IL_02a5: Unknown result type (might be due to invalid IL or missing references) Material fsr1EasuMaterial = DepthCopy.Fsr1EasuMaterial; Material fsr1RcasMaterial = DepthCopy.Fsr1RcasMaterial; float num = Mathf.Clamp01(GraphicsConfig.Fsr1Sharpness.Value); bool flag = num > 0.0005f; int width = Screen.width; int height = Screen.height; SetSource(fsr1EasuMaterial, ((Texture)rt).width, ((Texture)rt).height, flag); if (flag) { if ((Object)(object)s_mid == (Object)null || ((Texture)s_mid).width != width || ((Texture)s_mid).height != height) { ReleaseMid(); RenderTextureFormat val = (RenderTextureFormat)(SystemInfo.SupportsRenderTextureFormat((RenderTextureFormat)8) ? 8 : 0); s_mid = new RenderTexture(width, height, 0, val, (RenderTextureReadWrite)1) { name = "GO FSR 1 upscaled", filterMode = (FilterMode)0, wrapMode = (TextureWrapMode)1, useMipMap = false, hideFlags = (HideFlags)61 }; s_mid.Create(); } fsr1RcasMaterial.SetVector(SrcSizeId, new Vector4((float)width, (float)height, 1f / (float)width, 1f / (float)height)); fsr1RcasMaterial.SetVector(ParamsId, new Vector4(((int)QualitySettings.activeColorSpace == 1) ? 1f : 0f, Mathf.Pow(2f, -2f * (1f - num)), 1f, 0f)); } else { ReleaseMid(); } if ((Object)(object)s_cam != (Object)(object)s_wantCam) { Detach(); s_cam = s_wantCam; } if (s_cb == null) { s_cb = new CommandBuffer { name = "GO FSR 1 upscale" }; } if (!s_attached) { s_cam.AddCommandBuffer((CameraEvent)20, s_cb); s_attached = true; } if (!s_hooked) { CameraCallback onPreCull = Camera.onPreCull; object obj = <>O.<0>__OnPreCull; if (obj == null) { CameraCallback val2 = OnPreCull; <>O.<0>__OnPreCull = val2; obj = (object)val2; } Camera.onPreCull = (CameraCallback)Delegate.Combine((Delegate?)(object)onPreCull, (Delegate?)obj); s_hooked = true; } bool isActive = IsActive; IsActive = true; Sharpening = flag; RenderWidth = ((Texture)rt).width; RenderHeight = ((Texture)rt).height; OutputWidth = width; OutputHeight = height; foreach (FrameBufferScaler item in GameFrameBuffer.Subscribers(s_ufb)) { Show(item, rt); } StatusLine = $"{((Texture)rt).width}x{((Texture)rt).height} to {width}x{height}" + (flag ? "" : ", sharpening off"); if (!isActive) { Plugin.LogInfo($"[FSR 1] on: {StatusLine} ({SystemInfo.graphicsDeviceType})"); } } private static void Show(FrameBufferScaler scaler, RenderTexture rt) { RawImage val = (((Object)(object)scaler != (Object)null) ? GameFrameBuffer.RawImageOf(scaler) : null); if (!((Object)(object)val == (Object)null)) { Texture val2 = (Texture)(object)(Sharpening ? s_mid : rt); Material val3 = (Sharpening ? DepthCopy.Fsr1RcasMaterial : DepthCopy.Fsr1EasuMaterial); if (val.texture != val2) { val.texture = val2; } if (((Graphic)val).material != val3) { ((Graphic)val).material = val3; } } } public static void OnScalerBufferCreated(FrameBufferScaler scaler) { if (!IsActive || (Object)(object)s_ufb == (Object)null || (Object)(object)scaler == (Object)null) { return; } RenderTexture val = GameFrameBuffer.RenderTextureOf(s_ufb); if ((Object)(object)val == (Object)null || ((Texture)val).width >= Screen.width || ((Texture)val).height >= Screen.height) { RawImage val2 = GameFrameBuffer.RawImageOf(scaler); if ((Object)(object)val2 != (Object)null && Ours(((Graphic)val2).material)) { ((Graphic)val2).material = null; } } else { SetSource(DepthCopy.Fsr1EasuMaterial, ((Texture)val).width, ((Texture)val).height, Sharpening); RenderWidth = ((Texture)val).width; RenderHeight = ((Texture)val).height; Show(scaler, val); } } private static void OnPreCull(Camera cam) { //IL_0082: Unknown result type (might be due to invalid IL or missing references) if (s_cb == null || (Object)(object)cam == (Object)null || (Object)(object)cam != (Object)(object)s_cam) { return; } s_cb.Clear(); if (IsActive && Sharpening && !((Object)(object)s_mid == (Object)null)) { RenderTexture targetTexture = cam.targetTexture; Material fsr1EasuMaterial = DepthCopy.Fsr1EasuMaterial; if (!((Object)(object)targetTexture == (Object)null) && !((Object)(object)fsr1EasuMaterial == (Object)null)) { SetSource(fsr1EasuMaterial, ((Texture)targetTexture).width, ((Texture)targetTexture).height, sharpen: true); s_cb.Blit((Texture)(object)targetTexture, RenderTargetIdentifier.op_Implicit((Texture)(object)s_mid), fsr1EasuMaterial); } } } private static bool Ours(Material material) { if ((Object)(object)material != (Object)null) { if (material != DepthCopy.Fsr1EasuMaterial) { return material == DepthCopy.Fsr1RcasMaterial; } return true; } return false; } private static void Detach() { if (s_attached && (Object)(object)s_cam != (Object)null && s_cb != null) { s_cam.RemoveCommandBuffer((CameraEvent)20, s_cb); } s_attached = false; s_cam = null; } private static void ReleaseMid() { if (!((Object)(object)s_mid == (Object)null)) { s_mid.Release(); Object.Destroy((Object)(object)s_mid); s_mid = null; } } private static void Release() { //IL_0031: Unknown result type (might be due to invalid IL or missing references) //IL_003b: Expected O, but got Unknown //IL_0021: Unknown result type (might be due to invalid IL or missing references) //IL_0026: Unknown result type (might be due to invalid IL or missing references) //IL_002c: Expected O, but got Unknown Detach(); if (s_hooked) { CameraCallback onPreCull = Camera.onPreCull; object obj = <>O.<0>__OnPreCull; if (obj == null) { CameraCallback val = OnPreCull; <>O.<0>__OnPreCull = val; obj = (object)val; } Camera.onPreCull = (CameraCallback)Delegate.Remove((Delegate?)(object)onPreCull, (Delegate?)obj); s_hooked = false; } if (s_cb != null) { s_cb.Clear(); } if (IsActive && (Object)(object)s_ufb != (Object)null) { RenderTexture texture = GameFrameBuffer.RenderTextureOf(s_ufb); foreach (FrameBufferScaler item in GameFrameBuffer.Subscribers(s_ufb)) { RawImage val2 = (((Object)(object)item != (Object)null) ? GameFrameBuffer.RawImageOf(item) : null); if (!((Object)(object)val2 == (Object)null)) { if (Ours(((Graphic)val2).material)) { ((Graphic)val2).material = null; } if ((Object)(object)s_mid != (Object)null && (object)val2.texture == s_mid) { val2.texture = (Texture)(object)texture; } } } } ReleaseMid(); if (IsActive) { Plugin.LogInfo("[FSR 1] off"); } IsActive = false; Sharpening = false; } } internal static class GameFrameBuffer { private static FieldRef<uint> s_targetVertical; private static FieldRef<bool> s_autoTarget; private static FieldRef<UpscalingAlgorithm> s_algorithm; private static FieldRef<UpscaledFrameBuffer, Camera> s_camera; private static FieldRef<UpscaledFrameBuffer, RenderTexture> s_renderTexture; private static FieldRef<UpscaledFrameBuffer, List<FrameBufferScaler>> s_subscribers; private static FieldRef<UpscaledFrameBuffer, bool> s_scaledRendering; private static FieldRef<FrameBufferScaler, RawImage> s_rawImage; private static Action<UpscaledFrameBuffer> s_createClearCamera; private static Action<UpscaledFrameBuffer> s_reassignTexture; private static Action<UpscaledFrameBuffer> s_releaseTexture; private static Action<UpscaledFrameBuffer> s_destroyClearCamera; private static bool s_resolved; private static string s_missing = ""; internal static bool Available { get { Resolve(); return s_missing.Length == 0; } } internal static uint TargetVertical { get { return s_targetVertical.Invoke(); } set { s_targetVertical.Invoke() = value; } } internal static bool AutoTarget { get { return s_autoTarget.Invoke(); } set { s_autoTarget.Invoke() = value; } } internal static UpscalingAlgorithm Algorithm { get { return s_algorithm.Invoke(); } set { //IL_000a: Unknown result type (might be due to invalid IL or missing references) //IL_000c: Expected I4, but got Unknown s_algorithm.Invoke() = (UpscalingAlgorithm)(int)value; } } internal static ref Camera CameraOf(UpscaledFrameBuffer ufb) { return ref s_camera.Invoke(ufb); } internal static Camera ControllerCamera() { GameCamera instance = GameCamera.instance; if (!((Object)(object)instance != (Object)null)) { return null; } return ((Component)instance).GetComponent<Camera>(); } internal static RenderTexture RenderTextureOf(UpscaledFrameBuffer ufb) { return s_renderTexture.Invoke(ufb); } internal static List<FrameBufferScaler> Subscribers(UpscaledFrameBuffer ufb) { return s_subscribers.Invoke(ufb); } internal static ref bool UsingScaledRendering(UpscaledFrameBuffer ufb) { return ref s_scaledRendering.Invoke(ufb); } internal static RawImage RawImageOf(FrameBufferScaler scaler) { return s_rawImage.Invoke(scaler); } internal static void CreateClearCamera(UpscaledFrameBuffer ufb) { s_createClearCamera(ufb); } internal static void ReassignTextureIfNeeded(UpscaledFrameBuffer ufb) { s_reassignTexture(ufb); } internal static void ReleaseTextureIfExists(UpscaledFrameBuffer ufb) { s_releaseTexture(ufb); } internal static void DestroyClearCameraIfExists(UpscaledFrameBuffer ufb) { s_destroyClearCamera(ufb); } private static void Resolve() { if (!s_resolved) { s_resolved = true; List<string> list = new List<string>(); s_targetVertical = Static<uint>("m_targetResolutionVertical", list); s_autoTarget = Static<bool>("m_autoTargetResolution", list); s_algorithm = Static<UpscalingAlgorithm>("m_upscalingAlgorithm", list); s_camera = Instance<UpscaledFrameBuffer, Camera>("m_camera", list); s_renderTexture = Instance<UpscaledFrameBuffer, RenderTexture>("m_renderTexture", list); s_subscribers = Instance<UpscaledFrameBuffer, List<FrameBufferScaler>>("m_subscribers", list); s_scaledRendering = Instance<UpscaledFrameBuffer, bool>("m_isUsingScaledRendering", list); s_rawImage = Instance<FrameBufferScaler, RawImage>("m_rawImage", list); s_createClearCamera = Method("CreateClearCamera", list); s_reassignTexture = Method("ReassignTextureIfNeeded", list); s_releaseTexture = Method("ReleaseTextureIfExists", list); s_destroyClearCamera = Method("DestroyClearCameraIfExists", list); s_missing = string.Join(", ", list.ToArray()); if (list.Count > 0) { Plugin.LogWarning("[RenderScale] The game's 3D-resolution code has changed (not found: " + s_missing + "). The render scale dial, FSR 3 / DLSS and frame generation stay off; the game's own 3D resolution setting still works."); } } } internal static void ApplyHooks(Harmony harmony) { Resolve(); Hook(harmony, typeof(UpscaledFrameBuffer), "UpdateCameraTarget", Type.EmptyTypes, AccessTools.Method(typeof(WorldObjectPatches.Patch_UpscaledFrameBuffer_UpdateCameraTarget), "Prefix", (Type[])null, (Type[])null), null); Hook(harmony, typeof(FrameBufferScaler), "OnBufferCreated", new Type[2] { typeof(UpscaledFrameBuffer), typeof(RenderTexture) }, null, AccessTools.Method(typeof(NativeScalerPatches.Patch_FrameBufferScaler_OnBufferCreated), "Postfix", (Type[])null, (Type[])null)); } private static void Hook(Harmony harmony, Type type, string name, Type[] parameters, MethodInfo prefix, MethodInfo postfix) { //IL_003e: Unknown result type (might be due to invalid IL or missing references) //IL_0052: Unknown result type (might be due to invalid IL or missing references) string text = type.Name + "." + name + "()"; try { MethodInfo method = type.GetMethod(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, parameters, null); if (method != null) { harmony.Patch((MethodBase)method, (!(prefix != null)) ? ((HarmonyMethod)null) : new HarmonyMethod(prefix), (!(postfix != null)) ? ((HarmonyMethod)null) : new HarmonyMethod(postfix), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null); return; } } catch (Exception ex) { text = text + " (" + ex.GetType().Name + ": " + ex.Message + ")"; } s_missing = ((s_missing.Length == 0) ? text : (s_missing + ", " + text)); Plugin.LogWarning("[RenderScale] The game's 3D-resolution code has changed (hook not applied: " + text + "). The render scale dial, FSR 3 / DLSS and frame generation stay off; the game's own 3D resolution setting still works."); } private static FieldRef<T> Static<T>(string name, List<string> missing) { try { FieldInfo field = typeof(UpscaledFrameBuffer).GetField(name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); if (field != null && field.FieldType == typeof(T)) { return AccessTools.StaticFieldRefAccess<T>(field); } } catch (Exception) { } missing.Add("UpscaledFrameBuffer." + name); return null; } private static FieldRef<TOwner, T> Instance<TOwner, T>(string name, List<string> missing) { try