Search in sources :

Example 1 with Checksum

use of org.pepsoft.util.Checksum in project WorldPainter by Captain-Chaos.

the class BiomeSchemeManager method doInitialisation.

private static void doInitialisation() {
    synchronized (initialisationLock) {
        if (logger.isDebugEnabled()) {
            logger.debug("Performing initialisation on thread {}", Thread.currentThread().getName());
        }
        try {
            // Scan the Minecraft directory for supported jars
            minecraftDir = MinecraftUtil.findMinecraftDir();
            if (minecraftDir != null) {
                scanDir(new File(minecraftDir, "bin"));
                scanDir(new File(minecraftDir, "versions"));
            }
            // Collect the names of the files we already looked at so we can skip
            // them below
            Set<File> processedFiles = new HashSet<>();
            for (Map.Entry<Integer, SortedMap<Version, BiomeJar>> entry : BIOME_JARS.entrySet()) {
                processedFiles.addAll(entry.getValue().values().stream().map(biomeJar -> biomeJar.file).collect(Collectors.toList()));
            }
            // Check the jars stored in the configuration (if we haven't
            // encountered them above)
            Configuration config = Configuration.getInstance();
            Map<Integer, File> minecraftJars = config.getMinecraftJars();
            for (Iterator<Map.Entry<Integer, File>> i = minecraftJars.entrySet().iterator(); i.hasNext(); ) {
                Map.Entry<Integer, File> entry = i.next();
                File file = entry.getValue();
                if (file.getAbsolutePath().toLowerCase().contains("optifine")) {
                    // OptiFine-created files create problems, so filter
                    // them out if the configuration somehow got polluted
                    // with them
                    i.remove();
                    continue;
                } else if (processedFiles.contains(file)) {
                    continue;
                } else if ((!file.isFile()) || (!file.canRead())) {
                    // The file is no longer there, or it's not accessible;
                    // remove it from the configuration
                    i.remove();
                    continue;
                }
                try {
                    Checksum checksum = FileUtils.getMD5(file);
                    if (DESCRIPTORS.containsKey(checksum)) {
                        for (BiomeSchemeDescriptor descriptor : DESCRIPTORS.get(checksum)) {
                            SortedMap<Version, BiomeJar> jars = BIOME_JARS.computeIfAbsent(descriptor.biomeScheme, key -> new TreeMap<>());
                            jars.put(descriptor.minecraftVersion, new BiomeJar(file, checksum, descriptor));
                            // Also store it as a resources jar
                            ALL_JARS.put(descriptor.minecraftVersion, file);
                        }
                    } else {
                        // resources
                        try {
                            Version version = Version.parse(file.getName().substring(0, file.getName().length() - 4));
                            ALL_JARS.put(version, file);
                        } catch (NumberFormatException e) {
                            // We don't recognize this jar. Perhaps it has been
                            // replaced with an unsupported version. In any
                            // case, remove it from the configuration
                            i.remove();
                        }
                    }
                } catch (IOException e) {
                    logger.error("I/O error while scanning Minecraft jar " + file.getAbsolutePath() + "; skipping file", e);
                }
            }
        } finally {
            // Done
            initialised = true;
            initialising = false;
            initialisationLock.notifyAll();
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Thread {} finished initialisation", Thread.currentThread().getName());
        }
    }
}
Also used : Configuration(org.pepsoft.worldpainter.Configuration) IOException(java.io.IOException) Version(org.pepsoft.util.Version) Checksum(org.pepsoft.util.Checksum) File(java.io.File)

Example 2 with Checksum

use of org.pepsoft.util.Checksum in project WorldPainter by Captain-Chaos.

the class BiomeSchemeManager method scanDir.

private static void scanDir(File dir) {
    File[] files = dir.listFiles(pathname -> pathname.isDirectory() || pathname.getName().toLowerCase().endsWith(".jar"));
    // wild that it does, so check for it
    if (files != null) {
        for (File file : files) {
            try {
                if (file.getName().toLowerCase().contains("optifine")) {
                    // Skip anything OptiFine-related, as OptiFine creates Minecraft profiles with invalid json files.
                    continue;
                }
                if (file.isDirectory()) {
                    scanDir(file);
                } else {
                    if (logger.isTraceEnabled()) {
                        logger.trace("Scanning file {}", file);
                    }
                    Checksum hash = FileUtils.getMD5(file);
                    if (DESCRIPTORS.containsKey(hash)) {
                        for (BiomeSchemeDescriptor descriptor : DESCRIPTORS.get(hash)) {
                            SortedMap<Version, BiomeJar> jars = BIOME_JARS.get(descriptor.biomeScheme);
                            if (jars == null) {
                                jars = new TreeMap<>();
                                BIOME_JARS.put(descriptor.biomeScheme, jars);
                            }
                            jars.put(descriptor.minecraftVersion, new BiomeJar(file, hash, descriptor));
                            // Also store it as a resources jar
                            ALL_JARS.put(descriptor.minecraftVersion, file);
                        }
                    } else {
                        // resources
                        try {
                            Version version = Version.parse(file.getName().substring(0, file.getName().length() - 4));
                            ALL_JARS.put(version, file);
                        } catch (NumberFormatException e) {
                        // Skip silently
                        }
                    }
                }
            } catch (IOException e) {
                logger.error("I/O error while scanning potential Minecraft jar or directory " + file.getAbsolutePath() + "; skipping file", e);
            }
        }
    }
}
Also used : Version(org.pepsoft.util.Version) Checksum(org.pepsoft.util.Checksum) IOException(java.io.IOException) File(java.io.File)

Aggregations

File (java.io.File)2 IOException (java.io.IOException)2 Checksum (org.pepsoft.util.Checksum)2 Version (org.pepsoft.util.Version)2 Configuration (org.pepsoft.worldpainter.Configuration)1