Skip to content

Commit 63f15bf

Browse files
committed
Bridge custom mod properties
1 parent 5201690 commit 63f15bf

File tree

2 files changed

+57
-16
lines changed

2 files changed

+57
-16
lines changed

src/main/java/net/fabricmc/loader/impl/FMLModMetadata.java

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,29 @@
1616

1717
package net.fabricmc.loader.impl;
1818

19+
import com.electronwill.nightconfig.core.UnmodifiableConfig;
20+
import com.mojang.logging.LogUtils;
1921
import net.fabricmc.loader.api.Version;
2022
import net.fabricmc.loader.api.metadata.*;
23+
import net.fabricmc.loader.impl.metadata.CustomValueImpl;
2124
import net.fabricmc.loader.impl.metadata.SimplePerson;
2225
import net.neoforged.neoforgespi.language.IModInfo;
26+
import org.jetbrains.annotations.Nullable;
27+
import org.slf4j.Logger;
2328

2429
import java.util.*;
30+
import java.util.stream.Collectors;
2531
import java.util.stream.Stream;
2632

2733
import static cpw.mods.modlauncher.api.LambdaExceptionUtils.uncheck;
2834

2935
public class FMLModMetadata implements ModMetadata {
36+
private static final Logger LOGGER = LogUtils.getLogger();
37+
3038
private final IModInfo modInfo;
3139
private final Version version;
3240
private final Collection<Person> authors;
41+
private final Map<String, CustomValue> customValues;
3342

3443
public FMLModMetadata(IModInfo modInfo) {
3544
this.modInfo = modInfo;
@@ -38,6 +47,8 @@ public FMLModMetadata(IModInfo modInfo) {
3847
.flatMap(obj -> obj instanceof List list ? ((List<String>) list).stream() : Stream.of(obj.toString().split(",")))
3948
.<Person>map(SimplePerson::new)
4049
.toList();
50+
this.customValues = this.modInfo.getModProperties().entrySet().stream()
51+
.collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, e -> convertModProperty(e.getValue())));
4152
}
4253

4354
@Override
@@ -115,21 +126,51 @@ public Optional<String> getIconPath(int size) {
115126

116127
@Override
117128
public boolean containsCustomValue(String key) {
118-
return false;
129+
return this.modInfo.getModProperties().containsKey(key);
119130
}
120131

121132
@Override
122133
public CustomValue getCustomValue(String key) {
123-
return null;
134+
return this.customValues.get(key);
124135
}
125136

126137
@Override
127138
public Map<String, CustomValue> getCustomValues() {
128-
return Map.of();
139+
return this.customValues;
129140
}
130141

131142
@Override
132143
public boolean containsCustomElement(String key) {
133-
return false;
144+
return containsCustomValue(key);
145+
}
146+
147+
@Nullable
148+
private static CustomValue convertModProperty(@Nullable Object value) {
149+
switch (value) {
150+
case null -> {
151+
return CustomValueImpl.NULL;
152+
}
153+
case UnmodifiableConfig config -> {
154+
Map<String, CustomValue> entries = config.entrySet().stream()
155+
.collect(Collectors.toMap(UnmodifiableConfig.Entry::getKey, e -> convertModProperty(e.getValue())));
156+
return new CustomValueImpl.ObjectImpl(entries);
157+
}
158+
case ArrayList<?> list -> {
159+
List<CustomValue> contents = list.stream().map(FMLModMetadata::convertModProperty).toList();
160+
return new CustomValueImpl.ArrayImpl(contents);
161+
}
162+
case Boolean b -> {
163+
return b ? CustomValueImpl.BOOLEAN_TRUE : CustomValueImpl.BOOLEAN_FALSE;
164+
}
165+
case String str -> {
166+
return new CustomValueImpl.StringImpl(str);
167+
}
168+
case Number num -> {
169+
return new CustomValueImpl.NumberImpl(num);
170+
}
171+
default -> {}
172+
}
173+
LOGGER.warn("Ignoring custom mod property value '{}' of unsupported type '{}'", value, value.getClass().getName());
174+
return null;
134175
}
135176
}

src/main/java/net/fabricmc/loader/impl/metadata/CustomValueImpl.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
import net.fabricmc.loader.api.metadata.CustomValue;
3030
import net.fabricmc.loader.impl.lib.gson.JsonReader;
3131

32-
abstract class CustomValueImpl implements CustomValue {
33-
static final CustomValue BOOLEAN_TRUE = new BooleanImpl(true);
34-
static final CustomValue BOOLEAN_FALSE = new BooleanImpl(false);
35-
static final CustomValue NULL = new NullImpl();
32+
public abstract class CustomValueImpl implements CustomValue {
33+
public static final CustomValue BOOLEAN_TRUE = new BooleanImpl(true);
34+
public static final CustomValue BOOLEAN_FALSE = new BooleanImpl(false);
35+
public static final CustomValue NULL = new NullImpl();
3636

3737
public static CustomValue readCustomValue(JsonReader reader) throws IOException, ParseMetadataException {
3838
switch (reader.peek()) {
@@ -125,10 +125,10 @@ public final boolean getAsBoolean() {
125125
}
126126
}
127127

128-
private static final class ObjectImpl extends CustomValueImpl implements CvObject {
128+
public static final class ObjectImpl extends CustomValueImpl implements CvObject {
129129
private final Map<String, CustomValue> entries;
130130

131-
ObjectImpl(Map<String, CustomValue> entries) {
131+
public ObjectImpl(Map<String, CustomValue> entries) {
132132
this.entries = Collections.unmodifiableMap(entries);
133133
}
134134

@@ -158,10 +158,10 @@ public Iterator<Entry<String, CustomValue>> iterator() {
158158
}
159159
}
160160

161-
private static final class ArrayImpl extends CustomValueImpl implements CvArray {
161+
public static final class ArrayImpl extends CustomValueImpl implements CvArray {
162162
private final List<CustomValue> entries;
163163

164-
ArrayImpl(List<CustomValue> entries) {
164+
public ArrayImpl(List<CustomValue> entries) {
165165
this.entries = Collections.unmodifiableList(entries);
166166
}
167167

@@ -186,10 +186,10 @@ public Iterator<CustomValue> iterator() {
186186
}
187187
}
188188

189-
private static final class StringImpl extends CustomValueImpl {
189+
public static final class StringImpl extends CustomValueImpl {
190190
final String value;
191191

192-
StringImpl(String value) {
192+
public StringImpl(String value) {
193193
this.value = value;
194194
}
195195

@@ -199,10 +199,10 @@ public CvType getType() {
199199
}
200200
}
201201

202-
private static final class NumberImpl extends CustomValueImpl {
202+
public static final class NumberImpl extends CustomValueImpl {
203203
final Number value;
204204

205-
NumberImpl(Number value) {
205+
public NumberImpl(Number value) {
206206
this.value = value;
207207
}
208208

0 commit comments

Comments
 (0)