fix: Vendor in fml ModMetadata parsing to avoid unnecessary classloading

This commit is contained in:
FalsePattern committed 2025-12-15 17:13:51 +01:00
1 parent c017060698
commit e090429a32
3 files changed
+140 -16

No files matched your search

@@ -40,8 +40,6 @@ import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import cpw.mods.fml.common.MetadataCollection;
import cpw.mods.fml.common.ModMetadata;
import cpw.mods.fml.fpdeploader.SystemExitBypassHelper;
import javax.swing.JFrame;
@@ -58,7 +56,6 @@ import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
@@ -103,7 +100,7 @@ public class DependencyLoaderImpl {
private static final Map<String, String> loadedModIdMods = new ConcurrentHashMap<>();
private static final Set<String> remoteMavenRepositories = new ConcurrentSet<>();
private static final Set<String> localMavenRepositories = new ConcurrentSet<>();
static final Logger LOG = LogManager.getLogger("FalsePatternLib DepLoader");
public static final Logger LOG = LogManager.getLogger("FalsePatternLib DepLoader");
private static final AtomicLong counter = new AtomicLong(0);
private static final ExecutorService executor = Executors.newCachedThreadPool(r -> {
@@ -115,21 +112,11 @@ public class DependencyLoaderImpl {
private static final Path libDir;
private static final Path modsDir;
private static final Path tempDir;
private static final Field metadataCollectionModListAccessor;
private static final AtomicBoolean needReboot = new AtomicBoolean(false);
private static final Set<String> downloadedMods = new ConcurrentSet<>();
private static final Set<String> downloadFailed = new ConcurrentSet<>();
static {
try {
metadataCollectionModListAccessor = MetadataCollection.class.getDeclaredField("modList");
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
metadataCollectionModListAccessor.setAccessible(true);
}
private static void ensureExists(Path directory) {
if (!Files.exists(directory)) {
try {
@@ -300,8 +287,7 @@ public class DependencyLoaderImpl {
private static void readMCMod(InputStream input, String name) {
try {
val meta = MetadataCollection.from(input, name);
val modList = (ModMetadata[]) metadataCollectionModListAccessor.get(meta);
for (val mod : modList) {
for (val mod : meta.modList) {
val id = mod.modId;
if (id != null) {
val versionStr = mod.version;
@@ -0,0 +1,97 @@
/*
* This file is part of FalsePatternLib.
*
* Copyright (C) 2022-2025 FalsePattern
* All Rights Reserved
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* FalsePatternLib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, only version 3 of the License.
*
* FalsePatternLib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FalsePatternLib. If not, see <https://www.gnu.org/licenses/>.
*/
package com.falsepattern.deploader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;
import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
class MetadataCollection
{
@SuppressWarnings("unused")
private String modListVersion;
ModMetadata[] modList;
private Map<String, ModMetadata> metadatas = Maps.newHashMap();
public static MetadataCollection from(InputStream inputStream, String sourceName)
{
if (inputStream == null)
{
return new MetadataCollection();
}
InputStreamReader reader = new InputStreamReader(inputStream);
try
{
MetadataCollection collection;
Gson gson = new GsonBuilder().create();
JsonParser parser = new JsonParser();
JsonElement rootElement = parser.parse(reader);
if (rootElement.isJsonArray())
{
collection = new MetadataCollection();
JsonArray jsonList = rootElement.getAsJsonArray();
collection.modList = new ModMetadata[jsonList.size()];
int i = 0;
for (JsonElement mod : jsonList)
{
collection.modList[i++]=gson.fromJson(mod, ModMetadata.class);
}
}
else
{
collection = gson.fromJson(rootElement, MetadataCollection.class);
}
collection.parseModMetadataList();
return collection;
}
catch (JsonParseException e)
{
DependencyLoaderImpl.LOG.error(e);
DependencyLoaderImpl.LOG.error("The mcmod.info file in {} cannot be parsed as valid JSON. It will be ignored", sourceName);
return new MetadataCollection();
}
catch (Exception e)
{
throw Throwables.propagate(e);
}
}
private void parseModMetadataList()
{
for (ModMetadata modMetadata : modList)
{
metadatas.put(modMetadata.modId, modMetadata);
}
}
}
@@ -0,0 +1,41 @@
/*
* This file is part of FalsePatternLib.
*
* Copyright (C) 2022-2025 FalsePattern
* All Rights Reserved
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* FalsePatternLib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, only version 3 of the License.
*
* FalsePatternLib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FalsePatternLib. If not, see <https://www.gnu.org/licenses/>.
*/
package com.falsepattern.deploader;
import com.google.gson.annotations.SerializedName;
/**
* @author cpw
*
*/
class ModMetadata
{
@SerializedName("modid")
public String modId;
public String version = "";
public ModMetadata()
{
}
}