Skip to content

Commit

Permalink
Add option to keep comments as _comment
Browse files Browse the repository at this point in the history
  • Loading branch information
ichttt committed Dec 25, 2018
1 parent e6e9c87 commit 990cf5c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
28 changes: 20 additions & 8 deletions src/main/java/ichttt/mclang2json/Lang2JsonConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.nio.file.Paths;

public class Lang2JsonConverter {
public static final String VERSION = "1.4";
public static final String VERSION = "1.5";
public enum FileParseResult {
SUCCESS, NO_LANG_FILES, ABORT, ERRORS
}
Expand All @@ -21,7 +21,7 @@ public static void main(String[] args) {
LangConverterGui.init();
}

public static FileParseResult parseFolder(JFrame parent) {
public static FileParseResult parseFolder(JFrame parent, boolean keepComments) {
JFileChooser chooser = new JFileChooser(prevPath.toFile());
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
Expand All @@ -33,15 +33,17 @@ public static FileParseResult parseFolder(JFrame parent) {
File[] files = dir.listFiles();
if (files == null || files.length == 0)
return FileParseResult.NO_LANG_FILES;
System.out.println("Starting conversion of folder " + dir.getName() + "...");
boolean didRun = false;
boolean errors = false;
for (File f : files) {
String fileName = f.toString();
if (!fileName.endsWith(".lang"))
continue;
Path output = getOutput(f);
System.out.println("Converting " + f.getName() + "...");
try {
parse(f, output.toFile());
parse(f, output.toFile(), keepComments);
} catch (IOException e) {
System.err.println("Could not convert file: IO error");
e.printStackTrace();
Expand All @@ -63,7 +65,7 @@ public static FileParseResult parseFolder(JFrame parent) {
return FileParseResult.ABORT;
}

public static String parseFile(JFrame parent) throws IOException {
public static String parseFile(JFrame parent, boolean keepComments) throws IOException {
JFileChooser chooser = new JFileChooser(prevPath.toFile());
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setFileFilter(new FileFilter() {
Expand All @@ -81,7 +83,8 @@ public String getDescription() {
if (chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
Path output = getOutput(file);
parse(file, output.toFile());
System.out.println("Converting " + file.getName() + "...");
parse(file, output.toFile(), keepComments);
return output.toString();
}
return null;
Expand All @@ -100,7 +103,7 @@ private static Path getOutput(File file) {
return outputPath;
}

private static void parse(File file, File output) throws IOException {
private static void parse(File file, File output, boolean keepComments) throws IOException {
BufferedReader reader = null;
JsonWriter writer = null;
try {
Expand All @@ -125,14 +128,23 @@ private static void parse(File file, File output) throws IOException {
line = line.substring(1);
}
if (line.trim().charAt(0) == '#') {
System.out.println("WARNING: Cannot convert comment " + line + ", skipping!");
if (keepComments) {
System.out.println("Remapping comment line " + line);
writer.name("_comment").value(line.substring(line.indexOf('#')));
} else {
System.out.println("Removing comment line " + line);
}
continue;
}

if (split.length < 2)
throw new RuntimeException("Invalid line " + line);

String key = remapKey(split[0]);
if (key == null)
key = split[0];
else
System.out.println("Remapping key " + split[0] + " to " + split[1]);
StringBuilder value = new StringBuilder();
for (int i = 1; i < split.length; i++)
value.append(split[i]);
Expand All @@ -157,7 +169,7 @@ private static String remapKey(String key) {
} else if (key.startsWith("tile.") && key.endsWith(".name")) {
return "block".concat(key.substring("tile".length(), key.length() - ".name".length()));
}
return key;
return null;
}

private static void tryClose(Closeable closeable) {
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/ichttt/mclang2json/LangConverterGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class LangConverterGui implements ActionListener {
private static final JComboBox<String> intents = setupBase(new JComboBox<>(ALLOWED_INTENT));
private static final JButton convertFolder = setupButton("convert folder");
private static final JButton convertFile = setupButton("convert file");
private static final JCheckBox keepComment = setupBase(new JCheckBox("Keep Comments"));

public static void init() {
panel.setLayout(new GridBagLayout());
Expand Down Expand Up @@ -42,6 +43,14 @@ public static void init() {
layout.gridx = 2;
panel.add(intents, layout);

layout.gridy = 3;
layout.gridx = 1;
layout.weighty = 0.2;
layout.gridwidth = 2;
keepComment.setSelected(true);
keepComment.setToolTipText("Converts lang file comments (lines starting with #) to json with key _comment.\nThis can produce jsons with duplicate _comment keys");
panel.add(keepComment, layout);

frame.setMinimumSize(new Dimension(160, 100));
frame.setPreferredSize(new Dimension(480, 270));
frame.pack();
Expand Down Expand Up @@ -77,7 +86,7 @@ public static String getIntent() {
@Override
public void actionPerformed(ActionEvent event) {
if (event.getSource() == convertFolder) {
Lang2JsonConverter.FileParseResult result = Lang2JsonConverter.parseFolder(frame);
Lang2JsonConverter.FileParseResult result = Lang2JsonConverter.parseFolder(frame, keepComment.isSelected());
switch (result) {
case SUCCESS:
JOptionPane.showMessageDialog(frame, "Successfully converted all files in the folder " + Lang2JsonConverter.prevPath);
Expand All @@ -86,22 +95,23 @@ public void actionPerformed(ActionEvent event) {
JOptionPane.showMessageDialog(frame, "Could not find any lang files in the folder " + Lang2JsonConverter.prevPath, "Could not convert", JOptionPane.WARNING_MESSAGE);
break;
case ABORT:
System.out.println("Canceling");
// JOptionPane.showMessageDialog(frame, "Action cancelled as no directory has been chosen");
break;
case ERRORS:
JOptionPane.showMessageDialog(frame, "There were errors parsing some files. Please see the log for more information", "Could not convert", JOptionPane.ERROR_MESSAGE);
break;
}
} else if (event.getSource() == convertFile) {
String newFileName = saveParseFile();
String newFileName = safeParseFile();
if (newFileName != null)
JOptionPane.showMessageDialog(frame, "Successfully created " + newFileName);
}
}

private static String saveParseFile() {
private static String safeParseFile() {
try {
return Lang2JsonConverter.parseFile(frame);
return Lang2JsonConverter.parseFile(frame, keepComment.isSelected());
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(frame, "Could not convert file: IO error" +
Expand Down

0 comments on commit 990cf5c

Please sign in to comment.