forked from erfanoabdi/Capire-Le-Treble
-
Notifications
You must be signed in to change notification settings - Fork 1
/
frm_Main.vb
219 lines (185 loc) · 9.72 KB
/
frm_Main.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
Imports System.ComponentModel
Public Class frm_Main
Const yh As String = """"
Private threadEnd As Boolean = False
Private Sub LockUnlockCtrl(Optional isUnlock As Boolean = True)
txtSysImg.Enabled = isUnlock
txtSysPartSize.Enabled = isUnlock
txtGSI.Enabled = isUnlock
txtDSSIOut.Enabled = isUnlock
txtPropFiles.Enabled = isUnlock
btnOpenGSI.Enabled = isUnlock
btnOpenPropFiles.Enabled = isUnlock
btnOpenSysImg.Enabled = isUnlock
btnSaveDSSI.Enabled = isUnlock
btnStart.Enabled = isUnlock
If chkGetFileContext.Checked Then
txtFileContexts.Enabled = isUnlock
btnOpenFileContexts.Enabled = isUnlock
End If
chkGetFileContext.Enabled = isUnlock
chkIsABDevice.Enabled = isUnlock
End Sub
Private Sub AppendTxtOut(ByVal text As String)
Me.txtOut.SelectionStart = txtOut.TextLength
Me.txtOut.ScrollToCaret()
Me.txtOut.AppendText(text & vbCrLf)
End Sub
Private Sub RunExec()
On Error Resume Next
Dim tmppath As String, gsipath As String, syspath As String, outtmp As String, binpath As String
tmppath = Application.StartupPath & "\Temp\"
binpath = Application.StartupPath & "\bin\"
syspath = tmppath & "sysimg-out\"
gsipath = tmppath & "gsiimg-out\"
outtmp = tmppath & "system\"
AppendTxtOut("I 临时文件目录: " & tmppath)
If CheckDir(tmppath) Then
AppendTxtOut("I 清理临时文件" & vbCrLf)
My.Computer.FileSystem.DeleteDirectory(tmppath, FileIO.DeleteDirectoryOption.DeleteAllContents)
End If
CreatePath(tmppath)
AppendTxtOut("")
If Not CheckFile(txtSysImg.Text) Then
AppendTxtOut("E 错误: 未找到System镜像文件!")
GoTo exitD
End If
If Val(txtSysPartSize.Text.Length) <= 0 Then
AppendTxtOut("E 错误: System分区大小设置错误!")
GoTo exitD
End If
If Not CheckFile(txtGSI.Text) Then
AppendTxtOut("E 错误: 未找到GSI镜像文件!")
GoTo exitD
End If
If chkGetFileContext.Checked And (Not CheckFile(txtFileContexts.Text)) Then
AppendTxtOut("W 警告: 找不到自定义file_contexts! 将自动生成!")
chkGetFileContext.Checked = False
End If
If Trim(txtDSSIOut.Text).Length = 0 Then
AppendTxtOut("W 警告: 未指定DSSI镜像文件的保存路径! 使用默认路径!")
txtDSSIOut.Text = Application.StartupPath & "\DSSI.img"
AppendTxtOut("I DSSI镜像文件的保存路径被设置为" & txtDSSIOut.Text & vbCrLf)
End If
AppendTxtOut("I System镜像输出到: " & syspath & vbCrLf)
AppendTxtOut("I 解包System镜像 ")
RunCommand(binpath & "Ext4Extractor.exe", yh & txtSysImg.Text & yh & " " & yh & syspath & yh, txtOut)
AppendTxtOut("I GSI镜像输出到: " & gsipath & vbCrLf)
AppendTxtOut("I 解包GSI镜像 ")
RunCommand(binpath & "Ext4Extractor.exe", yh & txtGSI.Text & yh & " " & yh & gsipath & yh, txtOut)
AppendTxtOut("I 复制文件 ")
If chkIsABDevice.Checked Then
My.Computer.FileSystem.CopyDirectory(syspath, outtmp)
My.Computer.FileSystem.DeleteDirectory(outtmp & "system", FileIO.DeleteDirectoryOption.DeleteAllContents)
My.Computer.FileSystem.CopyDirectory(gsipath, outtmp & "system")
Else
My.Computer.FileSystem.CopyDirectory(gsipath, outtmp)
End If
AppendTxtOut("I 删除vendor软链接 ")
If chkIsABDevice.Checked Then
IO.File.Delete(outtmp & "system\vendor")
Else
IO.File.Delete(outtmp & "vendor")
End If
AppendTxtOut("I 复制vendor文件夹" & vbCrLf)
If chkIsABDevice.Checked Then
My.Computer.FileSystem.CopyDirectory(syspath & "system\vendor", outtmp & "system\vendor")
Else
My.Computer.FileSystem.CopyDirectory(syspath & "vendor", outtmp & "vendor")
End If
If CheckFile(txtPropFiles.Text) Then
Dim flagNoRelpace As Boolean = False
AppendTxtOut("I 从proprietary-files.txt复制文件")
ConvertDosUnix(txtPropFiles.Text, tmppath & "proprietary-files.txt")
Dim strArr() As String = IO.File.ReadAllLines(tmppath & "proprietary-files.txt")
Dim tmpInt As Int32, tmpLine As String
For tmpInt = 0 To UBound(strArr)
tmpLine = Trim(strArr(tmpInt))
If tmpLine.Length = 0 Or Strings.Left(tmpLine, 1) = "#" Or InStr(tmpLine, "vendor") > 0 Then Continue For
If Strings.Left(tmpLine, 1) = "?" Then
flagNoRelpace = True
tmpLine = Strings.Right(tmpLine, tmpLine.Length - 1)
End If
If Strings.Left(tmpLine, 1) = "-" Then
tmpLine = Strings.Right(tmpLine, tmpLine.Length - 1)
End If
tmpLine = Replace(CutStr(tmpLine, ":", "|"), "/", "\")
If chkIsABDevice.Checked Then tmpLine = "system\" & tmpLine
If CheckFile(syspath & tmpLine) Then
CreatePath(outtmp & Mid(tmpLine, 1, InStrRev(tmpLine, "\")))
AppendTxtOut(" Copy: " & tmpLine)
If Not flagNoRelpace Then IO.File.Delete(outtmp & tmpLine)
IO.File.Copy(syspath & tmpLine, outtmp & tmpLine)
End If
Next
AppendTxtOut("")
End If
If Not chkGetFileContext.Checked Then
AppendTxtOut("I 生成file_contexts" & vbCrLf)
If chkIsABDevice.Checked Then
IO.File.AppendAllText(tmppath & "file_contexts", IO.File.ReadAllText(outtmp & "system\etc\selinux\plat_file_contexts"))
IO.File.AppendAllText(tmppath & "file_contexts", vbLf)
IO.File.AppendAllText(tmppath & "file_contexts", IO.File.ReadAllText(outtmp & "system\vendor\etc\selinux\nonplat_file_contexts"))
SortFile(tmppath & "file_contexts")
UniqFile(tmppath & "file_contexts")
Else
IO.File.AppendAllText(tmppath & "file_contexts", IO.File.ReadAllText(outtmp & "etc\selinux\plat_file_contexts"))
IO.File.AppendAllText(tmppath & "file_contexts", vbLf)
IO.File.AppendAllText(tmppath & "file_contexts", IO.File.ReadAllText(outtmp & "vendor\etc\selinux\nonplat_file_contexts"))
End If
Else
AppendTxtOut("I 复制自定义file_context到临时文件夹" & vbCrLf)
IO.File.Copy(txtFileContexts.Text, tmppath & "file_contexts")
End If
AppendTxtOut("I System分区大小: " & txtSysPartSize.Text & vbCrLf)
AppendTxtOut("I 创建DSSI镜像")
If chkIsABDevice.Checked Then
RunCommand(binpath & "make_ext4fs.exe", "-T 0 -S " & yh & tmppath & "file_contexts" & yh & " -l " & txtSysPartSize.Text & " -s -L / -a / " & yh & txtDSSIOut.Text & yh & " " & yh & outtmp & yh, txtOut)
Else
RunCommand(binpath & "make_ext4fs.exe", "-T 0 -S " & yh & tmppath & "file_contexts" & yh & " -l " & txtSysPartSize.Text & " -s -L system -a system " & yh & txtDSSIOut.Text & yh & " " & yh & outtmp & yh, txtOut)
End If
AppendTxtOut("I 删除临时文件" & vbCrLf)
My.Computer.FileSystem.DeleteDirectory(tmppath, FileIO.DeleteDirectoryOption.DeleteAllContents)
AppendTxtOut("I 完成!")
MsgBox("完成!", vbInformation)
exitD:
SyncLock Threading.Thread.CurrentThread
threadEnd = True
End SyncLock
End Sub
Private Sub btnOpenSysImg_Click(sender As Object, e As EventArgs) Handles btnOpenSysImg.Click
FileDialog("img镜像|*.img", "选择System镜像", txtSysImg)
End Sub
Private Sub btnOpenGSI_Click(sender As Object, e As EventArgs) Handles btnOpenGSI.Click
FileDialog("img镜像|*.img", "选择GSI镜像", txtGSI)
End Sub
Private Sub btnOpenPropFiles_Click(sender As Object, e As EventArgs) Handles btnOpenPropFiles.Click
FileDialog("txt文件|*.txt", "选择proprietary-files.txt", txtPropFiles)
End Sub
Private Sub btnSaveDSSI_Click(sender As Object, e As EventArgs) Handles btnSaveDSSI.Click
FileDialog("img镜像|*.img", "保存DSSI镜像", txtDSSIOut, False)
End Sub
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
LockUnlockCtrl(False)
Dim tmpThread As System.Threading.Thread = New System.Threading.Thread(AddressOf RunExec)
tmpThread.Start()
'tmpThread.Join()
Do While Not threadEnd
My.Application.DoEvents()
System.Threading.Thread.Sleep(5)
Loop
LockUnlockCtrl()
AppendTxtOut(vbCrLf)
End Sub
Private Sub btnOpenFileContexts_Click(sender As Object, e As EventArgs) Handles btnOpenFileContexts.Click
FileDialog("file_contexts|file_contexts", "选择file_contexts", txtFileContexts)
End Sub
Private Sub chkGetFileContext_CheckedChanged(sender As Object, e As EventArgs) Handles chkGetFileContext.CheckedChanged
txtFileContexts.Enabled = chkGetFileContext.Checked
btnOpenFileContexts.Enabled = chkGetFileContext.Checked
End Sub
Private Sub frm_Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'It's security because only one thread will change controls
CheckForIllegalCrossThreadCalls = False
End Sub
End Class