This repository was archived by the owner on Apr 8, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cs
More file actions
190 lines (169 loc) · 6.28 KB
/
Main.cs
File metadata and controls
190 lines (169 loc) · 6.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using BaseMacro.Macro;
using HarmonyLib;
using SA.GoogleDoc;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityModManagerNet;
using static UnityModManagerNet.UnityModManager;
#nullable enable
namespace BaseMacro
{
#if DEBUG
[EnableReloading]
#endif
public static class Main
{
public static UnityModManager.ModEntry? Mod { get; private set; }
public static Harmony? Harmony { get; private set; }
public static Settings Settings { get; private set; } = null!;
private static GameObject? _uiObject;
public static bool Load(UnityModManager.ModEntry modEntry)
{
Mod = modEntry;
Settings = Settings.Load(modEntry);
// 手动初始化 InputSystem
if (InputSystem.Initialize())
{
modEntry.Logger.Log("[InputSystem] 初始化成功");
}
else
{
modEntry.Logger.Log("[InputSystem] 初始化失败");
}
if (InputSystem.IsUsingNtFunctions())
{
modEntry.Logger.Log("[InputSystem] 当前使用 NT 内核函数");
}
else
{
modEntry.Logger.Log("[InputSystem] 当前使用传统输入模拟");
}
modEntry.OnToggle = OnToggle;
modEntry.OnGUI = Settings.OnGUI;
modEntry.OnSaveGUI = Settings.OnSaveGUI;
modEntry.OnUnload = Unload;
Harmony = new Harmony(modEntry.Info.Id);
return true;
}
public static bool Unload(UnityModManager.ModEntry modEntry)
{
var harmony = new Harmony(modEntry.Info.Id);
harmony.UnpatchAll(modEntry.Info.Id);
// If you have created any dependent objects, they also need to be deleted.
return true;
}
public static bool IsDebugAssembly()
{
Assembly assembly = Assembly.GetExecutingAssembly();
var attribute = assembly.GetCustomAttribute<DebuggableAttribute>();
if (attribute == null)
return false;
return attribute.IsJITOptimizerDisabled;
}
private static bool OnToggle(UnityModManager.ModEntry modEntry, bool value)
{
if (value)
{
#if DEBUG
// None
#else
if (modEntry.Info.Version != "1.1.1" || modEntry.Info.Id != "BaseMacro" || modEntry.Info.DisplayName != "Base Macro" || modEntry.Info.Author != "HitMargin" || modEntry.Info.AssemblyName != "BaseMacro.dll" || modEntry.Info.EntryMethod != "BaseMacro.Main.Load")
{
Mod?.Logger.Error("Modifying the Info.json file is NOT allowed!");
Application.Quit();
}
#endif
Mod?.Info.IsCheat = true;
if (Mod!.Info.IsCheat)
{
Mod.Info.DisplayName += " (Cheat)";
}
if (IsDebugAssembly())
Mod.Info.DisplayName += " <color=grey>(Debug)</color>";
if (Settings.IsBeta)
Mod.Info.Version += $"\nBeta{Settings.BetaVersion}";
IsEnabled = true;
Harmony?.PatchAll(Assembly.GetExecutingAssembly());
if (_uiObject == null)
{
_uiObject = new GameObject("MacroText");
_uiObject.AddComponent<ShowText>();
UnityEngine.Object.DontDestroyOnLoad(_uiObject);
TrySetWindowTitle($"{GetClean(modEntry.Info.DisplayName)}, {GetClean(modEntry.Info.Version)}, {modEntry.Info.Author}");
}
}
else
{
IsEnabled = false;
Harmony?.UnpatchAll();
TrySetWindowTitle(null);
InputSystem.EmergencyStop();
}
return true;
}
public static string GetClean(string version)
{
if (version.Contains("<color="))
{
int startIndex = version.IndexOf('>') + 1;
int endIndex = version.LastIndexOf('<');
if (startIndex > 0 && endIndex > startIndex)
{
return version.Substring(startIndex, endIndex - startIndex);
}
}
return version;
}
[DllImport("user32.dll")]
private static extern bool SetWindowText(IntPtr hWnd, string lpString);
private static void TrySetWindowTitle(string? title)
{
try
{
// 获取当前进程
Process currentProcess = Process.GetCurrentProcess();
// 等待主窗口句柄可用
for (int i = 0; i < 10 && currentProcess.MainWindowHandle == IntPtr.Zero; i++)
{
currentProcess.Refresh();
System.Threading.Thread.Sleep(100);
}
IntPtr hwnd = currentProcess.MainWindowHandle;
if (hwnd != IntPtr.Zero)
{
string newTitle = $"A Dance of Fire and Ice";
if (title != null)
{
// 修复:将换行符替换为空格
string cleanTitle = title.Replace('\n', ' ').Replace('\r', ' ');
newTitle = $"A Dance of Fire and Ice - {cleanTitle}";
}
if (SetWindowText(hwnd, newTitle))
{
Mod?.Logger.Log($"成功设置窗口标题: {newTitle}");
}
else
{
Mod?.Logger.Log("设置窗口标题失败");
}
}
else
{
Mod?.Logger.Log("未获取到主窗口句柄");
}
}
catch (Exception ex)
{
Mod?.Logger.Log($"设置窗口标题异常: {ex.Message}");
}
}
public static bool IsEnabled { get; internal set; }
}
}