2020-09-07 03:51:42 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using MinecraftClient.Protocol;
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
{
|
2022-10-02 18:31:08 +08:00
|
|
|
|
private ReplayHandler? replay;
|
|
|
|
|
|
private readonly int backupInterval = 3000; // Unit: second * 10
|
2020-09-07 03:51:42 +08:00
|
|
|
|
private int backupCounter = -1;
|
|
|
|
|
|
|
|
|
|
|
|
public ReplayCapture(int backupInterval)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (backupInterval != -1)
|
|
|
|
|
|
this.backupInterval = backupInterval * 10;
|
2022-10-02 18:31:08 +08:00
|
|
|
|
else
|
|
|
|
|
|
this.backupInterval = -1;
|
2020-09-07 03:51:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void Initialize()
|
|
|
|
|
|
{
|
|
|
|
|
|
SetNetworkPacketEventEnabled(true);
|
|
|
|
|
|
replay = new ReplayHandler(GetProtocolVersion());
|
|
|
|
|
|
replay.MetaData.serverName = GetServerHost() + GetServerPort();
|
|
|
|
|
|
backupCounter = backupInterval;
|
|
|
|
|
|
|
2020-10-17 19:41:31 +08:00
|
|
|
|
RegisterChatBotCommand("replay", Translations.Get("bot.replayCapture.cmd"), "replay <save|stop>", Command);
|
2020-09-07 03:51:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void OnNetworkPacket(int packetID, List<byte> packetData, bool isLogin, bool isInbound)
|
|
|
|
|
|
{
|
2022-10-02 18:31:08 +08:00
|
|
|
|
replay!.AddPacket(packetID, packetData, isLogin, isInbound);
|
2020-09-07 03:51:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void Update()
|
|
|
|
|
|
{
|
2022-10-02 18:31:08 +08:00
|
|
|
|
if (backupInterval > 0 && replay!.RecordRunning)
|
2020-09-07 03:51:42 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (backupCounter <= 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
replay.CreateBackupReplay(@"recording_cache\REPLAY_BACKUP.mcpr");
|
|
|
|
|
|
backupCounter = backupInterval;
|
|
|
|
|
|
}
|
|
|
|
|
|
else backupCounter--;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override bool OnDisconnect(DisconnectReason reason, string message)
|
|
|
|
|
|
{
|
2022-10-02 18:31:08 +08:00
|
|
|
|
replay!.OnShutDown();
|
2020-09-07 03:51:42 +08:00
|
|
|
|
return base.OnDisconnect(reason, message);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string Command(string cmd, string[] args)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2022-10-02 18:31:08 +08:00
|
|
|
|
if (replay!.RecordRunning)
|
2020-09-07 03:51:42 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (args.Length > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
switch (args[0].ToLower())
|
|
|
|
|
|
{
|
|
|
|
|
|
case "save":
|
|
|
|
|
|
{
|
|
|
|
|
|
replay.CreateBackupReplay(@"replay_recordings\" + replay.GetReplayDefaultName());
|
2020-10-17 19:41:31 +08:00
|
|
|
|
return Translations.Get("bot.replayCapture.created");
|
2020-09-07 03:51:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
case "stop":
|
|
|
|
|
|
{
|
|
|
|
|
|
replay.OnShutDown();
|
2020-10-17 19:41:31 +08:00
|
|
|
|
return Translations.Get("bot.replayCapture.stopped");
|
2020-09-07 03:51:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-10-17 19:41:31 +08:00
|
|
|
|
return Translations.Get("general.available_cmd", "save, stop");
|
2020-09-07 03:51:42 +08:00
|
|
|
|
}
|
2020-10-17 19:41:31 +08:00
|
|
|
|
else return Translations.Get("bot.replayCapture.restart");
|
2020-09-07 03:51:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
return e.Message;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|