|
| 1 | +/* |
| 2 | + * Copyright (c) Forge Development LLC and contributors |
| 3 | + * SPDX-License-Identifier: LGPL-2.1-only |
| 4 | + */ |
| 5 | +package net.minecraftforge.mcpcleanup; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Collections; |
| 9 | +import java.util.Comparator; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Locale; |
| 13 | +import java.util.regex.Matcher; |
| 14 | +import java.util.regex.Pattern; |
| 15 | + |
| 16 | +// FG 1.0/Python version of FmlCleanup |
| 17 | +// This has the issue of not renaming inner classes |
| 18 | +class FmlCleanup1 { |
| 19 | + private static final Pattern METHOD_REG = Pattern.compile("^ {4}(\\w+\\s+\\S.*\\(.*|static)$"); |
| 20 | + private static final Pattern CATCH_REG = Pattern.compile("catch \\((.*)\\)$"); |
| 21 | + private static final Pattern NESTED_PERINTH = Pattern.compile("\\(.*\\("); |
| 22 | + private static final Pattern METHOD_PARAMS = Pattern.compile("\\((.+)\\)"); |
| 23 | + private static final Pattern METHOD_DEC_END = Pattern.compile("(}|\\);|throws .+?;)$"); |
| 24 | + private static final Pattern METHOD_END = Pattern.compile("^ {4}\\}$"); |
| 25 | + private static final Pattern CAPS_START = Pattern.compile("^[A-Z]"); |
| 26 | + private static final Pattern ARRAY = Pattern.compile("(\\[|\\.\\.\\.)"); |
| 27 | + private static final Pattern VAR_CALL = Pattern.compile("(?i)[a-z_$][a-z0-9_\\[\\]]+ var\\d+"); |
| 28 | + private static final Pattern VAR = Pattern.compile("var\\d+"); |
| 29 | + |
| 30 | + private static final Comparator<String> COMPARATOR = (str1, str2) -> str2.length() - str1.length(); |
| 31 | + |
| 32 | + public static String cleanup(String text) { |
| 33 | + String[] lines = text.split("(\r\n|\r|\n)"); |
| 34 | + String output = ""; |
| 35 | + |
| 36 | + boolean insideMethod = false; |
| 37 | + String method = ""; |
| 38 | + ArrayList<String> methodVars = new ArrayList<String>(); |
| 39 | + boolean skip = false; |
| 40 | + |
| 41 | + for (String line : lines) { |
| 42 | + // if re.search(METHOD_REG, line) and not re.search('=', line) and not re.search(r'\(.*\(', line): |
| 43 | + if (METHOD_REG.matcher(line).find() && !line.contains("=") && !NESTED_PERINTH.matcher(line).find()) { |
| 44 | + // if re.search(r'\(.+\)', line): |
| 45 | + Matcher match = METHOD_PARAMS.matcher(line); |
| 46 | + if (match.find()) { |
| 47 | + // method_variables += [s.strip() for s in re.search(r'\((.+)\)', line).group(1).split(',')] |
| 48 | + String[] pts = match.group(1).split(","); |
| 49 | + for (String str : pts) { |
| 50 | + str = str.trim(); |
| 51 | + methodVars.add(str); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + method += line + '\n'; |
| 56 | + // method += line |
| 57 | + |
| 58 | + // single line method? |
| 59 | + skip = true; |
| 60 | + |
| 61 | + // if not re.search(r'(}|\);|throws .+?;)$', line): |
| 62 | + if (!METHOD_DEC_END.matcher(line).find()) |
| 63 | + insideMethod = true; |
| 64 | + //elif re.search(r'^ {%s}}$' % indent, line): |
| 65 | + } else if (METHOD_END.matcher(line).find()) { |
| 66 | + //inside_method = False |
| 67 | + insideMethod = false; |
| 68 | + } |
| 69 | + |
| 70 | + // inside method actions now. |
| 71 | + if (insideMethod) { |
| 72 | + if (skip) { |
| 73 | + skip = false; |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + method += line + '\n'; |
| 78 | + |
| 79 | + Matcher matcher = CATCH_REG.matcher(line); |
| 80 | + if (matcher.find()) |
| 81 | + methodVars.add(matcher.group(1)); |
| 82 | + else { |
| 83 | + Matcher match = VAR_CALL.matcher(line); |
| 84 | + while (match.find()) { |
| 85 | + if (!match.group().startsWith("return") && !match.group().startsWith("throw")) |
| 86 | + methodVars.add(match.group()); |
| 87 | + } |
| 88 | + } |
| 89 | + } else { |
| 90 | + if (method != null && !method.isEmpty()){ |
| 91 | + FmlCleanup1 namer = new FmlCleanup1(); |
| 92 | + HashMap<String, String> todo = new HashMap<String, String>(); |
| 93 | + |
| 94 | + for (String var : methodVars) { |
| 95 | + String[] split = var.split(" "); |
| 96 | + if (split.length > 1) |
| 97 | + todo.put(split[1], namer.getName(split[0], split[1])); |
| 98 | + else |
| 99 | + System.out.printf("Unknown thing : %s (%s)\n", var, method); |
| 100 | + } |
| 101 | + |
| 102 | + List<String> sortedKeys = new ArrayList<String>(todo.keySet()); |
| 103 | + Collections.sort(sortedKeys, COMPARATOR); |
| 104 | + |
| 105 | + // closure changes the sort, to sort by the return value of the closure. |
| 106 | + for (String key : sortedKeys) { |
| 107 | + if (VAR.matcher(key).matches()) |
| 108 | + method = method.replace(key, todo.get(key)); |
| 109 | + } |
| 110 | + |
| 111 | + output += method; |
| 112 | + |
| 113 | + // clear methods. |
| 114 | + methodVars.clear(); |
| 115 | + method = ""; |
| 116 | + } |
| 117 | + |
| 118 | + if (skip) { |
| 119 | + skip = false; |
| 120 | + continue; |
| 121 | + } |
| 122 | + |
| 123 | + output += line + '\n'; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return output; |
| 128 | + } |
| 129 | + |
| 130 | + private final HashMap<String, Holder> last = new HashMap<String, Holder>(); |
| 131 | + private final HashMap<String, String> remap = new HashMap<String, String>(); |
| 132 | + |
| 133 | + private FmlCleanup1() { |
| 134 | + last.put("byte", new Holder(0, false, "b")); |
| 135 | + last.put("char", new Holder(0, false, "c")); |
| 136 | + last.put("short", new Holder(1, false, "short")); |
| 137 | + last.put("int", new Holder(0, true, "i", "j", "k", "l")); |
| 138 | + last.put("boolean", new Holder(0, true, "flag")); |
| 139 | + last.put("double", new Holder(0, false, "d")); |
| 140 | + last.put("float", new Holder(0, true, "f")); |
| 141 | + last.put("File", new Holder(1, true, "file")); |
| 142 | + last.put("String", new Holder(0, true, "s")); |
| 143 | + last.put("Class", new Holder(0, true, "oclass")); |
| 144 | + last.put("Long", new Holder(0, true, "olong")); |
| 145 | + last.put("Byte", new Holder(0, true, "obyte")); |
| 146 | + last.put("Short", new Holder(0, true, "oshort")); |
| 147 | + last.put("Boolean", new Holder(0, true, "obool")); |
| 148 | + last.put("Package", new Holder(0, true, "opackage")); |
| 149 | + remap.put("long", "int"); |
| 150 | + } |
| 151 | + |
| 152 | + private String getName(String type, String var) { |
| 153 | + String index = null; |
| 154 | + if (last.containsKey(type)) |
| 155 | + index = type; |
| 156 | + else if (remap.containsKey(type)) |
| 157 | + index = remap.get(type); |
| 158 | + |
| 159 | + if ((index == null || index.isEmpty()) && (CAPS_START.matcher(type).find() || ARRAY.matcher(type).find())) { |
| 160 | + // replace multi things with arrays. |
| 161 | + type = type.replace("...", "[]"); |
| 162 | + |
| 163 | + while (type.contains("[][]")) |
| 164 | + type = type.replaceAll("\\[\\]\\[\\]", "[]"); |
| 165 | + |
| 166 | + String name = lower(type); |
| 167 | + boolean skip_zero = true; |
| 168 | + |
| 169 | + //if (Pattern.compile("\\[").matcher(type).find()) { |
| 170 | + if (type.indexOf('[') != -1) { |
| 171 | + skip_zero = true; |
| 172 | + name = "a" + name; |
| 173 | + name = name.replace("[]", "").replace("...", ""); |
| 174 | + } |
| 175 | + |
| 176 | + last.put(type, new Holder(0, skip_zero, name)); |
| 177 | + index = type; |
| 178 | + } |
| 179 | + |
| 180 | + if (index == null || index.isEmpty()) { |
| 181 | + //Debug: System.out.println("NO DATA FOR TYPE " + type + " " + var); |
| 182 | + return lower(type); |
| 183 | + } |
| 184 | + |
| 185 | + Holder holder = last.get(index); |
| 186 | + int id = holder.id; |
| 187 | + List<String> data = holder.data; |
| 188 | + |
| 189 | + int ammount = data.size(); |
| 190 | + |
| 191 | + String name; |
| 192 | + if (ammount == 1) { |
| 193 | + name = data.get(0) + (id == 0 && holder.skip_zero ? "" : id); |
| 194 | + } else { |
| 195 | + int num = id / ammount; |
| 196 | + name = data.get(id % ammount) + (id < ammount && holder.skip_zero ? "" : num); |
| 197 | + } |
| 198 | + |
| 199 | + holder.id++; |
| 200 | + return name; |
| 201 | + } |
| 202 | + |
| 203 | + private static String lower(String string) { |
| 204 | + return string.toLowerCase(Locale.ENGLISH); |
| 205 | + } |
| 206 | + |
| 207 | + private class Holder { |
| 208 | + int id; |
| 209 | + final public boolean skip_zero; |
| 210 | + final ArrayList<String> data = new ArrayList<String>(); |
| 211 | + |
| 212 | + public Holder(int t1, boolean skip_zero, String... stuff) { |
| 213 | + this.id = t1; |
| 214 | + this.skip_zero = skip_zero; |
| 215 | + Collections.addAll(this.data, stuff); |
| 216 | + } |
| 217 | + } |
| 218 | +} |
0 commit comments