package crazypants.enderio.machine.hypercube; import java.io.File; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import net.minecraftforge.common.Configuration; import net.minecraftforge.common.DimensionManager; import net.minecraftforge.common.Property; import crazypants.enderio.Log; public class HyperCubeRegister { private static final String CATEGORY_PUBLIC_CHANNELS = "PublicChannels"; private static final String CATEGORY_PRIVATE_CHANNELS = "PrivateChannels"; private static final String KEY_PUBLIC_CHANNELS = "names"; public static HyperCubeRegister instance; private final Map> channelMapping = new HashMap>(); private final List publicChannels = new ArrayList(); private final List publicChannelsRO = Collections.unmodifiableList(publicChannels); private final Map> userChannels = new HashMap>(); //private Configuration config; private HyperCubeConfig conf; public static void load() { instance = new HyperCubeRegister(); instance.innerLoad(); } public static void unload() { instance = new HyperCubeRegister(); } private void innerLoad() { File f = DimensionManager.getCurrentSaveRootDirectory(); conf = new HyperCubeConfig(new File(DimensionManager.getCurrentSaveRootDirectory(), "enderio/dimensionalTransceiver.cfg")); File oldFile = new File(DimensionManager.getCurrentSaveRootDirectory(), "enderio/hypercubes.cfg"); if(oldFile.exists()) { convertOldFile(); } else { publicChannels.addAll(conf.getPublicChannels()); userChannels.putAll(conf.getUserChannels()); } } private void convertOldFile() { try { File oldFile = new File(DimensionManager.getCurrentSaveRootDirectory(), "enderio/hypercubes.cfg"); Configuration config = new Configuration(oldFile); config.load(); Property pcNamesProp = config.get(CATEGORY_PUBLIC_CHANNELS, "names", new String[] {}); String[] pcNames = pcNamesProp.getStringList(); if(pcNames != null) { for (String name : pcNames) { publicChannels.add(new Channel(name, null)); } } Property userNamesProp = config.get(CATEGORY_PRIVATE_CHANNELS, "users", new String[] {}); String[] userNames = userNamesProp.getStringList(); if(userNames != null) { for (String user : userNames) { Property userChannles = config.get(CATEGORY_PRIVATE_CHANNELS, user + ".channels", new String[] {}); String[] channelNames = userChannles.getStringList(); if(channelNames != null && channelNames.length > 0) { List channels = getChannelsForUser(user); for (String chanName : channelNames) { channels.add(new Channel(chanName, user)); } } } } updateConfig(); oldFile.renameTo(new File(DimensionManager.getCurrentSaveRootDirectory(), "/enderio/hypercubes.cfg.old")); } catch (Exception e) { Log.warn("Could not convert old Dimensional Transciever (hypercube.cfg) to new format. Channles will be reset."); } } public synchronized List getChannelsForUser(String user) { List result = userChannels.get(user); if(result == null) { result = new ArrayList(); userChannels.put(user, result); } return result; } public synchronized void register(TileHyperCube cube) { List cubes = innerGetCubesForChannel(cube.getChannel()); if(cubes != null && !cubes.contains(cube)) { cubes.add(cube); } } public synchronized void deregister(TileHyperCube cube, Channel channel) { List cubes = innerGetCubesForChannel(channel); if(cubes != null) { cubes.remove(cube); } } public synchronized void deregister(TileHyperCube cube) { List cubes = innerGetCubesForChannel(cube.getChannel()); if(cubes != null) { cubes.remove(cube); } } public synchronized List getCubesForChannel(Channel channel) { List chans = innerGetCubesForChannel(channel); if(chans == null) { return Collections.emptyList(); } return new ArrayList(chans); } public synchronized void addChannel(Channel channel) { if(channel == null || channel.name == null) { return; } if(channel.user == null) { if(!publicChannels.contains(channel)) { publicChannels.add(channel); updateConfig(); } } else { List channels = getChannelsForUser(channel.user); if(!channels.contains(channel)) { channels.add(channel); updateConfig(); } } } public void removeChannel(Channel channel) { if(channel == null || channel.name == null) { return; } if(channel.user == null) { if(publicChannels.contains(channel)) { publicChannels.remove(channel); updateConfig(); } } else { List channels = getChannelsForUser(channel.user); if(channels.contains(channel)) { channels.remove(channel); updateConfig(); } } } private void updateConfig() { if(conf == null) { Log.warn("HyperCubeRegister.updateConfig: Config was null."); return; } conf.setPublicChannels(publicChannels); conf.setUserChannels(userChannels); conf.save(); // if(config == null) { // Log.warn("HyperCubeRegister.updateConfig: Config was null."); // return; // } // //Configuration config = new Configuration(new File(DimensionManager.getCurrentSaveRootDirectory(), "/enderio/hypercubes.cfg")); // String[] publicNames = new String[publicChannels.size()]; // for (int i = 0; i < publicChannels.size(); i++) { // publicNames[i] = "\"" + publicChannels.get(i).name + "\""; // } // Property pcNamesProp = config.get(CATEGORY_PUBLIC_CHANNELS, "names", new String[] {}); // pcNamesProp.set(publicNames); // // Set users = userChannels.keySet(); // Property userNamesProp = config.get(CATEGORY_PRIVATE_CHANNELS, "users", new String[] {}); // userNamesProp.set(users == null ? new String[0] : users.toArray(new String[users.size()])); // // for (String user : users) { // Property userChansProp = config.get(CATEGORY_PRIVATE_CHANNELS, user + ".channels", new String[] {}); // List val = userChannels.get(user); // String[] channelNames; // if(val == null) { // channelNames = new String[0]; // } else { // channelNames = new String[val.size()]; // int i = 0; // for (Channel chan : val) { // channelNames[i] = chan.name; // ++i; // } // } // userChansProp.set(channelNames); // // } // // config.save(); } private List innerGetCubesForChannel(Channel channel) { if(channel == null) { return null; } List result = channelMapping.get(channel); if(result == null) { result = new ArrayList(); channelMapping.put(channel, result); } return result; } public List getPublicChannels() { return publicChannelsRO; } }