2018-03-25 15:59:43 +00:00
|
|
|
/*
|
|
|
|
* Minecraft Forge
|
2019-02-10 22:57:03 +00:00
|
|
|
* Copyright (c) 2016-2019.
|
2018-03-25 15:59:43 +00:00
|
|
|
*
|
|
|
|
* This library 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 version 2.1
|
|
|
|
* of the License.
|
|
|
|
*
|
|
|
|
* This library 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 this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
package net.minecraftforge.fml.loading.moddiscovery;
|
|
|
|
|
2018-07-01 20:10:13 +00:00
|
|
|
import com.electronwill.nightconfig.core.file.FileConfig;
|
2018-03-25 15:59:43 +00:00
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.reflect.TypeToken;
|
2019-01-14 03:51:36 +00:00
|
|
|
import net.minecraftforge.forgespi.language.IModFileInfo;
|
2018-08-27 17:10:07 +00:00
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.Logger;
|
2018-03-25 15:59:43 +00:00
|
|
|
|
2018-04-07 03:58:48 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.lang.reflect.Type;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
2018-03-25 15:59:43 +00:00
|
|
|
import java.util.Map;
|
2018-04-07 03:58:48 +00:00
|
|
|
import java.util.stream.Collectors;
|
2018-03-25 15:59:43 +00:00
|
|
|
|
2018-12-31 21:33:54 +00:00
|
|
|
import static net.minecraftforge.fml.loading.LogMarkers.LOADING;
|
2018-03-25 15:59:43 +00:00
|
|
|
|
|
|
|
public class ModFileParser {
|
2018-08-27 17:10:07 +00:00
|
|
|
|
|
|
|
private static final Logger LOGGER = LogManager.getLogger();
|
|
|
|
|
2018-06-11 01:12:46 +00:00
|
|
|
public static IModFileInfo readModList(final ModFile modFile) {
|
2018-08-27 17:10:07 +00:00
|
|
|
LOGGER.debug(LOADING,"Parsing mod file candidate {}", modFile.getFilePath());
|
2018-06-06 15:37:56 +00:00
|
|
|
final Path modsjson = modFile.getLocator().findPath(modFile, "META-INF", "mods.toml");
|
|
|
|
if (!Files.exists(modsjson)) {
|
2018-08-27 17:10:07 +00:00
|
|
|
LOGGER.warn(LOADING, "Mod file {} is missing mods.toml file", modFile);
|
2018-06-06 15:37:56 +00:00
|
|
|
return null;
|
2018-03-25 15:59:43 +00:00
|
|
|
}
|
2018-06-06 15:37:56 +00:00
|
|
|
return loadModFile(modFile, modsjson);
|
|
|
|
}
|
|
|
|
|
2018-06-11 01:12:46 +00:00
|
|
|
public static IModFileInfo loadModFile(final ModFile file, final Path modsjson)
|
2018-06-06 15:37:56 +00:00
|
|
|
{
|
2018-07-01 20:10:13 +00:00
|
|
|
final FileConfig fileConfig = FileConfig.builder(modsjson).build();
|
|
|
|
fileConfig.load();
|
|
|
|
fileConfig.close();
|
|
|
|
return new ModFileInfo(file, fileConfig);
|
2018-03-25 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2018-04-07 03:58:48 +00:00
|
|
|
protected static List<CoreModFile> getCoreMods(final ModFile modFile) {
|
|
|
|
Map<String,String> coreModPaths;
|
2018-03-25 15:59:43 +00:00
|
|
|
try {
|
2018-04-07 03:58:48 +00:00
|
|
|
final Path coremodsjson = modFile.getLocator().findPath(modFile, "META-INF", "coremods.json");
|
|
|
|
if (!Files.exists(coremodsjson)) {
|
|
|
|
return Collections.emptyList();
|
2018-03-25 15:59:43 +00:00
|
|
|
}
|
2018-04-07 03:58:48 +00:00
|
|
|
final Type type = new TypeToken<Map<String, String>>() {}.getType();
|
2018-03-25 15:59:43 +00:00
|
|
|
final Gson gson = new Gson();
|
2018-04-07 03:58:48 +00:00
|
|
|
coreModPaths = gson.fromJson(Files.newBufferedReader(coremodsjson), type);
|
|
|
|
} catch (IOException e) {
|
2018-08-27 17:10:07 +00:00
|
|
|
LOGGER.debug(LOADING,"Failed to read coremod list coremods.json", e);
|
2018-04-07 03:58:48 +00:00
|
|
|
return Collections.emptyList();
|
2018-03-25 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return coreModPaths.entrySet().stream().
|
2018-08-27 17:10:07 +00:00
|
|
|
peek(e-> LOGGER.debug(LOADING,"Found coremod {} with Javascript path {}", e.getKey(), e.getValue())).
|
2018-03-25 15:59:43 +00:00
|
|
|
map(e -> new CoreModFile(e.getKey(), modFile.getLocator().findPath(modFile, e.getValue()),modFile)).
|
2018-04-07 03:58:48 +00:00
|
|
|
collect(Collectors.toList());
|
2018-03-25 15:59:43 +00:00
|
|
|
}
|
2018-09-22 10:40:22 +00:00
|
|
|
}
|