ModEvent.vb 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. Public Module ModEvent
  2. Public Sub TryStartEvent(Type As String, Data As String)
  3. If String.IsNullOrWhiteSpace(Type) Then Exit Sub
  4. Dim RealData As String() = {""}
  5. If Data IsNot Nothing Then RealData = Data.Split("|")
  6. StartEvent(Type, RealData)
  7. End Sub
  8. Public Sub StartEvent(Type As String, Data As String())
  9. Try
  10. Log("[Control] 执行自定义事件:" & Type & ", " & Join(Data, ", "))
  11. Select Case Type
  12. Case "打开网页"
  13. Data(0) = Data(0).Replace("\", "/")
  14. If Not Data(0).Contains("://") OrElse Data(0).StartsWithF("file", True) Then '为了支持更多协议(#2200)
  15. MyMsgBox("EventData 必须为一个网址。" & vbCrLf & "如果想要启动程序,请将 EventType 改为 打开文件。", "事件执行失败")
  16. Exit Sub
  17. End If
  18. Hint("正在开启中,请稍候……")
  19. OpenWebsite(Data(0))
  20. Case "打开文件", "打开帮助"
  21. RunInThread(Sub()
  22. Try
  23. '确认实际路径
  24. Dim ActualPaths = GetEventAbsoluteUrls(Data(0), Type)
  25. Dim Location = ActualPaths(0), WorkingDir = ActualPaths(1)
  26. '执行
  27. If Type = "打开文件" Then
  28. Dim Info As New ProcessStartInfo With {
  29. .Arguments = If(Data.Length >= 2, Data(1), ""),
  30. .FileName = Location,
  31. .WorkingDirectory = WorkingDir
  32. }
  33. Process.Start(Info)
  34. Else '打开帮助
  35. PageOtherHelp.EnterHelpPage(Location)
  36. End If
  37. Catch ex As Exception
  38. Log(ex, "执行打开类自定义事件失败", LogLevel.Msgbox)
  39. End Try
  40. End Sub)
  41. Case "启动游戏"
  42. If McLaunchStart(New McLaunchOptions With
  43. {.ServerIp = If(Data.Length >= 2, Data(1), Nothing), .Version = New McVersion(Data(0))}) Then
  44. Hint("正在启动 " & Data(0) & "……")
  45. End If
  46. Case "复制文本"
  47. ClipboardSet(Join(Data, "|"))
  48. Case "刷新主页"
  49. FrmLaunchRight.ForceRefresh()
  50. If Data(0) = "" Then Hint("已刷新主页!", HintType.Finish)
  51. Case "刷新帮助"
  52. PageOtherLeft.RefreshHelp()
  53. Case "今日人品"
  54. PageOtherTest.Jrrp()
  55. Case "内存优化"
  56. RunInThread(Sub() PageOtherTest.MemoryOptimize(True))
  57. Case "清理垃圾"
  58. RunInThread(Sub() PageOtherTest.RubbishClear())
  59. Case "弹出窗口"
  60. MyMsgBox(Data(1).Replace("\n", vbCrLf), Data(0).Replace("\n", vbCrLf))
  61. Case "切换页面"
  62. FrmMain.PageChange(Val(Data(0)), Val(Data(1)))
  63. Case "导入整合包", "安装整合包"
  64. RunInUi(Sub() ModpackInstall())
  65. Case "下载文件"
  66. Data(0) = Data(0).Replace("\", "/")
  67. If Not (Data(0).StartsWithF("http://", True) OrElse Data(0).StartsWithF("https://", True)) Then
  68. MyMsgBox("EventData 必须为以 http:// 或 https:// 开头的网址。" & vbCrLf & "PCL 不支持其他乱七八糟的下载协议。", "事件执行失败")
  69. Exit Sub
  70. End If
  71. PageOtherTest.StartCustomDownload(Data(0), GetFileNameFromPath(Data(0)))
  72. Case Else
  73. MyMsgBox("未知的事件类型:" & Type & vbCrLf & "请检查事件类型填写是否正确,或者 PCL 是否为最新版本。", "事件执行失败")
  74. End Select
  75. Catch ex As Exception
  76. Log(ex, "事件执行失败", LogLevel.Msgbox)
  77. End Try
  78. End Sub
  79. ''' <summary>
  80. ''' 返回自定义事件的绝对 Url。实际返回 {绝对 Url, WorkingDir}。
  81. ''' 失败会抛出异常。
  82. ''' </summary>
  83. Public Function GetEventAbsoluteUrls(RelativeUrl As String, EventType As String) As String()
  84. '网页确认
  85. If RelativeUrl.StartsWithF("http", True) Then
  86. If RunInUi() Then
  87. Throw New Exception("能打开联网帮助页面的 MyListItem 必须手动设置 Title、Info 属性!")
  88. End If
  89. '获取文件名
  90. Dim RawFileName As String
  91. Try
  92. RawFileName = GetFileNameFromPath(RelativeUrl)
  93. If Not RawFileName.EndsWithF(".json", True) Then Throw New Exception("未指向 .json 后缀的文件")
  94. Catch ex As Exception
  95. Throw New Exception("联网帮助页面须指向一个帮助 JSON 文件,并在同路径下包含相应 XAML 文件!" & vbCrLf &
  96. "例如:" & vbCrLf &
  97. " - https://www.baidu.com/test.json(填写这个路径)" & vbCrLf &
  98. " - https://www.baidu.com/test.xaml(同时也需要包含这个文件)", ex)
  99. End Try
  100. '下载文件
  101. Dim LocalTemp1 As String = PathTemp & "CustomEvent\" & RawFileName
  102. Dim LocalTemp2 As String = PathTemp & "CustomEvent\" & RawFileName.Replace(".json", ".xaml")
  103. Log("[Event] 转换网络资源:" & RelativeUrl & " -> " & LocalTemp1)
  104. Hint("正在获取资源,请稍候……")
  105. Try
  106. NetDownload(RelativeUrl, LocalTemp1)
  107. NetDownload(RelativeUrl.Replace(".json", ".xaml"), LocalTemp1.Replace(".json", ".xaml"))
  108. Catch ex As Exception
  109. Throw New Exception("下载指定的文件失败!" & vbCrLf &
  110. "注意,联网帮助页面须指向一个帮助 JSON 文件,并在同路径下包含相应 XAML 文件!" & vbCrLf &
  111. "例如:" & vbCrLf &
  112. " - https://www.baidu.com/test.json(填写这个路径)" & vbCrLf &
  113. " - https://www.baidu.com/test.xaml(同时也需要包含这个文件)", ex)
  114. End Try
  115. RelativeUrl = LocalTemp1
  116. End If
  117. RelativeUrl = RelativeUrl.Replace("/", "\").ToLower.TrimStart("\")
  118. '确认实际路径
  119. Dim Location As String, WorkingDir As String = Path & "PCL"
  120. HelpTryExtract()
  121. If RelativeUrl.Contains(":\") Then
  122. '绝对路径
  123. Location = RelativeUrl
  124. Log("[Control] 自定义事件中由绝对路径" & EventType & ":" & Location)
  125. ElseIf File.Exists(Path & "PCL\" & RelativeUrl) Then
  126. '相对 PCL 文件夹的路径
  127. Location = Path & "PCL\" & RelativeUrl
  128. Log("[Control] 自定义事件中由相对 PCL 文件夹的路径" & EventType & ":" & Location)
  129. ElseIf File.Exists(Path & "PCL\Help\" & RelativeUrl) Then
  130. '相对 PCL 本地帮助文件夹的路径
  131. Location = Path & "PCL\Help\" & RelativeUrl
  132. WorkingDir = Path & "PCL\Help\"
  133. Log("[Control] 自定义事件中由相对 PCL 本地帮助文件夹的路径" & EventType & ":" & Location)
  134. ElseIf EventType = "打开帮助" AndAlso File.Exists(PathTemp & "Help\" & RelativeUrl) Then
  135. '相对 PCL 自带帮助文件夹的路径
  136. Location = PathTemp & "Help\" & RelativeUrl
  137. WorkingDir = PathTemp & "Help\"
  138. Log("[Control] 自定义事件中由相对 PCL 自带帮助文件夹的路径" & EventType & ":" & Location)
  139. ElseIf EventType = "打开文件" Then
  140. '直接使用原有路径启动程序
  141. Location = RelativeUrl
  142. Log("[Control] 自定义事件中直接" & EventType & ":" & Location)
  143. Else
  144. '打开帮助,但是格式不对劲
  145. Throw New FileNotFoundException("未找到 EventData 指向的本地 xaml 文件:" & RelativeUrl, RelativeUrl)
  146. End If
  147. Return {Location, WorkingDir}
  148. End Function
  149. End Module