Skip to content

Commit

Permalink
添加服务器 Ping 监视器
Browse files Browse the repository at this point in the history
  • Loading branch information
YangSpring114 committed Apr 17, 2024
1 parent 8796a05 commit 435a453
Show file tree
Hide file tree
Showing 11 changed files with 351 additions and 30 deletions.
64 changes: 46 additions & 18 deletions MinecraftLaunch.Test/Program.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,58 @@
using MinecraftLaunch;
using MinecraftLaunch.Components.Resolver;
using MinecraftLaunch.Components.Installer;
using MinecraftLaunch.Components.Watcher;
using MinecraftLaunch.Classes.Models.Event;

GameResolver gameResolver = new("C:\\Users\\w\\Downloads\\.minecraft");
# region ServerPing

VanlliaInstaller vanlliaInstaller = new(gameResolver, "1.12.2");
vanlliaInstaller.ProgressChanged += (_, args) => {
Console.WriteLine($"{args.Progress * 100:0.00} - {args.Status} - {args.ProgressStatus}");
};
ServerPingWatcher serverPingWatcher = new(25565, "mc.163mc.cn", 47);

await vanlliaInstaller.InstallAsync();
serverPingWatcher.ServerConnectionProgressChanged += OnServerConnectionProgressChanged;

Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
serverPingWatcher.ServerLatencyChanged += (_, args) => {
Console.WriteLine($"{args.Latency}ms");
};

ForgeInstaller forgeInstaller = new(gameResolver.GetGameEntity("1.12.2"),
(await ForgeInstaller.EnumerableFromVersionAsync("1.12.2")).First(),
"C:\\Program Files\\Java\\jdk1.8.0_301\\bin\\javaw.exe",
"1.12.2-forge-114514");
await serverPingWatcher.StartAsync();

forgeInstaller.ProgressChanged += (_, args) => {
void OnServerConnectionProgressChanged(object? sender, ProgressChangedEventArgs args) {
Console.WriteLine($"{args.Progress * 100:0.00} - {args.Status} - {args.ProgressStatus}");
};
if (args.Status == TaskStatus.Canceled) {
serverPingWatcher.ServerConnectionProgressChanged -= OnServerConnectionProgressChanged;
}
}

#endregion

# region Forge Install

//GameResolver gameResolver = new("C:\\Users\\w\\Downloads\\.minecraft");

//VanlliaInstaller vanlliaInstaller = new(gameResolver, "1.12.2");
//vanlliaInstaller.ProgressChanged += (_, args) => {
// Console.WriteLine($"{args.Progress * 100:0.00} - {args.Status} - {args.ProgressStatus}");
//};

//await vanlliaInstaller.InstallAsync();

//Console.WriteLine();
//Console.WriteLine();
//Console.WriteLine();
//Console.WriteLine();
//Console.WriteLine();

//ForgeInstaller forgeInstaller = new(gameResolver.GetGameEntity("1.12.2"),
// (await ForgeInstaller.EnumerableFromVersionAsync("1.12.2")).First(),
// "C:\\Program Files\\Java\\jdk1.8.0_301\\bin\\javaw.exe",
// "1.12.2-forge-114514");

//forgeInstaller.ProgressChanged += (_, args) => {
// Console.WriteLine($"{args.Progress * 100:0.00} - {args.Status} - {args.ProgressStatus}");
//};

//await forgeInstaller.InstallAsync();

#endregion

await forgeInstaller.InstallAsync();
Console.ReadKey();
8 changes: 8 additions & 0 deletions MinecraftLaunch/Classes/Interfaces/IWatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace MinecraftLaunch.Classes.Interfaces;

/// <summary>
/// 监视器统一接口
/// </summary>
public interface IWatcher {
void Start();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using MinecraftLaunch.Classes.Models.ServerPing;

namespace MinecraftLaunch.Classes.Models.Event;
public sealed class ServerLatencyChangedEventArgs : EventArgs {
public long Latency { get; set; }
public PingPayload Response { get; set; }
}
15 changes: 15 additions & 0 deletions MinecraftLaunch/Classes/Models/ServerPing/PingPayload.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace MinecraftLaunch.Classes.Models.ServerPing;

public sealed class PingPayload {
[JsonPropertyName("favicon")] public string Icon { get; set; }
[JsonPropertyName("version")] public VersionPayload Version { get; set; }
[JsonPropertyName("players")] public PlayersPayload Players { get; set; }
[JsonPropertyName("modinfo")] public ServerPingModInfo ModInfo { get; set; }
[JsonPropertyName("description")] public JsonElement Description { get; set; }
}

[JsonSerializable(typeof(PingPayload))]
sealed partial class PingPayloadContext : JsonSerializerContext;
8 changes: 8 additions & 0 deletions MinecraftLaunch/Classes/Models/ServerPing/Player.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Text.Json.Serialization;

namespace MinecraftLaunch.Classes.Models.ServerPing;

public sealed class Player {
[JsonPropertyName("id")] public string Id { get; set; }
[JsonPropertyName("name")] public string Name { get; set; }
}
9 changes: 9 additions & 0 deletions MinecraftLaunch/Classes/Models/ServerPing/PlayersPayload.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Text.Json.Serialization;

namespace MinecraftLaunch.Classes.Models.ServerPing;

public sealed class PlayersPayload {
[JsonPropertyName("max")] public int Max { get; set; }
[JsonPropertyName("online")] public int Online { get; set; }
[JsonPropertyName("sample")] public Player[] Sample { get; set; }
}
13 changes: 13 additions & 0 deletions MinecraftLaunch/Classes/Models/ServerPing/ServerPingModInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Text.Json.Serialization;

namespace MinecraftLaunch.Classes.Models.ServerPing;

public sealed class ServerPingModInfo {
[JsonPropertyName("type")] public string Type { get; set; }
[JsonPropertyName("modList")] public IEnumerable<ModInfo> ModList { get; set; }
}

public sealed class ModInfo {
[JsonPropertyName("modid")] public string ModId { get; set; }
[JsonPropertyName("version")] public string Version { get; set; }
}
8 changes: 8 additions & 0 deletions MinecraftLaunch/Classes/Models/ServerPing/VersionPayload.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Text.Json.Serialization;

namespace MinecraftLaunch.Classes.Models.ServerPing;

public sealed class VersionPayload {
[JsonPropertyName("name")] public string Name { get; set; }
[JsonPropertyName("protocol")] public int Protocol { get; set; }
}
21 changes: 13 additions & 8 deletions MinecraftLaunch/Components/Watcher/GameProcessWatcher.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System;
using System.Diagnostics;
using MinecraftLaunch.Classes.Interfaces;
using MinecraftLaunch.Classes.Models.Event;

Expand All @@ -7,7 +8,7 @@ namespace MinecraftLaunch.Components.Watcher;
/// <summary>
/// 游戏进程监视器
/// </summary>
public class GameProcessWatcher : IGameProcessWatcher {
public class GameProcessWatcher : IWatcher, IGameProcessWatcher {
public Process Process { get; }

public IEnumerable<string> Arguments { get; }
Expand All @@ -19,13 +20,17 @@ public class GameProcessWatcher : IGameProcessWatcher {
public GameProcessWatcher(Process process, IEnumerable<string> arguments) {
Process = process;
Arguments = arguments;
process.Exited += OnExited;
process.ErrorDataReceived += OnOutputDataReceived;
process.OutputDataReceived += OnOutputDataReceived;
process.Start();
Start();
}

public void Start() {
Process.Exited += OnExited;
Process.ErrorDataReceived += OnOutputDataReceived;
Process.OutputDataReceived += OnOutputDataReceived;

process.BeginErrorReadLine();
process.BeginOutputReadLine();
Process.Start();
Process.BeginErrorReadLine();
Process.BeginOutputReadLine();
}

private void OnExited(object sender, EventArgs e) {
Expand Down
Loading

0 comments on commit 435a453

Please sign in to comment.