-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecompile.ps1
More file actions
281 lines (255 loc) · 10.6 KB
/
Copy pathdecompile.ps1
File metadata and controls
281 lines (255 loc) · 10.6 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
param(
[Parameter(Mandatory = $true)]
[string]$McVersion,
[string]$WorkDir = "",
[ValidateSet("mojang", "fabric")]
[string]$MappingType = "fabric"
)
function Error-Exit {
param ([string]$Message)
Write-Error $Message
exit 1
}
function Info {
param ([string]$Message)
Write-Host "[+] $Message"
}
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Definition
if ([string]::IsNullOrEmpty($WorkDir)) {
$WorkDir = Join-Path $ScriptDir "..\minecraft-src-$McVersion"
}
$WorkDir = Resolve-Path -Path $WorkDir
New-Item -ItemType Directory -Path $WorkDir -Force | Out-Null
Set-Location $WorkDir
$ToolsDir = Join-Path $ScriptDir "tools"
$SpecialSourceJar = Join-Path $ToolsDir "specialsource.jar"
$JoptSimpleJar = Join-Path $ToolsDir "jopt-simple.jar"
$AsmJar = Join-Path $ToolsDir "asm.jar"
$AsmCommonsJar = Join-Path $ToolsDir "asm-commons.jar"
$AsmUtilJar = Join-Path $ToolsDir "asm-util.jar"
$AsmTreeJar = Join-Path $ToolsDir "asm-tree.jar"
$GuavaJar = Join-Path $ToolsDir "guava.jar"
$VineflowerJar = Join-Path $ToolsDir "vineflower.jar"
$TRCJar = Join-Path $ToolsDir "trc.jar"
$RequiredFiles = @($VineflowerJar, $SpecialSourceJar, $JoptSimpleJar, $AsmJar, $AsmCommonsJar, $AsmUtilJar, $AsmTreeJar, $GuavaJar, $TRCJar)
foreach ($File in $RequiredFiles) {
if (-not (Test-Path $File)) {
Error-Exit "Required tool not found: $File"
}
}
Info "Fetching version manifest..."
$ManifestUrl = "https://piston-meta.mojang.com/mc/game/version_manifest.json"
try {
$Manifest = Invoke-WebRequest -Uri $ManifestUrl -UseBasicParsing | ConvertFrom-Json
} catch {
Error-Exit "Failed to retrieve version manifest."
}
$VersionInfo = $Manifest.versions | Where-Object { $_.id -eq $McVersion }
if (-not $VersionInfo) {
Error-Exit "Version $McVersion not found in manifest."
}
$VersionUrl = $VersionInfo.url
try {
$VersionData = Invoke-WebRequest -Uri $VersionUrl -UseBasicParsing | ConvertFrom-Json
} catch {
Error-Exit "Failed to retrieve version data."
}
$LibrariesDir = Join-Path (Split-Path $WorkDir -Parent) "libraries"
foreach ($lib in $VersionData.libraries) {
if ($lib.downloads -and $lib.downloads.artifact) {
$artifact = $lib.downloads.artifact
$libPath = Join-Path $LibrariesDir $artifact.path
$libDir = Split-Path $libPath -Parent
if (-not (Test-Path $libDir)) {
New-Item -ItemType Directory -Path $libDir -Force | Out-Null
}
if (Test-Path $libPath) {
Info "Library already exists, skipping download: $($lib.name)"
continue
}
Info "Downloading library: $($lib.name)"
try {
Invoke-WebRequest -Uri $artifact.url -OutFile $libPath -UseBasicParsing -ErrorAction Stop
} catch {
Write-Warning "Failed to download library $($lib.name) from $($artifact.url)"
}
}
}
$ServerJarUrl = $VersionData.downloads.server.url
if (-not $ServerJarUrl) {
Error-Exit "Server JAR URL not found for version $McVersion."
}
Info "Downloading server JAR..."
Invoke-WebRequest -Uri $ServerJarUrl -OutFile "server.jar" -UseBasicParsing
Add-Type -AssemblyName System.IO.Compression.FileSystem
Info "Checking for Bundler..."
$HasBundler = $false
if (-not (Test-Path "server.jar")) {
Error-Exit "server.jar not found after download."
}
$ServerJarInfo = Get-Item "server.jar"
Write-Output "Inspecting server.jar at path: $($ServerJarInfo.FullName), size: $($ServerJarInfo.Length) bytes"
try {
$Zip = [System.IO.Compression.ZipFile]::OpenRead($ServerJarInfo.FullName)
foreach ($Entry in $Zip.Entries) {
if ($Entry.FullName -eq "net/minecraft/bundler/Main.class") {
$HasBundler = $true
break
}
}
$Zip.Dispose()
} catch {
Error-Exit "Failed to inspect server.jar for Bundler. $_"
}
if ($HasBundler) {
Info "Bundler detected. Unpacking..."
& java -cp $ServerJarInfo.FullName net.minecraft.bundler.Main
$ServerVersionJar = Get-ChildItem -Path "versions" -Recurse -Filter "*.jar" | Select-Object -First 1
if (-not $ServerVersionJar) {
Error-Exit "Unpacked server JAR not found."
}
$ServerJarPath = $ServerVersionJar.FullName
} else {
Info "No Bundler detected. Using server.jar directly."
$ServerJarPath = "server.jar"
}
$StrippedJarPath = Join-Path $WorkDir "server-stripped.jar"
Info "Stripping META-INF from server jar to avoid signature issues..."
Add-Type -AssemblyName System.IO.Compression.FileSystem
try {
if (Test-Path $StrippedJarPath) { Remove-Item $StrippedJarPath -Force }
$inStream = [System.IO.File]::OpenRead($ServerJarPath)
$outStream = [System.IO.File]::OpenWrite($StrippedJarPath)
$inZip = New-Object System.IO.Compression.ZipArchive($inStream, [System.IO.Compression.ZipArchiveMode]::Read)
$outZip = New-Object System.IO.Compression.ZipArchive($outStream, [System.IO.Compression.ZipArchiveMode]::Create)
foreach ($entry in $inZip.Entries) {
if ($entry.FullName -like "META-INF/*") { continue }
$newEntry = $outZip.CreateEntry($entry.FullName)
$entryStream = $entry.Open()
$newEntryStream = $newEntry.Open()
$entryStream.CopyTo($newEntryStream)
$entryStream.Close()
$newEntryStream.Close()
}
$inZip.Dispose()
$outZip.Dispose()
$inStream.Close()
$outStream.Close()
$ServerJarPath = $StrippedJarPath
} catch {
Error-Exit "Failed to strip META-INF from server jar: $_"
}
if ($MappingType -eq "mojang") {
$MappingsUrl = $VersionData.downloads.server_mappings.url
if (-not $MappingsUrl) {
Error-Exit "Mappings URL not found for version $McVersion."
}
Info "Downloading Mojang mappings..."
Invoke-WebRequest -Uri $MappingsUrl -OutFile "mappings.txt" -UseBasicParsing
Info "Applying Mojang mappings..."
New-Item -ItemType Directory -Path "build" -Force | Out-Null
$MappedJar = "build\server-mapped.jar"
$Classpath = "$SpecialSourceJar;$JoptSimpleJar;$AsmJar;$AsmCommonsJar;$AsmUtilJar;$AsmTreeJar;$GuavaJar"
& java -cp $Classpath net.md_5.specialsource.SpecialSource `
-i $ServerJarPath `
-m "mappings.txt" `
-o $MappedJar
}
elseif ($MappingType -eq "fabric") {
$FabricMetaUrl = "https://meta.fabricmc.net/v2/versions/intermediary/$McVersion"
Info "Fetching Fabric intermediary metadata..."
try {
$FabricMeta = Invoke-WebRequest -Uri $FabricMetaUrl -UseBasicParsing | ConvertFrom-Json
} catch {
Error-Exit "Failed to retrieve Fabric intermediary metadata."
}
if (-not $FabricMeta -or $FabricMeta.Count -eq 0) {
Error-Exit "No Fabric intermediary found for version $McVersion."
}
$MavenCoord = $FabricMeta[0].maven
$parts = $MavenCoord -split ':'
if ($parts.Length -ne 3) {
Error-Exit "Invalid Maven coordinate for intermediary: $MavenCoord"
}
$group = $parts[0] -replace '\.', '/'
$artifact = $parts[1]
$version = $parts[2]
# Try to download .tiny, fallback to extracting from .jar if not found
$IntermediaryTinyUrl = "https://maven.fabricmc.net/$group/$artifact/$version/$artifact-$version.tiny"
$IntermediaryJarUrl = "https://maven.fabricmc.net/$group/$artifact/$version/$artifact-$version.jar"
$TinyPath = Join-Path $WorkDir "intermediary.tiny"
$JarPath = Join-Path $WorkDir "intermediary.jar"
$downloadedTiny = $false
Info "Attempting to download Fabric intermediary mappings (.tiny): $IntermediaryTinyUrl"
try {
Invoke-WebRequest -Uri $IntermediaryTinyUrl -OutFile $TinyPath -UseBasicParsing -ErrorAction Stop
$downloadedTiny = $true
} catch {
Info "Direct .tiny not found, downloading intermediary jar: $IntermediaryJarUrl"
Invoke-WebRequest -Uri $IntermediaryJarUrl -OutFile $JarPath -UseBasicParsing
Add-Type -AssemblyName System.IO.Compression.FileSystem
$zip = [System.IO.Compression.ZipFile]::OpenRead((Resolve-Path $JarPath))
$entry = $zip.Entries | Where-Object { $_.FullName -eq "mappings/mappings.tiny" }
if (-not $entry) {
$zip.Dispose()
Error-Exit "mappings/mappings.tiny not found in intermediary jar."
}
$stream = $entry.Open()
$fileStream = [System.IO.File]::OpenWrite($TinyPath)
$stream.CopyTo($fileStream)
$fileStream.Close()
$stream.Close()
$zip.Dispose()
if (-not (Test-Path $TinyPath) -or ((Get-Item $TinyPath).Length -eq 0)) {
Error-Exit "Failed to extract mappings.tiny from intermediary jar."
}
Remove-Item $JarPath -Force
}
Info "Applying Fabric intermediary mappings with tiny-remapper..."
New-Item -ItemType Directory -Path "build" -Force | Out-Null
$MappedJar = "build\server-mapped.jar"
& java -jar $TRCJar `
--input "$ServerJarPath" `
--output "$MappedJar" `
--mappings $TinyPath `
--from "official" `
--to "intermediary"
$NamedMappingsUrl = "https://maven.fabricmc.net/net/fabricmc/yarn/$McVersion+build.1/yarn-$McVersion+build.1-tiny.gz"
$NamedTinyGzPath = Join-Path $WorkDir "named.tiny.gz"
$NamedTinyPath = Join-Path $WorkDir "named.tiny"
try {
Info "Downloading Fabric named mappings: $NamedMappingsUrl"
Invoke-WebRequest -Uri $NamedMappingsUrl -OutFile $NamedTinyGzPath -UseBasicParsing -ErrorAction Stop
# Decompress .gz to .tiny
Info "Decompressing Fabric named mappings .gz..."
$inStream = [System.IO.File]::OpenRead($NamedTinyGzPath)
$outStream = [System.IO.File]::Create($NamedTinyPath)
$gzipStream = New-Object System.IO.Compression.GzipStream($inStream, [System.IO.Compression.CompressionMode]::Decompress)
$gzipStream.CopyTo($outStream)
$gzipStream.Close()
$inStream.Close()
$outStream.Close()
Remove-Item $NamedTinyGzPath -Force
$NamedAvailable = $true
} catch {
Info "Fabric named mappings not found for this version, skipping named remap."
$NamedAvailable = $false
}
if ($NamedAvailable) {
$NamedMappedJar = "build\server-named.jar"
Info "Applying Fabric named mappings with tiny-remapper..."
& java -jar $TRCJar `
--input "$MappedJar" `
--output "$NamedMappedJar" `
--mappings $NamedTinyPath `
--from "intermediary" `
--to "named"
$MappedJar = $NamedMappedJar
}
} else {
Error-Exit "Unknown mapping type: $MappingType"
}
Info "Decompiling with VineFlower..."
New-Item -ItemType Directory -Path "sources" -Force | Out-Null
& java -jar $VineflowerJar $MappedJar --outputdir sources
Info "Decompilation complete. Sources located at: $(Resolve-Path 'sources')"