forked from xxdoc/NSEmuHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule.vb
257 lines (246 loc) · 12.5 KB
/
Module.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
Imports System.IO
Imports System.Net
Imports System.Net.Http
Imports System.Text.RegularExpressions
Imports System.Xml
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Module NSEmuHelperModule
Public AppPath As String = Application.StartupPath
'Public AppPath As String = "C:\Users\yidaozhan\source\repos\NSEmuHelper"
Public Config As New ConfigFilePattern
Public MainUILoaded As Boolean = False
Public ConfigUILoaded As Boolean = False
Public DownloadSources As New Newtonsoft.Json.Linq.JObject
'Public Const FIREFOX_USER_AGENT As String = "Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0"
Public Const GITHUB_PUBLIC_TOKEN As String = "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"
Public Async Function HTTPGetAsync(Url As String) As Task(Of String)
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 Or SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls
Dim MyClient As New HttpClient, Response As HttpResponseMessage
MyClient.DefaultRequestHeaders.Add("User-Agent", "NSEmuHelper/" & Application.ProductVersion)
Try
Response = Await MyClient.GetAsync(Url)
Catch QWQExpection As Exception
frmExpection.ShowMessage(QWQExpection.ToString)
Return 255
End Try
Select Case Response.StatusCode
Case 200
Return (Await Response.Content.ReadAsStringAsync)
Case 404
frmExpection.ShowMessage("HTTP 错误:404 Not Found")
End
Case 503
'留待备用
Case Else
MsgBox("HTTP 错误:" & Response.StatusCode & " " & Response.ReasonPhrase)
End
End Select
Return 0
End Function
Public Async Function GitHubAPI(Url As String) As Task(Of String)
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 Or SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls
Dim MyClient As New HttpClient, Response As HttpResponseMessage
MyClient.DefaultRequestHeaders.Add("User-Agent", "NSEmuHelper/" & Application.ProductVersion)
MyClient.DefaultRequestHeaders.Add("Authorization", GITHUB_PUBLIC_TOKEN)
Try
If Config.GitHubAPIProxy Then
Response = Await MyClient.GetAsync(New Uri(Config.CloudflareProxyPrefix & Url))
Else
Response = Await MyClient.GetAsync(New Uri(Url))
End If
Catch QWQExpection As Exception
frmExpection.ShowMessage(QWQExpection.ToString)
Return 255
End Try
Select Case Response.StatusCode
Case 200
Debug.Print("GitHub API Request Completed")
Return (Await Response.Content.ReadAsStringAsync)
Case 404
frmExpection.ShowMessage("HTTP 错误:404 Not Found")
MsgBox("发生错误")
Case 503
'留待备用
Case Else
MsgBox("HTTP 错误:" & Response.StatusCode & " " & Response.ReasonPhrase)
End
End Select
Return 0
End Function
Public Async Function GetMirrorFirmwareList() As Task(Of ArrayList)
Dim FirmwareList As New ArrayList, FirmwareJSON As JObject, FirmwareAPIURL As String
If Config.DownloadSource = "US3" Then
FirmwareAPIURL = DownloadSources("US3")("url").ToString
Else
FirmwareAPIURL = DownloadSources("OneDrive")("url").ToString
End If
FirmwareAPIURL &= "NSFirmware/?json"
FirmwareJSON = JObject.Parse(Await HTTPGetAsync(FirmwareAPIURL))
For Each VersionObject As JProperty In FirmwareJSON.Item("list")
VersionObject.CreateReader()
FirmwareList.Add(VersionObject.Value("name").ToString.Replace(".zip", "").Replace("Firmware_", ""))
Next
FirmwareList.Sort()
FirmwareList.Reverse()
Return FirmwareList
End Function
Public Async Function GetFullFirmwareList() As Task(Of ArrayList)
Dim FirmwareList As New ArrayList, tmpFirmwareVersion As String
Dim FirmwareXML As New XmlDocument
FirmwareXML.LoadXml(Await HTTPGetAsync(Config.CloudflareProxyPrefix & "https://archive.org/download/nintendo-switch-global-firmwares/nintendo-switch-global-firmwares_files.xml"))
Dim FirmwareNodeList As XmlNodeList = FirmwareXML.SelectNodes("/files/file")
For Each FirmwareNode As XmlNode In FirmwareNodeList
tmpFirmwareVersion = FirmwareNode.Attributes("name").Value
If tmpFirmwareVersion.Contains(".zip") Then
tmpFirmwareVersion = tmpFirmwareVersion.Replace("Firmware ", "").Replace(".zip", "")
If CInt(tmpFirmwareVersion.Split(".")(0)) >= 10 Then
FirmwareList.Add(tmpFirmwareVersion)
End If
End If
Next
FirmwareList.Sort()
FirmwareList.Reverse()
Return FirmwareList
End Function
Public Async Function GetFirmwareMD5(FirmwareVersion As String) As Task(Of String)
Dim MD5Text As String = Await HTTPGetAsync(Config.CloudflareProxyPrefix & "https://archive.org/download/nintendo-switch-global-firmwares/Official%20Global%20Firmware%20MD5%20Hashs.txt")
For Each Line As String In MD5Text.Replace(Chr(9), "/").Replace("Firmware ", "").Split(vbCrLf)
If Line.Split("/")(0).Replace(vbLf, "") = FirmwareVersion Then Return Line.Split("/").Last
Next
Return 0
End Function
Public Function MD5Sums(FilePath As String)
With System.Security.Cryptography.MD5.Create()
Return BitConverter.ToString(.ComputeHash(File.OpenRead(FilePath))).Replace("-", "").ToLower
End With
End Function '算文件md5
Public Sub CreateDirectory(FolderPath As String)
Try
If Not My.Computer.FileSystem.DirectoryExists(FolderPath) Then My.Computer.FileSystem.CreateDirectory(FolderPath)
Catch ex As Exception
frmExpection.ShowMessage(ex.Message)
MsgBox("文件系统访问错误") '仅用于暂停程序
End Try
End Sub '懒人创建文件夹
Public Enum EmulatorType
Yuzu = 1
Ryujinx = 2
YuzuUpdate = 3
YuzuFirmware = 4
RyujinxUpdate = 5
RyujinxFirmware = 6
End Enum
End Module
Module LatestVersion
Async Function YuzuEarlyAccess() As Task(Of String)
Select Case DownloadSources(Config.DownloadSource)("type")
Case "github"
Try
Return Replace(JsonConvert.DeserializeObject(Await GitHubAPI("https://api.github.com/repos/pineappleea/pineapple-src/releases"))(2)("tag_name"), "EA-", "")
Catch QWQExpection As Exception
frmExpection.ShowMessage("可能的解决方案:GitHub API 调用超出限制,请等一会重试。" & vbCrLf & "详细报错:" & QWQExpection.Message)
End
End Try
Case "onemanager"
Dim VersionNumbers As New ArrayList, VersionList As JObject
VersionList = JObject.Parse(Await HTTPGetAsync(DownloadSources(Config.DownloadSource)("url").ToString & "YuzuEarlyAccess/?json"))
For Each VersionObject As JProperty In VersionList.Item("list")
VersionObject.CreateReader()
VersionNumbers.Add(CInt(VersionObject.Value("name").ToString.Replace("Windows-Yuzu-EA-", "").Replace(".7z", "")))
Next
VersionNumbers.Sort()
VersionNumbers.Reverse()
Return VersionNumbers(0)
Case Else
MsgBox("无效的下载源!")
End
End Select
End Function
Async Function YuzuMainline() As Task(Of String)
Select Case DownloadSources(Config.DownloadSource)("type")
Case "github"
Try
Return Replace(JsonConvert.DeserializeObject(Await GitHubAPI("https://api.github.com/repos/yuzu-emu/yuzu-mainline/releases/latest"))("tag_name"), "mainline-0-", "")
Catch QWQExpection As Exception
frmExpection.ShowMessage("可能的解决方案:GitHub API 调用超出限制,请等一会重试。" & vbCrLf & "详细报错:" & QWQExpection.Message)
End
End Try
Case "onemanager"
Dim VersionNumbers As New ArrayList, VersionList As JObject
VersionList = JObject.Parse(Await HTTPGetAsync(DownloadSources(Config.DownloadSource)("url").ToString & "YuzuMainline/?json"))
For Each VersionObject As JProperty In VersionList.Item("list")
VersionObject.CreateReader()
VersionNumbers.Add(CInt(VersionObject.Value("name").ToString.Replace("yuzu-windows-msvc-", "").Replace(".7z", "")))
Next
VersionNumbers.Sort()
VersionNumbers.Reverse()
Return VersionNumbers(0)
Case Else
MsgBox("无效的下载源!")
End
End Select
End Function
Async Function Ryujinx() As Task(Of String)
Select Case DownloadSources(Config.DownloadSource)("type")
Case "github"
Try
Return JsonConvert.DeserializeObject(Await GitHubAPI("https://api.github.com/repos/Ryujinx/release-channel-master/releases/latest"))("tag_name")
Catch QWQExpection As Exception
frmExpection.ShowMessage("可能的解决方案:GitHub API 调用超出限制,请等一会重试。" & vbCrLf & "详细报错:" & QWQExpection.Message)
End
End Try
Case "onemanager"
Dim VersionNumbers As New ArrayList, VersionList As JObject
VersionList = JObject.Parse(Await HTTPGetAsync(DownloadSources(Config.DownloadSource)("url").ToString & "Ryujinx/?json"))
For Each VersionObject As JProperty In VersionList.Item("list")
VersionObject.CreateReader()
VersionNumbers.Add(VersionObject.Value("name").ToString.Replace("ryujinx-", "").Replace("-win_x64.zip", ""))
Next
VersionNumbers.Sort()
VersionNumbers.Reverse()
Return VersionNumbers(0)
Case Else
MsgBox("无效的下载源!")
End
End Select
End Function
End Module
Module ModDownloader
Function ParseTitleList(FileList As ObjectModel.ReadOnlyCollection(Of String), TitleList As String) As ArrayList
Dim TitleListXML As New XmlDocument, ReturnList As New ArrayList
TitleListXML.LoadXml(TitleList)
Dim TitleNodeList As XmlNodeList = TitleListXML.SelectNodes("/games/game")
For Each TitleNode As XmlNode In TitleNodeList
For Each TitleIDFolder In FileList
If IO.Path.GetFileName(TitleIDFolder) = TitleNode.LastChild.InnerText Then
ReturnList.Add(TitleNode.FirstChild.InnerText)
End If
Next
Next
Return ReturnList
End Function
Function GetModList(GameName As String, GameModListCache As String) As ArrayList
Dim GameModList As String() = Split(Split(GameModListCache,
vbLf & vbLf & "---" & vbLf & vbLf)(1).Replace("# " & vbLf, "#" & vbLf).Replace("#" & vbLf & "#", "#"),
vbLf & "#" & vbLf)
Dim ReturnList As New ArrayList
Dim TempDictionary As Dictionary(Of String, String)
Dim ModList As String()
For Each GameMod As String In GameModList
ModList = GameMod.Trim(vbLf).Split(vbLf)
If ModList(0) = "### " & GameName Then
For i As Integer = 3 To UBound(ModList)
TempDictionary = New Dictionary(Of String, String)
TempDictionary.Add("Name", New Regex("\[.*?\]").Match(Split(ModList(i), "| ")(1)).ToString.Replace("[", "").Replace("]", ""))
TempDictionary.Add("Url", New Regex("\(.*?\)").Match(Split(ModList(i), "| ")(1)).ToString.Replace("(", "").Replace(")", ""))
TempDictionary.Add("Desc", Split(ModList(i), "| ")(2).Trim())
TempDictionary.Add("Version", Split(ModList(i), "| ")(3).Trim().Replace("`", ""))
TempDictionary.Add("Author", Split(ModList(i), "| ")(4).Trim())
ReturnList.Add(TempDictionary)
Next
Return ReturnList
End If
Next
End Function
End Module