Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Plain Craft Launcher 2/Modules/Base/ModBase.vb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Imports System.Globalization
Imports System.IO.Compression
Imports System.Net.Sockets
Imports System.Runtime.CompilerServices
Imports System.Runtime.InteropServices
Imports System.Security.Cryptography
Imports System.Security.Principal
Imports System.Text.RegularExpressions
Expand Down Expand Up @@ -1421,7 +1422,44 @@ RetryDir:
Return ShortPath.ToString
End Function
Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As StringBuilder, ByVal cchBuffer As Integer) As Integer

Public Class KernelFile
<DllImport("kernel32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
Private Shared Function GetFinalPathNameByHandle(hFile As IntPtr, ByVal lpszFilePath As String, ByVal cchFilePath As Integer, ByVal dwFlags As Integer) As Integer
End Function

<DllImport("kernel32.dll", SetLastError:=True)>
Private Shared Function CreateFile(lpFileName As String, dwDesiredAccess As Integer, dwShareMode As FileShare, lpSecurityAttributes As IntPtr, dwCreationDisposition As FileMode, dwFlagsAndAttributes As Integer, hTemplateFile As IntPtr) As IntPtr
End Function

<DllImport("kernel32.dll", SetLastError:=True)>
Private Shared Function CloseHandle(hObject As IntPtr) As Boolean
End Function

Public Shared Function GetSymbolicLinkTarget(path As String) As String
Dim hFile As IntPtr = CreateFile(path, &H80000000, FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, &H2000000, IntPtr.Zero)
If hFile = IntPtr.Zero Then
Throw New IOException("无法打开文件句柄", Marshal.GetLastWin32Error())
End If

Try
Dim buffer = New String(Chr(0), 260)
Dim length As Integer = GetFinalPathNameByHandle(hFile, buffer, buffer.Length, 0)
If length = 0 Then
Throw New IOException("无法获取目标路径", Marshal.GetLastWin32Error())
End If

' 移除可能的前缀"\\?\"
Dim targetPath As String = buffer.Substring(0, length)
If targetPath.StartsWith("\\?\") Then
targetPath = targetPath.Substring(4)
End If
Return targetPath
Finally
CloseHandle(hFile)
End Try
End Function
End Class
#End Region

#Region "文本"
Expand Down
15 changes: 14 additions & 1 deletion Plain Craft Launcher 2/Modules/Minecraft/ModJava.vb
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,20 @@ Wait:
Private Sub JavaSearchFolder(OriginalPath As String, ByRef Results As Dictionary(Of String, Boolean), Source As Boolean, Optional IsFullSearch As Boolean = False)
Try
Log("[Java] 开始" & If(IsFullSearch, "完全", "部分") & "遍历查找:" & OriginalPath)
JavaSearchFolder(New DirectoryInfo(ShortenPath(OriginalPath)), Results, Source, IsFullSearch)
Dim Path As String = ShortenPath(OriginalPath)
Dim Root = New DirectoryInfo(Path)
Dim Info As FileSystemInfo = new FileInfo(Path)
If Info.Attributes.HasFlag(FileAttributes.ReparsePoint) Then
Log("[Java] 搜索目标为符号链接, 尝试转为真实地址进行搜索!")
Try
Dim RealPath = KernelFile.GetSymbolicLinkTarget(Path)
Log("[Java] 搜索目标转换为: "+ RealPath)
Root = New DirectoryInfo(RealPath)
Catch ex As Exception
Log(ex, "获取真实地址时异常(" & Path & "), 直接搜索符号链接")
End Try
End If
JavaSearchFolder(Root, Results, Source, IsFullSearch)
Catch ex As UnauthorizedAccessException
Log("[Java] 遍历查找 Java 时遭遇无权限的文件夹:" & OriginalPath)
Catch ex As Exception
Expand Down
Loading