Repository navigation
Expand file tree
/
Copy pathPlugin.cs
More file actions
96 lines (81 loc) · 3.87 KB
/
Copy pathPlugin.cs
File metadata and controls
96 lines (81 loc) · 3.87 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
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Unity.IL2CPP;
using Il2cppConsoleMod.Inspector;
using Il2cppConsoleMod.Web;
using Il2CppInterop.Runtime.Injection;
using UnityEngine;
namespace Il2cppConsoleMod;
[BepInPlugin(MyPluginInfo.PLUGIN_GUID, MyPluginInfo.PLUGIN_NAME, MyPluginInfo.PLUGIN_VERSION)]
public class Plugin : BasePlugin
{
private HttpDebugServer? _server;
private CSharpScriptSession? _session;
private LogBuffer? _logs;
public override void Load()
{
var listenAddress = Config.Bind(
"Server", "ListenAddress", "127.0.0.1",
"Address to listen on. Use 0.0.0.0 to allow LAN access.\n监听地址,0.0.0.0 表示允许局域网访问。");
var port = Config.Bind(
"Server", "Port", 18765,
new ConfigDescription(
"HTTP port for the debug server.\n调试服务器 HTTP 端口。",
new AcceptableValueRange<int>(1, 65535)));
var enabled = Config.Bind(
"Security", "Enabled", false,
"Enable the HTTP debug server. This mod can execute arbitrary C# code and may lead to command execution or other security issues. Use with caution.\n" +
"启用 HTTP 调试服务。该 mod 可执行任意 C# 代码,可能导致命令执行等安全问题,请谨慎使用。");
var acknowledgeRisk = Config.Bind(
"Security", "AcknowledgeRisk", false,
"Acknowledge the security risks of arbitrary code execution. Both this and Enabled must be true to start.\n" +
"确认任意代码执行的安全风险。此开关和 Enabled 都必须为 true 才能启动。");
var nativeUpdateHook = Config.Bind(
"Compatibility", "UseNativeUpdateHook", false,
"Run main-thread work from a Dobby hook on EventSystem.Update instead of an injected MonoBehaviour. Turn on for Android hosts where the injected Update never runs.\n" +
"用 Dobby 挂钩 EventSystem.Update 来执行主线程任务,代替注入的 MonoBehaviour。注入组件的 Update 不运行的 Android 环境请打开。");
if (!enabled.Value || !acknowledgeRisk.Value)
{
Log.LogWarning("Il2cppConsoleMod is disabled. Set Security.Enabled=true and Security.AcknowledgeRisk=true in the config to enable.");
return;
}
var token = GenerateToken();
Log.LogWarning($"Il2cppConsoleMod token: {token}");
try
{
if (nativeUpdateHook.Value)
{
NativeUpdateHook.Install(Log);
}
else
{
ClassInjector.RegisterTypeInIl2Cpp<RuntimeCSharpHost>();
RuntimeCSharpHost.Create();
}
_logs = new LogBuffer();
BepInEx.Logging.Logger.Listeners.Add(_logs);
TypeCatalog.Prewarm();
_session = new CSharpScriptSession();
var api = new WebApi(_session, new UnityGameHost(), _logs);
_server = new HttpDebugServer(listenAddress.Value, port.Value, token, Log, _session, api);
_server.Start();
foreach (var url in _server.BaseUrls())
Log.LogWarning($"Il2cppConsoleMod web console: {url}/#token={token}");
if (_server.IsLoopbackOnly && Application.platform == RuntimePlatform.Android)
Log.LogWarning($"Il2cppConsoleMod: on the PC run `adb forward tcp:{port.Value} tcp:{port.Value}` to open the link above");
}
catch (Exception ex)
{
Log.LogError($"Il2cppConsoleMod failed to start: {ex}");
}
}
public override bool Unload()
{
_server?.Stop();
NativeUpdateHook.Uninstall();
if (_logs != null)
BepInEx.Logging.Logger.Listeners.Remove(_logs);
return true;
}
private static string GenerateToken() => Guid.NewGuid().ToString("N");
}