ModEvent.vb 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. 
  2. #Region "附加属性"
  3. ''' <summary>
  4. ''' 用于在 XAML 中初始化列表对象。
  5. ''' 附加属性无法在 XAML 中为每个对象初始化独立的列表对象,因此需要一个包装类,然后在 XAML 中显式初始化。
  6. ''' </summary>
  7. <Markup.ContentProperty("Events")>
  8. Public Class CustomEventCollection
  9. Implements IEnumerable(Of CustomEvent)
  10. Dim _Events As New List(Of CustomEvent)
  11. Public ReadOnly Property Events As List(Of CustomEvent)
  12. Get
  13. Return _Events
  14. End Get
  15. End Property
  16. Public Function GetEnumerator() As IEnumerator(Of CustomEvent) Implements IEnumerable(Of CustomEvent).GetEnumerator
  17. Return DirectCast(Events, IEnumerable(Of CustomEvent)).GetEnumerator()
  18. End Function
  19. Private Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
  20. Return DirectCast(Events, IEnumerable).GetEnumerator()
  21. End Function
  22. End Class
  23. ''' <summary>
  24. ''' 提供自定义事件的附加属性。
  25. ''' </summary>
  26. Public Class CustomEventService
  27. 'Events
  28. Public Shared ReadOnly EventsProperty As DependencyProperty =
  29. DependencyProperty.RegisterAttached("Events", GetType(CustomEventCollection), GetType(CustomEventService), New PropertyMetadata(Nothing))
  30. <AttachedPropertyBrowsableForType(GetType(DependencyObject))>
  31. Public Shared Sub SetEvents(d As DependencyObject, value As CustomEventCollection)
  32. d.SetValue(EventsProperty, value)
  33. End Sub
  34. <AttachedPropertyBrowsableForType(GetType(DependencyObject))>
  35. Public Shared Function GetEvents(d As DependencyObject) As CustomEventCollection
  36. If d.GetValue(EventsProperty) Is Nothing Then d.SetValue(EventsProperty, New CustomEventCollection)
  37. Return d.GetValue(EventsProperty)
  38. End Function
  39. 'EventType
  40. Public Shared ReadOnly EventTypeProperty As DependencyProperty =
  41. DependencyProperty.RegisterAttached("EventType", GetType(CustomEvent.EventType), GetType(CustomEventService), New PropertyMetadata(Nothing))
  42. <AttachedPropertyBrowsableForType(GetType(DependencyObject))>
  43. Public Shared Sub SetEventType(d As DependencyObject, value As CustomEvent.EventType)
  44. d.SetValue(EventTypeProperty, value)
  45. End Sub
  46. <AttachedPropertyBrowsableForType(GetType(DependencyObject))>
  47. Public Shared Function GetEventType(d As DependencyObject) As CustomEvent.EventType
  48. Return d.GetValue(EventTypeProperty)
  49. End Function
  50. 'EventData
  51. Public Shared ReadOnly EventDataProperty As DependencyProperty =
  52. DependencyProperty.RegisterAttached("EventData", GetType(String), GetType(CustomEventService), New PropertyMetadata(Nothing))
  53. <AttachedPropertyBrowsableForType(GetType(DependencyObject))>
  54. Public Shared Sub SetEventData(d As DependencyObject, value As String)
  55. d.SetValue(EventDataProperty, value)
  56. End Sub
  57. <AttachedPropertyBrowsableForType(GetType(DependencyObject))>
  58. Public Shared Function GetEventData(d As DependencyObject) As String
  59. Return d.GetValue(EventDataProperty)
  60. End Function
  61. End Class
  62. Partial Public Module ModMain
  63. ''' <summary>
  64. ''' 触发该控件上的自定义事件。
  65. ''' 事件会在新线程中执行。
  66. ''' </summary>
  67. <Runtime.CompilerServices.Extension>
  68. Public Sub RaiseCustomEvent(Control As DependencyObject)
  69. '收集事件列表
  70. Dim Events = CustomEventService.GetEvents(Control).ToList
  71. Dim EventType = CustomEventService.GetEventType(Control)
  72. If EventType <> CustomEvent.EventType.None Then Events.Add(New CustomEvent(EventType, CustomEventService.GetEventData(Control)))
  73. '执行事件
  74. If Not Events.Any Then Return
  75. RunInNewThread(
  76. Sub()
  77. For Each e In Events
  78. e.Raise()
  79. Next
  80. End Sub, "执行自定义事件 " & GetUuid())
  81. End Sub
  82. End Module
  83. #End Region
  84. ''' <summary>
  85. ''' 自定义事件。
  86. ''' </summary>
  87. Public Class CustomEvent
  88. #Region "属性与触发"
  89. Public Property Type As EventType = EventType.None
  90. Public Property Data As String = Nothing
  91. Public Sub New()
  92. End Sub
  93. Public Sub New(Type As EventType, Data As String)
  94. Me.Type = Type
  95. Me.Data = Data
  96. End Sub
  97. ''' <summary>
  98. ''' 在当前线程中触发该自定义事件。
  99. ''' </summary>
  100. Public Sub Raise()
  101. Raise(Type, Data)
  102. End Sub
  103. #End Region
  104. Public Enum EventType
  105. None = 0
  106. 打开网页
  107. 打开文件
  108. 打开帮助
  109. 执行命令
  110. 启动游戏
  111. 复制文本
  112. 刷新主页
  113. 刷新页面
  114. 刷新帮助
  115. 今日人品
  116. 内存优化
  117. 清理垃圾
  118. 弹出窗口
  119. 弹出提示
  120. 切换页面
  121. 导入整合包
  122. 安装整合包
  123. 下载文件
  124. 修改设置
  125. 写入设置
  126. 修改变量
  127. 写入变量
  128. 加入房间
  129. End Enum
  130. ''' <summary>
  131. ''' 在当前线程中触发一个自定义事件。
  132. ''' </summary>
  133. Public Shared Sub Raise(Type As EventType, Arg As String)
  134. If Type = EventType.None Then Return
  135. Log($"[Control] 执行自定义事件:{Type}, {Arg}")
  136. Try
  137. Dim Args As String() = If(Arg?.Split("|"), {""})
  138. Select Case Type
  139. Case EventType.打开网页
  140. Arg = Arg.Replace("\", "/")
  141. If Not Arg.Contains("://") OrElse Arg.StartsWithF("file", True) Then '为了支持更多协议(#2200)
  142. MyMsgBox("EventData 必须为一个网址。" & vbCrLf & "如果想要启动程序,请将 EventType 改为 打开文件。", "事件执行失败")
  143. Return
  144. End If
  145. Hint("正在开启中,请稍候:" & Arg)
  146. RunInThread(Sub() OpenWebsite(Arg))
  147. Case EventType.打开文件, EventType.打开帮助, EventType.执行命令
  148. RunInThread(
  149. Sub()
  150. Try
  151. '确认实际路径
  152. Dim ActualPaths = GetAbsoluteUrls(Args(0), Type)
  153. Dim Location = ActualPaths(0), WorkingDir = ActualPaths(1)
  154. Log($"[Control] 打开类自定义事件实际路径:{Location},工作目录:{WorkingDir}")
  155. '执行
  156. If Type = EventType.打开帮助 Then
  157. PageOtherHelp.EnterHelpPage(Location)
  158. Else
  159. If Not EventSafetyConfirm("即将执行:" & Location & If(Args.Length >= 2, " " & Args(1), "")) Then Return
  160. Dim Info As New ProcessStartInfo With {
  161. .Arguments = If(Args.Length >= 2, Args(1), ""),
  162. .FileName = Location,
  163. .WorkingDirectory = WorkingDir
  164. }
  165. StartProcess(Info)
  166. End If
  167. Catch ex As Exception
  168. Log(ex, "执行打开类自定义事件失败", LogLevel.Msgbox)
  169. End Try
  170. End Sub)
  171. Case EventType.启动游戏
  172. If Args(0) = "\current" Then
  173. If McVersionCurrent Is Nothing Then
  174. Hint("请先选择一个 Minecraft 版本!", HintType.Red)
  175. Return
  176. Else
  177. Args(0) = McVersionCurrent.Name
  178. End If
  179. End If
  180. RunInUi(
  181. Sub()
  182. If McLaunchStart(New McLaunchOptions With
  183. {.ServerIp = If(Args.Length >= 2, Args(1), Nothing), .Version = New McVersion(Args(0))}) Then
  184. Hint($"正在启动 {Args(0)}……")
  185. End If
  186. End Sub)
  187. Case EventType.复制文本
  188. ClipboardSet(Arg)
  189. Case EventType.刷新主页
  190. RunInUi(
  191. Sub()
  192. FrmLaunchRight.ForceRefresh()
  193. If String.IsNullOrEmpty(Arg) Then Hint("已刷新!", HintType.Green)
  194. End Sub)
  195. Case EventType.刷新页面
  196. If TypeOf FrmMain.PageRight Is IRefreshable Then
  197. RunInUiWait(Sub() CType(FrmMain.PageRight, IRefreshable).Refresh())
  198. If String.IsNullOrEmpty(Arg) Then Hint("已刷新!", HintType.Green)
  199. Else
  200. Hint("当前页面不支持刷新操作!", HintType.Red)
  201. End If
  202. Case EventType.刷新帮助
  203. RunInUiWait(Sub() PageOtherLeft.RefreshHelp())
  204. If String.IsNullOrEmpty(Arg) Then Hint("已刷新!", HintType.Green)
  205. Case EventType.今日人品
  206. PageOtherTest.Jrrp()
  207. Case EventType.内存优化
  208. RunInThread(Sub() PageOtherTest.MemoryOptimize(True))
  209. Case EventType.清理垃圾
  210. RunInThread(Sub() PageOtherTest.RubbishClear())
  211. Case EventType.弹出窗口
  212. If Args.Length = 1 Then Throw New Exception($"EventType {Type} 需要至少 2 个以 | 分割的参数,例如 弹窗标题|弹窗内容")
  213. MyMsgBox(Args(1).Replace("\n", vbCrLf), Args(0).Replace("\n", vbCrLf), If(Args.Length > 2, Args(2), "确定"))
  214. Case EventType.弹出提示
  215. Hint(Args(0).Replace("\n", vbCrLf), If(Args.Length = 1, HintType.Blue, Args(1).ParseToEnum(Of HintType)))
  216. Case EventType.切换页面
  217. RunInUi(Sub() FrmMain.PageChange(
  218. Args(0).ParseToEnum(Of FormMain.PageType),
  219. If(Args.Length = 1, FormMain.PageSubType.Default, Args(1).ParseToEnum(Of FormMain.PageSubType))))
  220. Case EventType.导入整合包, EventType.安装整合包
  221. RunInUi(Sub() ModpackInstall())
  222. Case EventType.下载文件
  223. Args(0) = Args(0).Replace("\", "/")
  224. If Not (Args(0).StartsWithF("http://", True) OrElse Args(0).StartsWithF("https://", True)) Then
  225. MyMsgBox("EventData 必须为以 http:// 或 https:// 开头的网址。" & vbCrLf & "PCL 不支持其他乱七八糟的下载协议。", "事件执行失败")
  226. Return
  227. End If
  228. If Not EventSafetyConfirm("即将从该网址下载文件:" & vbCrLf & Args(0)) Then Return
  229. RunInUi(
  230. Sub()
  231. Try
  232. Select Case Args.Length
  233. Case 1
  234. PageOtherTest.StartCustomDownload(Args(0), GetFileNameFromPath(Args(0)))
  235. Case 2
  236. PageOtherTest.StartCustomDownload(Args(0), Args(1))
  237. Case Else
  238. PageOtherTest.StartCustomDownload(Args(0), Args(1), Args(2))
  239. End Select
  240. Catch
  241. PageOtherTest.StartCustomDownload(Args(0), "未知")
  242. End Try
  243. End Sub)
  244. Case EventType.修改设置, EventType.写入设置
  245. If Args.Length = 1 Then Throw New Exception($"EventType {Type} 需要至少 2 个以 | 分割的参数,例如 UiLauncherTransparent|400")
  246. Setup.SetSafe(Args(0), Args(1), Version:=McVersionCurrent)
  247. If Args.Length = 2 Then Hint($"已写入设置:{Args(0)} → {Args(1)}", HintType.Green)
  248. Case EventType.修改变量, EventType.写入变量
  249. If Args.Length = 1 Then Throw New Exception($"EventType {Type} 需要至少 2 个以 | 分割的参数,例如 VariableName|SomeValue")
  250. WriteReg("CustomEvent" & Args(0), Args(1))
  251. If Args.Length = 2 Then Hint($"已写入变量:{Args(0)} → {Args(1)}", HintType.Green)
  252. Case EventType.加入房间
  253. RunInUi(
  254. Sub()
  255. If String.IsNullOrWhiteSpace(Arg) Then
  256. PageLinkMain.Join()
  257. Else
  258. If Not EventSafetyConfirm("即将加入第三方联机房间。") Then Return
  259. PageLinkMain.Join(Arg)
  260. End If
  261. End Sub)
  262. Case Else
  263. MyMsgBox("未知的事件类型:" & Type & vbCrLf & "请检查事件类型填写是否正确,或者 PCL 是否为最新版本。", "事件执行失败")
  264. End Select
  265. Catch ex As Exception
  266. Log(ex, $"事件执行失败({Type}, {Arg})", LogLevel.Msgbox)
  267. End Try
  268. End Sub
  269. ''' <summary>
  270. ''' 返回自定义事件的绝对 Url。实际返回 {绝对 Url, WorkingDir}。
  271. ''' 失败会抛出异常。
  272. ''' </summary>
  273. Public Shared Function GetAbsoluteUrls(RelativeUrl As String, Type As EventType) As String()
  274. '网页确认
  275. If RelativeUrl.StartsWithF("http", True) Then
  276. If RunInUi() Then
  277. Throw New Exception("能打开联网帮助页面的 MyListItem 必须手动设置 Title、Info 属性!")
  278. End If
  279. '获取文件名
  280. Dim RawFileName As String
  281. Try
  282. RawFileName = GetFileNameFromPath(RelativeUrl)
  283. If Not RawFileName.EndsWithF(".json", True) Then Throw New Exception("未指向 .json 后缀的文件")
  284. Catch ex As Exception
  285. Throw New Exception("联网帮助页面须指向一个帮助 JSON 文件,并在同路径下包含相应 XAML 文件!" & vbCrLf &
  286. "例如:" & vbCrLf &
  287. " - https://www.baidu.com/test.json(填写这个路径)" & vbCrLf &
  288. " - https://www.baidu.com/test.xaml(同时也需要包含这个文件)", ex)
  289. End Try
  290. '下载文件
  291. Dim LocalTemp As String = RequestTaskTempFolder() & RawFileName
  292. Log("[Event] 转换网络资源:" & RelativeUrl & " -> " & LocalTemp)
  293. Try
  294. NetDownloadByClient(RelativeUrl, LocalTemp)
  295. NetDownloadByClient(RelativeUrl.Replace(".json", ".xaml"), LocalTemp.Replace(".json", ".xaml"))
  296. Catch ex As Exception
  297. Throw New Exception("下载指定的文件失败!" & vbCrLf &
  298. "注意,联网帮助页面须指向一个帮助 JSON 文件,并在同路径下包含相应 XAML 文件!" & vbCrLf &
  299. "例如:" & vbCrLf &
  300. " - https://www.baidu.com/test.json(填写这个路径)" & vbCrLf &
  301. " - https://www.baidu.com/test.xaml(同时也需要包含这个文件)", ex)
  302. End Try
  303. RelativeUrl = LocalTemp
  304. End If
  305. RelativeUrl = RelativeUrl.Replace("/", "\").ToLower.TrimStart("\")
  306. '确认实际路径
  307. Dim Location As String, WorkingDir As String = Path & "PCL"
  308. HelpTryExtract()
  309. If RelativeUrl.Contains(":\") Then
  310. '绝对路径
  311. Location = RelativeUrl
  312. Log("[Control] 自定义事件中由绝对路径" & Type & ":" & Location)
  313. ElseIf File.Exists(Path & "PCL\" & RelativeUrl) Then
  314. '相对 PCL 文件夹的路径
  315. Location = Path & "PCL\" & RelativeUrl
  316. Log("[Control] 自定义事件中由相对 PCL 文件夹的路径" & Type & ":" & Location)
  317. ElseIf File.Exists(Path & "PCL\Help\" & RelativeUrl) Then
  318. '相对 PCL 本地帮助文件夹的路径
  319. Location = Path & "PCL\Help\" & RelativeUrl
  320. WorkingDir = Path & "PCL\Help\"
  321. Log("[Control] 自定义事件中由相对 PCL 本地帮助文件夹的路径" & Type & ":" & Location)
  322. ElseIf Type = EventType.打开帮助 AndAlso File.Exists(PathTemp & "Help\" & RelativeUrl) Then
  323. '相对 PCL 自带帮助文件夹的路径
  324. Location = PathTemp & "Help\" & RelativeUrl
  325. WorkingDir = PathTemp & "Help\"
  326. Log("[Control] 自定义事件中由相对 PCL 自带帮助文件夹的路径" & Type & ":" & Location)
  327. ElseIf Type = EventType.打开文件 OrElse Type = EventType.执行命令 Then
  328. '直接使用原有路径启动程序
  329. Location = RelativeUrl
  330. Log("[Control] 自定义事件中直接" & Type & ":" & Location)
  331. Else
  332. '打开帮助,但是格式不对劲
  333. Throw New FileNotFoundException("未找到 EventData 指向的本地 xaml 文件:" & RelativeUrl, RelativeUrl)
  334. End If
  335. Return {Location, WorkingDir}
  336. End Function
  337. ''' <summary>
  338. ''' 弹出安全确认弹窗。返回是否继续执行。
  339. ''' </summary>
  340. Private Shared Function EventSafetyConfirm(Message As String) As Boolean
  341. If Setup.Get("HintCustomCommand") Then Return True
  342. Select Case MyMsgBox(Message & vbCrLf & "请在确认没有安全隐患后再继续。", "执行确认", "继续", "继续且今后不再要求确认", "取消")
  343. Case 1
  344. Return True
  345. Case 2
  346. Setup.Set("HintCustomCommand", True)
  347. Return True
  348. Case Else
  349. Return False
  350. End Select
  351. End Function
  352. End Class