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

Java tutorial

Introduction

Here is the source code for net.chris54721.infinitycubed.workers.PackLoader.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 com.google.gson.reflect.TypeToken;
import net.chris54721.infinitycubed.Launcher;
import net.chris54721.infinitycubed.data.Downloadable;
import net.chris54721.infinitycubed.data.ModPack;
import net.chris54721.infinitycubed.utils.LogHelper;
import net.chris54721.infinitycubed.utils.Reference;
import net.chris54721.infinitycubed.utils.Resources;
import net.chris54721.infinitycubed.utils.Utils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;

import java.io.File;
import java.lang.reflect.Type;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;

public class PackLoader implements Runnable {

    @Override
    public void run() {
        try {
            LogHelper.info("Loading and updating modpacks");
            String publicJson = Utils.toString(new URL(Reference.FILES_URL + "public.json"));
            Type stringIntMap = new TypeToken<LinkedHashMap<String, Integer>>() {
            }.getType();
            LinkedHashMap<String, Integer> publicObjects = Reference.DEFAULT_GSON.fromJson(publicJson,
                    stringIntMap);
            File localJson = new File(Resources.getFolder(Reference.PACKS_FOLDER), "public.json");
            List<String> updatePacks = new ArrayList<String>();
            if (localJson.isFile()) {
                String localPublicJson = Files.toString(localJson, Charsets.UTF_8);
                LinkedHashMap<String, Integer> localPublicObjects = Reference.DEFAULT_GSON.fromJson(localPublicJson,
                        stringIntMap);
                for (String pack : publicObjects.keySet()) {
                    if (!localPublicObjects.containsKey(pack)
                            || !localPublicObjects.get(pack).equals(publicObjects.get(pack))) {
                        updatePacks.add(pack);
                    }
                }
            } else
                updatePacks.addAll(publicObjects.keySet());
            Files.write(publicJson, localJson, Charsets.UTF_8);
            if (updatePacks.size() > 0) {
                for (String pack : updatePacks) {
                    LogHelper.info("Updating JSON file for modpack " + pack);
                    URL packJsonURL = Resources.getUrl(Resources.ResourceType.PACK_JSON, pack + ".json");
                    File packJsonFile = Resources.getFile(Resources.ResourceType.PACK_JSON, pack + ".json");
                    if (packJsonFile.isFile())
                        packJsonFile.delete();
                    Downloadable packJsonDownloadable = new Downloadable(packJsonURL, packJsonFile);
                    if (!packJsonDownloadable.download())
                        LogHelper.error("Failed updating JSON for modpack " + pack);
                }
            }
            Collection<File> packJsons = FileUtils.listFiles(Resources.getFolder(Reference.DATA_FOLDER),
                    new String[] { "json" }, false);
            for (File packJsonFile : packJsons)
                loadPack(FilenameUtils.getBaseName(packJsonFile.getName()));
        } catch (Exception e) {
            LogHelper.fatal("Failed updating and loading public packs", e);
        }
    }

    public static boolean loadPack(String id) {
        try {
            String packJson = Files.toString(Resources.getFile(Resources.ResourceType.PACK_JSON, id + ".json"),
                    Charsets.UTF_8);
            ModPack modPack = Reference.DEFAULT_GSON.fromJson(packJson, ModPack.class);
            modPack.loadImages();
            LogHelper.info("Loading modpack: " + modPack.getId());
            LogHelper.debug(ToStringBuilder.reflectionToString(modPack));
            Launcher.modpacks.add(modPack);
            if (modPack.getId().equalsIgnoreCase(Launcher.getSettings().getProperty("selectedPack")))
                selectPack(modPack);
            // if(Launcher.mainFrame != null) {
            //    TODO Refresh GUI
            // }
            return true;
        } catch (Exception e) {
            LogHelper.error("Failed loading modpack " + id, e);
            return false;
        }
    }

    public static boolean loadPrivatePack(String id) {
        try {
            URL packJsonURL = Resources.getUrl(Resources.ResourceType.PACK_JSON, id + ".json");
            File packJsonFile = Resources.getFile(Resources.ResourceType.PACK_JSON, id + ".json");
            Downloadable packJsonDownloadable = new Downloadable(packJsonURL, packJsonFile);
            packJsonDownloadable.setForce(false);
            if (packJsonDownloadable.download())
                return loadPack(id);
            else {
                FileUtils.deleteQuietly(packJsonFile);
                return false;
            }
        } catch (Exception e) {
            LogHelper.error("Failed loading private modpack " + id, e);
            return false;
        }
    }

    public static void selectPack(ModPack pack) {
        Launcher.selectedPack = pack;
        Launcher.getSettings().setProperty("selectedPack", pack.getId());
    }

    public static void selectPack(int index) {
        if (Launcher.modpacks.size() > index)
            selectPack(Launcher.modpacks.get(index));
    }

    public static void selectPack(String id) {
        for (ModPack pack : Launcher.modpacks)
            if (pack.getId().equalsIgnoreCase(id))
                selectPack(pack);
    }

}