summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/translation/LocalNameGenerator.java
blob: 18c966cdc49cec34e644ddf27f0f483277f9f731 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package cuchaz.enigma.translation;

import cuchaz.enigma.translation.mapping.NameValidator;
import cuchaz.enigma.translation.representation.TypeDescriptor;

import java.util.Collection;
import java.util.Locale;

public class LocalNameGenerator {
	public static String generateArgumentName(int index, TypeDescriptor desc, Collection<TypeDescriptor> arguments) {
		boolean uniqueType = arguments.stream().filter(desc::equals).count() <= 1;
		String translatedName;
		int nameIndex = index + 1;
		StringBuilder nameBuilder = new StringBuilder(getTypeName(desc));
		if (!uniqueType || NameValidator.isReserved(nameBuilder.toString())) {
			nameBuilder.append(nameIndex);
		}
		translatedName = nameBuilder.toString();
		return translatedName;
	}

	public static String generateLocalVariableName(int index, TypeDescriptor desc) {
		int nameIndex = index + 1;
		return getTypeName(desc) + nameIndex;
	}

	private static String getTypeName(TypeDescriptor desc) {
		// Unfortunately each of these have different name getters, so they have different code paths
		if (desc.isPrimitive()) {
			TypeDescriptor.Primitive argCls = desc.getPrimitive();
			return argCls.name().toLowerCase(Locale.ROOT);
		} else if (desc.isArray()) {
			// List types would require this whole block again, so just go with aListx
			return "arr";
		} else if (desc.isType()) {
			String typeName = desc.getTypeEntry().getSimpleName().replace("$", "");
			typeName = typeName.substring(0, 1).toLowerCase(Locale.ROOT) + typeName.substring(1);
			return typeName;
		} else {
			System.err.println("Encountered invalid argument type descriptor " + desc.toString());
			return "var";
		}
	}
}