package me.coley.recaf.mapping;

import me.coley.recaf.util.StringUtil;
import me.coley.recaf.workspace.Workspace;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Simple mappings file implementation where the old/new names are split by a space. The format of
 * the mappings matches the format outlined by
 * {@link org.objectweb.asm.commons.SimpleRemapper#SimpleRemapper(Map)}.
 *
 * @author Matt
 */
public class SimpleMappings extends FileMappings {
	/**
	 * Constructs mappings from a given file.
	 *
	 * @param file
	 * 		A file containing asm styled mappings.
	 * 		See {@link org.objectweb.asm.commons.SimpleRemapper#SimpleRemapper(Map)}.
	 * @param workspace
	 * 		Workspace to pull names from when using hierarchy lookups.
	 *
	 * @throws IOException
	 * 		Thrown if the file could not be read.
	 */
	SimpleMappings(File file, Workspace workspace) throws IOException {
		super(file, workspace);
	}

	@Override
	protected Map<String, String> parse(String text) {
		String[] lines = StringUtil.splitNewline(text);
		Map<String, String> map = new HashMap<>(lines.length);
		for(String line : lines) {
			String[] args = line.split(" ");
			map.put(args[0], args[1]);
		}
		return map;
	}
}

@@ -1134,16 +1134,17 @@
 );%0A%09%09for
+ 
 (String 
@@ -1166,41 +1166,296 @@
 %0A%09%09%09
-String%5B%5D args = line.split(%22 %22);%0A
+// Skip comments and empty lines%0A%09%09%09if (line.startsWith(%22#%22) %7C%7C line.trim().isEmpty())%0A%09%09%09%09continue;%0A%09%09%09String%5B%5D args = line.split(%22 %22);%0A%09%09%09if (args.length %3E 2) %7B%0A%09%09%09%09// Descriptor qualified field format%0A%09%09%09%09map.put(args%5B0%5D + %22 %22 + args%5B1%5D, args%5B2%5D);%0A%09%09%09%7D else %7B%0A%09%09%09%09// Any other format%0A%09
 %09%09%09m
@@ -1480,16 +1480,21 @@
 gs%5B1%5D);%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%09re

Allow simple mapper to support typed fields