net.chris54721.infinitycubed.data.Asset.java Source code

Java tutorial

Introduction

Here is the source code for net.chris54721.infinitycubed.data.Asset.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.data;

import net.chris54721.infinitycubed.utils.LogHelper;
import net.chris54721.infinitycubed.utils.Resources;
import net.chris54721.infinitycubed.utils.Utils;
import org.apache.commons.io.FileUtils;

import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.URL;

public class Asset extends Downloadable {

    private String name;
    private File legacyFile;

    public Asset(String name, AssetObject obj) {
        super(getAssetUrl(obj), getAssetFile(obj), obj.getSize());
        this.setForce(false);
        this.name = name;
        this.legacyFile = Resources.getFile(Resources.ResourceType.ASSET_LEGACY, name);
    }

    public String getName() {
        return name;
    }

    public File getLegacyFile() {
        return legacyFile;
    }

    @Override
    public boolean isLocalValid() {
        return super.isLocalValid() && getLegacyFile().isFile() && getLegacyFile().length() == getFileSize();
    }

    @Override
    public boolean download() {
        return download(null);
    }

    @Override
    public boolean download(ActionListener listener) {
        return copyFromVanilla() || super.download(listener) && copyToLegacy();
    }

    public static URL getAssetUrl(AssetObject obj) {
        String objFileName = obj.getHash().substring(0, 2) + "/" + obj.getHash();
        return Resources.getUrl(Resources.ResourceType.ASSET_OBJECT, objFileName);
    }

    public static File getAssetFile(AssetObject obj) {
        String objFileName = obj.getHash().substring(0, 2) + "/" + obj.getHash();
        return Resources.getFile(Resources.ResourceType.ASSET_OBJECT, objFileName);
    }

    public boolean copyToLegacy() {
        try {
            legacyFile.getParentFile().mkdirs();
            FileUtils.copyFile(getTarget(), legacyFile);
            return isLocalValid();
        } catch (IOException e) {
            LogHelper.error("Failed copying asset " + name + " to legacy file", e);
            return false;
        }
    }

    public boolean copyFromVanilla() {
        try {
            if (this.isLocalValid())
                return true;
            else {
                File mcAssetsDir = new File(Utils.getAppdataPath() + File.separator + ".minecraft", "assets");
                if (!mcAssetsDir.isDirectory())
                    return false;
                File mcAssetsFile = new File(mcAssetsDir + File.separator + "objects" + File.separator
                        + getTarget().getParentFile().getName(), getTarget().getName());
                if (!mcAssetsFile.isFile() || mcAssetsFile.length() != getFileSize())
                    return false;
                FileUtils.copyFile(mcAssetsFile, getTarget());
                return this.copyToLegacy();
            }
        } catch (IOException e) {
            LogHelper.error("Failed copying asset " + name + " from vanilla", e);
            return false;
        }
    }

}