-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix fabric mod entrypoint class unexpectedly loaded
Signed-off-by: Hendrix-Shen <HendrixShen@hendrixshen.top>
- Loading branch information
1 parent
112cea8
commit 0a3343c
Showing
3 changed files
with
135 additions
and
23 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
magiclib-core/common/src/main/java/top/hendrixshen/magiclib/util/FileUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,30 @@ | ||
package top.hendrixshen.magiclib.util; | ||
|
||
import com.google.common.collect.Sets; | ||
import org.jetbrains.annotations.NotNull; | ||
import top.hendrixshen.magiclib.MagicLib; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.util.Enumeration; | ||
import java.util.Set; | ||
|
||
public class FileUtil { | ||
public static @NotNull File getConfigFile(String identifier) { | ||
return MagicLib.getInstance().getPlatformManage().getCurrentPlatform() | ||
.getConfigFolder().resolve(identifier + ".json").toFile(); | ||
} | ||
|
||
public static @NotNull Set<URL> getResources(String name) throws IOException { | ||
ClassLoader urlLoader = Thread.currentThread().getContextClassLoader(); | ||
Set<URL> set = Sets.newHashSet(); | ||
Enumeration<URL> urlEnumeration = urlLoader.getResources(name); | ||
|
||
while (urlEnumeration.hasMoreElements()) { | ||
set.add(urlEnumeration.nextElement()); | ||
} | ||
|
||
return set; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...rc/main/java/top/hendrixshen/magiclib/impl/platform/adapter/internal/ModMetaDataLite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package top.hendrixshen.magiclib.impl.platform.adapter.internal; | ||
|
||
import com.google.common.collect.Maps; | ||
import com.google.common.collect.Sets; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonToken; | ||
import lombok.Getter; | ||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.NotNull; | ||
import top.hendrixshen.magiclib.MagicLib; | ||
import top.hendrixshen.magiclib.util.FileUtil; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
|
||
@ApiStatus.Internal | ||
public class ModMetaDataLite { | ||
private static final Map<String, MetaData> data = Maps.newHashMap(); | ||
|
||
public static MetaData getMetaData(String modIdentifier) { | ||
return ModMetaDataLite.data.get(modIdentifier); | ||
} | ||
|
||
static { | ||
try { | ||
for (URL url : FileUtil.getResources("fabric.mod.json")) { | ||
InputStream inputStream = url.openStream(); | ||
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8); | ||
JsonReader jsonReader = new JsonReader(inputStreamReader); | ||
|
||
try { | ||
MetaData modMetaData = new MetaData(jsonReader); | ||
ModMetaDataLite.data.put(modMetaData.getModIdentifier(), modMetaData); | ||
} catch (Throwable e) { | ||
MagicLib.getLogger().debug("Failed to parse {}.", url, e); | ||
} | ||
} | ||
} catch (IOException e) { | ||
MagicLib.getLogger().error("Failed to load fabric.mod.json", e); | ||
} | ||
} | ||
|
||
@Getter | ||
public static class MetaData { | ||
private String modIdentifier; | ||
private final Map<String, HashSet<String>> entrypoints; | ||
|
||
MetaData(@NotNull JsonReader reader) throws IOException { | ||
this.entrypoints = Maps.newHashMap(); | ||
reader.beginObject(); | ||
|
||
while (reader.hasNext()) { | ||
String key = reader.nextName(); | ||
|
||
switch (key) { | ||
case "id": | ||
this.modIdentifier = reader.nextString(); | ||
break; | ||
|
||
case "entrypoints": | ||
reader.beginObject(); | ||
|
||
while (reader.hasNext()) { | ||
final String entryKey = reader.nextName(); | ||
reader.beginArray(); | ||
|
||
while (reader.hasNext()) { | ||
if (reader.peek() == JsonToken.STRING) { | ||
this.entrypoints.computeIfAbsent(entryKey, k -> Sets.newHashSet()).add(reader.nextString()); | ||
} else { | ||
reader.skipValue(); | ||
} | ||
} | ||
|
||
reader.endArray(); | ||
} | ||
|
||
reader.endObject(); | ||
break; | ||
default: | ||
reader.skipValue(); | ||
} | ||
} | ||
|
||
reader.endObject(); | ||
} | ||
} | ||
} |