-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModCommandBase.cs
More file actions
92 lines (76 loc) · 2.52 KB
/
Copy pathModCommandBase.cs
File metadata and controls
92 lines (76 loc) · 2.52 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
using System;
using System.Reflection;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
namespace MossLib.Base;
public abstract class ModCommandBase
{
private static readonly object Lock = new();
private bool _isInitialized;
private string _lastErr = "";
// 提供受保护的方法供子类使用
protected ManualLogSource Logger { get; private set; }
protected void Initialize(ManualLogSource logger, Assembly pluginAssembly, Harmony harmonyInstance = null)
{
if (_isInitialized)
{
logger.LogWarning("ModCommandBase has already been initialized");
return;
}
lock (Lock)
{
if (_isInitialized) return;
Logger = logger;
var pluginAttribute = (BepInPlugin)Attribute.GetCustomAttribute(pluginAssembly, typeof(BepInPlugin));
var pluginGuid = pluginAttribute?.GUID ?? "unknown.plugin";
var harmony = harmonyInstance ?? new Harmony($"{pluginGuid}.modcommand");
harmony.PatchAll(GetType());
_isInitialized = true;
}
}
protected void LogToConsole(string text)
{
if (ConsoleScript.instance != null) ConsoleScript.instance.ExecuteCommand($"log {text.Replace(" ", " ")}");
}
public void ApplicationLogCallback(string condition, string stackTrace, LogType type)
{
if (_lastErr == condition)
return;
_lastErr = condition;
switch (type)
{
case LogType.Error or LogType.Assert:
LogToConsole($"<color=red>{condition}; {stackTrace}</color>");
break;
case LogType.Warning:
LogToConsole($"<color=yellow>{condition}; {stackTrace}</color>");
break;
case LogType.Log:
LogToConsole(condition);
break;
case LogType.Exception:
LogToConsole($"<color=red>{condition}; {stackTrace}</color>");
break;
}
}
[HarmonyPatch(typeof(ConsoleScript), "RegisterAllCommands")]
public class ConsoleScriptPatcher
{
[HarmonyPostfix]
// ReSharper disable once UnusedMember.Global
public static void RegisterModCommands()
{
}
}
[HarmonyPatch(typeof(ConsoleScript), "Awake")]
public class ConsoleScriptAwakePatcher
{
[HarmonyPostfix]
// ReSharper disable once UnusedMember.Global
public static void AddLogCallback()
{
}
}
}