Advertisement
SforzandoCF

obfuscation mapper

Sep 6th, 2024
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.26 KB | None | 0 0
  1. package lx01.projects.obmap;
  2.  
  3. import java.io.File;
  4. import java.nio.file.Files;
  5. import java.util.List;
  6.  
  7. import static lx01.projects.jd.JavaDecompiler.decompile;
  8.  
  9. public class ObfuscationMapper {
  10.     public static void main (String[] args) {
  11.         if (args.length < 4) throw new IllegalArgumentException ("Usage: \"java -jar [jar file] [file to be mapped] [file with mappings] [output file] [decompile (true or false)]\"");
  12.         File sourceFile = new File(args[0]);
  13.         File mappingsFile = new File(args[1]);
  14.         File out = new File(args[2]);
  15.         boolean decompile = args[3] == true;
  16.         String source = decompile ? decompile(source) : concat(Files.readAllLines(source.toPath()));
  17.         Mapping.Set mappings = readMappings(Files.readAllLines(mappingsFile.toPath()));
  18.         Mapper.map(source, mappings, out);
  19.     }
  20.    
  21.     private static String concat (List<String> strings) {
  22.         String s = "";
  23.         strings.forEach((s2) -> { s = s.concat(s2); });
  24.         return s;
  25.     }
  26.    
  27.     private static Mapping.Set readMappings (List<String> strings) {
  28.         Mapping.Set mappings = new Mapping.Set();
  29.         strings.forEach((s) -> { mappings.put(readMapping(s)); });
  30.         return mappings;
  31.     }
  32.    
  33.     private static Mapping readMapping (String mapping) {
  34.         String[] m = mapping.split("->");
  35.         return new Mapping.Builder().setTarget(m[0]).setResult(m[1]).build();
  36.     }
  37. }
  38.  
  39. //
  40.  
  41. package lx01.projects.obmap;
  42.  
  43. import java.util.Set;
  44.  
  45. public class Mapping {
  46.     private String target;
  47.     private String output;
  48.    
  49.     public Mapping () {}
  50.    
  51.     public void setTarget (String nt) {
  52.         this.target = nt;
  53.     }
  54.    
  55.     public String getTarget () {
  56.         return this.target;
  57.     }
  58.    
  59.     public void setOutput (String no) {
  60.         this.output = no;
  61.     }
  62.    
  63.     public String getOutput () {
  64.         return this.output;
  65.     }
  66.    
  67.     public static class Builder {
  68.         private Mapping m = new Mapping();
  69.        
  70.         public Builder () {};
  71.        
  72.         public Builder setTarget (String nt) {
  73.             this.m.setTarget(nt);
  74.             return this;
  75.         }
  76.        
  77.         public Builder setOutput (String no) {
  78.             this.m.setOutput(no);
  79.             return this;
  80.         }
  81.     }
  82.    
  83.     public static class Set {
  84.         private Set<Mapping> mappings = Set.<Mapping>of();
  85.        
  86.         public Set () {}
  87.        
  88.         public void put (Mapping m) {
  89.             this.mappings.put(m);
  90.         }
  91.        
  92.         public boolean applies (String s) {
  93.             for (Mapping m : this.mappings)
  94.                 if (s.contains(m.getTarget()))
  95.                     return true;
  96.             return false;
  97.         }
  98.        
  99.         public String applyAll (String s) {
  100.             for (Mapping m : this.mappings)
  101.                 if (s.contains(m.getTarget()))
  102.                     s = s.replaceAll(m.getTarget(), m.getOutput());
  103.             return s;
  104.         }
  105.     }
  106. }
  107.  
  108. //
  109.  
  110. package lx01.projects.obmap;
  111.  
  112. public class Mapper {
  113.     public static void map (String source, Mapping.Set mappings, File out) {
  114.         try {
  115.             new FileWriter(out).write(mappings.applyAll(s));
  116.         } catch (Exception e) {
  117.             return;
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement