Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/trunk' into 1.17
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/net/coderbot/iris/gui/element/ShaderPackOptionList.java
#	src/main/java/net/coderbot/iris/gui/element/ShaderPackSelectionList.java
  • Loading branch information
IMS212 committed Jan 18, 2022
2 parents ab0d2bd + b39c278 commit a313f57
Show file tree
Hide file tree
Showing 14 changed files with 712 additions and 159 deletions.
84 changes: 69 additions & 15 deletions src/main/java/net/coderbot/iris/Iris.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.coderbot.iris;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.FileSystem;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.FileSystems;
Expand All @@ -22,10 +23,14 @@
import net.coderbot.iris.pipeline.*;
import net.coderbot.iris.pipeline.newshader.NewWorldRenderingPipeline;
import net.coderbot.iris.shaderpack.DimensionId;
import net.coderbot.iris.shaderpack.OptionalBoolean;
import net.coderbot.iris.shaderpack.ProgramSet;
import net.coderbot.iris.shaderpack.ShaderPack;
import net.coderbot.iris.shaderpack.option.OptionSet;
import net.coderbot.iris.shaderpack.option.Profile;
import net.coderbot.iris.shaderpack.discovery.ShaderpackDirectoryManager;
import net.coderbot.iris.shaderpack.option.values.MutableOptionValues;
import net.coderbot.iris.shaderpack.option.values.OptionValues;
import net.fabricmc.loader.api.ModContainer;
import net.irisshaders.iris.api.v0.IrisApi;
import net.minecraft.ChatFormatting;
Expand Down Expand Up @@ -69,6 +74,9 @@ public class Iris implements ClientModInitializer {
private static KeyMapping shaderpackScreenKeybind;

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

private static String IRIS_VERSION;
Expand Down Expand Up @@ -277,7 +285,7 @@ private static boolean loadExternalShaderpack(String name) {
return false;
}

Map<String, String> changedConfigs = loadConfigProperties(shaderPackConfigTxt)
Map<String, String> changedConfigs = tryReadConfigProperties(shaderPackConfigTxt)
.map(properties -> (Map<String, String>) (Map) properties)
.orElse(new HashMap<>());

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

Properties configsToSave = new Properties();
configsToSave.putAll(changedConfigs);
saveConfigProperties(shaderPackConfigTxt, configsToSave);

try {
currentPack = new ShaderPack(shaderPackPath, changedConfigs);

MutableOptionValues changedConfigsValues = currentPack.getShaderPackOptions().getOptionValues().mutableCopy();

// Store changed values from those currently in use by the shader pack
Properties configsToSave = new Properties();
changedConfigsValues.getBooleanValues().forEach((k, v) -> configsToSave.setProperty(k, Boolean.toString(v)));
changedConfigsValues.getStringValues().forEach(configsToSave::setProperty);

tryUpdateConfigPropertiesFile(shaderPackConfigTxt, configsToSave);
} catch (Exception e) {
logger.error("Failed to load the shaderpack \"{}\"!", name);
logger.catching(e);
Expand Down Expand Up @@ -359,24 +372,37 @@ private static void setShadersDisabled() {
logger.info("Shaders are disabled");
}

private static Optional<Properties> loadConfigProperties(Path path) {
private static Optional<Properties> tryReadConfigProperties(Path path) {
Properties properties = new Properties();

try {
// NB: config properties are specified to be encoded with ISO-8859-1 by OptiFine,
// so we don't need to do the UTF-8 workaround here.
properties.load(Files.newInputStream(path));
} catch (IOException e) {
// TODO: Better error handling
return Optional.empty();
if (Files.exists(path)) {
try {
// NB: config properties are specified to be encoded with ISO-8859-1 by OptiFine,
// so we don't need to do the UTF-8 workaround here.
properties.load(Files.newInputStream(path));
} catch (IOException e) {
// TODO: Better error handling
return Optional.empty();
}
}

return Optional.of(properties);
}

private static void saveConfigProperties(Path path, Properties properties) {
private static void tryUpdateConfigPropertiesFile(Path path, Properties properties) {
try {
properties.store(Files.newOutputStream(path), null);
if (properties.isEmpty()) {
// Delete the file or don't create it if there are no changed configs
if (Files.exists(path)) {
Files.delete(path);
}

return;
}

try (OutputStream out = Files.newOutputStream(path)) {
properties.store(out, null);
}
} catch (IOException e) {
// TODO: Better error handling
}
Expand Down Expand Up @@ -426,6 +452,34 @@ public static void queueShaderPackOptionsFromProfile(Profile profile) {
getShaderPackOptionQueue().putAll(profile.optionValues);
}

public static void queueShaderPackOptionsFromProperties(Properties properties) {
queueDefaultShaderPackOptionValues();

properties.stringPropertyNames().forEach(key ->
getShaderPackOptionQueue().put(key, properties.getProperty(key)));
}

// Used in favor of resetShaderPackOptions as the aforementioned requires the pack to be reloaded
public static void queueDefaultShaderPackOptionValues() {
clearShaderPackOptionQueue();

getCurrentPack().ifPresent(pack -> {
OptionSet options = pack.getShaderPackOptions().getOptionSet();
OptionValues values = pack.getShaderPackOptions().getOptionValues();

options.getStringOptions().forEach((key, mOpt) -> {
if (values.getStringValue(key).isPresent()) {
getShaderPackOptionQueue().put(key, mOpt.getOption().getDefaultValue());
}
});
options.getBooleanOptions().forEach((key, mOpt) -> {
if (values.getBooleanValue(key) != OptionalBoolean.DEFAULT) {
getShaderPackOptionQueue().put(key, Boolean.toString(mOpt.getOption().getDefaultValue()));
}
});
});
}

public static void clearShaderPackOptionQueue() {
getShaderPackOptionQueue().clear();
}
Expand Down
65 changes: 65 additions & 0 deletions src/main/java/net/coderbot/iris/gui/FileDialogUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package net.coderbot.iris.gui;

import org.jetbrains.annotations.Nullable;
import org.lwjgl.PointerBuffer;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.util.tinyfd.TinyFileDialogs;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* Class used to make interfacing with {@link TinyFileDialogs} easier and asynchronous.
*/
public final class FileDialogUtil {
private static final ExecutorService FILE_DIALOG_EXECUTOR = Executors.newSingleThreadExecutor();

private FileDialogUtil() {}

/**
* Opens an asynchronous file select dialog window.
*
* @param dialog Whether to open a "save" dialog or an "open" dialog
* @param title The title of the dialog window
* @param origin The path that the window should start at
* @param filterLabel A label used to describe what file extensions are allowed and their purpose
* @param filters The file extension filters used by the dialog, each formatted as {@code "*.extension"}
* @return a {@link CompletableFuture} which is completed once a file is selected or the dialog is cancelled.
*/
public static CompletableFuture<Optional<Path>> fileSelectDialog(DialogType dialog, String title, @Nullable Path origin, @Nullable String filterLabel, String ... filters) {
CompletableFuture<Optional<Path>> future = new CompletableFuture<>();

FILE_DIALOG_EXECUTOR.submit(() -> {
String result = null;

try (MemoryStack stack = MemoryStack.stackPush()) {
PointerBuffer filterBuffer = stack.mallocPointer(filters.length);

for (String filter : filters) {
filterBuffer.put(stack.UTF8(filter));
}
filterBuffer.flip();

String path = origin != null ? origin.toAbsolutePath().toString() : null;

if (dialog == DialogType.SAVE) {
result = TinyFileDialogs.tinyfd_saveFileDialog(title, path, filterBuffer, filterLabel);
} else if (dialog == DialogType.OPEN) {
result = TinyFileDialogs.tinyfd_openFileDialog(title, path, filterBuffer, filterLabel, false);
}
}

future.complete(Optional.ofNullable(result).map(Paths::get));
});

return future;
}

public enum DialogType {
SAVE, OPEN
}
}
14 changes: 13 additions & 1 deletion src/main/java/net/coderbot/iris/gui/GuiUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,11 @@ public static void playButtonClickSound() {
public static class Icon {
public static final Icon SEARCH = new Icon(0, 0, 7, 8);
public static final Icon CLOSE = new Icon(7, 0, 5, 6);
public static final Icon REFRESH = new Icon(12, 0, 11, 11);
public static final Icon REFRESH = new Icon(12, 0, 10, 10);
public static final Icon EXPORT = new Icon(22, 0, 7, 8);
public static final Icon EXPORT_COLORED = new Icon(29, 0, 7, 8);
public static final Icon IMPORT = new Icon(22, 8, 7, 8);
public static final Icon IMPORT_COLORED = new Icon(29, 8, 7, 8);

private final int u;
private final int v;
Expand Down Expand Up @@ -199,5 +203,13 @@ public void draw(PoseStack poseStack, int x, int y) {
// Draw the texture to the screen
GuiComponent.blit(poseStack, x, y, u, v, width, height, 256, 256);
}

public int getWidth() {
return width;
}

public int getHeight() {
return height;
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/net/coderbot/iris/gui/NavigationController.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public void refresh() {
}
}

public boolean hasHistory() {
return this.history.size() > 0;
}

public void setActiveOptionList(ShaderPackOptionList optionList) {
this.optionList = optionList;
}
Expand Down
Loading

0 comments on commit a313f57

Please sign in to comment.