-
-
Notifications
You must be signed in to change notification settings - Fork 360
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ability to relocate/shade in assembly
- Loading branch information
Showing
4 changed files
with
154 additions
and
69 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package com.eed3si9n.jarjarabrams | ||
|
||
import java.io.{ByteArrayInputStream, InputStream} | ||
import java.nio.file.{ Files, Path, StandardOpenOption } | ||
import org.pantsbuild.jarjar.{ JJProcessor, _ } | ||
import org.pantsbuild.jarjar.util.EntryStruct | ||
import com.eed3si9n.jarjarabrams.Utils.readAllBytes | ||
|
||
object Shader { | ||
def shadeDirectory( | ||
rules: Seq[ShadeRule], | ||
dir: Path, | ||
mappings: Iterable[(Path, String)], | ||
verbose: Boolean | ||
): Unit = { | ||
val inputStreams = mappings.filter(x => !Files.isDirectory(x._1)).map(x => Files.newInputStream(x._1) -> x._2) | ||
val newMappings = shadeInputStreams(rules, inputStreams, verbose) | ||
mappings.filterNot(_._1.toFile.isDirectory).foreach(f => Files.delete(f._1)) | ||
newMappings.foreach { case (inputStream, mapping) => | ||
val out = dir.resolve(mapping) | ||
if (!Files.exists(out.getParent)) Files.createDirectories(out.getParent) | ||
Files.write(out, readAllBytes(inputStream), StandardOpenOption.CREATE) | ||
} | ||
} | ||
|
||
def shadeInputStreams( | ||
rules: Seq[ShadeRule], | ||
mappings: Iterable[(InputStream, String)], | ||
verbose: Boolean | ||
): Iterable[(InputStream, String)] = { | ||
val shader = inputStreamShader(rules, verbose) | ||
mappings.flatMap(f => shader(f._1, f._2)) | ||
} | ||
|
||
def inputStreamShader( | ||
rules: Seq[ShadeRule], | ||
verbose: Boolean | ||
): (InputStream, String) => Option[(InputStream, String)] = { | ||
val jjrules = rules.flatMap { r => | ||
r.shadePattern match { | ||
case ShadePattern.Rename(patterns) => | ||
patterns.map { case (from, to) => | ||
val jrule = new Rule() | ||
jrule.setPattern(from) | ||
jrule.setResult(to) | ||
jrule | ||
} | ||
case ShadePattern.Zap(patterns) => | ||
patterns.map { pattern => | ||
val jrule = new Zap() | ||
jrule.setPattern(pattern) | ||
jrule | ||
} | ||
case ShadePattern.Keep(patterns) => | ||
patterns.map { pattern => | ||
val jrule = new Keep() | ||
jrule.setPattern(pattern) | ||
jrule | ||
} | ||
case _ => Nil | ||
} | ||
} | ||
|
||
val proc = new JJProcessor(jjrules, verbose, true, null) | ||
|
||
{ (inputStream, mapping) => | ||
/* | ||
jarjar MisplacedClassProcessor class transforms byte[] to a class using org.objectweb.asm.ClassReader.getClassName | ||
which always translates class names containing '.' into '/', regardless of OS platform. | ||
We need to transform any windows file paths in order for jarjar to match them properly and not omit them. | ||
*/ | ||
val sanitizedMapping = if (mapping.contains('\\')) mapping.replace('\\', '/') else mapping | ||
val entry = new EntryStruct | ||
entry.data = readAllBytes(inputStream) | ||
entry.name = sanitizedMapping | ||
entry.time = -1 | ||
entry.skipTransform = false | ||
val shadedInputStream = | ||
if (proc.process(entry)) Some(new ByteArrayInputStream(entry.data) { | ||
override def close(): Unit = inputStream.close() | ||
} -> entry.name) | ||
else None | ||
val excludes = proc.getExcludes | ||
shadedInputStream.filterNot(a => excludes.contains(a._2)) | ||
} | ||
} | ||
} | ||
|
||
sealed trait ShadePattern { | ||
def inAll: ShadeRule = ShadeRule(this, Vector(ShadeTarget.inAll)) | ||
def inProject: ShadeRule = ShadeRule(this, Vector(ShadeTarget.inProject)) | ||
def inModuleCoordinates(moduleId: ModuleCoordinate*): ShadeRule = | ||
ShadeRule(this, moduleId.toVector map ShadeTarget.inModuleCoordinate) | ||
} | ||
|
||
object ShadePattern { | ||
case class Rename(patterns: List[(String, String)]) extends ShadePattern | ||
case class Zap(patterns: List[String]) extends ShadePattern | ||
case class Keep(patterns: List[String]) extends ShadePattern | ||
} |