001/*
002 * The FML Forge Mod Loader suite.
003 * Copyright (C) 2012 cpw
004 *
005 * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free
006 * Software Foundation; either version 2.1 of the License, or any later version.
007 *
008 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
009 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
010 *
011 * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51
012 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
013 */
014
015package cpw.mods.fml.common;
016
017import java.security.cert.Certificate;
018import java.util.Arrays;
019import java.util.Map;
020import java.util.Set;
021import java.util.logging.Level;
022
023import net.minecraft.nbt.NBTBase;
024import net.minecraft.nbt.NBTTagCompound;
025import net.minecraft.nbt.NBTTagList;
026import net.minecraft.world.storage.SaveHandler;
027import net.minecraft.world.storage.WorldInfo;
028
029import com.google.common.eventbus.EventBus;
030
031import cpw.mods.fml.common.registry.GameData;
032import cpw.mods.fml.common.registry.GameRegistry;
033import cpw.mods.fml.common.registry.ItemData;
034
035/**
036 * @author cpw
037 *
038 */
039public class FMLDummyContainer extends DummyModContainer implements WorldAccessContainer
040{
041    public FMLDummyContainer()
042    {
043        super(new ModMetadata());
044        ModMetadata meta = getMetadata();
045        meta.modId="FML";
046        meta.name="Forge Mod Loader";
047        meta.version=Loader.instance().getFMLVersionString();
048        meta.credits="Made possible with help from many people";
049        meta.authorList=Arrays.asList("cpw, LexManos");
050        meta.description="The Forge Mod Loader provides the ability for systems to load mods " +
051                    "from the file system. It also provides key capabilities for mods to be able " +
052                    "to cooperate and provide a good modding environment. " +
053                    "The mod loading system is compatible with ModLoader, all your ModLoader " +
054                    "mods should work.";
055        meta.url="https://github.com/MinecraftForge/FML/wiki";
056        meta.updateUrl="https://github.com/MinecraftForge/FML/wiki";
057        meta.screenshots=new String[0];
058        meta.logoFile="";
059    }
060
061    @Override
062    public boolean registerBus(EventBus bus, LoadController controller)
063    {
064        return true;
065    }
066
067    @Override
068    public NBTTagCompound getDataForWriting(SaveHandler handler, WorldInfo info)
069    {
070        NBTTagCompound fmlData = new NBTTagCompound();
071        NBTTagList list = new NBTTagList();
072        for (ModContainer mc : Loader.instance().getActiveModList())
073        {
074            NBTTagCompound mod = new NBTTagCompound();
075            mod.setString("ModId", mc.getModId());
076            mod.setString("ModVersion", mc.getVersion());
077            list.appendTag(mod);
078        }
079        fmlData.setTag("ModList", list);
080        NBTTagList itemList = new NBTTagList();
081        GameData.writeItemData(itemList);
082        fmlData.setTag("ModItemData", itemList);
083        return fmlData;
084    }
085
086    @Override
087    public void readData(SaveHandler handler, WorldInfo info, Map<String, NBTBase> propertyMap, NBTTagCompound tag)
088    {
089        if (tag.hasKey("ModList"))
090        {
091            NBTTagList modList = tag.getTagList("ModList");
092            for (int i = 0; i < modList.tagCount(); i++)
093            {
094                NBTTagCompound mod = (NBTTagCompound) modList.tagAt(i);
095                String modId = mod.getString("ModId");
096                String modVersion = mod.getString("ModVersion");
097                ModContainer container = Loader.instance().getIndexedModList().get(modId);
098                if (container == null)
099                {
100                    FMLLog.log("fml.ModTracker", Level.SEVERE, "This world was saved with mod %s which appears to be missing, things may not work well", modId);
101                    continue;
102                }
103                if (!modVersion.equals(container.getVersion()))
104                {
105                    FMLLog.log("fml.ModTracker", Level.INFO, "This world was saved with mod %s version %s and it is now at version %s, things may not work well", modId, modVersion, container.getVersion());
106                }
107            }
108        }
109        if (tag.hasKey("ModItemData"))
110        {
111            NBTTagList modList = tag.getTagList("ModItemData");
112            Set<ItemData> worldSaveItems = GameData.buildWorldItemData(modList);
113            GameData.validateWorldSave(worldSaveItems);
114        }
115        else
116        {
117            GameData.validateWorldSave(null);
118        }
119    }
120
121
122    @Override
123    public Certificate getSigningCertificate()
124    {
125        Certificate[] certificates = getClass().getProtectionDomain().getCodeSource().getCertificates();
126        return certificates != null ? certificates[0] : null;
127    }
128}