diff --git a/build.gradle b/build.gradle deleted file mode 100644 index 2caf8d8..0000000 --- a/build.gradle +++ /dev/null @@ -1,35 +0,0 @@ -apply plugin: 'kotlin' - -group = 'com.octarine' -version = '0.6' - -repositories { mavenCentral() } - -buildscript { - ext.kotlin_version = '1.0.0' - repositories { - mavenCentral() - } - dependencies { - classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" - } -} - -dependencies { - compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" - compile 'org.ow2.asm:asm-debug-all:5.0.4' - compile 'net.sf.jopt-simple:jopt-simple:4.9' - compile 'com.nothome:javaxdelta:2.0.1' -} - -jar { - manifest { - attributes "Main-Class": "com.octarine.simpledeobf.Main" - } - - configurations.compile.each { dep -> - from(project.zipTree(dep)){ - exclude 'META-INF', 'META-INF/**' - } - } -} \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts new file mode 100644 index 0000000..3085c5d --- /dev/null +++ b/build.gradle.kts @@ -0,0 +1,63 @@ +plugins { + idea + kotlin("jvm") version "1.7.20" +} + +base.archivesName.set("Simpledeobf") +version = "0.7" +group = "com.octarine" + +sourceSets.main { + java.srcDirs("src/main/kotlin") +} + +repositories { + mavenCentral() +} + +val shade: Configuration by configurations.creating + +dependencies { + shade("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.21") + shade("org.ow2.asm:asm-debug-all:6.0_BETA") + shade("net.sf.jopt-simple:jopt-simple:6.0-alpha-3") + shade("com.nothome:javaxdelta:2.0.1") + implementation(shade) +} + +tasks { + compileKotlin { + kotlinOptions { + jvmTarget = "1.8" + freeCompilerArgs = listOf( + "-Xinline-classes", + "-Xopt-in=kotlin.contracts.ExperimentalContracts", + "-Xlambdas=indy", + "-Xjvm-default=all", + ) + } + } + jar { + duplicatesStrategy = DuplicatesStrategy.EXCLUDE + manifest { + attributes( + "Main-Class" to "com.octarine.simpledeobf.Main" + ) + } + from( + shade.map { + if (it.isDirectory) it + else zipTree(it) + } + ) + exclude( + "org/intellij/**", + "org/jetbrains/**", + "gnu/**", + "module-info.class", + "META-INF/maven/**", + "META-INF/versions/**", + "META-INF/*.kotlin_module" + ) + } +} diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..dfac811 --- /dev/null +++ b/gradle.properties @@ -0,0 +1,6 @@ +# Sets default memory used for gradle commands. Can be overridden by user or command line properties. +org.gradle.jvmargs=-Xmx1G +org.gradle.daemon=true +org.gradle.caching=true +org.gradle.vfs.watch=true +org.gradle.unsafe.configuration-cache=true diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..5ae304f --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Wed Jul 02 15:54:47 CDT 2014 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..91a7e26 --- /dev/null +++ b/gradlew @@ -0,0 +1,164 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; +esac + +# For Cygwin, ensure paths are in UNIX format before anything is touched. +if $cygwin ; then + [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` +fi + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >&- +APP_HOME="`pwd -P`" +cd "$SAVED" >&- + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules +function splitJvmOpts() { + JVM_OPTS=("$@") +} +eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS +JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" + +exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..8a0b282 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,90 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windowz variants + +if not "%OS%" == "Windows_NT" goto win9xME_args +if "%@eval[2+2]" == "4" goto 4NT_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* +goto execute + +:4NT_args +@rem Get arguments from the 4NT Shell from JP Software +set CMD_LINE_ARGS=%$ + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle b/settings.gradle deleted file mode 100644 index b48d7a0..0000000 --- a/settings.gradle +++ /dev/null @@ -1,2 +0,0 @@ -rootProject.name = 'simpledeobf' - diff --git a/settings.gradle.kts b/settings.gradle.kts new file mode 100644 index 0000000..0a3d88c --- /dev/null +++ b/settings.gradle.kts @@ -0,0 +1 @@ +rootProject.name = "simpledeobf" \ No newline at end of file diff --git a/src/main/kotlin/com/octarine/simpledeobf/Main.kt b/src/main/kotlin/com/octarine/simpledeobf/Main.kt index 71b4fe5..4dfda8a 100644 --- a/src/main/kotlin/com/octarine/simpledeobf/Main.kt +++ b/src/main/kotlin/com/octarine/simpledeobf/Main.kt @@ -2,19 +2,26 @@ package com.octarine.simpledeobf import com.nothome.delta.GDiffPatcher + import com.sun.xml.internal.messaging.saaj.util.ByteInputStream + import joptsimple.OptionException import joptsimple.OptionParser + import org.objectweb.asm.ClassReader import org.objectweb.asm.ClassWriter import org.objectweb.asm.tree.ClassNode + import java.io.File import java.io.FileNotFoundException import java.io.FileOutputStream + import java.util.zip.ZipEntry import java.util.zip.ZipFile import java.util.zip.ZipOutputStream +import kotlin.system.exitProcess + fun main(args : Array) { try { @@ -34,27 +41,27 @@ fun main(args : Array) { if (options.has(help)) { parser.printHelpOn(System.out) - System.exit(0) + exitProcess(0) } if (options.valuesOf(outputFile).size != 1) { println("Maximum of 1 output file is allowed") - System.exit(1) + exitProcess(1) } if (options.valuesOf(defaultPkg).size > 1) { println("Maximum of 1 default package is allowed") - System.exit(1) + exitProcess(1) } if (options.has(xdeltaPrefix) != options.has(xdeltaPostfix)) { println("xdeltaPrefix and xdeltaPostfix need to be defined together") - System.exit(1) + exitProcess(1) } if (options.valuesOf(xdeltaPrefix).size > 1 || options.valuesOf(xdeltaPostfix).size > 1) { println("Only 1 xpatch prefix and postfix is allowed") - System.exit(1) + exitProcess(1) } val doPatchProcessing = options.valuesOf(xdeltaPrefix).size == 1 @@ -100,16 +107,17 @@ fun main(args : Array) { srcName.endsWith(options.valueOf(xdeltaPostfix)!!) && srcName.startsWith(options.valueOf(xdeltaPrefix)!!)) { // patch file - try to patch a matching file from a reference JAR - print(" patching: ${srcName}") + print(" patching: $srcName") val patchBytes = srcBytes + 0 // make sure there's an EOF marker val origName = srcEntry.name.subSequence( options.valueOf(xdeltaPrefix)!!.length, srcEntry.name.length - options.valueOf(xdeltaPostfix)!!.length ) as String - val origBytes = (options.valuesOf(inputFile) + options.valuesOf(referenceFile)).map { - val refFile = ZipFile(it) - refFile.getEntry(origName)?.let { refFile.getInputStream(it).readBytes() } - }.filterNotNull().firstOrNull() + val origBytes = + (options.valuesOf(inputFile) + options.valuesOf(referenceFile)).firstNotNullOfOrNull { it -> + val refFile = ZipFile(it) + refFile.getEntry(origName)?.let { refFile.getInputStream(it).readBytes() } + } if (origBytes != null) { srcName = origName srcBytes = GDiffPatcher().patch(origBytes, patchBytes) @@ -119,10 +127,10 @@ fun main(args : Array) { } } if (srcName.endsWith(".class")) { - print(" processing: ${srcName} ") + print(" processing: $srcName ") writeDeobfedClass(srcBytes, srcEntry.name) } else { - println(" copying: ${srcName}") + println(" copying: $srcName") destJar.putNextEntry(ZipEntry(srcName)) ByteInputStream(srcBytes, srcBytes.size).copyTo(destJar) destJar.closeEntry() @@ -134,9 +142,9 @@ fun main(args : Array) { destJar.close() } catch(e: OptionException) { println(e.message) - System.exit(1) + exitProcess(1) } catch(e: FileNotFoundException) { println(e.message) - System.exit(1) + exitProcess(1) } } \ No newline at end of file diff --git a/src/main/kotlin/com/octarine/simpledeobf/Remapper.kt b/src/main/kotlin/com/octarine/simpledeobf/Remapper.kt index 17b2f65..152545f 100644 --- a/src/main/kotlin/com/octarine/simpledeobf/Remapper.kt +++ b/src/main/kotlin/com/octarine/simpledeobf/Remapper.kt @@ -4,22 +4,22 @@ import org.objectweb.asm.* import org.objectweb.asm.commons.Remapper import org.objectweb.asm.commons.RemappingClassAdapter import org.objectweb.asm.tree.ClassNode + import java.io.File -import java.util.* import java.util.zip.ZipFile -class SimpleRemapper(val defaultPkg: String?) : Remapper() { +class SimpleRemapper(private val defaultPkg: String?) : Remapper() { - val String.partOwner: String get() = this.substring(0, this.lastIndexOf("/")) - val String.partName: String get() = this.substring(this.lastIndexOf("/") + 1) + private val String.partOwner: String get() = this.substring(0, this.lastIndexOf("/")) + private val String.partName: String get() = this.substring(this.lastIndexOf("/") + 1) - val mappings = HashMap() + private val mappings = HashMap() val hierarchy = HashMap() val hierarchyReader = SimpleHierarchyReader() inner class SimpleHierarchyReader : ClassVisitor(Opcodes.ASM5) { override fun visit(version: Int, access: Int, name: String, signature: String?, superName: String?, interfaces: Array?) { - hierarchy.put(name, ClassHierarchy(superName, interfaces)) + hierarchy[name] = ClassHierarchy(superName, interfaces) } fun visitAllFromFile(file: File) { @@ -49,19 +49,19 @@ class SimpleRemapper(val defaultPkg: String?) : Remapper() { if (tokens[0] == "MD:") addMethodMapping(tokens[1].partOwner, tokens[1].partName, tokens[2], tokens[3].partOwner, tokens[3].partName, tokens[4]) } - fun addClassMapping(fromName: String, toName: String) { - mappings.put(fromName, ClassMapping(toName)) + private fun addClassMapping(fromName: String, toName: String) { + mappings[fromName] = ClassMapping(toName) } - fun addFieldMapping(fromOwner: String, fromName: String, toOwner: String, toName: String) { + private fun addFieldMapping(fromOwner: String, fromName: String, toOwner: String, toName: String) { mappings[fromOwner]?.fields?.put(fromName, toName) } - fun addMethodMapping(fromOwner: String, fromName: String, fromDesc: String, toOwner: String, toName: String, toDesc: String) { + private fun addMethodMapping(fromOwner: String, fromName: String, fromDesc: String, toOwner: String, toName: String, toDesc: String) { mappings[fromOwner]?.methods?.put(fromName to fromDesc, toName) } - override fun map(typeName: String): String? = (mappings[typeName]?.mappedName ?: typeName).let { + override fun map(typeName: String): String = (mappings[typeName]?.mappedName ?: typeName).let { if (it.contains("/") || defaultPkg == null) it else "$defaultPkg/$it" } @@ -74,7 +74,7 @@ class SimpleRemapper(val defaultPkg: String?) : Remapper() { override fun mapMethodName(owner: String, name: String, desc: String) = mapMethodNameInternal(owner, name, desc) ?: name - fun mapMethodNameInternal(owner: String, name: String, desc: String): String? { + private fun mapMethodNameInternal(owner: String, name: String, desc: String): String? { mappings[owner]?.let { it.methods[name to desc] }?.let { return it } val h = hierarchy[owner] ?: return null if (h.superName != null) mapMethodNameInternal(h.superName, name, desc)?.let { return it } @@ -85,9 +85,9 @@ class SimpleRemapper(val defaultPkg: String?) : Remapper() { } } -class PublicAccessRemappingClassAdapter(cv: ClassVisitor, remapper: Remapper, val force: Boolean) : RemappingClassAdapter(cv, remapper) { +class PublicAccessRemappingClassAdapter(cv: ClassVisitor, remapper: Remapper, private val force: Boolean) : RemappingClassAdapter(cv, remapper) { - val Int.toPublic: Int get() = (this and 0xFFF8) or 0x1 + private val Int.toPublic: Int get() = (this and 0xFFF8) or 0x1 override fun visitMethod(access: Int, name: String?, desc: String?, signature: String?, exceptions: Array?): MethodVisitor? { return super.visitMethod(if (force) access.toPublic else access, name, desc, signature, exceptions) @@ -107,4 +107,4 @@ class ClassMapping(val mappedName: String) { val methods = HashMap, String>() } -class ClassHierarchy(val superName: String?, val interfaces: Array?) {} \ No newline at end of file +class ClassHierarchy(val superName: String?, val interfaces: Array?) \ No newline at end of file