    001/*
    002 * Forge Mod Loader
    003 * Copyright (c) 2012-2013 cpw.
    004 * All rights reserved. This program and the accompanying materials
    005 * are made available under the terms of the GNU Lesser Public License v2.1
    006 * which accompanies this distribution, and is available at
    007 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
    008 *
    009 * Contributors:
    010 *     cpw - implementation
    011 */
    012
    013package cpw.mods.fml.common.network;
    014
    015import java.util.List;
    016
    017import com.google.common.collect.ImmutableList;
    018import com.google.common.collect.ImmutableList.Builder;
    019import com.google.common.collect.Lists;
    020import com.google.common.io.ByteArrayDataInput;
    021import com.google.common.io.ByteArrayDataOutput;
    022import com.google.common.io.ByteStreams;
    023
    024import cpw.mods.fml.common.FMLCommonHandler;
    025import cpw.mods.fml.common.Loader;
    026import cpw.mods.fml.common.ModContainer;
    027import cpw.mods.fml.common.versioning.ArtifactVersion;
    028import cpw.mods.fml.common.versioning.DefaultArtifactVersion;
    029import cpw.mods.fml.common.versioning.VersionRange;
    030
    031import net.minecraft.network.INetworkManager;
    032import net.minecraft.network.packet.NetHandler;
    033
    034public class ModMissingPacket extends FMLPacket
    035{
    036
    037    private List<ModData> missing;
    038    private List<ModData> badVersion;
    039
    040    public ModMissingPacket()
    041    {
    042        super(Type.MOD_MISSING);
    043    }
    044
    045    @Override
    046    public byte[] generatePacket(Object... data)
    047    {
    048        ByteArrayDataOutput dat = ByteStreams.newDataOutput();
    049
    050        List<String> missing = (List<String>) data[0];
    051        List<String> badVersion = (List<String>) data[1];
    052
    053        dat.writeInt(missing.size());
    054        for (String missed : missing)
    055        {
    056            ModContainer mc = Loader.instance().getIndexedModList().get(missed);
    057            dat.writeUTF(missed);
    058            dat.writeUTF(mc.getVersion());
    059        }
    060        dat.writeInt(badVersion.size());
    061        for (String bad : badVersion)
    062        {
    063            ModContainer mc = Loader.instance().getIndexedModList().get(bad);
    064            dat.writeUTF(bad);
    065            dat.writeUTF(mc.getVersion());
    066        }
    067        return dat.toByteArray();
    068    }
    069
    070    private static class ModData
    071    {
    072        String modId;
    073        String modVersion;
    074    }
    075    @Override
    076    public FMLPacket consumePacket(byte[] data)
    077    {
    078        ByteArrayDataInput dat = ByteStreams.newDataInput(data);
    079        int missingLen = dat.readInt();
    080        missing = Lists.newArrayListWithCapacity(missingLen);
    081        for (int i = 0; i < missingLen; i++)
    082        {
    083            ModData md = new ModData();
    084            md.modId = dat.readUTF();
    085            md.modVersion = dat.readUTF();
    086            missing.add(md);
    087        }
    088        int badVerLength = dat.readInt();
    089        badVersion = Lists.newArrayListWithCapacity(badVerLength);
    090        for (int i = 0; i < badVerLength; i++)
    091        {
    092            ModData md = new ModData();
    093            md.modId = dat.readUTF();
    094            md.modVersion = dat.readUTF();
    095            badVersion.add(md);
    096        }
    097        return this;
    098    }
    099
    100    @Override
    101    public void execute(INetworkManager network, FMLNetworkHandler handler, NetHandler netHandler, String userName)
    102    {
    103        FMLCommonHandler.instance().getSidedDelegate().displayMissingMods(this);
    104    }
    105
    106    public List<ArtifactVersion> getModList()
    107    {
    108        Builder<ArtifactVersion> builder = ImmutableList.<ArtifactVersion>builder();
    109        for (ModData md : missing)
    110        {
    111            builder.add(new DefaultArtifactVersion(md.modId, VersionRange.createFromVersion(md.modVersion, null)));
    112        }
    113        for (ModData md : badVersion)
    114        {
    115            builder.add(new DefaultArtifactVersion(md.modId, VersionRange.createFromVersion(md.modVersion, null)));
    116        }
    117        return builder.build();
    118    }
    119
    120}