fr.ironcraft.assets.Assets.java Source code

Java tutorial

Introduction

Here is the source code for fr.ironcraft.assets.Assets.java

Source

/*
 * Copyright (C) 2014 Thog92
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.ironcraft.assets;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.Proxy;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;

/**
 * @author Thog
 */
public class Assets {

    private final File path;
    private final List<FileDownloader> queue;
    private final Gson gson;
    private String indexName;
    private AssetIndex index;
    private File objectsFolder;
    private File indexesFolder;
    private File virtualFolder;
    private File legacyFolder;

    /**
     * @param t Name of the index of assets
     * @param p   Download location
     */
    public Assets(String t, File p) {
        this.gson = new Gson();
        this.queue = new ArrayList<FileDownloader>();
        this.indexName = t;
        this.path = p;
        this.getResourceFiles();

    }

    /**
     * Download and convert assets if necessary
     */
    public void downloadAssets() {
        for (FileDownloader todown : queue) {
            todown.downloadFile();
        }
        if (this.indexName.equals("legacy")) {
            for (Entry<String, AssetObject> entry : index.getFileMap().entrySet()) {
                File target = new File(legacyFolder, entry.getKey());
                File original = new File(new File(objectsFolder, entry.getValue().getHash().substring(0, 2)),
                        entry.getValue().getHash());
                if (!target.isFile()) {
                    try {
                        FileUtils.copyFile(original, target, false);
                    } catch (IOException e) {
                    }

                }
            }

        }
    }

    private void getResourceFiles() {

        Proxy proxy = Proxy.NO_PROXY;
        InputStream inputStream = null;
        File assets = new File(path, "assets");
        objectsFolder = new File(assets, "objects");
        indexesFolder = new File(assets, "indexes");
        virtualFolder = new File(assets, "virtual");
        legacyFolder = new File(virtualFolder, "legacy");
        if (indexName == null) {
            indexName = "legacy";
        }
        File indexFile = new File(indexesFolder, indexName + ".json");

        try {
            URL indexUrl = new URL("https://s3.amazonaws.com/Minecraft.Download/indexes/" + indexName + ".json");
            inputStream = indexUrl.openConnection(proxy).getInputStream();
            String json = IOUtils.toString(inputStream);

            FileUtils.writeStringToFile(indexFile, json);
            index = gson.fromJson(json, AssetIndex.class);

            for (AssetObject object : index.getUniqueObjects()) {

                String filename = object.getHash().substring(0, 2) + "/" + object.getHash();

                File file = new File(objectsFolder, filename);
                if ((!file.isFile()) || (FileUtils.sizeOf(file) != object.getSize())) {
                    FileDownloader downloadable = new FileDownloader(
                            new URL("http://resources.download.minecraft.net/" + filename), file, object.getSize());

                    //downloadable.setExpectedSize(object.getSize());
                    queue.add(downloadable);

                }

            }

        } catch (IOException ex) {

        } catch (JsonSyntaxException ex) {

        } finally {
            IOUtils.closeQuietly(inputStream);
        }

    }
}