Created
April 28, 2020 17:37
-
-
Save KisaragiEffective/b54fe9db3db9dae38a411bc7b40705ff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.* | |
import java.net.HttpURLConnection | |
import java.net.URL | |
import java.nio.file.Files | |
import java.nio.file.Paths | |
import java.text.DecimalFormat | |
import java.util.* | |
import java.util.zip.ZipEntry | |
import java.util.zip.ZipInputStream | |
val version = "1.12.2" | |
val skipDownloadJar = true | |
if (!skipDownloadJar) { | |
println("Getting jar process: version = $version") | |
val aws = "https://s3.amazonaws.com/Minecraft.Download/versions/$version/minecraft_server.$version.jar" | |
println("Getting jar from $aws") | |
val http = (URL(aws).openConnection() as HttpURLConnection).apply { | |
requestMethod = "GET" | |
}; | |
val dot = kotlin.concurrent.timer("print", period = 1000) { | |
print("|") | |
} | |
val content = http.inputStream.buffered().readBytes() | |
println() | |
println((1..5).map{" $it $it"}.joinToString("", prefix = " ")) | |
println(" 5 0".repeat(5) + " 5") | |
dot.cancel() | |
println() | |
println("Successful getting. (${DecimalFormat.getNumberInstance().format(content.size / 1_000_000.0)} MB)") | |
println("File writing...") | |
val file = File("./out/vanilla.jar").apply { | |
delete() | |
createNewFile() | |
} | |
file.writeBytes(content) | |
println(file.readBytes().size) | |
} | |
val skipDownloadMapping = true | |
if (!skipDownloadMapping) { | |
val ver2path = mapOf( | |
"1.12.2" to URL("http://export.mcpbot.bspk.rs/mcp/1.12/mcp-1.12-srg.zip") | |
) | |
val targetURL = ver2path[version] ?: throw UnsupportedOperationException( | |
"Unsupported version: $version; expected: one of " + ver2path.keys.joinToString(", ", "[", "]")) | |
val input = (targetURL.openConnection() as HttpURLConnection).apply { | |
requestMethod = "GET" | |
connect() | |
}.getInputStream().readBytes() | |
val zipfile = Paths.get("./mcp-$version-mapping.zip") | |
zipfile.toFile().outputStream().apply { | |
write(input) | |
} | |
// val target = Paths.get("./mcp-$version/") | |
extract() | |
} | |
val generateSyntheticCode = true | |
if (generateSyntheticCode) { | |
println("Generating code...") | |
val mapfile = File("./mcp-$version/joined.srg").inputStream().bufferedReader() | |
val lines = mapfile.lineSequence() | |
.filter { it.split(' ')[1] != it.split(' ')[2]} | |
.groupBy { line -> line.split(": ")[0] } | |
(lines.get("CL") ?: emptyList()).forEach { | |
val splitten = it.split(" ") | |
val obfucated = splitten[1] | |
val obfucatedClassName = obfucated.replace("/", ".") | |
val normalized = splitten[2] | |
val normalizedPackage = normalized.replace("/", ".") | |
val normalizedClassName = normalized.split("/").takeLast(1).single() | |
println("$obfucated -> $normalized") | |
val packaged = normalized.split("/").dropLast(1).joinToString("/") | |
val `package` = File("C:/Users/Obsidian550D/Documents/intellij/nms-v12-alias/src/main/kotlin/$packaged") | |
with (`package`) { | |
mkdirs() | |
setWritable(true) | |
setReadable(true) | |
setExecutable(true) | |
require(this.exists()) { "must exist" } | |
val alias = File("${this.canonicalPath}/typealias.kt") | |
/* alias.also { a -> | |
if (!a.exists()) { | |
createNewFile() | |
a.outputStream().bufferedWriter().appendln(""" | |
// ### WARNING! THIS CODE CAN'T BE REDISTRIBUTED SINCE DECOMPILED CODE! | |
// ### SHADE IT UNDER YOUR OWN RISK! | |
package $normalizedPackage | |
""".trimIndent()) | |
} | |
a.setWritable(true) | |
a.setReadable(true) | |
a.setExecutable(true) | |
println(a.canonicalPath) | |
a.outputStream().fd.sync() | |
} */ | |
require(alias.exists()) { "must exist" } | |
alias.outputStream().bufferedWriter().use { | |
it.appendln("typealias $normalizedClassName = $obfucatedClassName") | |
} | |
val size = alias.inputStream().readBytes().size | |
require(size > 3) | |
} | |
} | |
} | |
fun <T, R> T.pipe(f: (T) -> R): R { | |
return f(this) | |
} | |
fun ZipInputStream.toIterator(): Iterator<ZipEntry> { | |
val list = LinkedList<ZipEntry>() | |
var e: ZipEntry? | |
while (this.nextEntry.also { e = it } != null) { | |
if (e != null) list.add(e as ZipEntry) | |
} | |
list.removeLast() | |
return list.iterator() | |
} | |
fun ZipInputStream.toIterable(): Iterable<ZipEntry> { | |
return object : Iterable<ZipEntry> { | |
override fun iterator() = toIterator() | |
} | |
} | |
fun extract() { | |
val fileZip = "./mcp-$version-mapping.zip" | |
val destDir = File("./mcp-$version") | |
val buffer = ByteArray(1024) | |
val zis = ZipInputStream(FileInputStream(fileZip)) | |
var zipEntry = zis.nextEntry | |
while (zipEntry != null) { | |
val newFile = newFile(destDir, zipEntry) | |
val fos = FileOutputStream(newFile) | |
var len: Int | |
while (zis.read(buffer).also { len = it } > 0) { | |
fos.write(buffer, 0, len) | |
} | |
fos.close() | |
zipEntry = zis.nextEntry | |
} | |
zis.closeEntry() | |
zis.close() | |
} | |
@Throws(IOException::class) | |
fun newFile(destinationDir: File, zipEntry: ZipEntry): File { | |
val destFile = File(destinationDir, zipEntry.name) | |
val destDirPath = destinationDir.canonicalPath | |
val destFilePath = destFile.canonicalPath | |
if (!destFilePath.startsWith(destDirPath + File.separator)) { | |
throw IOException("Entry is outside of the target dir: " + zipEntry.name) | |
} | |
return destFile | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment