Skip to content

Commit 84ec9c1

Browse files
committed
feat: 在Core中实现了Hint功能
1 parent 898f5fb commit 84ec9c1

File tree

7 files changed

+52
-74
lines changed

7 files changed

+52
-74
lines changed

Link/Lobby/LobbyController.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System.Text;
1616
using System.Text.Json.Nodes;
1717
using System.Threading.Tasks;
18+
using PCL.Core.UI;
1819
using static PCL.Core.Link.Lobby.LobbyInfoProvider;
1920
using static PCL.Core.Link.Natayark.NatayarkProfileManager;
2021
using LobbyType = PCL.Core.Link.Scaffolding.Client.Models.LobbyType;
@@ -44,7 +45,7 @@ public sealed class LobbyController
4445
/// <summary>
4546
/// Launch a Scaffolding Client.
4647
/// </summary>
47-
/// <param name="username">Join user name.</param>
48+
/// <param name="username">Join username.</param>
4849
/// <param name="code">Lobby share code.</param>
4950
/// <returns>Created <see cref="ScaffoldingClientEntity"/>.</returns>
5051
public async Task<ScaffoldingClientEntity?> LaunchClientAsync(string username, string code)
@@ -74,11 +75,10 @@ public sealed class LobbyController
7475

7576
foreach (var profile in scfEntity.Client.PlayerList)
7677
{
77-
if (profile.Kind == PlayerKind.HOST)
78-
{
79-
hostname = profile.Name;
80-
LogWrapper.Debug($"大厅创建者的用户名: {hostname}");
81-
}
78+
if (profile.Kind != PlayerKind.HOST) continue;
79+
80+
hostname = profile.Name;
81+
LogWrapper.Debug($"大厅创建者的用户名: {hostname}");
8282
}
8383

8484
var localPort = await scfEntity.EasyTier.AddPortForwardAsync(scfEntity.HostInfo.Ip, port)
@@ -96,20 +96,24 @@ public sealed class LobbyController
9696
catch (ArgumentNullException e)
9797
{
9898
LogWrapper.Error(e, "大厅创建者的用户名为空");
99+
HintWrapper.Show("大厅创建者的用户名为空", HintType.Critical);
99100
}
100101
catch (ArgumentException e)
101102
{
102103
if (e.Message.Contains("lobby code"))
103104
{
104105
LogWrapper.Error(e, "大厅编号无效");
106+
HintWrapper.Show("大厅编号无效", HintType.Critical);
105107
}
106108
else if (e.Message.Contains("hostname"))
107109
{
108110
LogWrapper.Error(e, "大厅创建者的用户名无效");
111+
HintWrapper.Show("大厅创建者的用户名无效", HintType.Critical);
109112
}
110113
else
111114
{
112115
LogWrapper.Error(e, "在加入大厅时出现意外的无效参数");
116+
HintWrapper.Show("在加入大厅时出现意外的无效参数", HintType.Critical);
113117
}
114118
}
115119
catch (Exception e)
@@ -123,7 +127,7 @@ public sealed class LobbyController
123127
/// <summary>
124128
/// Launch a Scaffolding Server.
125129
/// </summary>
126-
/// <param name="username">Host user name.</param>
130+
/// <param name="username">Host username.</param>
127131
/// <param name="port">Minecraft port.</param>
128132
/// <returns>Created <see cref="ScaffoldingServerEntity"/>.</returns>
129133
/// <remarks>
@@ -147,7 +151,7 @@ public sealed class LobbyController
147151
}
148152
catch (Exception e)
149153
{
150-
LogWrapper.Error(e, "Occurred error when launching Scafolding Server.");
154+
LogWrapper.Error(e, "Occurred error when launching Scaffolding Server.");
151155
}
152156

153157
return null;

Link/Lobby/LobbyService.cs

Lines changed: 17 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System.Threading;
1515
using System.Threading.Tasks;
1616
using System.Windows;
17+
using PCL.Core.UI;
1718

1819
namespace PCL.Core.Link.Lobby;
1920

@@ -32,7 +33,7 @@ public class LobbyService() : GeneralService("lobby", "LobbyService")
3233
private static readonly Timer _ServerGameWatcher =
3334
new(_CheckGameState, null, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(15));
3435

35-
private static bool _isGameWatcherRunnable = false;
36+
private static bool _isGameWatcherRunnable;
3637

3738
/// <summary>
3839
/// Current lobby state.
@@ -72,11 +73,6 @@ public class LobbyService() : GeneralService("lobby", "LobbyService")
7273
/// </summary>
7374
public static event Action<LobbyState, LobbyState>? StateChanged;
7475

75-
/// <summary>
76-
/// Used for UI layer to send Hint.
77-
/// </summary>
78-
public static event Action<string, CoreHintType>? OnHint;
79-
8076
/// <summary>
8177
/// Invoked when need to download EasyTier core files.
8278
/// </summary>
@@ -159,7 +155,7 @@ public static async Task InitializeAsync()
159155
Convert.ToDateTime(expTime).CompareTo(DateTime.Now) < 0)
160156
{
161157
Config.Link.NaidRefreshToken = string.Empty;
162-
OnHint?.Invoke("Natayark ID 令牌已过期,请重新登录", CoreHintType.Critical);
158+
HintWrapper.Show("Natayark ID 令牌已过期,请重新登录", HintType.Critical);
163159
}
164160
else
165161
{
@@ -175,7 +171,7 @@ public static async Task InitializeAsync()
175171
catch (Exception ex)
176172
{
177173
LogWrapper.Error(ex, "LobbyService", "Lobby service initialization failed.");
178-
OnHint?.Invoke("大厅服务初始化失败,请检查网络连接。", CoreHintType.Critical);
174+
HintWrapper.Show("大厅服务初始化失败,请检查网络连接。", HintType.Critical);
179175
_SetState(LobbyState.Error);
180176
}
181177
}
@@ -251,7 +247,7 @@ public static async Task<bool> CreateLobbyAsync(int port, string username)
251247
{
252248
if (_NotHaveNaid())
253249
{
254-
OnHint?.Invoke("请先登录 Natayark ID 再使用大厅!", CoreHintType.Critical);
250+
HintWrapper.Show("请先登录 Natayark ID 再使用大厅!", HintType.Critical);
255251
return false;
256252
}
257253

@@ -265,7 +261,7 @@ public static async Task<bool> CreateLobbyAsync(int port, string username)
265261
var serverEntity = await _LobbyController.LaunchServerAsync(username, port).ConfigureAwait(false);
266262
if (serverEntity is null)
267263
{
268-
OnHint?.Invoke("在创建房间的时候遇到了问题,请查看日志并将此问题反馈给开发者!", CoreHintType.Critical);
264+
HintWrapper.Show("在创建房间的时候遇到了问题,请查看日志并将此问题反馈给开发者!", HintType.Critical);
269265
return false;
270266
}
271267

@@ -289,7 +285,7 @@ public static async Task<bool> CreateLobbyAsync(int port, string username)
289285
catch (Exception ex)
290286
{
291287
LogWrapper.Error(ex, "LobbyService", "Failed to create lobby.");
292-
OnHint?.Invoke("创建大厅失败,请检查日志或向开发者反馈。", CoreHintType.Critical);
288+
HintWrapper.Show("创建大厅失败,请检查日志或向开发者反馈。", HintType.Critical);
293289
await LeaveLobbyAsync().ConfigureAwait(false);
294290

295291
return false;
@@ -333,7 +329,7 @@ private static void _ServerOnPlayerPing(IReadOnlyList<PlayerProfile> players)
333329
var sortedNewPlayers = PlayerListHandler.Sort(players);
334330

335331
var idsToRemove = currentMachineIds.Except(newMachineIds).ToList();
336-
if (idsToRemove.Any())
332+
if (idsToRemove.Count != 0)
337333
{
338334
var playersToRemove = Players.Where(p => idsToRemove.Contains(p.MachineId)).ToList();
339335
foreach (var player in playersToRemove)
@@ -343,13 +339,12 @@ private static void _ServerOnPlayerPing(IReadOnlyList<PlayerProfile> players)
343339
}
344340

345341
var idsToAdd = newMachineIds.Except(currentMachineIds).ToList();
346-
if (idsToAdd.Any())
347-
{
348-
var playersToAdd = sortedNewPlayers.Where(p => idsToAdd.Contains(p.MachineId)).ToList();
349-
foreach (var player in playersToAdd)
350-
{
351-
Players.Add(player);
352-
}
342+
if (idsToAdd.Count == 0) return;
343+
344+
var playersToAdd = sortedNewPlayers.Where(p => idsToAdd.Contains(p.MachineId)).ToList();
345+
foreach (var player in playersToAdd)
346+
{
347+
Players.Add(player);
353348
}
354349
});
355350
}
@@ -376,27 +371,18 @@ public static async Task<bool> JoinLobbyAsync(string lobbyCode, string username)
376371

377372
if (clientEntity is null)
378373
{
379-
throw new InvalidOperationException(
380-
"加入大厅失败,可能是大厅不存在或已被解散");
374+
return false;
381375
}
382376

383377
clientEntity.Client.Heartbeat += _ClientOnHeartbeat;
384378
clientEntity.Client.ServerShuttedDown += _ClientOnServerShutDown;
385379

386380
_SetState(LobbyState.Connected);
387381
}
388-
catch (ArgumentException codeEx)
389-
{
390-
LogWrapper.Error(codeEx, "LobbyService", $"Failed to join lobby {lobbyCode}.");
391-
OnHint?.Invoke("大厅编号不正确,请检查后再试!", CoreHintType.Critical);
392-
await LeaveLobbyAsync().ConfigureAwait(false);
393-
394-
return false;
395-
}
396382
catch (Exception ex)
397383
{
398384
LogWrapper.Error(ex, "LobbyService", $"Failed to join lobby {lobbyCode}.");
399-
OnHint?.Invoke(ex.Message, CoreHintType.Critical);
385+
HintWrapper.Show(ex.Message, HintType.Critical);
400386
await LeaveLobbyAsync().ConfigureAwait(false);
401387

402388
return false;
@@ -548,28 +534,4 @@ private static async Task _RunInUiAsync(Action action)
548534
/// </summary>
549535
/// <param name="Name">World name.</param>
550536
/// <param name="Port">World share port.</param>
551-
public record FoundWorld(string Name, int Port);
552-
553-
/// <summary>
554-
/// Hint type in PCL.Core (for UI display).
555-
/// </summary>
556-
public enum CoreHintType
557-
{
558-
/// <summary>
559-
/// 信息,通常是蓝色的“i”。
560-
/// </summary>
561-
/// <remarks></remarks>
562-
Info,
563-
564-
/// <summary>
565-
/// 已完成,通常是绿色的“√”。
566-
/// </summary>
567-
/// <remarks></remarks>
568-
Finish,
569-
570-
/// <summary>
571-
/// 错误,通常是红色的“×”。
572-
/// </summary>
573-
/// <remarks></remarks>
574-
Critical
575-
}
537+
public record FoundWorld(string Name, int Port);

Link/Natayark/NatayarkProfileManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public static async Task GetNaidDataAsync(string token, bool isRefresh = false,
135135
void WarnLog(string msg)
136136
{
137137
LogWrapper.Warn(ex, LogModule, msg);
138-
HintWrapper.Show(msg, HintTheme.Error);
138+
HintWrapper.Show(msg, HintType.Critical);
139139
}
140140
}
141141
finally

Logging/LogService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private static void _LogAction(ActionLevel level, string formatted, string plain
5252
// hint
5353
if (level is ActionLevel.Hint or ActionLevel.HintErr)
5454
{
55-
HintWrapper.Show(plain, (level == ActionLevel.Hint) ? HintTheme.Normal : HintTheme.Error);
55+
HintWrapper.Show(plain, (level == ActionLevel.Hint) ? HintType.Info : HintType.Critical);
5656
}
5757

5858
// message box

Minecraft/Folder/FolderManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public async Task McFolderListLoadAsync() {
3939

4040
foreach (var folder in Config.Launch.Folders.Split('|', StringSplitOptions.RemoveEmptyEntries)) {
4141
if (!folder.Contains('>') || !folder.EndsWith('\\')) {
42-
HintWrapper.Show($"无效的 Minecraft 文件夹:{folder}", HintTheme.Error);
42+
HintWrapper.Show($"无效的 Minecraft 文件夹:{folder}", HintType.Critical);
4343
continue;
4444
}
4545

Minecraft/Launch/Services/Argument/LaunchArgBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ private void AddLegacyServerArguments(string server) {
179179
_arguments.Add($"--server {host} --port {port}");
180180

181181
if (instance.InstanceInfo.HasPatch("optifine")) {
182-
HintWrapper.Show("OptiFine 与自动进入服务器可能不兼容,有概率导致材质丢失甚至游戏崩溃!", HintTheme.Error);
182+
HintWrapper.Show("OptiFine 与自动进入服务器可能不兼容,有概率导致材质丢失甚至游戏崩溃!", HintType.Critical);
183183
}
184184
}
185185

UI/HintWrapper.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,35 @@
11
namespace PCL.Core.UI;
22

3-
public enum HintTheme
3+
/// <summary>
4+
/// 提示信息的种类W
5+
/// </summary>
6+
public enum HintType
47
{
5-
Normal,
6-
Success,
7-
Error
8+
/// <summary>
9+
/// 信息
10+
/// </summary>
11+
Info,
12+
/// <summary>
13+
/// 已完成
14+
/// </summary>
15+
Finish,
16+
/// <summary>
17+
/// 错误
18+
/// </summary>
19+
Critical
820
}
921

1022
public delegate void HintHandler(
1123
string message,
12-
HintTheme theme
24+
HintType type
1325
);
1426

1527
public static class HintWrapper
1628
{
1729
public static event HintHandler? OnShow;
1830

19-
public static void Show(string message, HintTheme theme = HintTheme.Normal)
31+
public static void Show(string message, HintType type = HintType.Info)
2032
{
21-
OnShow?.Invoke(message, theme);
33+
OnShow?.Invoke(message, type);
2234
}
2335
}

0 commit comments

Comments
 (0)