    001    package cpw.mods.fml.common.network;
    002
    003    import java.util.List;
    004
    005    import com.google.common.collect.ImmutableList;
    006    import com.google.common.collect.ImmutableList.Builder;
    007    import com.google.common.collect.Lists;
    008    import com.google.common.io.ByteArrayDataInput;
    009    import com.google.common.io.ByteArrayDataOutput;
    010    import com.google.common.io.ByteStreams;
    011
    012    import cpw.mods.fml.common.FMLCommonHandler;
    013    import cpw.mods.fml.common.Loader;
    014    import cpw.mods.fml.common.ModContainer;
    015    import cpw.mods.fml.common.versioning.ArtifactVersion;
    016    import cpw.mods.fml.common.versioning.DefaultArtifactVersion;
    017    import cpw.mods.fml.common.versioning.VersionRange;
    018
    019    import net.minecraft.network.INetworkManager;
    020    import net.minecraft.network.packet.NetHandler;
    021
    022    public class ModMissingPacket extends FMLPacket
    023    {
    024
    025        private List<ModData> missing;
    026        private List<ModData> badVersion;
    027
    028        public ModMissingPacket()
    029        {
    030            super(Type.MOD_MISSING);
    031        }
    032
    033        @Override
    034        public byte[] generatePacket(Object... data)
    035        {
    036            ByteArrayDataOutput dat = ByteStreams.newDataOutput();
    037
    038            List<String> missing = (List<String>) data[0];
    039            List<String> badVersion = (List<String>) data[1];
    040
    041            dat.writeInt(missing.size());
    042            for (String missed : missing)
    043            {
    044                ModContainer mc = Loader.instance().getIndexedModList().get(missed);
    045                dat.writeUTF(missed);
    046                dat.writeUTF(mc.getVersion());
    047            }
    048            dat.writeInt(badVersion.size());
    049            for (String bad : badVersion)
    050            {
    051                ModContainer mc = Loader.instance().getIndexedModList().get(bad);
    052                dat.writeUTF(bad);
    053                dat.writeUTF(mc.getVersion());
    054            }
    055            return dat.toByteArray();
    056        }
    057
    058        private static class ModData
    059        {
    060            String modId;
    061            String modVersion;
    062        }
    063        @Override
    064        public FMLPacket consumePacket(byte[] data)
    065        {
    066            ByteArrayDataInput dat = ByteStreams.newDataInput(data);
    067            int missingLen = dat.readInt();
    068            missing = Lists.newArrayListWithCapacity(missingLen);
    069            for (int i = 0; i < missingLen; i++)
    070            {
    071                ModData md = new ModData();
    072                md.modId = dat.readUTF();
    073                md.modVersion = dat.readUTF();
    074                missing.add(md);
    075            }
    076            int badVerLength = dat.readInt();
    077            badVersion = Lists.newArrayListWithCapacity(badVerLength);
    078            for (int i = 0; i < badVerLength; i++)
    079            {
    080                ModData md = new ModData();
    081                md.modId = dat.readUTF();
    082                md.modVersion = dat.readUTF();
    083                badVersion.add(md);
    084            }
    085            return this;
    086        }
    087
    088        @Override
    089        public void execute(INetworkManager network, FMLNetworkHandler handler, NetHandler netHandler, String userName)
    090        {
    091            FMLCommonHandler.instance().getSidedDelegate().displayMissingMods(this);
    092        }
    093
    094        public List<ArtifactVersion> getModList()
    095        {
    096            Builder<ArtifactVersion> builder = ImmutableList.<ArtifactVersion>builder();
    097            for (ModData md : missing)
    098            {
    099                builder.add(new DefaultArtifactVersion(md.modId, VersionRange.createFromVersion(md.modVersion, null)));
    100            }
    101            for (ModData md : badVersion)
    102            {
    103                builder.add(new DefaultArtifactVersion(md.modId, VersionRange.createFromVersion(md.modVersion, null)));
    104            }
    105            return builder.build();
    106        }
    107
    108    }