Skip to content

Commit 8315da4

Browse files
authored
Merge pull request #299 from SkyBlade1978/master-1.12.2
Fix legacy Codec compatibility on Forge 1.12.2
2 parents f25a262 + 8471b3e commit 8315da4

18 files changed

Lines changed: 566 additions & 25 deletions

‎.github/workflows/ci.yml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ jobs:
126126
if-no-files-found: error
127127
retention-days: 30
128128
path: |
129-
build/libs/OreSpawn-4.0.16.112021.jar
130-
build/libs/OreSpawn-4.0.16.112021-sources.jar
131-
build/libs/OreSpawn-4.0.16.112021-javadoc.jar
129+
build/libs/OreSpawn-4.0.17.112021.jar
130+
build/libs/OreSpawn-4.0.17.112021-sources.jar
131+
build/libs/OreSpawn-4.0.17.112021-javadoc.jar
132132
build/release/SHA256SUMS
133133
CHANGELOG.txt
134134

‎CHANGELOG.txt‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
Version 4.0.17.112021
2+
3+
* Avoid a client-startup IncompatibleClassChangeError when another legacy
4+
modpack component supplies an interface-shaped Mojang serialization Codec
5+
ahead of OreSpawn's 1.12 compatibility adapter.
6+
* Preserve OreSpawn's existing public Codec descriptors and ordinary Forge 14
7+
behaviour while decoding built-in pattern settings through an internal
8+
class/interface-neutral bridge.
9+
110
Version 4.0.16.112021
211

312
* Adopt the shared 4.0.16 release identity. Forge 1.12 has neither

‎README.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ It gives mods and modpacks one place to configure ores, deposit shapes, optional
1010
rock strata and geomes, provider-owned underground fluid deposits, biome
1111
palettes and world materials, flat bedrock, and bounded ore retrogen.
1212

13-
This branch builds target-qualified version `4.0.16.112021`: the OreSpawn 4.0.16
14-
feature set for Minecraft 1.12.2 and Forge. See the
13+
This branch builds target-qualified version `4.0.17.112021`: the OreSpawn 4.0.17
14+
feature set for Minecraft 1.12.2 and Forge. This release avoids a startup
15+
linkage failure when a legacy modpack supplies an interface-shaped Mojang
16+
serialization `Codec` ahead of OreSpawn's compatibility adapter. See the
1517
[versioning policy](docs/VERSIONS.md) for the encoding and release convention.
1618

1719
Its deprecated OS3 compatibility layer imports OreSpawn 3 configuration and

‎build.gradle‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ minecraft {
112112
}
113113
register('client')
114114
register('server') {
115-
args '--nogui'
115+
args 'nogui'
116116
}
117117
}
118118
}
@@ -280,6 +280,9 @@ tasks.named('check') {
280280
}
281281
def configureFromForgeRun = { JavaExec process, String runTaskName ->
282282
process.actions.clear()
283+
if (runTaskName == 'runServer') {
284+
process.setArgs(['nogui'])
285+
}
283286
process.dependsOn {
284287
JavaExec run = tasks.getByName(runTaskName) as JavaExec
285288
run.taskDependencies.getDependencies(run)
@@ -1330,7 +1333,7 @@ tasks.register('verifyReleaseConfiguration') {
13301333
description = 'Validates the target-qualified release, API, schemas, reports, and publishing identity.'
13311334

13321335
doLast {
1333-
if (project.mod_version != '4.0.16.112021'
1336+
if (project.mod_version != '4.0.17.112021'
13341337
|| project.mod_group != expectedMavenGroup) {
13351338
throw new GradleException("Unexpected OreSpawn release version: ${project.mod_version}")
13361339
}
@@ -1357,8 +1360,8 @@ tasks.register('verifyReleaseConfiguration') {
13571360
'src/main/java/zone/moddev/mc/orespawn/worldgen/LegacyOs3ProfileMigration.java',
13581361
'README.md', 'CHANGELOG.txt'
13591362
].each { path ->
1360-
if (!file(path).getText('UTF-8').contains('4.0.16.112021')) {
1361-
throw new GradleException("Authoritative release location does not contain 4.0.16.112021: ${path}")
1363+
if (!file(path).getText('UTF-8').contains('4.0.17.112021')) {
1364+
throw new GradleException("Authoritative release location does not contain 4.0.17.112021: ${path}")
13621365
}
13631366
}
13641367
if (!file('docs/API.md').getText('UTF-8').contains('orespawn@[4.0.6,5.0.0)')) {
@@ -1421,6 +1424,7 @@ tasks.register('verifyReleaseArtifacts') {
14211424
['src/test/', 'src/biomeIntegrationTest/', 'src/migrationIntegrationTest/',
14221425
'src/clientIntegrationTest/', 'src/os1AbiFixture/', 'src/os3AbiFixture/',
14231426
'agent-notes/', 'surfaceprobe', 'migrationprobe', 'clientprobe',
1427+
'codec-conflict/', 'LegacyApiConsumer',
14241428
'org/junit/', 'org/mockito/', 'net/bytebuddy/'].each { forbidden ->
14251429
if (candidateZip.entries().any { it.name.contains(forbidden) }) {
14261430
throw new GradleException(
@@ -1787,6 +1791,9 @@ tasks.register('isolateEclipseProductionRuns') {
17871791
contents = contents.replace(
17881792
'key="MC_VERSION" value="${MC_VERSION}"',
17891793
"key=\"MC_VERSION\" value=\"${minecraft_version}\"")
1794+
contents = contents.replace(
1795+
'key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="--nogui"',
1796+
'key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="nogui"')
17901797
launch.setText(contents.replace('\r\n', '\n'), 'UTF-8')
17911798
}
17921799

@@ -1862,6 +1869,10 @@ tasks.register('verifyEclipseProductionClasspath') {
18621869
}.files as List<File>
18631870
allGeneratedLaunches.each { launch ->
18641871
String contents = launch.getText('UTF-8')
1872+
if (contents.contains('--nogui')) {
1873+
throw new GradleException(
1874+
"${launch.name} uses invalid legacy server argument --nogui; expected bare nogui")
1875+
}
18651876
if (contents.contains('${MC_VERSION}')) {
18661877
throw new GradleException(
18671878
"${launch.name} retains ForgeGradle's unresolved MC_VERSION token")

‎docs/VERSIONS.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Examples:
5252
| Minecraft | Loader | Target | Example full OreSpawn version |
5353
| --- | --- | ---: | --- |
5454
| 1.10.2 | Forge | `110021` | `4.0.6.110021` |
55-
| 1.12.2 | Forge | `112021` | `4.0.16.112021` |
55+
| 1.12.2 | Forge | `112021` | `4.0.17.112021` |
5656
| 1.13.2 | Forge | `113021` | `4.0.6.113021` |
5757
| 1.20.6 | Forge | `120061` | `4.0.6.120061` |
5858
| 1.21.11 | Forge | `121111` | `4.0.6.121111` |
@@ -154,8 +154,10 @@ to 4.0.13 for provider biome-filter parity and namespaced geome support, then to
154154
conversion. Forge 1.12 has no Y-sensitive biome-cell attribution or server-side
155155
GameTest harness, so 4.0.15 and the GameTest lifecycle portion of 4.0.16 are not
156156
applicable; it adopts the shared 4.0.16 identity while retaining ordinary
157-
benchmark auto-stop. A branch may therefore legitimately skip functional version
158-
numbers.
157+
benchmark auto-stop. Forge 1.12 then advances to 4.0.17 to tolerate legacy
158+
modpacks that supply an interface-shaped Mojang serialization `Codec` before
159+
OreSpawn's class-shaped compatibility adapter. A branch may therefore
160+
legitimately skip functional version numbers.
159161

160162
This provides three useful guarantees:
161163

‎gradle.properties‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ curseforge_project_id=245586
2626
mod_id=orespawn
2727
mod_name=MMD OreSpawn
2828
mod_license=LGPL-2.1
29-
mod_version=4.0.16.112021
29+
mod_version=4.0.17.112021
3030
mod_group=zone.moddev.mc.orespawn
3131
mod_authors=SkyBlade1978, dshadowwolf, the MMD Team
3232
mod_description=Configurable, provider-driven terrain, ore, and deposit generation.

‎src/main/java/com/mcmoddev/orespawn/compat/LegacyOs3Bridge.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ private static void writeHumanUpgradeReport(Path destination) throws IOException
818818
}
819819
}
820820
List<String> lines = new ArrayList<>();
821-
lines.add("OreSpawn 4.0.16.112021 Upgrade Report");
821+
lines.add("OreSpawn 4.0.17.112021 Upgrade Report");
822822
lines.add("================================");
823823
lines.add("");
824824
lines.add("RESULT: Legacy OreSpawn configuration was consumed and translated for OS4.");

‎src/main/java/zone/moddev/mc/orespawn/OreSpawn.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class OreSpawn {
5252

5353
public static final String MODID = "orespawn";
5454
public static final String NAME = "OreSpawn";
55-
public static final String VERSION = "4.0.16.112021";
55+
public static final String VERSION = "4.0.17.112021";
5656

5757
private static final Logger LOGGER = LogManager.getLogger();
5858

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
package zone.moddev.mc.orespawn.api;
2+
3+
import java.lang.reflect.InvocationHandler;
4+
import java.lang.reflect.InvocationTargetException;
5+
import java.lang.reflect.Method;
6+
import java.lang.reflect.Modifier;
7+
import java.lang.reflect.Proxy;
8+
import java.util.IdentityHashMap;
9+
import java.util.Map;
10+
import java.util.Optional;
11+
import java.util.function.Function;
12+
import java.util.function.Supplier;
13+
14+
import com.google.gson.JsonElement;
15+
import com.mojang.serialization.Codec;
16+
17+
/**
18+
* Keeps the legacy public Codec descriptor while avoiding class-versus-interface
19+
* invocation bytecode in OreSpawn's own startup and profile decoding paths.
20+
*/
21+
final class LegacyCodecBridge {
22+
private static final Map<Object, Function<JsonElement, ?>> DECODERS = new IdentityHashMap<>();
23+
24+
private LegacyCodecBridge() {
25+
}
26+
27+
@SuppressWarnings("unchecked")
28+
static <A> Codec<A> create(Function<JsonElement, A> decoder) {
29+
if (decoder == null) throw new NullPointerException("decoder");
30+
Class<?> codecType = Codec.class;
31+
Object codec;
32+
try {
33+
if (codecType.isInterface()) {
34+
codec = Proxy.newProxyInstance(codecType.getClassLoader(), new Class<?>[] { codecType },
35+
new CodecInvocationHandler<>(decoder));
36+
} else {
37+
Method factory = codecType.getMethod("of", Function.class);
38+
codec = factory.invoke(null, decoder);
39+
}
40+
} catch (ReflectiveOperationException exception) {
41+
throw linkageFailure("create the legacy ore-pattern codec", codecType, exception);
42+
}
43+
synchronized (DECODERS) {
44+
DECODERS.put(codec, decoder);
45+
}
46+
return (Codec<A>) codec;
47+
}
48+
49+
static Object decode(Object codec, JsonElement input) {
50+
Function<JsonElement, ?> decoder;
51+
synchronized (DECODERS) {
52+
decoder = DECODERS.get(codec);
53+
}
54+
if (decoder != null) {
55+
return decoder.apply(input);
56+
}
57+
58+
try {
59+
ClassLoader loader = codec.getClass().getClassLoader();
60+
Class<?> jsonOps = Class.forName("com.mojang.serialization.JsonOps", true, loader);
61+
Object operations = jsonOps.getField("INSTANCE").get(null);
62+
Method parse = findParse(codec.getClass(), operations, input);
63+
Object result = parse.invoke(codec, operations, input);
64+
return unwrapResult(result);
65+
} catch (InvocationTargetException exception) {
66+
Throwable cause = exception.getCause() == null ? exception : exception.getCause();
67+
throw new IllegalArgumentException(message(cause), cause);
68+
} catch (ReflectiveOperationException exception) {
69+
throw linkageFailure("decode legacy ore-pattern settings", codec.getClass(), exception);
70+
}
71+
}
72+
73+
private static Method findParse(Class<?> codecType, Object operations, JsonElement input)
74+
throws NoSuchMethodException {
75+
for (Method method : codecType.getMethods()) {
76+
if (!"parse".equals(method.getName()) || method.getParameterCount() != 2) continue;
77+
Class<?>[] parameters = method.getParameterTypes();
78+
if (parameters[0].isInstance(operations)
79+
&& (input == null || parameters[1].isInstance(input)
80+
|| parameters[1] == Object.class)) {
81+
return method;
82+
}
83+
}
84+
throw new NoSuchMethodException(codecType.getName()
85+
+ " has no JSON-compatible parse(operations, input) method");
86+
}
87+
88+
private static Object unwrapResult(Object dataResult) throws ReflectiveOperationException {
89+
if (dataResult == null) {
90+
throw new IllegalArgumentException("Codec returned no result");
91+
}
92+
Object value = dataResult.getClass().getMethod("result").invoke(dataResult);
93+
if (value instanceof Optional && ((Optional<?>) value).isPresent()) {
94+
return ((Optional<?>) value).get();
95+
}
96+
Object error = dataResult.getClass().getMethod("error").invoke(dataResult);
97+
String detail = error instanceof Optional && ((Optional<?>) error).isPresent()
98+
? String.valueOf(((Optional<?>) error).get()) : "unknown codec error";
99+
throw new IllegalArgumentException(detail);
100+
}
101+
102+
private static IllegalStateException linkageFailure(String action, Class<?> codecType,
103+
ReflectiveOperationException exception) {
104+
String origin = "unknown origin";
105+
if (codecType.getProtectionDomain() != null
106+
&& codecType.getProtectionDomain().getCodeSource() != null) {
107+
origin = String.valueOf(codecType.getProtectionDomain().getCodeSource().getLocation());
108+
}
109+
return new IllegalStateException("Cannot " + action + " using " + codecType.getName()
110+
+ " from " + origin, exception);
111+
}
112+
113+
private static String message(Throwable failure) {
114+
String text = failure.getMessage();
115+
return text == null || text.trim().isEmpty() ? failure.getClass().getSimpleName() : text;
116+
}
117+
118+
private static final class CodecInvocationHandler<A> implements InvocationHandler {
119+
private final Function<JsonElement, A> decoder;
120+
121+
private CodecInvocationHandler(Function<JsonElement, A> decoder) {
122+
this.decoder = decoder;
123+
}
124+
125+
@Override
126+
public Object invoke(Object proxy, Method method, Object[] arguments) throws Throwable {
127+
if (method.getDeclaringClass() == Object.class) {
128+
switch (method.getName()) {
129+
case "toString": return "OreSpawn legacy Codec bridge";
130+
case "hashCode": return System.identityHashCode(proxy);
131+
case "equals": return proxy == arguments[0];
132+
default: throw new UnsupportedOperationException(method.toString());
133+
}
134+
}
135+
if ("parse".equals(method.getName()) && arguments != null && arguments.length == 2
136+
&& arguments[1] instanceof JsonElement) {
137+
try {
138+
return dataResult(method.getReturnType(), decoder.apply((JsonElement) arguments[1]), null);
139+
} catch (RuntimeException exception) {
140+
return dataResult(method.getReturnType(), null, message(exception));
141+
}
142+
}
143+
throw new UnsupportedOperationException("OreSpawn's legacy Codec bridge supports JSON parse only: "
144+
+ method);
145+
}
146+
}
147+
148+
private static Object dataResult(Class<?> resultType, Object value, String error)
149+
throws ReflectiveOperationException {
150+
if (error == null) {
151+
return invokeStaticFactory(resultType, "success", value, null);
152+
}
153+
return invokeStaticFactory(resultType, "error", error, () -> error);
154+
}
155+
156+
private static Object invokeStaticFactory(Class<?> type, String name, Object direct,
157+
Supplier<String> supplied) throws ReflectiveOperationException {
158+
for (Method method : type.getMethods()) {
159+
if (!name.equals(method.getName()) || !Modifier.isStatic(method.getModifiers())
160+
|| method.getParameterCount() != 1) continue;
161+
Class<?> parameter = method.getParameterTypes()[0];
162+
if (supplied != null && Supplier.class.isAssignableFrom(parameter)) {
163+
return method.invoke(null, supplied);
164+
}
165+
if (direct == null || parameter.isInstance(direct) || parameter == Object.class) {
166+
return method.invoke(null, direct);
167+
}
168+
}
169+
throw new NoSuchMethodException(type.getName() + "." + name + "(value)");
170+
}
171+
}

‎src/main/java/zone/moddev/mc/orespawn/api/OrePatternType.java‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
import com.google.gson.JsonElement;
77
import com.mojang.serialization.Codec;
8-
import com.mojang.serialization.DataResult;
9-
import com.mojang.serialization.JsonOps;
108

119
import net.minecraftforge.registries.IForgeRegistryEntry;
1210

@@ -35,11 +33,14 @@ public Codec<?> codec() {
3533
}
3634

3735
public CompiledOrePattern decode(JsonElement configuration) {
38-
DataResult<?> result = codec.parse(JsonOps.INSTANCE, configuration);
39-
Object value = result.result().orElseThrow(() -> new IllegalArgumentException(
40-
"Invalid settings for ore pattern " + getRegistryName() + ": "
41-
+ result.error().map(Object::toString).orElse("unknown codec error")));
42-
return compile(value);
36+
try {
37+
return compile(LegacyCodecBridge.decode(codec, configuration));
38+
} catch (RuntimeException exception) {
39+
String detail = exception.getMessage() == null
40+
? exception.getClass().getSimpleName() : exception.getMessage();
41+
throw new IllegalArgumentException("Invalid settings for ore pattern "
42+
+ getRegistryName() + ": " + detail, exception);
43+
}
4344
}
4445

4546
private CompiledOrePattern compile(Object configuration) {

0 commit comments

Comments
 (0)