Skip to content

Commit

Permalink
don't write the profile in the file launcher_profiles.json if it is a…
Browse files Browse the repository at this point in the history
…lready in and if it is valid
  • Loading branch information
robin4002 committed Jun 4, 2016
1 parent 50982c3 commit 799b5ca
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 37 deletions.
92 changes: 57 additions & 35 deletions src/main/java/fr/minecraftforgefrance/common/ProcessInstall.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,14 @@ public class ProcessInstall

private final FileChecker fileChecker;
private final IInstallRunner runner;
private final boolean update;
private final String preset;

private static final JsonFormatter JSON_FORMATTER = new PrettyJsonFormatter();

public ProcessInstall(FileChecker file, IInstallRunner runner, boolean update, File mcDir, String preset)
public ProcessInstall(FileChecker file, IInstallRunner runner, File mcDir, String preset)
{
this.fileChecker = file;
this.runner = runner;
this.update = update;
this.mcDir = mcDir;
this.modPackDir = new File(new File(mcDir, "modpacks"), RemoteInfoReader.instance().getModPackName());
this.preset = preset;
Expand Down Expand Up @@ -416,15 +414,14 @@ public void finish()
this.frame.setTitle(LANG.getTranslation("misc.finishing"));
this.createOrUpdateProfile();
this.writeModPackInfo();
if(!this.update)
{
this.addToProfileList();
}

this.addToProfileList();
this.frame.dispose();
this.runner.onFinish();
}

/**
* create or update the file modpackName.json in the folder .minecraft/profile/modpackName
*/
private void createOrUpdateProfile()
{
String modpackName = RemoteInfoReader.instance().getModPackName();
Expand All @@ -449,18 +446,40 @@ private void createOrUpdateProfile()
}
}

public void addToProfileList()
/**
* determine if the profile exist and if it is valid in the file launcher_profiles.json
* @param profiles JsonRootNode of the file launcher_profiles.json
* @param modpackName name of the modpack
* @param displayName display name of the modpack
* @return true if the profile exist and is valid
*/
private boolean isProfileValid(JsonRootNode profiles, String modpackName, String displayName)
{
if(profiles.isObjectNode("profiles", displayName))
{
if(profiles.isStringValue("profiles", displayName, "name") && profiles.getStringValue("profiles", displayName, "name").equals(displayName))
{
return profiles.getStringValue("profiles", displayName, "lastVersionId").equals(modpackName);
}
}
return false;
}

/**
* add the profile in the file launcher_profiles.json
*/
private void addToProfileList()
{
String modpackName = RemoteInfoReader.instance().getModPackName();
String displayName = RemoteInfoReader.instance().getModPackDisplayName();
File launcherProfiles = new File(mcDir, "launcher_profiles.json");
JdomParser parser = new JdomParser();
JsonRootNode jsonProfileData;
if(!launcherProfiles.exists())
{
JOptionPane.showMessageDialog(null, LANG.getTranslation("err.mcprofilemissing"), LANG.getTranslation("misc.error"), JOptionPane.ERROR_MESSAGE);
this.frame.dispose();
}
JdomParser parser = new JdomParser();
JsonRootNode jsonProfileData;

try
{
jsonProfileData = parser.parse(Files.newReader(launcherProfiles, Charsets.UTF_8));
Expand All @@ -475,34 +494,37 @@ public void addToProfileList()
throw Throwables.propagate(e);
}

JsonField[] fields = null;
if(RemoteInfoReader.instance().hasArgument())
{
fields = new JsonField[] {JsonNodeFactories.field("name", JsonNodeFactories.string(RemoteInfoReader.instance().getModPackDisplayName())), JsonNodeFactories.field("lastVersionId", JsonNodeFactories.string(modpackName)), JsonNodeFactories.field("javaArgs", JsonNodeFactories.string(RemoteInfoReader.instance().getArgument()))};
}
else
if(!isProfileValid(jsonProfileData, modpackName, displayName))
{
fields = new JsonField[] {JsonNodeFactories.field("name", JsonNodeFactories.string(RemoteInfoReader.instance().getModPackDisplayName())), JsonNodeFactories.field("lastVersionId", JsonNodeFactories.string(modpackName))};
}
JsonField[] fields = null;
if(RemoteInfoReader.instance().hasArgument())
{
fields = new JsonField[] {JsonNodeFactories.field("name", JsonNodeFactories.string(displayName)), JsonNodeFactories.field("lastVersionId", JsonNodeFactories.string(modpackName)), JsonNodeFactories.field("javaArgs", JsonNodeFactories.string(RemoteInfoReader.instance().getArgument()))};
}
else
{
fields = new JsonField[] {JsonNodeFactories.field("name", JsonNodeFactories.string(displayName)), JsonNodeFactories.field("lastVersionId", JsonNodeFactories.string(modpackName))};
}

HashMap<JsonStringNode, JsonNode> profileCopy = Maps.newHashMap(jsonProfileData.getNode("profiles").getFields());
HashMap<JsonStringNode, JsonNode> rootCopy = Maps.newHashMap(jsonProfileData.getFields());
profileCopy.put(JsonNodeFactories.string(RemoteInfoReader.instance().getModPackDisplayName()), JsonNodeFactories.object(fields));
JsonRootNode profileJsonCopy = JsonNodeFactories.object(profileCopy);
HashMap<JsonStringNode, JsonNode> profileCopy = Maps.newHashMap(jsonProfileData.getNode("profiles").getFields());
HashMap<JsonStringNode, JsonNode> rootCopy = Maps.newHashMap(jsonProfileData.getFields());
profileCopy.put(JsonNodeFactories.string(displayName), JsonNodeFactories.object(fields));
JsonRootNode profileJsonCopy = JsonNodeFactories.object(profileCopy);

rootCopy.put(JsonNodeFactories.string("profiles"), profileJsonCopy);
rootCopy.put(JsonNodeFactories.string("profiles"), profileJsonCopy);

jsonProfileData = JsonNodeFactories.object(rootCopy);
jsonProfileData = JsonNodeFactories.object(rootCopy);

try
{
BufferedWriter newWriter = Files.newWriter(launcherProfiles, Charsets.UTF_8);
JSON_FORMATTER.format(jsonProfileData, newWriter);
newWriter.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, LANG.getTranslation("err.cannotwriteprofile"), LANG.getTranslation("misc.error"), JOptionPane.ERROR_MESSAGE);
try
{
BufferedWriter newWriter = Files.newWriter(launcherProfiles, Charsets.UTF_8);
JSON_FORMATTER.format(jsonProfileData, newWriter);
newWriter.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, LANG.getTranslation("err.cannotwriteprofile"), LANG.getTranslation("misc.error"), JOptionPane.ERROR_MESSAGE);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void actionPerformed(ActionEvent e)
{
InstallerFrame.this.dispose();
FileChecker checker = new FileChecker(InstallerFrame.this.mcDir);
ProcessInstall install = new ProcessInstall(checker, InstallerFrame.this, false, InstallerFrame.this.mcDir, InstallerFrame.this.preSet);
ProcessInstall install = new ProcessInstall(checker, InstallerFrame.this, InstallerFrame.this.mcDir, InstallerFrame.this.preSet);
install.run();
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/fr/minecraftforgefrance/updater/Updater.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public Updater(String[] args)
{
e.printStackTrace();
}
ProcessInstall install = new ProcessInstall(checker, this, true, mcDir, null);
ProcessInstall install = new ProcessInstall(checker, this, mcDir, null);
install.run();
}
}
Expand Down

0 comments on commit 799b5ca

Please sign in to comment.