| 1 | package net.minecraft.client.renderer;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import com.google.common.collect.ImmutableSet;
|
| 5 | import com.mojang.serialization.Codec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Map;
|
| 9 | import java.util.Set;
|
| 10 | import java.util.Map.Entry;
|
| 11 | import net.fabricmc.api.EnvType;
|
| 12 | import net.fabricmc.api.Environment;
|
| 13 |
|
| 14 | @Environment(EnvType.CLIENT)
|
| 15 | public record ShaderDefines(Map<String, String> values, Set<String> flags) {
|
| 16 | public static final ShaderDefines EMPTY = new ShaderDefines(Map.of(), Set.of());
|
| 17 | public static final Codec<ShaderDefines> CODEC = RecordCodecBuilder.create(
|
| 18 | i -> i.group(
|
| 19 | Codec.unboundedMap(Codec.STRING, Codec.STRING).optionalFieldOf("values", Map.of()).forGetter(ShaderDefines::values),
|
| 20 | Codec.STRING.listOf().xmap(Set::copyOf, List::copyOf).optionalFieldOf("flags", Set.of()).forGetter(ShaderDefines::flags)
|
| 21 | )
|
| 22 | .apply(i, ShaderDefines::new)
|
| 23 | );
|
| 24 |
|
| 25 | public static ShaderDefines.Builder builder() {
|
| 26 | return new ShaderDefines.Builder();
|
| 27 | }
|
| 28 |
|
| 29 | public ShaderDefines withOverrides(final ShaderDefines defines) {
|
| 30 | if (this.isEmpty()) {
|
| 31 | return defines;
|
| 32 | }
|
| 33 |
|
| 34 | if (defines.isEmpty()) {
|
| 35 | return this;
|
| 36 | }
|
| 37 |
|
| 38 | ImmutableMap.Builder<String, String> newValues = ImmutableMap.builderWithExpectedSize(this.values.size() + defines.values.size());
|
| 39 | newValues.putAll(this.values);
|
| 40 | newValues.putAll(defines.values);
|
| 41 | ImmutableSet.Builder<String> newFlags = ImmutableSet.builderWithExpectedSize(this.flags.size() + defines.flags.size());
|
| 42 | newFlags.addAll(this.flags);
|
| 43 | newFlags.addAll(defines.flags);
|
| 44 | return new ShaderDefines(newValues.buildKeepingLast(), newFlags.build());
|
| 45 | }
|
| 46 |
|
| 47 | public String asSourceDirectives() {
|
| 48 | StringBuilder directives = new StringBuilder();
|
| 49 |
|
| 50 | for (Entry<String, String> entry : this.values.entrySet()) {
|
| 51 | String key = entry.getKey();
|
| 52 | String value = entry.getValue();
|
| 53 | directives.append("#define ").append(key).append(" ").append(value).append('\n');
|
| 54 | }
|
| 55 |
|
| 56 | for (String flag : this.flags) {
|
| 57 | directives.append("#define ").append(flag).append('\n');
|
| 58 | }
|
| 59 |
|
| 60 | return directives.toString();
|
| 61 | }
|
| 62 |
|
| 63 | public boolean isEmpty() {
|
| 64 | return this.values.isEmpty() && this.flags.isEmpty();
|
| 65 | }
|
| 66 |
|
| 67 | @Environment(EnvType.CLIENT)
|
| 68 | public static class Builder {
|
| 69 | private final ImmutableMap.Builder<String, String> values = ImmutableMap.builder();
|
| 70 | private final ImmutableSet.Builder<String> flags = ImmutableSet.builder();
|
| 71 |
|
| 72 | private Builder() {
|
| 73 | }
|
| 74 |
|
| 75 | public ShaderDefines.Builder define(final String key, final String value) {
|
| 76 | if (value.isBlank()) {
|
| 77 | throw new IllegalArgumentException("Cannot define empty string");
|
| 78 | }
|
| 79 |
|
| 80 | this.values.put(key, escapeNewLines(value));
|
| 81 | return this;
|
| 82 | }
|
| 83 |
|
| 84 | private static String escapeNewLines(final String value) {
|
| 85 | return value.replaceAll("\n", "\\\\\n");
|
| 86 | }
|
| 87 |
|
| 88 | public ShaderDefines.Builder define(final String key, final float value) {
|
| 89 | this.values.put(key, String.valueOf(value));
|
| 90 | return this;
|
| 91 | }
|
| 92 |
|
| 93 | public ShaderDefines.Builder define(final String key, final int value) {
|
| 94 | this.values.put(key, String.valueOf(value));
|
| 95 | return this;
|
| 96 | }
|
| 97 |
|
| 98 | public ShaderDefines.Builder define(final String key) {
|
| 99 | this.flags.add(key);
|
| 100 | return this;
|
| 101 | }
|
| 102 |
|
| 103 | public ShaderDefines build() {
|
| 104 | return new ShaderDefines(this.values.build(), this.flags.build());
|
| 105 | }
|
| 106 | }
|
| 107 | }
|
| 108 |
|