-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoreTerminalCommandsPlugin.cs
475 lines (432 loc) · 19.1 KB
/
MoreTerminalCommandsPlugin.cs
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using LethalAPI.LibTerminal;
using LethalAPI.LibTerminal.Attributes;
using LethalAPI.LibTerminal.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity.Netcode;
using UnityEngine;
namespace MoreTerminalCommands
{
[BepInDependency(LethalAPI.LibTerminal.PluginInfo.PLUGIN_GUID, BepInDependency.DependencyFlags.HardDependency)]
[BepInPlugin("cn.chuxiaaaa.plugin.MoreTerminalCommands", "MoreTerminalCommands", "1.0.8")]
public class MoreTerminalCommandsPlugin : BaseUnityPlugin
{
private TerminalModRegistry Commands;
public static ManualLogSource ManualLog = null;
public static Dictionary<int, int> YRots = new Dictionary<int, int>();
public static bool Debug { get; set; }
ConfigEntry<string> LangugeConfig;
void Awake()
{
try
{
LangugeConfig = Config.Bind("config", "languge", "en_US", "mod languge");
Harmony.CreateAndPatchAll(typeof(TerminalAccessibleObjectPatch), "cn.chuxiaaaa.plugin.MoreTerminalCommands");
Harmony.CreateAndPatchAll(typeof(PlayerControllerBPatch3), "cn.chuxiaaaa.plugin.MoreTerminalCommands");
Harmony.CreateAndPatchAll(typeof(PlayerControllerBPatch2), "cn.chuxiaaaa.plugin.MoreTerminalCommands");
Harmony.CreateAndPatchAll(typeof(PlayerControllerBPatch), "cn.chuxiaaaa.plugin.MoreTerminalCommands");
LocalizationManager.SetLanguage(LangugeConfig.Value);
Commands = TerminalRegistry.CreateTerminalRegistry();
Commands.RegisterFrom(this);
ManualLog = Logger;
Logger.LogInfo("More Terminal Commands Loaded!");
}
catch (Exception ex)
{
Logger.LogError(ex.ToString());
}
}
private static float CalculateLootValue()
{
List<GrabbableObject> list = (from obj in GameObject.Find("/Environment/HangarShip").GetComponentsInChildren<GrabbableObject>()
where obj.name != "ClipboardManual" && obj.name != "StickyNoteItem"
select obj).ToList<GrabbableObject>();
return (float)list.Sum((GrabbableObject scrap) => scrap.scrapValue);
}
public void CollectObjectsOfType<T>(List<T> list, Predicate<T> predicate = null) where T : MonoBehaviour
{
foreach (var obj in UnityEngine.Object.FindObjectsOfType<T>())
{
if (predicate == null || predicate(obj))
{
list.Add(obj);
}
}
}
/// <summary>
/// 字符串转Vector3
/// </summary>
/// <param name="p_sVec3">需要转换的字符串</param>
/// <returns></returns>
public static Vector3 GetVec3ByString(string p_sVec3)
{
if (p_sVec3.Length <= 0)
return Vector3.zero;
string[] tmp_sValues = p_sVec3.Trim(' ').Split(',');
if (tmp_sValues != null && tmp_sValues.Length == 3)
{
float tmp_fX = float.Parse(tmp_sValues[0]);
float tmp_fY = float.Parse(tmp_sValues[1]);
float tmp_fZ = float.Parse(tmp_sValues[2]);
return new Vector3(tmp_fX, tmp_fY, tmp_fZ);
}
return Vector3.zero;
}
[TerminalCommand("Debug"), CommandInfo("Layout Debug")]
public string DebugCommand()
{
Debug = !Debug;
return Debug.ToString();
}
[TerminalCommand("Load"), CommandInfo("Load Layout From File")]
public string Load(string name)
{
Dictionary<string, int> objs = new Dictionary<string, int>();
var go = (from obj in GameObject.Find("/Environment/HangarShip").GetComponentsInChildren<GrabbableObject>()
where obj.name != "ClipboardManual" && obj.name != "StickyNoteItem"
select obj).ToList<GrabbableObject>();
var lines = File.ReadAllLines($"{name}.txt").ToList();
var lp = GameNetworkManager.Instance.localPlayerController;
float carryWeight = lp.carryWeight;
var localEulerAngles = lp.transform.localEulerAngles;
Logger.LogInfo("carryWeight:" + carryWeight);
if (lp.currentlyHeldObjectServer != null)
{
lp.DiscardHeldObject(false, null, new Vector3(0, 0, 0), true);
}
try
{
foreach (var grabbableObject in go)
{
if (!grabbableObject.isHeld && !grabbableObject.isPocketed)
{
int index = 1;
if (!objs.ContainsKey(grabbableObject.itemProperties.itemName))
{
objs.Add(grabbableObject.itemProperties.itemName, 1);
}
index = objs[grabbableObject.itemProperties.itemName];
Logger.LogInfo("index:" + index);
var line = lines.FindIndex(x => x.StartsWith($"{grabbableObject.itemProperties.itemName}{index}="));
string[] v = null;
if (line > -1)
{
v = lines[line].Split('=')[1].Split('|');
objs[grabbableObject.itemProperties.itemName]++;
}
else
{
line = lines.FindIndex(x => x.StartsWith($"{grabbableObject.itemProperties.itemName}-1="));
if (line > -1)
{
v = lines[line].Split('=')[1].Split('|');
}
else
{
line = lines.FindIndex(x => x.StartsWith($"Other="));
if (line > -1)
{
v = lines[line].Split('=')[1].Split('|');
}
}
}
if (v != null && v.Length > 0)
{
Logger.LogInfo("v[0]:" + v[0] + "|v[1]:" + v[1]);
Vector3 point = GetVec3ByString(v[0]);
if (grabbableObject.gameObject.transform.position == point)
{
continue;
}
grabbableObject.gameObject.transform.position = point;
var yrot = int.Parse(v[1]);
grabbableObject.floorYRot = (int)yrot;
grabbableObject.targetFloorPosition = GetVec3ByString(v[1]);
int key = grabbableObject.gameObject.GetComponent<NetworkObject>().GetInstanceID();
Logger.LogInfo("key:" + key + "," + yrot);
YRots.Add(key, (int)yrot);
grabbableObject.playerHeldBy = lp;
lp.currentlyHeldObjectServer = grabbableObject;
grabbableObject.EquipItem();
lp.DiscardHeldObject(false, null, new Vector3(0, 0, 0), true);
}
}
}
}
catch (Exception ex)
{
while (ex != null)
{
Logger.LogInfo(ex.ToString());
ex = ex.InnerException;
}
}
lp.transform.localEulerAngles = localEulerAngles;
lp.carryWeight = carryWeight;
return "ok!";
}
[TerminalCommand("Save"), CommandInfo("Save Layout To File")]
public string Save(string name)
{
var go = (from obj in GameObject.Find("/Environment/HangarShip").GetComponentsInChildren<GrabbableObject>()
where obj.name != "ClipboardManual" && obj.name != "StickyNoteItem"
select obj).ToList<GrabbableObject>();
StringBuilder sb = new StringBuilder();
foreach (var item in go)
{
sb.AppendLine($"{item.itemProperties.itemName}={PositionToString(item.transform.position)}|{item.transform.localEulerAngles.y}");
}
var sbl = sb.ToString().Split('\n').ToList();
sbl.Sort();
File.WriteAllLines(name, sbl.ToArray());
return "ok!";
}
public static string PositionToString(Vector3 postion)
{
return postion.ToString().Replace("(", "").Replace(")", "");
}
[TerminalCommand("dm"), CommandInfo("查看所有机关(机枪、地雷、机械门)代码")]
public string dm()
{
return Code();
}
[TerminalCommand("Code"), CommandInfo("List the codes of turrets, mines, and mechanical doors")]
public string Code()
{
StringBuilder sb = new StringBuilder();
TerminalAccessibleObject[] array = UnityEngine.Object.FindObjectsOfType<TerminalAccessibleObject>();
for (int i = 0; i < array.Length; i++)
{
sb.AppendLine($"{array[i].objectCode}|{LocalizationManager.GetString(array[i].name)}");
}
return sb.ToString();
}
public DateTime cooldown { get; set; }
[TerminalCommand("Lang"), CommandInfo("Set Plugin Languge")]
public string lang(string languge)
{
languge = languge.ToLower();
if (languge != "en_us" && languge != "zh_cn")
{
return "unknow languge:" + languge;
}
LangugeConfig.Value = languge;
LocalizationManager.SetLanguage(LangugeConfig.Value);
Config.Save();
return $"{LocalizationManager.GetString("Lang")}:" + languge;
}
[TerminalCommand("dr"), CommandInfo("探测设施内的敌人(冷却时间120秒)")]
public string dr()
{
return Enemy();
}
[TerminalCommand("Enemy"), CommandInfo("Detect enemies within the facility, with a cooldown period of 120 seconds")]
public string Enemy()
{
if (cooldown.AddSeconds(120) > DateTime.Now)
{
return $"[{LocalizationManager.GetString("Cooldown")}: {(cooldown.AddSeconds(120) - DateTime.Now).TotalSeconds} {LocalizationManager.GetString("sec")}.]";
}
List<EnemyAI> enemies = new List<EnemyAI>();
CollectObjectsOfType(enemies);
StringBuilder sb = new StringBuilder();
Dictionary<string, int> dics = new Dictionary<string, int>();
foreach (var item in enemies)
{
if (!item.isOutside && !item.isEnemyDead)
{
if (dics.ContainsKey(item.enemyType.enemyName))
{
dics[item.enemyType.enemyName]++;
}
else
{
dics.Add(item.enemyType.enemyName, 1);
}
}
}
if (dics.Count > 0)
{
sb.AppendLine($"[" + LocalizationManager.GetString("Enemies") + "]");
foreach (var item in dics)
{
sb.AppendLine(LocalizationManager.TryGetString("enemy_", item.Key) + ":" + item.Value);
}
sb.AppendLine();
sb.Append($"{LocalizationManager.GetString("enemy_tip1")}");
}
else
{
sb.Append($"{LocalizationManager.GetString("enemy_tip2")}");
}
cooldown = DateTime.Now;
return sb.ToString();
}
[TerminalCommand("kd"), CommandInfo("打开飞船的灯(只能打开,因为我不想看到来自终端的灯光秀)")]
public string kd()
{
return LightOn();
}
[TerminalCommand("km"), CommandInfo("打开或关闭飞船舱门(根据当前飞船舱门的状态决定)")]
public string km()
{
return ShipDoor();
}
[TerminalCommand("wk", true), CommandInfo("将监视器切换到工作中的同事(在工厂内部的同事)")]
public string wk()
{
return Work();
}
[TerminalCommand("Work",true), CommandInfo("Switch the monitor to colleagues who are working(colleagues in the factory)")]
public string Work()
{
return WorkMethod(false);
}
private string WorkMethod(bool callFromSelf)
{
Logger.LogInfo(StartOfRound.Instance.mapScreen.radarTargets.Count);
if (StartOfRound.Instance.mapScreen.radarTargets.Count > 1)
{
var @switch = false;
for (int i = StartOfRound.Instance.mapScreen.targetTransformIndex + 1; i < StartOfRound.Instance.mapScreen.radarTargets.Count; i++)
{
if (!StartOfRound.Instance.mapScreen.radarTargets[i].isNonPlayer)
{
var pl = StartOfRound.Instance.mapScreen.radarTargets[i].transform.gameObject.GetComponent<PlayerControllerB>();
if (pl != null && pl.isPlayerControlled && pl.isInsideFactory)
{
@switch = true;
StartOfRound.Instance.mapScreen.SwitchRadarTargetAndSync(i);
break;
}
}
}
if (!@switch)
{
StartOfRound.Instance.mapScreen.targetTransformIndex = 0;
if (!callFromSelf)
{
WorkMethod(true);
}
else
{
StartOfRound.Instance.mapScreen.SwitchRadarTargetAndSync(StartOfRound.Instance.mapScreen.targetTransformIndex);
}
}
}
else
{
StartOfRound.Instance.mapScreen.SwitchRadarTargetAndSync(0);
}
return "[" + LocalizationManager.GetString("Switch") + "]";
}
[TerminalCommand("LightOn"), CommandInfo("Turn On The Light")]
public string LightOn()
{
UnityEngine.Object.FindObjectOfType<ShipLights>().SetShipLightsServerRpc(true);
return $"[{LocalizationManager.GetString("ShipLight")}:{LocalizationManager.GetString("On")}]";
}
[TerminalCommand("ShipDoor"), CommandInfo("Open or Close the Ship Door")]
public string ShipDoor()
{
var shipdoor = UnityEngine.Object.FindObjectOfType<HangarShipDoor>().gameObject;
string animation = "";
if (StartOfRound.Instance.hangarDoorsClosed)
{
animation = "OpenDoor";
}
else
{
animation = "CloseDoor";
}
shipdoor.GetComponentsInChildren<AnimatedObjectTrigger>()
.First(trigger => trigger.animationString == animation)?
.GetComponentInParent<InteractTrigger>().onInteract.Invoke(GameNetworkManager.Instance?.localPlayerController);
return $"[{LocalizationManager.GetString("ShipDoor")}:{(!StartOfRound.Instance.hangarDoorsClosed ? LocalizationManager.GetString("Close") : LocalizationManager.GetString("Open"))}]";
}
private string TeleportMethod(bool Inverse)
{
ShipTeleporter[] shipTeleporters = UnityEngine.Object.FindObjectsOfType<ShipTeleporter>();
ShipTeleporter shipTeleporter = shipTeleporters.Where(x => x.isInverseTeleporter == Inverse).FirstOrDefault();
if (shipTeleporter == null)
{
return $"[Has 0 {(Inverse ? LocalizationManager.GetString("Inverse") : "")}{LocalizationManager.GetString("Teleporter")}]";
}
var cooldownTime = (float)typeof(ShipTeleporter).GetField("cooldownTime", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(shipTeleporter);
if (cooldownTime > 0)
{
return $"[{LocalizationManager.GetString("Cooldown")}: {cooldownTime} {LocalizationManager.GetString("sec")}.]";
}
shipTeleporter.PressTeleportButtonOnLocalClient();
return $"[{LocalizationManager.GetString("Teleport")}]";
}
[TerminalCommand("zj"), CommandInfo("计算当前飞船内的物品价值")]
public string zj()
{
return ShipLoot();
}
[TerminalCommand("ShipLoot"), CommandInfo("Calculate Loot Value In Ship")]
public string ShipLoot()
{
return $"[{LocalizationManager.GetString("ShipLoot")}:{CalculateLootValue()}]";
}
[TerminalCommand("Teleport"), CommandInfo("Teleport the player displayed on the current monitor")]
public string Teleport()
{
return TeleportMethod(false);
}
[TerminalCommand("pl"), CommandInfo("显示玩家列表(死亡、是否在飞船)")]
public string pl()
{
return Player();
}
[TerminalCommand("Player"), CommandInfo("Show the players info(death or inship)")]
public string Player()
{
List<PlayerControllerB> players = new List<PlayerControllerB>();
CollectObjectsOfType(players);
players = players.Where(x => !x.disconnectedMidGame &&
!x.playerUsername.Contains("Player #")).ToList();
int inShip = 0;
int death = 0;
int num = players.Count;
foreach (var item in players)
{
if (item.isPlayerDead)
{
death++;
}
else if (item.isInHangarShipRoom)
{
inShip++;
}
}
return $"[{LocalizationManager.GetString("Players")}]{Environment.NewLine}[{LocalizationManager.GetString("inShip")}]: {inShip}/{num}{Environment.NewLine}[{LocalizationManager.GetString("death")}]: {death}/{num}";
}
[TerminalCommand("tp"), CommandInfo("teleport the player displayed on the current monitor")]
public string tp()
{
return Teleport();
}
[TerminalCommand("Teleport2"), CommandInfo("Inverse Teleporter")]
public string Teleport2()
{
return TeleportMethod(true);
}
[TerminalCommand("tp2"), CommandInfo("Inverse Teleporter")]
public string tp2()
{
return Teleport2();
}
}
}