This repository has been archived by the owner on Feb 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
ClientTweaker.java
115 lines (90 loc) · 4.29 KB
/
ClientTweaker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* Copyright 2018 ImpactDevelopment
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package clientapi.load;
import clientapi.ClientAPI;
import clientapi.config.ClientConfiguration;
import clientapi.config.JsonConfiguration;
import clientapi.load.transform.impl.ValueAccessorTransformer;
import io.github.impactdevelopment.simpletweaker.SimpleTweaker;
import io.github.impactdevelopment.simpletweaker.transform.SimpleTransformer;
import net.minecraft.launchwrapper.Launch;
import net.minecraft.launchwrapper.LaunchClassLoader;
import org.spongepowered.asm.launch.MixinBootstrap;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.Mixins;
import org.spongepowered.tools.obfuscation.mcp.ObfuscationServiceMCP;
import java.io.InputStream;
import java.util.List;
/**
* Initializes the Mixin environment, confirms that a ClientAPI mod configuration is
* present, and then loads the custom defined mixins and transformers.
*
* @author Brady
* @since 1/20/2017
*/
public final class ClientTweaker extends SimpleTweaker {
@Override
public void injectIntoClassLoader(LaunchClassLoader classLoader) {
super.injectIntoClassLoader(classLoader);
ClientAPI.LOGGER.info("Injecting into ClassLoader");
this.setupMixin();
ClientConfiguration config = findClientConfig();
this.setupTransformers(config);
// Load mixin configs
this.loadMixinConfig("mixins.capi.json");
// Load Client defined mixin configs
for (String mixin : config.getMixins())
this.loadMixinConfig(mixin);
ClientAPI.LOGGER.info("Loaded Mixin Configurations");
}
@SuppressWarnings("unchecked")
private void setupMixin() {
MixinBootstrap.init();
ClientAPI.LOGGER.info("Initialized Mixin bootstrap");
// Find all of the other tweakers that are being loaded
List<String> tweakClasses = (List<String>) Launch.blackboard.get("TweakClasses");
// Default to NOTCH obfuscation context
String obfuscation = ObfuscationServiceMCP.NOTCH;
// If there are any tweak classes that are packaged under "net.minecraftforge.fml.common.launcher",
// switch the obfuscation context in the mixin environment to SEARGE
if (tweakClasses.stream().anyMatch(s -> s.contains("net.minecraftforge.fml.common.launcher"))) {
obfuscation = ObfuscationServiceMCP.SEARGE;
ClientAPI.LOGGER.info("Discovered FML! Switching to SEARGE mappings.");
}
MixinEnvironment.getDefaultEnvironment().setSide(MixinEnvironment.Side.CLIENT);
MixinEnvironment.getDefaultEnvironment().setObfuscationContext(obfuscation);
ClientAPI.LOGGER.info("Setup Mixin Environment");
}
private void setupTransformers(ClientConfiguration config) {
SimpleTransformer transformer = SimpleTransformer.getInstance();
if (!Boolean.valueOf(System.getProperty("clientapi.load.devMode", "false")))
transformer.registerAll(new ValueAccessorTransformer());
transformer.registerAll(config.getTransformers());
ClientAPI.LOGGER.info("Registered Bytecode Transformes");
}
private ClientConfiguration findClientConfig() {
InputStream stream;
if ((stream = this.getClass().getResourceAsStream("/client.json")) == null)
throw new ClientInitException("Unable to locate Client Configuration");
return JsonConfiguration.loadConfiguration(stream, ClientConfiguration.class);
}
private void loadMixinConfig(String config) {
// Verify the existence of the specified configuration file
if (this.getClass().getResourceAsStream("/" + config) == null)
throw new ClientInitException("Unable to locate mixin configuration %s", config);
Mixins.addConfiguration(config);
}
}