|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) SpongePowered <https://www.spongepowered.org> |
| 5 | + * Copyright (c) contributors |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in |
| 15 | + * all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | + * THE SOFTWARE. |
| 24 | + */ |
| 25 | +package org.spongepowered.obfuscation.merge.operation; |
| 26 | + |
| 27 | +import com.google.common.collect.HashMultimap; |
| 28 | +import com.google.common.collect.Multimap; |
| 29 | +import org.spongepowered.despector.ast.type.EnumEntry; |
| 30 | +import org.spongepowered.despector.ast.type.TypeEntry; |
| 31 | +import org.spongepowered.obfuscation.merge.MergeEngine; |
| 32 | +import org.spongepowered.obfuscation.merge.MergeOperation; |
| 33 | +import org.spongepowered.obfuscation.merge.data.MatchEntry; |
| 34 | + |
| 35 | +import java.util.ArrayList; |
| 36 | +import java.util.Collection; |
| 37 | +import java.util.List; |
| 38 | + |
| 39 | +public class MatchInnerClasses implements MergeOperation { |
| 40 | + |
| 41 | + private boolean prepared = false; |
| 42 | + |
| 43 | + private final Multimap<TypeEntry, TypeEntry> old_inners = HashMultimap.create(); |
| 44 | + private final Multimap<TypeEntry, TypeEntry> new_inners = HashMultimap.create(); |
| 45 | + |
| 46 | + private void prep(MergeEngine set) { |
| 47 | + for (TypeEntry type : set.getOldSourceSet().getAllClasses()) { |
| 48 | + if (!type.getName().contains("$")) { |
| 49 | + continue; |
| 50 | + } |
| 51 | + String parent_name = type.getName().substring(0, type.getName().lastIndexOf('$')); |
| 52 | + TypeEntry parent = set.getOldSourceSet().get(parent_name); |
| 53 | + if (parent == null) { |
| 54 | + throw new IllegalStateException(parent_name + " not found as parent type"); |
| 55 | + } |
| 56 | + this.old_inners.put(parent, type); |
| 57 | + } |
| 58 | + for (TypeEntry type : set.getNewSourceSet().getAllClasses()) { |
| 59 | + if (!type.getName().contains("$")) { |
| 60 | + continue; |
| 61 | + } |
| 62 | + String parent_name = type.getName().substring(0, type.getName().lastIndexOf('$')); |
| 63 | + TypeEntry parent = set.getNewSourceSet().get(parent_name); |
| 64 | + if (parent == null) { |
| 65 | + throw new IllegalStateException(parent_name + " not found as parent type"); |
| 66 | + } |
| 67 | + this.new_inners.put(parent, type); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void operate(MergeEngine set) { |
| 73 | + if (!this.prepared) { |
| 74 | + this.prepared = true; |
| 75 | + prep(set); |
| 76 | + } |
| 77 | + |
| 78 | + for (TypeEntry type : set.getOldSourceSet().getAllClasses()) { |
| 79 | + MatchEntry match = set.getMatch(type); |
| 80 | + if (match == null) { |
| 81 | + continue; |
| 82 | + } |
| 83 | + TypeEntry new_parent = match.getNewType(); |
| 84 | + Collection<TypeEntry> old_all_inners = this.old_inners.get(type); |
| 85 | + if (old_all_inners == null) { |
| 86 | + continue; |
| 87 | + } |
| 88 | + Collection<TypeEntry> new_all_inners = this.new_inners.get(new_parent); |
| 89 | + if (new_all_inners == null) { |
| 90 | + continue; |
| 91 | + } |
| 92 | + mergeDistinct(set, old_all_inners, new_all_inners); |
| 93 | + } |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + private void mergeDistinct(MergeEngine set, Collection<TypeEntry> old_inners, Collection<TypeEntry> new_inners) { |
| 98 | + List<TypeEntry> old_anon = new ArrayList<>(); |
| 99 | + List<TypeEntry> new_anon = new ArrayList<>(); |
| 100 | + List<TypeEntry> old_named = new ArrayList<>(); |
| 101 | + List<TypeEntry> new_named = new ArrayList<>(); |
| 102 | + |
| 103 | + for (TypeEntry type : old_inners) { |
| 104 | + if (type.isAnonType()) { |
| 105 | + old_anon.add(type); |
| 106 | + } else { |
| 107 | + old_named.add(type); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + for (TypeEntry type : new_inners) { |
| 112 | + if (type.isAnonType()) { |
| 113 | + new_anon.add(type); |
| 114 | + } else { |
| 115 | + new_named.add(type); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + if (old_anon.size() == 1 && new_anon.size() == 1) { |
| 120 | + set.vote(old_anon.get(0), new_anon.get(0)); |
| 121 | + } else { |
| 122 | + } |
| 123 | + |
| 124 | + if (old_named.size() == 1 && new_named.size() == 1) { |
| 125 | + set.vote(old_named.get(0), new_named.get(0)); |
| 126 | + } else { |
| 127 | + for (TypeEntry type : old_named) { |
| 128 | + if (set.isTypeMatched(type)) { |
| 129 | + continue; |
| 130 | + } |
| 131 | + if (type instanceof EnumEntry) { |
| 132 | + TypeEntry possible = null; |
| 133 | + int matches = 0; |
| 134 | + |
| 135 | + for (TypeEntry pos : new_named) { |
| 136 | + if (!(pos instanceof EnumEntry)) { |
| 137 | + continue; |
| 138 | + } |
| 139 | + EnumEntry pos_enum = (EnumEntry) pos; |
| 140 | + int m = 0; |
| 141 | + for (String cst : ((EnumEntry) type).getEnumConstants()) { |
| 142 | + if (pos_enum.getEnumConstants().contains(cst)) { |
| 143 | + m++; |
| 144 | + } |
| 145 | + } |
| 146 | + if (m > matches) { |
| 147 | + possible = pos; |
| 148 | + } else if (m == matches) { |
| 149 | + possible = null; |
| 150 | + } |
| 151 | + } |
| 152 | + if (possible != null) { |
| 153 | + set.vote(type, possible); |
| 154 | + } |
| 155 | + continue; |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments