-
Notifications
You must be signed in to change notification settings - Fork 455
Expand file tree
/
Copy pathReplayCapture.cs
More file actions
143 lines (126 loc) · 4.85 KB
/
ReplayCapture.cs
File metadata and controls
143 lines (126 loc) · 4.85 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
using System;
using System.Collections.Generic;
using Brigadier.NET.Builder;
using MinecraftClient.CommandHandler;
using MinecraftClient.CommandHandler.Patch;
using MinecraftClient.Protocol;
using MinecraftClient.Scripting;
using Tomlet.Attributes;
namespace MinecraftClient.ChatBots
{
/// <summary>
/// Record and save replay file that can be used by the Replay mod (https://www.replaymod.com/)
/// </summary>
public class ReplayCapture : ChatBot
{
public const string CommandName = "replay";
public static Configs Config = new();
[TomlDoNotInlineObject]
public class Configs
{
[NonSerialized]
private const string BotName = "ReplayCapture";
public bool Enabled = false;
[TomlInlineComment("$ChatBot.ReplayCapture.Backup_Interval$")]
public double Backup_Interval = 300.0;
public void OnSettingUpdate()
{
if (Backup_Interval < -1)
Backup_Interval = -1;
}
}
private ReplayHandler? replay;
private int backupCounter = -1;
public override void Initialize()
{
SetNetworkPacketEventEnabled(true);
replay = new ReplayHandler(GetProtocolVersion());
replay.MetaData.serverName = GetServerHost() + GetServerPort();
backupCounter = Settings.DoubleToTick(Config.Backup_Interval);
McClient.dispatcher.Register(l => l.Literal("help")
.Then(l => l.Literal(CommandName)
.Executes(r => OnCommandHelp(r.Source, string.Empty))
)
);
McClient.dispatcher.Register(l => l.Literal(CommandName)
.Then(l => l.Literal("save")
.Executes(r => OnCommandSave(r.Source)))
.Then(l => l.Literal("stop")
.Executes(r => OnCommandStop(r.Source)))
.Then(l => l.Literal("_help")
.Executes(r => OnCommandHelp(r.Source, string.Empty))
.Redirect(McClient.dispatcher.GetRoot().GetChild("help").GetChild(CommandName)))
);
}
public override void OnUnload()
{
McClient.dispatcher.Unregister(CommandName);
McClient.dispatcher.GetRoot().GetChild("help").RemoveChild(CommandName);
}
private int OnCommandHelp(CmdResult r, string? cmd)
{
return r.SetAndReturn(cmd switch
{
#pragma warning disable format // @formatter:off
_ => string.Format(Translations.general_available_cmd, "save, stop")
+ '\n' + McClient.dispatcher.GetAllUsageString(CommandName, false),
#pragma warning restore format // @formatter:on
});
}
private int OnCommandSave(CmdResult r)
{
try
{
if (replay!.RecordRunning)
{
replay.CreateBackupReplay(@"replay_recordings\" + replay.GetReplayDefaultName());
return r.SetAndReturn(CmdResult.Status.Done, Translations.bot_replayCapture_created);
}
else
return r.SetAndReturn(CmdResult.Status.Fail, Translations.bot_replayCapture_restart);
}
catch (Exception e)
{
return r.SetAndReturn(CmdResult.Status.Fail, e.Message);
}
}
private int OnCommandStop(CmdResult r)
{
try
{
if (replay!.RecordRunning)
{
replay.OnShutDown();
return r.SetAndReturn(CmdResult.Status.Done, Translations.bot_replayCapture_stopped);
}
else
return r.SetAndReturn(CmdResult.Status.Fail, Translations.bot_replayCapture_restart);
}
catch (Exception e)
{
return r.SetAndReturn(CmdResult.Status.Fail, e.Message);
}
}
public override void OnNetworkPacket(int packetID, List<byte> packetData, bool isLogin, bool isInbound)
{
replay!.AddPacket(packetID, packetData, isLogin, isInbound);
}
public override void Update()
{
if (Config.Backup_Interval > 0 && replay!.RecordRunning)
{
if (backupCounter <= 0)
{
replay.CreateBackupReplay(@"recording_cache\REPLAY_BACKUP.mcpr");
backupCounter = Settings.DoubleToTick(Config.Backup_Interval);
}
else backupCounter--;
}
}
public override int OnDisconnect(DisconnectReason reason, string message)
{
replay!.OnShutDown();
return base.OnDisconnect(reason, message);
}
}
}