Minecraft-Console-Client/MinecraftClient/Commands/Upgrade.cs

78 lines
3.1 KiB
C#
Raw Normal View History

2022-12-11 13:00:19 +08:00
using Brigadier.NET;
2022-12-06 20:32:46 +08:00
using Brigadier.NET.Builder;
using MinecraftClient.CommandHandler;
namespace MinecraftClient.Commands
{
class Upgrade : Command
{
public override string CmdName { get { return "upgrade"; } }
public override string CmdUsage { get { return "upgrade [-f|check|cancel|download]"; } }
public override string CmdDesc { get { return string.Empty; } }
2022-12-11 16:30:45 +08:00
public override void RegisterCommand(CommandDispatcher<CmdResult> dispatcher)
2022-12-11 13:00:19 +08:00
{
2022-12-06 20:32:46 +08:00
dispatcher.Register(l => l.Literal("help")
.Then(l => l.Literal(CmdName)
.Executes(r => GetUsage(r.Source, string.Empty))
.Then(l => l.Literal("cancel")
.Executes(r => GetUsage(r.Source, "cancel")))
.Then(l => l.Literal("check")
.Executes(r => GetUsage(r.Source, "check")))
.Then(l => l.Literal("download")
.Executes(r => GetUsage(r.Source, "download")))
)
);
dispatcher.Register(l => l.Literal(CmdName)
.Executes(r => DownloadUpdate(r.Source, force: false))
.Then(l => l.Literal("-f")
.Executes(r => DownloadUpdate(r.Source, force: true)))
.Then(l => l.Literal("download")
.Executes(r => DownloadUpdate(r.Source, force: false))
.Then(l => l.Literal("-f")
.Executes(r => DownloadUpdate(r.Source, force: true))))
.Then(l => l.Literal("check")
.Executes(r => CheckUpdate(r.Source)))
.Then(l => l.Literal("cancel")
.Executes(r => CancelDownloadUpdate(r.Source)))
.Then(l => l.Literal("_help")
.Redirect(dispatcher.GetRoot().GetChild("help").GetChild(CmdName)))
);
}
2022-12-11 13:00:19 +08:00
private int GetUsage(CmdResult r, string? cmd)
{
return r.SetAndReturn(cmd switch
{
2022-12-06 20:32:46 +08:00
#pragma warning disable format // @formatter:off
"cancel" => GetCmdDescTranslated(),
"check" => GetCmdDescTranslated(),
"download" => GetCmdDescTranslated(),
_ => GetCmdDescTranslated(),
#pragma warning restore format // @formatter:on
});
}
2022-12-06 20:32:46 +08:00
private static int DownloadUpdate(CmdResult r, bool force)
{
2022-12-01 23:37:15 +08:00
if (UpgradeHelper.DownloadLatestBuild(force))
2022-12-06 20:32:46 +08:00
return r.SetAndReturn(CmdResult.Status.Done, Translations.mcc_update_start);
2022-12-01 23:37:15 +08:00
else
2022-12-06 20:32:46 +08:00
return r.SetAndReturn(CmdResult.Status.Fail, Translations.mcc_update_already_running);
}
2022-12-06 20:32:46 +08:00
private static int CancelDownloadUpdate(CmdResult r)
{
UpgradeHelper.CancelDownloadUpdate();
2022-12-06 20:32:46 +08:00
return r.SetAndReturn(CmdResult.Status.Done, Translations.mcc_update_cancel);
}
2022-12-06 20:32:46 +08:00
private static int CheckUpdate(CmdResult r)
{
UpgradeHelper.CheckUpdate(forceUpdate: true);
2022-12-06 20:32:46 +08:00
return r.SetAndReturn(CmdResult.Status.Done, Translations.mcc_update_start);
}
}
}