Skip to content

Commit 5a6b13b

Browse files
committed
Merge remote-tracking branch 'origin/dev' into dev
2 parents 139dc41 + 1a8fc28 commit 5a6b13b

File tree

4 files changed

+35
-32
lines changed

4 files changed

+35
-32
lines changed

PCL.Core

Plain Craft Launcher 2/Pages/PageInstance/PageInstanceServer.xaml.vb

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
Imports System.IO
1+
Imports System.Collections.ObjectModel
2+
Imports System.IO
23
Imports System.Threading.Tasks
34
Imports fNbt
45
Imports PCL.Core.Link
@@ -82,33 +83,42 @@ Public Class PageInstanceServer
8283
ServerCardList.Add(serverCard)
8384
PanServers.Children.Add(serverCard)
8485

85-
Await serverCard.RefreshServerStatus(False)
86-
87-
Dim nbtData = Await NbtFileHandler.ReadNbtFileAsync(Of NbtList)(Path.Combine(PageInstanceLeft.Instance.PathIndie, "servers.dat"), "servers")
86+
Task.Run(Async Function()
87+
Await serverCard.RefreshServerStatus(False)
88+
End Function)
89+
90+
Dim serversDatPath = Path.Combine(PageInstanceLeft.Instance.PathIndie, "servers.dat")
91+
92+
Dim nbtData
93+
if Not File.Exists(serversDatPath) Then
94+
nbtData = New NbtList("servers", NbtTagType.Compound)
95+
Else
96+
nbtData = Await NbtFileHandler.ReadTagInNbtFileAsync(Of NbtList)(serversDatPath, "servers")
97+
End If
8898
If nbtData IsNot Nothing Then
8999
Dim server = New NbtCompound()
90100
server("name") = New NbtString("name", result.Name)
91101
server("ip") = New NbtString("ip", result.Address)
92102
nbtData.Add(server)
93103
Dim clonedNbtData = CType(nbtData.Clone(), NbtList)
94-
Await NbtFileHandler.WriteNbtFileAsync(clonedNbtData, Path.Combine(PageInstanceLeft.Instance.PathIndie, "servers.dat"))
104+
Await NbtFileHandler.WriteTagInNbtFileAsync(clonedNbtData, serversDatPath)
95105
End If
96106
End If
97107
End Sub
98108

99109
Public Shared Function GetServerInfo(server As MinecraftServerInfo) As (Name As String, Address As String, Success As Boolean)
100-
Dim newName As String = MyMsgBoxInput("编辑服务器信息", "请输入新的服务器名称:", server.Name)
110+
Dim newName As String = MyMsgBoxInput("编辑服务器信息", "请输入新的服务器名称:", server.Name,
111+
New Collection(Of Validate) From {New ValidateNullOrWhiteSpace()})
112+
101113
If String.IsNullOrEmpty(newName) Then
102-
Hint("服务器名称不能为空", HintType.Info)
103114
Return (String.Empty, String.Empty, False)
104115
End If
105116

106-
Dim newAddress As String = MyMsgBoxInput("编辑服务器信息", "请输入新的服务器地址:", server.Address)
117+
Dim newAddress As String = MyMsgBoxInput("编辑服务器信息", "请输入新的服务器地址:", server.Address,
118+
New Collection(Of Validate) From {New ValidateNullOrWhiteSpace()})
107119
If String.IsNullOrEmpty(newAddress) Then
108-
Hint("服务器地址不能为空", HintType.Info)
109120
Return (String.Empty, String.Empty, False)
110121
End If
111-
112122
Return (newName, newAddress, True)
113123
End Function
114124

@@ -123,7 +133,7 @@ Public Class PageInstanceServer
123133

124134
Try
125135
' 读取NBT格式的servers.dat文件
126-
Dim nbtData = await NbtFileHandler.ReadNbtFileAsync(Of NbtList)(serversFile, "servers")
136+
Dim nbtData = await NbtFileHandler.ReadTagInNbtFileAsync(Of NbtList)(serversFile, "servers")
127137
ParseServersFromNBT(nbtData)
128138
Catch ex As Exception
129139
Log(ex, "读取servers.dat文件失败", LogLevel.Debug)
@@ -147,7 +157,7 @@ Public Class PageInstanceServer
147157
Dim name As String = If(server.Get(Of NbtString)("name")?.Value, "Unknown")
148158
Dim iconBase64 As String = server.Get(Of NbtString)("icon")?.Value
149159

150-
Log(vbCrLf & $"服务器 {i + 1}:")
160+
Log($"服务器 {i + 1}:")
151161
Log($" 名字: {name}")
152162
Log($" IP: {ip}")
153163
' Log($" Hidden: {If(hidden = 1, "Yes", "No")}")
@@ -211,13 +221,15 @@ Public Class PageInstanceServer
211221
''' ping单个服务器
212222
''' </summary>
213223
Public Async Shared Function PingServer(server As MinecraftServerInfo) As Task(of MinecraftServerInfo)
214-
Dim addr = Await ServerAddressResolver.GetReachableAddressAsync(server.Address)
215-
216224
Try
217225
' Ping服务器
226+
Dim addr = Await ServerAddressResolver.GetReachableAddressAsync(server.Address)
227+
218228
Using query = New McPing(addr.Ip, addr.Port)
219229
Dim result As McPingResult
230+
Log("Pinging server: " & server.Address & ":" & addr.Port)
220231
result = Await query.PingAsync()
232+
Log("Ping result: " & If(result IsNot Nothing, "Success", "Failed"))
221233
If result <> Nothing
222234
server.Status = ServerStatus.Online
223235
server.PlayerCount = result.Players.Online
@@ -240,8 +252,6 @@ Public Class PageInstanceServer
240252
Public Shared Sub RemoveServer(server As ServerCard)
241253
Dim index = GetServerIndex(server)
242254
ServerCardList.Remove(server)
243-
Log("index: " & index)
244-
Log("_serverList.Count: " & ServerList.Count)
245255
If index >= 0 AndAlso index < ServerList.Count Then
246256
ServerList.RemoveAt(index)
247257
End If

Plain Craft Launcher 2/Pages/PageInstance/ServerCard.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838
<!-- ping 值与玩家人数 -->
3939
<StackPanel Grid.Row="2" Grid.Column="3" Orientation="Horizontal" VerticalAlignment="Bottom">
4040
<ContentPresenter x:Name="Signal" Content="{Binding SelectedIcon.XamlContent}"
41-
Width="20" Height="20" />
41+
Width="20" Height="20"/>
4242
<TextBlock x:Name="ServerPlayer" HorizontalAlignment="Left"
4343
TextTrimming="CharacterEllipsis" FontSize="12"
44-
Foreground="{DynamicResource ColorBrushGray3}" Opacity="0.4" VerticalAlignment="Bottom" />
44+
Foreground="{DynamicResource ColorBrushGray3}" Opacity="0.4" VerticalAlignment="Center"/>
4545
</StackPanel>
4646

4747
<!-- MotD -->

Plain Craft Launcher 2/Pages/PageInstance/ServerCard.xaml.vb

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -156,18 +156,11 @@ Public Class ServerCard
156156
' Get server information
157157
Dim result = PageInstanceServer.GetServerInfo(_server)
158158
If Not result.Success Then
159-
Hint("获取服务器信息失败", HintType.Critical)
160-
Exit Sub
161-
End If
162-
163-
' Validate server data
164-
If String.IsNullOrEmpty(result.Name) OrElse String.IsNullOrEmpty(result.Address) Then
165-
Hint("服务器名称或地址无效", HintType.Critical)
166159
Exit Sub
167160
End If
168161

169162
' Read NBT file
170-
Dim nbtData As NbtList = await NbtFileHandler.ReadNbTFileAsync(Of NbtList)(PageInstanceLeft.Instance.PathIndie + "servers.dat", "servers")
163+
Dim nbtData As NbtList = await NbtFileHandler.ReadTagInNbtFileAsync(Of NbtList)(PageInstanceLeft.Instance.PathIndie + "servers.dat", "servers")
171164
If nbtData Is Nothing Then
172165
Hint("无法读取服务器数据文件", HintType.Critical)
173166
Exit Sub
@@ -193,7 +186,7 @@ Public Class ServerCard
193186

194187
' Write updated NBT data
195188
Dim clonedNbtData = CType(nbtData.Clone(), NbtList)
196-
If Not Await NbtFileHandler.WriteNbtFileAsync(clonedNbtData, PageInstanceLeft.Instance.PathIndie + "servers.dat") Then
189+
If Not Await NbtFileHandler.WriteTagInNbtFileAsync(clonedNbtData, PageInstanceLeft.Instance.PathIndie + "servers.dat") Then
197190
Hint("无法写入服务器数据文件", HintType.Critical)
198191
Exit Sub
199192
End If
@@ -223,25 +216,25 @@ Public Class ServerCard
223216
End If
224217

225218
' Read NBT file
226-
Dim nbtData As NbtList = Await NbtFileHandler.ReadNbTFileAsync(Of NbtList)(PageInstanceLeft.Instance.PathIndie + "servers.dat", "servers")
219+
Dim nbtData As NbtList = Await NbtFileHandler.ReadTagInNbtFileAsync(Of NbtList)(IO.Path.Combine(PageInstanceLeft.Instance.PathIndie, "servers.dat"), "servers")
227220
If nbtData Is Nothing Then
228221
Hint("无法读取服务器数据文件", HintType.Critical)
229222
Exit Sub
230223
End If
231224

232225
' Verify server data
233-
Dim server As NbtCompound = TryCast(nbtData(index), NbtCompound)
226+
Dim server = TryCast(nbtData(index), NbtCompound)
234227
If server Is Nothing OrElse server.Get(Of NbtString)("name").Value <> _server.Name OrElse server.Get(Of NbtString)("ip").Value <> _server.Address Then
235228
Hint("服务器数据验证失败", HintType.Critical)
236229
Exit Sub
237230
End If
238231

239232
' Remove server from NBT data
240233
nbtData.RemoveAt(index)
241-
Dim clonedNbtData As NbtList = CType(nbtData.Clone(), NbtList)
234+
Dim clonedNbtData = CType(nbtData.Clone(), NbtList)
242235

243236
' Write back to NBT file
244-
If Not await NbtFileHandler.WriteNbtFileAsync(clonedNbtData, PageInstanceLeft.Instance.PathIndie + "servers.dat") Then
237+
If Not await NbtFileHandler.WriteTagInNbtFileAsync(clonedNbtData, PageInstanceLeft.Instance.PathIndie + "servers.dat") Then
245238
Hint("无法写入服务器数据文件", HintType.Critical)
246239
Exit Sub
247240
End If

0 commit comments

Comments
 (0)