Add support for creating replay mod capture files (#1246)

* Add test replay handler
* Fix incorrect built raw packet
* Fix incorrect built raw packet
* Add filter
* Add not working zip lib
* Add dotNetZip lib and complete basic function
* Update ReplayHandler.cs
* Complete Replay handler
Without client player handling
* Complete replay mod
- New ChatBot OnNetworkPacket event
* Add auto-backup and command for Replay Mod
* Add ReplayMod description to readme
* Small naming changes, fix compile error on .NET4.0
* ReplayHandler slight optimizations
Use Path.Combine to automatically use Windows '\' or Linux '/'
Move re-usable common parts outside the Replay handler
Small optimizations in building JSON strings
Co-authored-by: ORelio <oreliogitantispam.l0gin@spamgourmet.com>
This commit is contained in:
ReinforceZwei 2020-09-07 03:51:42 +08:00 committed by GitHub
parent cd1badb9d6
commit 7e20e409a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
47 changed files with 32732 additions and 21 deletions

View file

@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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
{
private ReplayHandler replay;
private int backupInterval = 3000; // Unit: second * 10
private int backupCounter = -1;
public ReplayCapture(int backupInterval)
{
if (backupInterval != -1)
this.backupInterval = backupInterval * 10;
else this.backupInterval = -1;
}
public override void Initialize()
{
SetNetworkPacketEventEnabled(true);
replay = new ReplayHandler(GetProtocolVersion());
replay.MetaData.serverName = GetServerHost() + GetServerPort();
backupCounter = backupInterval;
RegisterChatBotCommand("replay", "replay command", Command);
}
public override void OnNetworkPacket(int packetID, List<byte> packetData, bool isLogin, bool isInbound)
{
replay.AddPacket(packetID, packetData, isLogin, isInbound);
}
public override void Update()
{
if (backupInterval > 0 && replay.RecordRunning)
{
if (backupCounter <= 0)
{
replay.CreateBackupReplay(@"recording_cache\REPLAY_BACKUP.mcpr");
backupCounter = backupInterval;
}
else backupCounter--;
}
}
public override bool OnDisconnect(DisconnectReason reason, string message)
{
replay.OnShutDown();
return base.OnDisconnect(reason, message);
}
public string Command(string cmd, string[] args)
{
try
{
if (replay.RecordRunning)
{
if (args.Length > 0)
{
switch (args[0].ToLower())
{
case "save":
{
replay.CreateBackupReplay(@"replay_recordings\" + replay.GetReplayDefaultName());
return "Replay file created.";
}
case "stop":
{
replay.OnShutDown();
return "Record stopped.";
}
}
}
return "Available commands: save, stop";
}
else return "Record was stopped. Restart the program to start another record.";
}
catch (Exception e)
{
return e.Message;
}
}
}
}