Implement string sanitization for shader gen

This commit is contained in:
JFronny 2021-07-08 16:27:31 +02:00
parent 70e5e1d387
commit e7a0847adb
No known key found for this signature in database
GPG Key ID: BEC5ACBBD4EE17E5
4 changed files with 15 additions and 3 deletions

View File

@ -94,4 +94,16 @@ public class Respackopts implements ClientModInitializer {
}
}
}
public static String sanitizeString(String s) {
// This trims whitespace/underscores and removes non-alphabetical or underscore characters
// ^ = start of string
// $ = end of string
// * = zero or more times
// [\\s_] = whitespace or underscores
// | = or
// [^a-zA-Z_] = not character or underscore
return s.replaceAll("[^a-zA-Z_]|^[\\s_]*|[\\s_]*$", "");
}
}

View File

@ -83,7 +83,7 @@ public class ConfigBranch extends ConfigEntry<Map<String, ConfigEntry<?>>> {
@Override
public void buildShader(StringBuilder sb, String valueName) {
for (Map.Entry<String, ConfigEntry<?>> e : super.getValue().entrySet()) {
e.getValue().buildShader(sb, valueName + "_" + e.getKey());
e.getValue().buildShader(sb, valueName + "_" + Respackopts.sanitizeString(e.getKey()));
}
}

View File

@ -92,7 +92,7 @@ public class ConfigEnumEntry extends ConfigEntry<String> {
sb.append(' ');
sb.append(values.indexOf(getValue()));
for (int i = 0; i < values.size(); i++) {
String e2 = values.get(i);
String e2 = Respackopts.sanitizeString(values.get(i));
if (version == 1) {
sb.append("\n#define ");
sb.append(valueName);

View File

@ -15,7 +15,7 @@ public class FrexCompat implements FrexInitializer {
ShaderConfig.registerShaderConfigSupplier(new Identifier(Respackopts.ID, "config_supplier"), () -> {
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, ConfigBranch> e : Respackopts.CONFIG_BRANCH.entrySet()) {
e.getValue().buildShader(sb, e.getKey());
e.getValue().buildShader(sb, Respackopts.sanitizeString(e.getKey()));
}
sb.append("\n#define respackopts_loaded");
return sb.toString();