using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("SaveTheWindows")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace SaveTheWindows;
[BepInPlugin("starfi5h.plugin.SaveTheWindows", "SaveTheWindows", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
public const string GUID = "starfi5h.plugin.SaveTheWindows";
public const string NAME = "SaveTheWindows";
public const string VERSION = "1.0.0";
public static ManualLogSource Log;
public static ConfigFile ConfigFile;
private static Harmony harmony;
public void Awake()
{
//IL_0052: Unknown result type (might be due to invalid IL or missing references)
//IL_005c: Expected O, but got Unknown
Log = ((BaseUnityPlugin)this).Logger;
ConfigFile = ((BaseUnityPlugin)this).Config;
ConfigEntry<bool> obj = ((BaseUnityPlugin)this).Config.Bind<bool>("Config", "Enable Save Window Position", true, "启用窗口位置保存");
ConfigEntry<bool> val = ((BaseUnityPlugin)this).Config.Bind<bool>("Config", "Enable Drag Window Offset", true, "允许窗口部分超出边框");
harmony = new Harmony("starfi5h.plugin.SaveTheWindows");
if (obj.Value)
{
harmony.PatchAll(typeof(SaveWindow_Patch));
}
if (val.Value)
{
harmony.PatchAll(typeof(UIWindowDragOffset_Patch));
}
}
}
public class SaveWindow_Patch
{
private static readonly List<UIWindowDrag> _windows = new List<UIWindowDrag>();
[HarmonyPostfix]
[HarmonyPatch(typeof(UIRoot), "OpenGameUI")]
public static void OpenGameUI()
{
CollectWindows();
LoadWindowPos();
}
[HarmonyPostfix]
[HarmonyPatch(typeof(GameMain), "End")]
public static void OnGameEnd()
{
SaveWindowPos();
}
private static void CollectWindows()
{
Transform val = ((Component)UIRoot.instance.overlayCanvas).transform.Find("In Game/Windows");
if ((Object)(object)val != (Object)null)
{
_windows.Clear();
((Component)val).GetComponentsInChildren<UIWindowDrag>(true, _windows);
Plugin.Log.LogInfo((object)$"Collect {_windows.Count} windows");
}
else
{
Plugin.Log.LogWarning((object)"Can't find In Game/Windows");
}
}
private static void LoadWindowPos()
{
//IL_0042: Unknown result type (might be due to invalid IL or missing references)
//IL_004d: 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)
//IL_0054: 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_0063: Unknown result type (might be due to invalid IL or missing references)
foreach (UIWindowDrag window in _windows)
{
if (!((Object)(object)window?.dragTrans == (Object)null))
{
string name = ((Object)window).name;
RectTransform dragTrans = window.dragTrans;
Vector2 value = Plugin.ConfigFile.Bind<Vector2>("Window Position", name, Vector2.zero, (ConfigDescription)null).Value;
if (!(value == Vector2.zero))
{
dragTrans.anchoredPosition = value;
}
}
}
}
private static void SaveWindowPos()
{
//IL_003a: Unknown result type (might be due to invalid IL or missing references)
//IL_004b: Unknown result type (might be due to invalid IL or missing references)
//IL_006b: Unknown result type (might be due to invalid IL or missing references)
//IL_0076: Unknown result type (might be due to invalid IL or missing references)
Vector2 value = default(Vector2);
foreach (UIWindowDrag window in _windows)
{
if (!((Object)(object)window?.dragTrans == (Object)null))
{
string name = ((Object)window).name;
RectTransform dragTrans = window.dragTrans;
((Vector2)(ref value))..ctor((float)Mathf.RoundToInt(dragTrans.anchoredPosition.x), (float)Mathf.RoundToInt(dragTrans.anchoredPosition.y));
Plugin.ConfigFile.Bind<Vector2>("Window Position", name, Vector2.zero, (ConfigDescription)null).Value = value;
}
}
}
}
public class UIWindowDragOffset_Patch
{
[HarmonyTranspiler]
[HarmonyPatch(typeof(UIWindowDrag), "Update")]
public static IEnumerable<CodeInstruction> UIWindowDrag_Update_Transpiler(IEnumerable<CodeInstruction> instructions)
{
//IL_0002: Unknown result type (might be due to invalid IL or missing references)
//IL_0030: Unknown result type (might be due to invalid IL or missing references)
//IL_0036: Expected O, but got Unknown
//IL_004e: Unknown result type (might be due to invalid IL or missing references)
//IL_0054: Expected O, but got Unknown
//IL_0071: Unknown result type (might be due to invalid IL or missing references)
//IL_0077: Expected O, but got Unknown
//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
//IL_00ab: Expected O, but got Unknown
//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
//IL_00c9: Expected O, but got Unknown
//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
//IL_00ec: Expected O, but got Unknown
try
{
return new CodeMatcher(instructions, (ILGenerator)null).MatchForward(false, (CodeMatch[])(object)new CodeMatch[1]
{
new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Call && ((MethodInfo)i.operand).Name == "WorldToScreenPoint"), (string)null)
}).RemoveInstruction().InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[2]
{
new CodeInstruction(OpCodes.Ldarg_0, (object)null),
new CodeInstruction(OpCodes.Call, (object)AccessTools.Method(typeof(UIWindowDragOffset_Patch), "WorldToScreenPoint_Min", (Type[])null, (Type[])null))
})
.MatchForward(false, (CodeMatch[])(object)new CodeMatch[1]
{
new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Call && ((MethodInfo)i.operand).Name == "WorldToScreenPoint"), (string)null)
})
.RemoveInstruction()
.InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[2]
{
new CodeInstruction(OpCodes.Ldarg_0, (object)null),
new CodeInstruction(OpCodes.Call, (object)AccessTools.Method(typeof(UIWindowDragOffset_Patch), "WorldToScreenPoint_Max", (Type[])null, (Type[])null))
})
.InstructionEnumeration();
}
catch (Exception ex)
{
Plugin.Log.LogError((object)"Transpiler UIWindowDrag.Update error");
Plugin.Log.LogError((object)ex);
return instructions;
}
}
private static Vector2 WorldToScreenPoint_Min(Vector3 worldPos, UIWindowDrag window)
{
//IL_0000: Unknown result type (might be due to invalid IL or missing references)
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
//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)
//IL_002b: Unknown result type (might be due to invalid IL or missing references)
//IL_0030: Unknown result type (might be due to invalid IL or missing references)
//IL_003e: 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)
Vector2 val = UIRoot.WorldToScreenPoint(worldPos);
Rect rect = window.refTrans.rect;
float num = ((Rect)(ref rect)).width * 3f / 4f;
rect = window.refTrans.rect;
return val + new Vector2(num, ((Rect)(ref rect)).height - 60f);
}
private static Vector2 WorldToScreenPoint_Max(Vector3 worldPos, UIWindowDrag window)
{
//IL_0000: Unknown result type (might be due to invalid IL or missing references)
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
//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)
//IL_002b: Unknown result type (might be due to invalid IL or missing references)
//IL_0030: Unknown result type (might be due to invalid IL or missing references)
//IL_003e: 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)
Vector2 val = UIRoot.WorldToScreenPoint(worldPos);
Rect rect = window.refTrans.rect;
float num = ((Rect)(ref rect)).width * 3f / 4f;
rect = window.refTrans.rect;
return val - new Vector2(num, ((Rect)(ref rect)).height - 60f);
}
}