|
26 | 26 | import daomephsta.unpick.api.classresolvers.ClassResolvers; |
27 | 27 | import daomephsta.unpick.api.classresolvers.IClassResolver; |
28 | 28 | import daomephsta.unpick.api.constantgroupers.ConstantGroupers; |
| 29 | +import daomephsta.unpick.constantmappers.datadriven.parser.v3.UnpickV3Reader; |
| 30 | +import daomephsta.unpick.constantmappers.datadriven.tree.ForwardingUnpickV3Visitor; |
| 31 | +import daomephsta.unpick.constantmappers.datadriven.tree.GroupDefinition; |
| 32 | +import daomephsta.unpick.constantmappers.datadriven.tree.expr.Expression; |
| 33 | +import daomephsta.unpick.constantmappers.datadriven.tree.expr.FieldExpression; |
29 | 34 | import dev.denwav.hypo.asm.AsmClassData; |
30 | 35 | import dev.denwav.hypo.core.HypoContext; |
31 | 36 | import dev.denwav.hypo.model.data.ClassData; |
@@ -82,21 +87,20 @@ public void exec() { |
82 | 87 | if (isZip) { |
83 | 88 | try (final FileSystem definitionsFs = FileSystems.newFileSystem(this.unpickDefinitions)) { |
84 | 89 | final Path definitionsPath = definitionsFs.getPath("extras/definitions.unpick"); |
85 | | - this.unpick(definitionsPath, zips, this.context); |
| 90 | + this.unpick(definitionsPath, zips); |
86 | 91 | } catch (final IOException e) { |
87 | 92 | throw new UncheckedIOException(e); |
88 | 93 | } |
89 | 94 | } else { |
90 | 95 | try { |
91 | | - this.unpick(this.unpickDefinitions, zips, this.context); |
| 96 | + this.unpick(this.unpickDefinitions, zips); |
92 | 97 | } catch (final IOException e) { |
93 | 98 | throw new UncheckedIOException(e); |
94 | 99 | } |
95 | 100 | } |
96 | 101 | } |
97 | 102 |
|
98 | | - private void unpick(final Path definitionsPath, final List<ZipFile> zips, final HypoContext context) |
99 | | - throws IOException { |
| 103 | + private void unpick(final Path definitionsPath, final List<ZipFile> zips) throws IOException { |
100 | 104 | IClassResolver classResolver = new IClassResolver() { |
101 | 105 | @Override |
102 | 106 | public @Nullable ClassReader resolveClass(final String internalName) { |
@@ -128,7 +132,45 @@ private void unpick(final Path definitionsPath, final List<ZipFile> zips, final |
128 | 132 | this.uninliner = ConstantUninliner.builder() |
129 | 133 | .grouper(ConstantGroupers.dataDriven() |
130 | 134 | .classResolver(classResolver) |
131 | | - .mappingSource(definitionsReader) |
| 135 | + .mappingSource(visitor -> { |
| 136 | + try { |
| 137 | + new UnpickV3Reader(definitionsReader) |
| 138 | + .accept(new ForwardingUnpickV3Visitor(visitor) { |
| 139 | + // Filter out any groups where all constants reference missing classes |
| 140 | + // (client classes when applying to the server) |
| 141 | + // This may need further refinement to handle applying outdated definitions leniently |
| 142 | + @Override |
| 143 | + public void visitGroupDefinition( |
| 144 | + final GroupDefinition groupDefinition) { |
| 145 | + final List<Expression> constants = |
| 146 | + new ArrayList<>(groupDefinition.constants()); |
| 147 | + for (final Expression constant : groupDefinition.constants()) { |
| 148 | + if (constant instanceof final FieldExpression field) { |
| 149 | + try { |
| 150 | + final @Nullable ClassData clsData = UnpickPage.this |
| 151 | + .context |
| 152 | + .getContextProvider() |
| 153 | + .findClass(field.className); |
| 154 | + if (clsData == null) { |
| 155 | + constants.remove(constant); |
| 156 | + } |
| 157 | + } catch (final IOException e) { |
| 158 | + throw new UncheckedIOException(e); |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + if (!constants.isEmpty()) { |
| 163 | + super.visitGroupDefinition( |
| 164 | + GroupDefinition.Builder.from(groupDefinition) |
| 165 | + .setConstants(constants) |
| 166 | + .build()); |
| 167 | + } |
| 168 | + } |
| 169 | + }); |
| 170 | + } catch (final IOException e) { |
| 171 | + throw new UncheckedIOException(e); |
| 172 | + } |
| 173 | + }) |
132 | 174 | .build()) |
133 | 175 | .classResolver(classResolver) |
134 | 176 | .build(); |
|
0 commit comments