-
-
Notifications
You must be signed in to change notification settings - Fork 745
Expand file tree
/
Copy pathIrisConfig.java
More file actions
231 lines (205 loc) · 7.05 KB
/
IrisConfig.java
File metadata and controls
231 lines (205 loc) · 7.05 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package net.irisshaders.iris.config;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import net.irisshaders.iris.Iris;
import net.irisshaders.iris.gui.option.IrisVideoSettings;
import net.irisshaders.iris.pathways.colorspace.ColorSpace;
import net.minecraft.resources.ResourceLocation;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
/**
* A class dedicated to storing the config values of shaderpacks. Right now it only stores the path to the current shaderpack
*/
public class IrisConfig {
private static final String COMMENT =
"This file stores configuration options for Iris, such as the currently active shaderpack";
private final Path propertiesPath;
private final Path excludedPath;
/**
* The path to the current shaderpack. Null if the internal shaderpack is being used.
*/
private String shaderPackName;
/**
* Whether or not shaders are used for rendering. False to disable all shader-based rendering, true to enable it.
*/
private boolean enableShaders;
/**
* Whether or not to allow core shaders to draw to the main color texture.
*/
private boolean allowUnknownShaders;
/**
* If debug features should be enabled. Gives much more detailed OpenGL error outputs at the cost of performance.
*/
private boolean enableDebugOptions;
/**
* What shaders should be nuked.
*/
private List<ResourceLocation> shadersToSkip = new ArrayList<>();
/**
* If the update notification should be disabled or not.
*/
private boolean disableUpdateMessage;
public IrisConfig(Path propertiesPath, Path excluded) {
shaderPackName = null;
enableShaders = false;
allowUnknownShaders = false;
enableDebugOptions = false;
disableUpdateMessage = false;
this.propertiesPath = propertiesPath;
this.excludedPath = excluded;
}
/**
* Initializes the configuration, loading it if it is present and creating a default config otherwise.
*
* @throws IOException file exceptions
*/
public void initialize() throws IOException {
load();
if (!Files.exists(propertiesPath)) {
save();
}
}
/**
* returns whether or not the current shaderpack is internal
*
* @return if the shaderpack is internal
*/
public boolean isInternal() {
return false;
}
/**
* Returns the name of the current shaderpack
*
* @return Returns the current shaderpack name - if internal shaders are being used it returns "(internal)"
*/
public Optional<String> getShaderPackName() {
return Optional.ofNullable(shaderPackName);
}
/**
* Sets the name of the current shaderpack
*/
public void setShaderPackName(String name) {
if (name == null || name.equals("(internal)") || name.isEmpty()) {
this.shaderPackName = null;
this.enableShaders = false;
} else {
this.shaderPackName = name;
}
}
/**
* Determines whether or not shaders are used for rendering.
*
* @return False to disable all shader-based rendering, true to enable shader-based rendering.
*/
public boolean areShadersEnabled() {
return enableShaders;
}
public boolean areDebugOptionsEnabled() {
return enableDebugOptions;
}
public boolean shouldDisableUpdateMessage() {
return disableUpdateMessage;
}
public void setDebugEnabled(boolean enabled) {
enableDebugOptions = enabled;
}
/**
* Sets whether shaders should be used for rendering.
*/
public void setShadersEnabled(boolean enabled) {
this.enableShaders = enabled;
}
private static Gson GSON = new Gson();
/**
* loads the config file and then populates the string, int, and boolean entries with the parsed entries
*
* @throws IOException if the file cannot be loaded
*/
public void load() throws IOException {
if (Files.exists(excludedPath)) {
JsonArray json = JsonParser.parseString(Files.readString(excludedPath)).getAsJsonObject().getAsJsonArray("excluded");
for (int i = 0; i < json.size(); i++) {
ResourceLocation resource = ResourceLocation.tryParse(json.get(i).getAsString());
if (resource == null) {
Iris.logger.warn("Unknown shader " + json.get(i).getAsString());
}
shadersToSkip.add(resource);
}
} else {
JsonObject defaultV = new JsonObject();
JsonArray array = new JsonArray();
array.add("put:valuesHere");
defaultV.add("excluded", array);
Files.writeString(excludedPath, GSON.toJson(defaultV));
}
if (!Files.exists(propertiesPath)) {
return;
}
Properties properties = new Properties();
// NB: This uses ISO-8859-1 with unicode escapes as the encoding
try (InputStream is = Files.newInputStream(propertiesPath)) {
properties.load(is);
}
shaderPackName = properties.getProperty("shaderPack");
enableShaders = !"false".equals(properties.getProperty("enableShaders"));
allowUnknownShaders = "true".equals(properties.getProperty("allowUnknownShaders"));
enableDebugOptions = "true".equals(properties.getProperty("enableDebugOptions"));
disableUpdateMessage = "true".equals(properties.getProperty("disableUpdateMessage"));
try {
IrisVideoSettings.shadowDistance = Integer.parseInt(properties.getProperty("maxShadowRenderDistance", "32"));
IrisVideoSettings.colorSpace = ColorSpace.valueOf(properties.getProperty("colorSpace", "SRGB"));
} catch (IllegalArgumentException e) {
Iris.logger.error("Shadow distance setting reset; value is invalid.");
IrisVideoSettings.shadowDistance = 32;
IrisVideoSettings.colorSpace = ColorSpace.SRGB;
save();
}
if (shaderPackName != null) {
if (shaderPackName.equals("(internal)") || shaderPackName.isEmpty()) {
shaderPackName = null;
}
}
if (shaderPackName == null) {
enableShaders = false;
}
}
/**
* Serializes the config into a file. Should be called whenever any config values are modified.
*
* @throws IOException file exceptions
*/
public void save() throws IOException {
Properties properties = new Properties();
properties.setProperty("shaderPack", getShaderPackName().orElse(""));
properties.setProperty("enableShaders", enableShaders ? "true" : "false");
properties.setProperty("allowUnknownShaders", allowUnknownShaders ? "true" : "false");
properties.setProperty("enableDebugOptions", enableDebugOptions ? "true" : "false");
properties.setProperty("disableUpdateMessage", disableUpdateMessage ? "true" : "false");
properties.setProperty("maxShadowRenderDistance", String.valueOf(IrisVideoSettings.shadowDistance));
properties.setProperty("colorSpace", IrisVideoSettings.colorSpace.name());
// NB: This uses ISO-8859-1 with unicode escapes as the encoding
try (OutputStream os = Files.newOutputStream(propertiesPath)) {
properties.store(os, COMMENT);
}
}
public boolean shouldAllowUnknownShaders() {
return allowUnknownShaders;
}
public boolean shouldSkip(ResourceLocation value) {
return shadersToSkip.contains(value); // TODO
}
public void setUnknown(boolean b) throws IOException {
this.allowUnknownShaders = b;
save();
}
}