using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GeistTools.Tweaks.Core;
using GeistTools.Tweaks.Core.Tweaks;
using Microsoft.CodeAnalysis;
using MonoMod.Utils;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("GeistTools.Tweaks")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+462e34aa4339cb97a3500f6049bc26c427002359")]
[assembly: AssemblyProduct("My first plugin")]
[assembly: AssemblyTitle("GeistTools.Tweaks")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
internal sealed class EmbeddedAttribute : Attribute
{
}
}
namespace System.Runtime.CompilerServices
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
internal sealed class RefSafetyRulesAttribute : Attribute
{
public readonly int Version;
public RefSafetyRulesAttribute(int P_0)
{
Version = P_0;
}
}
}
namespace GeistTools.Tweaks
{
[BepInPlugin("com.alyren.geist.tweaks", "GeistTools.Tweaks", "0.1.0")]
public class Plugin : BaseUnityPlugin
{
internal static ManualLogSource Logger;
private List<ITweak> Tweaks { get; } = new List<ITweak>();
private void Awake()
{
Logger = ((BaseUnityPlugin)this).Logger;
Logger.LogInfo((object)"Tweaks loaded!");
LoadTweaks();
InitializeTweaks();
}
private void LoadTweaks()
{
if (Tweaks.Count != 0)
{
Logger.LogError((object)"Tweaks already loaded?");
return;
}
try
{
Assembly assembly = typeof(Plugin).Assembly;
Type tweakInterface = typeof(ITweak);
Type[] array = (from type in assembly.GetTypes()
where !type.IsInterface && !type.IsAbstract
where tweakInterface.IsAssignableFrom(type)
select type).ToArray();
Logger.LogInfo((object)$"Tweaks found: {array.Length}");
Type[] array2 = array;
foreach (Type tweakType in array2)
{
ITweak tweak = TryLoadTweak(tweakType);
if (tweak != null)
{
Logger.LogInfo((object)(" - Loaded: [" + tweak.Name + "]"));
Tweaks.Add(tweak);
}
}
}
catch (Exception ex)
{
Logger.LogError((object)("Failed to load tweaks. No tweaks will be initialized. Reason: " + ex.Message));
}
}
private ITweak TryLoadTweak(Type tweakType)
{
ITweak result = null;
try
{
Logger.LogDebug((object)("Loading tweak from type [" + tweakType.Name + "]"));
result = (ITweak)Activator.CreateInstance(tweakType);
}
catch (Exception ex)
{
Logger.LogError((object)("Failed to load tweak [" + tweakType.Name + "]: " + ex.Message));
}
return result;
}
private void InitializeTweaks()
{
TweakLoadContainer container = new TweakLoadContainer
{
Config = ((BaseUnityPlugin)this).Config
};
foreach (ITweak tweak in Tweaks)
{
try
{
tweak.Awake(container);
}
catch (Exception ex)
{
Logger.LogError((object)("Failed calling Awake on Tweak [" + tweak.Name + "]. Reason: " + ex.Message));
}
}
}
}
public static class MyPluginInfo
{
public const string PLUGIN_GUID = "GeistTools.Tweaks";
public const string PLUGIN_NAME = "My first plugin";
public const string PLUGIN_VERSION = "1.0.0";
}
}
namespace GeistTools.Tweaks.Core
{
internal class TweakLoadContainer
{
public ConfigFile Config { get; set; }
}
}
namespace GeistTools.Tweaks.Core.Tweaks
{
internal interface ITweak
{
string Name { get; }
void Awake(TweakLoadContainer container);
}
internal class UiScalingFix : ITweak
{
private ConfigEntry<bool> fixEnabled;
private ConfigEntry<float> widthToHeightRatio;
private ConfigEntry<int> referenceResolutionWidth;
private ConfigEntry<int> referenceResolutionHeight;
private ConfigEntry<float> escapeMenuScaleOverride;
private Dictionary<string, ConfigEntry<float>> customOverrides = new Dictionary<string, ConfigEntry<float>>();
public string Name => "UI Scaling Fix";
private ConfigFile Config { get; set; }
private List<CanvasScaler> scalersInScene { get; set; } = new List<CanvasScaler>();
public void Awake(TweakLoadContainer container)
{
Config = container.Config;
LoadConfig();
SceneManager.sceneLoaded += OnSceneLoaded;
}
public void LoadConfig()
{
fixEnabled = Config.Bind<bool>("Tweaks.UiScalingFix", "Enabled", true, "Enable the UI Scaling Fix.");
widthToHeightRatio = Config.Bind<float>("Tweaks.UiScalingFix", "ScaleRatio", 1f, "Ratio of scaling from screen width to screen height. (Keep 1 if ultrawide)");
referenceResolutionWidth = Config.Bind<int>("Tweaks.UiScalingFix", "UIResolutionWidth", 1600, "Reference Resolution X");
referenceResolutionHeight = Config.Bind<int>("Tweaks.UiScalingFix", "UIResolutionHeight", 900, "Reference Resolution Y");
escapeMenuScaleOverride = Config.Bind<float>("Tweaks.UiScalingFix", "EscapeMenuOverride", 1.5f, "Scale value for escape menu. (-1 to disable)");
Extensions.AddRange<string, ConfigEntry<float>>(customOverrides, new Dictionary<string, ConfigEntry<float>> { { "Canvas_EscapeMenu", escapeMenuScaleOverride } });
}
private void OnSceneLoaded(Scene newScene, LoadSceneMode mode)
{
if (!fixEnabled.Value)
{
Plugin.Logger.LogDebug((object)"Skipping past scene load due to tweak being disabled in config.");
return;
}
Plugin.Logger.LogInfo((object)$"Scene loaded into [{((Scene)(ref newScene)).buildIndex}] \"{((Scene)(ref newScene)).name}\"");
RefreshScalersInScene();
ApplyFixes();
}
private void RefreshScalersInScene()
{
scalersInScene = (from canvas in Object.FindObjectsOfType<Canvas>(true)
where (int)canvas.renderMode == 0 || (int)canvas.renderMode == 1
select ((Component)canvas).GetComponent<CanvasScaler>()).Cast<CanvasScaler>().ToList();
}
private void ApplyFixes()
{
//IL_0068: Unknown result type (might be due to invalid IL or missing references)
//IL_006d: Unknown result type (might be due to invalid IL or missing references)
//IL_0082: Unknown result type (might be due to invalid IL or missing references)
//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
Plugin.Logger.LogInfo((object)$"Applying global adjustments to {scalersInScene.Count} CanvasScalers");
Vector2 referenceResolution = default(Vector2);
((Vector2)(ref referenceResolution))..ctor((float)referenceResolutionWidth.Value, (float)referenceResolutionHeight.Value);
foreach (CanvasScaler item in scalersInScene)
{
float matchWidthOrHeight = item.matchWidthOrHeight;
Vector2 referenceResolution2 = item.referenceResolution;
item.matchWidthOrHeight = widthToHeightRatio.Value;
item.referenceResolution = referenceResolution;
Plugin.Logger.LogDebug((object)(" Applied: [" + ((Object)item).name + "]"));
Plugin.Logger.LogDebug((object)$" - matchWidthOrHeight: {matchWidthOrHeight} -> {item.matchWidthOrHeight}");
Plugin.Logger.LogDebug((object)$" - referenceResolution: {referenceResolution2} -> {item.referenceResolution}");
if (customOverrides.TryGetValue(((Object)item).name, out var value))
{
float scaleFactor = item.scaleFactor;
item.scaleFactor = value.Value;
Plugin.Logger.LogDebug((object)$" - scaleFactor: {scaleFactor} -> {item.scaleFactor}");
}
}
}
}
}