Search in sources :

Example 16 with Asset

use of io.xol.chunkstories.api.content.Asset in project chunkstories by Hugobros3.

the class LocalizationManagerImplementation method reload.

public void reload() {
    translations.clear();
    Iterator<Asset> i = modsManager.getAllAssetsByPrefix("./lang/");
    while (i.hasNext()) {
        Asset a = i.next();
        if (a.getName().endsWith("lang.info")) {
            String abrigedName = a.getName().substring(7, a.getName().length() - 10);
            if (abrigedName.contains("/"))
                continue;
            // System.out.println("Found translation: "+abrigedName);
            translations.put(abrigedName, a);
        }
    }
    if (activeTranslation != null)
        activeTranslation = new ActualTranslation(((ActualTranslation) activeTranslation).a);
    else
        activeTranslation = new ActualTranslation(translations.get(defaultTranslation));
}
Also used : Asset(io.xol.chunkstories.api.content.Asset)

Example 17 with Asset

use of io.xol.chunkstories.api.content.Asset in project chunkstories by Hugobros3.

the class PacketsStore method reload.

public void reload() {
    byNames.clear();
    byClasses.clear();
    // Load system.packets
    InputStream is = getClass().getResourceAsStream("/system.packets");
    readPacketsDefinitions(new BufferedReader(new InputStreamReader(is)), new Object() {

        @Override
        public String toString() {
            return "system.packets";
        }
    });
    // Load packets from content
    Iterator<Asset> i = store.modsManager().getAllAssetsByExtension("packets");
    while (i.hasNext()) {
        Asset f = i.next();
        readPacketsDefinitions(f);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) Asset(io.xol.chunkstories.api.content.Asset)

Example 18 with Asset

use of io.xol.chunkstories.api.content.Asset in project chunkstories by Hugobros3.

the class ModImplementation method computeMD5Hash.

private synchronized void computeMD5Hash() {
    // Makes a sorted list of the names of all the assets
    List<String> assetsSorted = new ArrayList<String>();
    for (Asset asset : assets()) {
        assetsSorted.add(asset.getName());
    }
    assetsSorted.sort(new Comparator<String>() {

        @Override
        public int compare(String o1, String o2) {
            return o1.compareTo(o2);
        }
    });
    // for(String s : assetsSorted)
    // System.out.println(s);
    // Concatenate their names...
    String completeNamesString = "";
    for (String s : assetsSorted) completeNamesString += s + ";";
    // MD5 it
    String hashedNames = HexTools.byteArrayAsHexString(md.digest(completeNamesString.getBytes()));
    // Iterate over each asset, hash it then add that to the sb
    StringBuilder sb = new StringBuilder();
    for (String s : assetsSorted) {
        Asset a = this.getAssetByName(s);
        byte[] buffer = new byte[4096];
        DigestInputStream eater = new DigestInputStream(a.read(), md);
        try {
            while (eater.read(buffer) != -1) ;
            eater.close();
        } catch (IOException e) {
        }
        // Append
        sb.append(HexTools.byteArrayAsHexString(md.digest()));
    }
    // Append hash of list of names
    sb.append(hashedNames);
    // Hash the whole stuff again
    md5hash = HexTools.byteArrayAsHexString(md.digest(sb.toString().getBytes()));
}
Also used : DigestInputStream(java.security.DigestInputStream) ArrayList(java.util.ArrayList) Asset(io.xol.chunkstories.api.content.Asset) IOException(java.io.IOException)

Example 19 with Asset

use of io.xol.chunkstories.api.content.Asset in project chunkstories by Hugobros3.

the class VoxelTexturesArrays method buildTextureAtlas.

public void buildTextureAtlas() {
    defaultAlbedo = null;
    defaultNormal = null;
    defaultMaterial = null;
    textures.clear();
    try {
        int gl_MaxTextureUnits = glGetInteger(GL_MAX_TEXTURE_IMAGE_UNITS);
        int gl_MaxTextureArraySize = glGetInteger(GL_MAX_ARRAY_TEXTURE_LAYERS);
        if (gl_MaxTextureArraySize < 2048) {
            logger().warn("Max texture array size < 2048. For ideal results please use a GPU from this geological era.");
        }
        if (gl_MaxTextureUnits < 32) {
            logger().warn("Max texture units < 32. This means your GPU is ancient and you'll run into a lot of issues!");
        }
        // We'll reserve 8 texture units for all the other fluff
        int maxTextureArrays = (gl_MaxTextureUnits - 8);
        List<AtlasElement> elements = new ArrayList<AtlasElement>();
        // Map<Integer, AtlasElement> sizeBuckets = new HashMap<Integer, AtlasElement>();
        // First we want to iterate over every file to get an idea of how many textures (and of how many sizes) we are dealing
        Iterator<AssetHierarchy> allFiles = content.modsManager().getAllUniqueEntries();
        AssetHierarchy entry;
        Asset f;
        while (allFiles.hasNext()) {
            entry = allFiles.next();
            if (entry.getName().startsWith("./voxels/textures/")) {
                String name = entry.getName().replace("./voxels/textures/", "");
                Type type = Type.ALBEDO;
                if (name.startsWith("normal/")) {
                    name = name.substring("normal/".length());
                    type = Type.NORMAL;
                } else if (name.startsWith("material/")) {
                    name = name.substring("material/".length());
                    type = Type.MATERIAL;
                }
                // We are only interested in top-level textures.
                if (name.contains("/"))
                    continue;
                f = entry.topInstance();
                if (f.getName().endsWith(".png")) {
                    String textureName = name.replace(".png", "");
                    // VoxelTextureAtlased voxelTexture = new VoxelTextureAtlased(textureName, uniquesIds);
                    int width, height;
                    try {
                        ImageReader reader = ImageIO.getImageReadersBySuffix("png").next();
                        ImageInputStream stream = ImageIO.createImageInputStream(f.read());
                        reader.setInput(stream);
                        width = reader.getWidth(reader.getMinIndex());
                        height = reader.getHeight(reader.getMinIndex());
                    } catch (Exception e) {
                        logger().warn("Could not obtain the size of the asset: " + f.getName());
                        // e.printStackTrace();
                        continue;
                    }
                    // We want nice powers of two
                    if ((width & (width - 1)) != 0 || (height & (height - 1)) != 0) {
                        logger().warn("Non pow2 texture size (" + width + ":" + height + ") for: " + f.getName() + ", skipping.");
                        continue;
                    }
                    // Width >= 16
                    if (width < 16 || height < 16) {
                        logger().warn("Too small (<16px) texture (" + width + ":" + height + ") for: " + f.getName() + ", skipping.");
                        continue;
                    }
                    int frames = height / width;
                    AtlasElement texture = new AtlasElement(f, type, textureName, width, frames);
                    elements.add(texture);
                    if (textureName.equals("notex")) {
                        switch(type) {
                            case ALBEDO:
                                defaultAlbedo = texture;
                                break;
                            case NORMAL:
                                defaultNormal = texture;
                                break;
                            case MATERIAL:
                                defaultMaterial = texture;
                                break;
                        }
                    }
                // System.out.println(textureName + " : " + frames);
                // sizeBuckets.put(width, texture);
                // System.out.println("Added: "+texture);
                } else if (f.getName().endsWith(".jpg") || f.getName().endsWith(".tiff") || f.getName().endsWith(".bmp") || f.getName().endsWith(".gif")) {
                    logger().warn("Found image file of unsupported format in voxels folder: " + f.getName() + ", ignoring.");
                    continue;
                }
            }
        }
        // Check we DID obtain default textures
        if (defaultAlbedo == null || defaultNormal == null || defaultMaterial == null) {
            logger().error("Missing 'notex.png' for one of the 3 texture types (albedo, normal, material), exiting !");
            System.exit(-602);
        }
        // Once that's done, try to fit all everything in the texture units constraints
        int[] sizes = new int[maxTextureArrays];
        int[] counts = new int[maxTextureArrays];
        AtlasElement[][] stuff = new AtlasElement[maxTextureArrays][gl_MaxTextureArraySize];
        int maxSize = 0;
        int maxDownscaled = 0;
        int downscaleTimes = 0;
        while (true) {
            // Check if everything could fit in N - 8 texture units
            for (int i = 0; i < maxTextureArrays; i++) {
                sizes[i] = 0;
                counts[i] = 0;
            }
            boolean ko = false;
            for (AtlasElement e : elements) {
                if (e.downscaleTo > maxSize)
                    maxSize = e.downscaleTo;
                boolean foundSpace = false;
                for (int i = 0; i < maxTextureArrays; i++) {
                    // Can create a new size
                    if (sizes[i] == 0) {
                        sizes[i] = e.downscaleTo;
                        stuff[i][counts[i]] = e;
                        counts[i] += e.animationFrames;
                        foundSpace = true;
                        break;
                    } else // Size already exist
                    if (sizes[i] == e.downscaleTo) {
                        // Has enough remaining space
                        if (gl_MaxTextureArraySize - counts[i] >= e.animationFrames) {
                            stuff[i][counts[i]] = e;
                            counts[i] += e.animationFrames;
                            foundSpace = true;
                            break;
                        }
                    }
                }
                if (foundSpace) {
                    continue;
                } else {
                    // We're out of space, cancel already
                    ko = true;
                    break;
                }
            }
            if (!ko) {
                // Everyone found a place! Go ahead it's cool
                break;
            } else {
                if (maxSize == 16) {
                    JOptionPane.showMessageDialog(null, "Exceeded vram constraints :(");
                }
                // Shrink the biggest texture size and try again
                for (AtlasElement e : elements) {
                    if (e.downscaleTo >= maxSize) {
                        e.downscaleTo = e.downscaleTo / 2;
                    }
                }
                downscaleTimes++;
                maxDownscaled = maxSize / 2;
                maxSize = 0;
            }
        }
        System.out.println("Found space OK for every " + elements.size() + " voxel texture, had to downscale " + downscaleTimes + " resolutions down to " + maxDownscaled);
        // Once we secured all the needed textures, let's assemble them in a big map
        for (int i = 0; i < maxTextureArrays; i++) {
            System.out.println("Array " + i + ": Size:" + sizes[i] + " Count:" + counts[i]);
            sizes[i] = 0;
            counts[i] = 0;
            for (int j = 0; j < counts[i]; j++) {
                AtlasElement element = stuff[i][j];
                VoxelTextureArrayed completedTexture = textures.get(element.name);
                if (completedTexture == null) {
                    completedTexture = new VoxelTextureArrayed(element.name);
                    textures.put(element.name, completedTexture);
                }
                switch(element.type) {
                    case ALBEDO:
                        completedTexture.albedo = element;
                        break;
                    case NORMAL:
                        completedTexture.normal = element;
                        break;
                    case MATERIAL:
                        completedTexture.material = element;
                        break;
                }
            }
        }
    } catch (Exception e) {
        logger().error("Exception during loading of voxel textures: " + e.getMessage());
        // e.printStackTrace(logger().getPrintWriter());
        e.printStackTrace();
    }
    System.exit(0);
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) ArrayList(java.util.ArrayList) Asset(io.xol.chunkstories.api.content.Asset) AssetHierarchy(io.xol.chunkstories.api.content.mods.AssetHierarchy) ImageReader(javax.imageio.ImageReader)

Example 20 with Asset

use of io.xol.chunkstories.api.content.Asset in project chunkstories by Hugobros3.

the class TexturesHandler method getTexture.

public static Texture2DGL getTexture(String name) {
    if (loadedTextures.containsKey(name)) {
        return loadedTextures.get(name);
    } else {
        if (name.startsWith("./")) {
            Asset asset = Client.getInstance().getContent().getAsset(name);
            if (asset == null)
                return nullTexture();
            Texture2DGL texture = new Texture2DAsset(asset);
            loadedTextures.put(name, texture);
            return texture;
        } else {
            // TODO check we are allowed to do this !
            File file = new File(name);
            if (file == null || !file.exists())
                return nullTexture();
            Texture2DGL texture = new Texture2DFile(file);
            loadedTextures.put(name, texture);
            return texture;
        }
    }
}
Also used : Asset(io.xol.chunkstories.api.content.Asset) File(java.io.File)

Aggregations

Asset (io.xol.chunkstories.api.content.Asset)22 IOException (java.io.IOException)6 AssetHierarchy (io.xol.chunkstories.api.content.mods.AssetHierarchy)5 File (java.io.File)3 ArrayList (java.util.ArrayList)3 IterableIterator (io.xol.chunkstories.api.util.IterableIterator)2 BufferedReader (java.io.BufferedReader)2 Iterator (java.util.Iterator)2 PNGDecoder (de.matthiasmann.twl.utils.PNGDecoder)1 ClientInterface (io.xol.chunkstories.api.client.ClientInterface)1 IllegalVoxelDeclarationException (io.xol.chunkstories.api.exceptions.content.IllegalVoxelDeclarationException)1 MeshLoadException (io.xol.chunkstories.api.exceptions.content.MeshLoadException)1 PluginLoadException (io.xol.chunkstories.api.exceptions.plugins.PluginLoadException)1 AnimatableMesh (io.xol.chunkstories.api.mesh.AnimatableMesh)1 Mesh (io.xol.chunkstories.api.mesh.Mesh)1 VoxelRenderer (io.xol.chunkstories.api.rendering.voxel.VoxelRenderer)1 ForeignCodeClassLoader (io.xol.chunkstories.content.sandbox.ForeignCodeClassLoader)1 NotAPluginException (io.xol.chunkstories.plugin.NotAPluginException)1 PluginInformationImplementation (io.xol.chunkstories.plugin.PluginInformationImplementation)1 BufferedImage (java.awt.image.BufferedImage)1