Search in sources :

Example 11 with Mod

use of mod.Mod in project ParadoxosModManager by ThibautSF.

the class ListManager method applyOneModListV2.

/**
 * @param applyList
 * @return
 * @throws IOException
 */
private boolean applyOneModListV2(ModList applyList) throws IOException {
    List<Mod> applyMods = applyList.getModlist();
    String sep = File.separator;
    // Clean customModFiles
    deleteCustomModFiles();
    if (applyList.isCustomOrder()) {
        // Generate .mod files with custom name
        generateCustomModFiles(applyMods);
    } else {
        // Sort list to ASCII order before apply
        Collections.sort(applyMods, new Comparator<Mod>() {

            @Override
            public int compare(Mod m1, Mod m2) {
                return m1.getName().compareTo(m2.getName());
            }
        });
    }
    Collections.reverse(applyMods);
    File inputFile = new File(ModManager.PATH + sep + ModManager.ACTMOD_FILE);
    File tempFile = new File(ModManager.PATH + sep + ModManager.ACTMOD_FILE + ".tmp");
    FileReader fileReader = new FileReader(inputFile);
    FileWriter fileWriter = new FileWriter(tempFile);
    Gson gson = new Gson();
    JsonObject json = new JsonObject();
    json = gson.fromJson(fileReader, JsonObject.class);
    /*
		if(!json.containsKey("disabled_dlcs"))
			json.put("disabled_dlcs", new String[0]);
		*/
    JsonArray enabled_mods = new JsonArray();
    String modfolder = "mod/";
    // if(!(modfolder.lastIndexOf("/")==modfolder.length()-1)) modfolder+="/";
    String prefix = "";
    if (applyList.isCustomOrder())
        prefix = "pmm_";
    for (Mod mod : applyMods) {
        String modpath = modfolder + prefix + mod.getFileName();
        enabled_mods.add(modpath);
    }
    json.add("enabled_mods", enabled_mods);
    try {
        fileWriter.write(json.toString());
    } catch (IOException e) {
        ErrorPrint.printError(e, "When writing json");
        e.printStackTrace();
    }
    fileReader.close();
    fileWriter.close();
    inputFile.delete();
    boolean successful = tempFile.renameTo(inputFile);
    return successful;
}
Also used : Mod(mod.Mod) FileWriter(java.io.FileWriter) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) JsonArray(com.google.gson.JsonArray) FileReader(java.io.FileReader) File(java.io.File)

Example 12 with Mod

use of mod.Mod in project ParadoxosModManager by ThibautSF.

the class MyXML method getSavedList.

/**
 * @return
 */
public List<ModList> getSavedList(Map<String, Mod> availableMods) {
    List<ModList> userLists = new ArrayList<ModList>();
    List<Element> modLists = root.getChildren(LIST);
    Iterator<Element> i = modLists.iterator();
    while (i.hasNext()) {
        List<Mod> listMods = new ArrayList<Mod>();
        Map<Integer, Mod> sortedMods = new TreeMap<Integer, Mod>();
        List<Mod> unsortedMods = new ArrayList<Mod>();
        Element oneListElement = (Element) i.next();
        String listName = oneListElement.getAttribute(NAME).getValue();
        boolean listCustomOrder = false;
        try {
            Attribute customOrderAttribute = oneListElement.getAttribute(CUSTOM_ORDER);
            if (customOrderAttribute != null) {
                listCustomOrder = customOrderAttribute.getBooleanValue();
            }
        } catch (DataConversionException e) {
        // Bad value
        }
        String listDescr = "";
        Element listDescrElement = oneListElement.getChild(DESCR);
        if (listDescrElement != null)
            listDescr = listDescrElement.getText();
        String launchArgs = "";
        Element listArgsElement = oneListElement.getChild(LAUNCHARGS);
        if (listArgsElement != null)
            launchArgs = listArgsElement.getText();
        String listLang = null;
        Element listLangElement = oneListElement.getChild(LANG);
        if (listLangElement != null)
            listLang = listLangElement.getText();
        List<Element> modsElements = oneListElement.getChildren(MOD);
        for (Element modElement : modsElements) {
            List<Attribute> modElementAttr = modElement.getAttributes();
            String fileName = "", modName = "", remoteFileId = null;
            int modOrder = -1;
            for (Attribute attribute : modElementAttr) {
                switch(attribute.getName()) {
                    case ID:
                    case FILE_NAME:
                        fileName = attribute.getValue();
                        break;
                    case MOD_NAME:
                        modName = attribute.getValue();
                        break;
                    case REMOTE_ID:
                        remoteFileId = attribute.getValue();
                        break;
                    case MOD_ORDER:
                        try {
                            modOrder = attribute.getIntValue();
                        } catch (DataConversionException e) {
                            ErrorPrint.printError(e, "When reading mod order attribute (import)");
                            e.printStackTrace();
                        }
                        break;
                    default:
                        break;
                }
            }
            Mod oneMod = availableMods.get(fileName);
            if (oneMod == null) {
                oneMod = new Mod(modName, fileName, remoteFileId);
                if (remoteFileId != null && !"".equals(remoteFileId)) {
                    for (Mod mod : availableMods.values()) {
                        if (mod.getRemoteFileID().equals(remoteFileId)) {
                            oneMod = mod;
                        }
                    }
                }
            }
            if (!unsortedMods.contains(oneMod) && !sortedMods.values().contains(oneMod)) {
                if (modOrder >= 0) {
                    sortedMods.put(modOrder, oneMod);
                } else {
                    unsortedMods.add(oneMod);
                }
            }
        }
        // Append mods with order value
        listMods.addAll(sortedMods.values());
        // Append mods without order value at the end of the list
        Collections.sort(unsortedMods, new Comparator<Mod>() {

            @Override
            public int compare(Mod m1, Mod m2) {
                return m1.getName().compareTo(m2.getName());
            }
        });
        listMods.addAll(unsortedMods);
        ModList oneList = new ModList(listName, listDescr, Languages.getLanguage(listLang), listMods, listCustomOrder, launchArgs);
        userLists.add(oneList);
    }
    return userLists;
}
Also used : Mod(mod.Mod) Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) ErrorPrint(debug.ErrorPrint) ModList(mod.ModList) DataConversionException(org.jdom2.DataConversionException)

Example 13 with Mod

use of mod.Mod in project ParadoxosModManager by ThibautSF.

the class MyXML method importList.

public String importList(String xml, Map<String, Mod> availableMods) throws Exception {
    SAXBuilder sxb = new SAXBuilder();
    Document importDocument = sxb.build(xml);
    Element importRoot = importDocument.getRootElement();
    if (importRoot.getAttribute(GAME_ID).getValue().equals(ModManager.STEAM_ID.toString())) {
        List<Element> modLists = importRoot.getChildren(LIST);
        Iterator<Element> i = modLists.iterator();
        while (i.hasNext()) {
            List<Mod> listMods = new ArrayList<Mod>();
            Map<Integer, Mod> sortedMods = new TreeMap<Integer, Mod>();
            List<Mod> unsortedMods = new ArrayList<Mod>();
            Element oneListElement = (Element) i.next();
            String listName = oneListElement.getAttribute(NAME).getValue();
            boolean listCustomOrder = false;
            try {
                Attribute customOrderAttribute = oneListElement.getAttribute(CUSTOM_ORDER);
                if (customOrderAttribute != null) {
                    listCustomOrder = customOrderAttribute.getBooleanValue();
                }
            } catch (DataConversionException e) {
            // Bad value
            }
            String listDescr = "";
            Element listDescrElement = oneListElement.getChild(DESCR);
            if (listDescrElement != null)
                listDescr = listDescrElement.getText();
            String launchArgs = "";
            Element listArgsElement = oneListElement.getChild(LAUNCHARGS);
            if (listArgsElement != null)
                launchArgs = listArgsElement.getText();
            String listLang = null;
            Element listLangElement = oneListElement.getChild(LANG);
            if (listLangElement != null)
                listLang = listLangElement.getText();
            List<Element> modsElements = oneListElement.getChildren(MOD);
            for (Element modElement : modsElements) {
                List<Attribute> modElementAttr = modElement.getAttributes();
                String fileName = "", modName = "", remoteFileId = null;
                int modOrder = -1;
                for (Attribute attribute : modElementAttr) {
                    switch(attribute.getName()) {
                        case ID:
                        case FILE_NAME:
                            fileName = attribute.getValue();
                            break;
                        case MOD_NAME:
                            modName = attribute.getValue();
                            break;
                        case REMOTE_ID:
                            remoteFileId = attribute.getValue();
                            break;
                        case MOD_ORDER:
                            try {
                                modOrder = attribute.getIntValue();
                            } catch (DataConversionException e) {
                                ErrorPrint.printError(e, "When reading mod order attribute (import)");
                                e.printStackTrace();
                            }
                            break;
                        default:
                            break;
                    }
                }
                Mod oneMod = availableMods.get(fileName);
                if (oneMod == null) {
                    oneMod = new Mod(modName, fileName, remoteFileId);
                    if (remoteFileId != null && !"".equals(remoteFileId)) {
                        for (Mod mod : availableMods.values()) {
                            if (mod.getRemoteFileID().equals(remoteFileId)) {
                                oneMod = mod;
                            }
                        }
                    }
                }
                if (!unsortedMods.contains(oneMod) && !sortedMods.values().contains(oneMod)) {
                    if (modOrder >= 0) {
                        sortedMods.put(modOrder, oneMod);
                    } else {
                        unsortedMods.add(oneMod);
                    }
                }
            }
            // Append mods with order value
            listMods.addAll(sortedMods.values());
            // Append mods without order value at the end of the list
            Collections.sort(unsortedMods, new Comparator<Mod>() {

                @Override
                public int compare(Mod m1, Mod m2) {
                    return m1.getName().compareTo(m2.getName());
                }
            });
            listMods.addAll(unsortedMods);
            ModList oneList = new ModList("[Imported]" + listName + "_" + System.currentTimeMillis(), listDescr, Languages.getLanguage(listLang), listMods, listCustomOrder, launchArgs);
            modifyList(oneList);
        }
        return "Import done.";
    }
    return "Import procedure aborted, this list is not for the current game !";
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) Mod(mod.Mod) Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) Document(org.jdom2.Document) TreeMap(java.util.TreeMap) ErrorPrint(debug.ErrorPrint) ModList(mod.ModList) DataConversionException(org.jdom2.DataConversionException)

Example 14 with Mod

use of mod.Mod in project ParadoxosModManager by ThibautSF.

the class TestMod method modifiedFilestest.

@Test
public void modifiedFilestest() {
    Mod mod = new Mod(modName, true);
    Set<String> modifiedFiles = mod.getModifiedFiles();
    Assert.assertEquals(7, modifiedFiles.size());
    Assert.assertTrue(modifiedFiles.contains("gfx\\interface\\player_counters_toggle.dds"));
    Assert.assertTrue(modifiedFiles.contains("gfx\\interface\\radar_toggle.dds"));
    Assert.assertTrue(modifiedFiles.contains("gfx\\texticons\\air_experience_20x20.dds"));
    Assert.assertTrue(modifiedFiles.contains("gfx\\texticons\\army_experience_20x20.dds"));
    Assert.assertTrue(modifiedFiles.contains("gfx\\texticons\\navy_experience_20x20.dds"));
    Assert.assertTrue(modifiedFiles.contains("localisation\\additional.yml"));
    Assert.assertTrue(modifiedFiles.contains("localisation\\additional_l_french.yml"));
}
Also used : Mod(mod.Mod) Test(org.junit.Test)

Aggregations

Mod (mod.Mod)14 ErrorPrint (debug.ErrorPrint)5 File (java.io.File)5 FileReader (java.io.FileReader)4 FileWriter (java.io.FileWriter)4 BufferedReader (java.io.BufferedReader)3 BufferedWriter (java.io.BufferedWriter)3 IOException (java.io.IOException)3 Element (org.jdom2.Element)3 FilenameFilter (java.io.FilenameFilter)2 ArrayList (java.util.ArrayList)2 TreeMap (java.util.TreeMap)2 ModList (mod.ModList)2 Attribute (org.jdom2.Attribute)2 DataConversionException (org.jdom2.DataConversionException)2 Gson (com.google.gson.Gson)1 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 FileNotFoundException (java.io.FileNotFoundException)1 URISyntaxException (java.net.URISyntaxException)1