Search in sources :

Example 1 with Version

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

the class WPHDMapManager method init.

void init(ConfigurationNode configNode) {
    DynmapCore core = new DynmapCore();
    perspectives.put("default", new IsoHDPerspective(core, configNode));
    SortedMap<Version, File> minecraftJars = BiomeSchemeManager.getAllMinecraftJars();
    if (minecraftJars.isEmpty()) {
        logger.info("No Minecraft jars found; falling back to solid shading for 3D dynmap previews");
        shaders.put("default", new DefaultHDShader(core, configNode));
    } else {
        Version latestVersion = minecraftJars.lastKey();
        if (checkDynmapResources(latestVersion, minecraftJars.get(latestVersion))) {
            // Note that technically we're reporting the wrong version number
            // here and theoretically it could be wrong. In practice it
            // should usually be right though:
            logger.info("Using textures from Minecraft " + latestVersion + " for 3D dynmap previews");
            configNode.put("texturepack", "standard");
            TexturePack.loadTextureMapping(core, configNode);
            // Force initialisation of texture pack to get early errors:
            TexturePack.getTexturePack(core, "standard");
            shaders.put("default", new TexturePackHDShader(core, configNode));
        } else {
            // Could not copy the textures for dynmap for whatever reason;
            // fall back to solid colours
            logger.error("Error copying textures from Minecraft; falling back to solid shading for 3D dynmap previews");
            shaders.put("default", new DefaultHDShader(core, configNode));
        }
    }
    shaders.put("caves", new CaveHDShader(core, configNode));
    lightings.put("default", new DefaultHDLighting(core, configNode));
}
Also used : Version(org.pepsoft.util.Version) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 2 with Version

use of org.pepsoft.util.Version 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 3 with Version

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

the class BiomeSchemeManager method getBiomeScheme.

public static BiomeScheme getBiomeScheme(final int biomeAlgorithm, final boolean shared) {
    if (logger.isTraceEnabled()) {
        logger.trace("Thread {} requesting biome scheme {}", Thread.currentThread().getName(), biomeAlgorithm, new Throwable("Invoked from"));
    }
    synchronized (initialisationLock) {
        if (!initialised) {
            initialise();
        }
    }
    if (shared && BIOME_SCHEMES.containsKey(biomeAlgorithm)) {
        // this algorithm, so reuse it
        return BIOME_SCHEMES.get(biomeAlgorithm);
    } else {
        final String version;
        switch(biomeAlgorithm) {
            case BIOME_ALGORITHM_1_1:
                version = "1.1";
                break;
            case BIOME_ALGORITHM_1_2_AND_1_3_DEFAULT:
                version = "1.6.4 or 1.2.3 - 1.6.2";
                break;
            case BIOME_ALGORITHM_1_3_LARGE:
                version = "1.6.4 or 1.3.1 - 1.6.2";
                break;
            case BIOME_ALGORITHM_1_7_DEFAULT:
                version = "1.12.2 or 1.7.2 - 1.10.2";
                break;
            case BIOME_ALGORITHM_1_7_LARGE:
                version = "1.12.2 or 1.7.2 - 1.10.2";
                break;
            default:
                throw new IllegalArgumentException();
        }
        SortedMap<Version, BiomeJar> biomeJars = BIOME_JARS.get(biomeAlgorithm);
        if ((biomeJars != null) && (!biomeJars.isEmpty())) {
            // We have a jar for this biome scheme
            final BiomeJar biomeJar = biomeJars.get(biomeJars.lastKey());
            logger.info("Creating biome scheme " + version + " from " + biomeJar.file.getAbsolutePath());
            BiomeScheme biomeScheme = biomeJar.descriptor.instantiate(biomeJar.file, minecraftDir, biomeJar.checksum);
            if (shared) {
                BIOME_SCHEMES.put(biomeAlgorithm, biomeScheme);
            }
            return biomeScheme;
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("Could not find compatible jar for biome scheme " + version);
            }
            return null;
        }
    }
}
Also used : Version(org.pepsoft.util.Version) BiomeScheme(org.pepsoft.worldpainter.BiomeScheme)

Example 4 with Version

use of org.pepsoft.util.Version 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)

Example 5 with Version

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

the class PlantLayerEditor method findIcon.

private static BufferedImage findIcon(String name) {
    if (resourcesJar == RESOURCES_NOT_AVAILABLE) {
        return null;
    }
    try {
        if (resourcesJar == null) {
            SortedMap<Version, File> jars = BiomeSchemeManager.getAllMinecraftJars();
            if (!jars.isEmpty()) {
                resourcesJar = jars.get(jars.lastKey());
            } else {
                logger.warn("Could not find Minecraft jar for loading plant icons");
                resourcesJar = RESOURCES_NOT_AVAILABLE;
                return null;
            }
        }
        try (JarFile jarFile = new JarFile(resourcesJar)) {
            JarEntry entry = jarFile.getJarEntry("assets/minecraft/textures/" + name);
            if (entry != null) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Loading plant icon " + name + " from " + resourcesJar);
                }
                try (InputStream in = jarFile.getInputStream(entry)) {
                    return ImageIO.read(in);
                }
            } else {
                if (logger.isDebugEnabled()) {
                    logger.debug("Could not find plant icon " + name + " in Minecraft jar " + resourcesJar);
                }
                return null;
            }
        }
    } catch (IOException e) {
        logger.error("I/O error while trying to load plant icon " + name + "; continuing without icon", e);
        resourcesJar = RESOURCES_NOT_AVAILABLE;
        return null;
    }
}
Also used : Version(org.pepsoft.util.Version) InputStream(java.io.InputStream) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile) File(java.io.File)

Aggregations

Version (org.pepsoft.util.Version)5 File (java.io.File)4 IOException (java.io.IOException)3 JarFile (java.util.jar.JarFile)2 Checksum (org.pepsoft.util.Checksum)2 InputStream (java.io.InputStream)1 JarEntry (java.util.jar.JarEntry)1 BiomeScheme (org.pepsoft.worldpainter.BiomeScheme)1 Configuration (org.pepsoft.worldpainter.Configuration)1