File size: 3,844 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package net.minecraft.client.renderer;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import com.mojang.serialization.codecs.RecordCodecBuilder.Instance;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

@OnlyIn(Dist.CLIENT)
public record ShaderDefines(Map<String, String> values, Set<String> flags) {
    public static final ShaderDefines EMPTY = new ShaderDefines(Map.of(), Set.of());
    public static final Codec<ShaderDefines> CODEC = RecordCodecBuilder.create(
        p_369294_ -> p_369294_.group(
                    Codec.unboundedMap(Codec.STRING, Codec.STRING).optionalFieldOf("values", Map.of()).forGetter(ShaderDefines::values),
                    Codec.STRING.listOf().xmap(Set::copyOf, List::copyOf).optionalFieldOf("flags", Set.of()).forGetter(ShaderDefines::flags)
                )
                .apply(p_369294_, ShaderDefines::new)
    );

    public static ShaderDefines.Builder builder() {
        return new ShaderDefines.Builder();
    }

    public ShaderDefines withOverrides(ShaderDefines p_361942_) {
        if (this.isEmpty()) {
            return p_361942_;
        } else if (p_361942_.isEmpty()) {
            return this;
        } else {
            ImmutableMap.Builder<String, String> builder = ImmutableMap.builderWithExpectedSize(this.values.size() + p_361942_.values.size());
            builder.putAll(this.values);
            builder.putAll(p_361942_.values);
            ImmutableSet.Builder<String> builder1 = ImmutableSet.builderWithExpectedSize(this.flags.size() + p_361942_.flags.size());
            builder1.addAll(this.flags);
            builder1.addAll(p_361942_.flags);
            return new ShaderDefines(builder.buildKeepingLast(), builder1.build());
        }
    }

    public String asSourceDirectives() {
        StringBuilder stringbuilder = new StringBuilder();

        for (Entry<String, String> entry : this.values.entrySet()) {
            String s = entry.getKey();
            String s1 = entry.getValue();
            stringbuilder.append("#define ").append(s).append(" ").append(s1).append('\n');
        }

        for (String s2 : this.flags) {
            stringbuilder.append("#define ").append(s2).append('\n');
        }

        return stringbuilder.toString();
    }

    public boolean isEmpty() {
        return this.values.isEmpty() && this.flags.isEmpty();
    }

    @OnlyIn(Dist.CLIENT)
    public static class Builder {
        private final ImmutableMap.Builder<String, String> values = ImmutableMap.builder();
        private final ImmutableSet.Builder<String> flags = ImmutableSet.builder();

        Builder() {
        }

        public ShaderDefines.Builder define(String p_360918_, String p_368570_) {
            if (p_368570_.isBlank()) {
                throw new IllegalArgumentException("Cannot define empty string");
            } else {
                this.values.put(p_360918_, escapeNewLines(p_368570_));
                return this;
            }
        }

        private static String escapeNewLines(String p_363744_) {
            return p_363744_.replaceAll("\n", "\\\\\n");
        }

        public ShaderDefines.Builder define(String p_363194_, float p_365800_) {
            this.values.put(p_363194_, String.valueOf(p_365800_));
            return this;
        }

        public ShaderDefines.Builder define(String p_367054_) {
            this.flags.add(p_367054_);
            return this;
        }

        public ShaderDefines build() {
            return new ShaderDefines(this.values.build(), this.flags.build());
        }
    }
}