net.chris54721.infinitycubed.workers.AssetsWorker.java Source code

Java tutorial

Introduction

Here is the source code for net.chris54721.infinitycubed.workers.AssetsWorker.java

Source

/*
 * This file is part of the InfinityCubed Launcher source code.
 * Copyright (C) 2014 InfinityCubed Team.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.chris54721.infinitycubed.workers;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
import net.chris54721.infinitycubed.data.Asset;
import net.chris54721.infinitycubed.data.AssetIndex;
import net.chris54721.infinitycubed.data.AssetObject;
import net.chris54721.infinitycubed.data.Downloadable;
import net.chris54721.infinitycubed.utils.*;

import javax.swing.*;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AssetsWorker implements Runnable {

    private String minecraft;
    private List<Asset> assetList;
    private int totalAssets;
    private int currentAsset = 0;

    public AssetsWorker(String minecraft) {
        this.minecraft = minecraft;
    }

    public String getMinecraft() {
        return minecraft;
    }

    @Override
    public void run() {
        try {
            LogHelper.info("Downloading assets for Minecraft " + minecraft);
            assetList = new ArrayList<Asset>();
            URL indexUrl = Resources.getUrl(Resources.ResourceType.ASSET_INDEX, minecraft + ".json");
            File indexFile = Resources.getFile(Resources.ResourceType.ASSET_INDEX, minecraft + ".json");
            Downloadable indexDownloadable = new Downloadable(indexUrl, indexFile);
            indexDownloadable.setForce(false);
            if (indexDownloadable.download()) {
                String indexJson = Files.toString(indexFile, Charsets.UTF_8);
                AssetIndex index = Reference.ASSETS_GSON.fromJson(indexJson, AssetIndex.class);
                this.totalAssets = index.getObjects().entrySet().size();
                for (Map.Entry<String, AssetObject> entry : index.getObjects().entrySet()) {
                    String assetName = entry.getKey();
                    AssetObject object = entry.getValue();
                    String assetFileName = object.getHash().substring(0, 2) + "/" + object.getHash();
                    URL assetFileUrl = Resources.getUrl(Resources.ResourceType.ASSET_OBJECT, assetFileName);
                    File assetFile = Resources.getFile(Resources.ResourceType.ASSET_OBJECT, assetFileName);
                    Asset asset = new Asset(assetName, object);
                    if (!asset.isLocalValid())
                        assetList.add(asset);
                }
                LogHelper.info(totalAssets + " assets queued for download");
                DownloadJob assetDownloadJob = new DownloadJob(assetList);
                assetDownloadJob.setForceDownload(false);
                assetDownloadJob.setActionListener(new AssetProgressListener(this));
                assetDownloadJob.run();
            } else
                throw new NullPointerException("Couldn't download index file");
        } catch (Exception e) {
            LogHelper.error("Failed loading assets for Minecraft " + minecraft, e);
        }
    }

    public void update(long doneBytes, long totalBytes) {
        String assetName = assetList.get(currentAsset).getName();
        int percent = (int) Math.floor((float) doneBytes / (float) totalBytes * 100);
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                // TODO Refresh GUI
            }
        });
        if (percent >= 100)
            currentAsset++;
    }

}