Skip to content

Commit 326d80a

Browse files
committed
Allow mixin configs to search for source from itself
- Helps raw addConfiguration calls to still have a config source
1 parent 80f7293 commit 326d80a

File tree

5 files changed

+112
-42
lines changed

5 files changed

+112
-42
lines changed

build.gradle

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,14 @@ sourceSets {
5151
ext.languageVersion = 8
5252
ext.compatibility = '1.8'
5353
}
54+
cleanroom {
55+
compileClasspath += main.output
56+
ext.languageVersion = 8
57+
ext.compatibility = '1.8'
58+
}
5459
main {
5560
compileClasspath += legacy.output
61+
compileClasspath += cleanroom.output
5662
ext.languageVersion = 8
5763
ext.compatibility = '1.8'
5864
}
@@ -80,11 +86,6 @@ sourceSets {
8086
test {
8187
ext.modularityExcluded = true
8288
}
83-
cleanroom {
84-
compileClasspath += main.output
85-
ext.languageVersion = 8
86-
ext.compatibility = '1.8'
87-
}
8889
launchwrapper {
8990
compileClasspath += main.output
9091
ext.languageVersion = 8

src/cleanroom/java/org/spongepowered/asm/service/cleanroom/ModContainerHandle.java renamed to src/cleanroom/java/org/spongepowered/asm/launch/platform/container/ContainerHandlePath.java

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,50 +22,37 @@
2222
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2323
* THE SOFTWARE.
2424
*/
25-
package org.spongepowered.asm.service.cleanroom;
25+
package org.spongepowered.asm.launch.platform.container;
2626

27-
import org.spongepowered.asm.launch.platform.container.ContainerHandleURI;
27+
import java.nio.file.Path;
2828

29-
import java.io.File;
30-
import java.net.URI;
31-
import java.nio.file.Paths;
32-
33-
public class ModContainerHandle extends ContainerHandleURI {
34-
35-
private final String id;
36-
37-
public ModContainerHandle(URI uri, String id) {
38-
super(uri);
39-
if (id == null) {
40-
String fileName = Paths.get(uri).getFileName().toString();
41-
fileName = fileName.indexOf('.') != -1 ? fileName.substring(0, fileName.lastIndexOf('.')) : fileName;
42-
this.id = fileName;
43-
} else {
44-
this.id = id;
45-
}
46-
}
47-
48-
public ModContainerHandle(File file, String id) {
49-
this(file.toURI(), id);
50-
}
29+
/**
30+
* A nicer wrapper for {@link ContainerHandleURI} with NIO Paths
31+
* Provides a {@link IContainerHandle#getId()} result based on the file name
32+
*/
33+
public class ContainerHandlePath extends ContainerHandleURI {
5134

52-
public ModContainerHandle(URI uri) {
53-
this(uri, null);
54-
}
35+
/**
36+
* Target Path
37+
*/
38+
private final Path path;
5539

56-
public ModContainerHandle(File file) {
57-
this(file.toURI(), null);
40+
public ContainerHandlePath(Path path) {
41+
super(path.getFileName().toString(), path.toUri());
42+
this.path = path;
5843
}
5944

6045
@Override
61-
public String getId() {
62-
return id;
46+
public String getDescription() {
47+
return this.path.toString();
6348
}
6449

50+
/* (non-Javadoc)
51+
* @see java.lang.Object#toString()
52+
*/
6553
@Override
6654
public String toString() {
67-
return String.format("ModContainerHandle(%s|%s)", this.getId(), this.getURI());
55+
return String.format("ContainerHandlePath(%s|%s)", this.getId(), this.path);
6856
}
6957

7058
}
71-
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* This file is part of Mixin, licensed under 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.asm.service.cleanroom;
26+
27+
import java.net.URL;
28+
29+
/**
30+
* Cleanroom extension methods for IMixinService instances
31+
*/
32+
public interface ICleanroomMixinService {
33+
34+
/**
35+
* Get a resource from the appropriate classloader, this is
36+
* delegated via the service so that the service can choose the correct
37+
* classloader from which to obtain the resource.
38+
*
39+
* @param name resource path
40+
* @return resource or null if resource not found
41+
*/
42+
public abstract URL getResource(String name);
43+
44+
}

src/main/java/org/spongepowered/asm/launch/platform/container/ContainerHandleURI.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@
3737
* from previous mixin versions
3838
*/
3939
public class ContainerHandleURI implements IContainerHandle {
40-
40+
41+
/**
42+
* String id
43+
*/
44+
private final String id;
4145
/**
4246
* Target URI
4347
*/
@@ -49,14 +53,19 @@ public class ContainerHandleURI implements IContainerHandle {
4953
private final MainAttributes attributes;
5054

5155
public ContainerHandleURI(URI uri) {
56+
this(null, uri);
57+
}
58+
59+
// Added by Cleanroom
60+
public ContainerHandleURI(String id, URI uri) {
61+
this.id = id;
5262
this.uri = uri;
5363
this.attributes = MainAttributes.of(uri);
5464
}
5565

5666
@Override
5767
public String getId() {
58-
// Not enough information for this
59-
return null;
68+
return id;
6069
}
6170

6271
@Override

src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,20 @@
2424
*/
2525
package org.spongepowered.asm.mixin.transformer;
2626

27+
import java.io.IOException;
2728
import java.io.InputStream;
2829
import java.io.InputStreamReader;
2930
import java.lang.reflect.Constructor;
31+
import java.net.JarURLConnection;
32+
import java.net.URISyntaxException;
33+
import java.net.URL;
34+
import java.nio.file.Paths;
3035
import java.util.*;
3136
import java.util.regex.Matcher;
3237
import java.util.regex.Pattern;
3338

3439
import com.google.common.base.Joiner;
40+
import org.spongepowered.asm.launch.platform.container.ContainerHandlePath;
3541
import org.spongepowered.asm.logging.Level;
3642
import org.spongepowered.asm.logging.ILogger;
3743
import org.spongepowered.asm.launch.MixinInitialisationError;
@@ -59,6 +65,7 @@
5965
import org.spongepowered.asm.mixin.transformer.throwables.InvalidMixinException;
6066
import org.spongepowered.asm.service.IMixinService;
6167
import org.spongepowered.asm.service.MixinService;
68+
import org.spongepowered.asm.service.cleanroom.ICleanroomMixinService;
6269
import org.spongepowered.asm.util.VersionNumber;
6370

6471
import com.google.common.base.Strings;
@@ -457,7 +464,7 @@ private MixinConfig() {}
457464
private boolean onLoad(IMixinService service, String name, MixinEnvironment fallbackEnvironment, IMixinConfigSource source) {
458465
this.service = service;
459466
this.name = name;
460-
this.source = source;
467+
this.source = findSource(source, name); // Added by Cleanroom
461468

462469
// If parent is specified, don't perform postinit until parent is assigned
463470
if (!Strings.isNullOrEmpty(this.parentName)) {
@@ -1416,6 +1423,28 @@ static Config create(String configFile, MixinEnvironment outer, IMixinConfigSour
14161423
}
14171424
}
14181425

1426+
// Added by Cleanroom
1427+
private static IMixinConfigSource findSource(IMixinConfigSource original, String configFile) {
1428+
IMixinService service = MixinService.getService();
1429+
if (original == null || service instanceof ICleanroomMixinService) {
1430+
try {
1431+
URL resource = ((ICleanroomMixinService) service).getResource(configFile);
1432+
if (resource == null) {
1433+
throw new IOException("Mixin config cannot be found, getResource and getResourceAsStream implementations are possibly inconsistent");
1434+
}
1435+
if (!"jar".equals(resource.getProtocol())) {
1436+
throw new IOException("Mixin config does not reside in a jar file");
1437+
}
1438+
JarURLConnection conn = (JarURLConnection) resource.openConnection();
1439+
return new ContainerHandlePath(Paths.get(conn.getJarFileURL().toURI()));
1440+
} catch (IOException | URISyntaxException e) {
1441+
service.getLogger("mixin").warn("Unable to locate resource for mixin config source: {}", configFile, e);
1442+
return null;
1443+
}
1444+
}
1445+
return original;
1446+
}
1447+
14191448
private static int getCollectionSize(Collection<?>... collections) {
14201449
int total = 0;
14211450
for (Collection<?> collection : collections) {

0 commit comments

Comments
 (0)