This repository was archived by the owner on Oct 20, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathModManager.vb
More file actions
234 lines (202 loc) · 8.81 KB
/
Copy pathModManager.vb
File metadata and controls
234 lines (202 loc) · 8.81 KB
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
Imports System.IO
Imports System.Collections.Generic
Imports Newtonsoft.Json
''' <summary>
''' Mod 管理模块。负责扫描、启用/禁用、删除、导入以及刷新 Mod。
''' </summary>
Public Module ModManager
''' <summary>
''' 支持的 Mod 文件扩展名列表。
''' </summary>
Public ReadOnly MOD_EXTENSIONS As New List(Of String) From {".jar", ".litemod", ".zip"}
Private Const DISABLED_SUFFIX As String = ".disabled"
''' <summary>
''' 返回指定版本的 mods 文件夹路径;如不存在则自动创建。
''' </summary>
''' <param name="versionId">版本号。</param>
Public Function GetModsFolder(versionId As String) As String
Try
Dim workDir As String = If(VersionManager.GetWorkDir(), "")
If String.IsNullOrWhiteSpace(versionId) Then
Return String.Empty
End If
Dim folder As String = Path.Combine(workDir, "versions", versionId, "mods")
If Not Directory.Exists(folder) Then
Directory.CreateDirectory(folder)
End If
Return folder
Catch
Return String.Empty
End Try
End Function
''' <summary>
''' 扫描 mods 文件夹,返回 ModEntry 列表。
''' </summary>
''' <param name="versionId">版本号。</param>
Public Function LoadMods(versionId As String) As List(Of ModEntry)
Dim result As New List(Of ModEntry)()
Try
Dim folder As String = GetModsFolder(versionId)
If String.IsNullOrWhiteSpace(folder) OrElse Not Directory.Exists(folder) Then
Return result
End If
Dim files As String()
Try
files = Directory.GetFiles(folder, "*.*", SearchOption.TopDirectoryOnly)
Catch
Return result
End Try
For Each file As String In files
Try
Dim fileName As String = Path.GetFileName(file)
Dim ext As String = Path.GetExtension(fileName)
Dim coreExt As String = ext
Dim isEnabled As Boolean = True
If String.Equals(ext, DISABLED_SUFFIX, StringComparison.OrdinalIgnoreCase) Then
Dim rawName As String = Path.GetFileNameWithoutExtension(fileName)
coreExt = Path.GetExtension(rawName)
isEnabled = False
End If
If Not MOD_EXTENSIONS.Contains(coreExt, StringComparer.OrdinalIgnoreCase) Then
Continue For
End If
Dim displayName As String = Path.GetFileNameWithoutExtension(If(isEnabled, fileName, Path.GetFileNameWithoutExtension(fileName)))
Dim entry As New ModEntry() With {
.FileName = fileName,
.Name = displayName,
.Description = String.Empty,
.FilePath = file,
.IsEnabled = isEnabled,
.Size = New FileInfo(file).Length
}
result.Add(entry)
Catch
End Try
Next
Catch
result.Clear()
End Try
Return result
End Function
''' <summary>
''' 切换一个 Mod 的启用状态。
''' </summary>
''' <param name="versionId">版本号。</param>
''' <param name="modFileName">Mod 文件名(如 xxx.jar 或 xxx.jar.disabled)。</param>
Public Sub ToggleMod(versionId As String, modFileName As String)
Try
Dim folder As String = GetModsFolder(versionId)
If String.IsNullOrWhiteSpace(folder) Then Return
If String.IsNullOrWhiteSpace(modFileName) Then Return
Dim sourcePath As String = Path.Combine(folder, modFileName)
If Not File.Exists(sourcePath) Then Return
Dim targetPath As String
If modFileName.EndsWith(DISABLED_SUFFIX, StringComparison.OrdinalIgnoreCase) Then
targetPath = Path.Combine(folder, modFileName.Substring(0, modFileName.Length - DISABLED_SUFFIX.Length))
Else
targetPath = Path.Combine(folder, modFileName & DISABLED_SUFFIX)
End If
If String.Equals(sourcePath, targetPath, StringComparison.OrdinalIgnoreCase) Then Return
If File.Exists(targetPath) Then
File.Delete(targetPath)
End If
File.Move(sourcePath, targetPath)
Catch
End Try
End Sub
''' <summary>
''' 启用一个 Mod(去掉 .disabled 后缀)。
''' </summary>
Public Sub EnableMod(versionId As String, modFileName As String)
Try
If String.IsNullOrWhiteSpace(modFileName) Then Return
If Not modFileName.EndsWith(DISABLED_SUFFIX, StringComparison.OrdinalIgnoreCase) Then Return
Dim folder As String = GetModsFolder(versionId)
If String.IsNullOrWhiteSpace(folder) Then Return
Dim sourcePath As String = Path.Combine(folder, modFileName)
If Not File.Exists(sourcePath) Then Return
Dim targetPath As String = Path.Combine(folder, modFileName.Substring(0, modFileName.Length - DISABLED_SUFFIX.Length))
If File.Exists(targetPath) Then File.Delete(targetPath)
File.Move(sourcePath, targetPath)
Catch
End Try
End Sub
''' <summary>
''' 禁用一个 Mod(加上 .disabled 后缀)。
''' </summary>
Public Sub DisableMod(versionId As String, modFileName As String)
Try
If String.IsNullOrWhiteSpace(modFileName) Then Return
If modFileName.EndsWith(DISABLED_SUFFIX, StringComparison.OrdinalIgnoreCase) Then Return
Dim folder As String = GetModsFolder(versionId)
If String.IsNullOrWhiteSpace(folder) Then Return
Dim sourcePath As String = Path.Combine(folder, modFileName)
If Not File.Exists(sourcePath) Then Return
Dim targetPath As String = Path.Combine(folder, modFileName & DISABLED_SUFFIX)
If File.Exists(targetPath) Then File.Delete(targetPath)
File.Move(sourcePath, targetPath)
Catch
End Try
End Sub
''' <summary>
''' 删除一个 Mod 文件。
''' </summary>
Public Sub DeleteMod(versionId As String, modFileName As String)
Try
If String.IsNullOrWhiteSpace(modFileName) Then Return
Dim folder As String = GetModsFolder(versionId)
If String.IsNullOrWhiteSpace(folder) Then Return
Dim filePath As String = Path.Combine(folder, modFileName)
If File.Exists(filePath) Then
File.Delete(filePath)
End If
Catch
End Try
End Sub
''' <summary>
''' 将外部文件复制到 mods 文件夹。若目标文件名已存在,会自动追加 (1)、(2) 等。
''' </summary>
''' <param name="versionId">版本号。</param>
''' <param name="sourceFilePath">源文件完整路径。</param>
''' <returns>是否成功。</returns>
Public Function ImportMod(versionId As String, sourceFilePath As String) As Boolean
Try
If String.IsNullOrWhiteSpace(sourceFilePath) Then Return False
If Not File.Exists(sourceFilePath) Then Return False
Dim folder As String = GetModsFolder(versionId)
If String.IsNullOrWhiteSpace(folder) Then Return False
Dim baseFileName As String = Path.GetFileName(sourceFilePath)
Dim baseName As String = Path.GetFileNameWithoutExtension(baseFileName)
Dim baseExt As String = Path.GetExtension(baseFileName)
Dim counter As Integer = 1
Dim targetFileName As String = baseFileName
Dim targetPath As String = Path.Combine(folder, targetFileName)
While File.Exists(targetPath)
targetFileName = baseName & " (" & counter.ToString() & ")" & baseExt
targetPath = Path.Combine(folder, targetFileName)
counter += 1
If counter > 10000 Then
Return False
End If
End While
File.Copy(sourceFilePath, targetPath, False)
Return True
Catch
Return False
End Try
End Function
''' <summary>
''' 清空给定列表并重新从磁盘加载。
''' </summary>
Public Sub RefreshMods(versionId As String, modList As List(Of ModEntry))
Try
If modList Is Nothing Then Return
modList.Clear()
Dim fresh As List(Of ModEntry) = LoadMods(versionId)
For Each entry As ModEntry In fresh
modList.Add(entry)
Next
Catch
End Try
End Sub
End Module