|
| 1 | +package de.oceanlabs.mcp.mcinjector.data; |
| 2 | + |
| 3 | +import de.oceanlabs.mcp.mcinjector.MCInjector; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.nio.charset.StandardCharsets; |
| 7 | +import java.nio.file.Files; |
| 8 | +import java.nio.file.Path; |
| 9 | +import java.nio.file.StandardOpenOption; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.stream.Collectors; |
| 14 | + |
| 15 | +public enum Parameters |
| 16 | +{ |
| 17 | + INSTANCE; |
| 18 | + |
| 19 | + private Map<String, String> fromDesc = new HashMap<>(); |
| 20 | + private Map<String, String> fromID = new HashMap<>(); |
| 21 | + private int maxID = 0; |
| 22 | + |
| 23 | + public boolean load(Path file) |
| 24 | + { |
| 25 | + this.fromDesc.clear(); |
| 26 | + this.fromID.clear(); |
| 27 | + try |
| 28 | + { |
| 29 | + MCInjector.LOG.fine("Loading Parameters from: " + file); |
| 30 | + Files.readAllLines(file).forEach(line -> |
| 31 | + { |
| 32 | + line = line.trim(); |
| 33 | + if (line.isEmpty() || line.startsWith("#")) |
| 34 | + return; |
| 35 | + |
| 36 | + String[] parts = line.split(" " ); |
| 37 | + int id = Integer.parseInt(parts[0].substring(1)); |
| 38 | + char prefix = parts[0].charAt(0); |
| 39 | + MCInjector.LOG.fine(" Method parameter ID loaded " + prefix + id + " " + parts[0] + " " + parts[1] + " " + parts[3]); |
| 40 | + this.setName(parts[1], parts[2], parts[3], id, prefix); |
| 41 | + }); |
| 42 | + } |
| 43 | + catch (IOException e) |
| 44 | + { |
| 45 | + e.printStackTrace(); |
| 46 | + MCInjector.LOG.warning("Could not load Parameters list: " + e.toString()); |
| 47 | + return false; |
| 48 | + } |
| 49 | + return true; |
| 50 | + } |
| 51 | + |
| 52 | + public boolean loadLegacy(Path file) |
| 53 | + { |
| 54 | + try { |
| 55 | + MCInjector.LOG.fine("Loading Constructors from: " + file); |
| 56 | + Files.readAllLines(file).forEach(line -> |
| 57 | + { |
| 58 | + line = line.trim(); |
| 59 | + if (line.isEmpty() || line.startsWith("#")) |
| 60 | + return; |
| 61 | + |
| 62 | + String[] parts = line.split(" " ); |
| 63 | + int id = Integer.parseInt(parts[0]); |
| 64 | + MCInjector.LOG.fine(" Constructor ID loaded " + id + " " + parts[0] + " " + parts[1]); |
| 65 | + this.setName(parts[1], "<init>", parts[2], id, 'i'); |
| 66 | + }); |
| 67 | + return true; |
| 68 | + } |
| 69 | + catch (IOException e) |
| 70 | + { |
| 71 | + e.printStackTrace(); |
| 72 | + MCInjector.LOG.warning("Could not import Constructors list: " + e.toString()); |
| 73 | + return false; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public boolean dump(Path file) |
| 78 | + { |
| 79 | + try |
| 80 | + { |
| 81 | + List<String> ret = this.fromID.entrySet().stream() |
| 82 | + .sorted((e1, e2) -> e1.getKey().compareTo(e2.getKey())) |
| 83 | + .map(e -> e.getKey() + " " + e.getValue()) |
| 84 | + .collect(Collectors.toList()); |
| 85 | + Files.write(file, String.join("\n", ret).getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE_NEW); |
| 86 | + } |
| 87 | + catch (IOException e) |
| 88 | + { |
| 89 | + e.printStackTrace(); |
| 90 | + MCInjector.LOG.warning("Could not dump Parameters list: " + e.toString()); |
| 91 | + return false; |
| 92 | + } |
| 93 | + return true; |
| 94 | + } |
| 95 | + |
| 96 | + public void setName(String cls, String method, String desc, int id, char prefix) |
| 97 | + { |
| 98 | + if (id < 0) |
| 99 | + throw new IllegalArgumentException("ID must be positive: " + id); |
| 100 | + this.maxID = Math.max(this.maxID, id); |
| 101 | + this.fromDesc.put(cls + " " + method + " " + desc, prefix + "" + id); |
| 102 | + this.fromID .put(prefix + "" + id, cls + " " + method + " " + desc); |
| 103 | + } |
| 104 | + |
| 105 | + public String getName(String cls, String method, String desc, boolean generate, boolean isStatic) |
| 106 | + { |
| 107 | + String id = this.fromDesc.get(cls + " " + method + " " + desc); |
| 108 | + if (id == null) |
| 109 | + { |
| 110 | + if (!generate) |
| 111 | + return method; //if we are not generating new names we will return the old parameter format, _p_funcname_x_ |
| 112 | + char prefix = getPrefix(method, isStatic); |
| 113 | + int newId = ++maxID; |
| 114 | + this.setName(cls, method, desc, newId, prefix); |
| 115 | + return prefix + "" + newId; |
| 116 | + } |
| 117 | + if(id.charAt(0) != getPrefix(method, isStatic)) |
| 118 | + { |
| 119 | + String num = id.substring(1); |
| 120 | + char prefix = getPrefix(method, isStatic); |
| 121 | + this.setName(cls, method, desc, Integer.parseInt(num), prefix); |
| 122 | + String newID = prefix + "" + num; |
| 123 | + MCInjector.LOG.info("Method " + cls + "." + method + desc + " has wrong name attribute, updating: " + id + " -> " + newID); |
| 124 | + return newID; |
| 125 | + } |
| 126 | + return id; |
| 127 | + } |
| 128 | + |
| 129 | + public char getPrefix(String name, boolean isStatic){ |
| 130 | + if(name.equals("<init>")) //this ensures constructors will still have the _p_i12345_x_ format |
| 131 | + return 'i'; |
| 132 | + if(isStatic) //since the static map does not cover these methods we encode it in the param prefix (s=static) |
| 133 | + return 's'; |
| 134 | + return 'e'; //while new methods named will use the _p_e12345_x_ format |
| 135 | + } |
| 136 | +} |
0 commit comments