Skip to content

Commit a313f57

Browse files
author
IMS212
committed
Merge remote-tracking branch 'origin/trunk' into 1.17
# Conflicts: # src/main/java/net/coderbot/iris/gui/element/ShaderPackOptionList.java # src/main/java/net/coderbot/iris/gui/element/ShaderPackSelectionList.java
2 parents ab0d2bd + b39c278 commit a313f57

File tree

14 files changed

+712
-159
lines changed

14 files changed

+712
-159
lines changed

src/main/java/net/coderbot/iris/Iris.java

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.coderbot.iris;
22

33
import java.io.IOException;
4+
import java.io.OutputStream;
45
import java.nio.file.FileSystem;
56
import java.nio.file.FileSystemNotFoundException;
67
import java.nio.file.FileSystems;
@@ -22,10 +23,14 @@
2223
import net.coderbot.iris.pipeline.*;
2324
import net.coderbot.iris.pipeline.newshader.NewWorldRenderingPipeline;
2425
import net.coderbot.iris.shaderpack.DimensionId;
26+
import net.coderbot.iris.shaderpack.OptionalBoolean;
2527
import net.coderbot.iris.shaderpack.ProgramSet;
2628
import net.coderbot.iris.shaderpack.ShaderPack;
29+
import net.coderbot.iris.shaderpack.option.OptionSet;
2730
import net.coderbot.iris.shaderpack.option.Profile;
2831
import net.coderbot.iris.shaderpack.discovery.ShaderpackDirectoryManager;
32+
import net.coderbot.iris.shaderpack.option.values.MutableOptionValues;
33+
import net.coderbot.iris.shaderpack.option.values.OptionValues;
2934
import net.fabricmc.loader.api.ModContainer;
3035
import net.irisshaders.iris.api.v0.IrisApi;
3136
import net.minecraft.ChatFormatting;
@@ -69,6 +74,9 @@ public class Iris implements ClientModInitializer {
6974
private static KeyMapping shaderpackScreenKeybind;
7075

7176
private static final Map<String, String> shaderPackOptionQueue = new HashMap<>();
77+
// Flag variable used when reloading
78+
// Used in favor of queueDefaultShaderPackOptionValues() for resetting as the
79+
// behavior is more concrete and therefore is more likely to repair a user's issues
7280
private static boolean resetShaderPackOptions = false;
7381

7482
private static String IRIS_VERSION;
@@ -277,7 +285,7 @@ private static boolean loadExternalShaderpack(String name) {
277285
return false;
278286
}
279287

280-
Map<String, String> changedConfigs = loadConfigProperties(shaderPackConfigTxt)
288+
Map<String, String> changedConfigs = tryReadConfigProperties(shaderPackConfigTxt)
281289
.map(properties -> (Map<String, String>) (Map) properties)
282290
.orElse(new HashMap<>());
283291

@@ -289,12 +297,17 @@ private static boolean loadExternalShaderpack(String name) {
289297
}
290298
resetShaderPackOptions = false;
291299

292-
Properties configsToSave = new Properties();
293-
configsToSave.putAll(changedConfigs);
294-
saveConfigProperties(shaderPackConfigTxt, configsToSave);
295-
296300
try {
297301
currentPack = new ShaderPack(shaderPackPath, changedConfigs);
302+
303+
MutableOptionValues changedConfigsValues = currentPack.getShaderPackOptions().getOptionValues().mutableCopy();
304+
305+
// Store changed values from those currently in use by the shader pack
306+
Properties configsToSave = new Properties();
307+
changedConfigsValues.getBooleanValues().forEach((k, v) -> configsToSave.setProperty(k, Boolean.toString(v)));
308+
changedConfigsValues.getStringValues().forEach(configsToSave::setProperty);
309+
310+
tryUpdateConfigPropertiesFile(shaderPackConfigTxt, configsToSave);
298311
} catch (Exception e) {
299312
logger.error("Failed to load the shaderpack \"{}\"!", name);
300313
logger.catching(e);
@@ -359,24 +372,37 @@ private static void setShadersDisabled() {
359372
logger.info("Shaders are disabled");
360373
}
361374

362-
private static Optional<Properties> loadConfigProperties(Path path) {
375+
private static Optional<Properties> tryReadConfigProperties(Path path) {
363376
Properties properties = new Properties();
364377

365-
try {
366-
// NB: config properties are specified to be encoded with ISO-8859-1 by OptiFine,
367-
// so we don't need to do the UTF-8 workaround here.
368-
properties.load(Files.newInputStream(path));
369-
} catch (IOException e) {
370-
// TODO: Better error handling
371-
return Optional.empty();
378+
if (Files.exists(path)) {
379+
try {
380+
// NB: config properties are specified to be encoded with ISO-8859-1 by OptiFine,
381+
// so we don't need to do the UTF-8 workaround here.
382+
properties.load(Files.newInputStream(path));
383+
} catch (IOException e) {
384+
// TODO: Better error handling
385+
return Optional.empty();
386+
}
372387
}
373388

374389
return Optional.of(properties);
375390
}
376391

377-
private static void saveConfigProperties(Path path, Properties properties) {
392+
private static void tryUpdateConfigPropertiesFile(Path path, Properties properties) {
378393
try {
379-
properties.store(Files.newOutputStream(path), null);
394+
if (properties.isEmpty()) {
395+
// Delete the file or don't create it if there are no changed configs
396+
if (Files.exists(path)) {
397+
Files.delete(path);
398+
}
399+
400+
return;
401+
}
402+
403+
try (OutputStream out = Files.newOutputStream(path)) {
404+
properties.store(out, null);
405+
}
380406
} catch (IOException e) {
381407
// TODO: Better error handling
382408
}
@@ -426,6 +452,34 @@ public static void queueShaderPackOptionsFromProfile(Profile profile) {
426452
getShaderPackOptionQueue().putAll(profile.optionValues);
427453
}
428454

455+
public static void queueShaderPackOptionsFromProperties(Properties properties) {
456+
queueDefaultShaderPackOptionValues();
457+
458+
properties.stringPropertyNames().forEach(key ->
459+
getShaderPackOptionQueue().put(key, properties.getProperty(key)));
460+
}
461+
462+
// Used in favor of resetShaderPackOptions as the aforementioned requires the pack to be reloaded
463+
public static void queueDefaultShaderPackOptionValues() {
464+
clearShaderPackOptionQueue();
465+
466+
getCurrentPack().ifPresent(pack -> {
467+
OptionSet options = pack.getShaderPackOptions().getOptionSet();
468+
OptionValues values = pack.getShaderPackOptions().getOptionValues();
469+
470+
options.getStringOptions().forEach((key, mOpt) -> {
471+
if (values.getStringValue(key).isPresent()) {
472+
getShaderPackOptionQueue().put(key, mOpt.getOption().getDefaultValue());
473+
}
474+
});
475+
options.getBooleanOptions().forEach((key, mOpt) -> {
476+
if (values.getBooleanValue(key) != OptionalBoolean.DEFAULT) {
477+
getShaderPackOptionQueue().put(key, Boolean.toString(mOpt.getOption().getDefaultValue()));
478+
}
479+
});
480+
});
481+
}
482+
429483
public static void clearShaderPackOptionQueue() {
430484
getShaderPackOptionQueue().clear();
431485
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package net.coderbot.iris.gui;
2+
3+
import org.jetbrains.annotations.Nullable;
4+
import org.lwjgl.PointerBuffer;
5+
import org.lwjgl.system.MemoryStack;
6+
import org.lwjgl.util.tinyfd.TinyFileDialogs;
7+
8+
import java.nio.file.Path;
9+
import java.nio.file.Paths;
10+
import java.util.Optional;
11+
import java.util.concurrent.CompletableFuture;
12+
import java.util.concurrent.ExecutorService;
13+
import java.util.concurrent.Executors;
14+
15+
/**
16+
* Class used to make interfacing with {@link TinyFileDialogs} easier and asynchronous.
17+
*/
18+
public final class FileDialogUtil {
19+
private static final ExecutorService FILE_DIALOG_EXECUTOR = Executors.newSingleThreadExecutor();
20+
21+
private FileDialogUtil() {}
22+
23+
/**
24+
* Opens an asynchronous file select dialog window.
25+
*
26+
* @param dialog Whether to open a "save" dialog or an "open" dialog
27+
* @param title The title of the dialog window
28+
* @param origin The path that the window should start at
29+
* @param filterLabel A label used to describe what file extensions are allowed and their purpose
30+
* @param filters The file extension filters used by the dialog, each formatted as {@code "*.extension"}
31+
* @return a {@link CompletableFuture} which is completed once a file is selected or the dialog is cancelled.
32+
*/
33+
public static CompletableFuture<Optional<Path>> fileSelectDialog(DialogType dialog, String title, @Nullable Path origin, @Nullable String filterLabel, String ... filters) {
34+
CompletableFuture<Optional<Path>> future = new CompletableFuture<>();
35+
36+
FILE_DIALOG_EXECUTOR.submit(() -> {
37+
String result = null;
38+
39+
try (MemoryStack stack = MemoryStack.stackPush()) {
40+
PointerBuffer filterBuffer = stack.mallocPointer(filters.length);
41+
42+
for (String filter : filters) {
43+
filterBuffer.put(stack.UTF8(filter));
44+
}
45+
filterBuffer.flip();
46+
47+
String path = origin != null ? origin.toAbsolutePath().toString() : null;
48+
49+
if (dialog == DialogType.SAVE) {
50+
result = TinyFileDialogs.tinyfd_saveFileDialog(title, path, filterBuffer, filterLabel);
51+
} else if (dialog == DialogType.OPEN) {
52+
result = TinyFileDialogs.tinyfd_openFileDialog(title, path, filterBuffer, filterLabel, false);
53+
}
54+
}
55+
56+
future.complete(Optional.ofNullable(result).map(Paths::get));
57+
});
58+
59+
return future;
60+
}
61+
62+
public enum DialogType {
63+
SAVE, OPEN
64+
}
65+
}

src/main/java/net/coderbot/iris/gui/GuiUtil.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,11 @@ public static void playButtonClickSound() {
169169
public static class Icon {
170170
public static final Icon SEARCH = new Icon(0, 0, 7, 8);
171171
public static final Icon CLOSE = new Icon(7, 0, 5, 6);
172-
public static final Icon REFRESH = new Icon(12, 0, 11, 11);
172+
public static final Icon REFRESH = new Icon(12, 0, 10, 10);
173+
public static final Icon EXPORT = new Icon(22, 0, 7, 8);
174+
public static final Icon EXPORT_COLORED = new Icon(29, 0, 7, 8);
175+
public static final Icon IMPORT = new Icon(22, 8, 7, 8);
176+
public static final Icon IMPORT_COLORED = new Icon(29, 8, 7, 8);
173177

174178
private final int u;
175179
private final int v;
@@ -199,5 +203,13 @@ public void draw(PoseStack poseStack, int x, int y) {
199203
// Draw the texture to the screen
200204
GuiComponent.blit(poseStack, x, y, u, v, width, height, 256, 256);
201205
}
206+
207+
public int getWidth() {
208+
return width;
209+
}
210+
211+
public int getHeight() {
212+
return height;
213+
}
202214
}
203215
}

src/main/java/net/coderbot/iris/gui/NavigationController.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ public void refresh() {
5252
}
5353
}
5454

55+
public boolean hasHistory() {
56+
return this.history.size() > 0;
57+
}
58+
5559
public void setActiveOptionList(ShaderPackOptionList optionList) {
5660
this.optionList = optionList;
5761
}

0 commit comments

Comments
 (0)