2019-07-01 03:48:54 +00:00
|
|
|
/*
|
|
|
|
* Minecraft Forge
|
|
|
|
* Copyright (c) 2016-2019.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2019-05-27 04:00:45 +00:00
|
|
|
package net.minecraftforge.fml.loading;
|
|
|
|
|
|
|
|
import cpw.mods.modlauncher.api.LamdbaExceptionUtils;
|
|
|
|
import cpw.mods.modlauncher.serviceapi.ITransformerDiscoveryService;
|
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.util.ArrayList;
|
2019-07-20 23:37:54 +00:00
|
|
|
import java.util.Collections;
|
2019-05-27 04:00:45 +00:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.zip.ZipFile;
|
|
|
|
|
|
|
|
public class ModDirTransformerDiscoverer implements ITransformerDiscoveryService {
|
|
|
|
@Override
|
|
|
|
public List<Path> candidates(final Path gameDirectory) {
|
|
|
|
final Path modsDir = gameDirectory.resolve(FMLPaths.MODSDIR.relative());
|
2019-07-20 23:37:54 +00:00
|
|
|
if (!Files.exists(modsDir)) {
|
|
|
|
// Skip if the mods dir doesn't exist yet.
|
|
|
|
return Collections.emptyList();
|
|
|
|
}
|
2019-05-27 04:00:45 +00:00
|
|
|
List<Path> paths = new ArrayList<>();
|
|
|
|
try {
|
2019-06-16 07:47:40 +00:00
|
|
|
Files.createDirectories(modsDir);
|
2019-05-27 04:00:45 +00:00
|
|
|
Files.walk(modsDir, 1).forEach(p -> {
|
|
|
|
if (!Files.isRegularFile(p)) return;
|
|
|
|
if (!p.toString().endsWith(".jar")) return;
|
|
|
|
if (LamdbaExceptionUtils.uncheck(()->Files.size(p)) == 0) return;
|
|
|
|
try (ZipFile zf = new ZipFile(new File(p.toUri()))) {
|
|
|
|
if (zf.getEntry("META-INF/services/cpw.mods.modlauncher.api.ITransformationService") != null) {
|
|
|
|
paths.add(p);
|
|
|
|
}
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
LogManager.getLogger().error("Zip Error when loading jar file {}", p, ioe);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (IOException | IllegalStateException ioe) {
|
|
|
|
LogManager.getLogger().error("Error during early discovery", ioe);
|
|
|
|
}
|
|
|
|
return paths;
|
|
|
|
}
|
2019-06-16 07:47:40 +00:00
|
|
|
}
|